Skip to content

Commit 0c18616

Browse files
authored
Add a "debug" config to print HTTP requests and responses (#758)
* Draft a "debug" config * Bugfix * Bugfixes
1 parent 3024522 commit 0c18616

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/AbstractApi.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,16 @@ final protected function getResponse(Request $request, ?RequestContext $context
136136
]
137137
);
138138

139-
return new Response($response, $this->httpClient, $this->logger);
139+
if ($debug = \filter_var($this->configuration->get('debug'), \FILTER_VALIDATE_BOOLEAN)) {
140+
$this->logger->debug('AsyncAws HTTP request sent: {method} {endpoint}', [
141+
'method' => $request->getMethod(),
142+
'endpoint' => $request->getEndpoint(),
143+
'headers' => json_encode($request->getHeaders()),
144+
'body' => 0 === $length ? null : $requestBody,
145+
]);
146+
}
147+
148+
return new Response($response, $this->httpClient, $this->logger, $debug);
140149
}
141150

142151
/**

src/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ final class Configuration
1717
public const DEFAULT_REGION = 'us-east-1';
1818

1919
public const OPTION_REGION = 'region';
20+
public const OPTION_DEBUG = 'debug';
2021
public const OPTION_PROFILE = 'profile';
2122
public const OPTION_ACCESS_KEY_ID = 'accessKeyId';
2223
public const OPTION_SECRET_ACCESS_KEY = 'accessKeySecret';
@@ -34,6 +35,7 @@ final class Configuration
3435

3536
private const AVAILABLE_OPTIONS = [
3637
self::OPTION_REGION => true,
38+
self::OPTION_DEBUG => true,
3739
self::OPTION_PROFILE => true,
3840
self::OPTION_ACCESS_KEY_ID => true,
3941
self::OPTION_SECRET_ACCESS_KEY => true,
@@ -69,6 +71,7 @@ final class Configuration
6971

7072
private const DEFAULT_OPTIONS = [
7173
self::OPTION_REGION => self::DEFAULT_REGION,
74+
self::OPTION_DEBUG => 'false',
7275
self::OPTION_PROFILE => 'default',
7376
self::OPTION_SHARED_CREDENTIALS_FILE => '~/.aws/credentials',
7477
self::OPTION_SHARED_CONFIG_FILE => '~/.aws/config',

src/Response.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,17 @@ class Response
6868
*/
6969
private $logger;
7070

71-
public function __construct(ResponseInterface $response, HttpClientInterface $httpClient, LoggerInterface $logger)
71+
/**
72+
* @var bool
73+
*/
74+
private $debug;
75+
76+
public function __construct(ResponseInterface $response, HttpClientInterface $httpClient, LoggerInterface $logger, bool $debug = false)
7277
{
7378
$this->httpResponse = $response;
7479
$this->httpClient = $httpClient;
7580
$this->logger = $logger;
81+
$this->debug = $debug;
7682
}
7783

7884
public function __destruct()
@@ -114,6 +120,21 @@ public function resolve(?float $timeout = null): bool
114120
$this->resolveResult = new NetworkException('Could not contact remote server.', 0, $e);
115121
}
116122

123+
if (true === $this->debug) {
124+
$httpStatusCode = $this->httpResponse->getInfo('http_code');
125+
if (0 === $httpStatusCode) {
126+
// Network exception
127+
$this->logger->debug('AsyncAws HTTP request could not be sent due network issues');
128+
} else {
129+
$this->logger->debug('AsyncAws HTTP response received with status code {status_code}', [
130+
'status_code' => $httpStatusCode,
131+
'headers' => json_encode($this->httpResponse->getHeaders(false)),
132+
'body' => $this->httpResponse->getContent(false),
133+
]);
134+
$this->bodyDownloaded = true;
135+
}
136+
}
137+
117138
return $this->getResolveStatus();
118139
}
119140

0 commit comments

Comments
 (0)