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/executer.go
SoXX c6be78419c refactor: added docs & limit types
Signed-off-by: SoXX <soxx@fenpa.ws>
2023-11-15 14:18:51 +01:00

90 lines
2.9 KiB
Go

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"
)
type RequestTypes interface {
model.User |
model.Tag |
model.Post |
model.PostResponse |
model.Pool |
model.Note |
[]model.User |
[]model.Tag |
[]model.Post |
[]model.Pool |
[]model.Note
}
// getRequest performs an HTTP GET request to the specified e621 API endpoint and unmarshals the JSON response into the provided type T.
//
// This generic function is designed to fetch data from the e621 API using an HTTP GET request.
// It supports generic types and can unmarshal the JSON response into the provided type T.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
// - e621Endpoint: The specific e621 API endpoint to be called.
// - query: A map containing additional query parameters for the API request. (Optional)
//
// Type Parameters:
// - T: The type into which the JSON response will be unmarshaled.
// - supported types are (also as slice): model.User, model.Tag, model.Post, model.PostResponse, model.Pool, model.Note
//
// Returns:
// - T: The result of unmarshaling the JSON response into the provided type.
// - error: An error, if any, encountered during the API request, response handling, or JSON unmarshaling.
func getRequest[T RequestTypes](requestContext model.RequestContext, e621Endpoint string, query map[string]string) (T, error) {
var base T
// Create a new HTTP GET request to fetch user information from the specified 'host' and 'username'.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/%s", requestContext.Host, e621Endpoint), nil)
if err != nil {
// Log the error and return an empty User struct and the error.
return base, err
}
// Append query parameters to the request URL.
if query != nil {
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 {
// Log the error and return an empty User struct and the error.
return base, 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 base, utils.StatusCodesToError(resp.StatusCode)
}
// Decode the JSON response into the User struct.
err = json.NewDecoder(resp.Body).Decode(&base)
if err != nil {
// Log the error and return an empty User struct and the error.
return base, err
}
// Return the user information and no error (nil).
return base, nil
}