From 467c9517200b8ffa234291e5125bdae7c664f8c6 Mon Sep 17 00:00:00 2001 From: SoXX Date: Thu, 11 Jul 2024 09:41:22 +0200 Subject: [PATCH] feat: added low level implementation of DMails (deletion and read all) --- example/lowlevel/dmail.go | 28 +++++++++++++++++-- pkg/e621/endpoints/dmail.go | 55 +++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/example/lowlevel/dmail.go b/example/lowlevel/dmail.go index 53fb4c4..730629c 100644 --- a/example/lowlevel/dmail.go +++ b/example/lowlevel/dmail.go @@ -41,7 +41,7 @@ func main() { // Define query parameters for retrieving a list of posts. query := map[string]string{ - "limit": "5", + "title_matches": "test", } // Call the GetPosts function to retrieve a list of posts based on the query parameters. @@ -51,7 +51,31 @@ func main() { log.Println(err) } else { // Log the number of posts retrieved. - log.Println(len(posts)) + log.Println(posts[0].Body) + } + log.Println("----------") + + log.Println("Mark all DMails as read: ") + + // Call the ReadAllDmails function to set all DMails as read. + err = endpoints.ReadAllDmails(requestContext) + + if err != nil { + log.Println(err) + } else { + log.Println("read all posts") + } + log.Println("----------") + + log.Println("Delete all DMails as read: ") + + // Call the ReadAllDmails function to set all DMails as read. + err = endpoints.DeleateDmail(requestContext, dMailID) + + if err != nil { + log.Println(err) + } else { + log.Println("DMail deleted") } log.Println("----------") } diff --git a/pkg/e621/endpoints/dmail.go b/pkg/e621/endpoints/dmail.go index 0b5eac6..260024e 100644 --- a/pkg/e621/endpoints/dmail.go +++ b/pkg/e621/endpoints/dmail.go @@ -50,6 +50,36 @@ func GetDmail(requestContext model.RequestContext, ID int) (model.DMail, error) return postResponse, nil } +func DeleateDmail(requestContext model.RequestContext, ID int) error { + // Create a new HTTP GET request to fetch the post information. + r, err := http.NewRequest("DELETE", fmt.Sprintf("%s/dmails/%d.json", requestContext.Host, ID), nil) + if err != nil { + // Log the error and return an empty Post struct and the error. + return err + } + + r.Header.Set("User-Agent", requestContext.UserAgent) + r.Header.Add("Accept", "application/json") + r.SetBasicAuth(requestContext.Username, requestContext.APIKey) + + // Send the request using the provided http.Client. + resp, err := requestContext.Client.Do(r) + if err != nil { + // Log the error and return an empty Post struct and the error. + + return err + } + + // Check if the HTTP response status code indicates success (2xx range). + if resp.StatusCode < 200 || resp.StatusCode > 300 { + // If the status code is outside the 2xx range, return an error based on the status code. + return utils.StatusCodesToError(resp.StatusCode) + } + + // Return the post information and no error (nil). + return nil +} + func GetDmails(requestContext model.RequestContext, query map[string]string) ([]model.DMail, error) { // Create a new HTTP GET request. r, err := http.NewRequest("GET", fmt.Sprintf("%s/dmails.json", requestContext.Host), nil) @@ -94,3 +124,28 @@ func GetDmails(requestContext model.RequestContext, query map[string]string) ([] // Return the list of posts and no error (nil). return postResponse, nil } + +func ReadAllDmails(requestContext model.RequestContext) error { + // Create a new HTTP GET request to fetch the post information. + r, err := http.NewRequest("PUT", fmt.Sprintf("%s/dmails/mark_all_as_read.json", requestContext.Host), nil) + + r.Header.Set("User-Agent", requestContext.UserAgent) + r.Header.Add("Accept", "application/json") + r.SetBasicAuth(requestContext.Username, requestContext.APIKey) + + // Send the request using the provided http.Client. + resp, err := requestContext.Client.Do(r) + if err != nil { + // Log the error and return an empty Post struct and the error. + return err + } + + // Check if the HTTP response status code indicates success (2xx range). + if resp.StatusCode < 200 || resp.StatusCode > 300 { + // If the status code is outside the 2xx range, return an error based on the status code. + return utils.StatusCodesToError(resp.StatusCode) + } + + // Return the post information and no error (nil). + return nil +}