refactor: moved http.Client to model.RequestContext

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2023-10-23 13:19:16 +02:00
parent e52735fe26
commit 50e8cc24cb

View File

@ -12,14 +12,13 @@ import (
// GetPool retrieves a pool by its ID from the e621 API.
//
// Parameters:
// - client: An http.Client used to make the API request.
// - 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(client http.Client, requestContext model.RequestContext, ID string) (model.Pool, error) {
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 {
@ -33,7 +32,7 @@ func GetPool(client http.Client, requestContext model.RequestContext, ID string)
r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
// Send the request using the provided http.Client.
resp, err := client.Do(r)
resp, err := requestContext.Client.Do(r)
if err != nil {
// Log the error and return an empty Pool struct and the error.
log.Println(err)
@ -64,14 +63,13 @@ func GetPool(client http.Client, requestContext model.RequestContext, ID string)
// GetPools retrieves a list of pools from the e621 API based on query parameters.
//
// Parameters:
// - client: An http.Client used to make the API request.
// - 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(client http.Client, requestContext model.RequestContext, query map[string]string) ([]model.Pool, error) {
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 {
@ -90,7 +88,7 @@ func GetPools(client http.Client, requestContext model.RequestContext, query map
r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
// Send the request using the provided http.Client.
resp, err := client.Do(r)
resp, err := requestContext.Client.Do(r)
if err != nil {
log.Print(err)
}