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
@ -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),