Skip to content

Commit

Permalink
Merge pull request #4 from vsouz4/support-429
Browse files Browse the repository at this point in the history
add support for HTTP 429 - too many requests
  • Loading branch information
tommey authored Sep 6, 2024
2 parents a97f765 + cbbaebf commit ae89878
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [2.2.0] - 2024-09-06
### Added
- Support for HTTP status code 429

## [2.1.0] - 2023-11-01
### Added
- Support for HTTP status codes 409, 410
Expand Down
16 changes: 9 additions & 7 deletions src/Factory/ResponseExceptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use DoclerLabs\ApiClientException\GoneResponseException;
use DoclerLabs\ApiClientException\NotFoundResponseException;
use DoclerLabs\ApiClientException\PaymentRequiredResponseException;
use DoclerLabs\ApiClientException\TooManyRequestsResponseException;
use DoclerLabs\ApiClientException\UnauthorizedResponseException;
use DoclerLabs\ApiClientException\UnexpectedResponseException;
use Fig\Http\Message\StatusCodeInterface;
Expand All @@ -19,13 +20,14 @@
class ResponseExceptionFactory
{
private const RESPONSE_EXCEPTIONS = [
StatusCodeInterface::STATUS_BAD_REQUEST => BadRequestResponseException::class,
StatusCodeInterface::STATUS_UNAUTHORIZED => UnauthorizedResponseException::class,
StatusCodeInterface::STATUS_PAYMENT_REQUIRED => PaymentRequiredResponseException::class,
StatusCodeInterface::STATUS_FORBIDDEN => ForbiddenResponseException::class,
StatusCodeInterface::STATUS_NOT_FOUND => NotFoundResponseException::class,
StatusCodeInterface::STATUS_CONFLICT => ConflictResponseException::class,
StatusCodeInterface::STATUS_GONE => GoneResponseException::class,
StatusCodeInterface::STATUS_BAD_REQUEST => BadRequestResponseException::class,
StatusCodeInterface::STATUS_UNAUTHORIZED => UnauthorizedResponseException::class,
StatusCodeInterface::STATUS_PAYMENT_REQUIRED => PaymentRequiredResponseException::class,
StatusCodeInterface::STATUS_FORBIDDEN => ForbiddenResponseException::class,
StatusCodeInterface::STATUS_NOT_FOUND => NotFoundResponseException::class,
StatusCodeInterface::STATUS_CONFLICT => ConflictResponseException::class,
StatusCodeInterface::STATUS_GONE => GoneResponseException::class,
StatusCodeInterface::STATUS_TOO_MANY_REQUESTS => TooManyRequestsResponseException::class,
];

public function create(string $message, ResponseInterface $response): UnexpectedResponseException
Expand Down
9 changes: 9 additions & 0 deletions src/TooManyRequestsResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace DoclerLabs\ApiClientException;

class TooManyRequestsResponseException extends UnexpectedResponseException
{
}
2 changes: 2 additions & 0 deletions test/suite/unit/Factory/ResponseExceptionsFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use DoclerLabs\ApiClientException\GoneResponseException;
use DoclerLabs\ApiClientException\NotFoundResponseException;
use DoclerLabs\ApiClientException\PaymentRequiredResponseException;
use DoclerLabs\ApiClientException\TooManyRequestsResponseException;
use DoclerLabs\ApiClientException\UnauthorizedResponseException;
use DoclerLabs\ApiClientException\UnexpectedResponseException;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -54,6 +55,7 @@ public function exceptionsDataProvider(): array
[404, 'not found', NotFoundResponseException::class],
[409, 'conflict', ConflictResponseException::class],
[410, 'gone', GoneResponseException::class],
[429, 'too many requests', TooManyRequestsResponseException::class],
[456, 'others', UnexpectedResponseException::class],
];
}
Expand Down
34 changes: 34 additions & 0 deletions test/suite/unit/TooManyRequestsResponseExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace DoclerLabs\ApiClientBase\Test\Unit;

use DoclerLabs\ApiClientException\TooManyRequestsResponseException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Throwable;

/**
* @covers \DoclerLabs\ApiClientException\TooManyRequestsResponseException
*/
class TooManyRequestsResponseExceptionTest extends TestCase
{
public function testException(): void
{
$statusCode = 429;

/** @var ResponseInterface|MockObject $response */
$response = $this->createMock(ResponseInterface::class);
$response
->expects(self::once())
->method('getStatusCode')
->willReturn($statusCode);

$sut = new TooManyRequestsResponseException('', $response);

self::assertInstanceOf(Throwable::class, $sut);
self::assertEquals($statusCode, $sut->getResponse()->getStatusCode());
}
}

0 comments on commit ae89878

Please sign in to comment.