Skip to content

Commit

Permalink
Fix serializer and update docs
Browse files Browse the repository at this point in the history
Signed-off-by: Kim Pepper <[email protected]>
  • Loading branch information
kimpepper committed Dec 20, 2024
1 parent 273949b commit 66c912e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 26 deletions.
39 changes: 29 additions & 10 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,31 @@ $guzzleClient = new \GuzzleHttp\Client([
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'User-Agent' => sprintf('opensearch-php/%s (%s; PHP %s)', Client::VERSION, PHP_OS, PHP_VERSION),
'User-Agent' => sprintf('opensearch-php/%s (%s; PHP %s)', \OpenSearch\Client::VERSION, PHP_OS, PHP_VERSION),
]
]);

$guzzleHttpFactory = new \GuzzleHttp\Psr7\HttpFactory();

$serializer = new \OpenSearch\Serializers\SmartSerializer();

$requestFactory = new \OpenSearch\RequestFactory(
$guzzleHttpFactory,
$guzzleHttpFactory,
$guzzleHttpFactory,
$serializer,
);

$transport = (new OpenSearch\TransportFactory())
->setHttpClient($guzzleClient)
->setPsrRequestFactory($guzzleHttpFactory)
->setStreamFactory($guzzleHttpFactory)
->setUriFactory($guzzleHttpFactory)
->setRequestFactory($requestFactory)
->create();

$endpointFactory = new \OpenSearch\EndpointFactory();
$client = new Client($transport, $endpointFactory, []);
$client = new \OpenSearch\Client($transport, $endpointFactory, []);

// Send a request to the 'info' endpoint.
$info = $client->info();
```

### Configuring Symfony HTTP Client in 2.x
Expand All @@ -75,14 +87,21 @@ $symfonyPsr18Client = (new \Symfony\Component\HttpClient\Psr18Client())->withOpt
],
]);

$transport = (new OpenSearch\TransportFactory())
$serializer = new \OpenSearch\Serializers\SmartSerializer();

$requestFactory = new \OpenSearch\RequestFactory(
$symfonyPsr18Client,
$symfonyPsr18Client,
$symfonyPsr18Client,
$serializer,
);

$transport = (new \OpenSearch\TransportFactory())
->setHttpClient($symfonyPsr18Client)
->setPsrRequestFactory($symfonyPsr18Client)
->setStreamFactory($symfonyPsr18Client)
->setUriFactory($symfonyPsr18Client)
->setRequestFactory($requestFactory)
->create();

$client = new Client($transport, $endpointFactory, []);
$client = new \OpenSearch\Client($transport, $endpointFactory, []);

// Send a request to the 'info' endpoint.
$info = $client->info();
Expand Down
41 changes: 26 additions & 15 deletions samples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
* SPDX-License-Identifier: Apache-2.0
*/

use OpenSearch\Client;

require_once __DIR__ . '/vendor/autoload.php';


// Auto-configure by discovery example

$transport = (new \OpenSearch\TransportFactory())->create();
$endpointFactory = new \OpenSearch\EndpointFactory();
$client = new Client($transport, $endpointFactory, []);
$client = new \OpenSearch\Client($transport, $endpointFactory, []);

// Send a request to the 'info' endpoint.
$info = $client->info();
Expand All @@ -29,24 +26,32 @@
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'User-Agent' => sprintf('opensearch-php/%s (%s; PHP %s)', Client::VERSION, PHP_OS, PHP_VERSION),
'User-Agent' => sprintf('opensearch-php/%s (%s; PHP %s)', \OpenSearch\Client::VERSION, PHP_OS, PHP_VERSION),
]
]);

$guzzleHttpFactory = new \GuzzleHttp\Psr7\HttpFactory();

$serializer = new \OpenSearch\Serializers\SmartSerializer();

$requestFactory = new \OpenSearch\RequestFactory(
$guzzleHttpFactory,
$guzzleHttpFactory,
$guzzleHttpFactory,
$serializer,
);

$transport = (new OpenSearch\TransportFactory())
->setHttpClient($guzzleClient)
->setPsrRequestFactory($guzzleHttpFactory)
->setStreamFactory($guzzleHttpFactory)
->setUriFactory($guzzleHttpFactory)
->setRequestFactory($requestFactory)
->create();

$endpointFactory = new \OpenSearch\EndpointFactory();
$client = new Client($transport, $endpointFactory, []);
$client = new \OpenSearch\Client($transport, $endpointFactory, []);

// Send a request to the 'info' endpoint.
$info = $client->info();


// Symfony example

$symfonyPsr18Client = (new \Symfony\Component\HttpClient\Psr18Client())->withOptions([
Expand All @@ -60,15 +65,21 @@
],
]);

$serializer = new \OpenSearch\Serializers\SmartSerializer();

$transport = (new OpenSearch\TransportFactory())
$requestFactory = new \OpenSearch\RequestFactory(
$symfonyPsr18Client,
$symfonyPsr18Client,
$symfonyPsr18Client,
$serializer,
);

$transport = (new \OpenSearch\TransportFactory())
->setHttpClient($symfonyPsr18Client)
->setPsrRequestFactory($symfonyPsr18Client)
->setStreamFactory($symfonyPsr18Client)
->setUriFactory($symfonyPsr18Client)
->setRequestFactory($requestFactory)
->create();

$client = new Client($transport, $endpointFactory, []);
$client = new \OpenSearch\Client($transport, $endpointFactory, []);

// Send a request to the 'info' endpoint.
$info = $client->info();
2 changes: 1 addition & 1 deletion src/OpenSearch/TransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function create(): HttpTransport
$this->httpClient = Psr18ClientDiscovery::find();
}

return new HttpTransport($this->httpClient, $this->requestFactory, $this->serializer);
return new HttpTransport($this->httpClient, $this->requestFactory, $this->getSerializer());
}

}

0 comments on commit 66c912e

Please sign in to comment.