46 lines
881 B
Go
46 lines
881 B
Go
package e621
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
func ExecuteGetAPIRequest[dataType any](c *Client, URIPath string) (*dataType, error) {
|
|
var err error
|
|
c.limiter.Wait(context.Background())
|
|
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
|
|
}
|