diff --git a/config/laravel-helpers.php b/config/laravel-helpers.php index 3d0fa6d..7dd4c44 100644 --- a/config/laravel-helpers.php +++ b/config/laravel-helpers.php @@ -21,4 +21,14 @@ 'Flavorly.LaravelHelpers.' => '', ], ], + + /* + |-------------------------------------------------------------------------- + | Debug Saloon Requests / Responses + |-------------------------------------------------------------------------- + | + | When enabled, all requests and responses will be logged to ray + | + */ + 'debug-requests' => false, ]; diff --git a/src/Helpers/Saloon/RecordsRequestAndResponses.php b/src/Helpers/Saloon/RecordsRequestAndResponses.php new file mode 100644 index 0000000..6b24d9c --- /dev/null +++ b/src/Helpers/Saloon/RecordsRequestAndResponses.php @@ -0,0 +1,29 @@ +<?php + +namespace Flavorly\LaravelHelpers\Helpers\Saloon; + +use GuzzleHttp\TransferStats; +use Saloon\Http\PendingRequest; + +trait RecordsRequestAndResponses +{ + /** + * Little debug help to see the request and response pairs to Ray + */ + public function bootRecordsRequestAndResponses(PendingRequest $pendingRequest): void + { + if (app()->hasDebugModeEnabled() && config('laravel-helpers.debug-requests', true)) { + $this->middleware()->onRequest(new RequestRecorder); + $this->middleware()->onResponse(new ResponseRecorder); + $this->config()->set([ + 'on_stats' => function (TransferStats $stats) { + // @codeCoverageIgnoreStart + ray('[Guzzle Request ]', $stats->getRequest()); + ray('[Guzzle Response ]', $stats->getResponse()); + ray('[Guzzle Response Body ]', (string) $stats->getResponse()?->getBody()); + // @codeCoverageIgnoreEnd + }, + ]); + } + } +} diff --git a/src/Helpers/Saloon/RequestRecorder.php b/src/Helpers/Saloon/RequestRecorder.php new file mode 100644 index 0000000..ed8e175 --- /dev/null +++ b/src/Helpers/Saloon/RequestRecorder.php @@ -0,0 +1,14 @@ +<?php + +namespace Flavorly\LaravelHelpers\Helpers\Saloon; + +use Saloon\Contracts\RequestMiddleware; +use Saloon\Http\PendingRequest; + +class RequestRecorder implements RequestMiddleware +{ + public function __invoke(PendingRequest $pendingRequest): void + { + ray('Dispatching Request', $pendingRequest); + } +} diff --git a/src/Helpers/Saloon/ResponseRecorder.php b/src/Helpers/Saloon/ResponseRecorder.php new file mode 100644 index 0000000..0a4095a --- /dev/null +++ b/src/Helpers/Saloon/ResponseRecorder.php @@ -0,0 +1,14 @@ +<?php + +namespace Flavorly\LaravelHelpers\Helpers\Saloon; + +use Saloon\Contracts\ResponseMiddleware; +use Saloon\Http\Response; + +class ResponseRecorder implements ResponseMiddleware +{ + public function __invoke(Response $response): void + { + ray('Response Received', $response); + } +}