Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ Loop::run(function () {
});
```

If your elasticsearch instance requires user authentication, you can set username and password when you build the client, like this:

```php
$client = new Webgriffe\AmpElasticsearch\Client('http://my.elasticsearch.test:9200', 'myUsername', 'myPassword');
});
```

See other usage examples in the [`tests/Integration/ClientTest.php`](./tests/Integration/ClientTest.php).

All client methods return an array representation of the ElasticSearch REST API responses in case of sucess or an `Webgriffe\AmpElasticsearch\Error` in case of error.
Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ services:
- elasticsearch

elasticsearch:
image: "docker.elastic.co/elasticsearch/elasticsearch-oss:7.4.0"
image: 'elastic/elasticsearch:8.4.2'
environment:
- "discovery.type=${ES_DISCOVERY_TYPE:-single-node}"
- "ES_JAVA_OPTS=${ES_JAVA_OPTS:--Xms512m -Xmx512m}"
- xpack.security.enabled=true
- ELASTIC_PASSWORD=mySecretPassword
ports:
- "9200:9200"
11 changes: 10 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

use Amp\Http\Client\HttpClient;
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\Interceptor\SetRequestHeaderIfUnset;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use function Amp\call;
use Amp\Promise;
use function Amp\call;

class Client
{
Expand All @@ -28,6 +29,14 @@ public function __construct(string $baseUri)
$this->baseUri = rtrim($baseUri, '/');
}

public function setCredentials(string $username, string $password): void
{
$authHeader = base64_encode("$username:$password");
$this->httpClient = (new HttpClientBuilder())
->intercept(new SetRequestHeaderIfUnset('Authorization', 'Basic ' . $authHeader))
->build();
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like it; the Client is being recreated from scratch. What if the constructor had initialized it differently?

public function createIndex(string $index, array $body = null): Promise
{
$method = 'PUT';
Expand Down
5 changes: 5 additions & 0 deletions tests/Integration/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class ClientTest extends TestCase
{
const TEST_INDEX = 'test_index';
const DEFAULT_ES_URL = 'http://127.0.0.1:9200';
const DEFAULT_ES_USER = 'elastic';
const DEFAULT_ES_PASSWORD = 'mySecretPassword';

/**
* @var Client
Expand All @@ -22,7 +24,10 @@ class ClientTest extends TestCase
protected function setUp(): void
{
$esUrl = getenv('ES_URL') ?: self::DEFAULT_ES_URL;
$esUser = getenv('ES_USER') ?: self::DEFAULT_ES_USER;
$esPassword = getenv('ES_PASSWORD') ?: self::DEFAULT_ES_PASSWORD;
$this->client = new Client($esUrl);
$this->client->setCredentials($esUser, $esPassword);
$indices = Promise\wait($this->client->catIndices());
foreach ($indices as $index) {
Promise\wait($this->client->deleteIndex($index['index']));
Expand Down
Loading