-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapple_adapter.go
123 lines (107 loc) · 2.95 KB
/
apple_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package streamnx
import (
"context"
"errors"
"fmt"
"github.com/GeorgeGorbanev/streamnx/internal/apple"
)
type AppleAdapter struct {
client apple.Client
}
func newAppleAdapter(client apple.Client) *AppleAdapter {
return &AppleAdapter{
client: client,
}
}
func (a *AppleAdapter) FetchTrack(ctx context.Context, id string) (*Entity, error) {
ck := apple.CompositeKey{}
if err := ck.Unmarshal(id); err != nil {
return nil, fmt.Errorf("failed to unmarshal track id: %w", err)
}
track, err := a.client.FetchTrack(ctx, ck.ID, ck.Storefront)
if err != nil {
if errors.Is(err, apple.NotFoundError) {
return nil, EntityNotFoundError
}
return nil, fmt.Errorf("failed to get track from apple: %w", err)
}
res, err := a.adaptTrack(track)
if err != nil {
return nil, err
}
return res, nil
}
func (a *AppleAdapter) SearchTrack(ctx context.Context, artistName, trackName string) (*Entity, error) {
track, err := a.client.SearchTrack(ctx, artistName, trackName)
if err != nil {
if errors.Is(err, apple.NotFoundError) {
return nil, EntityNotFoundError
}
return nil, fmt.Errorf("failed to search track from apple: %w", err)
}
res, err := a.adaptTrack(track)
if err != nil {
return nil, err
}
return res, nil
}
func (a *AppleAdapter) FetchAlbum(ctx context.Context, id string) (*Entity, error) {
ck := apple.CompositeKey{}
if err := ck.Unmarshal(id); err != nil {
return nil, fmt.Errorf("failed to unmarshal album id: %w", err)
}
album, err := a.client.FetchAlbum(ctx, ck.ID, ck.Storefront)
if err != nil {
if errors.Is(err, apple.NotFoundError) {
return nil, EntityNotFoundError
}
return nil, fmt.Errorf("failed to get album from apple: %w", err)
}
res, err := a.adaptAlbum(album)
if err != nil {
return nil, err
}
return res, nil
}
func (a *AppleAdapter) SearchAlbum(ctx context.Context, artistName, albumName string) (*Entity, error) {
album, err := a.client.SearchAlbum(ctx, artistName, albumName)
if err != nil {
if errors.Is(err, apple.NotFoundError) {
return nil, EntityNotFoundError
}
return nil, fmt.Errorf("failed to search album from apple: %w", err)
}
res, err := a.adaptAlbum(album)
if err != nil {
return nil, err
}
return res, nil
}
func (a *AppleAdapter) adaptTrack(track *apple.Entity) (*Entity, error) {
ck := apple.CompositeKey{}
if err := ck.ParseFromTrackURL(track.Attributes.URL); err != nil {
return nil, err
}
return &Entity{
ID: ck.Marshal(),
Title: track.Attributes.Name,
Artist: track.Attributes.ArtistName,
URL: track.Attributes.URL,
Provider: Apple,
Type: Track,
}, nil
}
func (a *AppleAdapter) adaptAlbum(album *apple.Entity) (*Entity, error) {
ck := apple.CompositeKey{}
if err := ck.ParseFromAlbumURL(album.Attributes.URL); err != nil {
return nil, err
}
return &Entity{
ID: ck.Marshal(),
Title: album.Attributes.Name,
Artist: album.Attributes.ArtistName,
URL: album.Attributes.URL,
Provider: Apple,
Type: Album,
}, nil
}