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 funkwhale
import (
"context"
"errors"
"strconv"
@ -54,15 +55,10 @@ func NewClient(serverURL string, token string) Client {
}
}
func (c Client) GetHistoryListenings(user string, page int, perPage int) (result ListeningsResult, err error) {
func (c Client) GetHistoryListenings(ctx context.Context, user string, page int, perPage int) (result ListeningsResult, err error) {
const path = "/api/v1/history/listenings"
response, err := c.HTTPClient.R().
SetQueryParams(map[string]string{
"username": user,
"page": strconv.Itoa(page),
"page_size": strconv.Itoa(perPage),
"ordering": "-creation_date",
}).
response, err := c.buildListRequest(ctx, page, perPage).
SetQueryParam("username", user).
SetResult(&result).
Get(path)
@ -73,14 +69,9 @@ func (c Client) GetHistoryListenings(user string, page int, perPage int) (result
return
}
func (c Client) GetFavoriteTracks(page int, perPage int) (result FavoriteTracksResult, err error) {
func (c Client) GetFavoriteTracks(ctx context.Context, page int, perPage int) (result FavoriteTracksResult, err error) {
const path = "/api/v1/favorites/tracks"
response, err := c.HTTPClient.R().
SetQueryParams(map[string]string{
"page": strconv.Itoa(page),
"page_size": strconv.Itoa(perPage),
"ordering": "-creation_date",
}).
response, err := c.buildListRequest(ctx, page, perPage).
SetResult(&result).
Get(path)
@ -90,3 +81,13 @@ func (c Client) GetFavoriteTracks(page int, perPage int) (result FavoriteTracksR
}
return
}
func (c Client) buildListRequest(ctx context.Context, page int, perPage int) *resty.Request {
return c.HTTPClient.R().
SetContext(ctx).
SetQueryParams(map[string]string{
"page": strconv.Itoa(page),
"page_size": strconv.Itoa(perPage),
"ordering": "-creation_date",
})
}