Skip to content

Commit 349577c

Browse files
authored
fixed old style form request (#24)
* implemented switch between json and form contenttype * bump version
1 parent bbcdf9d commit 349577c

File tree

5 files changed

+36
-35
lines changed

5 files changed

+36
-35
lines changed

src/Challonge/Challonge.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Challonge
1414
* ChallongePHP version.
1515
* Required to pass into Challonge.
1616
*/
17-
protected string $version = '4.0';
17+
protected string $version = '4.1';
1818

1919
/**
2020
* PSR-18 compatible HTTP client wrapped in our wrapper.
@@ -53,7 +53,7 @@ public function getClient(): ClientWrapper
5353
* @throws \JsonException
5454
*/
5555
public function getTournaments(): Collection {
56-
$response = $this->client->request('GET', 'tournaments');
56+
$response = $this->client->request('GET', 'tournaments', 'form');
5757
return Collection::make($response)
5858
->map(fn (array $tournament) => Tournament::fromResponse($this->client, $tournament['tournament']));
5959
}
@@ -72,7 +72,7 @@ public function getTournaments(): Collection {
7272
*/
7373
public function createTournament(array $options = []): Tournament
7474
{
75-
$response = $this->client->request('POST', 'tournaments', $this->client->mapOptions($options, 'tournament'));
75+
$response = $this->client->request('POST', 'tournaments', 'form', $this->client->mapOptions($options, 'tournament'));
7676
return Tournament::fromResponse($this->client, $response['tournament']);
7777
}
7878

@@ -90,7 +90,7 @@ public function createTournament(array $options = []): Tournament
9090
*/
9191
public function fetchTournament(string $tournament): Tournament
9292
{
93-
$response = $this->client->request('GET', "tournaments/{$tournament}");
93+
$response = $this->client->request('GET', "tournaments/{$tournament}", 'form');
9494
return Tournament::fromResponse($this->client, $response['tournament']);
9595
}
9696

@@ -108,7 +108,7 @@ public function fetchTournament(string $tournament): Tournament
108108
*/
109109
public function deleteTournament(string $tournament): Tournament
110110
{
111-
$response = $this->client->request('DELETE', "tournaments/{$tournament}");
111+
$response = $this->client->request('DELETE', "tournaments/{$tournament}", 'form');
112112
return Tournament::fromResponse($this->client, $response['tournament']);
113113
}
114114

@@ -126,7 +126,7 @@ public function deleteTournament(string $tournament): Tournament
126126
*/
127127
public function getParticipants(string $tournament): Collection
128128
{
129-
$response = $this->client->request('GET', "tournaments/{$tournament}/participants");
129+
$response = $this->client->request('GET', "tournaments/{$tournament}/participants", 'form');
130130
return Collection::make($response)
131131
->map(fn (array $participant) => Participant::fromResponse($this->client, $participant['participant']));
132132
}
@@ -145,7 +145,7 @@ public function getParticipants(string $tournament): Collection
145145
*/
146146
public function randomizeParticipants(string $tournament): Collection
147147
{
148-
$response = $this->client->request('POST', "tournaments/{$tournament}/participants/randomize");
148+
$response = $this->client->request('POST', "tournaments/{$tournament}/participants/randomize", 'form');
149149
return Collection::make($response)
150150
->map(fn (array $participant) => Participant::fromResponse($this->client, $participant['participant']));
151151
}
@@ -165,7 +165,7 @@ public function randomizeParticipants(string $tournament): Collection
165165
*/
166166
public function getParticipant(string $tournament, int $participant): Participant
167167
{
168-
$response = $this->client->request('GET', "tournaments/{$tournament}/participants/{$participant}");
168+
$response = $this->client->request('GET', "tournaments/{$tournament}/participants/{$participant}", 'form');
169169
return Participant::fromResponse($this->client, $response['participant']);
170170
}
171171

@@ -183,7 +183,7 @@ public function getParticipant(string $tournament, int $participant): Participan
183183
*/
184184
public function getMatches(string $tournament): Collection
185185
{
186-
$response = $this->client->request('GET', "tournaments/{$tournament}/matches");
186+
$response = $this->client->request('GET', "tournaments/{$tournament}/matches", 'form');
187187
return Collection::make($response)
188188
->map(fn (array $match) => MatchDto::fromResponse($this->client, $match['match']));
189189
}
@@ -203,7 +203,7 @@ public function getMatches(string $tournament): Collection
203203
*/
204204
public function getMatch(string $tournament, int $match): MatchDto
205205
{
206-
$response = $this->client->request('GET', "tournaments/{$tournament}/matches/{$match}");
206+
$response = $this->client->request('GET', "tournaments/{$tournament}/matches/{$match}", 'form');
207207
return MatchDto::fromResponse($this->client, $response['match']);
208208
}
209209

src/Challonge/ClientWrapper.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function __construct(ClientInterface $client, string $key, string $versio
4040
* Make a request to Challonge via the HTTP client.
4141
* @param string $method
4242
* @param string $uri
43+
* @param string $contenttype
4344
* @param array $content
4445
* @return array
4546
* @throws InvalidFormatException
@@ -51,15 +52,15 @@ public function __construct(ClientInterface $client, string $key, string $versio
5152
* @throws ValidationException
5253
* @throws ClientExceptionInterface
5354
*/
54-
public function request(string $method, string $uri, array $content = []): array
55+
public function request(string $method, string $uri, string $contenttype, array $content = []): array
5556
{
5657
$base_uri = "https://api.challonge.com/v1/{$uri}.json?api_key={$this->getKey()}";
5758

5859
$request = new Request(
5960
$method,
6061
$base_uri,
61-
$this->buildHeaders(),
62-
json_encode($content),
62+
$this->buildHeaders($contenttype),
63+
($contenttype == "json") ? json_encode($content) : \http_build_query($content, '', '&'),
6364
);
6465
$response = $this->client->sendRequest($request);
6566

@@ -70,11 +71,11 @@ public function request(string $method, string $uri, array $content = []): array
7071
* Build any headers the requests need.
7172
* @return array
7273
*/
73-
protected function buildHeaders(): array
74+
protected function buildHeaders(string $contenttype): array
7475
{
7576
return [
7677
'Accept' => 'application/json',
77-
'Content-Type' => 'application/json',
78+
'Content-Type' => ($contenttype == "json") ? 'application/json' : 'application/x-www-form-urlencoded',
7879
'User-Agent' => "ChallongePHP/{$this->version} ChallongePHP (https://github.com/teamreflex/ChallongePHP, {$this->version})",
7980
];
8081
}

src/Challonge/DTO/MatchDto.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class MatchDto extends DataTransferObject
6464
*/
6565
public function update(array $options = []): MatchDto
6666
{
67-
$response = $this->client->request('PUT', "tournaments/{$this->tournament_id}/matches/{$this->id}", $this->client->mapOptions($options, 'match'));
67+
$response = $this->client->request('PUT', "tournaments/{$this->tournament_id}/matches/{$this->id}", 'form', $this->client->mapOptions($options, 'match'));
6868
return self::fromResponse($this->client, $response['match']);
6969
}
7070

@@ -81,7 +81,7 @@ public function update(array $options = []): MatchDto
8181
*/
8282
public function reopen(): MatchDto
8383
{
84-
$response = $this->client->request('POST', "tournaments/{$this->tournament_id}/matches/{$this->id}/reopen");
84+
$response = $this->client->request('POST', "tournaments/{$this->tournament_id}/matches/{$this->id}/reopen", 'form');
8585
return self::fromResponse($this->client, $response['match']);
8686
}
8787

@@ -98,7 +98,7 @@ public function reopen(): MatchDto
9898
*/
9999
public function markAsUnderway(): MatchDto
100100
{
101-
$response = $this->client->request('POST', "tournaments/{$this->tournament_id}/matches/{$this->id}/mark_as_underway");
101+
$response = $this->client->request('POST', "tournaments/{$this->tournament_id}/matches/{$this->id}/mark_as_underway", 'form');
102102
return self::fromResponse($this->client, $response['match']);
103103
}
104104

@@ -115,7 +115,7 @@ public function markAsUnderway(): MatchDto
115115
*/
116116
public function unmarkAsUnderway(): MatchDto
117117
{
118-
$response = $this->client->request('POST', "tournaments/{$this->tournament_id}/matches/{$this->id}/unmark_as_underway");
118+
$response = $this->client->request('POST', "tournaments/{$this->tournament_id}/matches/{$this->id}/unmark_as_underway", 'form');
119119
return self::fromResponse($this->client, $response['match']);
120120
}
121121
}

src/Challonge/DTO/Participant.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Participant extends DataTransferObject
6666
*/
6767
public function update(array $options = []): Participant
6868
{
69-
$response = $this->client->request('PUT', "tournaments/{$this->tournament_id}/participants/{$this->id}", $this->client->mapOptions($options, 'participant'));
69+
$response = $this->client->request('PUT', "tournaments/{$this->tournament_id}/participants/{$this->id}", 'form', $this->client->mapOptions($options, 'participant'));
7070
return self::fromResponse($this->client, $response['participant']);
7171
}
7272

@@ -83,7 +83,7 @@ public function update(array $options = []): Participant
8383
*/
8484
public function delete(): Participant
8585
{
86-
$response = $this->client->request('DELETE', "tournaments/{$this->tournament_id}/participants/{$this->id}");
86+
$response = $this->client->request('DELETE', "tournaments/{$this->tournament_id}/participants/{$this->id}", 'form');
8787
return self::fromResponse($this->client, $response['participant']);
8888
}
8989

@@ -100,7 +100,7 @@ public function delete(): Participant
100100
*/
101101
public function checkin(): Participant
102102
{
103-
$response = $this->client->request('POST', "tournaments/{$this->tournament_id}/participants/{$this->id}/check_in");
103+
$response = $this->client->request('POST', "tournaments/{$this->tournament_id}/participants/{$this->id}/check_in", 'form');
104104
return self::fromResponse($this->client, $response['participant']);
105105
}
106106

@@ -117,7 +117,7 @@ public function checkin(): Participant
117117
*/
118118
public function undoCheckin(): Participant
119119
{
120-
$response = $this->client->request('POST', "tournaments/{$this->tournament_id}/participants/{$this->id}/undo_check_in");
120+
$response = $this->client->request('POST', "tournaments/{$this->tournament_id}/participants/{$this->id}/undo_check_in", 'form');
121121
return self::fromResponse($this->client, $response['participant']);
122122
}
123123
}

src/Challonge/DTO/Tournament.php

+12-12
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function start(): Tournament
126126
throw new AlreadyStartedException('Tournament is already underway.');
127127
}
128128

129-
$response = $this->client->request('POST', "tournaments/{$this->id}/start");
129+
$response = $this->client->request('POST', "tournaments/{$this->id}/start", 'form');
130130
return self::fromResponse($this->client, $response['tournament']);
131131
}
132132

@@ -148,7 +148,7 @@ public function finalize(): Tournament
148148
throw new StillRunningException('Tournament is still running.');
149149
}
150150

