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

@ -42,8 +42,8 @@ func TestNewClient(t *testing.T) {
func TestGetHistoryListenings(t *testing.T) { func TestGetHistoryListenings(t *testing.T) {
defer httpmock.DeactivateAndReset() defer httpmock.DeactivateAndReset()
token := "thetoken"
serverUrl := "https://funkwhale.example.com" serverUrl := "https://funkwhale.example.com"
token := "thetoken"
client := funkwhale.NewClient(serverUrl, token) client := funkwhale.NewClient(serverUrl, token)
setupHttpMock(t, client.HttpClient.GetClient(), setupHttpMock(t, client.HttpClient.GetClient(),
"https://funkwhale.example.com/api/v1/history/listenings", "https://funkwhale.example.com/api/v1/history/listenings",

View file

@ -22,6 +22,7 @@ THE SOFTWARE.
package listenbrainz package listenbrainz
import ( import (
"errors"
"strconv" "strconv"
"time" "time"
@ -52,17 +53,21 @@ func NewClient(token string) Client {
return client return client
} }
func (c Client) GetListens(user string, maxTime time.Time, minTime time.Time) (GetListensResult, error) { func (c Client) GetListens(user string, maxTime time.Time, minTime time.Time) (result GetListensResult, err error) {
const path = "/user/{username}/listens" const path = "/user/{username}/listens"
result := &GetListensResult{} response, err := c.HttpClient.R().
_, err := c.HttpClient.R().
SetPathParam("username", user). SetPathParam("username", user).
SetQueryParams(map[string]string{ SetQueryParams(map[string]string{
"max_ts": strconv.FormatInt(maxTime.Unix(), 10), "max_ts": strconv.FormatInt(maxTime.Unix(), 10),
"min_ts": strconv.FormatInt(minTime.Unix(), 10), "min_ts": strconv.FormatInt(minTime.Unix(), 10),
"count": strconv.FormatInt(int64(c.MaxResults), 10), "count": strconv.FormatInt(int64(c.MaxResults), 10),
}). }).
SetResult(result). SetResult(&result).
Get(path) Get(path)
return *result, err
if response.StatusCode() != 200 {
err = errors.New(response.String())
return
}
return
} }

View file

@ -53,6 +53,7 @@ func TestGetListens(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
assert.Equal(2, result.Payload.Count) assert.Equal(2, result.Payload.Count)
require.Len(t, result.Payload.Listens, 2)
assert.Equal("Shadowplay", result.Payload.Listens[0].TrackMetadata.TrackName) assert.Equal("Shadowplay", result.Payload.Listens[0].TrackMetadata.TrackName)
} }

View file

@ -63,7 +63,7 @@ out:
for _, listen := range result.Payload.Listens { for _, listen := range result.Payload.Listens {
if listen.ListenedAt > oldestTimestamp.Unix() { if listen.ListenedAt > oldestTimestamp.Unix() {
listens = append(listens, ListenFromListenBrainz(listen)) listens = append(listens, listen.ToListen())
} else { } else {
// result contains listens older then oldestTimestamp, // result contains listens older then oldestTimestamp,
// we can stop requesting more // we can stop requesting more
@ -76,7 +76,7 @@ out:
return listens, nil return listens, nil
} }
func ListenFromListenBrainz(lbListen Listen) models.Listen { func (lbListen Listen) ToListen() models.Listen {
track := lbListen.TrackMetadata track := lbListen.TrackMetadata
listen := models.Listen{ listen := models.Listen{
ListenedAt: time.Unix(lbListen.ListenedAt, 0), ListenedAt: time.Unix(lbListen.ListenedAt, 0),

View file

@ -30,36 +30,36 @@ import (
"go.uploadedlobster.com/scotty/models" "go.uploadedlobster.com/scotty/models"
) )
func TestListenFromListenBrainz(t *testing.T) { func TestListenBrainzListenToListen(t *testing.T) {
lbListen := listenbrainz.Listen{ lbListen := listenbrainz.Listen{
ListenedAt: 1699289873, ListenedAt: 1699289873,
UserName: "outsidecontext", UserName: "outsidecontext",
TrackMetadata: listenbrainz.Track{ TrackMetadata: listenbrainz.Track{
TrackName: "The Track", TrackName: "The Track",
ArtistName: "The Artist", ArtistName: "Dool",
ReleaseName: "The Release", ReleaseName: "Here Now, There Then",
AdditionalInfo: map[string]any{ AdditionalInfo: map[string]any{
"duration_ms": 528235, "duration_ms": 413787,
"foo": "bar", "foo": "bar",
"isrc": "DES561720901", "isrc": "DES561620801",
"tracknumber": 8, "tracknumber": 5,
"recording_mbid": "e225fb84-dc9a-419e-adcd-9890f59ec432", "recording_mbid": "c0a1fc94-5f04-4a5f-bc09-e5de0c49cd12",
"release_group_mbid": "80aca1ee-aa51-41be-9f75-024710d92ff4", "release_group_mbid": "80aca1ee-aa51-41be-9f75-024710d92ff4",
"release_mbid": "d7f22677-9803-4d21-ba42-081b633a6f68", "release_mbid": "d7f22677-9803-4d21-ba42-081b633a6f68",
}, },
}, },
} }
listen := listenbrainz.ListenFromListenBrainz(lbListen) listen := lbListen.ToListen()
assert.Equal(t, time.Unix(1699289873, 0), listen.ListenedAt) assert.Equal(t, time.Unix(1699289873, 0), listen.ListenedAt)
assert.Equal(t, lbListen.UserName, listen.UserName) assert.Equal(t, lbListen.UserName, listen.UserName)
assert.Equal(t, time.Duration(528235*time.Millisecond), listen.Duration) assert.Equal(t, time.Duration(413787*time.Millisecond), listen.Duration)
assert.Equal(t, lbListen.TrackMetadata.TrackName, listen.TrackName) assert.Equal(t, lbListen.TrackMetadata.TrackName, listen.TrackName)
assert.Equal(t, lbListen.TrackMetadata.ReleaseName, listen.ReleaseName) assert.Equal(t, lbListen.TrackMetadata.ReleaseName, listen.ReleaseName)
assert.Equal(t, []string{lbListen.TrackMetadata.ArtistName}, listen.ArtistNames) assert.Equal(t, []string{lbListen.TrackMetadata.ArtistName}, listen.ArtistNames)
assert.Equal(t, 8, listen.TrackNumber) assert.Equal(t, 5, listen.TrackNumber)
assert.Equal(t, models.MBID("e225fb84-dc9a-419e-adcd-9890f59ec432"), listen.RecordingMbid) assert.Equal(t, models.MBID("c0a1fc94-5f04-4a5f-bc09-e5de0c49cd12"), listen.RecordingMbid)
assert.Equal(t, models.MBID("d7f22677-9803-4d21-ba42-081b633a6f68"), listen.ReleaseMbid) assert.Equal(t, models.MBID("d7f22677-9803-4d21-ba42-081b633a6f68"), listen.ReleaseMbid)
assert.Equal(t, models.MBID("80aca1ee-aa51-41be-9f75-024710d92ff4"), listen.ReleaseGroupMbid) assert.Equal(t, models.MBID("80aca1ee-aa51-41be-9f75-024710d92ff4"), listen.ReleaseGroupMbid)
assert.Equal(t, "DES561720901", listen.Isrc) assert.Equal(t, "DES561620801", listen.Isrc)
assert.Equal(t, lbListen.TrackMetadata.AdditionalInfo["foo"], listen.AdditionalInfo["foo"]) assert.Equal(t, lbListen.TrackMetadata.AdditionalInfo["foo"], listen.AdditionalInfo["foo"])
} }

View file

@ -22,14 +22,15 @@ THE SOFTWARE.
package maloja package maloja
import ( import (
"errors"
"strconv" "strconv"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
) )
type Client struct { type Client struct {
resty *resty.Client HttpClient *resty.Client
token string token string
} }
func NewClient(serverUrl string, token string) Client { func NewClient(serverUrl string, token string) Client {
@ -37,22 +38,26 @@ func NewClient(serverUrl string, token string) Client {
resty.SetBaseURL(serverUrl) resty.SetBaseURL(serverUrl)
resty.SetHeader("Accept", "application/json") resty.SetHeader("Accept", "application/json")
client := Client{ client := Client{
resty: resty, HttpClient: resty,
token: token, token: token,
} }
return client 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" const path = "/apis/mlj_1/scrobbles"
result := &GetListensResult{} response, err := c.HttpClient.R().
_, err := c.resty.R().
SetQueryParams(map[string]string{ SetQueryParams(map[string]string{
"page": strconv.Itoa(page), "page": strconv.Itoa(page),
"perpage": strconv.Itoa(perPage), "perpage": strconv.Itoa(perPage),
}). }).
SetResult(result). SetResult(&result).
Get(path) Get(path)
return *result, err
if response.StatusCode() != 200 {
err = errors.New(response.String())
return
}
return
} }

View file

@ -0,0 +1,68 @@
/*
Copyright © 2023 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
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package maloja_test
import (
"net/http"
"testing"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uploadedlobster.com/scotty/backends/maloja"
)
func TestNewClient(t *testing.T) {
serverUrl := "https://maloja.example.com"
token := "foobar123"
client := maloja.NewClient(serverUrl, token)
assert.Equal(t, serverUrl, client.HttpClient.BaseURL)
}
func TestGetScrobbles(t *testing.T) {
defer httpmock.DeactivateAndReset()
serverUrl := "https://maloja.example.com"
token := "thetoken"
client := maloja.NewClient(serverUrl, token)
setupHttpMock(t, client.HttpClient.GetClient(),
"https://maloja.example.com/apis/mlj_1/scrobbles",
"testdata/scrobbles.json")
result, err := client.GetScrobbles(0, 2)
require.NoError(t, err)
assert := assert.New(t)
require.Len(t, result.List, 2)
assert.Equal("Way to Eden", result.List[0].Track.Title)
assert.Equal(int64(558), result.List[0].Duration)
}
func setupHttpMock(t *testing.T, client *http.Client, url string, testDataPath string) {
httpmock.ActivateNonDefault(client)
responder, err := httpmock.NewJsonResponder(200, httpmock.File(testDataPath))
if err != nil {
t.Fatal(err)
}
httpmock.RegisterResponder("GET", url, responder)
}

View file

@ -50,19 +50,19 @@ func (b MalojaApiBackend) ExportListens(oldestTimestamp time.Time) ([]models.Lis
out: out:
for { for {
result, err := b.client.GetListens(page, perPage) result, err := b.client.GetScrobbles(page, perPage)
if err != nil { if err != nil {
return nil, err return nil, err
} }
count := len(result.Listens) count := len(result.List)
if count == 0 { if count == 0 {
break break
} }
for _, listen := range result.Listens { for _, scrobble := range result.List {
if listen.ListenedAt > oldestTimestamp.Unix() { if scrobble.ListenedAt > oldestTimestamp.Unix() {
listens = append(listens, ListenFromMaloja(listen)) listens = append(listens, scrobble.ToListen())
} else { } else {
break out break out
} }
@ -75,11 +75,11 @@ out:
return listens, nil return listens, nil
} }
func ListenFromMaloja(mlListen Listen) models.Listen { func (s Scrobble) ToListen() models.Listen {
track := mlListen.Track track := s.Track
listen := models.Listen{ listen := models.Listen{
ListenedAt: time.Unix(mlListen.ListenedAt, 0), ListenedAt: time.Unix(s.ListenedAt, 0),
PlaybackDuration: time.Duration(mlListen.Duration * int64(time.Second)), PlaybackDuration: time.Duration(s.Duration * int64(time.Second)),
Track: models.Track{ Track: models.Track{
TrackName: track.Title, TrackName: track.Title,
ReleaseName: track.Album.Title, ReleaseName: track.Album.Title,
@ -89,7 +89,7 @@ func ListenFromMaloja(mlListen Listen) models.Listen {
}, },
} }
client, found := strings.CutPrefix(mlListen.Origin, "client:") client, found := strings.CutPrefix(s.Origin, "client:")
if found { if found {
listen.AdditionalInfo["media_player"] = client listen.AdditionalInfo["media_player"] = client
} }

View file

@ -0,0 +1,52 @@
/*
Copyright © 2023 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
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package maloja_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.uploadedlobster.com/scotty/backends/maloja"
)
func TestScrobbleToListen(t *testing.T) {
scrobble := maloja.Scrobble{
ListenedAt: 1699289873,
Track: maloja.Track{
Title: "Oweynagat",
Album: maloja.Album{
Title: "Here Now, There Then",
},
Artists: []string{"Dool"},
Length: 414,
},
Origin: "client:Funkwhale",
}
listen := scrobble.ToListen()
assert.Equal(t, time.Unix(1699289873, 0), listen.ListenedAt)
assert.Equal(t, time.Duration(414*time.Second), listen.Duration)
assert.Equal(t, scrobble.Track.Title, listen.TrackName)
assert.Equal(t, scrobble.Track.Album.Title, listen.ReleaseName)
assert.Equal(t, scrobble.Track.Artists, listen.ArtistNames)
assert.Equal(t, "Funkwhale", listen.AdditionalInfo["media_player"])
}

View file

@ -21,13 +21,13 @@ THE SOFTWARE.
*/ */
package maloja package maloja
type GetListensResult struct { type GetScrobblesResult struct {
Status string `json:"status"` Status string `json:"status"`
Listens []Listen `json:"list"` List []Scrobble `json:"list"`
Pagination Pagination `json:"pagination"` Pagination Pagination `json:"pagination"`
} }
type Listen struct { type Scrobble struct {
ListenedAt int64 `json:"time"` ListenedAt int64 `json:"time"`
Duration int64 `json:"duration"` Duration int64 `json:"duration"`
// Maloja sets Origin to the name of the API key // Maloja sets Origin to the name of the API key

47
backends/maloja/testdata/scrobbles.json vendored Normal file
View file

@ -0,0 +1,47 @@
{
"status": "ok",
"list": [
{
"time": 1699574369,
"track": {
"artists": [
"Hazeshuttle"
],
"title": "Way to Eden",
"album": {
"artists": [
"Hazeshuttle"
],
"albumtitle": "Hazeshuttle"
},
"length": 567
},
"duration": 558,
"origin": "client:Funkwhale"
},
{
"time": 1699573362,
"track": {
"artists": [
"Hazeshuttle"
],
"title": "Homosativa",
"album": {
"artists": [
"Hazeshuttle"
],
"albumtitle": "Hazeshuttle"
},
"length": 1007
},
"duration": null,
"origin": "client:Funkwhale"
}
],
"pagination": {
"page": 0,
"perpage": 2,
"next_page": "/apis/mlj_1/scrobbles?page=1&perpage=2",
"prev_page": null
}
}