Skip to content

Commit c64d640

Browse files
committed
add hooks to enable logging of response/request
1 parent b8bdd22 commit c64d640

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/Service/DomainrobotService.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Domainrobot\Lib\DomainrobotResult;
1313
use Domainrobot\Lib\DomainrobotPromise;
1414
use Domainrobot\Model\Certificate;
15+
use Illuminate\Support\Facades\Log;
1516
use Psr\Http\Message\ResponseInterface;
1617

1718
class DomainrobotService
@@ -28,6 +29,9 @@ class DomainrobotService
2829
*/
2930
protected $guzzleClientConfig;
3031

32+
private $logRequestCallback = null;
33+
private $logReesponseCallback = null;
34+
3135
public function __construct(DomainrobotConfig $domainrobotConfig)
3236
{
3337
$this->domainRobotConfig = $domainrobotConfig;
@@ -36,7 +40,7 @@ public function __construct(DomainrobotConfig $domainrobotConfig)
3640
$handle = fopen(__DIR__."/../../composer.json", "r");
3741
$contents = fread($handle, filesize(__DIR__."/../../composer.json"));
3842
fclose($handle);
39-
preg_match("/version.+(\d+\.\d+\.\d+)/",$contents,$matches);
43+
preg_match("/version.+(\d+\.\d+\.\d+)/", $contents, $matches);
4044

4145
$this->guzzleClientConfig = [
4246
'headers' => [
@@ -51,6 +55,18 @@ public function __construct(DomainrobotConfig $domainrobotConfig)
5155
];
5256
}
5357

58+
public function logRequest($callback)
59+
{
60+
$this->logRequestCallback = $callback;
61+
return $this;
62+
}
63+
64+
public function logResponse($callback)
65+
{
66+
$this->logResponseCallback = $callback;
67+
return $this;
68+
}
69+
5470
public function addHeaders($headers = [])
5571
{
5672
$this->guzzleClientConfig['headers'] = array_unique(array_merge($this->guzzleClientConfig['headers'], $headers));
@@ -71,6 +87,16 @@ public function sendRequest($url, $method, $options = [])
7187
{
7288
$guzzleClient = new Client($this->guzzleClientConfig);
7389

90+
if ($this->logRequestCallback!==null) {
91+
$this->logRequestCallback->call(
92+
$this,
93+
$method,
94+
$url,
95+
$options
96+
);
97+
}
98+
$startTime = microtime(true);
99+
74100
$promise = $guzzleClient->requestAsync(
75101
$method,
76102
$url,
@@ -80,10 +106,20 @@ public function sendRequest($url, $method, $options = [])
80106
*
81107
* @return DomainrobotException
82108
*/
83-
function (ResponseInterface $response) {
109+
function (ResponseInterface $response) use ($url, $startTime) {
84110
$rawResponse = $response->getBody()->getContents();
85111
$decodedResponse = json_decode($rawResponse, true);
86112

113+
if ($this->logResponseCallback!==null) {
114+
$this->logResponseCallback->call(
115+
$this,
116+
$url,
117+
$rawResponse,
118+
$response->getStatusCode(),
119+
microtime(true) - $startTime
120+
);
121+
}
122+
87123
return new DomainrobotResult($decodedResponse, $response->getStatusCode());
88124
},
89125
/**

0 commit comments

Comments
 (0)