151-
$response = $this->client->request('POST', "tournaments/{$this->id}/finalize");
151+
$response = $this->client->request('POST', "tournaments/{$this->id}/finalize", 'form');
152152
return self::fromResponse($this->client, $response['tournament']);
153153
}
154154

@@ -165,7 +165,7 @@ public function finalize(): Tournament
165165
*/
166166
public function reset(): Tournament
167167
{
168-
$response = $this->client->request('POST', "tournaments/{$this->id}/reset");
168+
$response = $this->client->request('POST', "tournaments/{$this->id}/reset", 'form');
169169
return self::fromResponse($this->client, $response['tournament']);
170170
}
171171

@@ -183,7 +183,7 @@ public function reset(): Tournament
183183
*/
184184
public function update(array $options = []): Tournament
185185
{
186-
$response = $this->client->request('PUT', "tournaments/{$this->id}", $this->client->mapOptions($options, 'tournament'));
186+
$response = $this->client->request('PUT', "tournaments/{$this->id}", 'form', $this->client->mapOptions($options, 'tournament'));
187187
return self::fromResponse($this->client, $response['tournament']);
188188
}
189189

@@ -200,7 +200,7 @@ public function update(array $options = []): Tournament
200200
*/
201201
public function delete(): Tournament
202202
{
203-
$response = $this->client->request('DELETE', "tournaments/{$this->id}");
203+
$response = $this->client->request('DELETE', "tournaments/{$this->id}", 'form');
204204
return self::fromResponse($this->client, $response['tournament']);
205205
}
206206

