doc: updated documentation for better readability

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2023-10-19 16:13:38 +02:00
parent d9fb7dfb88
commit fca7cdbd2b
10 changed files with 307 additions and 132 deletions

View File

@ -8,33 +8,50 @@ import (
)
func main() {
// Define the request context with essential information.
requestContext := model.RequestContext{
Host: "https://e621.net",
UserAgent: "Go-e621-SDK (@username)",
Username: "",
APIKey: "",
Username: "", // Replace with your username
APIKey: "", // Replace with your API key
}
// Log: Getting a single note.
log.Println("Getting single note: ")
// Initialize an HTTP client.
client := http.Client{}
note, err := endpoints.GetNote(client, requestContext, "36957")
// Specify the note ID for retrieval.
noteID := "36957" // Replace with the desired note's ID.
// Call the GetNote function to retrieve the specified note.
note, err := endpoints.GetNote(client, requestContext, noteID)
if err != nil {
log.Println(err)
} else {
// Log the body of the retrieved note.
log.Println(note.Body)
}
log.Println(note.Body)
log.Println("----------")
log.Println("Getting list of notes: ")
// Log: Getting a list of notes.
log.Println("Getting a list of notes: ")
// Define query parameters for retrieving a list of notes.
query := map[string]string{
"limit": "5",
}
// Call the GetNotes function to retrieve a list of notes based on the query parameters.
notes, err := endpoints.GetNotes(client, requestContext, query)
if err != nil {
log.Println(err)
} else {
// Log the number of notes retrieved.
log.Println(len(notes))
}
log.Println(len(notes))
log.Println("----------")
}

View File

@ -8,33 +8,50 @@ import (
)
func main() {
// Define the request context with essential information.
requestContext := model.RequestContext{
Host: "https://e621.net",
UserAgent: "Go-e621-SDK (@username)",
Username: "",
APIKey: "",
Username: "", // Replace with your username
APIKey: "", // Replace with your API key
}
// Log: Getting a single pool.
log.Println("Getting single pool: ")
// Initialize an HTTP client.
client := http.Client{}
pool, err := endpoints.GetPool(client, requestContext, "36957")
// Specify the pool ID for retrieval.
poolID := "36957" // Replace with the desired pool's ID.
// Call the GetPool function to retrieve the specified pool.
pool, err := endpoints.GetPool(client, requestContext, poolID)
if err != nil {
log.Println(err)
} else {
// Log the name of the retrieved pool.
log.Println(pool.Name)
}
log.Println(pool.Name)
log.Println("----------")
log.Println("Getting list of pools: ")
// Log: Getting a list of pools.
log.Println("Getting a list of pools: ")
// Define query parameters for retrieving a list of pools.
query := map[string]string{
"limit": "5",
}
// Call the GetPools function to retrieve a list of pools based on the query parameters.
pools, err := endpoints.GetPools(client, requestContext, query)
if err != nil {
log.Println(err)
} else {
// Log the number of pools retrieved.
log.Println(len(pools))
}
log.Println(len(pools))
log.Println("----------")
}

View File

@ -8,33 +8,50 @@ import (
)
func main() {
// Define the request context with essential information.
requestContext := model.RequestContext{
Host: "https://e621.net",
UserAgent: "Go-e621-SDK (@username)",
Username: "",
APIKey: "",
Username: "", // Replace with your username
APIKey: "", // Replace with your API key
}
// Log: Getting a single post.
log.Println("Getting single post: ")
// Initialize an HTTP client.
client := http.Client{}
post, err := endpoints.GetPost(client, requestContext, "4353480")
// 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)
if err != nil {
log.Println(err)
} else {
// Log the URL of the retrieved post.
log.Println(post.File.URL)
}
log.Println(post.File.URL)
log.Println("----------")
log.Println("Getting list of posts: ")
// Log: Getting a list of posts.
log.Println("Getting a list of posts: ")
// Define query parameters for retrieving a list of posts.
query := map[string]string{
"limit": "5",
}
// Call the GetPosts function to retrieve a list of posts based on the query parameters.
posts, err := endpoints.GetPosts(client, requestContext, query)
if err != nil {
log.Println(err)
} else {
// Log the number of posts retrieved.
log.Println(len(posts))
}
log.Println(len(posts))
log.Println("----------")
}

View File

