-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmethods.go
349 lines (315 loc) · 10.9 KB
/
methods.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package telegraph
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"strconv"
)
// CreateAccount creates a new account.
// Use this method to create a new Telegraph account. Most users only need one account, but this can be useful for channel administrators who would like to keep individual author names and profile links for each of their channels.
// On success, returns an Account object with the regular fields and an additional access_token field.
// - shortName (type string): Account name, helps users with several accounts remember which they are currently using. Displayed to the user above the "Edit/Publish" button on Telegra.ph, other users don't see this name.
// - opts (type CreateAccountOpts): All optional parameters.
// https://telegra.ph/api#createAccount
func (c *TelegraphClient) CreateAccount(shortName string, opts *CreateAccountOpts) (*Account, error) {
var (
u = url.Values{}
a Account
)
u.Add("short_name", shortName)
if opts != nil {
u.Add("author_name", opts.AuthorName)
u.Add("author_url", opts.AuthorUrl)
}
r, err := c.InvokeRequest("createAccount", u)
if err != nil {
return nil, err
}
return &a, json.Unmarshal(r, &a)
}
// EditAccountInfo edits certain information of an existing account.
// Use this method to update information about a Telegraph account. Pass only the parameters that you want to edit.
// On success, returns an Account object with the default fields.
// - accessToken (type string): Access token of the Telegraph account.
// - opts (type EditAccountInfoOpts): All optional parameters.
// https://telegra.ph/api#editAccountInfo
func (c *TelegraphClient) EditAccountInfo(accessToken string, opts *EditAccountInfoOpts) (*Account, error) {
var (
u = url.Values{}
a Account
)
u.Add("access_token", accessToken)
if opts != nil {
if opts.ShortName != "" {
u.Add("short_name", opts.ShortName)
}
if opts.AuthorName != "" {
u.Add("author_name", opts.AuthorName)
}
if opts.AuthorUrl != "" {
u.Add("author_url", opts.AuthorUrl)
}
}
r, err := c.InvokeRequest("editAccountInfo", u)
if err != nil {
return nil, err
}
return &a, json.Unmarshal(r, &a)
}
// GetAccountInfo returns account info of an existing account.
// Use this method to get information about a Telegraph account.
// Returns an Account object on success.
// - accessToken (type string): Access token of the Telegraph account.
// https://telegra.ph/api#getAccountInfo
func (c *TelegraphClient) GetAccountInfo(accessToken string) (*Account, error) {
var (
u = url.Values{}
a Account
)
u.Add("access_token", accessToken)
u.Add("fields", `["short_name", "author_name", "author_url", "auth_url", "page_count"]`)
r, err := c.InvokeRequest("getAccountInfo", u)
if err != nil {
return nil, err
}
return &a, json.Unmarshal(r, &a)
}
// RevokeAccessToken revokes an existing access-token.
// Use this method to revoke access_token and generate a new one, for example, if the user would like to reset all connected sessions, or you have reasons to believe the token was compromised.
// On success, returns an Account object with new access_token and auth_url fields.
// - accessToken (type string): Access token of the Telegraph account.
// https://telegra.ph/api#revokeAccessToken
func (c *TelegraphClient) RevokeAccessToken(accessToken string) (*Account, error) {
var (
u = url.Values{}
a Account
)
u.Add("access_token", accessToken)
r, err := c.InvokeRequest("revokeAccessToken", u)
if err != nil {
return nil, err
}
return &a, json.Unmarshal(r, &a)
}
// CreatePage creates a new telegraph page.
// Use this method to create a new Telegraph page.
// On success, returns a Page object.
// - accessToken (type string): Access token of the Telegraph account.
// - title (type string): Page title.
// - content (type string): Content of the page (Array of Node, up to 64 KB converted into a json string).
// - opts (type PageOpts): All optional parameters.
// https://telegra.ph/api#createPage
func (c *TelegraphClient) CreatePage(accessToken string, title string, content string, opts *PageOpts) (*Page, error) {
var (
u = url.Values{}
a Page
)
u.Add("access_token", accessToken)
u.Add("title", title)
cNode, err := ContentFormat(content)
if err != nil {
return nil, err
}
cNodeB, err := json.Marshal(cNode)
if err != nil {
return nil, err
}
u.Add("content", string(cNodeB))
if opts != nil {
u.Add("author_name", opts.AuthorName)
u.Add("author_url", opts.AuthorUrl)
u.Add("return_content", strconv.FormatBool(opts.ReturnContent))
}
r, err := c.InvokeRequest("createPage", u)
if err != nil {
return nil, err
}
return &a, json.Unmarshal(r, &a)
}
// EditPage edits an existing Telegraph page.
// Use this method to edit an existing Telegraph page.
// On success, returns a Page object.
// - accessToken (type string): Access token of the Telegraph account.
// - path (type string): Path to the page.
// - title (type string): Page title.
// - content (type string): Content of the page (Array of Node, up to 64 KB converted into a json string).
// - opts (type PageOpts): All optional parameters.
// https://telegra.ph/api#editPage
func (c *TelegraphClient) EditPage(accessToken, path, title, content string, opts *PageOpts) (*Page, error) {
var (
u = url.Values{}
a Page
)
u.Add("access_token", accessToken)
u.Add("path", path)
u.Add("title", title)
cNode, err := ContentFormat(content)
if err != nil {
return nil, err
}
cNodeB, err := json.Marshal(cNode)
if err != nil {
return nil, err
}
u.Add("content", string(cNodeB))
if opts != nil {
u.Add("author_name", opts.AuthorName)
u.Add("author_url", opts.AuthorUrl)
u.Add("return_content", strconv.FormatBool(opts.ReturnContent))
}
r, err := c.InvokeRequest("editPage", u)
if err != nil {
return nil, err
}
return &a, json.Unmarshal(r, &a)
}
// GetPage returns information of an existing Telegraph page.
// Use this method to get a Telegraph page.
// On success, returns a Page object.
// - path (type string): Path to the Telegraph page (in the format Title-12-31, i.e. everything that comes after http://telegra.ph/).
// - returnContent (type bool): If true, content field will be returned in Page object.
// https://telegra.ph/api#getPage
func (c *TelegraphClient) GetPage(path string, returnContent bool) (*Page, error) {
var (
u = url.Values{}
a Page
)
u.Add("path", path)
u.Add("return_content", strconv.FormatBool(returnContent))
r, err := c.InvokeRequest("getPage", u)
if err != nil {
return nil, err
}
return &a, json.Unmarshal(r, &a)
}
// GetPageList returns a list of pages.
// Use this method to get a list of pages belonging to a Telegraph account.
// Returns a PageList object, sorted by most recently created pages first.
// - accessToken (type string): Access token of the Telegraph account.
// - opts
// https://telegra.ph/api#getPageList
func (c *TelegraphClient) GetPageList(accessToken string, opts *PageListOpts) (*PageList, error) {
var (
u = url.Values{}
a PageList
)
u.Add("access_token", accessToken)
if opts != nil {
if opts.Offset != 0 {
u.Add("offset", strconv.FormatInt(opts.Offset, 10))
}
if opts.Limit != 0 {
u.Add("limit", strconv.FormatInt(opts.Limit, 10))
}
}
r, err := c.InvokeRequest("getPageList", u)
if err != nil {
return nil, err
}
return &a, json.Unmarshal(r, &a)
}
// GetViews returns views of an existing Telegraph article.
// Use this method to get the number of views for a Telegraph article.
// Returns a PageViews object on success. By default, the total number of page views will be returned.
// - path (type string): Path to the Telegraph page (in the format Title-12-31, i.e. everything that comes after http://telegra.ph/).
// - opts (type PageViewsOpts): All optional parameters.
// https://telegra.ph/api#getViews
func (c *TelegraphClient) GetViews(path string, opts *PageViewsOpts) (*PageViews, error) {
var (
u = url.Values{}
a PageViews
)
u.Add("path", path)
if opts != nil {
u.Add("year", strconv.FormatInt(opts.Year, 10))
u.Add("month", strconv.FormatInt(opts.Month, 10))
u.Add("day", strconv.FormatInt(opts.Day, 10))
u.Add("hour", strconv.FormatInt(opts.Hour, 10))
}
r, err := c.InvokeRequest("getViews", u)
if err != nil {
return nil, err
}
return &a, json.Unmarshal(r, &a)
}
// UploadFile uploads a file to Telegraph.
// Use this method to upload a file to Telegraph.
// (You can upload some specific file formats like .jpg, .jpeg, .png, .gif, etc only)
// Returns a path to the uploaded file i.e. everything that comes after https://telegra.ph/
// - filePath (type string): location of the file to upload to Telegraph.
// https://telegra.ph/upload
func (c *TelegraphClient) UploadFile(filePath string) (string, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
file, err := os.Open(filePath)
if err != nil {
return "", err
}
part, err := writer.CreateFormFile("file", filePath)
if err != nil {
return "", err
}
if _, err = io.Copy(part, file); err != nil {
return "", err
}
if err = writer.Close(); err != nil {
return "", err
}
return c.doUploadFile(writer.FormDataContentType(), body)
}
// UploadFileByBytes uploads a file to Telegraph by bytes.
// Use this method to upload a file to Telegraph.
// (You can upload some specific file formats like .jpg, .jpeg, .png, .gif, etc only)
// Returns a path to the uploaded file i.e. everything that comes after https://telegra.ph/
// - filePath (type string): location of the file to upload to Telegraph.
// https://telegra.ph/upload
func (c *TelegraphClient) UploadFileByBytes(content []byte) (string, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", "file_name")
if err != nil {
return "", err
}
if _, err = io.Copy(part, bytes.NewReader(content)); err != nil {
return "", err
}
if err = writer.Close(); err != nil {
return "", err
}
return c.doUploadFile(writer.FormDataContentType(), body)
}
// UploadFileByBytes uploads a file to Telegraph by bytes.
// Use this method to upload a file to Telegraph.
// (You can upload some specific file formats like .jpg, .jpeg, .png, .gif, etc only)
// Returns a path to the uploaded file i.e. everything that comes after https://telegra.ph/
// - filePath (type string): location of the file to upload to Telegraph.
// https://telegra.ph/upload
func (c *TelegraphClient) doUploadFile(contentType string, body io.Reader) (string, error) {
request, err := http.NewRequest(http.MethodPost, "https://telegra.ph/upload", body)
if err != nil {
return "", err
}
request.Header.Set("Content-Type", contentType)
httpResponse, err := c.HttpClient.Do(request)
if err != nil {
return "", err
}
b, err := io.ReadAll(httpResponse.Body)
if err != nil {
return "", err
}
var rUpload []Upload
if err := json.Unmarshal(b, &rUpload); err != nil {
m := map[string]string{}
if err := json.Unmarshal(b, &m); err != nil {
return "", err
}
return "", fmt.Errorf("failed to upload: %s", m["error"])
}
return rUpload[0].Path, nil
}