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
@ -22,6 +22,7 @@ THE SOFTWARE.
package maloja
import (
"context"
"errors"
"strconv"
@ -48,9 +49,10 @@ func NewClient(serverURL string, token string) Client {
}
}
func (c Client) GetScrobbles(page int, perPage int) (result GetScrobblesResult, err error) {
func (c Client) GetScrobbles(ctx context.Context, page int, perPage int) (result GetScrobblesResult, err error) {
const path = "/apis/mlj_1/scrobbles"
response, err := c.HTTPClient.R().
SetContext(ctx).
SetQueryParams(map[string]string{
"page": strconv.Itoa(page),
"perpage": strconv.Itoa(perPage),
@ -65,10 +67,11 @@ func (c Client) GetScrobbles(page int, perPage int) (result GetScrobblesResult,
return
}
func (c Client) NewScrobble(scrobble NewScrobble) (result NewScrobbleResult, err error) {
func (c Client) NewScrobble(ctx context.Context, scrobble NewScrobble) (result NewScrobbleResult, err error) {
const path = "/apis/mlj_1/newscrobble"
scrobble.Key = c.token
response, err := c.HTTPClient.R().
SetContext(ctx).
SetBody(scrobble).
SetResult(&result).
Post(path)

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 maloja_test
import (
"context"
"net/http"
"testing"
@ -48,7 +49,8 @@ func TestGetScrobbles(t *testing.T) {
"https://maloja.example.com/apis/mlj_1/scrobbles",
"testdata/scrobbles.json")
result, err := client.GetScrobbles(0, 2)
ctx := context.Background()
result, err := client.GetScrobbles(ctx, 0, 2)
require.NoError(t, err)
assert := assert.New(t)
@ -69,12 +71,13 @@ func TestNewScrobble(t *testing.T) {
url := server + "/apis/mlj_1/newscrobble"
httpmock.RegisterResponder("POST", url, responder)
ctx := context.Background()
scrobble := maloja.NewScrobble{
Title: "Oweynagat",
Artist: "Dool",
Time: 1699574369,
}
result, err := client.NewScrobble(scrobble)
result, err := client.NewScrobble(ctx, scrobble)
require.NoError(t, err)
assert.Equal(t, "success", result.Status)

View file

@ -17,6 +17,7 @@ Scotty. If not, see <https://www.gnu.org/licenses/>.
package maloja
import (
"context"
"errors"
"sort"
"strings"
@ -64,6 +65,7 @@ func (b *MalojaApiBackend) StartImport() error { return nil }
func (b *MalojaApiBackend) FinishImport() error { return nil }
func (b *MalojaApiBackend) ExportListens(oldestTimestamp time.Time, results chan models.ListensResult, progress chan models.TransferProgress) {
ctx := context.TODO()
page := 0
perPage := MaxItemsPerGet
@ -77,7 +79,7 @@ func (b *MalojaApiBackend) ExportListens(oldestTimestamp time.Time, results chan
out:
for {
result, err := b.client.GetScrobbles(page, perPage)
result, err := b.client.GetScrobbles(ctx, page, perPage)
if err != nil {
p.Export.Abort()
progress <- p
@ -112,6 +114,8 @@ out:
}
func (b *MalojaApiBackend) ImportListens(export models.ListensResult, importResult models.ImportResult, progress chan models.TransferProgress) (models.ImportResult, error) {
ctx := context.TODO()
p := models.TransferProgress{}.FromImportResult(importResult, false)
for _, listen := range export.Items {
scrobble := NewScrobble{
@ -124,7 +128,7 @@ func (b *MalojaApiBackend) ImportListens(export models.ListensResult, importResu
Nofix: b.nofix,
}
resp, err := b.client.NewScrobble(scrobble)
resp, err := b.client.NewScrobble(ctx, scrobble)
if err != nil {
return importResult, err
} else if resp.Status != "success" {