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

[WIP] Upgrading guzzle. #219

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
8 changes: 5 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ jobs:
composer run unit

integration-test-elasticsearch:
name: Tntegration Test (Elasticsearch)
name: Integration Test (Elasticsearch)
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
Expand Down Expand Up @@ -175,7 +175,7 @@ jobs:
OPENSEARCH_URL: 'http://localhost:9200'

integration-test-opensearch:
name: Tntegration Test (OpenSearch)
name: Integration Test (OpenSearch)
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
Expand Down Expand Up @@ -234,7 +234,7 @@ jobs:
OPENSEARCH_URL: 'http://localhost:9200'

integration-test-opensearch-strong-password:
name: Tntegration Test (OpenSearch w/Strong Password)
name: Integration Test (OpenSearch w/Strong Password)
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
Expand All @@ -244,6 +244,8 @@ jobs:
search-server-image:
- opensearchproject/opensearch:2.12.0
- opensearchproject/opensearch:2.13.0
- opensearchproject/opensearch:2.14.0
- opensearchproject/opensearch:2.15.0
services:
search-server:
image: ${{ matrix.search-server-image }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test_unreleased.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
fail-fast: false
matrix:
opensearch_ref: [ '1.x', '2.x', '2.0', 'main' ]
opensearch_ref: [ '1.x', '2.x', 'main' ]
steps:
- name: Checkout PHP Client
uses: actions/checkout@v2
Expand Down Expand Up @@ -73,6 +73,6 @@ jobs:

- name: Integration tests
run: |
composer run integration
composer run integration-min
env:
OPENSEARCH_URL: 'http://localhost:9200'
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Added
- Generate endpoints from OpenSearch API Specification ([#194](https://github.com/opensearch-project/opensearch-php/pull/194))
- Added workflow for automated API update using OpenSearch API specification ([#209](https://github.com/opensearch-project/opensearch-php/pull/209))
- Added samples ([#218](https://github.com/opensearch-project/opensearch-php/pull/218))
### Changed
### Deprecated
### Removed
Expand Down
18 changes: 17 additions & 1 deletion DEVELOPER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,23 @@ If you don't have a running server, you can start one with Docker using `docker

The integration tests are using by default following address `https://admin:admin@localhost:9200`. This can be changed by setting the environment variable `OPENSEARCH_URL` to a different url.

To run the integration tests, you can use `composer run integration`
To run the integration tests, you can use `composer run integration-min` for just OpenSearch or `composer run integration` for OpenSearch and its plugins.

```bash
export OPENSEARCH_PASSWORD=myStrongPassword123!
export OPENSEARCH_URL=https://admin:$OPENSEARCH_PASSWORD@localhost:9200

composer run integration
```


```bash
export OPENSEARCH_PASSWORD=myStrongPassword123!
export OPENSEARCH_URL=https://admin:$OPENSEARCH_PASSWORD@localhost:9200

composer run integration
```


### Static analyse and code style checker

Expand Down
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,40 @@

## Welcome!

**opensearch-php** is [a community-driven, open source fork](https://aws.amazon.com/blogs/opensource/introducing-opensearch/) of elasticsearch-php licensed under the [Apache v2.0 License](https://github.com/opensearch-project/opensearch-php/blob/main/LICENSE). For more information, see [opensearch.org](https://opensearch.org/).
**opensearch-php** is [a community-driven, open source fork](https://aws.amazon.com/blogs/opensource/introducing-opensearch/) of elasticsearch-php licensed under the [Apache v2.0 License](LICENSE). For more information, see [opensearch.org](https://opensearch.org/).

## Project Resources

* [Project Website](https://opensearch.org/)
* [User Guide And Sample Code](https://github.com/opensearch-project/opensearch-php/blob/main/USER_GUIDE.md)
* [Developer Guide](https://github.com/opensearch-project/opensearch-php/blob/main/DEVELOPER_GUIDE.md)
* [User Guide](USER_GUIDE.md)
* [Samples](samples)
* [Developer Guide](DEVELOPER_GUIDE.md)
* [Downloads](https://opensearch.org/downloads.html).
* [Documentation](https://opensearch.org/docs/latest/)
* Need help? Try [Forums](https://discuss.opendistrocommunity.dev/)
* [Project Principles](https://opensearch.org/#principles)
* [Contributing to OpenSearch](https://github.com/opensearch-project/opensearch-php/blob/main/CONTRIBUTING.md)
* [Maintainer Responsibilities](https://github.com/opensearch-project/opensearch-php/blob/main/MAINTAINERS.md)
* [Release Management](https://github.com/opensearch-project/opensearch-php/blob/main/RELEASING.md)
* [Admin Responsibilities](https://github.com/opensearch-project/opensearch-php/blob/main/ADMINS.md)
* [Security](https://github.com/opensearch-project/opensearch-php/blob/main/SECURITY.md)
* [Contributing to OpenSearch](CONTRIBUTING.md)
* [Maintainer Responsibilities](MAINTAINERS.md)
* [Release Management](RELEASING.md)
* [Admin Responsibilities](ADMINS.md)
* [Security](SECURITY.md)

## Code of Conduct

This project has adopted the [Amazon Open Source Code of Conduct](https://github.com/opensearch-project/opensearch-php/blob/main/CODE_OF_CONDUCT.md). For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq), or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
This project has adopted the [Amazon Open Source Code of Conduct](CODE_OF_CONDUCT.md). For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq), or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.

## Sample code

See [Sample Code](https://github.com/opensearch-project/opensearch-php/blob/main/USER_GUIDE.md).
See [Sample Code](USER_GUIDE.md).

## Compatibility with OpenSearch

See [Compatibility](COMPATIBILITY.md).

## License

This project is licensed under the [Apache v2.0 License](https://github.com/opensearch-project/opensearch-php/blob/main/LICENSE).
This project is licensed under the [Apache v2.0 License](LICENSE).

## Copyright

Copyright OpenSearch Contributors. See [NOTICE](https://github.com/opensearch-project/opensearch-php/blob/main/NOTICE) for details.
Copyright OpenSearch Contributors. See [NOTICE](NOTICE) for details.
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
],
"require": {
"php": "^7.3 || ^8.0",
"ext-json": ">=1.3.7",
"ext-curl": "*",
"ezimuel/ringphp": "^1.1.2",
"ext-json": ">=1.3.7",
"guzzlehttp/guzzle": "^7.9",
"guzzlehttp/promises": "^2.0",
"psr/http-client": "^1.0",
"psr/http-message": "^1.1 || ^2.0",
"psr/log": "^1|^2|^3",
"symfony/yaml": "*"
},
Expand Down Expand Up @@ -61,6 +64,9 @@
"integration": [
"phpunit --group Integration"
],
"integration-min": [
"phpunit --group Integration-Min"
],
"phpunit": [
"phpunit"
],
Expand Down
15 changes: 15 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
### OpenSearch PHP Client Samples

Start an OpenSearch instance.

```
docker run -d -p 9200:9200 -p 9600:9600 -e "discovery.type=single-node" -e "OPENSEARCH_INITIAL_ADMIN_PASSWORD=myStrongPassword123!" opensearchproject/opensearch:latest
```

Run sample.

```
export OPENSEARCH_PASSWORD=myStrongPassword123!
composer install
composer run index
```
18 changes: 18 additions & 0 deletions samples/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "opensearch-php/samples",
"description": "OpenSearch PHP client samples.",
"type": "project",
"authors": [],
"scripts": {
"index": ["php index.php"]
},
"repositories": [
{
"type": "path",
"url": "../"
}
],
"require": {
"opensearch-project/opensearch-php": "dev-main"
}
}
23 changes: 23 additions & 0 deletions samples/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

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

$client = OpenSearch\ClientBuilder::fromConfig([
'Hosts' => [
'https://localhost:9200'
],
'BasicAuthentication' => ['admin', getenv('OPENSEARCH_PASSWORD')],
'Retries' => 2,
'SSLVerification' => false
]);

$info = $client->info();

echo "{$info['version']['distribution']}: {$info['version']['number']}\n";

?>
85 changes: 45 additions & 40 deletions src/OpenSearch/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@
use OpenSearch\Namespaces\NamespaceBuilderInterface;
use OpenSearch\Serializers\SerializerInterface;
use OpenSearch\Serializers\SmartSerializer;
use GuzzleHttp\Ring\Client\CurlHandler;
use GuzzleHttp\Ring\Client\CurlMultiHandler;
use GuzzleHttp\Ring\Client\Middleware;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use ReflectionClass;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\Handler\CurlMultiHandler;
use GuzzleHttp\Handler\Proxy;
use Psr\Http\Message\RequestInterface;
use GuzzleHttp\Utils;

class ClientBuilder
{
Expand Down Expand Up @@ -239,30 +241,30 @@ public static function fromConfig(array $config, bool $quiet = false): Client
return $builder->build();
}

/**
* Get the default handler
*
* @param array $multiParams
* @param array $singleParams
* @throws \RuntimeException
*/
public static function defaultHandler(array $multiParams = [], array $singleParams = []): callable
{
$future = null;
if (extension_loaded('curl')) {
$config = array_merge([ 'mh' => curl_multi_init() ], $multiParams);
if (function_exists('curl_reset')) {
$default = new CurlHandler($singleParams);
$future = new CurlMultiHandler($config);
} else {
$default = new CurlMultiHandler($config);
}
} else {
throw new \RuntimeException('OpenSearch-PHP requires cURL, or a custom HTTP handler.');
}

return $future ? Middleware::wrapFuture($default, $future) : $default;
}
// /**
// * Get the default handler
// *
// * @param array $multiParams
// * @param array $singleParams
// * @throws \RuntimeException
// */
// public static function defaultHandler(array $multiParams = [], array $singleParams = []): callable
// {
// $future = null;
// if (extension_loaded('curl')) {
// $config = array_merge([ 'mh' => curl_multi_init() ], $multiParams);
// if (function_exists('curl_reset')) {
// $default = new CurlHandler($singleParams);
// $future = new CurlMultiHandler($config);
// } else {
// $default = new CurlMultiHandler($config);
// }
// } else {
// throw new \RuntimeException('OpenSearch-PHP requires cURL, or a custom HTTP handler.');
// }

// return $future ? Proxy::wrapSync($default, $future) : $default;
// }

/**
* Get the multi handler for async (CurlMultiHandler)
Expand Down Expand Up @@ -583,7 +585,7 @@ public function build(): Client
$this->buildLoggers();

if (is_null($this->handler)) {
$this->handler = ClientBuilder::defaultHandler();
$this->handler = Utils::chooseHandler(); // ClientBuilder::defaultHandler();
}

if (!is_null($this->sigV4CredentialProvider)) {
Expand All @@ -610,18 +612,21 @@ public function build(): Client
}

if (!is_null($sslOptions)) {
$sslHandler = function (callable $handler, array $sslOptions) {
return function (array $request) use ($handler, $sslOptions) {
// Add our custom headers
foreach ($sslOptions as $key => $value) {
$request['client'][$key] = $value;
}

// Send the request using the handler and return the response.
return $handler($request);
};
};
$this->handler = $sslHandler($this->handler, $sslOptions);
// $sslHandler = function (callable $handler, array $sslOptions) {
// return function (RequestInterface $request, array $options) use ($handler, $sslOptions) {
// // Add our custom headers
// foreach ($sslOptions as $key => $value) {
// $request['client'][$key] = $value;
// }

// // Send the request using the handler and return the response.
// return $handler($request);
// };
// };
// $this->handler = $sslHandler($this->handler, $sslOptions);
foreach ($sslOptions as $key => $value) {
$this->connectionParams['client'][$key] = $value;
}
}

if (is_null($this->serializer)) {
Expand Down
Loading
Loading