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 (
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/providers"
|
||||
)
|
||||
|
||||
// newDeezerTestProvider returns a fresh enabled provider configured
|
||||
@@ -18,7 +20,7 @@ func newDeezerTestProvider(t *testing.T, baseURL string) *deezerProvider {
|
||||
deezerBaseURL = baseURL
|
||||
t.Cleanup(func() { deezerBaseURL = old })
|
||||
|
||||
p := &deezerProvider{client: newHTTPClient(httpClientOptions{Name: "deezer", HTTPClient: &http.Client{Timeout: 5 * time.Second}, BaseBackoff: time.Millisecond})}
|
||||
p := &deezerProvider{client: providers.NewHTTPClient(providers.HTTPOptions{Name: "deezer", HTTPClient: &http.Client{Timeout: 5 * time.Second}, BaseBackoff: time.Millisecond})}
|
||||
p.enabled.Store(true)
|
||||
return p
|
||||
}
|
||||
@@ -93,7 +95,7 @@ func TestDeezer_FetchArtistArt_NameMismatch(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDeezer_FetchArtistArt_EmptyName(t *testing.T) {
|
||||
p := &deezerProvider{client: newHTTPClient(httpClientOptions{Name: "deezer", HTTPClient: &http.Client{Timeout: 5 * time.Second}, BaseBackoff: time.Millisecond})}
|
||||
p := &deezerProvider{client: providers.NewHTTPClient(providers.HTTPOptions{Name: "deezer", HTTPClient: &http.Client{Timeout: 5 * time.Second}, BaseBackoff: time.Millisecond})}
|
||||
p.enabled.Store(true)
|
||||
_, _, err := p.FetchArtistArt(context.Background(), ArtistRef{})
|
||||
if !errors.Is(err, ErrNotFound) {
|
||||
@@ -102,7 +104,7 @@ func TestDeezer_FetchArtistArt_EmptyName(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDeezer_FetchArtistArt_Disabled(t *testing.T) {
|
||||
p := &deezerProvider{client: newHTTPClient(httpClientOptions{Name: "deezer", HTTPClient: &http.Client{Timeout: 5 * time.Second}, BaseBackoff: time.Millisecond})}
|
||||
p := &deezerProvider{client: providers.NewHTTPClient(providers.HTTPOptions{Name: "deezer", HTTPClient: &http.Client{Timeout: 5 * time.Second}, BaseBackoff: time.Millisecond})}
|
||||
// enabled defaults to false
|
||||
_, _, err := p.FetchArtistArt(context.Background(), ArtistRef{Name: "X"})
|
||||
if !errors.Is(err, ErrNotFound) {
|
||||
@@ -150,7 +152,7 @@ func TestDeezer_FetchAlbumCover_TitleMismatch(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDeezer_FetchAlbumCover_MissingFields(t *testing.T) {
|
||||
p := &deezerProvider{client: newHTTPClient(httpClientOptions{Name: "deezer", HTTPClient: &http.Client{Timeout: 5 * time.Second}, BaseBackoff: time.Millisecond})}
|
||||
p := &deezerProvider{client: providers.NewHTTPClient(providers.HTTPOptions{Name: "deezer", HTTPClient: &http.Client{Timeout: 5 * time.Second}, BaseBackoff: time.Millisecond})}
|
||||
p.enabled.Store(true)
|
||||
|
||||
cases := []AlbumRef{
|
||||
@@ -180,7 +182,7 @@ func TestDeezer_RateLimit_Serializes(t *testing.T) {
|
||||
// Build a provider WITH the production MinInterval so this test can
|
||||
// verify the rate-limit actually serializes. Other tests use
|
||||
// newDeezerTestProvider which leaves MinInterval=0 for speed.
|
||||
p := &deezerProvider{client: newHTTPClient(httpClientOptions{
|
||||
p := &deezerProvider{client: providers.NewHTTPClient(providers.HTTPOptions{
|
||||
Name: "deezer",
|
||||
HTTPClient: &http.Client{Timeout: 5 * time.Second},
|
||||
MinInterval: deezerMinPeriod,
|
||||
|
||||
Reference in New Issue
Block a user