From bbcdf9d18c0effebd1efe2e899954bcf8a864575 Mon Sep 17 00:00:00 2001 From: Kyle <450516+kylewardnz@users.noreply.github.com> Date: Wed, 3 Aug 2022 15:41:29 +1200 Subject: [PATCH] Switch to JSON form submission (#22) * switch from query params to json form body * update guzzle * update version --- README.md | 4 ++-- composer.lock | 30 +++++++++++++++--------------- src/Challonge/Challonge.php | 2 +- src/Challonge/ClientWrapper.php | 20 ++++++++++++++++++-- src/Challonge/DTO/Tournament.php | 9 ++++++++- tests/TournamentTest.php | 5 ++++- 6 files changed, 48 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index e975ee3..3e32a6d 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Refer to the table for PHP version compatibility: | ChallongePHP Ver. | Compatible PHP | |----------|-------------| -| ^4.0 | 8.0 -8.1 | +| ^4.0 | 8.0 - 8.1 | | ^3.0 | 7.4 - 8.0 | | ^2.1 | 7.4 | | ^2.0 | 7.4 | @@ -68,6 +68,6 @@ As the package is fully type-hinted, everything should be self documenting, howe ## Contact - [@Reflexgg](http://twitter.com/Reflexgg) -- [@Kairuxo](http://twitter.com/Kairuxo) +- [@rfxkairu](http://twitter.com/rfxkairu) [Challonge]: diff --git a/composer.lock b/composer.lock index 58bc5dc..9bec4ca 100644 --- a/composer.lock +++ b/composer.lock @@ -8,7 +8,7 @@ "packages": [ { "name": "illuminate/collections", - "version": "v9.22.1", + "version": "v9.23.0", "source": { "type": "git", "url": "https://github.com/illuminate/collections.git", @@ -63,16 +63,16 @@ }, { "name": "illuminate/conditionable", - "version": "v9.22.1", + "version": "v9.23.0", "source": { "type": "git", "url": "https://github.com/illuminate/conditionable.git", - "reference": "46b7beed47948bd2e67f523d0a76daa62775031e" + "reference": "5b40f51ccb07e0e7b1ec5559d8db9e0e2dc51883" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/conditionable/zipball/46b7beed47948bd2e67f523d0a76daa62775031e", - "reference": "46b7beed47948bd2e67f523d0a76daa62775031e", + "url": "https://api.github.com/repos/illuminate/conditionable/zipball/5b40f51ccb07e0e7b1ec5559d8db9e0e2dc51883", + "reference": "5b40f51ccb07e0e7b1ec5559d8db9e0e2dc51883", "shasum": "" }, "require": { @@ -105,11 +105,11 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2022-05-31T14:47:50+00:00" + "time": "2022-07-29T19:44:19+00:00" }, { "name": "illuminate/contracts", - "version": "v9.22.1", + "version": "v9.23.0", "source": { "type": "git", "url": "https://github.com/illuminate/contracts.git", @@ -157,7 +157,7 @@ }, { "name": "illuminate/macroable", - "version": "v9.22.1", + "version": "v9.23.0", "source": { "type": "git", "url": "https://github.com/illuminate/macroable.git", @@ -3523,16 +3523,16 @@ }, { "name": "symfony/http-client", - "version": "v5.4.9", + "version": "v5.4.11", "source": { "type": "git", "url": "https://github.com/symfony/http-client.git", - "reference": "dc0b15e42b762c040761c1eb9ce86a55d47cf672" + "reference": "5c5c37eb2a276d8d7d669dd76688aa1606ee78fb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/http-client/zipball/dc0b15e42b762c040761c1eb9ce86a55d47cf672", - "reference": "dc0b15e42b762c040761c1eb9ce86a55d47cf672", + "url": "https://api.github.com/repos/symfony/http-client/zipball/5c5c37eb2a276d8d7d669dd76688aa1606ee78fb", + "reference": "5c5c37eb2a276d8d7d669dd76688aa1606ee78fb", "shasum": "" }, "require": { @@ -3590,7 +3590,7 @@ "description": "Provides powerful methods to fetch HTTP resources synchronously or asynchronously", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/http-client/tree/v5.4.9" + "source": "https://github.com/symfony/http-client/tree/v5.4.11" }, "funding": [ { @@ -3606,7 +3606,7 @@ "type": "tidelift" } ], - "time": "2022-05-21T08:57:05+00:00" + "time": "2022-07-28T13:33:28+00:00" }, { "name": "symfony/http-client-contracts", @@ -4119,5 +4119,5 @@ "php": "^8.0 | ^8.1" }, "platform-dev": [], - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.3.0" } diff --git a/src/Challonge/Challonge.php b/src/Challonge/Challonge.php index eb75e8c..7b97f02 100644 --- a/src/Challonge/Challonge.php +++ b/src/Challonge/Challonge.php @@ -14,7 +14,7 @@ class Challonge * ChallongePHP version. * Required to pass into Challonge. */ - protected string $version = '3.1'; + protected string $version = '4.0'; /** * PSR-18 compatible HTTP client wrapped in our wrapper. diff --git a/src/Challonge/ClientWrapper.php b/src/Challonge/ClientWrapper.php index 4402b3d..e7356c9 100644 --- a/src/Challonge/ClientWrapper.php +++ b/src/Challonge/ClientWrapper.php @@ -59,7 +59,7 @@ public function request(string $method, string $uri, array $content = []): array $method, $base_uri, $this->buildHeaders(), - \http_build_query($content, '', '&'), + json_encode($content), ); $response = $this->client->sendRequest($request); @@ -74,7 +74,7 @@ protected function buildHeaders(): array { return [ 'Accept' => 'application/json', - 'Content-Type' => 'application/x-www-form-urlencoded', + 'Content-Type' => 'application/json', 'User-Agent' => "ChallongePHP/{$this->version} ChallongePHP (https://github.com/teamreflex/ChallongePHP, {$this->version})", ]; } @@ -153,6 +153,22 @@ public function setKey(string $key): void $this->key = $key; } + /** + * @return bool + */ + public function getMapOptions(): bool + { + return $this->mapOptions; + } + + /** + * @param bool $mapOptions + */ + public function setMapOptions(bool $mapOptions): void + { + $this->mapOptions = $mapOptions; + } + /** * Get the underlying client. * @return ClientInterface diff --git a/src/Challonge/DTO/Tournament.php b/src/Challonge/DTO/Tournament.php index 4a0342e..10443db 100644 --- a/src/Challonge/DTO/Tournament.php +++ b/src/Challonge/DTO/Tournament.php @@ -297,7 +297,14 @@ public function addParticipant(array $options = []): Participant */ public function bulkAddParticipant(array $options = []): Collection { - $response = $this->client->request('POST', "tournaments/{$this->id}/participants/bulk_add", $this->client->mapOptions($options, 'participant')); + // have to bypass the mapOptions function as the format is a bit different + if ($this->client->getMapOptions()) { + $options = [ + 'participants' => $options, + ]; + } + + $response = $this->client->request('POST', "tournaments/{$this->id}/participants/bulk_add", $options); return Collection::make($response) ->map(fn (array $participant) => Participant::fromResponse($this->client, $participant['participant'])); } diff --git a/tests/TournamentTest.php b/tests/TournamentTest.php index b3728ef..d97aac3 100644 --- a/tests/TournamentTest.php +++ b/tests/TournamentTest.php @@ -192,7 +192,10 @@ public function test_tournament_bulkadd_participant(): void json_decode(file_get_contents(__DIR__ . '/stubs/tournament_fetch.json'), true)['tournament'] ); - $response = $tournament->bulkAddParticipant(); + $response = $tournament->bulkAddParticipant([ + ['name' => 'Team 1'], + ['name' => 'Team 2'], + ]); $this->assertCount(3, $response); }