Skip to content

Commit bd0fb26

Browse files
authored
feat: allow passing uri to RequestFactory (#191)
1 parent 732435d commit bd0fb26

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/Client/Http/RequestFactory.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
use Psr\Http\Message\RequestFactoryInterface;
88
use Psr\Http\Message\RequestInterface;
99
use Psr\Http\Message\StreamFactoryInterface;
10+
use Psr\Http\Message\UriFactoryInterface;
11+
use Psr\Http\Message\UriInterface;
1012

1113
use function http_build_query;
14+
use function is_string;
1215

1316
use const PHP_QUERY_RFC3986;
1417

@@ -17,6 +20,8 @@ final class RequestFactory
1720
public function __construct(
1821
private RequestFactoryInterface $requestFactory,
1922
private StreamFactoryInterface $streamFactory,
23+
private UriFactoryInterface|null $uriFactory = null,
24+
private UriInterface|string $uri = '',
2025
) {
2126
}
2227

@@ -29,7 +34,20 @@ public function prepareRequest(RequestOptions $requestOptions): RequestInterface
2934
PHP_QUERY_RFC3986,
3035
);
3136

32-
return $this->requestFactory->createRequest('POST', $query === '' ? '' : '?' . $query)
37+
if ($this->uriFactory === null) {
38+
return $this->requestFactory->createRequest('POST', $query === '' ? '' : '?' . $query)
39+
->withBody($this->streamFactory->createStream($requestOptions->sql));
40+
}
41+
42+
$uri = $this->uri;
43+
if (is_string($uri)) {
44+
$uri = $this->uriFactory->createUri($uri);
45+
}
46+
47+
$uriQuery = $uri->getQuery();
48+
$uri = $uri->withQuery($uriQuery . ($uriQuery !== '' && $query !== '' ? '&' : '') . $query);
49+
50+
return $this->requestFactory->createRequest('POST', $uri)
3351
->withBody($this->streamFactory->createStream($requestOptions->sql));
3452
}
3553
}

tests/Client/Http/RequestFactoryTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ final class RequestFactoryTest extends TestCaseBase
1515
public function testPrepareRequest(): void
1616
{
1717
$psr17Factory = new Psr17Factory();
18-
$requestFactory = new RequestFactory($psr17Factory, $psr17Factory);
18+
$requestFactory = new RequestFactory(
19+
$psr17Factory,
20+
$psr17Factory,
21+
$psr17Factory,
22+
'http://localhost:8123?format=JSON',
23+
);
1924

2025
$request = $requestFactory->prepareRequest(new RequestOptions(
2126
'SELECT 1',
@@ -24,7 +29,10 @@ public function testPrepareRequest(): void
2429
));
2530

2631
self::assertSame('POST', $request->getMethod());
27-
self::assertSame('?database=database&max_block_size=1', $request->getUri()->__toString());
32+
self::assertSame(
33+
'http://localhost:8123?format=JSON&database=database&max_block_size=1',
34+
$request->getUri()->__toString(),
35+
);
2836
self::assertSame('SELECT 1', $request->getBody()->__toString());
2937
}
3038
}

0 commit comments

Comments
 (0)