-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathuser_collection.go
135 lines (121 loc) · 4.41 KB
/
user_collection.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
124
125
126
127
128
129
130
131
132
133
134
135
package discogs
import (
"strconv"
)
// CollectionService is an interface to work with collection.
type CollectionService interface {
// Retrieve a list of folders in a user’s collection.
// If folder_id is not 0, authentication as the collection owner is required.
CollectionFolders(username string) (*CollectionFolders, error)
// Retrieve a list of items in a folder in a user’s collection.
// If folderID is not 0, authentication with token is required.
CollectionItemsByFolder(username string, folderID int, pagination *Pagination) (*CollectionItems, error)
// Retrieve the user’s collection folders which contain a specified release.
// The releaseID must be non-zero.
CollectionItemsByRelease(username string, releaseID int) (*CollectionItems, error)
// Retrieve metadata about a folder in a user’s collection.
Folder(username string, folderID int) (*Folder, error)
}
type collectionService struct {
url string
}
func newCollectionService(url string) CollectionService {
return &collectionService{
url: url,
}
}
// Folder serves folder response from discogs.
type Folder struct {
ID int `json:"id"`
Count int `json:"count"`
Name string `json:"name"`
ResourceURL string `json:"resource_url"`
}
func (s *collectionService) Folder(username string, folderID int) (*Folder, error) {
if username == "" {
return nil, ErrInvalidUsername
}
var folder *Folder
err := request(s.url+"/"+username+"/collection/folders/"+strconv.Itoa(folderID), nil, &folder)
return folder, err
}
// CollectionFolders serves collection response from discogs.
type CollectionFolders struct {
Folders []Folder `json:"folders"`
}
func (s *collectionService) CollectionFolders(username string) (*CollectionFolders, error) {
if username == "" {
return nil, ErrInvalidUsername
}
var collection *CollectionFolders
err := request(s.url+"/"+username+"/collection/folders", nil, &collection)
return collection, err
}
// CollectionItemSource ...
type CollectionItemSource struct {
ID int `json:"id"`
BasicInformation BasicInformation `json:"basic_information"`
DateAdded string `json:"date_added"`
FolderID int `json:"folder_id,omitempty"`
InstanceID int `json:"instance_id"`
Notes []Notes `json:"notes,omitempty"`
Rating int `json:"rating"`
}
// BasicInformation ...
type BasicInformation struct {
ID int `json:"id"`
Artists []ArtistSource `json:"artists"`
CoverImage string `json:"cover_image"`
Formats []Format `json:"formats"`
Labels []LabelSource `json:"labels"`
Genres []string `json:"genres"`
MasterID int `json:"master_id"`
MasterURL *string `json:"master_url"`
ResourceURL string `json:"resource_url"`
Styles []string `json:"styles"`
Thumb string `json:"thumb"`
Title string `json:"title"`
Year int `json:"year"`
}
// CollectionItems list of items in a user’s collection
type CollectionItems struct {
Pagination Page `json:"pagination"`
Items []CollectionItemSource `json:"releases"`
}
// valid sort keys
// https://www.discogs.com/developers#page:user-collection,header:user-collection-collection-items-by-folder
var validItemsByFolderSort = map[string]struct{}{
"": struct{}{},
"label": struct{}{},
"artist": struct{}{},
"title": struct{}{},
"catno": struct{}{},
"format": struct{}{},
"rating": struct{}{},
"added": struct{}{},
"year": struct{}{},
}
func (s *collectionService) CollectionItemsByFolder(username string, folderID int, pagination *Pagination) (*CollectionItems, error) {
if username == "" {
return nil, ErrInvalidUsername
}
if pagination != nil {
if _, ok := validItemsByFolderSort[pagination.Sort]; !ok {
return nil, ErrInvalidSortKey
}
}
var items *CollectionItems
err := request(s.url+"/"+username+"/collection/folders/"+strconv.Itoa(folderID)+"/releases", pagination.params(), &items)
return items, err
}
func (s *collectionService) CollectionItemsByRelease(username string, releaseID int) (*CollectionItems, error) {
if username == "" {
return nil, ErrInvalidUsername
}
if releaseID == 0 {
return nil, ErrInvalidReleaseID
}
var items *CollectionItems
err := request(s.url+"/"+username+"/collection/releases/"+strconv.Itoa(releaseID), nil, &items)
return items, err
}