Skip to content

Commit

Permalink
Adding Saloon requests debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
nunoxavier committed Sep 19, 2024
1 parent 1c03420 commit 8af27a8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
10 changes: 10 additions & 0 deletions config/laravel-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,14 @@
'Flavorly.LaravelHelpers.' => '',
],
],

/*
|--------------------------------------------------------------------------
| Debug Saloon Requests / Responses
|--------------------------------------------------------------------------
|
| When enabled, all requests and responses will be logged to ray
|
*/
'debug-requests' => false,
];
29 changes: 29 additions & 0 deletions src/Helpers/Saloon/RecordsRequestAndResponses.php
Original file line number Diff line number Diff line change
@@ -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
},
]);
}
}
}
14 changes: 14 additions & 0 deletions src/Helpers/Saloon/RequestRecorder.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
14 changes: 14 additions & 0 deletions src/Helpers/Saloon/ResponseRecorder.php
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 8af27a8

Please sign in to comment.