@@ -217,7 +217,7 @@ public function delete(): Tournament
217217
*/
218218
public function clear(): Tournament
219219
{
220-
$response = $this->client->request('POST', "tournaments/{$this->id}/participants/clear");
220+
$response = $this->client->request('POST', "tournaments/{$this->id}/participants/clear", 'form');
221221
return self::fromResponse($this->client, $response['tournament']);
222222
}
223223

@@ -239,7 +239,7 @@ public function processCheckins(): Tournament
239239
throw new AlreadyStartedException('Tournament is already underway.');
240240
}
241241

242-
$response = $this->client->request('POST', "tournaments/{$this->id}/process_check_ins");
242+
$response = $this->client->request('POST', "tournaments/{$this->id}/process_check_ins", 'form');
243243
return self::fromResponse($this->client, $response['tournament']);
244244
}
245245

@@ -261,7 +261,7 @@ public function abortCheckins(): Tournament
261261
throw new AlreadyStartedException('Tournament is already underway.');
262262
}
263263

264-
$response = $this->client->request('POST', "tournaments/{$this->id}/abort_check_in");
264+
$response = $this->client->request('POST', "tournaments/{$this->id}/abort_check_in", 'form');
265265
return self::fromResponse($this->client, $response['tournament']);
266266
}
267267

