refactor: moved http.Client to model.RequestContext

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2023-10-23 10:21:25 +02:00
parent 84e344e34d
commit 38cfa7c9cf
4 changed files with 14 additions and 13 deletions

View File

@ -10,6 +10,7 @@ import (
func main() {
// Define the request context with essential information.
requestContext := model.RequestContext{
Client: http.Client{},
Host: "https://e621.net",
UserAgent: "Go-e621-SDK (@username)",
Username: "", // Replace with your username
@ -19,14 +20,11 @@ func main() {
// Log: Getting a single user.
log.Println("Getting a single user: ")
// Initialize an http.Client.
client := http.Client{}
// Specify the username for retrieval.
username := "selloo" // Replace with the desired username.
// Call the GetUser function to retrieve the specified user.
user, err := endpoints.GetUser(client, requestContext, username)
user, err := endpoints.GetUser(requestContext, username)
if err != nil {
log.Println(err)
@ -45,7 +43,7 @@ func main() {
}
// Call the GetUsers function to retrieve a list of users based on the query parameters.
userList, err := endpoints.GetUsers(client, requestContext, query)
userList, err := endpoints.GetUsers(requestContext, query)
if err != nil {
log.Println(err)

View File

@ -19,7 +19,7 @@ import (
// Returns:
// - model.User: The retrieved user.
// - error: An error, if any, encountered during the API request or response handling.
func GetUser(client http.Client, requestContext model.RequestContext, username string) (model.User, error) {
func GetUser(requestContext model.RequestContext, username string) (model.User, error) {
// 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 {
@ -33,7 +33,7 @@ func GetUser(client http.Client, requestContext model.RequestContext, username s
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 User struct and the error.
log.Println(err)
@ -71,7 +71,7 @@ func GetUser(client http.Client, requestContext model.RequestContext, username s
// Returns:
// - []model.User: A slice of users.
// - error: An error, if any, encountered during the API request or response handling.
func GetUsers(client http.Client, requestContext model.RequestContext, query map[string]string) ([]model.User, error) {
func GetUsers(requestContext model.RequestContext, query map[string]string) ([]model.User, error) {
// Create a new HTTP GET request.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/users.json", requestContext.Host), nil)
if err != nil {
@ -90,7 +90,7 @@ func GetUsers(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)
}

View File

@ -46,14 +46,14 @@ func TestGetUser(t *testing.T) {
httpmock.RegisterResponder("GET", "https://e621.net/users/MaxMustermannDer69ste.json", jsonResponser)
requestContext := model.RequestContext{
Client: http.Client{},
Host: "https://e621.net",
UserAgent: "Go-e621-SDK (@username)",
Username: "memo",
APIKey: "123456",
}
client := http.Client{}
user, err := GetUser(client, requestContext, "MaxMustermannDer69ste")
user, err := GetUser(requestContext, "MaxMustermannDer69ste")
if err != nil {
t.Error(err)
return
@ -135,14 +135,14 @@ func TestGetUsers(t *testing.T) {
httpmock.RegisterResponder("GET", "https://e621.net/users.json", jsonResponser)
requestContext := model.RequestContext{
Client: http.Client{},
Host: "https://e621.net",
UserAgent: "Go-e621-SDK (@username)",
Username: "memo",
APIKey: "123456",
}
client := http.Client{}
user, err := GetUsers(client, requestContext, map[string]string{})
user, err := GetUsers(requestContext, map[string]string{})
if err != nil {
t.Error(err)
return

View File

@ -1,6 +1,9 @@
package model
import "net/http"
type RequestContext struct {
Client http.Client
Host string
UserAgent string
Username string