1
|
|
|
<?php |
2
|
|
|
namespace ParcelValue\ApiClient\Traits; |
3
|
|
|
|
4
|
|
|
use WebServCo\Framework\Cli\Ansi; |
5
|
|
|
use WebServCo\Framework\Cli\Sgr; |
6
|
|
|
use WebServCo\Framework\Http\Method; |
7
|
|
|
|
8
|
|
|
trait ControllerApiTrait |
9
|
|
|
{ |
10
|
|
|
protected $httpResponse; |
11
|
|
|
protected $requestHeaders; |
12
|
|
|
protected $responseStatus; |
13
|
|
|
protected $responseContent; |
14
|
|
|
protected $responseHeaders; |
15
|
|
|
|
16
|
|
|
abstract protected function config(); |
17
|
|
|
abstract public function data($key, $defaultValue = false); |
18
|
|
|
abstract protected function request(); |
19
|
|
|
abstract protected function outputCli($string, $eol = true); |
20
|
|
|
|
21
|
|
|
protected function handleApiCall($url, $method, array $headers = [], $requestData = null) |
22
|
|
|
{ |
23
|
|
|
$this->outputCli('', true); |
24
|
|
|
$this->outputCli(sprintf('REQUEST: %s %s', $method, $url), true); |
25
|
|
|
|
26
|
|
|
$logger = new \WebServCo\Framework\Log\FileLogger( |
27
|
|
|
'ParcelValueAPI', |
28
|
|
|
sprintf('%svar/log/', $this->data('path/project', '')), |
29
|
|
|
$this->request() |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
$apiHelper = new \ParcelValue\Api\Helper($logger, $this->config()->getEnv()); |
33
|
|
|
$this->httpResponse = $apiHelper->getResponse($url, $method, $headers, $requestData); |
34
|
|
|
|
35
|
|
|
$this->requestHeaders = $apiHelper->getRequestHeaders(); |
36
|
|
|
foreach ($this->requestHeaders as $key => $value) { |
37
|
|
|
$this->outputCli(sprintf('%s: %s', Ansi::sgr($key, [Sgr::BOLD]), $value), true); |
38
|
|
|
} |
39
|
|
|
if (Method::POST == $method) { |
40
|
|
|
$this->outputCli('', true); |
41
|
|
|
$this->outputCli($requestData, true); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->responseStatus = $this->httpResponse->getStatus(); |
45
|
|
|
$this->responseHeaders = $this->httpResponse->getHeaders(); |
46
|
|
|
$this->responseContent = $this->httpResponse->getContent(); |
47
|
|
|
|
48
|
|
|
$this->outputCli('', true); |
49
|
|
|
$statusCodes = \WebServCo\Framework\Http\StatusCode::getSupported(); |
50
|
|
|
$this->outputCli( |
51
|
|
|
sprintf( |
52
|
|
|
'RESPONSE: %s', |
53
|
|
|
Ansi::sgr( |
54
|
|
|
sprintf( |
55
|
|
|
'%s %s', |
56
|
|
|
$this->responseStatus, |
57
|
|
|
$statusCodes[$this->responseStatus] ?: null |
58
|
|
|
), |
59
|
|
|
[400 > $this->responseStatus ? Sgr::GREEN : sgr::RED] |
60
|
|
|
) |
61
|
|
|
), |
62
|
|
|
true |
63
|
|
|
); |
64
|
|
|
foreach ($this->responseHeaders as $key => $value) { |
65
|
|
|
$this->outputCli(sprintf('%s: %s', Ansi::sgr($key, [Sgr::BOLD]), $value), true); |
66
|
|
|
} |
67
|
|
|
$this->outputCli('', true); |
68
|
|
|
$this->outputCli($this->responseContent, true); |
69
|
|
|
|
70
|
|
|
$this->outputCli('', true); |
71
|
|
|
$this->outputCli( |
72
|
|
|
sprintf('Processed result: %s', json_encode(json_decode($this->responseContent, true), JSON_PRETTY_PRINT)), |
73
|
|
|
true |
74
|
|
|
); |
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function validateApiConfig() |
79
|
|
|
{ |
80
|
|
|
foreach ([ |
81
|
|
|
'api/url' => $this->apiUrl, |
82
|
|
|
'api/version' => $this->apiVersion, |
83
|
|
|
'clientId' => $this->clientId, |
84
|
|
|
'clientKey' => $this->clientKey, |
85
|
|
|
'serverKey' => $this->serverKey, |
86
|
|
|
] as $key => $value) { |
87
|
|
|
if (empty($value)) { |
88
|
|
|
throw new \InvalidArgumentException(sprintf('Missing or invalid configuration data: %s', $key)); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|