Skip to content

Commit 14181fa

Browse files
kimpeppershyimdblock
authored
Update user guide with new factory approach (#291)
* Update user guide with new factory approach Signed-off-by: Kim Pepper <[email protected]> * Update USER_GUIDE.md Co-authored-by: Shyim <[email protected]> Signed-off-by: Kim Pepper <[email protected]> * Update USER_GUIDE.md Co-authored-by: Daniel (dB.) Doubrovkine <[email protected]> Signed-off-by: Kim Pepper <[email protected]> * Address feedback Signed-off-by: Kim Pepper <[email protected]> --------- Signed-off-by: Kim Pepper <[email protected]> Co-authored-by: Shyim <[email protected]> Co-authored-by: Daniel (dB.) Doubrovkine <[email protected]>
1 parent ce3d284 commit 14181fa

File tree

1 file changed

+79
-18
lines changed

1 file changed

+79
-18
lines changed

USER_GUIDE.md

Lines changed: 79 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
- [User Guide](#user-guide)
22
- [Example usage](#example-usage)
3+
- [Client Factories](#client-factories)
4+
- [Guzzle Client Factory](#guzzle-client-factory)
5+
- [Symfony Client Factory](#symfony-client-factory)
6+
- [ClientBuilder (Deprecated)](#clientbuilder-deprecated)
7+
- [Advanced Features](#advanced-features)
8+
39
# User Guide
410

5-
Install this client using Composer into your project `composer req opensearch-project/opensearch-php`
11+
Install this client using Composer into your project
12+
```bash
13+
composer require opensearch-project/opensearch-php
14+
```
15+
16+
Install a PSR-18 compatible HTTP client. For example, to use Guzzle:
17+
```bash
18+
composer require guzzlehttp/guzzle
19+
```
20+
21+
Alternatively, you can use Symfony:
22+
```bash
23+
composer require symfony/http-client
24+
```
625

726
## Example usage
827

@@ -25,23 +44,11 @@ class MyOpenSearchClass
2544
public function __construct()
2645
{
2746
// Simple Setup
28-
$this->client = OpenSearch\ClientBuilder::fromConfig([
29-
'hosts' => [
30-
'https://localhost:9200'
31-
],
32-
'retries' => 2,
33-
'handler' => OpenSearch\ClientBuilder::multiHandler()
47+
$this->client = (new \OpenSearch\GuzzleClientFactory())->create([
48+
'base_uri' => 'https://localhost:9200',
49+
'auth' => ['admin', getenv('OPENSEARCH_PASSWORD')],
50+
'verify' => false, // Disables SSL verification for local development.
3451
]);
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();
4552
}
4653

4754

@@ -359,7 +366,61 @@ try {
359366

360367
```
361368

362-
## ClientBuilder
369+
## Client Factories
370+
371+
You can create an OpenSearch Client using any [PSR-18](https://www.php-fig.org/psr/psr-18/) compatible HTTP client. Two factories are provided to reduce the boilerplate code required to create a client using [Guzzle](https://docs.guzzlephp.org/en/stable/) or [Symfony](https://symfony.com/doc/current/http_client.html) HTTP clients.
372+
373+
The `GuzzleClientFactory` and `SymfonyClientFactory` classes are used to create an OpenSearch Client instance.
374+
375+
### Guzzle Client Factory
376+
377+
This factory creates an OpenSearch Client instance using the Guzzle HTTP client.
378+
379+
```bash
380+
composer require guzzlehttp/guzzle
381+
```
382+
383+
```php
384+
$client = (new \OpenSearch\GuzzleClientFactory())->create([
385+
'base_uri' => 'https://localhost:9200',
386+
'auth' => ['admin', getenv('OPENSEARCH_PASSWORD')],
387+
'verify' => false,
388+
]);
389+
```
390+
`GuzzleClientFactory::__construct()` accepts an `int $maxRetries` as the first argument to enable retrying a request
391+
when a `500 Server Error` status code is received in the response, or network connection error occurs. A PSR-3 Logger
392+
can be passed as the second argument to log the retries.
393+
394+
[Guzzle Request options](https://docs.guzzlephp.org/en/stable/request-options.html) can be passed to the
395+
`create()` method. The `base_uri` option is *required*.
396+
397+
### Symfony Client Factory
398+
399+
This factory creates an OpenSearch Client instance using the Symfony HTTP client.
400+
401+
```bash
402+
composer require symfony/http-client
403+
```
404+
405+
```php
406+
$client = (new \OpenSearch\SymfonyClientFactory())->create([
407+
'base_uri' => 'https://localhost:9200',
408+
'auth_basic' => ['admin', getenv('OPENSEARCH_PASSWORD')],
409+
'verify_peer' => false,
410+
]);
411+
````
412+
413+
The `SymfonyClientFactory` constructor accepts an `int $maxRetries` as the first argument to enable retrying a request
414+
when a `500 Server Error` status code is received in the response, or network connection error occurs. A PSR-3 Logger
415+
can be passed as the second argument to log the retries.
416+
417+
[Symfony HTTP Client configuration options](https://symfony.com/doc/current/http_client.html#configuration-options) can
418+
be passed to the `create()` method. The `base_uri` option is *required*.
419+
420+
## ClientBuilder (Deprecated)
421+
422+
**`ClientBuilder` is deprecated in 2.4.0 and will be removed in 3.0.0. Use the `GuzzleClientFactory` or `SymfonyClientFactory` instead.**
423+
363424

364425
The `\OpenSearch\ClientBuilder` class is used to create a `\OpenSearch\Client` instance. It provides a fluent interface for configuring the client.
365426

0 commit comments

Comments
 (0)