/
DropboxRequest.php
340 lines (296 loc) 路 6.56 KB
/
DropboxRequest.php
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
<?php
namespace Kunnu\Dropbox;
use Kunnu\Dropbox\Http\RequestBodyStream;
use Kunnu\Dropbox\Http\RequestBodyJsonEncoded;
/**
* DropboxRequest
*/
class DropboxRequest
{
/**
* Access Token to use for this request
*
* @var string
*/
protected $accessToken = null;
/**
* The HTTP method for this request
*
* @var string
*/
protected $method = "GET";
/**
* The params for this request
*
* @var array
*/
protected $params = null;
/**
* The Endpoint for this request
*
* @var string
*/
protected $endpoint = null;
/**
* The Endpoint Type for this request
*
* @var string
*/
protected $endpointType = null;
/**
* The headers to send with this request
*
* @var array
*/
protected $headers = [];
/**
* File to upload
*
* @var \Kunnu\Dropbox\DropboxFile
*/
protected $file = null;
/**
* Content Type for the Request
*
* @var string
*/
protected $contentType = 'application/json';
/**
* Create a new DropboxRequest instance
*
* @param string $method HTTP Method of the Request
* @param string $endpoint API endpoint of the Request
* @param string $accessToken Access Token for the Request
* @param string $endpointType Endpoint type ['api'|'content']
* @param mixed $params Request Params
* @param array $headers Headers to send along with the Request
*/
public function __construct($method, $endpoint, $accessToken, $endpointType = "api", array $params = [], array $headers = [], $contentType = null)
{
$this->setMethod($method);
$this->setEndpoint($endpoint);
$this->setAccessToken($accessToken);
$this->setEndpointType($endpointType);
$this->setParams($params);
$this->setHeaders($headers);
if ($contentType) {
$this->setContentType($contentType);
}
}
/**
* Get the Request Method
*
* @return string
*/
public function getMethod()
{
return $this->method;
}
/**
* Get the Request Params
*
* @return array
*/
public function getParams()
{
return $this->params;
}
/**
* Get Access Token for the Request
*
* @return string
*/
public function getAccessToken()
{
return $this->accessToken;
}
/**
* Get the Endpoint of the Request
*
* @return string
*/
public function getEndpoint()
{
return $this->endpoint;
}
/**
* Get the Endpoint Type of the Request
*
* @return string
*/
public function getEndpointType()
{
return $this->endpointType;
}
/**
* Get the Content Type of the Request
*
* @return string
*/
public function getContentType()
{
return $this->contentType;
}
/**
* Get Request Headers
*
* @return array
*/
public function getHeaders()
{
return $this->headers;
}
/**
* Get the File to be sent with the Request
*
* @return \Kunnu\Dropbox\DropboxFile
*/
public function getFile()
{
return $this->file;
}
/**
* Set the Request Method
*
* @param string
*
* @return \Kunnu\Dropbox\DropboxRequest
*/
public function setMethod($method)
{
$this->method = $method;
return $this;
}
/**
* Set the Request Params
*
* @param array
*
* @return \Kunnu\Dropbox\DropboxRequest
*/
public function setParams(array $params = [])
{
//Process Params
$params = $this->processParams($params);
//Set the params
$this->params = $params;
return $this;
}
/**
* Set Access Token for the Request
*
* @param string
*
* @return \Kunnu\Dropbox\DropboxRequest
*/
public function setAccessToken($accessToken)
{
$this->accessToken = $accessToken;
return $this;
}
/**
* Set the Endpoint of the Request
*
* @param string
*
* @return \Kunnu\Dropbox\DropboxRequest
*/
public function setEndpoint($endpoint)
{
$this->endpoint = $endpoint;
return $this;
}
/**
* Set the Endpoint Type of the Request
*
* @param string
*
* @return \Kunnu\Dropbox\DropboxRequest
*/
public function setEndpointType($endpointType)
{
$this->endpointType = $endpointType;
return $this;
}
/**
* Set Request Headers
*
* @param array
*
* @return \Kunnu\Dropbox\DropboxRequest
*/
public function setHeaders(array $headers)
{
$this->headers = array_merge($this->headers, $headers);
return $this;
}
/**
* Set the Content Type of the Request
*
* @param string
*
* @return \Kunnu\Dropbox\DropboxRequest
*/
public function setContentType($contentType)
{
$this->contentType = $contentType;
return $this;
}
/**
* Set the File to be sent with the Request
*
* @param \Kunnu\Dropbox\DropboxFile
*
* @return \Kunnu\Dropbox\DropboxRequest
*/
public function setFile(DropboxFile $file)
{
$this->file = $file;
return $this;
}
/**
* Get JSON Encoded Request Body
*
* @return \Kunnu\Dropbox\Http\RequestBodyJsonEncoded
*/
public function getJsonBody()
{
return new RequestBodyJsonEncoded($this->getParams());
}
/**
* Get Stream Request Body
*
* @return \Kunnu\Dropbox\Http\RequestBodyStream
*/
public function getStreamBody()
{
return new RequestBodyStream($this->getFile());
}
/**
* Returns true if Request has file to be uploaded
*
* @return boolean
*/
public function hasFile()
{
return !is_null($this->file) ? true : false;
}
/**
* Process Params for the File parameter
*
* @param array $params Request Params
*
* @return array
*/
protected function processParams(array $params)
{
//If a file needs to be uploaded
if (isset($params['file']) && $params['file'] instanceof DropboxFile) {
//Set the file property
$this->setFile($params['file']);
//Remove the file item from the params array
unset($params['file']);
}
return $params;
}
}