-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKiotaClientFactory.php
81 lines (73 loc) · 2.61 KB
/
KiotaClientFactory.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
<?php
/**
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* Licensed under the MIT License. See License in the project root
* for license information.
*/
namespace Microsoft\Kiota\Http;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware as GuzzleMiddleware;
use GuzzleHttp\Utils;
use Microsoft\Kiota\Http\Middleware\HeadersInspectionHandler;
use Microsoft\Kiota\Http\Middleware\KiotaMiddleware;
use Microsoft\Kiota\Http\Middleware\ParametersNameDecodingHandler;
use Microsoft\Kiota\Http\Middleware\RetryHandler;
use Microsoft\Kiota\Http\Middleware\UserAgentHandler;
/**
* Class KiotaClientFactory
*
* This class is used to build the \GuzzleHttp\Client instance used by the core service.
*
* @package Microsoft\Kiota\Http
* @copyright 2022 Microsoft Corporation
* @license https://opensource.org/licenses/MIT MIT License
* @link https://developer.microsoft.com/graph
*/
class KiotaClientFactory
{
/**
* Initialises the Guzzle client with default middleware
*
* @return Client
*/
public static function create(): Client
{
return self::createWithMiddleware(self::getDefaultHandlerStack());
}
/**
* Initialises the Guzzle client with provided middleware
*
* @param HandlerStack $handlerStack
* @return Client
*/
public static function createWithMiddleware(HandlerStack $handlerStack): Client
{
return new Client(['handler' => $handlerStack]);
}
/**
* Initialises the client with Guzzle request options (https://docs.guzzlephp.org/en/stable/request-options.html)
*
* @param array<string, mixed> $guzzleConfig
* @return Client
*/
public static function createWithConfig(array $guzzleConfig): Client
{
return new Client(array_merge(['handler' => self::getDefaultHandlerStack()], $guzzleConfig));
}
/**
* Returns default set of middleware to use for Guzzle clients
*
* @return HandlerStack
*/
public static function getDefaultHandlerStack(): HandlerStack
{
$handlerStack = new HandlerStack(Utils::chooseHandler());
$handlerStack->push(KiotaMiddleware::parameterNamesDecoding(), ParametersNameDecodingHandler::HANDLER_NAME);
$handlerStack->push(GuzzleMiddleware::redirect(), 'kiotaRedirectHandler');
$handlerStack->push(KiotaMiddleware::userAgent(), UserAgentHandler::HANDLER_NAME);
$handlerStack->push(KiotaMiddleware::retry(), RetryHandler::HANDLER_NAME);
$handlerStack->push(KiotaMiddleware::headersInspection(), HeadersInspectionHandler::HANDLER_NAME);
return $handlerStack;
}
}