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.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)

View File

@ -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(&notesResponse)
err = json.Unmarshal(respBodyBytes, &notesResponse)
if err != nil {
// Log the error and return an empty slice and the error.
log.Println(err)

View File

@ -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