mirror of
https://git.sr.ht/~phw/scotty
synced 2025-05-01 14:17:06 +02:00
Custom OAuth2 TokenSource to persist refreshed tokens in database
This commit is contained in:
parent
4a9f26d9db
commit
e29d8e2a63
8 changed files with 92 additions and 39 deletions
|
@ -39,9 +39,9 @@ type Client struct {
|
|||
HttpClient *resty.Client
|
||||
}
|
||||
|
||||
func NewClient(conf oauth2.Config, token *oauth2.Token) Client {
|
||||
func NewClient(token oauth2.TokenSource) Client {
|
||||
ctx := context.Background()
|
||||
httpClient := conf.Client(ctx, token)
|
||||
httpClient := oauth2.NewClient(ctx, token)
|
||||
client := resty.NewWithClient(httpClient)
|
||||
client.SetBaseURL(baseURL)
|
||||
client.SetHeader("Accept", "application/json")
|
||||
|
|
|
@ -34,16 +34,15 @@ import (
|
|||
)
|
||||
|
||||
func TestNewClient(t *testing.T) {
|
||||
conf := oauth2.Config{}
|
||||
token := &oauth2.Token{}
|
||||
client := spotify.NewClient(conf, token)
|
||||
token := oauth2.StaticTokenSource(&oauth2.Token{})
|
||||
client := spotify.NewClient(token)
|
||||
assert.IsType(t, spotify.Client{}, client)
|
||||
}
|
||||
|
||||
func TestRecentlyPlayedAfter(t *testing.T) {
|
||||
defer httpmock.DeactivateAndReset()
|
||||
|
||||
client := spotify.NewClient(oauth2.Config{}, nil)
|
||||
client := spotify.NewClient(nil)
|
||||
setupHttpMock(t, client.HttpClient.GetClient(),
|
||||
"https://api.spotify.com/v1/me/player/recently-played",
|
||||
"testdata/recently-played.json")
|
||||
|
@ -63,7 +62,7 @@ func TestRecentlyPlayedAfter(t *testing.T) {
|
|||
func TestGetUserTracks(t *testing.T) {
|
||||
defer httpmock.DeactivateAndReset()
|
||||
|
||||
client := spotify.NewClient(oauth2.Config{}, nil)
|
||||
client := spotify.NewClient(nil)
|
||||
setupHttpMock(t, client.HttpClient.GetClient(),
|
||||
"https://api.spotify.com/v1/me/tracks",
|
||||
"testdata/user-tracks.json")
|
||||
|
|
|
@ -59,9 +59,8 @@ func (b *SpotifyApiBackend) OAuth2Config(redirectUrl *url.URL) oauth2.Config {
|
|||
}
|
||||
}
|
||||
|
||||
func (b *SpotifyApiBackend) OAuth2Setup(redirectUrl *url.URL, token *oauth2.Token) error {
|
||||
config := b.OAuth2Config(redirectUrl)
|
||||
b.client = NewClient(config, token)
|
||||
func (b *SpotifyApiBackend) OAuth2Setup(token oauth2.TokenSource) error {
|
||||
b.client = NewClient(token)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue