Make web service clients context aware

This commit is contained in:
Philipp Wolfer 2025-05-22 09:22:05 +02:00
parent adfe3f5771
commit d1642b7f1f
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
15 changed files with 128 additions and 76 deletions

View file

@ -1,5 +1,5 @@
/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
Copyright © 2023-2025 Philipp Wolfer <phw@uploadedlobster.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@ -59,17 +59,18 @@ func NewClient(token oauth2.TokenSource) Client {
}
}
func (c Client) RecentlyPlayedAfter(after time.Time, limit int) (RecentlyPlayedResult, error) {
return c.recentlyPlayed(&after, nil, limit)
func (c Client) RecentlyPlayedAfter(ctx context.Context, after time.Time, limit int) (RecentlyPlayedResult, error) {
return c.recentlyPlayed(ctx, &after, nil, limit)
}
func (c Client) RecentlyPlayedBefore(before time.Time, limit int) (RecentlyPlayedResult, error) {
return c.recentlyPlayed(nil, &before, limit)
func (c Client) RecentlyPlayedBefore(ctx context.Context, before time.Time, limit int) (RecentlyPlayedResult, error) {
return c.recentlyPlayed(ctx, nil, &before, limit)
}
func (c Client) recentlyPlayed(after *time.Time, before *time.Time, limit int) (result RecentlyPlayedResult, err error) {
func (c Client) recentlyPlayed(ctx context.Context, after *time.Time, before *time.Time, limit int) (result RecentlyPlayedResult, err error) {
const path = "/me/player/recently-played"
request := c.HTTPClient.R().
SetContext(ctx).
SetQueryParam("limit", strconv.Itoa(limit)).
SetResult(&result)
if after != nil {
@ -85,9 +86,10 @@ func (c Client) recentlyPlayed(after *time.Time, before *time.Time, limit int) (
return
}
func (c Client) UserTracks(offset int, limit int) (result TracksResult, err error) {
func (c Client) UserTracks(ctx context.Context, offset int, limit int) (result TracksResult, err error) {
const path = "/me/tracks"
response, err := c.HTTPClient.R().
SetContext(ctx).
SetQueryParams(map[string]string{
"offset": strconv.Itoa(offset),
"limit": strconv.Itoa(limit),

View file

@ -1,5 +1,5 @@
/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
Copyright © 2023-2025 Philipp Wolfer <phw@uploadedlobster.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@ -22,6 +22,7 @@ THE SOFTWARE.
package spotify_test
import (
"context"
"net/http"
"testing"
"time"
@ -47,7 +48,8 @@ func TestRecentlyPlayedAfter(t *testing.T) {
"https://api.spotify.com/v1/me/player/recently-played",
"testdata/recently-played.json")
result, err := client.RecentlyPlayedAfter(time.Now(), 3)
ctx := context.Background()
result, err := client.RecentlyPlayedAfter(ctx, time.Now(), 3)
require.NoError(t, err)
assert := assert.New(t)
@ -67,7 +69,8 @@ func TestGetUserTracks(t *testing.T) {
"https://api.spotify.com/v1/me/tracks",
"testdata/user-tracks.json")
result, err := client.UserTracks(0, 2)
ctx := context.Background()
result, err := client.UserTracks(ctx, 0, 2)
require.NoError(t, err)
assert := assert.New(t)

View file

@ -18,6 +18,7 @@ Scotty. If not, see <https://www.gnu.org/licenses/>.
package spotify
import (
"context"
"math"
"net/url"
"sort"
@ -96,6 +97,7 @@ func (b *SpotifyApiBackend) OAuth2Setup(token oauth2.TokenSource) error {
}
func (b *SpotifyApiBackend) ExportListens(oldestTimestamp time.Time, results chan models.ListensResult, progress chan models.TransferProgress) {
ctx := context.TODO()
startTime := time.Now()
minTime := oldestTimestamp
@ -107,7 +109,7 @@ func (b *SpotifyApiBackend) ExportListens(oldestTimestamp time.Time, results cha
}
for {
result, err := b.client.RecentlyPlayedAfter(minTime, MaxItemsPerGet)
result, err := b.client.RecentlyPlayedAfter(ctx, minTime, MaxItemsPerGet)
if err != nil {
p.Export.Abort()
progress <- p
@ -163,6 +165,7 @@ func (b *SpotifyApiBackend) ExportListens(oldestTimestamp time.Time, results cha
}
func (b *SpotifyApiBackend) ExportLoves(oldestTimestamp time.Time, results chan models.LovesResult, progress chan models.TransferProgress) {
ctx := context.TODO()
// Choose a high offset, we attempt to search the loves backwards starting
// at the oldest one.
offset := math.MaxInt32
@ -178,7 +181,7 @@ func (b *SpotifyApiBackend) ExportLoves(oldestTimestamp time.Time, results chan
out:
for {
result, err := b.client.UserTracks(offset, perPage)
result, err := b.client.UserTracks(ctx, offset, perPage)
if err != nil {
p.Export.Abort()
progress <- p