Skip to content

Commit 54326fc

Browse files
authored
feat: use multipart stream for request body (#242)
1 parent b03bfb6 commit 54326fc

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"require": {
3030
"php": "^8.2",
3131
"guzzlehttp/promises": "^2.0",
32+
"guzzlehttp/psr7": "^2.6",
3233
"php-http/client-common": "^2.0",
3334
"psr/http-client": "^1.0",
3435
"psr/http-factory": "^1.0",

src/Client/Http/RequestFactory.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace SimPod\ClickHouseClient\Client\Http;
66

7+
use GuzzleHttp\Psr7\MultipartStream;
78
use InvalidArgumentException;
89
use Psr\Http\Message\RequestFactoryInterface;
910
use Psr\Http\Message\RequestInterface;
10-
use Psr\Http\Message\StreamFactoryInterface;
1111
use Psr\Http\Message\UriFactoryInterface;
1212
use Psr\Http\Message\UriInterface;
1313

@@ -24,7 +24,6 @@ final class RequestFactory
2424
/** @throws InvalidArgumentException */
2525
public function __construct(
2626
private RequestFactoryInterface $requestFactory,
27-
private StreamFactoryInterface $streamFactory,
2827
UriFactoryInterface|null $uriFactory = null,
2928
UriInterface|string $uri = '',
3029
) {
@@ -50,8 +49,6 @@ public function prepareRequest(RequestOptions $requestOptions): RequestInterface
5049
PHP_QUERY_RFC3986,
5150
);
5251

53-
$body = $this->streamFactory->createStream($requestOptions->sql);
54-
5552
if ($this->uri === null) {
5653
$uri = $query === '' ? '' : '?' . $query;
5754
} else {
@@ -64,8 +61,14 @@ public function prepareRequest(RequestOptions $requestOptions): RequestInterface
6461
}
6562

6663
$request = $this->requestFactory->createRequest('POST', $uri);
64+
65+
$streamElements = [['name' => 'query', 'contents' => $requestOptions->sql]];
66+
6767
try {
68-
$request = $request->withBody($body);
68+
$body = new MultipartStream($streamElements);
69+
$request = $request
70+
->withHeader('Content-Type', 'multipart/form-data; boundary=' . $body->getBoundary())
71+
->withBody($body);
6972
} catch (InvalidArgumentException) {
7073
absurd();
7174
}

tests/Client/Http/RequestFactoryTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public function testPrepareRequest(string $uri, string $expectedUri): void
2020
{
2121
$psr17Factory = new Psr17Factory();
2222
$requestFactory = new RequestFactory(
23-
$psr17Factory,
2423
$psr17Factory,
2524
$psr17Factory,
2625
$uri,
@@ -37,7 +36,7 @@ public function testPrepareRequest(string $uri, string $expectedUri): void
3736
$expectedUri,
3837
$request->getUri()->__toString(),
3938
);
40-
self::assertSame('SELECT 1', $request->getBody()->__toString());
39+
self::assertStringContainsString('SELECT 1', $request->getBody()->__toString());
4140
}
4241

4342
/** @return Generator<string, array{string, string}> */

0 commit comments

Comments
 (0)