|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2016 SURFnet. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
namespace SURFnet\VPN\Common\HttpClient; |
|
19
|
|
|
|
|
20
|
|
|
use GuzzleHttp\Client; |
|
21
|
|
|
use SURFnet\VPN\Common\HttpClient\Exception\HttpClientException; |
|
22
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
|
23
|
|
|
|
|
24
|
|
|
class GuzzleHttpClient implements HttpClientInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var \GuzzleHttp\Client */ |
|
27
|
|
|
private $httpClient; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(array $guzzleOptions) |
|
30
|
|
|
{ |
|
31
|
|
|
// http://docs.guzzlephp.org/en/5.3/clients.html#request-options |
|
32
|
|
|
$defaultOptions = [ |
|
33
|
|
|
'allow_redirects' => false, |
|
34
|
|
|
'timeout' => 5, |
|
35
|
|
|
'headers' => [ |
|
36
|
|
|
'Accept' => 'application/json', |
|
37
|
|
|
], |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
$this->httpClient = new Client( |
|
41
|
|
|
array_merge_recursive($defaultOptions, $guzzleOptions) |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function get($requestUri, array $requestOptions = []) |
|
46
|
|
|
{ |
|
47
|
|
|
try { |
|
48
|
|
|
return $this->httpClient->get($requestUri, $requestOptions)->json(); |
|
49
|
|
|
} catch (BadResponseException $e) { |
|
50
|
|
|
$this->handleError($e); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function post($requestUri, array $postData, array $requestOptions = []) |
|
55
|
|
|
{ |
|
56
|
|
|
try { |
|
57
|
|
|
return $this->httpClient->post( |
|
58
|
|
|
$requestUri, |
|
59
|
|
|
array_merge_recursive( |
|
60
|
|
|
$requestOptions, |
|
61
|
|
|
[ |
|
62
|
|
|
'body' => [ |
|
63
|
|
|
$postData, |
|
64
|
|
|
], |
|
65
|
|
|
] |
|
66
|
|
|
) |
|
67
|
|
|
)->json(); |
|
68
|
|
|
} catch (BadResponseException $e) { |
|
69
|
|
|
$this->handleError($e); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function handleError(BadResponseException $e) |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
|
|
try { |
|
76
|
|
|
$responseData = $e->getResponse()->json(); |
|
77
|
|
|
} catch (InvalidArgumentException $e) { |
|
|
|
|
|
|
78
|
|
|
// unable to decode JSON |
|
79
|
|
|
throw new RuntimeException('expected JSON from HTTP endpoint'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (!is_array($responseData) && !array_key_exists($responseData, 'error')) { |
|
83
|
|
|
throw new RuntimeException(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
throw new HttpClientException($responseData['error']); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.