mirror of
https://git.sr.ht/~phw/scotty
synced 2025-06-06 12:58:35 +02:00
Make web service clients context aware
This commit is contained in:
parent
adfe3f5771
commit
d1642b7f1f
15 changed files with 128 additions and 76 deletions
|
@ -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
|
||||
|
@ -23,6 +23,7 @@ THE SOFTWARE.
|
|||
package deezer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
|
@ -52,14 +53,14 @@ func NewClient(token oauth2.TokenSource) Client {
|
|||
}
|
||||
}
|
||||
|
||||
func (c Client) UserHistory(offset int, limit int) (result HistoryResult, err error) {
|
||||
func (c Client) UserHistory(ctx context.Context, offset int, limit int) (result HistoryResult, err error) {
|
||||
const path = "/user/me/history"
|
||||
return listRequest[HistoryResult](c, path, offset, limit)
|
||||
return listRequest[HistoryResult](ctx, c, path, offset, limit)
|
||||
}
|
||||
|
||||
func (c Client) UserTracks(offset int, limit int) (TracksResult, error) {
|
||||
func (c Client) UserTracks(ctx context.Context, offset int, limit int) (TracksResult, error) {
|
||||
const path = "/user/me/tracks"
|
||||
return listRequest[TracksResult](c, path, offset, limit)
|
||||
return listRequest[TracksResult](ctx, c, path, offset, limit)
|
||||
}
|
||||
|
||||
func (c Client) setToken(req *resty.Request) error {
|
||||
|
@ -72,8 +73,9 @@ func (c Client) setToken(req *resty.Request) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func listRequest[T Result](c Client, path string, offset int, limit int) (result T, err error) {
|
||||
func listRequest[T Result](ctx context.Context, c Client, path string, offset int, limit int) (result T, err error) {
|
||||
request := c.HTTPClient.R().
|
||||
SetContext(ctx).
|
||||
SetQueryParams(map[string]string{
|
||||
"index": strconv.Itoa(offset),
|
||||
"limit": strconv.Itoa(limit),
|
||||
|
|
|
@ -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
|
||||
|
@ -23,6 +23,7 @@ THE SOFTWARE.
|
|||
package deezer_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
|
@ -48,7 +49,8 @@ func TestGetUserHistory(t *testing.T) {
|
|||
"https://api.deezer.com/user/me/history",
|
||||
"testdata/user-history.json")
|
||||
|
||||
result, err := client.UserHistory(0, 2)
|
||||
ctx := context.Background()
|
||||
result, err := client.UserHistory(ctx, 0, 2)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert := assert.New(t)
|
||||
|
@ -69,7 +71,8 @@ func TestGetUserTracks(t *testing.T) {
|
|||
"https://api.deezer.com/user/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)
|
||||
|
|
|
@ -16,6 +16,7 @@ Scotty. If not, see <https://www.gnu.org/licenses/>.
|
|||
package deezer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"net/url"
|
||||
|
@ -78,6 +79,8 @@ func (b *DeezerApiBackend) OAuth2Setup(token oauth2.TokenSource) error {
|
|||
}
|
||||
|
||||
func (b *DeezerApiBackend) ExportListens(oldestTimestamp time.Time, results chan models.ListensResult, 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
|
||||
|
@ -96,7 +99,7 @@ func (b *DeezerApiBackend) ExportListens(oldestTimestamp time.Time, results chan
|
|||
|
||||
out:
|
||||
for {
|
||||
result, err := b.client.UserHistory(offset, perPage)
|
||||
result, err := b.client.UserHistory(ctx, offset, perPage)
|
||||
if err != nil {
|
||||
p.Export.Abort()
|
||||
progress <- p
|
||||
|
@ -154,6 +157,8 @@ out:
|
|||
}
|
||||
|
||||
func (b *DeezerApiBackend) 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
|
||||
|
@ -168,7 +173,7 @@ func (b *DeezerApiBackend) ExportLoves(oldestTimestamp time.Time, results chan m
|
|||
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue