refactor: moved http.client to model.RequestContext

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2023-10-23 16:01:42 +02:00
parent 0c0a8eb8a4
commit 93567a2d8b
2 changed files with 4 additions and 8 deletions

View File

@ -22,16 +22,13 @@ func main() {
// Log: Getting favorites from the API.
log.Println("Getting favorites API user: ")
// Initialize an http.Client.
client := http.Client{}
// Define query parameters for retrieving favorites.
query := map[string]string{
"limit": "5",
}
// Call the GetFavorites function to retrieve favorite posts.
posts, err := endpoints.GetFavorites(client, requestContext, query)
posts, err := endpoints.GetFavorites(requestContext, query)
if err != nil {
log.Println(err)
@ -51,7 +48,7 @@ func main() {
}
// Call the GetFavorites function to retrieve favorite posts for the specified user.
posts, err = endpoints.GetFavorites(client, requestContext, query)
posts, err = endpoints.GetFavorites(requestContext, query)
if err != nil {
log.Println(err)

View File

@ -14,14 +14,13 @@ import (
// The user_id parameter is required to get the favorites of a user.
//
// 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 favorite posts.
// - error: An error, if any, encountered during the API request or response handling.
func GetFavorites(client http.Client, requestContext model.RequestContext, query map[string]string) ([]model.Post, error) {
func GetFavorites(requestContext model.RequestContext, query map[string]string) ([]model.Post, error) {
// Create a new HTTP GET request.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/favorites.json", requestContext.Host), nil)
if err != nil {
@ -40,7 +39,7 @@ func GetFavorites(client http.Client, requestContext model.RequestContext, query
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)
}