-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalbum.go
152 lines (138 loc) · 4.27 KB
/
album.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
package smugmug
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)
// AlbumService is the API for album endpoints
type AlbumService service
// AlbumIterFunc is called for each album in the results
type AlbumIterFunc func(*Album) (bool, error)
func (s *AlbumService) album(req *http.Request) (*Album, error) {
res := &albumResponse{}
err := s.client.do(req, res)
if err != nil {
return nil, err
}
return s.expand(res.Response.Album, res.Expansions)
}
func (s *AlbumService) expand(album *Album, expansions map[string]*json.RawMessage) (*Album, error) {
if album.URIs.User != nil {
if val, ok := expansions[album.URIs.User.URI]; ok {
res := struct {
User *User `json:"User"`
}{}
if err := json.Unmarshal(*val, &res); err != nil {
return nil, err
}
album.User = res.User
}
}
if album.URIs.HighlightImage != nil {
if val, ok := expansions[album.URIs.HighlightImage.URI]; ok {
res := struct {
Image *Image `json:"Image"`
}{}
if err := json.Unmarshal(*val, &res); err != nil {
return nil, err
}
album.HighlightImage = res.Image
}
}
if album.URIs.Node != nil {
if val, ok := expansions[album.URIs.Node.URI]; ok {
res := struct {
Node *Node `json:"Node"`
}{}
if err := json.Unmarshal(*val, &res); err != nil {
return nil, err
}
album.Node = res.Node
}
}
return album, nil
}
// Album returns the album with key `albumKey`
func (s *AlbumService) Album(ctx context.Context, albumKey string, options ...APIOption) (*Album, error) {
uri := fmt.Sprintf("album/%s", albumKey)
req, err := s.client.newRequest(ctx, uri, options)
if err != nil {
return nil, err
}
return s.album(req)
}
func (s *AlbumService) albums(req *http.Request) ([]*Album, *Pages, error) {
res := &albumsResponse{}
err := s.client.do(req, res)
if err != nil {
return nil, nil, err
}
for i := range res.Response.Album {
if _, err = s.expand(res.Response.Album[i], res.Expansions); err != nil {
return nil, nil, err
}
}
return res.Response.Album, res.Response.Pages, nil
}
// Albums returns a single page of albums for the user
func (s *AlbumService) Albums(ctx context.Context, userID string, options ...APIOption) ([]*Album, *Pages, error) {
uri := fmt.Sprintf("user/%s!albums", userID)
req, err := s.client.newRequest(ctx, uri, options)
if err != nil {
return nil, nil, err
}
return s.albums(req)
}
// AlbumsIter iterates all albums for the user
func (s *AlbumService) AlbumsIter(ctx context.Context, userID string, iter AlbumIterFunc, options ...APIOption) error {
return iterate(ctx, func(ctx context.Context, options ...APIOption) ([]*Album, *Pages, error) {
return s.Albums(ctx, userID, options...)
}, iter, options...)
}
// Search returns a single page of search results
func (s *AlbumService) Search(ctx context.Context, options ...APIOption) ([]*Album, *Pages, error) {
uri := "album!search"
req, err := s.client.newRequest(ctx, uri, options)
if err != nil {
return nil, nil, err
}
return s.albums(req)
}
// SearchIter iterates all search results
// The results of this query might be very large depending on the scope and query
func (s *AlbumService) SearchIter(ctx context.Context, iter AlbumIterFunc, options ...APIOption) error {
return iterate(ctx, s.Search, iter, options...)
}
// Patch updates the metadata for `albumKey`
func (s *AlbumService) Patch(
ctx context.Context, albumKey string, data map[string]interface{}, options ...APIOption) (*Album, error) {
uri := fmt.Sprintf("album/%s", albumKey)
body, err := json.Marshal(data)
if err != nil {
return nil, err
}
req, err := s.client.newRequestWithBody(ctx, http.MethodPatch, uri, bytes.NewReader(body), options)
if err != nil {
return nil, err
}
return s.album(req)
}
type albumsResponse struct {
Response struct {
Album []*Album `json:"Album"`
Pages *Pages `json:"Pages"`
} `json:"Response"`
Expansions map[string]*json.RawMessage `json:"Expansions,omitempty"`
Code int `json:"Code"`
Message string `json:"Message"`
}
type albumResponse struct {
Response struct {
Album *Album `json:"Album"`
} `json:"Response"`
Expansions map[string]*json.RawMessage `json:"Expansions,omitempty"`
Code int `json:"Code"`
Message string `json:"Message"`
}