Skip to content

Commit fa7f750

Browse files
committed
curl cmd
1 parent 6d4bb09 commit fa7f750

3 files changed

Lines changed: 86 additions & 18 deletions

File tree

packages/http/src/Exception/HttpRequestException.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace Windwalker\Http\Exception;
66

77
use Psr\Http\Message\ResponseInterface;
8+
use Psr\Http\Message\StreamInterface;
89
use UnexpectedValueException;
9-
use Windwalker\Database\Driver\StatementInterface;
1010

1111
/**
1212
* The HttpRequestException class.
@@ -15,17 +15,46 @@
1515
*/
1616
class HttpRequestException extends UnexpectedValueException
1717
{
18-
public protected(set) ?ResponseInterface $response;
18+
public protected(set) ?ResponseInterface $response = null;
1919

20-
public StatementInterface|null $body {
20+
public StreamInterface|null $body {
2121
get => $this->response?->getBody();
2222
}
2323

24+
protected \Closure $curlCmdCallback;
25+
2426
public function withResponse(ResponseInterface $response): static
2527
{
2628
$new = new static($this->getMessage(), $this->getCode(), $this);
2729
$new->response = $response;
2830

2931
return $new;
3032
}
33+
34+
public function setCurlCmdCallback(\Closure $callback): static
35+
{
36+
$this->curlCmdCallback = $callback;
37+
38+
return $this;
39+
}
40+
41+
public function getBodyString(): string
42+
{
43+
$body = $this->body;
44+
45+
if ($body === null) {
46+
return '';
47+
}
48+
49+
if ($body->isSeekable()) {
50+
$body->rewind();
51+
}
52+
53+
return $body->getContents();
54+
}
55+
56+
public function toCurlCmd(): string
57+
{
58+
return ($this->curlCmdCallback)();
59+
}
3160
}

packages/http/src/HttpClient.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Windwalker\Event\EventAwareTrait;
1313
use Windwalker\Http\Event\HttpClient\AfterRequestEvent;
1414
use Windwalker\Http\Event\HttpClient\BeforeRequestEvent;
15+
use Windwalker\Http\Exception\HttpRequestException;
1516
use Windwalker\Http\File\HttpUploadFile;
1617
use Windwalker\Http\File\HttpUploadStream;
1718
use Windwalker\Http\File\HttpUploadStringFile;
@@ -133,10 +134,6 @@ public function download(
133134
return HttpClientResponse::from($transport->download($request, $dest));
134135
}
135136

136-
public function downloadStream()
137-
{
138-
}
139-
140137
/**
141138
* Send a request to remote.
142139
*
@@ -474,19 +471,25 @@ public function request(
474471

475472
$transportOptions->optionMerged = true;
476473

477-
$response = $this->sendRequest($request, $transportOptions);
474+
try {
475+
$response = $this->sendRequest($request, $transportOptions);
478476

479-
$httpClient = $this;
477+
$httpClient = $this;
480478

481-
$event = $this->emit(
482-
new AfterRequestEvent(
483-
httpClient: $httpClient,
484-
request: $request,
485-
response: $response
486-
),
487-
);
479+
$event = $this->emit(
480+
new AfterRequestEvent(
481+
httpClient: $httpClient,
482+
request: $request,
483+
response: $response
484+
),
485+
);
488486

489-
return HttpClientResponse::from($event->response);
487+
return HttpClientResponse::fromHttpClient($request, $event->response, fn () => $this->toCurlCmd($request));
488+
} catch (HttpRequestException $e) {
489+
$e->setCurlCmdCallback(fn () => $this->toCurlCmd($request));
490+
491+
throw $e;
492+
}
490493
}
491494

492495
public function hydrateRequest(

packages/http/src/Response/HttpClientResponse.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
namespace Windwalker\Http\Response;
66

7+
use Psr\Http\Message\RequestInterface;
8+
use Psr\Http\Message\ResponseInterface;
9+
use Windwalker\Http\HttpClient;
10+
711
/**
812
* The HttpClientResponse class.
913
*/
@@ -14,7 +18,23 @@ class HttpClientResponse extends Response
1418
*
1519
* @var mixed
1620
*/
17-
protected mixed $info = null;
21+
public protected(set) mixed $info = null;
22+
23+
public protected(set) RequestInterface $request;
24+
25+
protected \Closure $curlCmdCallback;
26+
27+
public static function fromHttpClient(
28+
RequestInterface $request,
29+
ResponseInterface $response,
30+
\Closure $curlCmdCallback,
31+
): static {
32+
$res = static::from($response);
33+
$res->request = $request;
34+
$res->curlCmdCallback = $curlCmdCallback;
35+
36+
return $res;
37+
}
1838

1939
public function withInfo(mixed $info): static
2040
{
@@ -28,4 +48,20 @@ public function getInfo(): mixed
2848
{
2949
return $this->info;
3050
}
51+
52+
public function getBodyString(): string
53+
{
54+
$body = $this->getBody();
55+
56+
if ($body->isSeekable()) {
57+
$body->rewind();
58+
}
59+
60+
return $body->getContents();
61+
}
62+
63+
public function toCurlCmd(): string
64+
{
65+
return ($this->curlCmdCallback)();
66+
}
3167
}

0 commit comments

Comments
 (0)