Skip to content

Commit

Permalink
fix: do not use multipart stream when there are no native query param…
Browse files Browse the repository at this point in the history
…s present vol.2 (#252)
  • Loading branch information
simPod authored Jan 19, 2024
1 parent c4d8106 commit 8ca155f
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions src/Client/Http/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,41 +73,46 @@ 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);
if ($matches === []) {
if ($matches[0] === []) {
$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),
];
}

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

$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),
];
}

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

return $request;
}
}

0 comments on commit 8ca155f

Please sign in to comment.