From 8af27a8182f0c9829ce22a3dd97970612ff50c98 Mon Sep 17 00:00:00 2001 From: nunoxavier Date: Thu, 19 Sep 2024 10:24:32 +0100 Subject: [PATCH] Adding Saloon requests debugger --- config/laravel-helpers.php | 10 +++++++ .../Saloon/RecordsRequestAndResponses.php | 29 +++++++++++++++++++ src/Helpers/Saloon/RequestRecorder.php | 14 +++++++++ src/Helpers/Saloon/ResponseRecorder.php | 14 +++++++++ 4 files changed, 67 insertions(+) create mode 100644 src/Helpers/Saloon/RecordsRequestAndResponses.php create mode 100644 src/Helpers/Saloon/RequestRecorder.php create mode 100644 src/Helpers/Saloon/ResponseRecorder.php 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 @@ +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 @@ +