From 1a45e42dc1654d06b40cdbd557599fc7c7176759 Mon Sep 17 00:00:00 2001 From: Martin de Keijzer Date: Thu, 6 Oct 2022 15:48:38 +0200 Subject: [PATCH 1/4] Update docblock --- src/DeezerAPI.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DeezerAPI.php b/src/DeezerAPI.php index b97a021..fa631fd 100644 --- a/src/DeezerAPI.php +++ b/src/DeezerAPI.php @@ -226,7 +226,7 @@ public function addPlaylistToFavorites($playlistId) } /** - * FunctionDescription + * Send a search query * * @param $query * @param $strict From 38ab855eefa6ca7951524d2a93226dbb014bf352 Mon Sep 17 00:00:00 2001 From: Martin de Keijzer Date: Thu, 6 Oct 2022 15:51:24 +0200 Subject: [PATCH 2/4] Set data types --- src/DeezerAPI.php | 10 +++++----- tests/DeezerAPITest.php | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/DeezerAPI.php b/src/DeezerAPI.php index fa631fd..5245280 100644 --- a/src/DeezerAPI.php +++ b/src/DeezerAPI.php @@ -10,7 +10,7 @@ class DeezerAPI /** * @var DeezerAPIClient */ - protected $client; + protected DeezerAPIClient $client; /** * DeezerAPI constructor. @@ -228,15 +228,15 @@ public function addPlaylistToFavorites($playlistId) /** * Send a search query * - * @param $query - * @param $strict - * @param $order + * @param string $query + * @param bool $strict + * @param string|null $order * * @return array|object * * @throws DeezerAPIException */ - public function search($query, $strict = false, $order = null) + public function search(string $query, bool $strict = false, string $order = null): object|array { if (empty($query)) { throw new DeezerAPIException('A query parameter is mandatory'); diff --git a/tests/DeezerAPITest.php b/tests/DeezerAPITest.php index 0d0b868..d7be36b 100644 --- a/tests/DeezerAPITest.php +++ b/tests/DeezerAPITest.php @@ -304,7 +304,7 @@ public function testSearch(): void $this->client->expects(static::once()) ->method('apiRequest') ->with('GET', 'search', [], 'q=bohemian&strict=on&order=RANKING') - ->willReturn('{}'); + ->willReturn([]); $this->deezerApi->search('bohemian', true, 'RANKING'); } } From 47792f6e1b69f832ad05c20611c4dc981feff396 Mon Sep 17 00:00:00 2001 From: Martin de Keijzer Date: Thu, 10 Nov 2022 15:11:34 +0100 Subject: [PATCH 3/4] Update search to send parameters as querystring as required by the API docs --- src/DeezerAPI.php | 8 ++++---- src/DeezerAPIClient.php | 22 ++++++++++++++-------- tests/DeezerAPITest.php | 2 +- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/DeezerAPI.php b/src/DeezerAPI.php index 5245280..dc7e024 100644 --- a/src/DeezerAPI.php +++ b/src/DeezerAPI.php @@ -242,16 +242,16 @@ public function search(string $query, bool $strict = false, string $order = null throw new DeezerAPIException('A query parameter is mandatory'); } - $parameters = sprintf('q=%s', $query); + $apiQuery = ['q' => $query]; if (true === $strict) { - $parameters.= '&strict=on'; + $apiQuery['strict'] = 'on'; } if ($order) { - $parameters.= sprintf('&order=%s', $order); + $apiQuery['order'] = $order; } - return $this->client->apiRequest('GET', 'search', [], $parameters); + return $this->client->apiRequest('GET', 'search', [], null, $apiQuery); } } diff --git a/src/DeezerAPIClient.php b/src/DeezerAPIClient.php index 3f3c22e..9014d05 100644 --- a/src/DeezerAPIClient.php +++ b/src/DeezerAPIClient.php @@ -74,22 +74,28 @@ public function getResponseType(): int * @param string $service * @param array $headers * @param array|string|resource|\Traversable|\Closure $body + * @param array|null $query * * @return object|array * * @throws DeezerAPIException */ - public function apiRequest(string $method, string $service, array $headers = [], $body = null) + public function apiRequest(string $method, string $service, array $headers = [], $body = null, $query = null) { - $url = sprintf( - '%s/%s?access_token=%s', - self::DEEZER_API_URL, - $service, - $this->accessToken - ); + $url = sprintf('%s/%s', self::DEEZER_API_URL, $service); + + if (null === $query) { + $query = []; + } + + $query['access_token'] = $this->accessToken; try { - $response = $this->httpClient->request($method, $url, ['headers' => $headers, 'body' => $body]); + $response = $this->httpClient->request($method, $url, [ + 'headers' => $headers, + 'body' => $body, + 'query' => $query + ]); return json_decode($response->getContent(), $this->responseType === self::RETURN_AS_ASSOC); } catch (ServerExceptionInterface | ClientExceptionInterface | RedirectionExceptionInterface | TransportExceptionInterface $exception) { diff --git a/tests/DeezerAPITest.php b/tests/DeezerAPITest.php index d7be36b..0c3e840 100644 --- a/tests/DeezerAPITest.php +++ b/tests/DeezerAPITest.php @@ -303,7 +303,7 @@ public function testSearch(): void { $this->client->expects(static::once()) ->method('apiRequest') - ->with('GET', 'search', [], 'q=bohemian&strict=on&order=RANKING') + ->with('GET', 'search', [], null, ['q' => 'bohemian', 'strict' => 'on', 'order' => 'RANKING']) ->willReturn([]); $this->deezerApi->search('bohemian', true, 'RANKING'); } From ab0a97a753b8aa0683b2dc134cb58a10f2e5d209 Mon Sep 17 00:00:00 2001 From: Martin de Keijzer Date: Thu, 10 Nov 2022 15:40:24 +0100 Subject: [PATCH 4/4] Append query string to URL --- src/DeezerAPIClient.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/DeezerAPIClient.php b/src/DeezerAPIClient.php index 9014d05..db15814 100644 --- a/src/DeezerAPIClient.php +++ b/src/DeezerAPIClient.php @@ -89,6 +89,7 @@ public function apiRequest(string $method, string $service, array $headers = [], } $query['access_token'] = $this->accessToken; + $url.= '?'.http_build_query($query); try { $response = $this->httpClient->request($method, $url, [