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