-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify_adapter.go
89 lines (75 loc) · 2.15 KB
/
spotify_adapter.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package streamnx
import (
"context"
"errors"
"fmt"
"github.com/GeorgeGorbanev/streamnx/internal/spotify"
)
type SpotifyAdapter struct {
client spotify.Client
}
func newSpotifyAdapter(client spotify.Client) *SpotifyAdapter {
return &SpotifyAdapter{
client: client,
}
}
func (a *SpotifyAdapter) FetchTrack(ctx context.Context, id string) (*Entity, error) {
track, err := a.client.FetchTrack(ctx, id)
if err != nil {
if errors.Is(err, spotify.NotFoundError) {
return nil, EntityNotFoundError
}
return nil, fmt.Errorf("failed to get track from spotify: %w", err)
}
return a.adaptTrack(track), nil
}
func (a *SpotifyAdapter) SearchTrack(ctx context.Context, artistName, trackName string) (*Entity, error) {
track, err := a.client.SearchTrack(ctx, artistName, trackName)
if err != nil {
if errors.Is(err, spotify.NotFoundError) {
return nil, EntityNotFoundError
}
return nil, fmt.Errorf("failed to search track on spotify: %w", err)
}
return a.adaptTrack(track), nil
}
func (a *SpotifyAdapter) FetchAlbum(ctx context.Context, id string) (*Entity, error) {
album, err := a.client.FetchAlbum(ctx, id)
if err != nil {
if errors.Is(err, spotify.NotFoundError) {
return nil, EntityNotFoundError
}
return nil, fmt.Errorf("failed to get album from spotify: %w", err)
}
return a.adaptAlbum(album), nil
}
func (a *SpotifyAdapter) SearchAlbum(ctx context.Context, artistName, albumName string) (*Entity, error) {
album, err := a.client.SearchAlbum(ctx, artistName, albumName)
if err != nil {
if errors.Is(err, spotify.NotFoundError) {
return nil, EntityNotFoundError
}
return nil, fmt.Errorf("failed to search album on spotify: %w", err)
}
return a.adaptAlbum(album), nil
}
func (a *SpotifyAdapter) adaptTrack(track *spotify.Track) *Entity {
return &Entity{
ID: track.ID,
Title: track.Name,
Artist: track.Artists[0].Name,
URL: track.URL(),
Provider: Spotify,
Type: Track,
}
}
func (a *SpotifyAdapter) adaptAlbum(album *spotify.Album) *Entity {
return &Entity{
ID: album.ID,
Title: album.Name,
Artist: album.Artists[0].Name,
URL: album.URL(),
Provider: Spotify,
Type: Album,
}
}