2023-10-16 13:56:00 +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"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// GetUser retrieves user information from e621.net based on the provided username.
|
|
|
|
//
|
|
|
|
// Parameters:
|
2023-10-23 06:56:14 +00:00
|
|
|
// - client: An http.Client used to make the API request.
|
2023-10-19 14:13:38 +00:00
|
|
|
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
|
|
|
|
// - username: The username of the user to retrieve.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - model.User: The retrieved user.
|
|
|
|
// - error: An error, if any, encountered during the API request or response handling.
|
2023-10-23 08:21:25 +00:00
|
|
|
func GetUser(requestContext model.RequestContext, username string) (model.User, error) {
|
2023-10-16 13:56:00 +00:00
|
|
|
// 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 {
|
|
|
|
// Log the error and return an empty User struct and the error.
|
|
|
|
log.Println(err)
|
|
|
|
return model.User{}, 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 08:21:25 +00:00
|
|
|
resp, err := requestContext.Client.Do(r)
|
2023-10-16 13:56:00 +00:00
|
|
|
if err != nil {
|
|
|
|
// Log the error and return an empty User struct and the error.
|
|
|
|
log.Println(err)
|
|
|
|
return model.User{}, 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.User{}, utils.StatusCodesToError(resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize a User struct to store the response data.
|
|
|
|
var user model.User
|
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// Decode the JSON response into the User struct.
|
2023-10-16 13:56:00 +00:00
|
|
|
err = json.NewDecoder(resp.Body).Decode(&user)
|
|
|
|
if err != nil {
|
|
|
|
// Log the error and return an empty User struct and the error.
|
|
|
|
log.Println(err)
|
|
|
|
return model.User{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the user information and no error (nil).
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// GetUsers retrieves a list of users from e621.net based on query parameters.
|
|
|
|
//
|
|
|
|
// Parameters:
|
2023-10-23 06:56:14 +00:00
|
|
|
// - client: An http.Client used to make the API request.
|
2023-10-19 14:13:38 +00:00
|
|
|
// - 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.User: A slice of users.
|
|
|
|
// - error: An error, if any, encountered during the API request or response handling.
|
2023-10-23 08:21:25 +00:00
|
|
|
func GetUsers(requestContext model.RequestContext, query map[string]string) ([]model.User, error) {
|
2023-10-16 13:56:00 +00:00
|
|
|
// Create a new HTTP GET request.
|
|
|
|
r, err := http.NewRequest("GET", fmt.Sprintf("%s/users.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 08:21:25 +00:00
|
|
|
resp, err := requestContext.Client.Do(r)
|
2023-10-16 13:56:00 +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 []model.User{}, utils.StatusCodesToError(resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// Initialize a slice of User struct to store the response data.
|
|
|
|
var users []model.User
|
2023-10-16 13:56:00 +00:00
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// Decode the JSON response into the slice of User structs.
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&users)
|
2023-10-16 13:56:00 +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-16 13:56:00 +00:00
|
|
|
log.Println(err)
|
|
|
|
return []model.User{}, err
|
|
|
|
}
|
|
|
|
|
2023-10-19 14:13:38 +00:00
|
|
|
// Return the list of users and no error (nil).
|
|
|
|
return users, nil
|
2023-10-16 13:56:00 +00:00
|
|
|
}
|