|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Incapsula; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use GuzzleHttp\ClientInterface; |
|
9
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
10
|
|
|
use GuzzleHttp\Psr7\Request; |
|
11
|
|
|
use Incapsula\Api\AccountsApi; |
|
12
|
|
|
use Incapsula\Api\IntegrationApi; |
|
13
|
|
|
use Incapsula\Api\SitesApi; |
|
14
|
|
|
use Incapsula\Credentials\CredentialProvider; |
|
15
|
|
|
use Incapsula\Credentials\Credentials; |
|
16
|
|
|
|
|
17
|
|
|
class Client |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var Credentials |
|
21
|
|
|
*/ |
|
22
|
|
|
private $credentials; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var ClientInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $httpClient; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(array $options = []) |
|
30
|
|
|
{ |
|
31
|
|
|
$profile = $options['profile'] ?? null; |
|
32
|
|
|
$credentials = $options['credentials'] ?? null; |
|
33
|
|
|
if (null === $credentials) { |
|
34
|
|
|
$credentials = CredentialProvider::env(); |
|
35
|
|
|
if (null === $credentials) { |
|
36
|
|
|
$credentials = CredentialProvider::ini($profile); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$this->credentials = $credentials; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getHttpClient(): ClientInterface |
|
44
|
|
|
{ |
|
45
|
|
|
if (null === $this->httpClient) { |
|
46
|
|
|
return new HttpClient(['timeout' => 20]); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $this->httpClient; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function setHttpClient(ClientInterface $client): void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->httpClient = $client; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function integration(): IntegrationApi |
|
58
|
|
|
{ |
|
59
|
|
|
return new IntegrationApi($this); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function sites(): SitesApi |
|
63
|
|
|
{ |
|
64
|
|
|
return new SitesApi($this); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function accounts(): AccountsApi |
|
68
|
|
|
{ |
|
69
|
|
|
return new AccountsApi($this); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @throws Exception |
|
74
|
|
|
*/ |
|
75
|
|
|
public function send(string $uri, array $params = [], array $headers = []): array |
|
76
|
|
|
{ |
|
77
|
|
|
$data = $this->sendRaw($uri, $params, $headers); |
|
78
|
|
|
|
|
79
|
|
|
if (0 !== $data['res']) { |
|
80
|
|
|
throw new Exception(sprintf('Bad response: %s (code: %s)', $data['res_message'], $data['res'])); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $data; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Sends a request to the Incapsula API and returns the raw response (with no checking or parsing done, beyond |
|
88
|
|
|
* ensuring there was at least *some* response). Useful for when the API endpoint does not implement the expected |
|
89
|
|
|
* 'res' structure expected by self::send(). |
|
90
|
|
|
* |
|
91
|
|
|
* @throws GuzzleException |
|
92
|
|
|
*/ |
|
93
|
|
|
public function sendRaw(string $uri, array $params = [], array $headers = []): array |
|
94
|
|
|
{ |
|
95
|
|
|
// apply credentials to all api calls except integration/ips, which doesn't require them. |
|
96
|
|
|
if (false === strpos($uri, 'integration/ips')) { |
|
97
|
|
|
$params = array_merge($params, [ |
|
98
|
|
|
'api_id' => $this->credentials->getApiId(), |
|
99
|
|
|
'api_key' => $this->credentials->getApiKey(), |
|
100
|
|
|
]); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$request = new Request('POST', $uri, $headers); |
|
104
|
|
|
$response = $this->getHttpClient()->send($request, ['form_params' => $params]); |
|
105
|
|
|
$data = json_decode((string) $response->getBody(), true); |
|
106
|
|
|
|
|
107
|
|
|
if (null === $data) { |
|
108
|
|
|
throw new Exception(sprintf('Could not parse JSON (code: %s)', json_last_error())); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $data; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|