feat: added low level implementation of DMails (deletion and read all)

This commit is contained in:
SoXX 2024-07-11 09:41:22 +02:00
parent 7d780bc3e3
commit 467c951720
2 changed files with 81 additions and 2 deletions

View File

@ -41,7 +41,7 @@ func main() {
// Define query parameters for retrieving a list of posts. // Define query parameters for retrieving a list of posts.
query := map[string]string{ query := map[string]string{
"limit": "5", "title_matches": "test",
} }
// Call the GetPosts function to retrieve a list of posts based on the query parameters. // Call the GetPosts function to retrieve a list of posts based on the query parameters.
@ -51,7 +51,31 @@ func main() {
log.Println(err) log.Println(err)
} else { } else {
// Log the number of posts retrieved. // 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("----------") log.Println("----------")
} }

View File

@ -50,6 +50,36 @@ func GetDmail(requestContext model.RequestContext, ID int) (model.DMail, error)
return postResponse, nil 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) { func GetDmails(requestContext model.RequestContext, query map[string]string) ([]model.DMail, error) {
// Create a new HTTP GET request. // Create a new HTTP GET request.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/dmails.json", requestContext.Host), nil) 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 the list of posts and no error (nil).
return postResponse, 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
}