Files
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

73 lines
2.0 KiB
Go

// Package xkcdru provides api to xkcd.ru.
package xkcdru
import (
"aleesa-webapp-go/internal/config"
"aleesa-webapp-go/internal/log"
"errors"
"fmt"
"io"
"math/rand/v2"
"net/http"
"time"
)
// APIClient клиент сервиса xkcd.ru.
func APIClient(cfg *config.MyConfig) (string, error) {
var (
err error
url = "https://xkcd.ru/random/"
c = http.Client{
Timeout: 10 * time.Second,
// Возвращаем ответ сразу же, не переходя по редиректам.
CheckRedirect: func(req *http.Request, via []*http.Request) error { //nolint: revive // Ну, не используется req и что из этого?
return http.ErrUseLastResponse
},
}
)
req, err := http.NewRequest(http.MethodGet, url, http.NoBody) //nolint: noctx
if err != nil {
return "", err
}
req.Header.Set("User-Agent", cfg.UserAgents[rand.IntN(len(cfg.UserAgents))])
var resp *http.Response
resp, err = c.Do(req) //nolint: bodyclose // Посмотри ниже, блять, тело запроса закрывается.
if err != nil {
return "", err
}
// Типа, надо закрывать Body в любом случае, как рекомендуют в документации https://pkg.go.dev/net/http .
defer func(Body io.ReadCloser) {
if err := Body.Close(); err != nil {
log.Errorf("Unable to close response body for request to %s: %s", url, err)
}
}(resp.Body)
// Response body нам не интересен, нам интересен статус и нам интересен заголовок Location.
if resp.StatusCode == 302 {
location := resp.Header.Get("Location")
if len(location) <= 3 {
err = errors.New("location header too short")
return "", err
}
location = location[1:(len(location) - 1)]
return fmt.Sprintf("https://xkcd.ru/i/%s_v1.png", location), nil
}
err = fmt.Errorf("resp.StatusCode: %d", resp.StatusCode)
return "", err
}
/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */