Skip to content

Commit

Permalink
Added withDebug option to the client builder.
Browse files Browse the repository at this point in the history
  • Loading branch information
MouaYing committed Jul 30, 2018
1 parent f9f45ab commit 297cb17
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ class ClientBuilder {
$maxTimeout,
$urlPrefix,
$proxy,
$logger;
$logger,
$debugMode;

public function __construct(Credentials $signer = null) {
$this->serializer = new NativeSerializer();
$this->maxRetries = 5;
$this->maxTimeout = 10000;
$this->signer = $signer;
$this->logger = new MyLogger();
$this->debugMode = false;
}

/**
Expand Down Expand Up @@ -129,6 +131,15 @@ public function withDefaultLogger(Logger $logger) {
return $this;
}

/**
* Enables debug mode, which will print information about the HTTP request and response to STDERR
* @return $this Returns <b>this</b> to accommodate method chaining.
*/
public function withDebug() {
$this->debugMode = true;
return $this;
}

public function buildUSAutocompleteApiClient() {
$this->ensureURLPrefixNotNull(self::US_AUTOCOMPLETE_API_URL);
return new USAutoCompleteApiClient($this->buildSender(), $this->serializer);
Expand Down Expand Up @@ -158,7 +169,7 @@ private function buildSender() {
if ($this->httpSender != null)
return $this->httpSender;

$sender = new NativeSender($this->maxTimeout, $this->proxy);
$sender = new NativeSender($this->maxTimeout, $this->proxy, $this->debugMode);

$sender = new StatusCodeSender($sender);

Expand Down
20 changes: 18 additions & 2 deletions src/NativeSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,23 @@

class NativeSender implements Sender {
private $maxTimeOut,
$proxy;
$proxy,
$debugMode;

public function __construct($maxTimeOut = 10000, Proxy $proxy = null) {
public function __construct($maxTimeOut = 10000, Proxy $proxy = null, $debugMode = false) {
$this->maxTimeOut = $maxTimeOut;
$this->proxy = $proxy;
$this->debugMode = $debugMode;
}

function send(Request $smartyRequest) {
$ch = $this->buildRequest($smartyRequest);
$this->setHeaders($smartyRequest, $ch);
$payload = curl_exec($ch);

if ($this->debugMode)
$this->printDebugInfo($ch, $smartyRequest, $payload);

$connectCode = curl_getinfo($ch, CURLINFO_HTTP_CONNECTCODE);
if ($payload === FALSE && $connectCode != 200) {
$errorMessage = curl_error($ch);
Expand All @@ -44,6 +49,7 @@ private function buildRequest(Request $smartyRequest) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->setHeaders($smartyRequest));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->maxTimeOut);
curl_setopt($ch, CURLOPT_USERAGENT, 'smartystreets (sdk:php@' . VERSION . ')');
curl_setopt($ch, CURLINFO_HEADER_OUT, STDERR);

if ($this->proxy != null)
$this->setProxy($ch);
Expand Down Expand Up @@ -72,4 +78,14 @@ private function setHeaders(Request $smartyRequest) {

return $headers;
}

private function printDebugInfo($ch, Request $smartyRequest, $responsePayload) {
fwrite(STDERR, "*****Request*****\r\n" . curl_getinfo($ch, CURLINFO_HEADER_OUT));
if ($smartyRequest->getPayload() != null)
fwrite(STDERR, "Data: " . $smartyRequest->getPayload());

fwrite(STDERR, "\r\n\r\n*****Response*****");
fwrite(STDERR, "\r\nStatus: " . curl_getinfo($ch, CURLINFO_HTTP_CODE));
fwrite(STDERR, "\r\nResponse body: " . $responsePayload);
}
}

0 comments on commit 297cb17

Please sign in to comment.