Skip to content

Commit be59e63

Browse files
committed
Fixes
1 parent 45081ca commit be59e63

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/ApiClient.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ protected function handleStream($curl, $streamData) {
5454
return -1;
5555
}
5656

57+
foreach($this->curlHandlers as $handler) {
58+
$handler("stream", $streamData);
59+
}
60+
5761
foreach(explode("data: ", $streamData) as $bodyParsed) {
5862
if (!$bodyParsed) continue;
5963

src/Client.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,17 @@ public function addResultHandler(callable $handler) : self
282282

283283
return $this;
284284
}
285+
286+
/**
287+
* Adds a handler for curl, first argument holds the type ("request" or "response"), second argument the data.
288+
*
289+
* @param callable $listener
290+
* @return self
291+
*/
292+
public function addCurlHandler(callable $listener) : self
293+
{
294+
$this->driver->addCurlHandler($listener);
295+
296+
return $this;
297+
}
285298
}

src/Traits/WithCurlRequests.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ trait WithCurlRequests
77
public bool $shouldAbort = false;
88
public bool $isBusy = false;
99

10+
public array $curlHandlers = [];
11+
12+
/**
13+
* Adds a new io handler to the client.
14+
*
15+
* @param callable $handler The handler function to add.
16+
*/
17+
public function addCurlHandler(callable $handler)
18+
{
19+
$this->curlHandlers[] = $handler;
20+
}
21+
1022
/**
1123
* Sends a JSON request to a specified URL.
1224
*
@@ -48,9 +60,18 @@ protected function sendJsonRequest(string $method, string $url, array $jsonData,
4860
$curl = curl_init();
4961

5062
curl_setopt_array($curl, $curlOptions);
63+
64+
foreach($this->curlHandlers as $handler) {
65+
$handler("request", $curlOptions);
66+
}
67+
5168
$body = curl_exec($curl);
5269
curl_close($curl);
5370

71+
foreach($this->curlHandlers as $handler) {
72+
$handler("response", $body);
73+
}
74+
5475
try {
5576
$bodyParsed = json_decode($body, true);
5677
} catch (\Throwable $th) {

0 commit comments

Comments
 (0)