From 7d780bc3e3021bb00cead44dbb0757bb61fee2b9 Mon Sep 17 00:00:00 2001 From: SoXX Date: Wed, 10 Jul 2024 21:43:18 +0200 Subject: [PATCH] feat: added low level implementation of DMails --- .gitignore | 2 + example/lowlevel/dmail.go | 57 ++++++++++++++++++++++ pkg/e621/endpoints/dmail.go | 96 +++++++++++++++++++++++++++++++++++++ pkg/e621/model/dmail.go | 16 +++++++ 4 files changed, 171 insertions(+) create mode 100644 example/lowlevel/dmail.go create mode 100644 pkg/e621/endpoints/dmail.go create mode 100644 pkg/e621/model/dmail.go diff --git a/.gitignore b/.gitignore index c200667..3d25383 100644 --- a/.gitignore +++ b/.gitignore @@ -190,3 +190,5 @@ $RECYCLE.BIN/ # End of https://www.toptal.com/developers/gitignore/api/windows,linux,goland+all,macos,go /.scannerwork/ + +*.env \ No newline at end of file diff --git a/example/lowlevel/dmail.go b/example/lowlevel/dmail.go new file mode 100644 index 0000000..53fb4c4 --- /dev/null +++ b/example/lowlevel/dmail.go @@ -0,0 +1,57 @@ +package main + +import ( + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + _ "github.com/joho/godotenv/autoload" + "log" + "net/http" + "os" +) + +func main() { + // Define the request context with essential information. + requestContext := model.RequestContext{ + Client: http.Client{}, + Host: "https://e621.net", + UserAgent: "Go-e621-SDK (@username)", + Username: os.Getenv("API_USER"), // Replace with your username + APIKey: os.Getenv("API_KEY"), // Replace with your API key + } + + // Log: Getting a single post. + log.Println("Getting single dm: ") + + // Specify the post ID for retrieval. + dMailID := 2062973 // Replace with the desired post's ID. + + // Call the GetPost function to retrieve the specified post. + dmail, err := endpoints.GetDmail(requestContext, dMailID) + + if err != nil { + log.Println(err) + } else { + // Log the URL of the retrieved post. + log.Println(dmail.Body) + } + log.Println("----------") + + // Log: Getting a list of posts. + log.Println("Getting a list of posts: ") + + // Define query parameters for retrieving a list of posts. + query := map[string]string{ + "limit": "5", + } + + // Call the GetPosts function to retrieve a list of posts based on the query parameters. + posts, err := endpoints.GetDmails(requestContext, query) + + if err != nil { + log.Println(err) + } else { + // Log the number of posts retrieved. + log.Println(len(posts)) + } + log.Println("----------") +} diff --git a/pkg/e621/endpoints/dmail.go b/pkg/e621/endpoints/dmail.go new file mode 100644 index 0000000..0b5eac6 --- /dev/null +++ b/pkg/e621/endpoints/dmail.go @@ -0,0 +1,96 @@ +package endpoints + +import ( + "encoding/json" + "fmt" + "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) { + // Create a new HTTP GET request to fetch the post information. + r, err := http.NewRequest("GET", 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 model.DMail{}, 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 model.DMail{}, 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 model.DMail{}, utils.StatusCodesToError(resp.StatusCode) + } + + // Initialize a Post struct to store the response data. + var postResponse model.DMail + + // Decode the JSON response into the PostResponse struct. + err = json.NewDecoder(resp.Body).Decode(&postResponse) + if err != nil { + // Log the error and return an empty Post struct and the error. + + return model.DMail{}, err + } + + // Return the post information and no error (nil). + return postResponse, 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) + if err != nil { + return nil, err + } + + // Append query parameters to the request URL. + q := r.URL.Query() + for k, v := range query { + q.Add(k, v) + } + r.URL.RawQuery = q.Encode() + + 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 { + return nil, 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 nil, utils.StatusCodesToError(resp.StatusCode) + } + + // Initialize a slice of Post struct to store the response data. + var postResponse []model.DMail + + // Decode the JSON response into the PostResponse struct. + err = json.NewDecoder(resp.Body).Decode(&postResponse) + if err != nil { + // Log the error and return an empty slice and the error. + + return nil, err + } + + // Return the list of posts and no error (nil). + return postResponse, nil +} diff --git a/pkg/e621/model/dmail.go b/pkg/e621/model/dmail.go new file mode 100644 index 0000000..294b99d --- /dev/null +++ b/pkg/e621/model/dmail.go @@ -0,0 +1,16 @@ +package model + +import "time" + +type DMail struct { + Body string `json:"body"` + CreatedAt time.Time `json:"created_at"` + FromId int `json:"from_id"` + Id int `json:"id"` + IsDeleted bool `json:"is_deleted"` + IsRead bool `json:"is_read"` + OwnerId int `json:"owner_id"` + Title string `json:"title"` + ToId int `json:"to_id"` + UpdatedAt time.Time `json:"updated_at"` +}