From 7acff845fdbf3e32348ff95a152ce77eb8766d0f Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 23 Oct 2023 13:15:37 +0200 Subject: [PATCH] refactor: moved http.Client to model.RequestContext Signed-off-by: SoXX --- example/lowlevel/post.go | 7 ++----- pkg/e621/endpoints/post.go | 13 +++++-------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/example/lowlevel/post.go b/example/lowlevel/post.go index e7e3fb0..b8dfdfb 100644 --- a/example/lowlevel/post.go +++ b/example/lowlevel/post.go @@ -22,14 +22,11 @@ func main() { // Log: Getting a single post. log.Println("Getting single post: ") - // Initialize an http.Client. - client := http.Client{} - // Specify the post ID for retrieval. postID := "4353480" // Replace with the desired post's ID. // Call the GetPost function to retrieve the specified post. - post, err := endpoints.GetPost(client, requestContext, postID) + post, err := endpoints.GetPost(requestContext, postID) if err != nil { log.Println(err) @@ -48,7 +45,7 @@ func main() { } // Call the GetPosts function to retrieve a list of posts based on the query parameters. - posts, err := endpoints.GetPosts(client, requestContext, query) + posts, err := endpoints.GetPosts(requestContext, query) if err != nil { log.Println(err) diff --git a/pkg/e621/endpoints/post.go b/pkg/e621/endpoints/post.go index d26dcfb..19e69e1 100644 --- a/pkg/e621/endpoints/post.go +++ b/pkg/e621/endpoints/post.go @@ -11,15 +11,13 @@ import ( // GetPost retrieves a single post 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. +// Parameters: // - requestContext: The context for the API request, including the host, user agent, username, and API key. // - ID: The ID of the post to retrieve. // // Returns: // - model.Post: The retrieved post. // - error: An error, if any, encountered during the API request or response handling. -func GetPost(client http.Client, requestContext model.RequestContext, ID string) (model.Post, error) { +func GetPost(requestContext model.RequestContext, ID string) (model.Post, error) { // Create a new HTTP GET request to fetch the post information. r, err := http.NewRequest("GET", fmt.Sprintf("%s/posts/%s.json", requestContext.Host, ID), nil) if err != nil { @@ -33,7 +31,7 @@ func GetPost(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 Post struct and the error. log.Println(err) @@ -64,14 +62,13 @@ func GetPost(client http.Client, requestContext model.RequestContext, ID string) // GetPosts retrieves a list of posts 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.Post: A slice of posts. // - error: An error, if any, encountered during the API request or response handling. -func GetPosts(client http.Client, requestContext model.RequestContext, query map[string]string) ([]model.Post, error) { +func GetPosts(requestContext model.RequestContext, query map[string]string) ([]model.Post, error) { // Create a new HTTP GET request. r, err := http.NewRequest("GET", fmt.Sprintf("%s/posts.json", requestContext.Host), nil) if err != nil { @@ -90,7 +87,7 @@ func GetPosts(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) }