refactor(providers): one paced, retrying HTTP client for cover art and tags (#8002)
coverart/httpclient.go and tags/httpclient.go were the same client: pacing, exponential backoff with jitter, 401/403-as-transient, body caps. Tags had added a User-Agent and a shared Pacer, cover art an image GET. providers.HTTPClient now has all of it, with the tests that were in coverart. ErrTransient is shared too, so both families' errors.Is still match. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,8 @@ import (
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/providers"
|
||||
)
|
||||
|
||||
// lastfmBaseURL is a var (not const) so tests can override it.
|
||||
@@ -41,12 +43,12 @@ var lastfmPlaceholderHashes = map[string]bool{
|
||||
type lastfmProvider struct {
|
||||
enabled atomic.Bool
|
||||
apiKey atomic.Pointer[string]
|
||||
client *httpClient
|
||||
client *providers.HTTPClient
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register(&lastfmProvider{
|
||||
client: newHTTPClient(httpClientOptions{
|
||||
client: providers.NewHTTPClient(providers.HTTPOptions{
|
||||
Name: "lastfm",
|
||||
HTTPClient: &http.Client{Timeout: 30 * time.Second},
|
||||
MinInterval: lastfmMinPeriod,
|
||||
@@ -111,7 +113,7 @@ func (p *lastfmProvider) FetchArtistArt(ctx context.Context, ref ArtistRef) (thu
|
||||
} `json:"artist"`
|
||||
Error int `json:"error"` // present on error responses
|
||||
}
|
||||
if err := p.client.getJSON(ctx, lastfmBaseURL+"?"+q.Encode(), &resp); err != nil {
|
||||
if err := p.client.GetJSON(ctx, lastfmBaseURL+"?"+q.Encode(), &resp); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if resp.Error != 0 || resp.Artist.Name == "" {
|
||||
@@ -121,7 +123,7 @@ func (p *lastfmProvider) FetchArtistArt(ctx context.Context, ref ArtistRef) (thu
|
||||
if imgURL == "" || isLastfmPlaceholder(imgURL) {
|
||||
return nil, nil, ErrNotFound
|
||||
}
|
||||
img, ferr := p.client.getImage(ctx, imgURL)
|
||||
img, ferr := p.client.GetImage(ctx, imgURL)
|
||||
if ferr != nil {
|
||||
return nil, nil, ferr
|
||||
}
|
||||
@@ -157,7 +159,7 @@ func (p *lastfmProvider) FetchAlbumCover(ctx context.Context, ref AlbumRef) ([]b
|
||||
} `json:"album"`
|
||||
Error int `json:"error"`
|
||||
}
|
||||
if err := p.client.getJSON(ctx, lastfmBaseURL+"?"+q.Encode(), &resp); err != nil {
|
||||
if err := p.client.GetJSON(ctx, lastfmBaseURL+"?"+q.Encode(), &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.Error != 0 || resp.Album.Name == "" {
|
||||
@@ -167,7 +169,7 @@ func (p *lastfmProvider) FetchAlbumCover(ctx context.Context, ref AlbumRef) ([]b
|
||||
if imgURL == "" || isLastfmPlaceholder(imgURL) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return p.client.getImage(ctx, imgURL)
|
||||
return p.client.GetImage(ctx, imgURL)
|
||||
}
|
||||
|
||||
// lastfmImage models one entry of Last.fm's image[] array.
|
||||
|
||||
Reference in New Issue
Block a user