Skip to content

Commit 65fd385

Browse files
authored
Merge pull request #9 from olekjs/dev-release-v1.7.1
Add port and api key configuration
2 parents 8f8f4dd + b9fdd46 commit 65fd385

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

config/services.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
return [
44
'elasticsearch' => [
55
'url' => env('ELASTICSEARCH_URL', 'http://localhost:9200'),
6+
'port' => env('ELASTICSEARCH_PORT'),
7+
'api_key' => env('ELASTICSEARCH_API_KEY'),
68
],
79
];

src/Client.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Olekjs\Elasticsearch;
44

5-
use Illuminate\Contracts\Support\Jsonable;
6-
use Illuminate\Support\Facades\Http;
75
use Olekjs\Elasticsearch\Contracts\AbstractClient;
86
use Olekjs\Elasticsearch\Contracts\BulkOperationInterface;
97
use Olekjs\Elasticsearch\Contracts\ClientInterface;

src/Contracts/AbstractClient.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Illuminate\Http\Client\PendingRequest;
66
use Illuminate\Support\Facades\Http;
7+
use Illuminate\Support\Str;
8+
use Illuminate\Support\Stringable;
79
use Olekjs\Elasticsearch\Exceptions\ConflictResponseException;
810
use Olekjs\Elasticsearch\Exceptions\DeleteResponseException;
911
use Olekjs\Elasticsearch\Exceptions\IndexNotFoundResponseException;
@@ -17,9 +19,21 @@ abstract class AbstractClient
1719
{
1820
protected function getBaseClient(): PendingRequest
1921
{
20-
return Http::acceptJson()
22+
$apiKey = config('services.elasticsearch.api_key');
23+
$port = config('services.elasticsearch.port');
24+
25+
$url = Str::of(config('services.elasticsearch.url'))
26+
->when(!is_null($port), fn(Stringable $str) => $str->append(':' . $port));
27+
28+
$http = Http::acceptJson()
2129
->asJson()
22-
->baseUrl(config('services.elasticsearch.url'));
30+
->baseUrl($url);
31+
32+
if (!is_null($apiKey)) {
33+
$http->withToken($apiKey, 'ApiKey');
34+
}
35+
36+
return $http;
2337
}
2438

2539
/**

0 commit comments

Comments
 (0)