-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHypernodeClientFactory.php
More file actions
54 lines (46 loc) · 1.73 KB
/
HypernodeClientFactory.php
File metadata and controls
54 lines (46 loc) · 1.73 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
<?php
declare(strict_types=1);
namespace Hypernode\Api;
use Composer\InstalledVersions;
use Http\Client\Common\HttpMethodsClient;
use Http\Client\Common\Plugin\AddHostPlugin;
use Http\Client\Common\Plugin\AddPathPlugin;
use Http\Client\Common\Plugin\HeaderSetPlugin;
use Http\Client\Common\PluginClient;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Psr\Http\Client\ClientInterface;
class HypernodeClientFactory
{
public static function create(string $authToken, ?ClientInterface $httpClient = null): HypernodeClient
{
$httpHeaders = [
'Authorization' => sprintf('Token %s', $authToken),
'User-Agent' => sprintf(
'Hypernode API PHP Client v%s',
InstalledVersions::getVersion('hypernode/api-client'),
),
'Accept' => 'application/json',
'Content-Type' => 'application/json',
];
$httpClient = self::getHttpClient(Defaults::HYPERNODE_API_URL, $httpHeaders, $httpClient);
$apiClient = new HttpMethodsClient(
$httpClient, Psr17FactoryDiscovery::findRequestFactory(), Psr17FactoryDiscovery::findStreamFactory()
);
return new HypernodeClient($apiClient);
}
public static function getHttpClient(
string $apiUrl,
array $headers,
?ClientInterface $httpClient = null
): ClientInterface {
$httpClient = $httpClient ?? Psr18ClientDiscovery::find();
$uri = Psr17FactoryDiscovery::findUriFactory()->createUri($apiUrl);
$plugins = [
new AddHostPlugin($uri),
new AddPathPlugin($uri),
new HeaderSetPlugin($headers),
];
return new PluginClient($httpClient, $plugins);
}
}