Добавил логи
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
@@ -38,7 +39,7 @@ const maxBody = 4 << 20 // 4 MiB — потолок на тело ответа
|
||||
|
||||
// getJSON выполняет GET и декодирует JSON-ответ в out. headers — опц.
|
||||
// дополнительные заголовки (напр. Authorization).
|
||||
func getJSON(ctx context.Context, hc *http.Client, rawURL string, headers map[string]string, out any) error {
|
||||
func getJSON(ctx context.Context, hc *http.Client, log *slog.Logger, rawURL string, headers map[string]string, out any) error {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("metadata: build request: %w", err)
|
||||
@@ -47,11 +48,11 @@ func getJSON(ctx context.Context, hc *http.Client, rawURL string, headers map[st
|
||||
for k, v := range headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
return doJSON(hc, req, out)
|
||||
return doJSON(hc, log, req, out)
|
||||
}
|
||||
|
||||
// postJSON выполняет POST с JSON-телом и декодирует ответ.
|
||||
func postJSON(ctx context.Context, hc *http.Client, rawURL string, body, out any) error {
|
||||
func postJSON(ctx context.Context, hc *http.Client, log *slog.Logger, rawURL string, body, out any) error {
|
||||
payload, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("metadata: marshal body: %w", err)
|
||||
@@ -62,26 +63,46 @@ func postJSON(ctx context.Context, hc *http.Client, rawURL string, body, out any
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
return doJSON(hc, req, out)
|
||||
return doJSON(hc, log, req, out)
|
||||
}
|
||||
|
||||
func doJSON(hc *http.Client, req *http.Request, out any) error {
|
||||
// doJSON выполняет запрос и декодирует ответ, логируя исход. В лог идут только
|
||||
// host и path (без query) — у TMDB api_key передаётся query-параметром, его
|
||||
// нельзя светить в логах.
|
||||
func doJSON(hc *http.Client, log *slog.Logger, req *http.Request, out any) error {
|
||||
if log == nil {
|
||||
log = slog.Default()
|
||||
}
|
||||
start := time.Now()
|
||||
resp, err := hc.Do(req)
|
||||
if err != nil {
|
||||
log.Warn("metadata: request failed",
|
||||
"method", req.Method, "host", req.URL.Host, "path", req.URL.Path,
|
||||
"duration", time.Since(start), "err", err)
|
||||
return fmt.Errorf("metadata: request: %w", err)
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
raw, err := io.ReadAll(io.LimitReader(resp.Body, maxBody))
|
||||
if err != nil {
|
||||
log.Warn("metadata: read body failed",
|
||||
"host", req.URL.Host, "path", req.URL.Path, "err", err)
|
||||
return fmt.Errorf("metadata: read body: %w", err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
log.Warn("metadata: non-ok status",
|
||||
"method", req.Method, "host", req.URL.Host, "path", req.URL.Path,
|
||||
"status", resp.StatusCode, "duration", time.Since(start))
|
||||
return fmt.Errorf("metadata: status %d: %s", resp.StatusCode, snippet(raw))
|
||||
}
|
||||
if err := json.Unmarshal(raw, out); err != nil {
|
||||
log.Warn("metadata: decode failed",
|
||||
"host", req.URL.Host, "path", req.URL.Path, "err", err)
|
||||
return fmt.Errorf("metadata: decode: %w (body: %s)", err, snippet(raw))
|
||||
}
|
||||
log.Debug("metadata: request ok",
|
||||
"method", req.Method, "host", req.URL.Host, "path", req.URL.Path,
|
||||
"duration", time.Since(start))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ func TestIntegration_TVMaze(t *testing.T) {
|
||||
if os.Getenv("JELLYBIT_LIVE") == "" {
|
||||
t.Skip("set JELLYBIT_LIVE=1 to run network tests")
|
||||
}
|
||||
c, err := metadata.NewTVMaze(metadata.TVMazeConfig{Timeout: 20 * time.Second})
|
||||
c, err := metadata.NewTVMaze(metadata.TVMazeConfig{Timeout: 20 * time.Second}, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewTVMaze: %v", err)
|
||||
}
|
||||
@@ -57,7 +57,7 @@ func TestIntegration_TVDB(t *testing.T) {
|
||||
if key == "" {
|
||||
t.Skip("set TVDB_API_KEY to run")
|
||||
}
|
||||
c, err := metadata.NewTVDB(metadata.TVDBConfig{APIKey: key, Timeout: 20 * time.Second})
|
||||
c, err := metadata.NewTVDB(metadata.TVDBConfig{APIKey: key, Timeout: 20 * time.Second}, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewTVDB: %v", err)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package metadata
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
@@ -25,10 +26,11 @@ type TMDB struct {
|
||||
apiKey string
|
||||
baseURL string
|
||||
hc *http.Client
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
// NewTMDB собирает клиент TMDB.
|
||||
func NewTMDB(cfg TMDBConfig) (*TMDB, error) {
|
||||
// NewTMDB собирает клиент TMDB. logger nil → slog.Default().
|
||||
func NewTMDB(cfg TMDBConfig, logger *slog.Logger) (*TMDB, error) {
|
||||
if cfg.APIKey == "" {
|
||||
return nil, fmt.Errorf("metadata: tmdb api_key required")
|
||||
}
|
||||
@@ -40,7 +42,10 @@ func NewTMDB(cfg TMDBConfig) (*TMDB, error) {
|
||||
if base == "" {
|
||||
base = tmdbDefaultBaseURL
|
||||
}
|
||||
return &TMDB{apiKey: cfg.APIKey, baseURL: strings.TrimRight(base, "/"), hc: hc}, nil
|
||||
if logger == nil {
|
||||
logger = slog.Default()
|
||||
}
|
||||
return &TMDB{apiKey: cfg.APIKey, baseURL: strings.TrimRight(base, "/"), hc: hc, log: logger}, nil
|
||||
}
|
||||
|
||||
func (t *TMDB) Name() string { return "tmdb" }
|
||||
@@ -73,13 +78,15 @@ func (t *TMDB) Search(ctx context.Context, q Query) ([]Candidate, error) {
|
||||
params.Set("first_air_date_year", strconv.Itoa(q.Year))
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("metadata: tmdb: неизвестный тип %q", q.Type)
|
||||
return nil, fmt.Errorf("metadata: tmdb: unknown type %q", q.Type)
|
||||
}
|
||||
|
||||
t.log.Debug("tmdb: search", "type", q.Type, "title", q.Title, "year", q.Year)
|
||||
var resp tmdbSearchResp
|
||||
if err := getJSON(ctx, t.hc, t.baseURL+path+"?"+params.Encode(), nil, &resp); err != nil {
|
||||
if err := getJSON(ctx, t.hc, t.log, t.baseURL+path+"?"+params.Encode(), nil, &resp); err != nil {
|
||||
return nil, fmt.Errorf("tmdb search: %w", err)
|
||||
}
|
||||
t.log.Debug("tmdb: search done", "title", q.Title, "results", len(resp.Results))
|
||||
|
||||
out := make([]Candidate, 0, len(resp.Results))
|
||||
for _, r := range resp.Results {
|
||||
@@ -109,7 +116,7 @@ type tmdbTVResp struct {
|
||||
func (t *TMDB) SeasonEpisodeCounts(ctx context.Context, id string) (map[int]int, error) {
|
||||
params := url.Values{"api_key": {t.apiKey}}
|
||||
var resp tmdbTVResp
|
||||
if err := getJSON(ctx, t.hc, t.baseURL+"/tv/"+url.PathEscape(id)+"?"+params.Encode(), nil, &resp); err != nil {
|
||||
if err := getJSON(ctx, t.hc, t.log, t.baseURL+"/tv/"+url.PathEscape(id)+"?"+params.Encode(), nil, &resp); err != nil {
|
||||
return nil, fmt.Errorf("tmdb tv %s: %w", id, err)
|
||||
}
|
||||
out := make(map[int]int, len(resp.Seasons))
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
func newTMDB(t *testing.T, url string) *TMDB {
|
||||
t.Helper()
|
||||
c, err := NewTMDB(TMDBConfig{APIKey: "k", BaseURL: url})
|
||||
c, err := NewTMDB(TMDBConfig{APIKey: "k", BaseURL: url}, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewTMDB: %v", err)
|
||||
}
|
||||
@@ -103,7 +103,7 @@ func TestTMDB_ErrorStatus(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNewTMDB_RequiresKey(t *testing.T) {
|
||||
if _, err := NewTMDB(TMDBConfig{}); err == nil {
|
||||
if _, err := NewTMDB(TMDBConfig{}, nil); err == nil {
|
||||
t.Fatal("want error without api_key")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 == "" {
|
||||
|
||||
@@ -52,7 +52,7 @@ func fakeTVDB(t *testing.T, logins *atomic.Int32) *httptest.Server {
|
||||
|
||||
func newTVDB(t *testing.T, url string) *TVDB {
|
||||
t.Helper()
|
||||
c, err := NewTVDB(TVDBConfig{APIKey: "k", BaseURL: url})
|
||||
c, err := NewTVDB(TVDBConfig{APIKey: "k", BaseURL: url}, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewTVDB: %v", err)
|
||||
}
|
||||
@@ -126,7 +126,7 @@ func TestTVDB_ReloginOn401(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNewTVDB_RequiresKey(t *testing.T) {
|
||||
if _, err := NewTVDB(TVDBConfig{}); err == nil {
|
||||
if _, err := NewTVDB(TVDBConfig{}, nil); err == nil {
|
||||
t.Fatal("want error without api_key")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package metadata
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
@@ -25,10 +26,11 @@ type TVMazeConfig struct {
|
||||
type TVMaze struct {
|
||||
baseURL string
|
||||
hc *http.Client
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
// NewTVMaze собирает клиент TVMaze (ключ не нужен).
|
||||
func NewTVMaze(cfg TVMazeConfig) (*TVMaze, error) {
|
||||
// NewTVMaze собирает клиент TVMaze (ключ не нужен). logger nil → slog.Default().
|
||||
func NewTVMaze(cfg TVMazeConfig, logger *slog.Logger) (*TVMaze, error) {
|
||||
hc, err := newHTTPClient(cfg.Proxy, cfg.Timeout)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -37,7 +39,10 @@ func NewTVMaze(cfg TVMazeConfig) (*TVMaze, error) {
|
||||
if base == "" {
|
||||
base = tvmazeDefaultBaseURL
|
||||
}
|
||||
return &TVMaze{baseURL: strings.TrimRight(base, "/"), hc: hc}, nil
|
||||
if logger == nil {
|
||||
logger = slog.Default()
|
||||
}
|
||||
return &TVMaze{baseURL: strings.TrimRight(base, "/"), hc: hc, log: logger}, nil
|
||||
}
|
||||
|
||||
func (t *TVMaze) Name() string { return "tvmaze" }
|
||||
@@ -63,9 +68,11 @@ func (t *TVMaze) Search(ctx context.Context, q Query) ([]Candidate, error) {
|
||||
Show tvmazeShow `json:"show"`
|
||||
}
|
||||
rawURL := t.baseURL + "/search/shows?q=" + url.QueryEscape(q.Title)
|
||||
if err := getJSON(ctx, t.hc, rawURL, nil, &resp); err != nil {
|
||||
t.log.Debug("tvmaze: search", "title", q.Title)
|
||||
if err := getJSON(ctx, t.hc, t.log, rawURL, nil, &resp); err != nil {
|
||||
return nil, fmt.Errorf("tvmaze search: %w", err)
|
||||
}
|
||||
t.log.Debug("tvmaze: search done", "title", q.Title, "results", len(resp))
|
||||
|
||||
out := make([]Candidate, 0, len(resp))
|
||||
for _, r := range resp {
|
||||
@@ -97,7 +104,7 @@ type tvmazeEpisode struct {
|
||||
func (t *TVMaze) SeasonEpisodeCounts(ctx context.Context, id string) (map[int]int, error) {
|
||||
var eps []tvmazeEpisode
|
||||
rawURL := t.baseURL + "/shows/" + url.PathEscape(id) + "/episodes"
|
||||
if err := getJSON(ctx, t.hc, rawURL, nil, &eps); err != nil {
|
||||
if err := getJSON(ctx, t.hc, t.log, rawURL, nil, &eps); err != nil {
|
||||
return nil, fmt.Errorf("tvmaze episodes %s: %w", id, err)
|
||||
}
|
||||
out := map[int]int{}
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
func newTVMaze(t *testing.T, url string) *TVMaze {
|
||||
t.Helper()
|
||||
c, err := NewTVMaze(TVMazeConfig{BaseURL: url})
|
||||
c, err := NewTVMaze(TVMazeConfig{BaseURL: url}, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewTVMaze: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user