Files
aleesa-webapp-go/internal/randomfox/randomfox.go
eleksir f2fc168abe Replace prazdnikisegodnyaru with celebratoday, small underhood fixes
- Replace prazdnikisegodnyaru with celebratoday as first is banned in Russia
- Update .golangci.yml rule set
- Megatons of fixes for linter
- Update README.md
2026-02-10 20:05:13 +03:00

49 lines
1.0 KiB
Go

// Package randomfox implements randomfox.ca service client.
package randomfox
import (
"aleesa-webapp-go/internal/config"
"context"
"fmt"
"math/rand/v2"
"regexp"
"github.com/carlmjohnson/requests"
)
type (
// RandomFox структура, возвращаемая на запрос в randomfox.ca API.
RandomFox struct {
Image string `json:"image,omitempty"`
Link string `json:"link"`
}
)
// APIClient клиент сервиса randomfox.ca.
func APIClient(cfg *config.MyConfig) (string, error) {
var (
ctx = context.Background()
url = "https://randomfox.ca/floof/"
userAgent = cfg.UserAgents[rand.IntN(len(cfg.UserAgents))]
fox RandomFox
)
err := requests.
URL(url).
UserAgent(userAgent).
ToJSON(&fox).
Fetch(ctx)
if err != nil {
return "", fmt.Errorf("unable to GET %s: %w", url, err)
}
var re = regexp.MustCompile(`\\`)
answer := re.ReplaceAll([]byte(fox.Link), []byte(""))
return string(answer), nil
}
/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */