|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* eduVPN - End-user friendly VPN. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright: 2016-2017, The Commons Conservancy eduVPN Programme |
|
7
|
|
|
* SPDX-License-Identifier: AGPL-3.0+ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace SURFnet\VPN\Common\HttpClient; |
|
11
|
|
|
|
|
12
|
|
|
use SURFnet\VPN\Common\HttpClient\Exception\ApiException; |
|
13
|
|
|
use SURFnet\VPN\Common\HttpClient\Exception\HttpClientException; |
|
14
|
|
|
|
|
15
|
|
|
class ServerClient |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var HttpClientInterface */ |
|
18
|
|
|
private $httpClient; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
private $baseUri; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(HttpClientInterface $httpClient, $baseUri) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->httpClient = $httpClient; |
|
26
|
|
|
$this->baseUri = $baseUri; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function get($requestPath, array $getData = []) |
|
30
|
|
|
{ |
|
31
|
|
|
$requestUri = sprintf('%s/%s', $this->baseUri, $requestPath); |
|
32
|
|
|
if (0 !== count($getData)) { |
|
33
|
|
|
$requestUri = sprintf('%s?%s', $requestUri, http_build_query($getData)); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return self::responseHandler( |
|
37
|
|
|
'GET', |
|
38
|
|
|
$requestPath, |
|
39
|
|
|
$this->httpClient->get($requestUri) |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function post($requestPath, array $postData) |
|
44
|
|
|
{ |
|
45
|
|
|
$requestUri = sprintf('%s/%s', $this->baseUri, $requestPath); |
|
46
|
|
|
|
|
47
|
|
|
return self::responseHandler( |
|
48
|
|
|
'POST', |
|
49
|
|
|
$requestPath, |
|
50
|
|
|
$this->httpClient->post($requestUri, $postData) |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private static function responseHandler($requestMethod, $requestPath, array $clientResponse) |
|
55
|
|
|
{ |
|
56
|
|
|
list($statusCode, $responseData) = $clientResponse; |
|
57
|
|
|
self::validateClientResponse($requestMethod, $requestPath, $statusCode, $responseData); |
|
58
|
|
|
|
|
59
|
|
|
if (400 <= $statusCode) { |
|
60
|
|
|
// either we sent an incorrect request, or there is a server error |
|
61
|
|
|
throw new HttpClientException(sprintf('[%d] %s "/%s": %s', $statusCode, $requestMethod, $requestPath, $responseData['error'])); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// the request was correct, and there was not a server error |
|
65
|
|
|
if ($responseData[$requestPath]['ok']) { |
|
66
|
|
|
// our request was handled correctly |
|
67
|
|
|
if (array_key_exists('data', $responseData[$requestPath])) { |
|
68
|
|
|
return $responseData[$requestPath]['data']; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// our request was not handled correctly, something went wrong... |
|
75
|
|
|
throw new ApiException($responseData[$requestPath]['error']); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private static function validateClientResponse($requestMethod, $requestPath, $statusCode, $responseData) |
|
79
|
|
|
{ |
|
80
|
|
|
// responseData MUST be array |
|
81
|
|
|
if (!is_array($responseData)) { |
|
82
|
|
|
throw new HttpClientException(sprintf('[%d] %s "/%s": responseData MUST be array', $statusCode, $requestMethod, $requestPath)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
View Code Duplication |
if (400 <= $statusCode) { |
|
|
|
|
|
|
86
|
|
|
// if status code is 4xx or 5xx it MUST have an 'error' field |
|
87
|
|
|
if (!array_key_exists('error', $responseData)) { |
|
88
|
|
|
throw new HttpClientException(sprintf('[%d] %s "/%s": responseData MUST contain "error" field', $statusCode, $requestMethod, $requestPath)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
View Code Duplication |
if (!array_key_exists($requestPath, $responseData)) { |
|
|
|
|
|
|
95
|
|
|
throw new HttpClientException(sprintf('[%d] %s "/%s": responseData MUST contain "%s" field', $statusCode, $requestMethod, $requestPath, $requestPath)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
View Code Duplication |
if (!array_key_exists('ok', $responseData[$requestPath])) { |
|
|
|
|
|
|
99
|
|
|
throw new HttpClientException(sprintf('[%d] %s "/%s": responseData MUST contain "%s/ok" field', $statusCode, $requestMethod, $requestPath, $requestPath)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if (!$responseData[$requestPath]['ok']) { |
|
103
|
|
|
// not OK response, MUST contain error field |
|
104
|
|
View Code Duplication |
if (!array_key_exists('error', $responseData[$requestPath])) { |
|
|
|
|
|
|
105
|
|
|
throw new HttpClientException(sprintf('[%d] %s "/%s": responseData MUST contain "%s/error" field', $statusCode, $requestMethod, $requestPath, $requestPath)); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.