forked from heavyengineer/go-vimeo
/
channels.go
203 lines (166 loc) · 5.61 KB
/
channels.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
package vimeo
import (
"fmt"
"strings"
"time"
)
// ChannelsService handles communication with the channels related
// methods of the Vimeo API.
//
// Vimeo API docs: https://developer.vimeo.com/api/endpoints/channels
type ChannelsService service
type dataListChannel struct {
Data []*Channel `json:"data"`
pagination
}
// Channel represents a channel.
type Channel struct {
URI string `json:"uri,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Link string `json:"link,omitempty"`
CreatedTime time.Time `json:"created_time,omitempty"`
ModifiedTime time.Time `json:"modified_time,omitempty"`
User *User `json:"user,omitempty"`
Pictures *Pictures `json:"pictures,omitempty"`
Header *Header `json:"header,omitempty"`
Privacy *Privacy `json:"privacy,omitempty"`
ResourceKey string `json:"resource_key,omitempty"`
}
// ChannelRequest represents a request to create/edit an channel.
type ChannelRequest struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Privacy string `json:"privacy,omitempty"`
}
// GetID returns the identifier (ID) of the channel.
func (c Channel) GetID() string {
l := strings.SplitN(c.URI, "/", -1)
id := l[len(l)-1]
return id
}
func listChannel(c *Client, url string, opt ...CallOption) ([]*Channel, *Response, error) {
u, err := addOptions(url, opt...)
if err != nil {
return nil, nil, err
}
req, err := c.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
channels := &dataListChannel{}
resp, err := c.Do(req, channels)
if err != nil {
return nil, resp, err
}
resp.setPaging(channels)
return channels.Data, resp, err
}
// List lists the channel for an category.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/channels
func (s *ChannelsService) List(opt ...CallOption) ([]*Channel, *Response, error) {
channels, resp, err := listChannel(s.client, "channels", opt...)
return channels, resp, err
}
// Create a new channel.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/channels
func (s *ChannelsService) Create(r *ChannelRequest) (*Channel, *Response, error) {
req, err := s.client.NewRequest("POST", "channels", r)
if err != nil {
return nil, nil, err
}
channel := &Channel{}
resp, err := s.client.Do(req, channel)
if err != nil {
return nil, resp, err
}
return channel, resp, nil
}
// Get specific channel by name.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/channels/%7Bchannel_id%7D
func (s *ChannelsService) Get(ch string, opt ...CallOption) (*Channel, *Response, error) {
u, err := addOptions(fmt.Sprintf("channels/%s", ch), opt...)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
channel := &Channel{}
resp, err := s.client.Do(req, channel)
if err != nil {
return nil, resp, err
}
return channel, resp, err
}
// Edit specific channel by name.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/channels/%7Bchannel_id%7D
func (s *ChannelsService) Edit(ch string, r *ChannelRequest) (*Channel, *Response, error) {
u := fmt.Sprintf("channels/%s", ch)
req, err := s.client.NewRequest("PATCH", u, r)
if err != nil {
return nil, nil, err
}
channel := &Channel{}
resp, err := s.client.Do(req, channel)
if err != nil {
return nil, resp, err
}
return channel, resp, nil
}
// Delete specific channel by name.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/channels/%7Bchannel_id%7D
func (s *ChannelsService) Delete(ch string) (*Response, error) {
u := fmt.Sprintf("channels/%s", ch)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
// ListUser lists the user for an channel.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/channels/%7Bchannel_id%7D/users
func (s *ChannelsService) ListUser(ch string, opt ...CallOption) ([]*User, *Response, error) {
u := fmt.Sprintf("channels/%s/users", ch)
users, resp, err := listUser(s.client, u, opt...)
return users, resp, err
}
// ListVideo lists the video for an channel.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/channels/%7Bchannel_id%7D/videos
func (s *ChannelsService) ListVideo(ch string, opt ...CallOption) ([]*Video, *Response, error) {
u := fmt.Sprintf("channels/%s/videos", ch)
videos, resp, err := listVideo(s.client, u, opt...)
return videos, resp, err
}
// GetVideo specific video by channel name and video ID.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/channels/%7Bchannel_id%7D/videos/%7Bvideo_id%7D
func (s *ChannelsService) GetVideo(ch string, vid int, opt ...CallOption) (*Video, *Response, error) {
u := fmt.Sprintf("channels/%s/videos/%d", ch, vid)
video, resp, err := getVideo(s.client, u, opt...)
return video, resp, err
}
// AddVideo add video to channel by ID.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/channels/%7Bchannel_id%7D/videos/%7Bvideo_id%7D
func (s *ChannelsService) AddVideo(ch string, vid int) (*Video, *Response, error) {
u := fmt.Sprintf("channels/%s/videos/%d", ch, vid)
video, resp, err := addVideo(s.client, u)
return video, resp, err
}
// DeleteVideo specific video by channel name and video ID.
//
// Vimeo API docs: https://developer.vimeo.com/api/playground/channels/%7Bchannel_id%7D/videos/%7Bvideo_id%7D
func (s *ChannelsService) DeleteVideo(ch string, vid int) (*Response, error) {
u := fmt.Sprintf("channels/%s/videos/%d", ch, vid)
resp, err := deleteVideo(s.client, u)
return resp, err
}