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/pool.go

115 lines
3.8 KiB
Go
Raw Permalink Normal View History

2024-07-11 11:10:30 +00:00
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"
)
// GetPool retrieves a pool by its ID from the e621 API.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
// - ID: The ID of the pool to retrieve.
//
// Returns:
// - model.Pool: The retrieved pool.
// - error: An error, if any, encountered during the API request or response handling.
func GetPool(requestContext model.RequestContext, ID string) (model.Pool, error) {
// Create a new HTTP GET request to fetch the pool information.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/pools/%s.json", requestContext.Host, ID), nil)
if err != nil {
// Log the error and return an empty Pool struct and the error.
return model.Pool{}, 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 Pool struct and the error.
return model.Pool{}, 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.Pool{}, utils.StatusCodesToError(resp.StatusCode)
}
// Initialize a Pool struct to store the response data.
var poolResponse model.Pool
// Decode the JSON response into the Pool struct.
err = json.NewDecoder(resp.Body).Decode(&poolResponse)
if err != nil {
// Log the error and return an empty Pool struct and the error.
return model.Pool{}, err
}
// Return the pool information and no error (nil).
return poolResponse, nil
}
// GetPools retrieves a list of pools 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.Pool: A slice of pools.
// - error: An error, if any, encountered during the API request or response handling.
func GetPools(requestContext model.RequestContext, query map[string]string) ([]model.Pool, error) {
// Create a new HTTP GET request.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/pools.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 Pool struct to store the response data.
var poolsResponse []model.Pool
// Decode the JSON response into the slice of Pool structs.
err = json.NewDecoder(resp.Body).Decode(&poolsResponse)
if err != nil {
// Log the error and return an empty slice and the error.
return nil, err
}
// Return the list of pools and no error (nil).
return poolsResponse, nil
}