-
Notifications
You must be signed in to change notification settings - Fork 1
/
HttpResponseOfClient.php
163 lines (132 loc) · 3.49 KB
/
HttpResponseOfClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
namespace Poirot\ApiClient;
use Poirot\ApiClient\Exceptions\exServerError;
use Poirot\ApiClient\Response\ExpectedJson;
use Poirot\Std\Struct\DataEntity;
use Psr\Http\Message\ResponseInterface;
class HttpResponseOfClient
extends ResponseOfClient
{
/**
* HttpResponseOfClient constructor.
*
* @param ResponseInterface $httpResponse
*/
function __construct(ResponseInterface $httpResponse)
{
parent::__construct(
$httpResponse->getBody()->getContents()
, $httpResponse->getStatusCode()
, $httpResponse->getHeaders()
);
}
/**
* @override normalize input meta key
* @inheritdoc
*/
function getMeta($metaKey = null)
{
if (null !== $metaKey)
$metaKey = strtolower($metaKey);
return parent::getMeta($metaKey);
}
/**
* Process Raw Body As Result
*
* :proc
* mixed function($originResult, $self);
*
* @param callable $callable
*
* @return mixed
*/
function expected(/*callable*/ $callable = null)
{
if ( $callable === null )
// Retrieve Json Parsed Data Result
$callable = $this->_getDataParser();
return parent::expected($callable);
}
/**
* Was the response successful?
*
* @return bool
*/
protected function _isSuccess()
{
$code = $this->getResponseCode();
return (200 <= $code && $code < 300);
}
/**
* Has Exception?
*
* @return \Exception|false
*/
function hasException()
{
if ( $this->exception )
// Already has exception
return $this->exception;
## Try to detect response include exception?
#
try
{
if (! $this->_isSuccess() ) {
$this->doRecognizeErrorWithStatusCode( $this->getResponseCode() );
$expected = $this->expected();
$this->doRecognizeErrorFromExpected( $expected );
}
} catch (\Exception $e) {
$this->exception = $e;
}
return $this->exception;
}
/**
* @param $responseCode
*
* @throws \Exception Api Error
*/
function doRecognizeErrorWithStatusCode($responseCode)
{
// Implement this in child classes
}
/**
* @param DataEntity $expected
*/
function doRecognizeErrorFromExpected($expected)
{
// Implement this in child classes
}
// ...
function _getDataParser()
{
if ($this->responseCode == 204) {
return function() {
return null;
};
}
if ( false !== strpos($this->getMeta('content-type'), 'application/json') )
// Retrieve Json Parsed Data Result
return new ExpectedJson;
elseif ( false !== strpos($this->getMeta('content-type'), 'text/html') )
return function ($raw) {
return $raw;
};
throw new exServerError($this->rawBody);
}
/**
* @override Override to normalize headers label/value
* @inheritdoc
*/
protected function _assertMetaData($meta)
{
$meta = parent::_assertMetaData($meta);
foreach ($meta as $label => $value) {
unset($meta[$label]);
if ( is_array($value) )
$value = implode(", ", $value);
$meta[strtolower($label)] = $value;
}
return $meta;
}
}