mirror of
https://git.sr.ht/~phw/scotty
synced 2025-05-03 23:17:05 +02:00
Deezer export listens
This commit is contained in:
parent
3a364b6ae4
commit
1a06168039
8 changed files with 318 additions and 30 deletions
|
@ -31,7 +31,7 @@ import (
|
|||
)
|
||||
|
||||
const baseURL = "https://api.deezer.com/"
|
||||
const MaxItemsPerGet = 50
|
||||
const MaxItemsPerGet = 1000
|
||||
const DefaultRateLimitWaitSeconds = 5
|
||||
|
||||
type Client struct {
|
||||
|
@ -50,23 +50,14 @@ func NewClient(token oauth2.TokenSource) Client {
|
|||
}
|
||||
}
|
||||
|
||||
func (c Client) UserTracks(offset int, limit int) (result TracksResult, err error) {
|
||||
const path = "/user/me/tracks"
|
||||
request := c.HttpClient.R().
|
||||
SetQueryParams(map[string]string{
|
||||
"index": strconv.Itoa(offset),
|
||||
"limit": strconv.Itoa(limit),
|
||||
}).
|
||||
SetResult(&result)
|
||||
c.setToken(request)
|
||||
response, err := request.Get(path)
|
||||
func (c Client) UserHistory(offset int, limit int) (result HistoryResult, err error) {
|
||||
const path = "/user/me/history"
|
||||
return listRequest[HistoryResult](c, path, offset, limit)
|
||||
}
|
||||
|
||||
if response.StatusCode() != 200 {
|
||||
err = errors.New(response.String())
|
||||
} else if result.Error != nil {
|
||||
err = errors.New(result.Error.Message)
|
||||
}
|
||||
return
|
||||
func (c Client) UserTracks(offset int, limit int) (TracksResult, error) {
|
||||
const path = "/user/me/tracks"
|
||||
return listRequest[TracksResult](c, path, offset, limit)
|
||||
}
|
||||
|
||||
func (c Client) setToken(req *resty.Request) error {
|
||||
|
@ -78,3 +69,21 @@ func (c Client) setToken(req *resty.Request) error {
|
|||
req.SetQueryParam("access_token", tok.AccessToken)
|
||||
return nil
|
||||
}
|
||||
|
||||
func listRequest[T Result](c Client, path string, offset int, limit int) (result T, err error) {
|
||||
request := c.HttpClient.R().
|
||||
SetQueryParams(map[string]string{
|
||||
"index": strconv.Itoa(offset),
|
||||
"limit": strconv.Itoa(limit),
|
||||
}).
|
||||
SetResult(&result)
|
||||
c.setToken(request)
|
||||
response, err := request.Get(path)
|
||||
|
||||
if response.StatusCode() != 200 {
|
||||
err = errors.New(response.String())
|
||||
} else if result.Error() != nil {
|
||||
err = errors.New(result.Error().Message)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue