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:
@@ -7,6 +7,8 @@ import (
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/providers"
|
||||
)
|
||||
|
||||
// theAudioDBBaseURL is a var (not const) so tests can override it
|
||||
@@ -43,12 +45,12 @@ const (
|
||||
type theAudioDBProvider struct {
|
||||
enabled atomic.Bool
|
||||
apiKey atomic.Pointer[string] // nil until first Configure; falls back to test key when empty
|
||||
client *httpClient
|
||||
client *providers.HTTPClient
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register(&theAudioDBProvider{
|
||||
client: newHTTPClient(httpClientOptions{
|
||||
client: providers.NewHTTPClient(providers.HTTPOptions{
|
||||
Name: "theaudiodb",
|
||||
HTTPClient: &http.Client{Timeout: 30 * time.Second},
|
||||
MinInterval: theAudioDBMinPeriod,
|
||||
@@ -105,13 +107,13 @@ func (p *theAudioDBProvider) FetchAlbumCover(ctx context.Context, ref AlbumRef)
|
||||
StrAlbumThumb *string `json:"strAlbumThumb"`
|
||||
} `json:"album"`
|
||||
}
|
||||
if err := p.client.getJSON(ctx, p.jsonURL("album-mb.php?i="+ref.MBID), &resp); err != nil {
|
||||
if err := p.client.GetJSON(ctx, p.jsonURL("album-mb.php?i="+ref.MBID), &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(resp.Album) == 0 || resp.Album[0].StrAlbumThumb == nil || *resp.Album[0].StrAlbumThumb == "" {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return p.client.getImage(ctx, *resp.Album[0].StrAlbumThumb)
|
||||
return p.client.GetImage(ctx, *resp.Album[0].StrAlbumThumb)
|
||||
}
|
||||
|
||||
// FetchArtistArt hits /artist-mb.php?i=<mbid>, parses strArtistThumb +
|
||||
@@ -139,7 +141,7 @@ func (p *theAudioDBProvider) FetchArtistArt(ctx context.Context, ref ArtistRef)
|
||||
StrArtistFanart *string `json:"strArtistFanart"`
|
||||
} `json:"artists"`
|
||||
}
|
||||
if err := p.client.getJSON(ctx, p.jsonURL("artist-mb.php?i="+ref.MBID), &resp); err != nil {
|
||||
if err := p.client.GetJSON(ctx, p.jsonURL("artist-mb.php?i="+ref.MBID), &resp); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if len(resp.Artists) == 0 {
|
||||
@@ -159,7 +161,7 @@ func (p *theAudioDBProvider) FetchArtistArt(ctx context.Context, ref ArtistRef)
|
||||
}
|
||||
|
||||
if thumbURL != "" {
|
||||
thumb, err = p.client.getImage(ctx, thumbURL)
|
||||
thumb, err = p.client.GetImage(ctx, thumbURL)
|
||||
if err != nil && !errors.Is(err, ErrTransient) && !errors.Is(err, ErrNotFound) {
|
||||
return nil, nil, err
|
||||
}
|
||||
@@ -169,7 +171,7 @@ func (p *theAudioDBProvider) FetchArtistArt(ctx context.Context, ref ArtistRef)
|
||||
}
|
||||
}
|
||||
if fanartURL != "" {
|
||||
fanart, err = p.client.getImage(ctx, fanartURL)
|
||||
fanart, err = p.client.GetImage(ctx, fanartURL)
|
||||
if err != nil && !errors.Is(err, ErrTransient) && !errors.Is(err, ErrNotFound) {
|
||||
return thumb, nil, err
|
||||
}
|
||||
@@ -201,5 +203,5 @@ func (p *theAudioDBProvider) TestConnection(ctx context.Context) error {
|
||||
IDAlbum *string `json:"idAlbum"`
|
||||
} `json:"album"`
|
||||
}
|
||||
return p.client.getJSON(ctx, p.jsonURL("album-mb.php?i="+theAudioDBTestSampleMBID), &resp)
|
||||
return p.client.GetJSON(ctx, p.jsonURL("album-mb.php?i="+theAudioDBTestSampleMBID), &resp)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user