Unified code for backend clients and tests

This commit is contained in:
Philipp Wolfer 2023-11-12 16:28:23 +01:00
parent 9316838d59
commit aa01ae1342
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
11 changed files with 220 additions and 42 deletions

View file

@ -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
}