forked from Medium/medium-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
medium.go
492 lines (436 loc) · 13.9 KB
/
medium.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// Copyright 2015 A Medium Corporation
// Package medium provides a client for Medium's OAuth2 API.
package medium
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"os"
"path/filepath"
"strings"
"time"
)
// Available scope options when requesting access to a user's Medium account.
const (
ScopeBasicProfile Scope = "basicProfile"
ScopePublishPost = "publishPost"
ScopeUploadImage = "uploadImage"
ScopeListPublications = "listPublications"
)
// Content formats that are available when creating a post on Medium.
const (
ContentFormatHTML ContentFormat = "html"
ContentFormatMarkdown = "markdown"
)
// Publish statuses that are available when creating a post on Medium.
const (
PublishStatusDraft PublishStatus = "draft"
PublishStatusUnlisted = "unlisted"
PublishStatusPublic = "public"
)
// Licenses that are available when creating a post on Medium.
const (
LicenseAllRightsReserved License = "all-rights-reserved"
LicenseCC40By = "cc-40-by"
LicenseCC40BySA = "cc-40-by-sa"
LicenseCC40ByND = "cc-40-by-nd"
LicenseCC40ByNC = "cc-40-by-nc"
LicenseCC40ByNCND = "cc-40-by-nc-nd"
LicenseCC40ByNCSA = "cc-40-by-nc-sa"
LicenseCC40Zero = "cc-40-zero"
LicensePublicDomain = "public-domain"
)
const (
// host is the default host of Medium's API.
host = "https://api.medium.com"
// defaultTimeout is the default timeout duration used on HTTP requests.
defaultTimeout = 5 * time.Second
// defaultCode is the default error code for failures.
defaultCode = -1
)
// formats used for marshalling data for requests.
const (
formatJSON = "json"
formatForm = "form"
formatFile = "file"
)
// fileOpener defines the methods needed to support file uploads.
type fileOpener interface {
Open(name string) (io.ReadCloser, error)
}
// CreatePostOptions defines the options for creating a post on Medium.
type CreatePostOptions struct {
UserID string `json:"-"`
Title string `json:"title"`
Content string `json:"content"`
ContentFormat ContentFormat `json:"contentFormat"`
Tags []string `json:"tags,omitempty"`
CanonicalURL string `json:"canonicalUrl,omitempty"`
PublishStatus PublishStatus `json:"publishStatus,omitempty"`
License License `json:"license,omitempty"`
}
// UploadOptions defines the options for uploading files to Medium.
type UploadOptions struct {
FilePath string
ContentType string
fieldName string
}
// AccessToken defines credentials with which Medium's API may be accessed.
type AccessToken struct {
TokenType string `json:"token_type"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
Scope []string `json:"scope"`
ExpiresAt int64 `json:"expires_at"`
}
// User defines a Medium user
type User struct {
ID string `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
URL string `json:"url"`
ImageURL string `json:"imageUrl"`
}
// Publications inherit all Medium user publications
type Publications struct {
Data []Publication `json:"data"`
}
// Publication defines a Medium user publication
type Publication struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
URL string `json:"url"`
ImageURL string `json:"imageUrl"`
}
// Contributors inherit all Medium publication contributors
type Contributors struct {
Data []Contributor `json:"data"`
}
// Contributor defines a Medium publication contributor
type Contributor struct {
PublicationID string `json:"publicationID"`
UserID string `json:"userID"`
Role string `json:"role"`
}
// Post defines a Medium post
type Post struct {
ID string `json:"id"`
Title string `json:"title"`
AuthorID string `json:"authorId"`
Tags []string `json:"tags"`
URL string `json:"url"`
CanonicalURL string `json:"canonicalUrl"`
PublishState PublishStatus `json:"publishStatus"`
License License `json:"license"`
LicenseURL string `json:"licenseUrl"`
}
// Image defines a Medium image
type Image struct {
URL string `json:"url"`
MD5 string `json:"md5"`
}
// Error defines an error received when making a request to the API.
type Error struct {
Message string `json:"message"`
Code int `json:"code"`
}
// Error returns a string representing the error, satisfying the error interface.
func (e Error) Error() string {
return fmt.Sprintf("medium: %s (%d)", e.Message, e.Code)
}
// Medium defines the Medium client.
type Medium struct {
ApplicationID string
ApplicationSecret string
AccessToken string
Host string
Timeout time.Duration
Transport http.RoundTripper
fs fileOpener
}
// NewClient returns a new Medium API client which can be used to make RPC requests.
func NewClient(id, secret string) *Medium {
return &Medium{
ApplicationID: id,
ApplicationSecret: secret,
Host: host,
Timeout: defaultTimeout,
Transport: http.DefaultTransport,
fs: osFS{},
}
}
// NewClientWithAccessToken returns a new Medium API client which can be used to make RPC requests.
func NewClientWithAccessToken(accessToken string) *Medium {
return &Medium{
AccessToken: accessToken,
Host: host,
fs: osFS{},
}
}
// GetAuthorizationURL returns the URL to which an application may send
// a user in order to acquire authorization.
func (m *Medium) GetAuthorizationURL(state, redirectURL string, scopes ...Scope) string {
s := make([]string, len(scopes))
for i, scp := range scopes {
s[i] = string(scp)
}
v := url.Values{
"client_id": {m.ApplicationID},
"scope": {strings.Join(s, ",")},
"state": {state},
"response_type": {"code"},
"redirect_uri": {redirectURL},
}
return fmt.Sprintf("https://medium.com/m/oauth/authorize?%s", v.Encode())
}
// ExchangeAuthorizationCode exchanges the supplied code for a long-lived access token.
func (m *Medium) ExchangeAuthorizationCode(code, redirectURL string) (AccessToken, error) {
v := url.Values{
"code": {code},
"client_id": {m.ApplicationID},
"client_secret": {m.ApplicationSecret},
"grant_type": {"authorization_code"},
"redirect_uri": {redirectURL},
}
return m.acquireAccessToken(v)
}
// ExchangeRefreshToken exchanges the supplied refresh token for a new access token.
func (m *Medium) ExchangeRefreshToken(rt string) (AccessToken, error) {
v := url.Values{
"refresh_token": {rt},
"client_id": {m.ApplicationID},
"client_secret": {m.ApplicationSecret},
"grant_type": {"refresh_token"},
}
return m.acquireAccessToken(v)
}
// GetUser gets the profile identified by the current AccessToken.
// It will get the specified user or the current user if userID is empty.
// This requires m.AccessToken to have the BasicProfile scope.
func (m *Medium) GetUser(userID string) (*User, error) {
var r clientRequest
if userID == "" {
r = clientRequest{
method: "GET",
path: "/v1/me",
}
} else {
r = clientRequest{
method: "GET",
path: fmt.Sprintf("/v1/%s", userID),
}
}
u := &User{}
err := m.request(r, u)
return u, err
}
// GetUserPublications gets user publications by the current AccessToken.
// This requires m.AccessToken to have the BasicPublications scope.
func (m *Medium) GetUserPublications(userID string) (*Publications, error) {
r := clientRequest{
method: "GET",
path: fmt.Sprintf("/v1/users/%s/publications", userID),
}
p := &Publications{}
err := m.request(r, p)
return p, err
}
// GetPublicationContributors gets contributors for givaen a publication
// by the current AccessToken.
// This requires m.AccessToken to have the BasicPublications scope.
func (m *Medium) GetPublicationContributors(publicationID string) (*Contributors, error) {
r := clientRequest{
method: "GET",
path: fmt.Sprintf("/v1/publications/%s/contributors", publicationID),
}
p := &Contributors{}
err := m.request(r, p)
return p, err
}
// CreatePost creates a post on the profile identified by the current AccessToken.
// This requires m.AccessToken to have the PublishPost scope.
func (m *Medium) CreatePost(o CreatePostOptions) (*Post, error) {
r := clientRequest{
method: "POST",
path: fmt.Sprintf("/v1/users/%s/posts", o.UserID),
data: o,
}
p := &Post{}
err := m.request(r, p)
return p, err
}
// UploadImage uploads an image to Medium.
// This requires m.AccessToken to have the UploadImage scope.
func (m *Medium) UploadImage(o UploadOptions) (*Image, error) {
o.fieldName = "image"
r := clientRequest{
method: "POST",
path: fmt.Sprintf("/v1/images"),
format: formatFile,
data: o,
}
i := &Image{}
err := m.request(r, i)
return i, err
}
// generateJSONRequestData returns the body and content type for a JSON request.
func (m *Medium) generateJSONRequestData(cr clientRequest) ([]byte, string, error) {
body, err := json.Marshal(cr.data)
if err != nil {
return nil, "", Error{fmt.Sprintf("Could not marshal JSON: %s", err), defaultCode}
}
return body, "application/json", nil
}
// generateFormRequestData returns the body and content type for a form data request.
func (m *Medium) generateFormRequestData(cr clientRequest) ([]byte, string, error) {
var body []byte
switch d := cr.data.(type) {
case string:
body = []byte(d)
case []byte:
body = d
default:
return nil, "", Error{"Invalid data passed for form request", defaultCode}
}
return body, "application/x-www-form-urlencoded", nil
}
// generateFileRequestData returns the body and content type for a file upload request.
func (m *Medium) generateFileRequestData(cr clientRequest) ([]byte, string, error) {
uo, ok := cr.data.(UploadOptions)
if !ok {
return nil, "", Error{"Invalid data passed for file upload", defaultCode}
}
file, err := m.fs.Open(uo.FilePath)
if err != nil {
return nil, "", Error{fmt.Sprintf("Could not open file: %s", err), defaultCode}
}
defer file.Close()
// Create a form part
b := bytes.Buffer{}
w := multipart.NewWriter(&b)
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
escapeQuotes(uo.fieldName), escapeQuotes(filepath.Base(uo.FilePath))))
h.Set("Content-Type", uo.ContentType)
part, err := w.CreatePart(h)
if err != nil {
return nil, "", Error{fmt.Sprintf("Could not create form part: %s", err), defaultCode}
}
_, err = io.Copy(part, file)
if err != nil {
return nil, "", Error{fmt.Sprintf("Could not copy data: %s", err), defaultCode}
}
w.Close()
return b.Bytes(), w.FormDataContentType(), nil
}
// request makes a request to Medium's API
func (m *Medium) request(cr clientRequest, result interface{}) error {
f := cr.format
if f == "" {
f = formatJSON
}
// Get the body and content type.
var g requestDataGenerator
switch f {
case formatJSON:
g = m.generateJSONRequestData
case formatForm:
g = m.generateFormRequestData
case formatFile:
g = m.generateFileRequestData
default:
return Error{fmt.Sprintf("Unknown format: %s", cr.format), defaultCode}
}
body, ct, err := g(cr)
if err != nil {
return err
}
// Construct the request
req, err := http.NewRequest(cr.method, m.Host+cr.path, bytes.NewReader(body))
if err != nil {
return Error{fmt.Sprintf("Could not create request: %s", err), defaultCode}
}
req.Header.Add("Content-Type", ct)
req.Header.Add("Accept", "application/json")
req.Header.Add("Accept-Charset", "utf-8")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", m.AccessToken))
// Create the HTTP client
client := &http.Client{
Transport: m.Transport,
Timeout: m.Timeout,
}
// Make the request
res, err := client.Do(req)
if err != nil {
return Error{fmt.Sprintf("Failed to make request: %s", err), defaultCode}
}
defer res.Body.Close()
// Parse the response
c, err := ioutil.ReadAll(res.Body)
if err != nil {
return Error{fmt.Sprintf("Could not read response: %s", err), defaultCode}
}
var env envelope
if err := json.Unmarshal(c, &env); err != nil {
return Error{fmt.Sprintf("Could not parse response: %s", err), defaultCode}
}
if http.StatusOK <= res.StatusCode && res.StatusCode < http.StatusMultipleChoices {
if env.Data != nil {
c, _ = json.Marshal(env.Data)
}
return json.Unmarshal(c, &result)
}
e := env.Errors[0]
return Error{e.Message, e.Code}
}
// acquireAccessToken makes a request to Medium for an access token.
func (m *Medium) acquireAccessToken(v url.Values) (AccessToken, error) {
cr := clientRequest{
method: "POST",
path: "/v1/tokens",
format: formatForm,
data: v.Encode(),
}
at := AccessToken{}
err := m.request(cr, &at)
// Set the access token on the service.
if err == nil {
m.AccessToken = at.AccessToken
}
return at, err
}
type ContentFormat string
type PublishStatus string
type License string
type Scope string
// clientRequest defines information that can be used to make a request to Medium.
type clientRequest struct {
method string
path string
data interface{}
format string
}
// payload defines a struct to represent payloads that are returned from Medium.
type envelope struct {
Data interface{} `json:"data"`
Errors []Error `json:"errors,omitempty"`
}
// osFS is an implementation of fileOpener that uses the disk.
type osFS struct{}
// Open opens a file from disk.
func (osFS) Open(name string) (io.ReadCloser, error) { return os.Open(name) }
// requestDataGenerator defines a function that can generate request data.
type requestDataGenerator func(cr clientRequest) ([]byte, string, error)
// Borrowed from multipart/writer.go
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
// escapeQuotes returns the supplied string with quotes escaped.
func escapeQuotes(s string) string {
return quoteEscaper.Replace(s)
}