refactor: added docs & limit types

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2023-11-15 14:18:51 +01:00
parent 208b9a3baa
commit c6be78419c

View File

@ -8,7 +8,38 @@ import (
"net/http" "net/http"
) )
func getRequest[T any](requestContext model.RequestContext, e621Endpoint string, query map[string]string) (T, error) { 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 var base T
// Create a new HTTP GET request to fetch user information from the specified 'host' and 'username'. // 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) r, err := http.NewRequest("GET", fmt.Sprintf("%s/%s", requestContext.Host, e621Endpoint), nil)