This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
e621-sdk-go/pkg/e621/endpoints/post.go

107 lines
3.3 KiB
Go
Raw Normal View History

2023-10-16 13:56:00 +00:00
package endpoints
2023-10-19 12:20:05 +00:00
import (
"encoding/json"
"fmt"
"git.dragse.it/anthrove/e621-to-graph/internal/utils"
"git.dragse.it/anthrove/e621-to-graph/pkg/e621/model"
"log"
"net/http"
)
// GetPost allows you to get a post by ID
func GetPost(client http.Client, requestContext model.RequestContext, ID string) (model.Post, error) {
// Create a new HTTP GET request to fetch Tag information from the specified 'host' and 'ID'.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/posts/%s.json", requestContext.Host, ID), nil)
if err != nil {
// Log the error and return an empty Tag struct and the error.
log.Println(err)
return model.Post{}, 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 := client.Do(r)
if err != nil {
// Log the error and return an empty Tag struct and the error.
log.Println(err)
return model.Post{}, 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.Post{}, utils.StatusCodesToError(resp.StatusCode)
}
// Initialize a Tag struct to store the response data.
var post model.PostResponse
// Decode the JSON response into the Tag struct.
err = json.NewDecoder(resp.Body).Decode(&post)
if err != nil {
// Log the error and return an empty Tag struct and the error.
log.Println(err)
return model.Post{}, err
}
// Return the Tag information and no error (nil).
return post.Post, nil
}
// GetPosts allows you to get a list of posts
func GetPosts(client http.Client, requestContext model.RequestContext, query map[string]string) ([]model.Post, error) {
// Create a new HTTP GET request to fetch Tag information from the specified 'host' and 'ID'.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/posts.json", requestContext.Host), nil)
if err != nil {
// Log the error and return an empty Tag struct and the error.
log.Println(err)
return nil, err
}
r.Header.Set("User-Agent", requestContext.UserAgent)
r.Header.Add("Accept", "application/json")
r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
// 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()
// Send the request using the provided HTTP client.
resp, err := client.Do(r)
if err != nil {
// Log the error and return an empty Tag struct and the error.
log.Println(err)
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 Tag struct to store the response data.
var posts model.PostResponse
// Decode the JSON response into the Tag struct.
err = json.NewDecoder(resp.Body).Decode(&posts)
if err != nil {
// Log the error and return an empty Tag struct and the error.
log.Println(err)
return nil, err
}
// Return the Tag information and no error (nil).
return posts.Posts, nil
}