refactor: moved http.Client to model.RequestContext

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2023-10-23 15:35:14 +02:00
parent 989f853d72
commit 443e7d14ed
3 changed files with 18 additions and 16 deletions

View File

@ -22,14 +22,11 @@ func main() {
// Log: Getting a single note. // Log: Getting a single note.
log.Println("Getting single note: ") log.Println("Getting single note: ")
// Initialize an http.Client.
client := http.Client{}
// Specify the note ID for retrieval. // Specify the note ID for retrieval.
noteID := "36957" // Replace with the desired note's ID. noteID := "36957" // Replace with the desired note's ID.
// Call the GetNote function to retrieve the specified note. // 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 { if err != nil {
log.Println(err) log.Println(err)
@ -48,7 +45,7 @@ func main() {
} }
// Call the GetNotes function to retrieve a list of notes based on the query parameters. // 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 { if err != nil {
log.Println(err) log.Println(err)

View File

@ -5,21 +5,22 @@ import (
"fmt" "fmt"
"git.dragse.it/anthrove/e621-to-graph/internal/utils" "git.dragse.it/anthrove/e621-to-graph/internal/utils"
"git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model"
"io"
"log" "log"
"net/http" "net/http"
"strings"
) )
// GetNote retrieves a single note by its ID from the e621 API. // GetNote retrieves a single note by its ID from the e621 API.
// //
// 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. // - requestContext: The context for the API request, including the host, user agent, username, and API key.
// - ID: The ID of the note to retrieve. // - ID: The ID of the note to retrieve.
// //
// Returns: // Returns:
// - model.Note: The retrieved note. // - model.Note: The retrieved note.
// - error: An error, if any, encountered during the API request or response handling. // - 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. // 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) r, err := http.NewRequest("GET", fmt.Sprintf("%s/notes/%s.json", requestContext.Host, ID), nil)
if err != nil { if err != nil {
@ -33,7 +34,7 @@ func GetNote(client http.Client, requestContext model.RequestContext, ID string)
r.SetBasicAuth(requestContext.Username, requestContext.APIKey) r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
// Send the request using the provided http.Client. // Send the request using the provided http.Client.
resp, err := client.Do(r) resp, err := requestContext.Client.Do(r)
if err != nil { if err != nil {
// Log the error and return an empty Note struct and the error. // Log the error and return an empty Note struct and the error.
log.Println(err) 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. // GetNotes retrieves a list of notes from the e621 API based on query parameters.
// //
// 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. // - 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. // - query: A map containing additional query parameters for the API request.
// //
// Returns: // Returns:
// - []model.Note: A slice of notes. // - []model.Note: A slice of notes.
// - error: An error, if any, encountered during the API request or response handling. // - 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. // Create a new HTTP GET request.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/notes.json", requestContext.Host), nil) r, err := http.NewRequest("GET", fmt.Sprintf("%s/notes.json", requestContext.Host), nil)
if err != nil { if err != nil {
@ -90,7 +90,7 @@ func GetNotes(client http.Client, requestContext model.RequestContext, query map
r.SetBasicAuth(requestContext.Username, requestContext.APIKey) r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
// Send the request using the provided http.Client. // Send the request using the provided http.Client.
resp, err := client.Do(r) resp, err := requestContext.Client.Do(r)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
} }
@ -101,11 +101,16 @@ func GetNotes(client http.Client, requestContext model.RequestContext, query map
return nil, utils.StatusCodesToError(resp.StatusCode) 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. // Initialize a slice of Note struct to store the response data.
var notesResponse []model.Note var notesResponse []model.Note
// Decode the JSON response into the slice of Note structs. // Decode the JSON response into the slice of Note structs.
err = json.NewDecoder(resp.Body).Decode(&notesResponse) err = json.Unmarshal(respBodyBytes, &notesResponse)
if err != nil { if err != nil {
// Log the error and return an empty slice and the error. // Log the error and return an empty slice and the error.
log.Println(err) log.Println(err)

View File

@ -35,14 +35,14 @@ func TestGetNote(t *testing.T) {
httpmock.RegisterResponder("GET", "https://e621.net/notes/36957.json", jsonResponser) httpmock.RegisterResponder("GET", "https://e621.net/notes/36957.json", jsonResponser)
requestContext := model.RequestContext{ requestContext := model.RequestContext{
Client: http.Client{},
Host: "https://e621.net", Host: "https://e621.net",
UserAgent: "Go-e621-SDK (@username)", UserAgent: "Go-e621-SDK (@username)",
Username: "memo", Username: "memo",
APIKey: "123456", APIKey: "123456",
} }
client := http.Client{} pool, err := GetNote(requestContext, "36957")
pool, err := GetNote(client, requestContext, "36957")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
@ -116,14 +116,14 @@ func TestGetNotes(t *testing.T) {
httpmock.RegisterResponder("GET", "https://e621.net/notes.json", jsonResponser) httpmock.RegisterResponder("GET", "https://e621.net/notes.json", jsonResponser)
requestContext := model.RequestContext{ requestContext := model.RequestContext{
Client: http.Client{},
Host: "https://e621.net", Host: "https://e621.net",
UserAgent: "Go-e621-SDK (@username)", UserAgent: "Go-e621-SDK (@username)",
Username: "memo", Username: "memo",
APIKey: "123456", APIKey: "123456",
} }
client := http.Client{} pools, err := GetNotes(requestContext, map[string]string{})
pools, err := GetNotes(client, requestContext, map[string]string{})
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return