From 38cfa7c9cf01106bd8e431f7a875ce28020591a3 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 23 Oct 2023 10:21:25 +0200 Subject: [PATCH] refactor: moved http.Client to model.RequestContext Signed-off-by: SoXX --- example/lowlevel/user.go | 8 +++----- pkg/e621/endpoints/user.go | 8 ++++---- pkg/e621/endpoints/user_test.go | 8 ++++---- pkg/e621/model/basic.go | 3 +++ 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/example/lowlevel/user.go b/example/lowlevel/user.go index d121e7d..2d22582 100644 --- a/example/lowlevel/user.go +++ b/example/lowlevel/user.go @@ -10,6 +10,7 @@ import ( func main() { // Define the request context with essential information. requestContext := model.RequestContext{ + Client: http.Client{}, Host: "https://e621.net", UserAgent: "Go-e621-SDK (@username)", Username: "", // Replace with your username @@ -19,14 +20,11 @@ func main() { // Log: Getting a single user. log.Println("Getting a single user: ") - // Initialize an http.Client. - client := http.Client{} - // Specify the username for retrieval. username := "selloo" // Replace with the desired username. // Call the GetUser function to retrieve the specified user. - user, err := endpoints.GetUser(client, requestContext, username) + user, err := endpoints.GetUser(requestContext, username) if err != nil { log.Println(err) @@ -45,7 +43,7 @@ func main() { } // Call the GetUsers function to retrieve a list of users based on the query parameters. - userList, err := endpoints.GetUsers(client, requestContext, query) + userList, err := endpoints.GetUsers(requestContext, query) if err != nil { log.Println(err) diff --git a/pkg/e621/endpoints/user.go b/pkg/e621/endpoints/user.go index f2a25c7..b3636a9 100644 --- a/pkg/e621/endpoints/user.go +++ b/pkg/e621/endpoints/user.go @@ -19,7 +19,7 @@ import ( // Returns: // - model.User: The retrieved user. // - error: An error, if any, encountered during the API request or response handling. -func GetUser(client http.Client, requestContext model.RequestContext, username string) (model.User, error) { +func GetUser(requestContext model.RequestContext, username string) (model.User, error) { // Create a new HTTP GET request to fetch user information from the specified 'host' and 'username'. r, err := http.NewRequest("GET", fmt.Sprintf("%s/users/%s.json", requestContext.Host, username), nil) if err != nil { @@ -33,7 +33,7 @@ func GetUser(client http.Client, requestContext model.RequestContext, username s 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 User struct and the error. log.Println(err) @@ -71,7 +71,7 @@ func GetUser(client http.Client, requestContext model.RequestContext, username s // Returns: // - []model.User: A slice of users. // - error: An error, if any, encountered during the API request or response handling. -func GetUsers(client http.Client, requestContext model.RequestContext, query map[string]string) ([]model.User, error) { +func GetUsers(requestContext model.RequestContext, query map[string]string) ([]model.User, error) { // Create a new HTTP GET request. r, err := http.NewRequest("GET", fmt.Sprintf("%s/users.json", requestContext.Host), nil) if err != nil { @@ -90,7 +90,7 @@ func GetUsers(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) } diff --git a/pkg/e621/endpoints/user_test.go b/pkg/e621/endpoints/user_test.go index b81a09d..4e0a2a6 100644 --- a/pkg/e621/endpoints/user_test.go +++ b/pkg/e621/endpoints/user_test.go @@ -46,14 +46,14 @@ func TestGetUser(t *testing.T) { httpmock.RegisterResponder("GET", "https://e621.net/users/MaxMustermannDer69ste.json", jsonResponser) requestContext := model.RequestContext{ + Client: http.Client{}, Host: "https://e621.net", UserAgent: "Go-e621-SDK (@username)", Username: "memo", APIKey: "123456", } - client := http.Client{} - user, err := GetUser(client, requestContext, "MaxMustermannDer69ste") + user, err := GetUser(requestContext, "MaxMustermannDer69ste") if err != nil { t.Error(err) return @@ -135,14 +135,14 @@ func TestGetUsers(t *testing.T) { httpmock.RegisterResponder("GET", "https://e621.net/users.json", jsonResponser) requestContext := model.RequestContext{ + Client: http.Client{}, Host: "https://e621.net", UserAgent: "Go-e621-SDK (@username)", Username: "memo", APIKey: "123456", } - client := http.Client{} - user, err := GetUsers(client, requestContext, map[string]string{}) + user, err := GetUsers(requestContext, map[string]string{}) if err != nil { t.Error(err) return diff --git a/pkg/e621/model/basic.go b/pkg/e621/model/basic.go index 492b125..3e96e3a 100644 --- a/pkg/e621/model/basic.go +++ b/pkg/e621/model/basic.go @@ -1,6 +1,9 @@ package model +import "net/http" + type RequestContext struct { + Client http.Client Host string UserAgent string Username string