@ -8,58 +8,90 @@ import (
)
func main() {
// Define the request context with essential information.
requestContext := model.RequestContext{
Host: "https://e621.net",
UserAgent: "Go-e621-SDK (@username)",
Username: "",
APIKey: "",
Username: "", // Replace with your username
APIKey: "", // Replace with your API key
}
log.Println("Getting single tag: ")
// Log: Getting a single tag.
log.Println("Getting a single tag: ")
// Initialize an HTTP client.
client := http.Client{}
tag, err := endpoints.GetTag(client, requestContext, "1530")
// Specify the tag ID for retrieval.
tagID := "1530" // Replace with the desired tag's ID.
// Call the GetTag function to retrieve the specified tag.
tag, err := endpoints.GetTag(client, requestContext, tagID)
if err != nil {
log.Println(err)
} else {
// Log the name of the retrieved tag.
log.Println(tag.Name)
}
log.Println(tag.Name)
log.Println("----------")
log.Println("Getting list of tags: ")
// Log: Getting a list of tags.
log.Println("Getting a list of tags: ")
// Define query parameters for retrieving a list of tags.
query := map[string]string{
"limit": "5",
}
// Call the GetTags function to retrieve a list of tags based on the query parameters.
tagList, err := endpoints.GetTags(client, requestContext, query)
if err != nil {
log.Println(err)
} else {
// Log the number of tags retrieved.
log.Println(len(tagList))
}
log.Println(len(tagList))
log.Println("----------")
log.Println("seach for tags containing cat: ")
// Log: Searching for tags containing "cat."
log.Println("Searching for tags containing 'cat': ")
// Define query parameters for searching tags that match "cat."
query = map[string]string{
"limit": "5",
"search[name_matches]": "cat*",
}
// Call the GetTags function with the search query to retrieve matching tags.
tagList, err = endpoints.GetTags(client, requestContext, query)
if err != nil {
log.Println(err)
} else {
// Log the retrieved tags.
log.Println(tagList)
}
log.Println(tagList)
log.Println("----------")
log.Println("seach for tags with the category artist: ")
// Log: Searching for tags with the category "artist."
log.Println("Searching for tags with the category 'artist': ")
// Define query parameters for searching tags in the "artist" category.
query = map[string]string{
"limit": "5",
"search[category]": "1",
}
// Call the GetTags function with the category filter to retrieve artist tags.
tagList, err = endpoints.GetTags(client, requestContext, query)
if err != nil {
log.Println(err)
} else {
// Log the retrieved artist tags.
log.Println(tagList)
}
log.Println(tagList)
log.Println("----------")
}

View File

@ -8,32 +8,50 @@ import (
)
func main() {
// Define the request context with essential information.
requestContext := model.RequestContext{
Host: "https://e621.net",
UserAgent: "Go-e621-SDK (@username)",
Username: "",
APIKey: "",
Username: "", // Replace with your username
APIKey: "", // Replace with your API key
}
log.Println("Getting single user: ")
// Log: Getting a single user.
log.Println("Getting a single user: ")
// Initialize an HTTP client.
client := http.Client{}
user, err := endpoints.GetUser(client, requestContext, "selloo")
// 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)
if err != nil {
log.Println(err)
} else {
// Log the ID of the retrieved user.
log.Println(user.ID)
}
log.Println(user.ID)
log.Println("----------")
log.Println("Getting list of users: ")
// Log: Getting a list of users.
log.Println("Getting a list of users: ")
// Define query parameters for retrieving a list of users.
query := map[string]string{
"limit": "5",
}
// Call the GetUsers function to retrieve a list of users based on the query parameters.
userList, err := endpoints.GetUsers(client, requestContext, query)
if err != nil {
log.Println(err)
} else {
// Log the number of users retrieved.
log.Println(len(userList))
}
log.Println(len(userList))
log.Println("----------")
}

View File

