Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit fa092ea

Browse files
authored
Merge pull request #4 from elastic/fix/serverless-api-version
Added Elastic-Api-Version
2 parents ee924b5 + 805c07e commit fa092ea

File tree

4 files changed

+19
-57
lines changed

4 files changed

+19
-57
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ You can install the library using [composer](https://getcomposer.org/) with the
1313
composer require elastic/elasticsearch-serverless
1414
```
1515

16-
You need specify the version, if you want to install an `aplha` or `rc` release, as follows:
16+
You need specify the version, if you want to install an `alpha` or `rc` release.
17+
For instance, if you want to install the latest alpha version:
1718

1819
```bash
19-
composer require elastic/elasticsearch-serverless:0.1.0-alpha1
20+
composer require elastic/elasticsearch-serverless:*@alpha
2021
```
2122

2223
Please remember that `alpha` releases are quite unstable, the code can change between releases.
23-
Instead, a release candidate `rc` will not break the backward compatibility but with the possibility
24-
of having bugs to be fixed.
24+
Instead, a release candidate `rc` will not break the backward compatibility. Typically an `rc`
25+
version includes only bug fixes.
2526

2627
### Instantiate a Client
2728

src/Client.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
final class Client implements ClientInterface
2929
{
3030
const CLIENT_NAME = 'esv';
31-
const VERSION = '0.1.0-alpha1';
32-
const API_VERSION = '20231031';
33-
const API_COMPATIBILITY_HEADER = '%s/vnd.elasticsearch+%s; compatible-with=8';
34-
31+
const VERSION = '0.1.0-alpha2';
32+
const API_VERSION_HEADER = 'Elastic-Api-Version';
33+
const API_VERSION = '2023-10-31';
34+
3535
use ClientEndpointsTrait;
3636
use EndpointTrait;
3737
use NamespaceTrait;

src/Traits/EndpointTrait.php

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ protected function createRequest(string $method, string $url, array $headers, $b
145145
$content = is_string($body) ? $body : $this->bodySerialize($body, $headers['Content-Type']);
146146
$request = $request->withBody($streamFactory->createStream($content));
147147
}
148-
$headers = $this->buildCompatibilityHeaders($headers);
148+
$headers = $this->buildApiVersion($headers);
149149

150150
// Headers
151151
foreach ($headers as $name => $value) {
@@ -159,30 +159,14 @@ protected function createRequest(string $method, string $url, array $headers, $b
159159
}
160160

161161
/**
162-
* Build the API compatibility headers
163-
* transfrom Content-Type and Accept adding vnd.elasticsearch+ and compatible-with
164-
*
165-
* @see https://github.com/elastic/elasticsearch-serverless-php/pull/1142
162+
* Build the API version header
166163
*/
167-
protected function buildCompatibilityHeaders(array $headers): array
164+
protected function buildApiVersion(array $headers): array
168165
{
169-
if (isset($headers['Content-Type'])) {
170-
if (preg_match('/application\/([^,]+)$/', $headers['Content-Type'], $matches)) {
171-
$headers['Content-Type'] = sprintf(Client::API_COMPATIBILITY_HEADER, 'application', $matches[1]);
172-
}
173-
}
174-
if (isset($headers['Accept'])) {
175-
$values = explode(',', $headers['Accept']);
176-
foreach ($values as &$value) {
177-
if (preg_match('/(application|text)\/([^,]+)/', $value, $matches)) {
178-
$value = sprintf(Client::API_COMPATIBILITY_HEADER, $matches[1], $matches[2]);
179-
}
180-
}
181-
$headers['Accept'] = implode(',', $values);
182-
}
166+
$headers[Client::API_VERSION_HEADER] = Client::API_VERSION;
183167
return $headers;
184168
}
185-
169+
186170
/**
187171
* Check if the $required parameters are present in $params
188172
* @throws MissingParameterException

tests/Traits/EndpointTraitTest.php

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -66,34 +66,11 @@ public function testBodySerializeUnknownContentTypeThrowsException()
6666
$this->bodySerialize(['foo' => 'bar'], 'Unknown-content-type');
6767
}
6868

69-
public function getCompatibilityHeaders()
69+
70+
public function testBuildApiVersion()
7071
{
71-
return [
72-
[['Content-Type' => 'application/json'], ['Content-Type' => sprintf(Client::API_COMPATIBILITY_HEADER, 'application', 'json')]],
73-
[['Accept' => 'application/json'], ['Accept' => sprintf(Client::API_COMPATIBILITY_HEADER, 'application', 'json')]],
74-
[['Content-Type' => 'application/x-ndjson'], ['Content-Type' => sprintf(Client::API_COMPATIBILITY_HEADER, 'application', 'x-ndjson')]],
75-
[['Accept' => 'application/x-ndjson'], ['Accept' => sprintf(Client::API_COMPATIBILITY_HEADER, 'application', 'x-ndjson')]],
76-
[['Content-Type' => 'application/text'], ['Content-Type' => sprintf(Client::API_COMPATIBILITY_HEADER, 'application', 'text')]],
77-
[['Accept' => 'text/plain'], ['Accept' => sprintf(Client::API_COMPATIBILITY_HEADER, 'text', 'plain')]],
78-
// Multiple values
79-
[[
80-
'Accept' => 'text/plain,application/json'
81-
], [
82-
'Accept' => sprintf(
83-
"%s,%s",
84-
sprintf(Client::API_COMPATIBILITY_HEADER, 'text', 'plain'),
85-
sprintf(Client::API_COMPATIBILITY_HEADER, 'application', 'json')
86-
)
87-
]],
88-
];
89-
}
90-
91-
/**
92-
* @dataProvider getCompatibilityHeaders
93-
*/
94-
public function testBuildCompatibilityHeaders(array $input, array $expected)
95-
{
96-
$this->assertEquals($expected, $this->buildCompatibilityHeaders($input));
72+
$headers = $this->buildApiVersion([]);
73+
$this->assertEquals($headers[Client::API_VERSION_HEADER], Client::API_VERSION);
9774
}
9875

9976
public function getRequestParts(): array
@@ -119,7 +96,7 @@ public function testCreateRequest(string $method, string $url, array $headers, $
11996
$host = parse_url($url, PHP_URL_HOST);
12097
$port = parse_url($url, PHP_URL_PORT);
12198
$this->assertEquals(empty($port) ? $host : $host . ':' . $port, $request->getHeader('Host')[0]);
122-
$headers = $this->buildCompatibilityHeaders($headers);
99+
$headers = $this->buildApiVersion($headers);
123100
foreach ($headers as $name => $value) {
124101
$header = $request->getHeader($name);
125102
$this->assertEquals($value, implode(',', $header));

0 commit comments

Comments
 (0)