Skip to content

Commit ece8458

Browse files
committed
Update user guide with new factory approach
Signed-off-by: Kim Pepper <[email protected]>
1 parent 2706543 commit ece8458

File tree

1 file changed

+76
-18
lines changed

1 file changed

+76
-18
lines changed

USER_GUIDE.md

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,20 @@
22
- [Example usage](#example-usage)
33
# User Guide
44

5-
Install this client using Composer into your project `composer req opensearch-project/opensearch-php`
5+
Install this client using Composer into your project
6+
```bash
7+
composer require opensearch-project/opensearch-php`
8+
```
9+
10+
Install a PSR-18 compatible HTTP client. For example, to use Guzzle:
11+
```bash
12+
composer require guzzlehttp/guzzle
13+
```
14+
15+
Alternatively, you can use Symfony HTTP Client:
16+
```bash
17+
composer require symfony/http-client
18+
```
619

720
## Example usage
821

@@ -25,23 +38,11 @@ class MyOpenSearchClass
2538
public function __construct()
2639
{
2740
// Simple Setup
28-
$this->client = OpenSearch\ClientBuilder::fromConfig([
29-
'hosts' => [
30-
'https://localhost:9200'
31-
],
32-
'retries' => 2,
33-
'handler' => OpenSearch\ClientBuilder::multiHandler()
41+
$client = (new \OpenSearch\GuzzleClientFactory())->create([
42+
'base_uri' => 'https://localhost:9200',
43+
'auth' => ['admin', getenv('OPENSEARCH_PASSWORD')],
44+
'verify' => false,
3445
]);
35-
36-
// OR via Builder
37-
// $this->client = (new \OpenSearch\ClientBuilder())
38-
// ->setHosts(['https://localhost:9200'])
39-
// ->setBasicAuthentication('admin', 'admin') // For testing only. Don't store credentials in code.
40-
// // or, if using AWS SigV4 authentication:
41-
// ->setSigV4Region('us-east-2')
42-
// ->setSigV4CredentialProvider(true)
43-
// ->setSSLVerification(false) // For testing only. Use certificate for validation
44-
// ->build();
4546
}
4647
4748
@@ -359,7 +360,64 @@ try {
359360
360361
```
361362
362-
## ClientBuilder
363+
## Client Factories
364+
365+
You can create an OpenSearch Client using any [PSR-18](https://www.php-fig.org/psr/psr-18/) compatible
366+
HTTP client. Two factories are provided to reduce the boilerplate code required to create a client
367+
using [Guzzle](https://docs.guzzlephp.org/en/stable/) or [Symfony](https://symfony.com/doc/current/http_client.html)
368+
HTTP clients.
369+
370+
The `GuzzleClientFactory` and `SymfonyClientFactory` classes are used to create an OpenSearch Client instance.
371+
372+
### Guzzle Client Factory
373+
374+
This factory creates an OpenSearch Client instance using the Guzzle HTTP client.
375+
376+
```bash
377+
composer require guzzlehttp/guzzle
378+
```
379+
380+
```php
381+
$client = (new \OpenSearch\GuzzleClientFactory())->create([
382+
'base_uri' => 'https://localhost:9200',
383+
'auth' => ['admin', getenv('OPENSEARCH_PASSWORD')],
384+
'verify' => false,
385+
]);
386+
```
387+
`GuzzleClientFactory::__construct()` accepts an `int $maxRetries` as the first argument to enable retrying a request
388+
when a `500 Server Error` status code is received in the response, or network connection error occurs. A PSR-3 Logger
389+
can be passed as the second argument to log the retries.
390+
391+
[Guzzle Request options](https://docs.guzzlephp.org/en/stable/request-options.html) can be passed to the
392+
`create()` method. The `base_uri` option is *required*.
393+
394+
### Symfony Client Factory
395+
396+
This factory creates an OpenSearch Client instance using the Symfony HTTP client.
397+
398+
```bash
399+
composer require symfony/http-client
400+
```
401+
402+
```php
403+
$client = (new \OpenSearch\SymfonyClientFactory())->create([
404+
'base_uri' => 'https://localhost:9200',
405+
'auth_basic' => ['admin', getenv('OPENSEARCH_PASSWORD')],
406+
'verify_peer' => false,
407+
]);
408+
````
409+
410+
`SymfonyClientFactory::__construct()` accepts an `int $maxRetries` as the first argument to enable retrying a request
411+
when a `500 Server Error` status code is received in the response, or network connection error occurs. A PSR-3 Logger
412+
can be passed as the second argument to log the retries.
413+
414+
[Symfony HTTP Client configuration options](https://symfony.com/doc/current/http_client.html#configuration-options) can
415+
be passed to the `create()` method. The `base_uri` option is *required*.
416+
417+
## ClientBuilder (Deprecated)
418+
419+
**`ClientBuilder` is deprecated in 2.4.0 and will be removed in 3.0.0. Use the `GuzzleClientFactory` or `SymfonyClientFactory` instead.**
420+
363421
364422
The `\OpenSearch\ClientBuilder` class is used to create a `\OpenSearch\Client` instance. It provides a fluent interface for configuring the client.
365423

0 commit comments

Comments
 (0)