Skip to content

Commit

Permalink
Final saloon logger :)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunoxavier committed Oct 17, 2024
1 parent 9e822d1 commit 5ec9464
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions src/Helpers/Saloon/RecordsRequestAndResponses.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,35 @@
namespace Flavorly\LaravelHelpers\Helpers\Saloon;

use GuzzleHttp\TransferStats;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Saloon\Http\PendingRequest;
use Saloon\Config;
use Saloon\Http\Response;
use Saloon\Http\PendingRequest;

trait RecordsRequestAndResponses
class RecordsRequestAndResponses
{
/**
* Little debug help to see the request and response pairs to Ray
*
* Usage: call this method in your Connector __construct, so it's only registered once
* Usage: call this method in your AppServiceProvider
*/
public function debugRequestsAndResponses(): void
public static function saloonDebugging(): void
{
if (app()->hasDebugModeEnabled() && config('laravel-helpers.debug-requests', true)) {
$this->debugRequest(function (PendingRequest $pendingRequest, RequestInterface $psrRequest) {
Config::globalMiddleware()->onRequest(function (PendingRequest $pendingRequest) {
ray('Dispatching Request', $pendingRequest);

$pendingRequest->config()->set([
'on_stats' => function (TransferStats $stats) {
// @codeCoverageIgnoreStart
ray('[Guzzle Response Body]', (string) $stats->getResponse()?->getBody());
// @codeCoverageIgnoreEnd
},
]);
});

$this->debugResponse(function (Response $response, ResponseInterface $psrResponse) {
Config::globalMiddleware()->onResponse(function (Response $response) {
ray('Response Received', $response);
});

$this->config()->set([
'on_stats' => function (TransferStats $stats) {
// @codeCoverageIgnoreStart
ray('[Guzzle Response Body]', (string) $stats->getResponse()?->getBody());
// @codeCoverageIgnoreEnd
},
]);
}
}
}

0 comments on commit 5ec9464

Please sign in to comment.