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
SoXX 251647cc69 feat: added pool support
Signed-off-by: SoXX <soxx@fenpa.ws>
2023-10-19 15:19:40 +02:00

104 lines
3.1 KiB
Go

package endpoints
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"
)
func GetPool(client http.Client, requestContext model.RequestContext, ID string) (model.Pool, error) {
// Create a new HTTP GET request to fetch user information from the specified 'host' and 'username'.
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 User struct and the error.
log.Println(err)
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 := client.Do(r)
if err != nil {
// Log the error and return an empty User struct and the error.
log.Println(err)
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 User struct to store the response data.
var poolResponse model.Pool
// Decode the JSON response into the model.poolResponse{} struct.
err = json.NewDecoder(resp.Body).Decode(&poolResponse)
if err != nil {
// Log the error and return an empty User struct and the error.
log.Println(err)
return model.Pool{}, err
}
// Return the user information and no error (nil).
return poolResponse, nil
}
func GetPools(client http.Client, 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 {
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)
// Send the request using the provided HTTP client.
resp, err := client.Do(r)
if err != nil {
log.Print(err)
}
// Send the request using the provided HTTP client.
resp, err = client.Do(r)
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)
}
// Initialize a User struct to store the response data.
var poolResponse []model.Pool
// Decode the JSON response into the user struct.
err = json.NewDecoder(resp.Body).Decode(&poolResponse)
if err != nil {
// Log the error and return an empty User struct and the error.
log.Println(err)
return nil, err
}
return poolResponse, nil
}