From 5c9266edb5084f8539a1238b2be0c33639ee9727 Mon Sep 17 00:00:00 2001 From: David Janowski Date: Thu, 11 Jul 2024 12:21:40 +0200 Subject: [PATCH] feat: finished low level implementation of DMs --- pkg/e621/endpoints/dmail.go | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/pkg/e621/endpoints/dmail.go b/pkg/e621/endpoints/dmail.go index 260024e..42ccd87 100644 --- a/pkg/e621/endpoints/dmail.go +++ b/pkg/e621/endpoints/dmail.go @@ -3,9 +3,11 @@ package endpoints import ( "encoding/json" "fmt" + "log" + "net/http" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" - "net/http" ) func GetDmail(requestContext model.RequestContext, ID int) (model.DMail, error) { @@ -80,7 +82,33 @@ func DeleateDmail(requestContext model.RequestContext, ID int) error { return nil } -func GetDmails(requestContext model.RequestContext, query map[string]string) ([]model.DMail, error) { +func MarkAsReadDmail(requestContext model.RequestContext, ID int) error { + // Create a new HTTP GET request to fetch the post information. + r, err := http.NewRequest("PUT", fmt.Sprintf("%s/dmails/%d/mark_as_read.json", requestContext.Host, ID), nil) + log.Print(r.URL) + + 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 GetAllDmails(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) if err != nil { @@ -125,7 +153,7 @@ func GetDmails(requestContext model.RequestContext, query map[string]string) ([] return postResponse, nil } -func ReadAllDmails(requestContext model.RequestContext) error { +func MarkAsReadAllDmails(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)