Skip to content

Commit 923a65a

Browse files
authored
Support Request Headers (#137)
- rename `ClickHouseClient::selectWithParameters()` to `ClickHouseClient::selectWithParams()` (same for `ClickHouseAsyncClient`) - `$requestQueryParameters` renamed to `$requestQueryParams`
1 parent 6952a02 commit 923a65a

File tree

12 files changed

+194
-96
lines changed

12 files changed

+194
-96
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ jobs:
8080
- "21.1"
8181
- "21.2"
8282
- "21.3"
83-
- "21.4"
83+
# - "21.4" It does not support header auth
8484
- "21.5"
8585
- "21.6"
8686
- "21.7"

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Naming used here is the same as in ClickHouse docs.
2525
- [PSR Factories who?](#psr-factories-who)
2626
- [Sync API](#sync-api)
2727
- [Select](#select)
28-
- [Select With Parameters](#select-with-parameters)
28+
- [Select With Params](#select-with-params)
2929
- [Insert](#insert)
3030
- [Async API](#async-api)
3131
- [Select](#select-1)
@@ -57,10 +57,10 @@ $clickHouseClient = new PsrClickHouseClient(
5757
),
5858
'https://localhost:8123',
5959
[
60-
'database' => 'dbname',
61-
'user' => 'username',
62-
'password' => 'secret',
60+
‘X-ClickHouse-User' => 'username',
61+
'X-ClickHouse-Key' => 'secret',
6362
],
63+
['database' => 'dbname'],
6464
new DateTimeZone('UTC')
6565
);
6666
```
@@ -117,13 +117,14 @@ use SimPod\ClickHouseClient\Output;
117117
$output = $client->select(
118118
'SELECT * FROM table',
119119
new JsonEachRow(),
120+
[],
120121
['force_primary_key' => 1]
121122
);
122123
```
123124

124-
### Select With Parameters
125+
### Select With Params
125126

126-
`ClickHouseClient::selectWithParameters()`
127+
`ClickHouseClient::selectWithParams()`
127128

128129
Same as `ClickHouseClient::select()` except it also allows [parameter binding](#parameters-binding).
129130

@@ -136,10 +137,11 @@ use SimPod\ClickHouseClient\Output;
136137

137138
/** @var ClickHouseClient $client */
138139
/** @var Output\JsonEachRow $output */
139-
$output = $client->selectWithParameters(
140+
$output = $client->selectWithParams(
140141
'SELECT * FROM :table',
141142
['table' => 'table_name'],
142143
new JsonEachRow(),
144+
[],
143145
['force_primary_key' => 1]
144146
);
145147
```

src/Client/ClickHouseAsyncClient.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,36 @@
88
use SimPod\ClickHouseClient\Format\Format;
99
use SimPod\ClickHouseClient\Output\Output;
1010

11+
/** @see Output hack for IDE to preserve `use` */
1112
interface ClickHouseAsyncClient
1213
{
1314
/**
14-
* @see Output hack for IDE to preserve `use`
15-
*
16-
* @param array<string, float|int|string> $requestParameters
15+
* @param array<string, string|array<string>> $requestHeaders
16+
* @param array<string, float|int|string> $requestQueryParams
1717
* @psalm-param Format<O> $outputFormat
1818
*
1919
* @template O of Output
2020
*/
21-
public function select(string $sql, Format $outputFormat, array $requestParameters = []) : PromiseInterface;
21+
public function select(
22+
string $sql,
23+
Format $outputFormat,
24+
array $requestHeaders = [],
25+
array $requestQueryParams = []
26+
) : PromiseInterface;
2227

2328
/**
24-
* @param array<string, float|int|string> $requestParameters
25-
* @param array<string, mixed> $queryParameters
29+
* @param array<string, mixed> $statementParams
30+
* @param array<string, string|array<string>> $requestHeaders
31+
* @param array<string, float|int|string> $requestQueryParams
2632
* @psalm-param Format<O> $outputFormat
2733
*
2834
* @template O of Output
2935
*/
30-
public function selectWithParameters(
31-
string $query,
32-
array $queryParameters,
36+
public function selectWithParams(
37+
string $sql,
38+
array $statementParams,
3339
Format $outputFormat,
34-
array $requestParameters = []
40+
array $requestHeaders = [],
41+
array $requestQueryParams = []
3542
) : PromiseInterface;
3643
}

src/Client/ClickHouseClient.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,56 @@
99

1010
interface ClickHouseClient
1111
{
12-
/** @param array<string, float|int|string> $requestParameters */
13-
public function executeQuery(string $query, array $requestParameters = []) : void;
12+
/**
13+
* @param array<string, string|array<string>> $requestHeaders
14+
* @param array<string, float|int|string> $requestQueryParams
15+
*/
16+
public function executeQuery(string $query, array $requestHeaders = [], array $requestQueryParams = []) : void;
1417

1518
/**
16-
* @param array<string, mixed> $queryParameters
17-
* @param array<string, float|int|string> $requestParameters
19+
* @param array<string, mixed> $statementParams
20+
* @param array<string, string|array<string>> $requestHeaders
21+
* @param array<string, float|int|string> $requestQueryParams
1822
*/
19-
public function executeQueryWithParameters(string $query, array $queryParameters, array $requestParameters = []) : void;
23+
public function executeQueryWithParameters(
24+
string $query,
25+
array $statementParams,
26+
array $requestHeaders = [],
27+
array $requestQueryParams = []
28+
) : void;
2029

2130
/**
22-
* @param array<string, float|int|string> $requestParameters
31+
* @param array<string, string|array<string>> $requestHeaders
32+
* @param array<string, float|int|string> $requestQueryParams
2333
* @psalm-param Format<O> $outputFormat
2434
*
2535
* @psalm-return O
2636
*
2737
* @template O of Output
2838
*/
29-
public function select(string $query, Format $outputFormat, array $requestParameters = []) : Output;
39+
public function select(
40+
string $query,
41+
Format $outputFormat,
42+
array $requestHeaders = [],
43+
array $requestQueryParams = []
44+
) : Output;
3045

3146
/**
32-
* @param array<string, float|int|string> $requestParameters
33-
* @param array<string, mixed> $queryParameters
47+
* @param array<string, string|array<string>> $requestHeaders
48+
* @param array<string, float|int|string> $requestQueryParams
49+
* @param array<string, mixed> $statementParams
3450
* @psalm-param Format<O> $outputFormat
3551
*
3652
* @psalm-return O
3753
*
3854
* @template O of Output
3955
*/
40-
public function selectWithParameters(
56+
public function selectWithParams(
4157
string $query,
42-
array $queryParameters,
58+
array $statementParams,
4359
Format $outputFormat,
44-
array $requestParameters = []
60+
array $requestHeaders = [],
61+
array $requestQueryParams = []
4562
) : Output;
4663

4764
/**

src/Client/Http/RequestFactory.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,20 @@ public function prepareRequest(string $endpoint, RequestOptions $requestOptions)
3636
$uri = $this->uriFactory->createUri($endpoint);
3737
$uri = $uri->withQuery(
3838
http_build_query(
39-
$requestOptions->parameters,
39+
$requestOptions->queryParams,
4040
'',
4141
'&',
4242
PHP_QUERY_RFC3986
4343
)
4444
);
4545

46-
$body = $this->streamFactory->createStream($requestOptions->sql);
47-
4846
$request = $this->requestFactory->createRequest('POST', $uri);
4947

48+
foreach ($requestOptions->headers as $name => $value) {
49+
$request = $request->withHeader($name, $value);
50+
}
51+
52+
$body = $this->streamFactory->createStream($requestOptions->sql);
5053
$request = $request->withBody($body);
5154

5255
return $request;

src/Client/Http/RequestOptions.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,27 @@ final class RequestOptions
88
{
99
public string $sql;
1010

11+
/** @var array<string, string|array<string>> */
12+
public array $headers;
13+
1114
/** @var array<string, float|int|string> */
12-
public array $parameters;
15+
public array $queryParams;
1316

1417
/**
15-
* @param array<string, float|int|string> $defaultParameters
16-
* @param array<string, float|int|string> $requestParameters
18+
* @param array<string, string|array<string>> $defaultHeaders
19+
* @param array<string, string|array<string>> $requestHeaders
20+
* @param array<string, float|int|string> $defaultQueryParams
21+
* @param array<string, float|int|string> $requestParams
1722
*/
18-
public function __construct(string $sql, array $defaultParameters, array $requestParameters)
19-
{
20-
$this->sql = $sql;
21-
$this->parameters = $defaultParameters + $requestParameters;
23+
public function __construct(
24+
string $sql,
25+
array $defaultHeaders,
26+
array $requestHeaders,
27+
array $defaultQueryParams,
28+
array $requestParams
29+
) {
30+
$this->sql = $sql;
31+
$this->headers = $defaultHeaders + $requestHeaders;
32+
$this->queryParams = $defaultQueryParams + $requestParams;
2233
}
2334
}

src/Client/PsrClickHouseAsyncClient.php

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,51 @@ class PsrClickHouseAsyncClient implements ClickHouseAsyncClient
2525

2626
private string $endpoint;
2727

28+
/** @var array<string, string|array<string>> */
29+
private array $defaultHeaders;
30+
2831
/** @var array<string, float|int|string> */
29-
private array $defaultParameters;
32+
private array $defaultQueryParams;
3033

3134
private SqlFactory $sqlFactory;
3235

33-
/** @param array<string, float|int|string> $defaultParameters */
36+
/**
37+
* @param array<string, string|array<string>> $defaultHeaders
38+
* @param array<string, float|int|string> $defaultQueryParams
39+
*/
3440
public function __construct(
3541
HttpAsyncClient $asyncClient,
3642
RequestFactory $requestFactory,
3743
string $endpoint,
38-
array $defaultParameters = [],
44+
array $defaultHeaders = [],
45+
array $defaultQueryParams = [],
3946
?DateTimeZone $clickHouseTimeZone = null
4047
) {
41-
$this->asyncClient = $asyncClient;
42-
$this->requestFactory = $requestFactory;
43-
$this->endpoint = $endpoint;
44-
$this->defaultParameters = $defaultParameters;
45-
$this->sqlFactory = new SqlFactory(new ValueFormatter($clickHouseTimeZone));
48+
$this->asyncClient = $asyncClient;
49+
$this->requestFactory = $requestFactory;
50+
$this->endpoint = $endpoint;
51+
$this->defaultHeaders = $defaultHeaders;
52+
$this->defaultQueryParams = $defaultQueryParams;
53+
$this->sqlFactory = new SqlFactory(new ValueFormatter($clickHouseTimeZone));
4654
}
4755

4856
/**
4957
* {@inheritDoc}
5058
*/
51-
public function select(string $sql, Format $outputFormat, array $requestParameters = []) : PromiseInterface
52-
{
59+
public function select(
60+
string $sql,
61+
Format $outputFormat,
62+
array $requestHeaders = [],
63+
array $requestParameters = []
64+
) : PromiseInterface {
5365
$formatClause = $outputFormat::toSql();
5466

5567
return $this->executeRequest(
5668
<<<CLICKHOUSE
5769
$sql
5870
$formatClause
5971
CLICKHOUSE,
72+
$requestHeaders,
6073
$requestParameters,
6174
static function (ResponseInterface $response) use ($outputFormat) : Output {
6275
return $outputFormat::output((string) $response->getBody());
@@ -67,30 +80,40 @@ static function (ResponseInterface $response) use ($outputFormat) : Output {
6780
/**
6881
* {@inheritDoc}
6982
*/
70-
public function selectWithParameters(string $query, array $queryParameters, Format $outputFormat, array $requestParameters = []) : PromiseInterface
71-
{
83+
public function selectWithParams(
84+
string $sql,
85+
array $statementParams,
86+
Format $outputFormat,
87+
array $requestHeaders = [],
88+
array $requestQueryParams = []
89+
) : PromiseInterface {
7290
return $this->select(
73-
$this->sqlFactory->createWithParameters($query, $queryParameters),
91+
$this->sqlFactory->createWithParameters($sql, $statementParams),
7492
$outputFormat,
75-
$requestParameters
93+
$requestHeaders,
94+
$requestQueryParams
7695
);
7796
}
7897

7998
/**
80-
* @param array<string, float|int|string> $requestParameters
81-
* @param callable(ResponseInterface):mixed|null $processResponse
99+
* @param array<string, string|array<string>> $requestHeaders
100+
* @param array<string, float|int|string> $requestQueryParams
101+
* @param (callable(ResponseInterface):mixed)|null $processResponse
82102
*/
83103
private function executeRequest(
84104
string $sql,
85-
array $requestParameters = [],
105+
array $requestHeaders = [],
106+
array $requestQueryParams = [],
86107
?callable $processResponse = null
87108
) : PromiseInterface {
88109
$request = $this->requestFactory->prepareRequest(
89110
$this->endpoint,
90111
new RequestOptions(
91112
$sql,
92-
$this->defaultParameters,
93-
$requestParameters
113+
$this->defaultHeaders,
114+
$requestHeaders,
115+
$this->defaultQueryParams,
116+
$requestQueryParams
94117
)
95118
);
96119

0 commit comments

Comments
 (0)