Добавил логи
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
@@ -30,13 +31,14 @@ type TVDB struct {
|
||||
apiKey string
|
||||
baseURL string
|
||||
hc *http.Client
|
||||
log *slog.Logger
|
||||
|
||||
mu sync.Mutex
|
||||
token string
|
||||
}
|
||||
|
||||
// NewTVDB собирает клиент TVDB.
|
||||
func NewTVDB(cfg TVDBConfig) (*TVDB, error) {
|
||||
// NewTVDB собирает клиент TVDB. logger nil → slog.Default().
|
||||
func NewTVDB(cfg TVDBConfig, logger *slog.Logger) (*TVDB, error) {
|
||||
if cfg.APIKey == "" {
|
||||
return nil, fmt.Errorf("metadata: tvdb api_key required")
|
||||
}
|
||||
@@ -48,7 +50,10 @@ func NewTVDB(cfg TVDBConfig) (*TVDB, error) {
|
||||
if base == "" {
|
||||
base = tvdbDefaultBaseURL
|
||||
}
|
||||
return &TVDB{apiKey: cfg.APIKey, baseURL: strings.TrimRight(base, "/"), hc: hc}, nil
|
||||
if logger == nil {
|
||||
logger = slog.Default()
|
||||
}
|
||||
return &TVDB{apiKey: cfg.APIKey, baseURL: strings.TrimRight(base, "/"), hc: hc, log: logger}, nil
|
||||
}
|
||||
|
||||
func (t *TVDB) Name() string { return "tvdb" }
|
||||
@@ -65,7 +70,8 @@ func (t *TVDB) login(ctx context.Context) (string, error) {
|
||||
Token string `json:"token"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := postJSON(ctx, t.hc, t.baseURL+"/login",
|
||||
t.log.Debug("tvdb: login (fetching bearer token)")
|
||||
if err := postJSON(ctx, t.hc, t.log, t.baseURL+"/login",
|
||||
map[string]string{"apikey": t.apiKey}, &resp); err != nil {
|
||||
return "", fmt.Errorf("tvdb login: %w", err)
|
||||
}
|
||||
@@ -87,6 +93,7 @@ func (t *TVDB) get(ctx context.Context, path string, out any) error {
|
||||
return err
|
||||
}
|
||||
if status == http.StatusUnauthorized {
|
||||
t.log.Warn("tvdb: token expired, re-login", "path", path)
|
||||
t.mu.Lock()
|
||||
t.token = "" // сбрасываем протухший токен
|
||||
t.mu.Unlock()
|
||||
@@ -113,15 +120,21 @@ func (t *TVDB) rawGet(ctx context.Context, path, token string) (int, []byte, err
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
start := time.Now()
|
||||
resp, err := t.hc.Do(req)
|
||||
if err != nil {
|
||||
t.log.Warn("tvdb: request failed",
|
||||
"host", req.URL.Host, "path", req.URL.Path, "duration", time.Since(start), "err", err)
|
||||
return 0, nil, fmt.Errorf("tvdb: request: %w", err)
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
raw, err := io.ReadAll(io.LimitReader(resp.Body, maxBody))
|
||||
if err != nil {
|
||||
t.log.Warn("tvdb: read body failed", "host", req.URL.Host, "path", req.URL.Path, "err", err)
|
||||
return 0, nil, fmt.Errorf("tvdb: read body: %w", err)
|
||||
}
|
||||
t.log.Debug("tvdb: request done",
|
||||
"host", req.URL.Host, "path", req.URL.Path, "status", resp.StatusCode, "duration", time.Since(start))
|
||||
return resp.StatusCode, raw, nil
|
||||
}
|
||||
|
||||
@@ -143,10 +156,12 @@ func (t *TVDB) Search(ctx context.Context, q Query) ([]Candidate, error) {
|
||||
if q.Year > 0 {
|
||||
params.Set("year", strconv.Itoa(q.Year))
|
||||
}
|
||||
t.log.Debug("tvdb: search", "type", q.Type, "title", q.Title, "year", q.Year)
|
||||
var resp tvdbSearchResp
|
||||
if err := t.get(ctx, "/search?"+params.Encode(), &resp); err != nil {
|
||||
return nil, fmt.Errorf("tvdb search: %w", err)
|
||||
}
|
||||
t.log.Debug("tvdb: search done", "title", q.Title, "results", len(resp.Data))
|
||||
out := make([]Candidate, 0, len(resp.Data))
|
||||
for _, r := range resp.Data {
|
||||
if r.TVDBID == "" {
|
||||
|
||||
Reference in New Issue
Block a user