mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-30 21:57:06 +02:00
Unified code for backend clients and tests
This commit is contained in:
parent
9316838d59
commit
aa01ae1342
11 changed files with 220 additions and 42 deletions
|
@ -22,14 +22,15 @@ THE SOFTWARE.
|
|||
package maloja
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
resty *resty.Client
|
||||
token string
|
||||
HttpClient *resty.Client
|
||||
token string
|
||||
}
|
||||
|
||||
func NewClient(serverUrl string, token string) Client {
|
||||
|
@ -37,22 +38,26 @@ func NewClient(serverUrl string, token string) Client {
|
|||
resty.SetBaseURL(serverUrl)
|
||||
resty.SetHeader("Accept", "application/json")
|
||||
client := Client{
|
||||
resty: resty,
|
||||
token: token,
|
||||
HttpClient: resty,
|
||||
token: token,
|
||||
}
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
func (c Client) GetListens(page int, perPage int) (GetListensResult, error) {
|
||||
func (c Client) GetScrobbles(page int, perPage int) (result GetScrobblesResult, err error) {
|
||||
const path = "/apis/mlj_1/scrobbles"
|
||||
result := &GetListensResult{}
|
||||
_, err := c.resty.R().
|
||||
response, err := c.HttpClient.R().
|
||||
SetQueryParams(map[string]string{
|
||||
"page": strconv.Itoa(page),
|
||||
"perpage": strconv.Itoa(perPage),
|
||||
}).
|
||||
SetResult(result).
|
||||
SetResult(&result).
|
||||
Get(path)
|
||||
return *result, err
|
||||
|
||||
if response.StatusCode() != 200 {
|
||||
err = errors.New(response.String())
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue