2023-10-19 13:30:36 +00:00
|
|
|
package endpoints
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"git.dragse.it/anthrove/e621-to-graph/internal/utils"
|
|
|
|
"git.dragse.it/anthrove/e621-to-graph/pkg/e621/model"
|
2023-10-23 13:35:14 +00:00
|
|
|
"io"
|
2023-10-19 13:30:36 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2023-10-23 13:35:14 +00:00
|
|
|
"strings"
|
2023-10-19 13:30:36 +00:00
|
|
|
)
|
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// GetNote retrieves a single note by its ID from the e621 API.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - 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.
|
2023-10-23 13:35:14 +00:00
|
|
|
func GetNote(requestContext model.RequestContext, ID string) (model.Note, error) {
|
2023-10-19 14:13:38 +00:00
|
|
|
// Create a new HTTP GET request to fetch the note information.
|
2023-10-19 13:30:36 +00:00
|
|
|
r, err := http.NewRequest("GET", fmt.Sprintf("%s/notes/%s.json", requestContext.Host, ID), nil)
|
|
|
|
if err != nil {
|
2023-10-19 14:13:38 +00:00
|
|
|
// Log the error and return an empty Note struct and the error.
|
2023-10-19 13:30:36 +00:00
|
|
|
log.Println(err)
|
|
|
|
return model.Note{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Header.Set("User-Agent", requestContext.UserAgent)
|
|
|
|
r.Header.Add("Accept", "application/json")
|
|
|
|
r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
|
|
|
|
|
2023-10-23 06:56:14 +00:00
|
|
|
// Send the request using the provided http.Client.
|
2023-10-23 13:35:14 +00:00
|
|
|
resp, err := requestContext.Client.Do(r)
|
2023-10-19 13:30:36 +00:00
|
|
|
if err != nil {
|
2023-10-19 14:13:38 +00:00
|
|
|
// Log the error and return an empty Note struct and the error.
|
2023-10-19 13:30:36 +00:00
|
|
|
log.Println(err)
|
|
|
|
return model.Note{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the HTTP response status code indicates success (2xx range).
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 300 {
|
|
|
|
// If the status code is outside the 2xx range, return an error based on the status code.
|
|
|
|
return model.Note{}, utils.StatusCodesToError(resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// Initialize a Note struct to store the response data.
|
|
|
|
var noteResponse model.Note
|
2023-10-19 13:30:36 +00:00
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// Decode the JSON response into the Note struct.
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(¬eResponse)
|
2023-10-19 13:30:36 +00:00
|
|
|
if err != nil {
|
2023-10-19 14:13:38 +00:00
|
|
|
// Log the error and return an empty Note struct and the error.
|
2023-10-19 13:30:36 +00:00
|
|
|
log.Println(err)
|
|
|
|
return model.Note{}, err
|
|
|
|
}
|
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// Return the note information and no error (nil).
|
|
|
|
return noteResponse, nil
|
2023-10-19 13:30:36 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// GetNotes retrieves a list of notes from the e621 API based on query parameters.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - 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.
|
2023-10-23 13:35:14 +00:00
|
|
|
func GetNotes(requestContext model.RequestContext, query map[string]string) ([]model.Note, error) {
|
2023-10-19 13:30:36 +00:00
|
|
|
// Create a new HTTP GET request.
|
|
|
|
r, err := http.NewRequest("GET", fmt.Sprintf("%s/notes.json", requestContext.Host), nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append query parameters to the request URL.
|
|
|
|
q := r.URL.Query()
|
|
|
|
for k, v := range query {
|
|
|
|
q.Add(k, v)
|
|
|
|
}
|
|
|
|
r.URL.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
r.Header.Set("User-Agent", requestContext.UserAgent)
|
|
|
|
r.Header.Add("Accept", "application/json")
|
|
|
|
r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
|
|
|
|
|
2023-10-23 06:56:14 +00:00
|
|
|
// Send the request using the provided http.Client.
|
2023-10-23 13:35:14 +00:00
|
|
|
resp, err := requestContext.Client.Do(r)
|
2023-10-19 13:30:36 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the HTTP response status code indicates success (2xx range).
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 300 {
|
|
|
|
// If the status code is outside the 2xx range, return an error based on the status code.
|
|
|
|
return nil, utils.StatusCodesToError(resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
2023-10-23 13:35:14 +00:00
|
|
|
respBodyBytes, err := io.ReadAll(resp.Body)
|
|
|
|
if strings.Contains(string(respBodyBytes), "{\"notes\":[]}") {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// Initialize a slice of Note struct to store the response data.
|
|
|
|
var notesResponse []model.Note
|
2023-10-19 13:30:36 +00:00
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// Decode the JSON response into the slice of Note structs.
|
2023-10-23 13:35:14 +00:00
|
|
|
err = json.Unmarshal(respBodyBytes, ¬esResponse)
|
2023-10-19 13:30:36 +00:00
|
|
|
if err != nil {
|
2023-10-19 14:13:38 +00:00
|
|
|
// Log the error and return an empty slice and the error.
|
2023-10-19 13:30:36 +00:00
|
|
|
log.Println(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// Return the list of notes and no error (nil).
|
|
|
|
return notesResponse, nil
|
2023-10-19 13:30:36 +00:00
|
|
|
}
|