Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update user guide with new factory approach #291

Merged
merged 4 commits into from
Feb 12, 2025
Merged
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
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
Loading