/
collections_service.go
221 lines (206 loc) · 7.45 KB
/
collections_service.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (c) 2017 Hardik Bagdi <hbagdi1@binghamton.edu>
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package unsplash
import (
"encoding/json"
"errors"
"fmt"
)
// CollectionsService interacts with /users endpoint
type CollectionsService service
// All returns a list of all collections on unsplash.
// Note that some fields in collection structs from this result will be missing.
// Use Collection() method to get all details of the Collection.
func (cs *CollectionsService) All(opt *ListOpt) (*[]Collection, *Response, error) {
s := (service)(*cs)
return s.getCollections(opt, getEndpoint(collections))
}
// Featured returns a list of featured collections on unsplash.
// Note that some fields in collection structs from this result will be missing.
// Use Collection() method to get all details of the Collection.
func (cs *CollectionsService) Featured(opt *ListOpt) (*[]Collection, *Response, error) {
s := (service)(*cs)
return s.getCollections(opt, getEndpoint(collections)+"/featured")
}
// Curated returns a list of curated collections on unsplash.
// Note that some fields in collection structs from this result will be missing.
// Use Collection() method to get all details of the Collection.
func (cs *CollectionsService) Curated(opt *ListOpt) (*[]Collection, *Response, error) {
s := (service)(*cs)
return s.getCollections(opt, getEndpoint(collections)+"/curated")
}
// Related returns a list of collections related to collections with id.
func (cs *CollectionsService) Related(id string, opt *ListOpt) (*[]Collection, *Response, error) {
if "" == id {
return nil, nil, &IllegalArgumentError{ErrString: "Collection ID cannot be nil"}
}
s := (service)(*cs)
endpoint := fmt.Sprintf("%v/%v/%v", getEndpoint(collections), id, "related")
return s.getCollections(opt, endpoint)
}
// Collection returns a collection with id.
func (cs *CollectionsService) Collection(id string) (*Collection, *Response, error) {
if "" == id {
return nil, nil, &IllegalArgumentError{ErrString: "Collection ID cannot be nil"}
}
endpoint := fmt.Sprintf("%v/%v", getEndpoint(collections), id)
req, err := newRequest(GET, endpoint, nil, nil)
if err != nil {
return nil, nil, err
}
resp, err := cs.client.do(req)
if err != nil {
return nil, nil, err
}
var collection Collection
err = json.Unmarshal(*resp.body, &collection)
if err != nil {
return nil, nil, err
}
return &collection, resp, nil
}
//CollectionOpt shows various available optional parameters available
//during creatioin of collection
type CollectionOpt struct {
Title *string `url:"title,omitempty"`
Description *string `url:"description,omitempty"`
Private *bool `url:"private,omitempty"`
}
//Create creates a new collection on the authenticated user's profile.
func (cs *CollectionsService) Create(opt *CollectionOpt) (*Collection, *Response, error) {
if nil == opt {
return nil, nil, &IllegalArgumentError{ErrString: "Opt cannot be nil"}
}
if *opt.Title == "" {
return nil, nil, &IllegalArgumentError{ErrString: "Need to provide a title for the new collection."}
}
req, err := newRequest(POST, getEndpoint(collections), opt, nil)
if err != nil {
return nil, nil, err
}
resp, err := cs.client.do(req)
if err != nil {
return nil, nil, err
}
var collection Collection
err = json.Unmarshal(*resp.body, &collection)
if err != nil {
return nil, nil, err
}
if resp.httpResponse.StatusCode != 201 {
return nil, nil, errors.New("failed to create the collection")
}
return &collection, resp, nil
}
//Update updates an existing collection on the authenticated user's profile.
func (cs *CollectionsService) Update(collectionID int, opt *CollectionOpt) (*Collection, *Response, error) {
if nil == opt {
return nil, nil, &IllegalArgumentError{ErrString: "Opt cannot be nil"}
}
if collectionID == 0 {
return nil, nil, &IllegalArgumentError{ErrString: "collectionID cannot be nil."}
}
endpoint := fmt.Sprintf("%v/%v", getEndpoint(collections), collectionID)
req, err := newRequest(PUT, endpoint, opt, nil)
if err != nil {
return nil, nil, err
}
resp, err := cs.client.do(req)
if err != nil {
return nil, nil, err
}
var collection Collection
err = json.Unmarshal(*resp.body, &collection)
if err != nil {
return nil, nil, err
}
return &collection, resp, nil
}
//Delete deletes a collection on the authenticated user's profile.
func (cs *CollectionsService) Delete(collectionID int) (*Response, error) {
if collectionID == 0 {
return nil, &IllegalArgumentError{ErrString: "CollectionID cannot be empty or zero."}
}
endpoint := fmt.Sprintf("%v/%v", getEndpoint(collections), collectionID)
req, err := newRequest(DELETE, endpoint, nil, nil)
if err != nil {
return nil, err
}
resp, err := cs.client.do(req)
if err != nil {
return nil, err
}
if resp.httpResponse.StatusCode != 204 {
return nil, errors.New("failed to delete the collection")
}
return resp, nil
}
type addPhoto struct {
Photo string `url:"photo_id"`
}
//AddPhoto adds a photo to a collection owned by an authenticated user.
func (cs *CollectionsService) AddPhoto(collectionID int, photoID string) (*Response, error) {
if collectionID == 0 {
return nil, &IllegalArgumentError{ErrString: "CollectionID cannot be empty or zero."}
}
if photoID == "" {
return nil, &IllegalArgumentError{ErrString: "PhotoID cannot be empty or zero."}
}
opt := &addPhoto{photoID}
endpoint := fmt.Sprintf("%v/%v/add", getEndpoint(collections), collectionID)
req, err := newRequest(POST, endpoint, opt, nil)
if err != nil {
return nil, err
}
resp, err := cs.client.do(req)
if err != nil {
return nil, err
}
if resp.httpResponse.StatusCode != 201 {
return nil, errors.New("failed to add photo to the collection")
}
return resp, nil
}
//RemovePhoto removes a photo from a collection owned by an authenticated user.
func (cs *CollectionsService) RemovePhoto(collectionID int, photoID string) (*Response, error) {
if collectionID == 0 {
return nil, &IllegalArgumentError{ErrString: "CollectionID cannot be empty or zero."}
}
if photoID == "" {
return nil, &IllegalArgumentError{ErrString: "PhotoID cannot be empty or zero."}
}
opt := &addPhoto{photoID}
endpoint := fmt.Sprintf("%v/%v/remove", getEndpoint(collections), collectionID)
req, err := newRequest(DELETE, endpoint, opt, nil)
if err != nil {
return nil, err
}
resp, err := cs.client.do(req)
if err != nil {
return nil, err
}
if resp.httpResponse.StatusCode != 200 {
return nil, errors.New("failed to remove photo from the collection")
}
return resp, nil
}