46 lines
869 B
Go
46 lines
869 B
Go
package e621
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
func ExecuteGetAPIRequest[dataType any](c *Client, URIPath string) (*dataType, error) {
|
|
time.Sleep(1 * time.Second)
|
|
var err error
|
|
url := fmt.Sprintf("%s/%s", baseURL, URIPath)
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return nil, 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 nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("failed to retrieve posts: %s", resp.Status)
|
|
}
|
|
|
|
var r dataType
|
|
err = json.Unmarshal(body, &r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &r, nil
|
|
}
|