@ -9,11 +9,21 @@ import (
"net/http"
)
// GetNote retrieves a single note 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.
// - 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.
func GetNote(client http.Client, requestContext model.RequestContext, ID string) (model.Note, error) {
// Create a new HTTP GET request to fetch user information from the specified 'host' and 'username'.
// Create a new HTTP GET request to fetch the note information.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/notes/%s.json", requestContext.Host, ID), nil)
if err != nil {
// Log the error and return an empty User struct and the error.
// Log the error and return an empty Note struct and the error.
log.Println(err)
return model.Note{}, err
}
@ -25,7 +35,7 @@ func GetNote(client http.Client, requestContext model.RequestContext, ID string)
// Send the request using the provided HTTP client.
resp, err := client.Do(r)
if err != nil {
// Log the error and return an empty User struct and the error.
// Log the error and return an empty Note struct and the error.
log.Println(err)
return model.Note{}, err
}
@ -36,21 +46,31 @@ func GetNote(client http.Client, requestContext model.RequestContext, ID string)
return model.Note{}, utils.StatusCodesToError(resp.StatusCode)
}
// Initialize a User struct to store the response data.
var poolResponse model.Note
// Initialize a Note struct to store the response data.
var noteResponse model.Note
// Decode the JSON response into the model.poolResponse{} struct.
err = json.NewDecoder(resp.Body).Decode(&poolResponse)
// Decode the JSON response into the Note struct.
err = json.NewDecoder(resp.Body).Decode(&noteResponse)
if err != nil {
// Log the error and return an empty User struct and the error.
// Log the error and return an empty Note struct and the error.
log.Println(err)
return model.Note{}, err
}
// Return the user information and no error (nil).
return poolResponse, nil
// Return the note information and no error (nil).
return noteResponse, nil
}
// GetNotes retrieves a list of notes 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.Note: A slice of notes.
// - error: An error, if any, encountered during the API request or response handling.
func GetNotes(client http.Client, requestContext model.RequestContext, query map[string]string) ([]model.Note, error) {
// Create a new HTTP GET request.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/notes.json", requestContext.Host), nil)
@ -75,29 +95,23 @@ func GetNotes(client http.Client, requestContext model.RequestContext, query map
log.Print(err)
}
// Send the request using the provided HTTP client.
resp, err = client.Do(r)
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)
}
// Initialize a User struct to store the response data.
var poolResponse []model.Note
// Initialize a slice of Note struct to store the response data.
var notesResponse []model.Note
// Decode the JSON response into the user struct.
err = json.NewDecoder(resp.Body).Decode(&poolResponse)
// Decode the JSON response into the slice of Note structs.
err = json.NewDecoder(resp.Body).Decode(&notesResponse)
if err != nil {
// Log the error and return an empty User struct and the error.
// Log the error and return an empty slice and the error.
log.Println(err)
return nil, err
}
return poolResponse, nil
// Return the list of notes and no error (nil).
return notesResponse, nil
}

View File

@ -9,11 +9,21 @@ import (
"net/http"
)
// GetPool retrieves a pool 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.
// - ID: The ID of the pool to retrieve.
//
// Returns:
// - model.Pool: The retrieved pool.
// - error: An error, if any, encountered during the API request or response handling.
func GetPool(client http.Client, requestContext model.RequestContext, ID string) (model.Pool, error) {
// Create a new HTTP GET request to fetch user information from the specified 'host' and 'username'.
// Create a new HTTP GET request to fetch the pool information.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/pools/%s.json", requestContext.Host, ID), nil)
if err != nil {
// Log the error and return an empty User struct and the error.
// Log the error and return an empty Pool struct and the error.
log.Println(err)
return model.Pool{}, err
}
@ -25,7 +35,7 @@ func GetPool(client http.Client, requestContext model.RequestContext, ID string)
// Send the request using the provided HTTP client.
resp, err := client.Do(r)
if err != nil {
// Log the error and return an empty User struct and the error.
// Log the error and return an empty Pool struct and the error.
log.Println(err)
return model.Pool{}, err
}
@ -36,21 +46,31 @@ func GetPool(client http.Client, requestContext model.RequestContext, ID string)
return model.Pool{}, utils.StatusCodesToError(resp.StatusCode)
}
// Initialize a User struct to store the response data.
// Initialize a Pool struct to store the response data.
var poolResponse model.Pool
// Decode the JSON response into the model.poolResponse{} struct.
// Decode the JSON response into the Pool struct.
err = json.NewDecoder(resp.Body).Decode(&poolResponse)
if err != nil {
// Log the error and return an empty User struct and the error.
// Log the error and return an empty Pool struct and the error.
log.Println(err)
return model.Pool{}, err
}
// Return the user information and no error (nil).
// Return the pool information and no error (nil).
return poolResponse, nil
}
// GetPools retrieves a list of pools 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.Pool: A slice of pools.
// - error: An error, if any, encountered during the API request or response handling.
func GetPools(client http.Client, requestContext model.RequestContext, query map[string]string) ([]model.Pool, error) {
// Create a new HTTP GET request.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/pools.json", requestContext.Host), nil)
@ -75,29 +95,23 @@ func GetPools(client http.Client, requestContext model.RequestContext, query map
log.Print(err)
}
// Send the request using the provided HTTP client.
resp, err = client.Do(r)
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)
}
// Initialize a User struct to store the response data.
var poolResponse []model.Pool
// Initialize a slice of Pool struct to store the response data.
var poolsResponse []model.Pool
// Decode the JSON response into the user struct.
err = json.NewDecoder(resp.Body).Decode(&poolResponse)
// Decode the JSON response into the slice of Pool structs.
err = json.NewDecoder(resp.Body).Decode(&poolsResponse)
if err != nil {
// Log the error and return an empty User struct and the error.
// Log the error and return an empty slice and the error.
log.Println(err)
return nil, err
}
return poolResponse, nil
// Return the list of pools and no error (nil).
return poolsResponse, nil
}

