From 443e7d14ed2a73326576cffd38df9dc2282eb045 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 23 Oct 2023 15:35:14 +0200 Subject: [PATCH] refactor: moved http.Client to model.RequestContext Signed-off-by: SoXX --- example/lowlevel/note.go | 7 ++----- pkg/e621/endpoints/note.go | 19 ++++++++++++------- pkg/e621/endpoints/note_test.go | 8 ++++---- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/example/lowlevel/note.go b/example/lowlevel/note.go index 405dfce..e1cd333 100644 --- a/example/lowlevel/note.go +++ b/example/lowlevel/note.go @@ -22,14 +22,11 @@ func main() { // Log: Getting a single note. log.Println("Getting single note: ") - // Initialize an http.Client. - client := http.Client{} - // Specify the note ID for retrieval. noteID := "36957" // Replace with the desired note's ID. // Call the GetNote function to retrieve the specified note. - note, err := endpoints.GetNote(client, requestContext, noteID) + note, err := endpoints.GetNote(requestContext, noteID) if err != nil { log.Println(err) @@ -48,7 +45,7 @@ func main() { } // Call the GetNotes function to retrieve a list of notes based on the query parameters. - notes, err := endpoints.GetNotes(client, requestContext, query) + notes, err := endpoints.GetNotes(requestContext, query) if err != nil { log.Println(err) diff --git a/pkg/e621/endpoints/note.go b/pkg/e621/endpoints/note.go index 83a6f2d..c99c7d1 100644 --- a/pkg/e621/endpoints/note.go +++ b/pkg/e621/endpoints/note.go @@ -5,21 +5,22 @@ import ( "fmt" "git.dragse.it/anthrove/e621-to-graph/internal/utils" "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "io" "log" "net/http" + "strings" ) // GetNote retrieves a single note 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 note to retrieve. // // Returns: // - model.Note: The retrieved note. // - error: An error, if any, encountered during the API request or response handling. -func GetNote(client http.Client, requestContext model.RequestContext, ID string) (model.Note, error) { +func GetNote(requestContext model.RequestContext, ID string) (model.Note, error) { // Create a new HTTP GET request to fetch the note information. r, err := http.NewRequest("GET", fmt.Sprintf("%s/notes/%s.json", requestContext.Host, ID), nil) if err != nil { @@ -33,7 +34,7 @@ func GetNote(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 Note struct and the error. log.Println(err) @@ -64,14 +65,13 @@ func GetNote(client http.Client, requestContext model.RequestContext, ID string) // GetNotes retrieves a list of notes 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.Note: A slice of notes. // - error: An error, if any, encountered during the API request or response handling. -func GetNotes(client http.Client, requestContext model.RequestContext, query map[string]string) ([]model.Note, error) { +func GetNotes(requestContext model.RequestContext, query map[string]string) ([]model.Note, error) { // Create a new HTTP GET request. r, err := http.NewRequest("GET", fmt.Sprintf("%s/notes.json", requestContext.Host), nil) if err != nil { @@ -90,7 +90,7 @@ func GetNotes(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) } @@ -101,11 +101,16 @@ func GetNotes(client http.Client, requestContext model.RequestContext, query map return nil, utils.StatusCodesToError(resp.StatusCode) } + respBodyBytes, err := io.ReadAll(resp.Body) + if strings.Contains(string(respBodyBytes), "{\"notes\":[]}") { + return nil, nil + } + // Initialize a slice of Note struct to store the response data. var notesResponse []model.Note // Decode the JSON response into the slice of Note structs. - err = json.NewDecoder(resp.Body).Decode(¬esResponse) + err = json.Unmarshal(respBodyBytes, ¬esResponse) if err != nil { // Log the error and return an empty slice and the error. log.Println(err) diff --git a/pkg/e621/endpoints/note_test.go b/pkg/e621/endpoints/note_test.go index 1c639a5..5b036e1 100644 --- a/pkg/e621/endpoints/note_test.go +++ b/pkg/e621/endpoints/note_test.go @@ -35,14 +35,14 @@ func TestGetNote(t *testing.T) { httpmock.RegisterResponder("GET", "https://e621.net/notes/36957.json", jsonResponser) requestContext := model.RequestContext{ + Client: http.Client{}, Host: "https://e621.net", UserAgent: "Go-e621-SDK (@username)", Username: "memo", APIKey: "123456", } - client := http.Client{} - pool, err := GetNote(client, requestContext, "36957") + pool, err := GetNote(requestContext, "36957") if err != nil { t.Error(err) return @@ -116,14 +116,14 @@ func TestGetNotes(t *testing.T) { httpmock.RegisterResponder("GET", "https://e621.net/notes.json", jsonResponser) requestContext := model.RequestContext{ + Client: http.Client{}, Host: "https://e621.net", UserAgent: "Go-e621-SDK (@username)", Username: "memo", APIKey: "123456", } - client := http.Client{} - pools, err := GetNotes(client, requestContext, map[string]string{}) + pools, err := GetNotes(requestContext, map[string]string{}) if err != nil { t.Error(err) return