2023-10-16 13:56:00 +00:00
|
|
|
package endpoints
|
|
|
|
|
2023-10-19 13:00:17 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-10-24 13:10:39 +00:00
|
|
|
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
|
|
|
|
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils"
|
2023-10-19 13:00:17 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// GetPost retrieves a single post by its ID from the e621 API.
|
|
|
|
//
|
2023-10-23 11:15:37 +00:00
|
|
|
// Parameters: // - requestContext: The context for the API request, including the host, user agent, username, and API key.
|
2023-10-19 14:13:38 +00:00
|
|
|
// - ID: The ID of the post to retrieve.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - model.Post: The retrieved post.
|
|
|
|
// - error: An error, if any, encountered during the API request or response handling.
|
2023-10-23 11:15:37 +00:00
|
|
|
func GetPost(requestContext model.RequestContext, ID string) (model.Post, error) {
|
2023-10-19 14:13:38 +00:00
|
|
|
// Create a new HTTP GET request to fetch the post information.
|
2023-10-19 13:00:17 +00:00
|
|
|
r, err := http.NewRequest("GET", fmt.Sprintf("%s/posts/%s.json", requestContext.Host, ID), nil)
|
|
|
|
if err != nil {
|
2023-10-19 14:13:38 +00:00
|
|
|
// Log the error and return an empty Post struct and the error.
|
2023-10-24 08:35:33 +00:00
|
|
|
|
2023-10-19 13:00:17 +00:00
|
|
|
return model.Post{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Header.Set("User-Agent", requestContext.UserAgent)
|
|
|
|
r.Header.Add("Accept", "application/json")
|
|
|
|
r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
|
|
|
|
|
2023-10-23 06:56:14 +00:00
|
|
|
// Send the request using the provided http.Client.
|
2023-10-23 11:15:37 +00:00
|
|
|
resp, err := requestContext.Client.Do(r)
|
2023-10-19 13:00:17 +00:00
|
|
|
if err != nil {
|
2023-10-19 14:13:38 +00:00
|
|
|
// Log the error and return an empty Post struct and the error.
|
2023-10-24 08:35:33 +00:00
|
|
|
|
2023-10-19 13:00:17 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// Initialize a Post struct to store the response data.
|
|
|
|
var postResponse model.PostResponse
|
2023-10-19 13:00:17 +00:00
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// Decode the JSON response into the PostResponse struct.
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&postResponse)
|
2023-10-19 13:00:17 +00:00
|
|
|
if err != nil {
|
2023-10-19 14:13:38 +00:00
|
|
|
// Log the error and return an empty Post struct and the error.
|
2023-10-24 08:35:33 +00:00
|
|
|
|
2023-10-19 13:00:17 +00:00
|
|
|
return model.Post{}, err
|
|
|
|
}
|
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// Return the post information and no error (nil).
|
|
|
|
return postResponse.Post, nil
|
2023-10-19 13:00:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// GetPosts retrieves a list of posts from the e621 API based on query parameters.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
|
|
|
|
// - query: A map containing additional query parameters for the API request.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - []model.Post: A slice of posts.
|
|
|
|
// - error: An error, if any, encountered during the API request or response handling.
|
2023-10-23 11:15:37 +00:00
|
|
|
func GetPosts(requestContext model.RequestContext, query map[string]string) ([]model.Post, error) {
|
2023-10-19 13:00:17 +00:00
|
|
|
// Create a new HTTP GET request.
|
|
|
|
r, err := http.NewRequest("GET", fmt.Sprintf("%s/posts.json", requestContext.Host), nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(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)
|
|
|
|
|
2023-10-23 06:56:14 +00:00
|
|
|
// Send the request using the provided http.Client.
|
2023-10-23 11:15:37 +00:00
|
|
|
resp, err := requestContext.Client.Do(r)
|
2023-10-19 13:00:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Print(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)
|
|
|
|
}
|
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// Initialize a slice of Post struct to store the response data.
|
2023-10-19 13:00:17 +00:00
|
|
|
var postResponse model.PostResponse
|
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// Decode the JSON response into the PostResponse struct.
|
2023-10-19 13:00:17 +00:00
|
|
|
err = json.NewDecoder(resp.Body).Decode(&postResponse)
|
|
|
|
if err != nil {
|
2023-10-19 14:13:38 +00:00
|
|
|
// Log the error and return an empty slice and the error.
|
2023-10-24 08:35:33 +00:00
|
|
|
|
2023-10-19 13:00:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// Return the list of posts and no error (nil).
|
2023-10-19 13:00:17 +00:00
|
|
|
return postResponse.Posts, nil
|
|
|
|
}
|