View File

@ -9,11 +9,21 @@ import (
"net/http"
)
// 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.
// - 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) {
// Create a new HTTP GET request to fetch user information from the specified 'host' and 'username'.
// 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 {
// Log the error and return an empty User struct and the error.
// Log the error and return an empty Post struct and the error.
log.Println(err)
return model.Post{}, err
}
@ -25,7 +35,7 @@ func GetPost(client http.Client, requestContext model.RequestContext, ID string)
// Send the request using the provided HTTP client.
resp, err := client.Do(r)
if err != nil {
// Log the error and return an empty User struct and the error.
// Log the error and return an empty Post struct and the error.
log.Println(err)
return model.Post{}, err
}
@ -36,21 +46,31 @@ func GetPost(client http.Client, requestContext model.RequestContext, ID string)
return model.Post{}, utils.StatusCodesToError(resp.StatusCode)
}
// Initialize a User struct to store the response data.
var PostResponse model.PostResponse
// Initialize a Post struct to store the response data.
var postResponse model.PostResponse
// Decode the JSON response into the model.PostResponse{} struct.
err = json.NewDecoder(resp.Body).Decode(&PostResponse)
// Decode the JSON response into the PostResponse struct.
err = json.NewDecoder(resp.Body).Decode(&postResponse)
if err != nil {
// Log the error and return an empty User struct and the error.
// Log the error and return an empty Post struct and the error.
log.Println(err)
return model.Post{}, err
}
// Return the user information and no error (nil).
return PostResponse.Post, nil
// Return the post information and no error (nil).
return postResponse.Post, nil
}
// 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) {
// Create a new HTTP GET request.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/posts.json", requestContext.Host), nil)
@ -75,29 +95,23 @@ func GetPosts(client http.Client, requestContext model.RequestContext, query map
log.Print(err)
}
// Send the request using the provided HTTP client.
resp, err = client.Do(r)
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)
}
// Initialize a User struct to store the response data.
// Initialize a slice of Post struct to store the response data.
var postResponse model.PostResponse
// Decode the JSON response into the user struct.
// Decode the JSON response into the PostResponse struct.
err = json.NewDecoder(resp.Body).Decode(&postResponse)
if err != nil {
// Log the error and return an empty User struct and the error.
// Log the error and return an empty slice and the error.
log.Println(err)
return nil, err
}
// Return the list of posts and no error (nil).
return postResponse.Posts, nil
}

View File

