-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Fetch classes and exceptions for HTTP handling
- Loading branch information
1 parent
0eded66
commit 3e686d9
Showing
24 changed files
with
838 additions
and
1,593 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
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fetch\Events; | ||
|
||
use Fetch\Exceptions\ConnectionException; | ||
use Fetch\Request; | ||
|
||
class ConnectionFailed | ||
{ | ||
/** | ||
* The request instance. | ||
*/ | ||
public Request $request; | ||
|
||
/** | ||
* The exception instance. | ||
*/ | ||
public ConnectionException $exception; | ||
|
||
/** | ||
* Create a new event instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct(Request $request, ConnectionException $exception) | ||
{ | ||
$this->request = $request; | ||
$this->exception = $exception; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fetch\Events; | ||
|
||
use Fetch\Request; | ||
|
||
class RequestSending | ||
{ | ||
/** | ||
* The request instance. | ||
*/ | ||
public Request $request; | ||
|
||
/** | ||
* Create a new event instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct(Request $request) | ||
{ | ||
$this->request = $request; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fetch\Events; | ||
|
||
use Fetch\Request; | ||
use Fetch\Response; | ||
|
||
class ResponseReceived | ||
{ | ||
/** | ||
* The request instance. | ||
*/ | ||
public Request $request; | ||
|
||
/** | ||
* The response instance. | ||
*/ | ||
public Response $response; | ||
|
||
/** | ||
* Create a new event instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct(Request $request, Response $response) | ||
{ | ||
$this->request = $request; | ||
$this->response = $response; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fetch\Exceptions; | ||
|
||
class ConnectionException extends HttpClientException | ||
{ | ||
// | ||
} |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fetch\Exceptions; | ||
|
||
use Exception; | ||
|
||
class HttpClientException extends Exception | ||
{ | ||
// | ||
} |
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fetch\Exceptions; | ||
|
||
use Fetch\Response; | ||
use GuzzleHttp\Psr7\Message; | ||
|
||
class RequestException extends HttpClientException | ||
{ | ||
/** | ||
* The response instance. | ||
*/ | ||
public Response $response; | ||
|
||
/** | ||
* The truncation length for the exception message. | ||
*/ | ||
public static int|false $truncateAt = 120; | ||
|
||
/** | ||
* Create a new exception instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct(Response $response) | ||
{ | ||
parent::__construct( | ||
$this->prepareMessage($response), | ||
$response->status() | ||
); | ||
|
||
$this->response = $response; | ||
} | ||
|
||
/** | ||
* Enable truncation of request exception messages. | ||
*/ | ||
public static function truncate(): void | ||
{ | ||
static::$truncateAt = 120; | ||
} | ||
|
||
/** | ||
* Set the truncation length for request exception messages. | ||
*/ | ||
public static function truncateAt(int $length): void | ||
{ | ||
static::$truncateAt = $length; | ||
} | ||
|
||
/** | ||
* Disable truncation of request exception messages. | ||
*/ | ||
public static function dontTruncate(): void | ||
{ | ||
static::$truncateAt = false; | ||
} | ||
|
||
/** | ||
* Prepare the exception message. | ||
*/ | ||
protected function prepareMessage(Response $response): string | ||
{ | ||
$message = "HTTP request returned status code {$response->status()}"; | ||
|
||
$summary = static::$truncateAt | ||
? Message::bodySummary($response->toPsrResponse(), static::$truncateAt) | ||
: Message::toString($response->toPsrResponse()); | ||
|
||
return is_null($summary) ? $message : $message .= ":\n{$summary}\n"; | ||
} | ||
} |
Oops, something went wrong.