tstor/plugins/kemono/kemonoapi/kemono.go
2025-03-22 08:49:14 +04:00

40 lines
825 B
Go

package kemonoapi
import (
"log/slog"
"net/http"
"time"
"github.com/go-resty/resty/v2"
"golang.org/x/time/rate"
)
type Downloader interface {
Download(<-chan FileWithIndex, Creator, Post) <-chan error
Get(url string) (resp *http.Response, err error)
WriteContent(Creator, Post, string) error
}
type Client struct {
client *resty.Client
ratelimit *rate.Limiter
log *slog.Logger
}
func NewClient(site string) *Client {
k := &Client{
ratelimit: rate.NewLimiter(rate.Every(time.Second), 3),
client: resty.New().
SetBaseURL(site + "/api/v1").
SetRetryCount(3).
SetRetryWaitTime(5 * time.Second).
AddRetryCondition(func(r *resty.Response, err error) bool {
return r != nil && r.StatusCode() == http.StatusTooManyRequests
}),
}
if k.log == nil {
k.log = slog.Default()
}
return k
}