@@ -126,6 +126,11 @@ class ClientBuilder
126
126
*/
127
127
private $ sslVerification = null ;
128
128
129
+ /**
130
+ * @var null|string
131
+ */
132
+ private $ apiKey = null ;
133
+
129
134
public static function create (): ClientBuilder
130
135
{
131
136
return new static ();
@@ -330,13 +335,64 @@ public function setHosts(array $hosts): ClientBuilder
330
335
return $ this ;
331
336
}
332
337
338
+ /**
339
+ * Set the APIKey for Authenication
340
+ *
341
+ * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-api-key.html
342
+ *
343
+ * @param string $apiKey
344
+ */
345
+ public function setApiKeyAuthentication (string $ apiKey )
346
+ {
347
+ $ this ->connectionParams ['client ' ]['headers ' ]['Authorization ' ] = ['ApiKey ' . $ apiKey ];
348
+
349
+ return $ this ;
350
+ }
351
+
352
+ /**
353
+ * Set the APIKey Pair, consiting of the API Id and the ApiKey of the Response from /_security/api_key
354
+ *
355
+ * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-api-key.html
356
+ *
357
+ * @param string $id
358
+ * @param string $apiKey
359
+ */
360
+ public function setApiKeyPairAuthentication (string $ id , string $ apiKey )
361
+ {
362
+ $ this ->setApiKeyAuthentication (base64_encode ($ id . ': ' . $ apiKey ));
363
+
364
+ return $ this ;
365
+ }
366
+
333
367
public function setConnectionParams (array $ params ): ClientBuilder
334
368
{
335
369
$ this ->connectionParams = $ params ;
336
370
337
371
return $ this ;
338
372
}
339
373
374
+ /**
375
+ * Set Elastic Cloud's best Practices Connection Parameters
376
+ *
377
+ * <i>Add custom/additional Params as parameter</i>
378
+ *
379
+ * @param array $additional
380
+ */
381
+ public function setElasticCloudConnectionParams (array $ additional = [])
382
+ {
383
+ $ this ->setConnectionParams (
384
+ [
385
+ 'client ' => [
386
+ 'curl ' => [
387
+ CURLOPT_ENCODING => 1 ,
388
+ ],
389
+ ]
390
+ ] + $ additional
391
+ );
392
+
393
+ return $ this ;
394
+ }
395
+
340
396
public function setRetries (int $ retries ): ClientBuilder
341
397
{
342
398
$ this ->retries = $ retries ;
@@ -565,6 +621,7 @@ private function buildConnectionsFromHosts(array $hosts): array
565
621
$ this ->logger ->error ("Could not parse host: " .print_r ($ host , true ));
566
622
throw new RuntimeException ("Could not parse host: " .print_r ($ host , true ));
567
623
}
624
+
568
625
$ connections [] = $ this ->connectionFactory ->create ($ host );
569
626
}
570
627
0 commit comments