-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultipart.js
171 lines (156 loc) · 3.96 KB
/
multipart.js
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
var request = require('../').extend({
Request: {
oauth: require('request-oauth'),
multipart: require('../'),
}
}).client
// ----------------------------------------------------------------------------
var fs = require('fs')
var path = require('path')
var file = {
cat: path.resolve(__dirname, '../test/fixtures/cat.png'),
beep: path.resolve(__dirname, '../test/fixtures/beep.mp3'),
}
// ----------------------------------------------------------------------------
;({
asana: async () => {
var token = '...' // no scopes
var task = '...'
await request({
method: 'POST',
url: `https://app.asana.com/api/1.0/tasks/${task}/attachments`,
headers: {
authorization: `Bearer ${token}`
},
multipart: {
file: fs.createReadStream(file.cat)
}
})
},
box: async () => {
var token = '...' // scope: root_readwrite
await request({
method: 'POST',
url: 'https://upload.box.com/api/2.0/files/content',
headers: {
authorization: `Bearer ${token}`
},
multipart: {
attributes: JSON.stringify({
name: 'cat.png',
parent: {id: 0},
}),
file: fs.createReadStream(file.cat)
}
})
},
drive: async () => {
var token = '...' // scope: drive.file
await request({
method: 'POST',
url: 'https://www.googleapis.com/upload/drive/v3/files',
headers: {
authorization: `Bearer ${token}`
},
qs: {
uploadType: 'multipart'
},
multipart: [
{
'Content-Type': 'application/json',
body: JSON.stringify({name: 'cat.png'})
},
{
'Content-Type': 'image/png',
body: fs.createReadStream(file.cat)
}
]
})
},
flickr: async () => {
await request({
method: 'POST',
url: 'https://up.flickr.com/services/upload/',
qs: {
title: `Sent on ${new Date()}`,
description: 'Hi',
is_public: 0
},
oauth: { // scope: write
consumer_key: '...',
consumer_secret: '...',
token: '...',
token_secret: '...',
},
multipart: {
title: `Sent on ${new Date()}`,
description: 'Hi',
is_public: 0,
photo: fs.createReadStream(file.cat)
}
})
},
mailgun: async () => {
var apikey = '...'
var subdomain = '...'
await request({
method: 'POST',
url: `https://api.mailgun.net/v3/${subdomain}/messages`,
auth: {user: 'api', pass: apikey},
multipart: {
from: '[email protected]',
subject: 'Purest is awesome! (mailgun+attachments)',
html: '<h1>Purest is awesome!</h1>',
text: 'True idd!',
attachment: [
fs.createReadStream(file.cat),
fs.createReadStream(file.beep)
]
}
})
},
slack: async () => {
var token = '...' // scope: files:write:user
await request({
method: 'POST',
url: 'https://slack.com/api/files.upload',
headers: {
authorization: `Bearer ${token}`
},
multipart: {
file: fs.createReadStream(file.cat)
}
})
},
trello: async () => {
var card = '...'
await request({
method: 'POST',
url: `https://api.trello.com/1/cards/${card}/attachments`,
qs: { // scope: read, write
key: '...',
token: '...'
},
multipart: {
file: fs.createReadStream(file.cat)
}
})
},
twitter: async () => {
await request({
method: 'POST',
url: 'https://api.twitter.com/1.1/statuses/update_with_media.json',
oauth: { // app with write access
consumer_key: '...',
consumer_secret: '...',
token: '...',
token_secret: '...',
},
multipart: {
status: `Sent on ${new Date()}`,
'media[]': fs.createReadStream(file.cat)
}
})
},
})[process.argv[2]]()