Skip to content

Commit

Permalink
Improved debugging docs
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Dec 6, 2021
1 parent 03ac1f8 commit d0533ca
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,32 +252,39 @@ Considerations:

## Debugging

If you need to debug what HTTP requests the SDK is doing, the `HttpClient` can be provided with an optional fifth argument, which is an object implementing `Shlinkio\Shlink\SDK\Http\Debug\HttpDebuggerInterface`.
If you need to debug what HTTP requests the SDK is doing, the `HttpClient` and `ShlinkClientBuilder` can be provided with an optional last argument, which is an object implementing `Shlinkio\Shlink\SDK\Http\Debug\HttpDebuggerInterface`.

This interface exposes a method that will get invoked with the request object sent for every request.
This interface exposes a method that will get invoked with the object sent for every request.

This way, you will be able to inspect or log the headers, URL, body, or anything you need to know.
This way, you will be able to inspect or log the headers, URL, body, or anything you need.

```php
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use Psr\Http\Message\RequestInterface;
use Shlinkio\Shlink\SDK\Builder\ShlinkClientBuilder;
use Shlinkio\Shlink\SDK\Config\ShlinkConfig;
use Shlinkio\Shlink\SDK\Http\Debug\HttpDebuggerInterface;
use Shlinkio\Shlink\SDK\Http\HttpClient;

$debugger = new class implements HttpDebuggerInterface {
public function debugRequest(RequestInterface $req): void
{
var_dump($req->getUri()->__toString());
var_dump($req->getBody()->__toString());
var_dump($req->getHeaders());
}
};

$httpClient = new HttpClient(
new Client(),
new HttpFactory(),
new HttpFactory(),
/* Client */,
/* RequestBuilder */,
/* StreamBuilder */,
ShlinkConfig::fromEnv(),
new class implements HttpDebuggerInterface {
public function debugRequest(RequestInterface $req): void
{
var_dump($req->getUri()->__toString());
var_dump($req->getBody()->__toString());
var_dump($req->getHeaders());
}
}
$debugger,
)
$builder = new ShlinkClientBuilder(
/* Client */,
/* RequestBuilder */,
/* StreamBuilder */,
$debugger,
)
```

0 comments on commit d0533ca

Please sign in to comment.