- Replace prazdnikisegodnyaru with celebratoday as first is banned in Russia - Update .golangci.yml rule set - Megatons of fixes for linter - Update README.md
45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
// Package thecatapi implements api.thecatapi.com service client.
|
|
package thecatapi
|
|
|
|
import (
|
|
"aleesa-webapp-go/internal/config"
|
|
"context"
|
|
"fmt"
|
|
"math/rand/v2"
|
|
|
|
"github.com/carlmjohnson/requests"
|
|
)
|
|
|
|
type (
|
|
// TheCatAPIinnerStruct структурка, описывающая элемент массива, возвращемый Тhe cat api.
|
|
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.
|
|
TheCatAPI []TheCatAPIinnerStruct
|
|
)
|
|
|
|
// APIClient клиент сервиса thecatapi.com.
|
|
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: */
|