@@ -279,7 +279,7 @@ public function abortCheckins(): Tournament
279279
*/
280280
public function addParticipant(array $options = []): Participant
281281
{
282-
$response = $this->client->request('POST', "tournaments/{$this->id}/participants", $this->client->mapOptions($options, 'participant'));
282+
$response = $this->client->request('POST', "tournaments/{$this->id}/participants", 'form', $this->client->mapOptions($options, 'participant'));
283283
return Participant::fromResponse($this->client, $response['participant']);
284284
}
285285

@@ -304,7 +304,7 @@ public function bulkAddParticipant(array $options = []): Collection
304304
];
305305
}
306306

307-
$response = $this->client->request('POST', "tournaments/{$this->id}/participants/bulk_add", $options);
307+
$response = $this->client->request('POST', "tournaments/{$this->id}/participants/bulk_add", 'json', $options);
308308
return Collection::make($response)
309309
->map(fn (array $participant) => Participant::fromResponse($this->client, $participant['participant']));
310310
}
@@ -323,7 +323,7 @@ public function bulkAddParticipant(array $options = []): Collection
323323
*/
324324
public function deleteParticipant(int $id): Participant
325325
{
326-
$response = $this->client->request('DELETE', "tournaments/{$this->id}/participants/{$id}");
326+
$response = $this->client->request('DELETE', "tournaments/{$this->id}/participants/{$id}", 'form');
327327
return Participant::fromResponse($this->client, $response['participant']);
328328
}
329329

@@ -342,7 +342,7 @@ public function deleteParticipant(int $id): Participant
342342
*/
343343
public function updateParticipant(int $id, array $options = []): Participant
344344
{
345-
$response = $this->client->request('PUT', "tournaments/{$this->id}/participants/{$id}", $this->client->mapOptions($options, 'participant'));
345+
$response = $this->client->request('PUT', "tournaments/{$this->id}/participants/{$id}", 'form', $this->client->mapOptions($options, 'participant'));
346346
return Participant::fromResponse($this->client, $response['participant']);
347347
}
348348
}

0 commit comments

Comments
 (0)