Skip to content

Commit

Permalink
feat: pass parameters natively via http interface along the query
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Jan 12, 2024
1 parent 3b62345 commit 8252715
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"require": {
"php": "^8.2",
"guzzlehttp/promises": "^2.0",
"guzzlehttp/psr7": "^2.6",
"php-http/client-common": "^2.0",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0",
Expand Down
9 changes: 8 additions & 1 deletion src/Client/Http/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SimPod\ClickHouseClient\Client\Http;

use GuzzleHttp\Psr7\MultipartStream;
use InvalidArgumentException;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
Expand Down Expand Up @@ -50,7 +51,11 @@ public function prepareRequest(RequestOptions $requestOptions): RequestInterface
PHP_QUERY_RFC3986,
);

$body = $this->streamFactory->createStream($requestOptions->sql);
// $body = $this->streamFactory->createStream($requestOptions->sql);
// $body = $this->streamFactory->createStream(http_build_query(['query'=>$requestOptions->sql]));
// $body = $this->streamFactory->createStream(http_build_query(['query'=>'select 1', 'param_p1'=>'value_p1']));
$body = new MultipartStream([[ 'name' => 'query', 'contents' => $requestOptions->sql ]]);

Check failure on line 57 in src/Client/Http/RequestFactory.php

View workflow job for this annotation

GitHub Actions / Coding Standards (8.2)

Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

Check failure on line 57 in src/Client/Http/RequestFactory.php

View workflow job for this annotation

GitHub Actions / Coding Standards (8.2)

Expected 0 spaces after array opening bracket, 1 found.

Check failure on line 57 in src/Client/Http/RequestFactory.php

View workflow job for this annotation

GitHub Actions / Coding Standards (8.2)

Expected 0 spaces before array closing bracket, 1 found.

Check failure on line 57 in src/Client/Http/RequestFactory.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

Method SimPod\ClickHouseClient\Client\Http\RequestFactory::prepareRequest() throws checked exception InvalidArgumentException but it's missing from the PHPDoc @throws tag.
$boundary = $body->getBoundary();

if ($this->uri === null) {
$uri = $query === '' ? '' : '?' . $query;
Expand All @@ -64,6 +69,8 @@ public function prepareRequest(RequestOptions $requestOptions): RequestInterface
}

$request = $this->requestFactory->createRequest('POST', $uri);
$request = $request->withHeader('Content-Type', 'multipart/form-data; boundary='.$boundary);

Check failure on line 72 in src/Client/Http/RequestFactory.php

View workflow job for this annotation

GitHub Actions / Coding Standards (8.2)

Expected at least 1 space before "."; 0 found

Check failure on line 72 in src/Client/Http/RequestFactory.php

View workflow job for this annotation

GitHub Actions / Coding Standards (8.2)

Expected at least 1 space after "."; 0 found

Check failure on line 72 in src/Client/Http/RequestFactory.php

View workflow job for this annotation

GitHub Actions / Coding Standards (8.2)

Concat operator must be surrounded by a single space

Check warning on line 72 in src/Client/Http/RequestFactory.php

View workflow job for this annotation

GitHub Actions / Infection

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ } } $request = $this->requestFactory->createRequest('POST', $uri); - $request = $request->withHeader('Content-Type', 'multipart/form-data; boundary=' . $boundary); + $request = $request->withHeader('Content-Type', 'multipart/form-data; boundary='); // $request = $request->withHeader('Content-Type', 'multipart/form-data; boundary=' . $requestOptions->boundary); try { $request = $request->withBody($body);

Check failure on line 72 in src/Client/Http/RequestFactory.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

Method SimPod\ClickHouseClient\Client\Http\RequestFactory::prepareRequest() throws checked exception InvalidArgumentException but it's missing from the PHPDoc @throws tag.
// $request = $request->withHeader('Content-Type', 'multipart/form-data; boundary=' . $requestOptions->boundary);
try {
$request = $request->withBody($body);
} catch (InvalidArgumentException) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Client/Http/RequestFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testPrepareRequest(string $uri, string $expectedUri): void
$expectedUri,
$request->getUri()->__toString(),
);
self::assertSame('SELECT 1', $request->getBody()->__toString());
self::assertStringContainsString('SELECT 1', $request->getBody()->__toString());
}

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

0 comments on commit 8252715

Please sign in to comment.