PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / trunk
Booking for Appointments and Events Calendar – Amelia vtrunk
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / vendor / microsoft / microsoft-graph / src / Http / GraphRequest.php
ameliabooking / vendor / microsoft / microsoft-graph / src / Http Last commit date
GraphCollectionRequest.php 6 months ago GraphRequest.php 6 months ago GraphResponse.php 6 months ago
GraphRequest.php
556 lines
1 <?php
2 /**
3 * Copyright (c) Microsoft Corporation. All Rights Reserved.
4 * Licensed under the MIT License. See License in the project root
5 * for license information.
6 *
7 * GraphRequest File
8 * PHP version 7
9 *
10 * @category Library
11 * @package Microsoft.Graph
12 * @copyright 2016 Microsoft Corporation
13 * @license https://opensource.org/licenses/MIT MIT License
14 * @version GIT: 0.1.0
15 * @link https://graph.microsoft.io/
16 */
17
18 namespace Microsoft\Graph\Http;
19
20 use AmeliaVendor\GuzzleHttp\Client;
21 use AmeliaVendor\GuzzleHttp\Exception\BadResponseException;
22 use Microsoft\Graph\Core\GraphConstants;
23 use Microsoft\Graph\Core\ExceptionWrapper;
24 use Microsoft\Graph\Exception\GraphException;
25
26 /**
27 * Class GraphRequest
28 *
29 * @category Library
30 * @package Microsoft.Graph
31 * @license https://opensource.org/licenses/MIT MIT License
32 * @link https://graph.microsoft.io/
33 */
34 class GraphRequest
35 {
36 /**
37 * A valid access token
38 *
39 * @var string
40 */
41 protected $accessToken;
42 /**
43 * The API version to use ("v1.0", "beta")
44 *
45 * @var string
46 */
47 protected $apiVersion;
48 /**
49 * The base url to call
50 *
51 * @var string
52 */
53 protected $baseUrl;
54 /**
55 * The endpoint to call
56 *
57 * @var string
58 */
59 protected $endpoint;
60 /**
61 * An array of headers to send with the request
62 *
63 * @var array(string => string)
64 */
65 protected $headers;
66 /**
67 * The body of the request (optional)
68 *
69 * @var string
70 */
71 protected $requestBody;
72 /**
73 * The type of request to make ("GET", "POST", etc.)
74 *
75 * @var object
76 */
77 protected $requestType;
78 /**
79 * True if the response should be returned as
80 * a stream
81 *
82 * @var bool
83 */
84 protected $returnsStream;
85 /**
86 * The return type to cast the response as
87 *
88 * @var object
89 */
90 protected $returnType;
91 /**
92 * The timeout, in seconds
93 *
94 * @var string
95 */
96 protected $timeout;
97 /**
98 * The proxy port to use. Null to disable
99 *
100 * @var string
101 */
102 protected $proxyPort;
103 /**
104 * Whether SSL verification should be used for proxy requests
105 *
106 * @var bool
107 */
108 protected $proxyVerifySSL;
109 /**
110 * Request options to decide if Guzzle Client should throw exceptions when http code is 4xx or 5xx
111 *
112 * @var bool
113 */
114 protected $http_errors;
115
116 /**
117 * Constructs a new Graph Request object
118 *
119 * @param string $requestType The HTTP method to use, e.g. "GET" or "POST"
120 * @param string $endpoint The Graph endpoint to call
121 * @param string $accessToken A valid access token to validate the Graph call
122 * @param string $baseUrl The base URL to call
123 * @param string $apiVersion The API version to use
124 * @param string $proxyPort The url where to proxy through
125 * @param bool $proxyVerifySSL Whether the proxy requests should perform SSL verification
126 * @throws GraphException when no access token is provided
127 */
128 public function __construct($requestType, $endpoint, $accessToken, $baseUrl, $apiVersion, $proxyPort = null, $proxyVerifySSL = false)
129 {
130 $this->requestType = $requestType;
131 $this->endpoint = $endpoint;
132 $this->accessToken = $accessToken;
133 $this->http_errors = true;
134
135 if (!$this->accessToken) {
136 throw new GraphException(GraphConstants::NO_ACCESS_TOKEN);
137 }
138
139 $this->baseUrl = $baseUrl;
140 $this->apiVersion = $apiVersion;
141 $this->timeout = 100;
142 $this->headers = $this->_getDefaultHeaders();
143 $this->proxyPort = $proxyPort;
144 $this->proxyVerifySSL = $proxyVerifySSL;
145 }
146
147 /**
148 * Gets the Base URL the request is made to
149 *
150 * @return string
151 */
152 public function getBaseUrl()
153 {
154 return $this->baseUrl;
155 }
156
157 /**
158 * Gets the API version in use for the request
159 *
160 * @return string
161 */
162 public function getApiVersion()
163 {
164 return $this->apiVersion;
165 }
166
167 /**
168 * Gets whether request returns a stream or not
169 *
170 * @return boolean
171 */
172 public function getReturnsStream()
173 {
174 return $this->returnsStream;
175 }
176
177 /**
178 * Sets a http errors option
179 *
180 * @param string $http_errors A bool option to the Graph call
181 *
182 * @return $this object
183 */
184 public function setHttpErrors($http_errors)
185 {
186 $this->http_errors = $http_errors;
187 return $this;
188 }
189
190 /**
191 * Sets a new accessToken
192 *
193 * @param string $accessToken A valid access token to validate the Graph call
194 *
195 * @return $this object
196 */
197 public function setAccessToken($accessToken)
198 {
199 $this->accessToken = $accessToken;
200 $this->headers['Authorization'] = 'Bearer ' . $this->accessToken;
201 return $this;
202 }
203
204 /**
205 * Sets the return type of the response object
206 *
207 * @param mixed $returnClass The object class to use
208 *
209 * @return $this object
210 */
211 public function setReturnType($returnClass)
212 {
213 $this->returnType = $returnClass;
214 if ($this->returnType == "AmeliaVendor\GuzzleHttp\Psr7\Stream" || $this->returnType === \AmeliaVendor\Psr\Http\Message\StreamInterface::class) {
215 $this->returnsStream = true;
216 } else {
217 $this->returnsStream = false;
218 }
219 return $this;
220 }
221
222 /**
223 * Adds custom headers to the request
224 *
225 * @param array $headers An array of custom headers
226 *
227 * @return $this object
228 */
229 public function addHeaders($headers)
230 {
231 $this->headers = array_merge($this->headers, $headers);
232 return $this;
233 }
234
235 /**
236 * Get the request headers
237 *
238 * @return array of headers
239 */
240 public function getHeaders()
241 {
242 return $this->headers;
243 }
244
245 /**
246 * Attach a body to the request. Will JSON encode
247 * any Microsoft\Graph\Model objects as well as arrays
248 *
249 * @param mixed $obj The object to include in the request
250 *
251 * @return $this object
252 */
253 public function attachBody($obj)
254 {
255 // Attach streams & JSON automatically
256 if (is_string($obj) || is_a($obj, \AmeliaVendor\Psr\Http\Message\StreamInterface::class)) {
257 $this->requestBody = $obj;
258 }
259 // By default, JSON-encode
260 else {
261 $this->requestBody = json_encode($obj);
262 }
263 return $this;
264 }
265
266 /**
267 * Get the body of the request
268 *
269 * @return mixed request body of any type
270 */
271 public function getBody()
272 {
273 return $this->requestBody;
274 }
275
276 /**
277 * Sets the timeout limit of the cURL request
278 *
279 * @param string $timeout The timeout in seconds
280 *
281 * @return $this object
282 */
283 public function setTimeout($timeout)
284 {
285 $this->timeout = $timeout;
286 return $this;
287 }
288
289 /**
290 * Gets the timeout value of the request
291 *
292 * @return string
293 */
294 public function getTimeout()
295 {
296 return $this->timeout;
297 }
298
299 /**
300 * Executes the HTTP request using Guzzle
301 *
302 * @param mixed $client The client to use in the request
303 *
304 * @throws \AmeliaVendor\GuzzleHttp\Exception\GuzzleException
305 *
306 * @return mixed object or array of objects
307 * of class $returnType
308 */
309 public function execute($client = null)
310 {
311 if (is_null($client)) {
312 $client = $this->createGuzzleClient();
313 }
314
315 try {
316 $result = $client->request(
317 $this->requestType,
318 $this->_getRequestUrl(),
319 [
320 'body' => $this->requestBody,
321 'timeout' => $this->timeout
322 ]
323 );
324 } catch(BadResponseException $e) {
325 throw ExceptionWrapper::wrapGuzzleBadResponseException($e);
326 }
327
328 // Check to see if returnType is a stream, if so return it immediately
329 if($this->returnsStream) {
330 return $result->getBody();
331 }
332
333 // Wrap response in GraphResponse layer
334 $response = new GraphResponse(
335 $this,
336 $result->getBody(),
337 $result->getStatusCode(),
338 $result->getHeaders()
339 );
340
341 // If no return type is specified, return GraphResponse
342 $returnObj = $response;
343
344 if ($this->returnType) {
345 $returnObj = $response->getResponseAsObject($this->returnType);
346 }
347 return $returnObj;
348 }
349
350 /**
351 * Executes the HTTP request asynchronously using Guzzle
352 *
353 * @param mixed $client The client to use in the request
354 *
355 * @return mixed object or array of objects
356 * of class $returnType
357 */
358 public function executeAsync($client = null)
359 {
360 if (is_null($client)) {
361 $client = $this->createGuzzleClient();
362 }
363
364 $promise = $client->requestAsync(
365 $this->requestType,
366 $this->_getRequestUrl(),
367 [
368 'body' => $this->requestBody,
369 'timeout' => $this->timeout
370 ]
371 )->then(
372 // On success, return the result/response
373 function ($result) {
374
375 // Check to see if returnType is a stream, if so return it immediately
376 if($this->returnsStream) {
377 return $result->getBody();
378 }
379
380 $response = new GraphResponse(
381 $this,
382 $result->getBody(),
383 $result->getStatusCode(),
384 $result->getHeaders()
385 );
386 $returnObject = $response;
387 if ($this->returnType) {
388 $returnObject = $response->getResponseAsObject(
389 $this->returnType
390 );
391 }
392 return $returnObject;
393 },
394 // On fail, log the error and return null
395 function ($reason) {
396 if ($reason instanceof BadResponseException) {
397 $reason = ExceptionWrapper::wrapGuzzleBadResponseException($reason);
398 }
399 trigger_error("Async call failed: " . $reason->getMessage());
400 return null;
401 }
402 );
403 return $promise;
404 }
405
406 /**
407 * Download a file from OneDrive to a given location
408 *
409 * @param string $path The path to download the file to
410 * @param mixed $client The client to use in the request
411 *
412 * @throws GraphException if file path is invalid
413 * @throws \AmeliaVendor\GuzzleHttp\Exception\GuzzleException
414 *
415 * @return null
416 */
417 public function download($path, $client = null)
418 {
419 if (is_null($client)) {
420 $client = $this->createGuzzleClient();
421 }
422 try {
423 $file = fopen($path, 'w');
424 if (!$file) {
425 throw new GraphException(GraphConstants::INVALID_FILE);
426 }
427
428 $client->request(
429 $this->requestType,
430 $this->_getRequestUrl(),
431 [
432 'body' => $this->requestBody,
433 'sink' => $file,
434 'timeout' => $this->timeout
435 ]
436 );
437 if(is_resource($file)){
438 fclose($file);
439 }
440
441 } catch(GraphException $e) {
442 throw new GraphException(GraphConstants::INVALID_FILE);
443 } catch(BadResponseException $e) {
444 throw ExceptionWrapper::wrapGuzzleBadResponseException($e);
445 }
446
447 return null;
448 }
449
450 /**
451 * Upload a file to OneDrive from a given location
452 *
453 * @param string $path The path of the file to upload
454 * @param mixed $client The client to use in the request
455 *
456 * @throws GraphException if file is invalid
457 * @throws \AmeliaVendor\GuzzleHttp\Exception\GuzzleException
458 *
459 * @return mixed DriveItem or array of DriveItems
460 */
461 public function upload($path, $client = null)
462 {
463 if (is_null($client)) {
464 $client = $this->createGuzzleClient();
465 }
466 try {
467 if (file_exists($path) && is_readable($path)) {
468 $file = fopen($path, 'r');
469 $stream = \AmeliaVendor\GuzzleHttp\Psr7\Utils::streamFor($file);
470 $this->requestBody = $stream;
471 return $this->execute($client);
472 } else {
473 throw new GraphException(GraphConstants::INVALID_FILE);
474 }
475 } catch(GraphException $e) {
476 throw new GraphException(GraphConstants::INVALID_FILE);
477 }
478 }
479
480 /**
481 * Get a list of headers for the request
482 *
483 * @return array The headers for the request
484 */
485 private function _getDefaultHeaders()
486 {
487 return [
488 'Host' => $this->baseUrl,
489 'Content-Type' => 'application/json',
490 'SdkVersion' => 'Graph-php-' . GraphConstants::SDK_VERSION,
491 'Authorization' => 'Bearer ' . $this->accessToken
492 ];
493 }
494
495 /**
496 * Get the concatenated request URL
497 *
498 * @return string request URL
499 */
500 private function _getRequestUrl()
501 {
502 //Send request with opaque URL
503 if (stripos($this->endpoint, "http") === 0) {
504 return $this->endpoint;
505 }
506
507 return $this->apiVersion . $this->endpoint;
508 }
509
510 /**
511 * Checks whether the endpoint currently contains query
512 * parameters and returns the relevant concatenator for
513 * the new query string
514 *
515 * @return string "?" or "&"
516 */
517 protected function getConcatenator()
518 {
519 if (stripos($this->endpoint, "?") === false) {
520 return "?";
521 }
522 return "&";
523 }
524
525 /**
526 * Create a new Guzzle client
527 * To allow for user flexibility, the
528 * client is not reused. This allows the user
529 * to set and change headers on a per-request
530 * basis
531 *
532 * If a proxyPort was passed in the constructor, all
533 * requests will be forwared through this proxy.
534 *
535 * @return \AmeliaVendor\GuzzleHttp\Client The new client
536 */
537 protected function createGuzzleClient()
538 {
539 $clientSettings = [
540 'base_uri' => $this->baseUrl,
541 'http_errors' => $this->http_errors,
542 'headers' => $this->headers
543 ];
544 if ($this->proxyPort !== null) {
545 $clientSettings['verify'] = $this->proxyVerifySSL;
546 $clientSettings['proxy'] = $this->proxyPort;
547 }
548 if (extension_loaded('curl') && defined('CURL_VERSION_HTTP2') && (curl_version()["features"] & CURL_VERSION_HTTP2 !== 0)) {
549
550 // Enable HTTP/2 if curl lib exists and supports it
551 $clientSettings['version'] = '2';
552 }
553 return new Client($clientSettings);
554 }
555 }
556