From 565db89adc9f3a40081e4c41680fee21b132aa51 Mon Sep 17 00:00:00 2001 From: Dylan Pierce Date: Sun, 26 Aug 2018 09:48:13 -0600 Subject: [PATCH 1/4] Passing bearer token to the guzzle client --- src/Client.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index eaa3729..f93a8b9 100644 --- a/src/Client.php +++ b/src/Client.php @@ -101,7 +101,11 @@ public function __construct($accessToken, HttpClient $httpClient = null, $apiLan */ private function defaultHttpClient() { - return new GuzzleHttpClient(); + return new GuzzleHttpClient([ + 'headers' => [ + 'Authorization' => sprintf('Bearer %s', $this->accessToken), + ], + ]); } /** From df65ca0404884130a529b2b30d791b9cbb7ce63a Mon Sep 17 00:00:00 2001 From: Dylan Pierce Date: Sun, 26 Aug 2018 09:58:50 -0600 Subject: [PATCH 2/4] Didnt realize that the GuzzleHttpClient was wrapped internally --- src/Client.php | 6 ++---- src/HttpClient/GuzzleHttpClient.php | 3 ++- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Client.php b/src/Client.php index f93a8b9..6a781fe 100644 --- a/src/Client.php +++ b/src/Client.php @@ -101,10 +101,8 @@ public function __construct($accessToken, HttpClient $httpClient = null, $apiLan */ private function defaultHttpClient() { - return new GuzzleHttpClient([ - 'headers' => [ - 'Authorization' => sprintf('Bearer %s', $this->accessToken), - ], + return new GuzzleHttpClient(null, [ + 'Authorization' => sprintf('Bearer %s', $this->accessToken), ]); } diff --git a/src/HttpClient/GuzzleHttpClient.php b/src/HttpClient/GuzzleHttpClient.php index 98b9b02..0bcaded 100644 --- a/src/HttpClient/GuzzleHttpClient.php +++ b/src/HttpClient/GuzzleHttpClient.php @@ -24,12 +24,13 @@ class GuzzleHttpClient implements HttpClient * * @param ClientInterface|null $guzzleClient */ - public function __construct(ClientInterface $guzzleClient = null) + public function __construct(ClientInterface $guzzleClient = null, $headers) { $this->guzzleClient = $guzzleClient ?: new GuzzleClient([ 'base_uri' => Client::API_BASE_URI . Client::DEFAULT_API_ENDPOINT, 'timeout' => Client::DEFAULT_TIMEOUT, 'connect_timeout' => Client::DEFAULT_TIMEOUT, + 'headers' => $headers, ]); } From cdb51c2f5fb8de632dc4168b45bf74f11a008bbc Mon Sep 17 00:00:00 2001 From: Dylan Pierce Date: Sun, 26 Aug 2018 10:41:58 -0600 Subject: [PATCH 3/4] Fixing authorization header problem, bypassing wrapped guzzle client --- .gitignore | 2 ++ codeception.yml | 10 +++++++++ composer.json | 6 ++++- src/Client.php | 20 +++++++++++------ tests/_data/.gitkeep | 0 tests/_output/.gitignore | 2 ++ tests/_support/AcceptanceTester.php | 26 ++++++++++++++++++++++ tests/_support/FunctionalTester.php | 26 ++++++++++++++++++++++ tests/_support/Helper/Acceptance.php | 10 +++++++++ tests/_support/Helper/Functional.php | 10 +++++++++ tests/_support/Helper/Unit.php | 10 +++++++++ tests/_support/UnitTester.php | 26 ++++++++++++++++++++++ tests/_support/_generated/.gitignore | 2 ++ tests/acceptance.suite.yml | 12 ++++++++++ tests/functional.suite.yml | 12 ++++++++++ tests/unit.suite.yml | 11 ++++++++++ tests/unit/QueryTest.php | 33 ++++++++++++++++++++++++++++ 17 files changed, 210 insertions(+), 8 deletions(-) create mode 100644 codeception.yml create mode 100644 tests/_data/.gitkeep create mode 100644 tests/_output/.gitignore create mode 100644 tests/_support/AcceptanceTester.php create mode 100644 tests/_support/FunctionalTester.php create mode 100644 tests/_support/Helper/Acceptance.php create mode 100644 tests/_support/Helper/Functional.php create mode 100644 tests/_support/Helper/Unit.php create mode 100644 tests/_support/UnitTester.php create mode 100644 tests/_support/_generated/.gitignore create mode 100644 tests/acceptance.suite.yml create mode 100644 tests/functional.suite.yml create mode 100644 tests/unit.suite.yml create mode 100644 tests/unit/QueryTest.php diff --git a/.gitignore b/.gitignore index b5a5916..e5fecf4 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ composer.lock *.sublime-workspace *.phar *.log + +.env diff --git a/codeception.yml b/codeception.yml new file mode 100644 index 0000000..f0446cc --- /dev/null +++ b/codeception.yml @@ -0,0 +1,10 @@ +paths: + tests: tests + output: tests/_output + data: tests/_data + support: tests/_support + envs: tests/_envs +actor_suffix: Tester +extensions: + enabled: + - Codeception\Extension\RunFailed diff --git a/composer.json b/composer.json index 00b9000..1547c19 100644 --- a/composer.json +++ b/composer.json @@ -25,5 +25,9 @@ "require": { "php": ">=5.5.0", "guzzlehttp/guzzle": "^6.2" + }, + "require-dev": { + "codeception/codeception": "^2.4", + "vlucas/phpdotenv": "^2.5" } -} \ No newline at end of file +} diff --git a/src/Client.php b/src/Client.php index 6a781fe..6f9a1d3 100644 --- a/src/Client.php +++ b/src/Client.php @@ -3,7 +3,7 @@ namespace DialogFlow; use DialogFlow\HttpClient\HttpClient; -use DialogFlow\HttpClient\GuzzleHttpClient; +use GuzzleHttp\Client as GuzzleClient; use DialogFlow\Exception\BadResponseException; use GuzzleHttp\Promise\PromiseInterface; use function GuzzleHttp\Promise\rejection_for; @@ -101,8 +101,13 @@ public function __construct($accessToken, HttpClient $httpClient = null, $apiLan */ private function defaultHttpClient() { - return new GuzzleHttpClient(null, [ - 'Authorization' => sprintf('Bearer %s', $this->accessToken), + return new GuzzleClient([ + 'base_uri' => Client::API_BASE_URI . Client::DEFAULT_API_ENDPOINT, + 'timeout' => Client::DEFAULT_TIMEOUT, + 'connect_timeout' => Client::DEFAULT_TIMEOUT, + 'headers' => [ + 'Authorization' => sprintf('Bearer %s', $this->accessToken), + ], ]); } @@ -146,7 +151,7 @@ public function getLastResponse() */ public function get($uri, array $params = []) { - return $this->send('GET', $uri, null, $params); + return $this->send('GET', $uri, $params); } /** @@ -192,14 +197,15 @@ public function postAsync($uri, array $params = []) * * @return ResponseInterface */ - public function send($method, $uri, $body = null, array $query = [], array $headers = [], array $options = []) + public function send($method, $uri, array $query = []) { $this->validateMethod($method); $query = array_merge($this->getDefaultQuery(), $query); - $headers = array_merge($this->getDefaultHeaders(), $headers); - $this->lastResponse = $this->client->send($method, $uri, $body, $query, $headers, $options); + $this->lastResponse = $this->client->request($method, $uri, [ + 'query' => $query, + ]); $this->validateResponse($this->lastResponse); diff --git a/tests/_data/.gitkeep b/tests/_data/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/_output/.gitignore b/tests/_output/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/tests/_output/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/tests/_support/AcceptanceTester.php b/tests/_support/AcceptanceTester.php new file mode 100644 index 0000000..4c7dcbb --- /dev/null +++ b/tests/_support/AcceptanceTester.php @@ -0,0 +1,26 @@ +load(); + $this->client = new Client($_ENV['DIALOGFLOW_CLIENT_ACCESS_TOKEN']); + } + + protected function _after() + { + } + + // tests + public function testQueryDoesNotRaiseException() + { + $query = $this->client->get('query', [ + 'query' => 'Hello', + 'sessionId' => '1', + ]); + + $response = json_decode((string) $query->getBody(), true); + } +} From 7e1cdce0bf86e12ea403db2d457f30c2ec03bee6 Mon Sep 17 00:00:00 2001 From: Dylan Pierce Date: Sun, 26 Aug 2018 10:53:41 -0600 Subject: [PATCH 4/4] Adding missing sessionId to documentation --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e05b50d..7cbafdb 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ try { $query = $client->get('query', [ 'query' => 'Hello', + 'sessionId' => '13371337' ]); $response = json_decode((string) $query->getBody(), true);