This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
e621-sdk-go/e621/api/users.go
2023-05-22 13:08:08 +02:00

47 lines
982 B
Go

package api
import (
"e621_to_neo4j/e621/api/models"
"fmt"
"io"
"net/http"
"time"
)
// GetUserInfo retrieves the users information from e621 API.
func (c *Client) GetUserInfo(user string) (models.E621User, error) {
var e621User models.E621User
time.Sleep(2 * time.Second)
url := fmt.Sprintf("%s/users/%s.json", baseURL, user)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return e621User, err
}
req.Header.Set("User-Agent", "FavGetter (by Selloo)")
req.Header.Add("Accept", "application/json")
req.SetBasicAuth(c.username, c.apiKey)
resp, err := c.client.Do(req)
if err != nil {
return e621User, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return e621User, err
}
if resp.StatusCode != http.StatusOK {
return e621User, fmt.Errorf("failed to retrieve posts: %s", resp.Status)
}
e621User, err = models.UnmarshalE621User(body)
if err != nil {
return e621User, err
}
return e621User, nil
}