Skip to content

Commit

Permalink
Switch to JSON form submission (#22)
Browse files Browse the repository at this point in the history
* switch from query params to json form body

* update guzzle

* update version
  • Loading branch information
kylekz committed Aug 3, 2022
1 parent fc55801 commit bbcdf9d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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]: <http://api.challonge.com/v1>
30 changes: 15 additions & 15 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Challonge/Challonge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 18 additions & 2 deletions src/Challonge/ClientWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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})",
];
}
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion src/Challonge/DTO/Tournament.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']));
}
Expand Down
5 changes: 4 additions & 1 deletion tests/TournamentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit bbcdf9d

Please sign in to comment.