|
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; |
|
7
|
|
|
|
|
8
|
|
|
trait ControllerApiTrait |
|
9
|
|
|
{ |
|
10
|
|
|
protected $logger; |
|
11
|
|
|
protected $curlBrowser; |
|
12
|
|
|
|
|
13
|
|
|
protected $httpResponse; |
|
14
|
|
|
protected $requestHeaders; |
|
15
|
|
|
protected $responseStatus; |
|
16
|
|
|
protected $responseContent; |
|
17
|
|
|
protected $responseHeaders; |
|
18
|
|
|
|
|
19
|
|
|
abstract protected function config(); |
|
20
|
|
|
abstract public function data($key, $defaultValue = false); |
|
21
|
|
|
abstract protected function request(); |
|
22
|
|
|
abstract protected function outputCli($string, $eol = true); |
|
23
|
|
|
|
|
24
|
|
|
protected function initApiCall() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->logger = new \WebServCo\Framework\FileLogger( |
|
27
|
|
|
__FUNCTION__, |
|
28
|
|
|
sprintf('%svar/log/', $this->data('path/project', '')), |
|
29
|
|
|
$this->request() |
|
30
|
|
|
); |
|
31
|
|
|
$this->curlBrowser = new \WebServCo\Framework\CurlBrowser($this->logger); |
|
32
|
|
|
if (\WebServCo\Framework\Environment::ENV_DEV == $this->config()->getEnv()) { |
|
33
|
|
|
$this->curlBrowser->setSkipSSlVerification(true); |
|
34
|
|
|
} |
|
35
|
|
|
$this->curlBrowser->setRequestHeader('Accept', \WebServCo\Api\JsonApi\Document::CONTENT_TYPE); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function handleApiCall($url, $method, array $headers = [], $postData = null) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->outputCli('', true); |
|
41
|
|
|
$this->outputCli(sprintf('REQUEST: %s %s', $method, $url), true); |
|
42
|
|
|
|
|
43
|
|
|
foreach ($headers as $key => $value) { |
|
44
|
|
|
$this->curlBrowser->setRequestHeader($key, $value); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
switch ($method) { |
|
48
|
|
|
case Http::METHOD_POST: |
|
49
|
|
|
$this->curlBrowser->setPostData($postData); |
|
50
|
|
|
break; |
|
51
|
|
|
case Http::METHOD_GET: |
|
52
|
|
|
case Http::METHOD_HEAD: |
|
53
|
|
|
break; |
|
54
|
|
|
default: |
|
55
|
|
|
throw new \WebServCo\Framework\Exceptions\NotImplementedException('Functionality not implemented'); |
|
56
|
|
|
break; |
|
57
|
|
|
} |
|
58
|
|
|
$this->curlBrowser->setMethod($method); |
|
59
|
|
|
$this->httpResponse = $this->curlBrowser->retrieve($url); |
|
60
|
|
|
|
|
61
|
|
|
$this->requestHeaders = $this->curlBrowser->getRequestHeaders(); |
|
62
|
|
|
foreach ($this->requestHeaders as $key => $value) { |
|
63
|
|
|
$this->outputCli(sprintf('%s: %s', Ansi::sgr($key, [Sgr::BOLD]), $value), true); |
|
64
|
|
|
} |
|
65
|
|
|
if (Http::METHOD_POST == $method) { |
|
66
|
|
|
$this->outputCli('', true); |
|
67
|
|
|
$this->outputCli($postData, true); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->responseStatus = $this->httpResponse->getStatus(); |
|
71
|
|
|
$this->responseHeaders = $this->httpResponse->getHeaders(); |
|
72
|
|
|
$this->responseContent = $this->httpResponse->getContent(); |
|
73
|
|
|
|
|
74
|
|
|
$this->outputCli('', true); |
|
75
|
|
|
$this->outputCli( |
|
76
|
|
|
sprintf( |
|
77
|
|
|
'RESPONSE: %s', |
|
78
|
|
|
Ansi::sgr( |
|
79
|
|
|
sprintf( |
|
80
|
|
|
'%s %s', |
|
81
|
|
|
$this->responseStatus, |
|
82
|
|
|
Http::$statusCodes[$this->responseStatus] ?: null |
|
83
|
|
|
), |
|
84
|
|
|
[400 > $this->responseStatus ? Sgr::GREEN : sgr::RED] |
|
85
|
|
|
) |
|
86
|
|
|
), |
|
87
|
|
|
true |
|
88
|
|
|
); |
|
89
|
|
|
foreach ($this->responseHeaders as $key => $value) { |
|
90
|
|
|
$this->outputCli(sprintf('%s: %s', Ansi::sgr($key, [Sgr::BOLD]), $value), true); |
|
91
|
|
|
} |
|
92
|
|
|
$this->outputCli('', true); |
|
93
|
|
|
$this->outputCli($this->responseContent, true); |
|
94
|
|
|
|
|
95
|
|
|
$this->outputCli('', true); |
|
96
|
|
|
$this->outputCli( |
|
97
|
|
|
sprintf('Processed result: %s', json_encode(json_decode($this->responseContent, true), JSON_PRETTY_PRINT)), |
|
98
|
|
|
true |
|
99
|
|
|
); |
|
100
|
|
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function validateApiConfig() |
|
104
|
|
|
{ |
|
105
|
|
|
foreach ([ |
|
106
|
|
|
'api/url' => $this->apiUrl, |
|
107
|
|
|
'api/version' => $this->apiVersion, |
|
108
|
|
|
'clientId' => $this->clientId, |
|
109
|
|
|
'clientKey' => $this->clientKey, |
|
110
|
|
|
'serverKey' => $this->serverKey, |
|
111
|
|
|
] as $key => $value) { |
|
112
|
|
|
if (empty($value)) { |
|
113
|
|
|
throw new \InvalidArgumentException(sprintf('Missing or invalid configuration data: %s', $key)); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|