Skip to content

Commit

Permalink
Update user guide with new factory approach (#291)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Feb 12, 2025
1 parent ce3d284 commit 14181fa
Showing 1 changed file with 79 additions and 18 deletions.
97 changes: 79 additions & 18 deletions USER_GUIDE.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
- [User Guide](#user-guide)
- [Example usage](#example-usage)
- [Client Factories](#client-factories)
- [Guzzle Client Factory](#guzzle-client-factory)
- [Symfony Client Factory](#symfony-client-factory)
- [ClientBuilder (Deprecated)](#clientbuilder-deprecated)
- [Advanced Features](#advanced-features)

# User Guide

Install this client using Composer into your project `composer req opensearch-project/opensearch-php`
Install this client using Composer into your project
```bash
composer require opensearch-project/opensearch-php
```

Install a PSR-18 compatible HTTP client. For example, to use Guzzle:
```bash
composer require guzzlehttp/guzzle
```

Alternatively, you can use Symfony:
```bash
composer require symfony/http-client
```

## Example usage

Expand All @@ -25,23 +44,11 @@ class MyOpenSearchClass
public function __construct()
{
// Simple Setup
$this->client = OpenSearch\ClientBuilder::fromConfig([
'hosts' => [
'https://localhost:9200'
],
'retries' => 2,
'handler' => OpenSearch\ClientBuilder::multiHandler()
$this->client = (new \OpenSearch\GuzzleClientFactory())->create([
'base_uri' => 'https://localhost:9200',
'auth' => ['admin', getenv('OPENSEARCH_PASSWORD')],
'verify' => false, // Disables SSL verification for local development.
]);

// OR via Builder
// $this->client = (new \OpenSearch\ClientBuilder())
// ->setHosts(['https://localhost:9200'])
// ->setBasicAuthentication('admin', 'admin') // For testing only. Don't store credentials in code.
// // or, if using AWS SigV4 authentication:
// ->setSigV4Region('us-east-2')
// ->setSigV4CredentialProvider(true)
// ->setSSLVerification(false) // For testing only. Use certificate for validation
// ->build();
}


Expand Down Expand Up @@ -359,7 +366,61 @@ try {

```

## ClientBuilder
## Client Factories

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.

The `GuzzleClientFactory` and `SymfonyClientFactory` classes are used to create an OpenSearch Client instance.

### Guzzle Client Factory

This factory creates an OpenSearch Client instance using the Guzzle HTTP client.

```bash
composer require guzzlehttp/guzzle
```

```php
$client = (new \OpenSearch\GuzzleClientFactory())->create([
'base_uri' => 'https://localhost:9200',
'auth' => ['admin', getenv('OPENSEARCH_PASSWORD')],
'verify' => false,
]);
```
`GuzzleClientFactory::__construct()` accepts an `int $maxRetries` as the first argument to enable retrying a request
when a `500 Server Error` status code is received in the response, or network connection error occurs. A PSR-3 Logger
can be passed as the second argument to log the retries.

[Guzzle Request options](https://docs.guzzlephp.org/en/stable/request-options.html) can be passed to the
`create()` method. The `base_uri` option is *required*.

### Symfony Client Factory

This factory creates an OpenSearch Client instance using the Symfony HTTP client.

```bash
composer require symfony/http-client
```

```php
$client = (new \OpenSearch\SymfonyClientFactory())->create([
'base_uri' => 'https://localhost:9200',
'auth_basic' => ['admin', getenv('OPENSEARCH_PASSWORD')],
'verify_peer' => false,
]);
````

The `SymfonyClientFactory` constructor accepts an `int $maxRetries` as the first argument to enable retrying a request
when a `500 Server Error` status code is received in the response, or network connection error occurs. A PSR-3 Logger
can be passed as the second argument to log the retries.

[Symfony HTTP Client configuration options](https://symfony.com/doc/current/http_client.html#configuration-options) can
be passed to the `create()` method. The `base_uri` option is *required*.

## ClientBuilder (Deprecated)

**`ClientBuilder` is deprecated in 2.4.0 and will be removed in 3.0.0. Use the `GuzzleClientFactory` or `SymfonyClientFactory` instead.**


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

Expand Down

0 comments on commit 14181fa

Please sign in to comment.