@ -9,10 +9,18 @@ import (
"net/http"
)
// GetTag allows you to get a tag by ID
// GetTag retrieves a tag 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.
// - ID: The ID of the tag to retrieve.
//
// Returns:
// - model.Tag: The retrieved tag.
// - error: An error, if any, encountered during the API request or response handling.
func GetTag(client http.Client, requestContext model.RequestContext, ID string) (model.Tag, error) {
// Create a new HTTP GET request to fetch Tag information from the specified 'host' and 'ID'.
// Create a new HTTP GET request to fetch tag information.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/tags/%s.json", requestContext.Host, ID), nil)
if err != nil {
// Log the error and return an empty Tag struct and the error.
@ -21,7 +29,7 @@ func GetTag(client http.Client, requestContext model.RequestContext, ID string)
}
r.Header.Set("User-Agent", requestContext.UserAgent)
r.Header.Add("Accept", "application/json")
r.Header.Add("Accept", "application.json")
r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
// Send the request using the provided HTTP client.
@ -49,21 +57,30 @@ func GetTag(client http.Client, requestContext model.RequestContext, ID string)
return model.Tag{}, err
}
// Return the Tag information and no error (nil).
// Return the tag information and no error (nil).
return tag, nil
}
// GetTags allows you to search based on the following query:
// GetTags retrieves a list of tags 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.
//
// Query:
// - search[name_matches]: A tag name expression to match against, which can include * as a wildcard.
// - search[category]: Filters results to a particular category. Default value is blank (show all tags). See below for allowed values.
// - 0 general; 1 artist; 3 copyright; 4 character; 5 species; 6 invalid; 7 meta; 8 lore
// - search[category]: Filters results to a particular category. Default value is blank (show all tags). 0 general; 1 artist; 3 copyright; 4 character; 5 species; 6 invalid; 7 meta; 8 lore
// - search[order]: Changes the sort order. Pass one of date (default), count, or name.
// - search[hide_empty]: Hide tags with zero visible posts. Pass true (default) or false.
// - search[has_wiki]: Show only tags with, or without, a wiki page. Pass true, false, or blank (default).
// - search[has_artist]: Show only tags with, or without an artist page. Pass true, false, or blank (default).
// - limit: Maximum number of results to return per query. Default is 75. There is a hard upper limit of 320.
// - page: The page that will be returned. Can also be used with a or b + tag_id to get the tags after or before the specified tag ID. For example a13 gets every tag after tag_id 13 up to the limit. This overrides the specified search ordering, date is always used instead.
//
// Returns:
// - []model.Tag: A slice of tags.
// - error: An error, if any, encountered during the API request or response handling.
func GetTags(client http.Client, requestContext model.RequestContext, query map[string]string) ([]model.Tag, error) {
// Create a new HTTP GET request.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/tags.json", requestContext.Host), nil)
@ -94,16 +111,17 @@ func GetTags(client http.Client, requestContext model.RequestContext, query map[
return []model.Tag{}, utils.StatusCodesToError(resp.StatusCode)
}
// Initialize a Tag struct to store the response data.
var tag []model.Tag
// Initialize a slice of Tag struct to store the response data.
var tags []model.Tag
// Decode the JSON response into the user struct.
err = json.NewDecoder(resp.Body).Decode(&tag)
// Decode the JSON response into the slice of Tag structs.
err = json.NewDecoder(resp.Body).Decode(&tags)
if err != nil {
// Log the error and return an empty Tag struct and the error.
// Log the error and return an empty slice and the error.
log.Println(err)
return []model.Tag{}, err
}
return tag, nil
// Return the list of tags and no error (nil).
return tags, nil
}

View File

@ -9,9 +9,16 @@ import (
"net/http"
)
// GetUser sends an HTTP GET request to fetch user information from e621.net.
// It constructs a URL based on the provided 'host' and 'username'.
// The response is returned as a *http.Response pointer.
// GetUser retrieves user information from e621.net based on the provided username.
//
// 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.
// - 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.
func GetUser(client http.Client, 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)
@ -42,7 +49,7 @@ func GetUser(client http.Client, requestContext model.RequestContext, username s
// Initialize a User struct to store the response data.
var user model.User
// Decode the JSON response into the user struct.
// Decode the JSON response into the User struct.
err = json.NewDecoder(resp.Body).Decode(&user)
if err != nil {
// Log the error and return an empty User struct and the error.
@ -54,9 +61,16 @@ func GetUser(client http.Client, requestContext model.RequestContext, username s
return user, nil
}
// GetUsers sends an HTTP GET request to fetch user data from e621.net.
// It constructs a URL based on the provided 'host' and 'query' parameters.
// The response is returned as a *http.Response pointer.
// GetUsers retrieves a list of users from e621.net 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.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) {
// Create a new HTTP GET request.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/users.json", requestContext.Host), nil)
@ -87,17 +101,17 @@ func GetUsers(client http.Client, requestContext model.RequestContext, query map
return []model.User{}, utils.StatusCodesToError(resp.StatusCode)
}
// Initialize a User struct to store the response data.
var user []model.User
// Initialize a slice of User struct to store the response data.
var users []model.User
// Decode the JSON response into the user struct.
err = json.NewDecoder(resp.Body).Decode(&user)
// Decode the JSON response into the slice of User structs.
err = json.NewDecoder(resp.Body).Decode(&users)
if err != nil {
// Log the error and return an empty User struct and the error.
// Log the error and return an empty slice and the error.
log.Println(err)
return []model.User{}, err
}
return user, nil
// Return the list of users and no error (nil).
return users, nil
}