|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the geo-api-library package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2020 WEBEWEB |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WBW\Library\GeoAPI\Provider; |
|
13
|
|
|
|
|
14
|
|
|
use Exception; |
|
15
|
|
|
use GuzzleHttp\Client; |
|
16
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
17
|
|
|
use Psr\Log\LoggerInterface; |
|
18
|
|
|
use WBW\Library\Core\Exception\ApiException; |
|
19
|
|
|
use WBW\Library\Core\Provider\AbstractProvider as BaseProvider; |
|
20
|
|
|
use WBW\Library\GeoAPI\Request\AbstractRequest; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Abstract provider. |
|
24
|
|
|
* |
|
25
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
26
|
|
|
* @package WBW\Library\GeoAPI\Provider |
|
27
|
|
|
* @abstract |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract class AbstractProvider extends BaseProvider { |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param LoggerInterface|null $logger The logger. |
|
35
|
|
|
*/ |
|
36
|
65 |
|
public function __construct(LoggerInterface $logger = null) { |
|
37
|
65 |
|
parent::__construct($logger); |
|
38
|
65 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Build the configuration. |
|
42
|
|
|
* |
|
43
|
|
|
* @return array Returns the configuration. |
|
44
|
|
|
*/ |
|
45
|
50 |
|
private function buildConfiguration(): array { |
|
46
|
|
|
return [ |
|
47
|
50 |
|
"base_uri" => $this->getEndpointPath() . "/", |
|
48
|
50 |
|
"debug" => $this->getDebug(), |
|
49
|
|
|
"headers" => [ |
|
50
|
|
|
"User-Agent" => "webeweb/geo-api-library", |
|
51
|
|
|
], |
|
52
|
|
|
"synchronous" => true, |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Call the API. |
|
58
|
|
|
* |
|
59
|
|
|
* @param AbstractRequest $request The request. |
|
60
|
|
|
* @param array $queryData The query data. |
|
61
|
|
|
* @param array $postData The post data. |
|
62
|
|
|
* @return string Returns the raw response. |
|
63
|
|
|
* @throws GuzzleException Throws a Guzzle exception if an error occurs. |
|
64
|
|
|
* @throws ApiException Throws an API exception if an error occurs. |
|
65
|
|
|
*/ |
|
66
|
50 |
|
protected function callApi(AbstractRequest $request, array $queryData, array $postData = []): string { |
|
67
|
|
|
|
|
68
|
|
|
try { |
|
69
|
|
|
|
|
70
|
50 |
|
$config = $this->buildConfiguration(); |
|
71
|
|
|
|
|
72
|
50 |
|
$client = new Client($config); |
|
73
|
|
|
|
|
74
|
50 |
|
$method = 0 === count($postData) ? "GET" : "POST"; |
|
75
|
50 |
|
$uri = substr($request->getResourcePath(), 1); |
|
76
|
10 |
|
$options = [ |
|
77
|
50 |
|
"query" => $queryData, |
|
78
|
50 |
|
"multipart" => $postData, |
|
79
|
|
|
]; |
|
80
|
|
|
|
|
81
|
50 |
|
$this->logInfo(sprintf("Call API provider %s %s", $method, $uri), ["config" => $config]); |
|
82
|
|
|
|
|
83
|
50 |
|
$response = $client->request($method, $uri, $options); |
|
84
|
|
|
|
|
85
|
50 |
|
return $response->getBody()->getContents(); |
|
86
|
|
|
} catch (Exception $ex) { |
|
87
|
|
|
|
|
88
|
|
|
throw new ApiException(sprintf("Call API provider failed"), 500, $ex); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get the endpoint path. |
|
94
|
|
|
* |
|
95
|
|
|
* @return string Returns the endpoint path. |
|
96
|
|
|
*/ |
|
97
|
|
|
abstract public function getEndpointPath(): string; |
|
98
|
|
|
} |