Files
aleesa-webapp-go/internal/thecatapi/thecatapi.go
2024-09-17 23:59:38 +03:00

45 lines
1.1 KiB
Go

package thecatapi
import (
"aleesa-webapp-go/internal/config"
"context"
"fmt"
"math/rand/v2"
"github.com/carlmjohnson/requests"
)
// TheCatAPIinnerStruct структурка, описывающая элемент массива, возвращемый Тhe cat api.
type TheCatAPIinnerStruct struct {
ID string `json:"id,omitempty"`
URL string `json:"url"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
}
// TheCatAPI структурка, описывающая массив, возвращаемый на запрос урла картинки из The Cat API.
type TheCatAPI []TheCatAPIinnerStruct
func APIClient(cfg *config.MyConfig) (string, error) {
var (
ctx = context.Background()
url = "https://api.thecatapi.com/v1/images/search"
userAgent = cfg.UserAgents[rand.IntN(len(cfg.UserAgents))]
cat TheCatAPI
)
err := requests.
URL(url).
UserAgent(userAgent).
ToJSON(&cat).
Fetch(ctx)
if err != nil {
return "", fmt.Errorf("unable to GET %s: %w", url, err)
}
return cat[0].URL, nil
}
/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */