-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correcting response from server in case when we have 404 error (#891)
* Correcting response from server in case when we have 404 error * small fixes
- Loading branch information
1 parent
454ab2b
commit 89628fc
Showing
73 changed files
with
369 additions
and
299 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
use Mailgun\Exception; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Throwable; | ||
|
||
/** | ||
* @author Tobias Nyholm <[email protected]> | ||
|
@@ -48,7 +49,11 @@ public function __construct(string $message, int $code, ResponseInterface $respo | |
} | ||
} | ||
|
||
public static function badRequest(ResponseInterface $response) | ||
/** | ||
* @param ResponseInterface $response | ||
* @return HttpClientException | ||
*/ | ||
public static function badRequest(ResponseInterface $response): HttpClientException | ||
{ | ||
$body = $response->getBody()->__toString(); | ||
if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) { | ||
|
@@ -63,37 +68,72 @@ public static function badRequest(ResponseInterface $response) | |
return new self($message, 400, $response); | ||
} | ||
|
||
public static function unauthorized(ResponseInterface $response) | ||
/** | ||
* @param ResponseInterface $response | ||
* @return HttpClientException | ||
*/ | ||
public static function unauthorized(ResponseInterface $response): HttpClientException | ||
{ | ||
return new self('Your credentials are incorrect.', 401, $response); | ||
} | ||
|
||
public static function requestFailed(ResponseInterface $response) | ||
/** | ||
* @param ResponseInterface $response | ||
* @return HttpClientException | ||
*/ | ||
public static function requestFailed(ResponseInterface $response): HttpClientException | ||
{ | ||
return new self('Parameters were valid but request failed. Try again.', 402, $response); | ||
} | ||
|
||
public static function notFound(ResponseInterface $response) | ||
/** | ||
* @param ResponseInterface $response | ||
* @return HttpClientException | ||
*/ | ||
public static function notFound(ResponseInterface $response): HttpClientException | ||
{ | ||
return new self('The endpoint you have tried to access does not exist. Check if the domain matches the domain you have configure on Mailgun.', 404, $response); | ||
$serverMessage = []; | ||
$defaultMessage = 'The endpoint you have tried to access does not exist. Check if the domain matches the domain you have configure on Mailgun.'; | ||
try { | ||
$serverMessage = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); | ||
} catch (Throwable $throwable) { | ||
} | ||
|
||
return new self($serverMessage['message'] ?? $defaultMessage, 404, $response); | ||
} | ||
|
||
public static function conflict(ResponseInterface $response) | ||
/** | ||
* @param ResponseInterface $response | ||
* @return HttpClientException | ||
*/ | ||
public static function conflict(ResponseInterface $response): HttpClientException | ||
{ | ||
return new self('Request conflicts with current state of the target resource.', 409, $response); | ||
} | ||
|
||
public static function payloadTooLarge(ResponseInterface $response) | ||
/** | ||
* @param ResponseInterface $response | ||
* @return HttpClientException | ||
*/ | ||
public static function payloadTooLarge(ResponseInterface $response): HttpClientException | ||
{ | ||
return new self('Payload too large, your total attachment size is too big.', 413, $response); | ||
} | ||
|
||
public static function tooManyRequests(ResponseInterface $response) | ||
/** | ||
* @param ResponseInterface $response | ||
* @return HttpClientException | ||
*/ | ||
public static function tooManyRequests(ResponseInterface $response): HttpClientException | ||
{ | ||
return new self('Too many requests.', 429, $response); | ||
} | ||
|
||
public static function forbidden(ResponseInterface $response) | ||
/** | ||
* @param ResponseInterface $response | ||
* @return HttpClientException | ||
*/ | ||
public static function forbidden(ResponseInterface $response): HttpClientException | ||
{ | ||
$body = $response->getBody()->__toString(); | ||
if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) { | ||
|
@@ -108,16 +148,25 @@ public static function forbidden(ResponseInterface $response) | |
return new self($message, 403, $response); | ||
} | ||
|
||
/** | ||
* @return ResponseInterface|null | ||
*/ | ||
public function getResponse(): ?ResponseInterface | ||
{ | ||
return $this->response; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getResponseBody(): array | ||
{ | ||
return $this->responseBody; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getResponseCode(): int | ||
{ | ||
return $this->responseCode; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,10 @@ | |
|
||
namespace Mailgun\Tests\Model\Domain; | ||
|
||
use Mailgun\Model\Domain\CredentialResponseItem; | ||
use Mailgun\Tests\Model\BaseModelTest; | ||
use Mailgun\Model\Domain\CredentialResponseItem as CredentialResponseItemAlias; | ||
use Mailgun\Tests\Model\BaseModel; | ||
|
||
class CredentialResponseItemTest extends BaseModelTest | ||
class CredentialResponseItem extends BaseModel | ||
{ | ||
public function testCreate() | ||
{ | ||
|
@@ -27,7 +27,7 @@ public function testCreate() | |
"login": "user" | ||
} | ||
JSON; | ||
$model = CredentialResponseItem::create(json_decode($json, true)); | ||
$model = CredentialResponseItemAlias::create(json_decode($json, true)); | ||
$this->assertEquals('user', $model->getLogin()); | ||
$this->assertEquals('[email protected]', $model->getMailbox()); | ||
$this->assertEquals('5', $model->getSizeBytes()); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.