Skip to content

Commit

Permalink
Add debugToFile methods to SellingPartnerApi
Browse files Browse the repository at this point in the history
  • Loading branch information
jlevers committed Jun 13, 2024
1 parent 6b2dfd2 commit bfff702
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Validate and lint
name: Validate, lint, and test

on:
push:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ $connector->debugRequest(

Then make requests with the connector as usual, and you'll hit the closure above every time a request is fired. You can also debug responses in a similar fashion – check out the [Saloon docs](https://docs.saloon.dev/digging-deeper/debugging#debugging-responses) for more details.

If you want to output your debug data to a file, you can do so with the `SellingPartnerApi::debugRequestToFile()`, `SellingPartnerApi::debugResponseToFile()`, and `SellingPartnerApi::debugToFile()` methods. These methods all take an `$outputPath` argument and an optional `$die` argument.

## Supported API segments

Expand Down
88 changes: 88 additions & 0 deletions src/SellingPartnerApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DateTimeImmutable;
use GuzzleHttp\Client;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Saloon\Contracts\Authenticator;
use Saloon\Contracts\OAuthAuthenticator;
use Saloon\Exceptions\Request\RequestException;
Expand All @@ -30,6 +31,9 @@
use SellingPartnerApi\Seller\TokensV20210301\Dto\CreateRestrictedDataTokenRequest;
use SellingPartnerApi\Seller\TokensV20210301\Dto\RestrictedResource;
use SellingPartnerApi\Vendor\VendorConnector;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\VarDumper;

abstract class SellingPartnerApi extends Connector
{
Expand Down Expand Up @@ -189,6 +193,90 @@ public function getUserAgent(): string
return "jlevers/selling-partner-api/v$version/php";
}

/**
* I couldn't find any great way of reusing the original Debugger::symfonyRequestDebugger() method from
* the Saloon package, so I'm just going to copy and paste the code here, but with the VarDumper
* configured to dump to a file.
*/
public function debugRequestToFile(string $outputPath, bool $die = false): static
{
return $this->debugRequest(
function (PendingRequest $pendingRequest, RequestInterface $psrRequest) use ($outputPath) {
$headers = [];
foreach ($psrRequest->getHeaders() as $headerName => $value) {
$headers[$headerName] = implode(';', $value);
}

$className = explode('\\', $pendingRequest->getRequest()::class);
$label = end($className);

VarDumper::setHandler(function ($var) use ($outputPath, $label) {
$file = fopen($outputPath, 'a');

$cloner = new VarCloner();
$dumper = new CliDumper($file);
$cloned = $cloner->cloneVar($var)
->withContext(['label' => 'Saloon Request('.$label.') ->']);
$dumper->dump($cloned);

fclose($file);
});
VarDumper::dump([
'connector' => $pendingRequest->getConnector()::class,
'request' => $pendingRequest->getRequest()::class,
'method' => $psrRequest->getMethod(),
'uri' => (string) $psrRequest->getUri(),
'headers' => $headers,
'body' => (string) $psrRequest->getBody(),
]);
},
die: $die,
);
}

/**
* I couldn't find any great way of reusing the original Debugger::symfonyResponseDebugger() method from
* the Saloon package, so I'm just going to copy and paste the code here, but with the VarDumper
* configured to dump to a file.
*/
public function debugResponseToFile(string $outputPath, bool $die = false): static
{
return $this->debugResponse(
function (Response $response, ResponseInterface $psrResponse) use ($outputPath) {
$headers = [];
foreach ($psrResponse->getHeaders() as $headerName => $value) {
$headers[$headerName] = implode(';', $value);
}

$className = explode('\\', $response->getRequest()::class);
$label = end($className);

VarDumper::setHandler(function ($var) use ($outputPath, $label) {
$file = fopen($outputPath, 'a');

$cloner = new VarCloner();
$dumper = new CliDumper($file);
$cloned = $cloner->cloneVar($var)
->withContext(['label' => 'Saloon Response('.$label.') ->']);
$dumper->dump($cloned);

fclose($file);
});
VarDumper::dump([
'status' => $response->status(),
'headers' => $headers,
'body' => $response->body(),
]);
},
die: $die,
);
}

public function debugToFile(string $outputPath, bool $die = false): static
{
return $this->debugRequestToFile($outputPath)->debugResponseToFile($outputPath, $die);
}

public function getAccessToken(
array $scopes = [],
string $scopeSeparator = ' ',
Expand Down

0 comments on commit bfff702

Please sign in to comment.