From c6be78419c70e86e3b954a7f227e54624c4f3e1f Mon Sep 17 00:00:00 2001 From: SoXX Date: Wed, 15 Nov 2023 14:18:51 +0100 Subject: [PATCH] refactor: added docs & limit types Signed-off-by: SoXX --- pkg/e621/endpoints/executer.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/pkg/e621/endpoints/executer.go b/pkg/e621/endpoints/executer.go index ee5065f..917a675 100644 --- a/pkg/e621/endpoints/executer.go +++ b/pkg/e621/endpoints/executer.go @@ -8,7 +8,38 @@ import ( "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 // 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)