-
Notifications
You must be signed in to change notification settings - Fork 4
/
Factory.php
70 lines (59 loc) · 1.86 KB
/
Factory.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
<?php
namespace meteocontrol\client\vcomapi;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use meteocontrol\client\vcomapi\handlers\AuthorizationHandlerInterface;
use meteocontrol\client\vcomapi\handlers\BasicAuthorizationHandler;
use meteocontrol\client\vcomapi\handlers\OAuthAuthorizationHandler;
class Factory {
public const API_VERSION = 'v2';
/**
* @param Config|null $config
* @param ClientInterface|null $client
* @return ApiClient
*/
public function getApiClient(Config $config = null, ClientInterface $client = null): ApiClient {
$config = $config ?: new Config();
$config->validate();
if (!$client) {
$client = self::getHttpClient($config);
}
return new ApiClient(
$client,
self::getAuthorizationHandler($config)
);
}
/**
* @param Config $config
* @return array
*/
public static function getDefaultHeaders(Config $config): array {
return [
'X-API-KEY' => $config->getApiKey(),
'Accept' => '*/*'
];
}
/**
* @param Config $config
* @return Client
*/
public static function getHttpClient(Config $config): Client {
$baseUri = sprintf("%s/%s/", $config->getApiUrl(), self::API_VERSION);
return new Client(
[
'base_uri' => $baseUri,
'headers' => self::getDefaultHeaders($config),
'debug' => false
]
);
}
/**
* @param Config $config
* @return AuthorizationHandlerInterface
*/
public static function getAuthorizationHandler(Config $config): AuthorizationHandlerInterface {
return $config->getApiAuthorizationMode() === 'basic' ?
new BasicAuthorizationHandler($config) :
new OAuthAuthorizationHandler($config);
}
}