refactor: moved http.Client to model.RequestContext

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2023-10-23 13:15:37 +02:00
parent 64a9623b3c
commit 7acff845fd
2 changed files with 7 additions and 13 deletions

View File

@ -22,14 +22,11 @@ func main() {
// Log: Getting a single post.
log.Println("Getting single post: ")
// Initialize an http.Client.
client := http.Client{}
// Specify the post ID for retrieval.
postID := "4353480" // Replace with the desired post's ID.
// Call the GetPost function to retrieve the specified post.
post, err := endpoints.GetPost(client, requestContext, postID)
post, err := endpoints.GetPost(requestContext, postID)
if err != nil {
log.Println(err)
@ -48,7 +45,7 @@ func main() {
}
// Call the GetPosts function to retrieve a list of posts based on the query parameters.
posts, err := endpoints.GetPosts(client, requestContext, query)
posts, err := endpoints.GetPosts(requestContext, query)
if err != nil {
log.Println(err)

View File

@ -11,15 +11,13 @@ import (
// GetPost retrieves a single post 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.
// Parameters: // - requestContext: The context for the API request, including the host, user agent, username, and API key.
// - ID: The ID of the post to retrieve.
//
// Returns:
// - model.Post: The retrieved post.
// - error: An error, if any, encountered during the API request or response handling.
func GetPost(client http.Client, requestContext model.RequestContext, ID string) (model.Post, error) {
func GetPost(requestContext model.RequestContext, ID string) (model.Post, error) {
// Create a new HTTP GET request to fetch the post information.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/posts/%s.json", requestContext.Host, ID), nil)
if err != nil {
@ -33,7 +31,7 @@ func GetPost(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 Post struct and the error.
log.Println(err)
@ -64,14 +62,13 @@ func GetPost(client http.Client, requestContext model.RequestContext, ID string)
// GetPosts retrieves a list of posts 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.Post: A slice of posts.
// - error: An error, if any, encountered during the API request or response handling.
func GetPosts(client http.Client, requestContext model.RequestContext, query map[string]string) ([]model.Post, error) {
func GetPosts(requestContext model.RequestContext, query map[string]string) ([]model.Post, error) {
// Create a new HTTP GET request.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/posts.json", requestContext.Host), nil)
if err != nil {
@ -90,7 +87,7 @@ func GetPosts(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)
}