feat: added low level implementation of DMails

This commit is contained in:
SoXX 2024-07-10 21:43:18 +02:00
parent e2bce2bda7
commit 7d780bc3e3
4 changed files with 171 additions and 0 deletions

2
.gitignore vendored
View File

@ -190,3 +190,5 @@ $RECYCLE.BIN/
# End of https://www.toptal.com/developers/gitignore/api/windows,linux,goland+all,macos,go
/.scannerwork/
*.env

57
example/lowlevel/dmail.go Normal file
View File

@ -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("----------")
}

View File

@ -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
}

16
pkg/e621/model/dmail.go Normal file
View File

@ -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"`
}