Skip to content

Commit 910ea21

Browse files
authored
API-1700: Early return for HTTP SUCCESS Responses (#219)
1 parent 8795441 commit 910ea21

File tree

3 files changed

+53
-28
lines changed

3 files changed

+53
-28
lines changed

spec/Client/HttpClientSpec.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ function it_sends_a_successful_request(
5151
'http://akeneo.com/api/rest/v1/products/foo'
5252
)->willReturn($request);
5353

54+
$response->getStatusCode()->willReturn(HttpClient::HTTP_OK);
55+
5456
$request->withBody($stream)->willReturn($request);
5557
$request->withHeader('Content-Type', 'application/json')->willReturn($request);
5658

src/Client/HttpClient.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,23 @@
1717
*/
1818
class HttpClient implements HttpClientInterface
1919
{
20-
/** @var ClientInterface */
21-
protected $httpClient;
20+
const HTTP_OK = 200;
21+
const HTTP_CREATED = 201;
22+
const HTTP_NO_CONTENT = 204;
23+
const HTTP_BAD_REQUEST = 400;
24+
const HTTP_UNAUTHORIZED = 401;
25+
const HTTP_FORBIDDEN = 403;
26+
const HTTP_NOT_FOUND = 404;
27+
const HTTP_METHOD_NOT_ALLOWED = 405;
28+
const HTTP_NOT_ACCEPTABLE = 406;
29+
const HTTP_UNSUPPORTED_MEDIA_TYPE = 415;
30+
const HTTP_UNPROCESSABLE_ENTITY = 422;
31+
const HTTP_TOO_MANY_REQUESTS = 429;
2232

23-
/** @var RequestFactoryInterface */
24-
protected $requestFactory;
25-
26-
/** @var HttpExceptionHandler */
27-
protected $httpExceptionHandler;
28-
29-
/** @var StreamFactoryInterface */
30-
private $streamFactory;
33+
protected ClientInterface $httpClient;
34+
protected RequestFactoryInterface $requestFactory;
35+
protected HttpExceptionHandler $httpExceptionHandler;
36+
private StreamFactoryInterface $streamFactory;
3137

3238
public function __construct(
3339
ClientInterface $httpClient,

src/Client/HttpExceptionHandler.php

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,49 +24,47 @@ class HttpExceptionHandler
2424
/**
2525
* Transforms response to an exception if possible.
2626
*
27-
* @param RequestInterface $request Request of the call
28-
* @param ResponseInterface $response Response of the call
29-
*
30-
* @throws BadRequestHttpException If response status code is a 400
3127
* @throws UnauthorizedHttpException If response status code is a 401
3228
* @throws NotFoundHttpException If response status code is a 404
3329
* @throws UnprocessableEntityHttpException If response status code is a 422
3430
* @throws ClientErrorHttpException If response status code is a 4xx
3531
* @throws ServerErrorHttpException If response status code is a 5xx
3632
*
37-
* @return ResponseInterface
33+
* @throws BadRequestHttpException If response status code is a 400
3834
*/
39-
public function transformResponseToException(RequestInterface $request, ResponseInterface $response): ResponseInterface
40-
{
41-
if ($response->getStatusCode() >= 300 && $response->getStatusCode() < 400) {
35+
public function transformResponseToException(
36+
RequestInterface $request,
37+
ResponseInterface $response
38+
): ResponseInterface {
39+
if ($this->isSuccessStatusCode($response->getStatusCode())) {
40+
return $response;
41+
}
42+
43+
if ($this->isRedirectionStatusCode($response->getStatusCode())) {
4244
throw new RedirectionHttpException($this->getResponseMessage($response), $request, $response);
4345
}
4446

45-
if (400 === $response->getStatusCode()) {
47+
if (HttpClient::HTTP_BAD_REQUEST === $response->getStatusCode()) {
4648
throw new BadRequestHttpException($this->getResponseMessage($response), $request, $response);
4749
}
4850

49-
if (401 === $response->getStatusCode()) {
51+
if (HttpClient::HTTP_UNAUTHORIZED === $response->getStatusCode()) {
5052
throw new UnauthorizedHttpException($this->getResponseMessage($response), $request, $response);
5153
}
5254

53-
if (404 === $response->getStatusCode()) {
55+
if (HttpClient::HTTP_NOT_FOUND === $response->getStatusCode()) {
5456
throw new NotFoundHttpException($this->getResponseMessage($response), $request, $response);
5557
}
5658

57-
if (422 === $response->getStatusCode()) {
59+
if (HttpClient::HTTP_UNPROCESSABLE_ENTITY === $response->getStatusCode()) {
5860
throw new UnprocessableEntityHttpException($this->getResponseMessage($response), $request, $response);
5961
}
6062

61-
if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 500) {
63+
if ($this->isApiClientErrorStatusCode($response->getStatusCode())) {
6264
throw new ClientErrorHttpException($this->getResponseMessage($response), $request, $response);
6365
}
6466

65-
if ($response->getStatusCode() >= 500 && $response->getStatusCode() < 600) {
66-
throw new ServerErrorHttpException($this->getResponseMessage($response), $request, $response);
67-
}
68-
69-
return $response;
67+
throw new ServerErrorHttpException($this->getResponseMessage($response), $request, $response);
7068
}
7169

7270
/**
@@ -86,4 +84,23 @@ protected function getResponseMessage(ResponseInterface $response): string
8684

8785
return isset($decodedBody['message']) ? $decodedBody['message'] : $response->getReasonPhrase();
8886
}
87+
88+
private function isSuccessStatusCode(int $statusCode): bool
89+
{
90+
return in_array($statusCode, [
91+
HttpClient::HTTP_OK,
92+
HttpClient::HTTP_CREATED,
93+
HttpClient::HTTP_NO_CONTENT,
94+
]);
95+
}
96+
97+
private function isApiClientErrorStatusCode(int $statusCode): bool
98+
{
99+
return $statusCode >= 400 && $statusCode < 500;
100+
}
101+
102+
private function isRedirectionStatusCode(int $statusCode): bool
103+
{
104+
return $statusCode >= 300 && $statusCode < 400;
105+
}
89106
}

0 commit comments

Comments
 (0)