Skip to content

Commit

Permalink
feat: undeprecate ValueFormatter (#250)
Browse files Browse the repository at this point in the history
- do not use multipart stream when there are no native query params present
  • Loading branch information
simPod authored Jan 18, 2024
1 parent 52ebb6d commit 2b104bf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
62 changes: 33 additions & 29 deletions src/Client/Http/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use InvalidArgumentException;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UriInterface;
use SimPod\ClickHouseClient\Exception\UnsupportedParamType;
Expand All @@ -31,6 +32,7 @@ final class RequestFactory
public function __construct(
private ParamValueConverterRegistry $paramValueConverterRegistry,
private RequestFactoryInterface $requestFactory,
private StreamFactoryInterface $streamFactory,
UriFactoryInterface|null $uriFactory = null,
UriInterface|string $uri = '',
) {
Expand Down Expand Up @@ -71,37 +73,39 @@ public function prepareRequest(RequestOptions $requestOptions): RequestInterface
$request = $this->requestFactory->createRequest('POST', $uri);

preg_match_all('~\{([a-zA-Z\d]+):([a-zA-Z\d ]+(\(.+\))?)}~', $requestOptions->sql, $matches);

$typeToParam = array_reduce(
array_keys($matches[1]),
static function (array $acc, string|int $k) use ($matches) {
$acc[$matches[1][$k]] = Type::fromString($matches[2][$k]);

return $acc;
},
[],
);

$streamElements = [['name' => 'query', 'contents' => $requestOptions->sql]];
foreach ($requestOptions->params as $name => $value) {
$type = $typeToParam[$name] ?? null;
if ($type === null) {
continue;
if ($matches === []) {
$body = $this->streamFactory->createStream($requestOptions->sql);
} else {
$typeToParam = array_reduce(
array_keys($matches[1]),
static function (array $acc, string|int $k) use ($matches) {
$acc[$matches[1][$k]] = Type::fromString($matches[2][$k]);

return $acc;
},
[],
);

$streamElements = [['name' => 'query', 'contents' => $requestOptions->sql]];
foreach ($requestOptions->params as $name => $value) {
$type = $typeToParam[$name] ?? null;
if ($type === null) {
continue;
}

$streamElements[] = [
'name' => 'param_' . $name,
'contents' => $this->paramValueConverterRegistry->get($type)($value, $type, false),
];
}

$streamElements[] = [
'name' => 'param_' . $name,
'contents' => $this->paramValueConverterRegistry->get($type)($value, $type, false),
];
}

try {
$body = new MultipartStream($streamElements);
$request = $request
->withHeader('Content-Type', 'multipart/form-data; boundary=' . $body->getBoundary())
->withBody($body);
} catch (InvalidArgumentException) {
absurd();
try {
$body = new MultipartStream($streamElements);
$request = $request->withBody($body)
->withHeader('Content-Type', 'multipart/form-data; boundary=' . $body->getBoundary());
} catch (InvalidArgumentException) {
absurd();
}
}

return $request;
Expand Down
1 change: 1 addition & 0 deletions tests/Client/Http/RequestFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function testPrepareRequest(string $uri, string $expectedUri): void
new ParamValueConverterRegistry(),
$psr17Factory,
$psr17Factory,
$psr17Factory,
$uri,
);

Expand Down
3 changes: 3 additions & 0 deletions tests/WithClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ private static function restartClickHouseClient(): void
new ParamValueConverterRegistry(),
new Psr17Factory(),
new Psr17Factory(),
new Psr17Factory(),
),
);

Expand All @@ -105,6 +106,7 @@ private static function restartClickHouseClient(): void
new ParamValueConverterRegistry(),
new Psr17Factory(),
new Psr17Factory(),
new Psr17Factory(),
),
);

Expand All @@ -120,6 +122,7 @@ private static function restartClickHouseClient(): void
new ParamValueConverterRegistry(),
new Psr17Factory(),
new Psr17Factory(),
new Psr17Factory(),
),
);

Expand Down

0 comments on commit 2b104bf

Please sign in to comment.