@@ -339,6 +339,8 @@ public function setHosts(array $hosts): ClientBuilder
339
339
/**
340
340
* Set the APIKey Pair, consiting of the API Id and the ApiKey of the Response from /_security/api_key
341
341
*
342
+ * <i>APIKey will have precedence over Basic Authentication</i>
343
+ *
342
344
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-api-key.html
343
345
*
344
346
* @param string $id
@@ -350,12 +352,29 @@ public function setApiKey(string $id, string $apiKey)
350
352
'ApiKey ' . base64_encode ($ id . ': ' . $ apiKey )
351
353
];
352
354
355
+ // Remove Basic Auth Credentials if set
356
+ unset($ this ->connectionParams ['client ' ]['curl ' ][CURLOPT_HTTPAUTH ]);
357
+ unset($ this ->connectionParams ['client ' ]['curl ' ][CURLOPT_USERPWD ]);
358
+
353
359
return $ this ;
354
360
}
355
361
356
- public function setConnectionParams (array $ params ): ClientBuilder
362
+ /**
363
+ * Set the APIKey Pair, consiting of the API Id and the ApiKey of the Response from /_security/api_key
364
+ *
365
+ * @param string $username
366
+ * @param string $password
367
+ */
368
+ public function setBasicAuthentication (string $ username , string $ password )
357
369
{
358
- $ this ->connectionParams = $ params ;
370
+ if (isset ($ this ->connectionParams ['client ' ]['curl ' ]) === false ) {
371
+ $ this ->connectionParams ['client ' ]['curl ' ] = [];
372
+ }
373
+
374
+ $ this ->connectionParams ['client ' ]['curl ' ] += [
375
+ CURLOPT_HTTPAUTH => CURLAUTH_BASIC ,
376
+ CURLOPT_USERPWD => $ username .': ' .$ password
377
+ ];
359
378
360
379
return $ this ;
361
380
}
@@ -390,6 +409,13 @@ public function setElasticCloudId(string $cloudId)
390
409
return $ this ;
391
410
}
392
411
412
+ public function setConnectionParams (array $ params ): ClientBuilder
413
+ {
414
+ $ this ->connectionParams = $ params ;
415
+
416
+ return $ this ;
417
+ }
418
+
393
419
public function setRetries (int $ retries ): ClientBuilder
394
420
{
395
421
$ this ->retries = $ retries ;
0 commit comments