forked from microsoftgraph/msgraph-sdk-php
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraphRequest.php
More file actions
461 lines (430 loc) · 11.8 KB
/
Copy pathGraphRequest.php
File metadata and controls
461 lines (430 loc) · 11.8 KB
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
<?php
/**
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* Licensed under the MIT License. See License in the project root
* for license information.
*
* GraphRequest File
* PHP version 7
*
* @category Library
* @package Microsoft.Graph
* @copyright 2016 Microsoft Corporation
* @license https://opensource.org/licenses/MIT MIT License
* @version GIT: 0.1.0
* @link https://graph.microsoft.io/
*/
namespace Microsoft\Graph\Http;
use GuzzleHttp\Client;
use Microsoft\Graph\Core\GraphConstants;
use Microsoft\Graph\Exception\GraphException;
/**
* Class GraphRequest
*
* @category Library
* @package Microsoft.Graph
* @license https://opensource.org/licenses/MIT MIT License
* @link https://graph.microsoft.io/
*/
class GraphRequest
{
/**
* A valid access token
*
* @var string
*/
protected $accessToken;
/**
* The API version to use ("v1.0", "beta")
*
* @var string
*/
protected $apiVersion;
/**
* The base url to call
*
* @var string
*/
protected $baseUrl;
/**
* The endpoint to call
*
* @var string
*/
protected $endpoint;
/**
* An array of headers to send with the request
*
* @var array(string => string)
*/
protected $headers;
/**
* The body of the request (optional)
*
* @var string
*/
protected $requestBody;
/**
* The type of request to make ("GET", "POST", etc.)
*
* @var object
*/
protected $requestType;
/**
* True if the response should be returned as
* a stream
*
* @var bool
*/
protected $returnsStream;
/**
* The return type to cast the response as
*
* @var object
*/
protected $returnType;
/**
* The timeout, in seconds
*
* @var string
*/
protected $timeout;
/**
* The proxy port to use. Null to disable
*
* @var string
*/
protected $proxyPort;
/**
* Constructs a new Graph Request object
*
* @param string $requestType The HTTP method to use, e.g. "GET" or "POST"
* @param string $endpoint The Graph endpoint to call
* @param string $accessToken A valid access token to validate the Graph call
* @param string $baseUrl The base URL to call
* @param string $apiVersion The API version to use
* @param string $proxyPort The url where to proxy through
* @throws GraphException when no access token is provided
*/
public function __construct($requestType, $endpoint, $accessToken, $baseUrl, $apiVersion, $proxyPort = null)
{
$this->requestType = $requestType;
$this->endpoint = $endpoint;
$this->accessToken = $accessToken;
if (!$this->accessToken) {
throw new GraphException(GraphConstants::NO_ACCESS_TOKEN);
}
$this->baseUrl = $baseUrl;
$this->apiVersion = $apiVersion;
$this->timeout = 0;
$this->headers = $this->_getDefaultHeaders();
$this->proxyPort = $proxyPort;
}
/**
* Sets a new accessToken
*
* @param string $accessToken A valid access token to validate the Graph call
*
* @return GraphRequest object
*/
public function setAccessToken($accessToken)
{
$this->accessToken = $accessToken;
$this->headers['Authorization'] = 'Bearer ' . $this->accessToken;
return $this;
}
/**
* Sets the return type of the response object
*
* @param mixed $returnClass The object class to use
*
* @return GraphRequest object
*/
public function setReturnType($returnClass)
{
$this->returnType = $returnClass;
if ($this->returnType == "GuzzleHttp\Psr7\Stream") {
$this->returnsStream = true;
} else {
$this->returnsStream = false;
}
return $this;
}
/**
* Adds custom headers to the request
*
* @param array $headers An array of custom headers
*
* @return GraphRequest object
*/
public function addHeaders($headers)
{
$this->headers = array_merge($this->headers, $headers);
return $this;
}
/**
* Get the request headers
*
* @return array of headers
*/
public function getHeaders()
{
return $this->headers;
}
/**
* Attach a body to the request. Will JSON encode
* any Microsoft\Graph\Model objects as well as arrays
*
* @param mixed $obj The object to include in the request
*
* @return GraphRequest object
*/
public function attachBody($obj)
{
// Attach streams & JSON automatically
if (is_string($obj) || is_a($obj, 'GuzzleHttp\\Psr7\\Stream')) {
$this->requestBody = $obj;
}
// By default, JSON-encode
else {
$this->requestBody = json_encode($obj);
}
return $this;
}
/**
* Get the body of the request
*
* @return mixed request body of any type
*/
public function getBody()
{
return $this->requestBody;
}
/**
* Sets the timeout limit of the cURL request
*
* @param string $timeout The timeout in ms
*
* @return GraphRequest object
*/
public function setTimeout($timeout)
{
$this->timeout = $timeout;
return $this;
}
/**
* Executes the HTTP request using Guzzle
*
* @param mixed $client The client to use in the request
*
* @throws GraphException if response is invalid
*
* @return mixed object or array of objects
* of class $returnType
*/
public function execute($client = null)
{
if (is_null($client)) {
$client = $this->createGuzzleClient();
}
$result = $client->request(
$this->requestType,
$this->_getRequestUrl(),
[
'body' => $this->requestBody,
'stream' => $this->returnsStream,
'timeout' => $this->timeout
]
);
// Wrap response in GraphResponse layer
$response = new GraphResponse(
$this,
$result->getBody(),
$result->getStatusCode(),
$result->getHeaders()
);
// If no return type is specified, return GraphResponse
$returnObj = $response;
if ($this->returnType) {
$returnObj = $response->getResponseAsObject($this->returnType);
}
return $returnObj;
}
/**
* Executes the HTTP request asynchronously using Guzzle
*
* @param mixed $client The client to use in the request
*
* @return mixed object or array of objects
* of class $returnType
*/
public function executeAsync($client = null)
{
if (is_null($client)) {
$client = $this->createGuzzleClient();
}
$promise = $client->requestAsync(
$this->requestType,
$this->_getRequestUrl(),
[
'body' => $this->requestBody,
'stream' => $this->returnsStream,
'timeout' => $this->timeout
]
)->then(
// On success, return the result/response
function ($result) {
$response = new GraphResponse(
$this,
$result->getBody(),
$result->getStatusCode(),
$result->getHeaders()
);
$returnObject = $response;
if ($this->returnType) {
$returnObject = $response->getResponseAsObject(
$this->returnType
);
}
return $returnObject;
},
// On fail, log the error and return null
function ($reason) {
trigger_error("Async call failed: " . $reason->getMessage());
return null;
}
);
return $promise;
}
/**
* Download a file from OneDrive to a given location
*
* @param string $path The path to download the file to
* @param mixed $client The client to use in the request
*
* @throws GraphException if file path is invalid
*
* @return null
*/
public function download($path, $client = null)
{
if (is_null($client)) {
$client = $this->createGuzzleClient();
}
try {
$file = fopen($path, 'w');
if (!$file) {
throw new GraphException(GraphConstants::INVALID_FILE);
}
$client->request(
$this->requestType,
$this->_getRequestUrl(),
[
'body' => $this->requestBody,
'sink' => $file
]
);
if(is_resource($file)){
fclose($file);
}
} catch(GraphException $e) {
throw new GraphException(GraphConstants::INVALID_FILE);
}
return null;
}
/**
* Upload a file to OneDrive from a given location
*
* @param string $path The path of the file to upload
* @param mixed $client The client to use in the request
*
* @throws GraphException if file is invalid
*
* @return mixed DriveItem or array of DriveItems
*/
public function upload($path, $client = null)
{
if (is_null($client)) {
$client = $this->createGuzzleClient();
}
try {
if (file_exists($path) && is_readable($path)) {
$file = fopen($path, 'r');
$stream = \GuzzleHttp\Psr7\stream_for($file);
$this->requestBody = $stream;
return $this->execute($client);
} else {
throw new GraphException(GraphConstants::INVALID_FILE);
}
} catch(GraphException $e) {
throw new GraphException(GraphConstants::INVALID_FILE);
}
}
/**
* Get a list of headers for the request
*
* @return array The headers for the request
*/
private function _getDefaultHeaders()
{
$headers = [
'Host' => $this->baseUrl,
'Content-Type' => 'application/json',
'SdkVersion' => 'Graph-php-' . GraphConstants::SDK_VERSION,
'Authorization' => 'Bearer ' . $this->accessToken
];
return $headers;
}
/**
* Get the concatenated request URL
*
* @return string request URL
*/
private function _getRequestUrl()
{
//Send request with opaque URL
if (stripos($this->endpoint, "http") === 0) {
return $this->endpoint;
}
return $this->apiVersion . $this->endpoint;
}
/**
* Checks whether the endpoint currently contains query
* parameters and returns the relevant concatenator for
* the new query string
*
* @return string "?" or "&"
*/
protected function getConcatenator()
{
if (stripos($this->endpoint, "?") === false) {
return "?";
}
return "&";
}
/**
* Create a new Guzzle client
* To allow for user flexibility, the
* client is not reused. This allows the user
* to set and change headers on a per-request
* basis
*
* If a proxyPort was passed in the constructor, all
* requests will be forwared through this proxy.
*
* @return \GuzzleHttp\Client The new client
*/
protected function createGuzzleClient()
{
$clientSettings = [
'base_uri' => $this->baseUrl,
'headers' => $this->headers
];
if ($this->proxyPort !== null) {
$clientSettings['verify'] = false;
$clientSettings['proxy'] = $this->proxyPort;
}
$client = new Client($clientSettings);
return $client;
}
}