1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunnu\Dropbox; |
4
|
|
|
|
5
|
|
|
use Kunnu\Dropbox\Http\RequestBodyStream; |
6
|
|
|
use Kunnu\Dropbox\Http\RequestBodyJsonEncoded; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* DropboxRequest |
10
|
|
|
*/ |
11
|
|
|
class DropboxRequest |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Access Token to use for this request |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $accessToken = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The HTTP method for this request |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $method = "GET"; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The params for this request |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $params = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The Endpoint for this request |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $endpoint = null; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The Endpoint Type for this request |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $endpointType = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The headers to send with this request |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $headers = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* File to upload |
58
|
|
|
* |
59
|
|
|
* @var \Kunnu\Dropbox\DropboxFile |
60
|
|
|
*/ |
61
|
|
|
protected $file = null; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Content Type for the Request |
65
|
|
|
* |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
protected $contentType = 'application/json'; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* If the Response needs to be validated |
72
|
|
|
* against being a valid JSON response. |
73
|
|
|
* Set this to false when an endpoint or |
74
|
|
|
* request has no return values. |
75
|
|
|
* |
76
|
|
|
* @var boolean |
77
|
|
|
*/ |
78
|
|
|
protected $validateResponse = true; |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Create a new DropboxRequest instance |
83
|
|
|
* |
84
|
|
|
* @param string $method HTTP Method of the Request |
85
|
|
|
* @param string $endpoint API endpoint of the Request |
86
|
|
|
* @param string $accessToken Access Token for the Request |
87
|
|
|
* @param string $endpointType Endpoint type ['api'|'content'] |
88
|
|
|
* @param mixed $params Request Params |
89
|
|
|
* @param array $headers Headers to send along with the Request |
90
|
|
|
*/ |
91
|
2 |
|
public function __construct($method, $endpoint, $accessToken, $endpointType = "api", array $params = [], array $headers = [], $contentType = null) |
92
|
|
|
{ |
93
|
2 |
|
$this->setMethod($method); |
94
|
2 |
|
$this->setEndpoint($endpoint); |
95
|
2 |
|
$this->setAccessToken($accessToken); |
96
|
2 |
|
$this->setEndpointType($endpointType); |
97
|
2 |
|
$this->setParams($params); |
98
|
2 |
|
$this->setHeaders($headers); |
99
|
|
|
|
100
|
2 |
|
if ($contentType) { |
101
|
|
|
$this->setContentType($contentType); |
102
|
|
|
} |
103
|
2 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the Request Method |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
2 |
|
public function getMethod() |
111
|
|
|
{ |
112
|
2 |
|
return $this->method; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set the Request Method |
117
|
|
|
* |
118
|
|
|
* @param string |
119
|
|
|
* |
120
|
|
|
* @return \Kunnu\Dropbox\DropboxRequest |
121
|
|
|
*/ |
122
|
2 |
|
public function setMethod($method) |
123
|
|
|
{ |
124
|
2 |
|
$this->method = $method; |
125
|
|
|
|
126
|
2 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get Access Token for the Request |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
2 |
|
public function getAccessToken() |
135
|
|
|
{ |
136
|
2 |
|
return $this->accessToken; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Set Access Token for the Request |
141
|
|
|
* |
142
|
|
|
* @param string |
143
|
|
|
* |
144
|
|
|
* @return \Kunnu\Dropbox\DropboxRequest |
145
|
|
|
*/ |
146
|
2 |
|
public function setAccessToken($accessToken) |
147
|
|
|
{ |
148
|
2 |
|
$this->accessToken = $accessToken; |
149
|
|
|
|
150
|
2 |
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get the Endpoint of the Request |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
2 |
|
public function getEndpoint() |
159
|
|
|
{ |
160
|
2 |
|
return $this->endpoint; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Set the Endpoint of the Request |
165
|
|
|
* |
166
|
|
|
* @param string |
167
|
|
|
* |
168
|
|
|
* @return \Kunnu\Dropbox\DropboxRequest |
169
|
|
|
*/ |
170
|
2 |
|
public function setEndpoint($endpoint) |
171
|
|
|
{ |
172
|
2 |
|
$this->endpoint = $endpoint; |
173
|
|
|
|
174
|
2 |
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get the Endpoint Type of the Request |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
2 |
|
public function getEndpointType() |
183
|
|
|
{ |
184
|
2 |
|
return $this->endpointType; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Set the Endpoint Type of the Request |
189
|
|
|
* |
190
|
|
|
* @param string |
191
|
|
|
* |
192
|
|
|
* @return \Kunnu\Dropbox\DropboxRequest |
193
|
|
|
*/ |
194
|
2 |
|
public function setEndpointType($endpointType) |
195
|
|
|
{ |
196
|
2 |
|
$this->endpointType = $endpointType; |
197
|
|
|
|
198
|
2 |
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Get the Content Type of the Request |
203
|
|
|
* |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
2 |
|
public function getContentType() |
207
|
|
|
{ |
208
|
2 |
|
return $this->contentType; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Set the Content Type of the Request |
213
|
|
|
* |
214
|
|
|
* @param string |
215
|
|
|
* |
216
|
|
|
* @return \Kunnu\Dropbox\DropboxRequest |
217
|
|
|
*/ |
218
|
|
|
public function setContentType($contentType) |
219
|
|
|
{ |
220
|
|
|
$this->contentType = $contentType; |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Get Request Headers |
227
|
|
|
* |
228
|
|
|
* @return array |
229
|
|
|
*/ |
230
|
2 |
|
public function getHeaders() |
231
|
|
|
{ |
232
|
2 |
|
return $this->headers; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Set Request Headers |
237
|
|
|
* |
238
|
|
|
* @param array |
239
|
|
|
* |
240
|
|
|
* @return \Kunnu\Dropbox\DropboxRequest |
241
|
|
|
*/ |
242
|
2 |
|
public function setHeaders(array $headers) |
243
|
|
|
{ |
244
|
2 |
|
$this->headers = array_merge($this->headers, $headers); |
245
|
|
|
|
246
|
2 |
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Get JSON Encoded Request Body |
251
|
|
|
* |
252
|
|
|
* @return \Kunnu\Dropbox\Http\RequestBodyJsonEncoded |
253
|
|
|
*/ |
254
|
2 |
|
public function getJsonBody() |
255
|
|
|
{ |
256
|
2 |
|
return new RequestBodyJsonEncoded($this->getParams()); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Get the Request Params |
261
|
|
|
* |
262
|
|
|
* @return array |
263
|
|
|
*/ |
264
|
2 |
|
public function getParams() |
265
|
|
|
{ |
266
|
2 |
|
return $this->params; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Set the Request Params |
271
|
|
|
* |
272
|
|
|
* @param array |
273
|
|
|
* |
274
|
|
|
* @return \Kunnu\Dropbox\DropboxRequest |
275
|
|
|
*/ |
276
|
2 |
|
public function setParams(array $params = []) |
277
|
|
|
{ |
278
|
|
|
|
279
|
|
|
//Process Params |
280
|
2 |
|
$params = $this->processParams($params); |
281
|
|
|
|
282
|
|
|
//Set the params |
283
|
2 |
|
$this->params = $params; |
284
|
|
|
|
285
|
2 |
|
return $this; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Get Stream Request Body |
290
|
|
|
* |
291
|
|
|
* @return \Kunnu\Dropbox\Http\RequestBodyStream |
292
|
|
|
*/ |
293
|
|
|
public function getStreamBody() |
294
|
|
|
{ |
295
|
|
|
return new RequestBodyStream($this->getFile()); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Get the File to be sent with the Request |
300
|
|
|
* |
301
|
|
|
* @return \Kunnu\Dropbox\DropboxFile |
302
|
|
|
*/ |
303
|
|
|
public function getFile() |
304
|
|
|
{ |
305
|
|
|
return $this->file; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Set the File to be sent with the Request |
310
|
|
|
* |
311
|
|
|
* @param \Kunnu\Dropbox\DropboxFile |
312
|
|
|
* |
313
|
|
|
* @return \Kunnu\Dropbox\DropboxRequest |
314
|
|
|
*/ |
315
|
|
|
public function setFile(DropboxFile $file) |
316
|
|
|
{ |
317
|
|
|
$this->file = $file; |
318
|
|
|
|
319
|
|
|
return $this; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Returns true if Request has file to be uploaded |
324
|
|
|
* |
325
|
|
|
* @return boolean |
326
|
|
|
*/ |
327
|
|
|
public function hasFile() |
328
|
|
|
{ |
329
|
|
|
return !is_null($this->file) ? true : false; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Whether to validate response or not |
334
|
|
|
* |
335
|
|
|
* @return boolean |
336
|
|
|
*/ |
337
|
2 |
|
public function validateResponse() |
338
|
|
|
{ |
339
|
2 |
|
return $this->validateResponse; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Process Params for the File parameter |
344
|
|
|
* |
345
|
|
|
* @param array $params Request Params |
346
|
|
|
* |
347
|
|
|
* @return array |
348
|
|
|
*/ |
349
|
2 |
|
protected function processParams(array $params) |
350
|
|
|
{ |
351
|
|
|
//If a file needs to be uploaded |
352
|
2 |
|
if (isset($params['file']) && $params['file'] instanceof DropboxFile) { |
353
|
|
|
//Set the file property |
354
|
|
|
$this->setFile($params['file']); |
355
|
|
|
//Remove the file item from the params array |
356
|
|
|
unset($params['file']); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
//Whether the response needs to be validated |
360
|
|
|
//against being a valid JSON response |
361
|
2 |
|
if (isset($params['validateResponse'])) { |
362
|
|
|
//Set the validateResponse |
363
|
|
|
$this->validateResponse = $params['validateResponse']; |
364
|
|
|
//Remove the validateResponse from the params array |
365
|
|
|
unset($params['validateResponse']); |
366
|
|
|
} |
367
|
|
|
|
368
|
2 |
|
return $params; |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|