Skip to content

Commit 5a22a88

Browse files
committed
Added X-Opaque-Id support
1 parent 2da5f93 commit 5a22a88

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/Elasticsearch/Endpoints/AbstractEndpoint.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ abstract class AbstractEndpoint
5757
/**
5858
* @return string[]
5959
*/
60-
abstract public function getParamWhitelist();
60+
abstract public function getParamWhitelist(): array;
6161

6262
/**
6363
* @return string
6464
*/
65-
abstract public function getURI();
65+
abstract public function getURI(): string;
6666

6767
/**
6868
* @return string
6969
*/
70-
abstract public function getMethod();
70+
abstract public function getMethod(): string;
7171

7272

7373
/**
@@ -82,9 +82,9 @@ public function setParams($params)
8282
$params = (array) $params;
8383
}
8484

85+
$this->extractOptions($params);
8586
$this->checkUserParams($params);
8687
$params = $this->convertCustom($params);
87-
$this->extractOptions($params);
8888
$this->params = $this->convertArraysToStrings($params);
8989

9090
return $this;
@@ -243,7 +243,7 @@ private function checkUserParams($params)
243243
return; //no params, just return.
244244
}
245245

246-
$whitelist = array_merge($this->getParamWhitelist(), array('client', 'custom', 'filter_path', 'human'));
246+
$whitelist = array_merge($this->getParamWhitelist(), array('client', 'custom', 'filter_path', 'human', 'opaqueId'));
247247

248248
$invalid = array_diff(array_keys($params), $whitelist);
249249
if (count($invalid) > 0) {
@@ -264,6 +264,15 @@ private function extractOptions(&$params)
264264
{
265265
// Extract out client options, then start transforming
266266
if (isset($params['client']) === true) {
267+
// Check if the opaqueId is populated and add the header
268+
if (isset($params['client']['opaqueId']) === true) {
269+
if (isset($params['client']['headers']) === false) {
270+
$params['client']['headers'] = [];
271+
}
272+
$params['client']['headers']['x-opaque-id'] = [trim($params['client']['opaqueId'])];
273+
unset($params['client']['opaqueId']);
274+
}
275+
267276
$this->options['client'] = $params['client'];
268277
unset($params['client']);
269278
}

tests/Elasticsearch/Tests/Endpoints/AbstractEndpointTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,22 @@ protected function setUp(): void
5050
{
5151
$this->endpoint = $this->getMockForAbstractClass(AbstractEndpoint::class);
5252
}
53+
54+
/**
55+
* @covers AbstractEndpoint::setParams
56+
* @covers AbstractEndpoint::extractOptions
57+
* @covers AbstractEndpoint::getOptions
58+
*/
59+
public function testOpaqueIdInHeaders()
60+
{
61+
$params = ['client' => ['opaqueId' => 'test_id_' . rand(1000, 9999)]];
62+
$this->endpoint->setParams($params);
63+
64+
$options = $this->endpoint->getOptions();
65+
$this->assertArrayHasKey('client', $options);
66+
$this->assertArrayHasKey('headers', $options['client']);
67+
$this->assertArrayHasKey('x-opaque-id', $options['client']['headers']);
68+
$this->assertNotEmpty($options['client']['headers']['x-opaque-id']);
69+
$this->assertEquals($params['client']['opaqueId'], $options['client']['headers']['x-opaque-id'][0]);
70+
}
5371
}

0 commit comments

Comments
 (0)