/
user.go
56 lines (49 loc) · 1.6 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package spotify
import (
"context"
"fmt"
"github.com/camphor-/relaym-server/domain/entity"
"github.com/camphor-/relaym-server/domain/service"
"github.com/zmb3/spotify/v2"
)
// GetMe は自分の情報をSpotify APIから取得します。
func (c *Client) GetMe(ctx context.Context) (*entity.SpotifyUser, error) {
token, ok := service.GetTokenFromContext(ctx)
if !ok {
return nil, fmt.Errorf("token not found")
}
cli := spotify.New(c.auth.Client(ctx, token))
user, err := cli.CurrentUser(ctx)
if err != nil {
return nil, fmt.Errorf("get private user through spotiry api: %w", err)
}
return &entity.SpotifyUser{
SpotifyUserID: user.ID,
DisplayName: user.DisplayName,
Product: user.Product,
}, nil
}
// GetActiveDevices はSpotify APIを通して、ログインしているユーザがSpotifyを起動している端末を取得します。
func (c *Client) GetActiveDevices(ctx context.Context) ([]*entity.Device, error) {
token, ok := service.GetTokenFromContext(ctx)
if !ok {
return nil, fmt.Errorf("token not found")
}
cli := spotify.New(c.auth.Client(ctx, token))
devices, err := cli.PlayerDevices(ctx)
if err != nil {
return nil, fmt.Errorf("playerDevices information about available devices for the current user: %w", err)
}
return c.toDevices(devices), nil
}
func (c *Client) toDevices(resultDevices []spotify.PlayerDevice) []*entity.Device {
devices := make([]*entity.Device, len(resultDevices))
for i, rd := range resultDevices {
devices[i] = &entity.Device{
ID: rd.ID.String(),
IsRestricted: rd.Restricted,
Name: rd.Name,
}
}
return devices
}