Skip to content

Commit

Permalink
Updates:
Browse files Browse the repository at this point in the history
- Implement RateLimitExceededException for too many client requests.
- Client errors are now throwing TransmissionFailedException which extends MyDataException.
  • Loading branch information
firebed committed Jun 14, 2024
1 parent 1a13f2d commit 5a7ab4a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 16 deletions.
9 changes: 6 additions & 3 deletions src/Exceptions/MyDataAuthenticationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Firebed\AadeMyData\Exceptions;

class MyDataAuthenticationException extends MyDataException

use Throwable;

class MyDataAuthenticationException extends TransmissionFailedException
{
public function __construct()
public function __construct(int $code = 0, Throwable $previous = null)
{
parent::__construct("Authentication failed. Please check your user id and subscription key.", 401);
parent::__construct("Authentication failed. Please check your user id and subscription key.", $code, $previous);
}
}
8 changes: 5 additions & 3 deletions src/Exceptions/MyDataConnectionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Firebed\AadeMyData\Exceptions;

class MyDataConnectionException extends MyDataException
use Throwable;

class MyDataConnectionException extends TransmissionFailedException
{
public function __construct()
public function __construct(int $code = 0, Throwable $previous = null)
{
parent::__construct("myDATA servers are down or unreachable. Please try again later.");
parent::__construct("myDATA servers are down or unreachable. Please try again later.", $code, $previous);
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/RateLimitExceededException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Firebed\AadeMyData\Exceptions;

use Throwable;

class RateLimitExceededException extends TransmissionFailedException
{
public function __construct(string $message = null, int $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
8 changes: 8 additions & 0 deletions src/Exceptions/TransmissionFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Firebed\AadeMyData\Exceptions;

class TransmissionFailedException extends MyDataException
{

}
30 changes: 20 additions & 10 deletions src/Http/MyDataRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Firebed\AadeMyData\Exceptions\MyDataAuthenticationException;
use Firebed\AadeMyData\Exceptions\MyDataConnectionException;
use Firebed\AadeMyData\Exceptions\MyDataException;
use Firebed\AadeMyData\Exceptions\RateLimitExceededException;
use Firebed\AadeMyData\Exceptions\TransmissionFailedException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Handler\MockHandler;
Expand Down Expand Up @@ -97,10 +99,10 @@ public static function setConnectionTimeout(int $seconds): void
}

/**
* You can customize requests created and transferred by a client using request options.
* Request options control various aspects of a request including, headers, query string
* You can customize requests created and transferred by a client using request options.
* Request options control various aspects of a request including, headers, query string
* parameters, timeout settings, the body of a request, and much more.
*
*
* @param array $requestOptions
* @return void
* @see https://docs.guzzlephp.org/en/stable/request-options.html
Expand Down Expand Up @@ -131,7 +133,7 @@ public static function isProvider(): bool
private static function validateCredentials(): void
{
if (empty(self::$user_id) || empty(self::$subscription_key)) {
throw new MyDataAuthenticationException();
throw new MyDataAuthenticationException(401);
}
}

Expand All @@ -145,7 +147,7 @@ protected function get(array $query): ResponseInterface
try {
return $this->client()->get($this->getUrl(), ['query' => $query]);
} catch (GuzzleException $e) {
$this->handleException($e);
$this->handleTransmissionException($e);
}
}

Expand All @@ -168,25 +170,33 @@ protected function post(array $query = null, string $body = null): ResponseInter
try {
return $this->client()->post($this->getUrl(), $params);
} catch (GuzzleException $e) {
$this->handleException($e);
$this->handleTransmissionException($e);
}
}

/**
* Authorization errors, bad request, communication errors,
* myDATA server errors, rate limits, connection timeout, etc.
*
* @throws MyDataAuthenticationException|MyDataException
*/
protected function handleException(GuzzleException $exception)
protected function handleTransmissionException(GuzzleException $exception)
{
if ($exception->getCode() === 401) {
throw new MyDataAuthenticationException();
throw new MyDataAuthenticationException($exception->getCode(), $exception);
}

// In case the endpoint url is wrong or connection timed out
if ($exception->getCode() === 0) {
throw new MyDataConnectionException();
throw new MyDataConnectionException($exception->getCode(), $exception);
}

// Rate limit exception
if ($exception->getCode() === 429) {
throw new RateLimitExceededException($exception->getMessage(), $exception->getCode(), $exception);
}

throw new MyDataException($exception->getMessage(), $exception->getCode());
throw new TransmissionFailedException($exception->getMessage(), $exception->getCode(), $exception);
}

private function client(): Client
Expand Down

0 comments on commit 5a7ab4a

Please sign in to comment.