Skip to content

Commit 2103392

Browse files
authored
Merge pull request #366 from kbsali/363-rename-responsegetbody
Add `Request` interface and rename `Response::getBody()`
2 parents c06b115 + a9fb021 commit 2103392

36 files changed

+629
-513
lines changed

src/Redmine/Api/AbstractApi.php

Lines changed: 104 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Redmine\Exception;
1010
use Redmine\Exception\SerializerException;
1111
use Redmine\Http\HttpClient;
12+
use Redmine\Http\Request;
1213
use Redmine\Http\Response;
1314
use Redmine\Serializer\JsonSerializer;
1415
use Redmine\Serializer\PathSerializer;
@@ -108,9 +109,13 @@ public function lastCallFailed()
108109
*/
109110
protected function get($path, $decodeIfJson = true)
110111
{
111-
$this->lastResponse = $this->getHttpClient()->request('GET', strval($path));
112+
$this->lastResponse = $this->getHttpClient()->request($this->createRequest(
113+
'GET',
114+
strval($path),
115+
$this->getContentTypeFromPath(strval($path))
116+
));
112117

113-
$body = $this->lastResponse->getBody();
118+
$body = $this->lastResponse->getContent();
114119
$contentType = $this->lastResponse->getContentType();
115120

116121
// if response is XML, return a SimpleXMLElement object
@@ -139,9 +144,14 @@ protected function get($path, $decodeIfJson = true)
139144
*/
140145
protected function post($path, $data)
141146
{
142-
$this->lastResponse = $this->getHttpClient()->request('POST', strval($path), $data);
143-
144-
$body = $this->lastResponse->getBody();
147+
$this->lastResponse = $this->getHttpClient()->request($this->createRequest(
148+
'POST',
149+
strval($path),
150+
$this->getContentTypeFromPath(strval($path)),
151+
$data
152+
));
153+
154+
$body = $this->lastResponse->getContent();
145155
$contentType = $this->lastResponse->getContentType();
146156

147157
// if response is XML, return a SimpleXMLElement object
@@ -162,9 +172,14 @@ protected function post($path, $data)
162172
*/
163173
protected function put($path, $data)
164174
{
165-
$this->lastResponse = $this->getHttpClient()->request('PUT', strval($path), $data);
166-
167-
$body = $this->lastResponse->getBody();
175+
$this->lastResponse = $this->getHttpClient()->request($this->createRequest(
176+
'PUT',
177+
strval($path),
178+
$this->getContentTypeFromPath(strval($path)),
179+
$data
180+
));
181+
182+
$body = $this->lastResponse->getContent();
168183
$contentType = $this->lastResponse->getContentType();
169184

170185
// if response is XML, return a SimpleXMLElement object
@@ -184,9 +199,13 @@ protected function put($path, $data)
184199
*/
185200
protected function delete($path)
186201
{
187-
$this->lastResponse = $this->getHttpClient()->request('DELETE', strval($path));
202+
$this->lastResponse = $this->getHttpClient()->request($this->createRequest(
203+
'DELETE',
204+
strval($path),
205+
$this->getContentTypeFromPath(strval($path))
206+
));
188207

189-
return $this->lastResponse->getBody();
208+
return $this->lastResponse->getContent();
190209
}
191210

192211
/**
@@ -234,7 +253,7 @@ protected function retrieveAll($endpoint, array $params = [])
234253
try {
235254
$data = $this->retrieveData(strval($endpoint), $params);
236255
} catch (Exception $e) {
237-
if ($this->getLastResponse()->getBody() === '') {
256+
if ($this->getLastResponse()->getContent() === '') {
238257
return false;
239258
}
240259

@@ -258,7 +277,11 @@ protected function retrieveAll($endpoint, array $params = [])
258277
protected function retrieveData(string $endpoint, array $params = []): array
259278
{
260279
if (empty($params)) {
261-
$this->lastResponse = $this->getHttpClient()->request('GET', strval($endpoint));
280+
$this->lastResponse = $this->getHttpClient()->request($this->createRequest(
281+
'GET',
282+
strval($endpoint),
283+
$this->getContentTypeFromPath(strval($endpoint))
284+
));
262285

263286
return $this->getResponseAsArray($this->lastResponse);
264287
}
@@ -287,10 +310,11 @@ protected function retrieveData(string $endpoint, array $params = []): array
287310
$params['limit'] = $_limit;
288311
$params['offset'] = $offset;
289312

290-
$this->lastResponse = $this->getHttpClient()->request(
313+
$this->lastResponse = $this->getHttpClient()->request($this->createRequest(
291314
'GET',
292-
PathSerializer::create($endpoint, $params)->getPath()
293-
);
315+
PathSerializer::create($endpoint, $params)->getPath(),
316+
$this->getContentTypeFromPath($endpoint)
317+
));
294318

295319
$newDataSet = $this->getResponseAsArray($this->lastResponse);
296320

@@ -368,7 +392,7 @@ protected function attachCustomFieldXML(SimpleXMLElement $xml, array $fields)
368392
*/
369393
private function getResponseAsArray(Response $response): array
370394
{
371-
$body = $response->getBody();
395+
$body = $response->getContent();
372396
$contentType = $response->getContentType();
373397
$returnData = null;
374398

@@ -400,16 +424,16 @@ public function __construct(Client $client, Closure $responseFactory)
400424
$this->responseFactory = $responseFactory;
401425
}
402426

403-
public function request(string $method, string $path, string $body = ''): Response
427+
public function request(Request $request): Response
404428
{
405-
if ($method === 'POST') {
406-
$this->client->requestPost($path, $body);
407-
} elseif ($method === 'PUT') {
408-
$this->client->requestPut($path, $body);
409-
} elseif ($method === 'DELETE') {
410-
$this->client->requestDelete($path);
429+
if ($request->getMethod() === 'POST') {
430+
$this->client->requestPost($request->getPath(), $request->getContent());
431+
} elseif ($request->getMethod() === 'PUT') {
432+
$this->client->requestPut($request->getPath(), $request->getContent());
433+
} elseif ($request->getMethod() === 'DELETE') {
434+
$this->client->requestDelete($request->getPath());
411435
} else {
412-
$this->client->requestGet($path);
436+
$this->client->requestGet($request->getPath());
413437
}
414438

415439
return ($this->responseFactory)(
@@ -445,10 +469,65 @@ public function getContentType(): string
445469
return $this->contentType;
446470
}
447471

448-
public function getBody(): string
472+
public function getContent(): string
449473
{
450474
return $this->body;
451475
}
452476
};
453477
}
478+
479+
private function createRequest(string $method, string $path, string $contentType, string $content = ''): Request
480+
{
481+
return new class ($method, $path, $contentType, $content) implements Request {
482+
private $method;
483+
private $path;
484+
private $contentType;
485+
private $content;
486+
487+
public function __construct(string $method, string $path, string $contentType, string $content)
488+
{
489+
$this->method = $method;
490+
$this->path = $path;
491+
$this->contentType = $contentType;
492+
$this->content = $content;
493+
}
494+
495+
public function getMethod(): string
496+
{
497+
return $this->method;
498+
}
499+
500+
public function getPath(): string
501+
{
502+
return $this->path;
503+
}
504+
505+
public function getContentType(): string
506+
{
507+
return $this->contentType;
508+
}
509+
510+
public function getContent(): string
511+
{
512+
return $this->content;
513+
}
514+
};
515+
}
516+
517+
private function getContentTypeFromPath(string $path): string
518+
{
519+
$tmp = parse_url($path);
520+
521+
$path = strtolower($path);
522+
523+
if (false !== strpos($path, '/uploads.json') || false !== strpos($path, '/uploads.xml')) {
524+
return 'application/octet-stream';
525+
} elseif ('json' === substr($tmp['path'], -4)) {
526+
return 'application/json';
527+
} elseif ('xml' === substr($tmp['path'], -3)) {
528+
return 'application/xml';
529+
} else {
530+
return '';
531+
}
532+
}
454533
}

src/Redmine/Api/CustomField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function all(array $params = [])
5555
try {
5656
$this->customFields = $this->list($params);
5757
} catch (Exception $e) {
58-
if ($this->getLastResponse()->getBody() === '') {
58+
if ($this->getLastResponse()->getContent() === '') {
5959
return false;
6060
}
6161

src/Redmine/Api/Group.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function all(array $params = [])
5959
try {
6060
$this->groups = $this->list($params);
6161
} catch (Exception $e) {
62-
if ($this->getLastResponse()->getBody() === '') {
62+
if ($this->getLastResponse()->getContent() === '') {
6363
return false;
6464
}
6565

src/Redmine/Api/Issue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function all(array $params = [])
110110
try {
111111
return $this->list($params);
112112
} catch (Exception $e) {
113-
if ($this->getLastResponse()->getBody() === '') {
113+
if ($this->getLastResponse()->getContent() === '') {
114114
return false;
115115
}
116116

src/Redmine/Api/IssueCategory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function all($project, array $params = [])
6969
try {
7070
return $this->listByProject(strval($project), $params);
7171
} catch (Exception $e) {
72-
if ($this->getLastResponse()->getBody() === '') {
72+
if ($this->getLastResponse()->getContent() === '') {
7373
return false;
7474
}
7575

src/Redmine/Api/IssuePriority.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function all(array $params = [])
5555
try {
5656
$this->issuePriorities = $this->list($params);
5757
} catch (Exception $e) {
58-
if ($this->getLastResponse()->getBody() === '') {
58+
if ($this->getLastResponse()->getContent() === '') {
5959
return false;
6060
}
6161

src/Redmine/Api/IssueRelation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function all($issueId, array $params = [])
5858
try {
5959
$this->relations = $this->listByIssueId($issueId, $params);
6060
} catch (Exception $e) {
61-
if ($this->getLastResponse()->getBody() === '') {
61+
if ($this->getLastResponse()->getContent() === '') {
6262
return false;
6363
}
6464

src/Redmine/Api/IssueStatus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function all(array $params = [])
5555
try {
5656
$this->issueStatuses = $this->list($params);
5757
} catch (Exception $e) {
58-
if ($this->getLastResponse()->getBody() === '') {
58+
if ($this->getLastResponse()->getContent() === '') {
5959
return false;
6060
}
6161

src/Redmine/Api/Membership.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function all($project, array $params = [])
6868
try {
6969
$this->memberships = $this->listByProject(strval($project), $params);
7070
} catch (Exception $e) {
71-
if ($this->getLastResponse()->getBody() === '') {
71+
if ($this->getLastResponse()->getContent() === '') {
7272
return false;
7373
}
7474

src/Redmine/Api/News.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function all($project = null, array $params = [])
8888
$this->news = $this->listByProject(strval($project), $params);
8989
}
9090
} catch (Exception $e) {
91-
if ($this->getLastResponse()->getBody() === '') {
91+
if ($this->getLastResponse()->getContent() === '') {
9292
return false;
9393
}
9494

src/Redmine/Api/Project.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function all(array $params = [])
5959
try {
6060
$this->projects = $this->list($params);
6161
} catch (Exception $e) {
62-
if ($this->getLastResponse()->getBody() === '') {
62+
if ($this->getLastResponse()->getContent() === '') {
6363
return false;
6464
}
6565

src/Redmine/Api/Query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function all(array $params = [])
5555
try {
5656
$this->query = $this->list($params);
5757
} catch (Exception $e) {
58-
if ($this->getLastResponse()->getBody() === '') {
58+
if ($this->getLastResponse()->getContent() === '') {
5959
return false;
6060
}
6161

src/Redmine/Api/Role.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function all(array $params = [])
5555
try {
5656
$this->roles = $this->list($params);
5757
} catch (Exception $e) {
58-
if ($this->getLastResponse()->getBody() === '') {
58+
if ($this->getLastResponse()->getContent() === '') {
5959
return false;
6060
}
6161

src/Redmine/Api/Search.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function search($query, array $params = [])
5555
try {
5656
$this->results = $this->listByQuery($query, $params);
5757
} catch (Exception $e) {
58-
if ($this->getLastResponse()->getBody() === '') {
58+
if ($this->getLastResponse()->getContent() === '') {
5959
return false;
6060
}
6161

src/Redmine/Api/TimeEntry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function all(array $params = [])
5757
try {
5858
$this->timeEntries = $this->list($params);
5959
} catch (Exception $e) {
60-
if ($this->getLastResponse()->getBody() === '') {
60+
if ($this->getLastResponse()->getContent() === '') {
6161
return false;
6262
}
6363

src/Redmine/Api/TimeEntryActivity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function all(array $params = [])
5151
try {
5252
$this->timeEntryActivities = $this->list($params);
5353
} catch (Exception $e) {
54-
if ($this->getLastResponse()->getBody() === '') {
54+
if ($this->getLastResponse()->getContent() === '') {
5555
return false;
5656
}
5757

src/Redmine/Api/Tracker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function all(array $params = [])
5555
try {
5656
$this->trackers = $this->list($params);
5757
} catch (Exception $e) {
58-
if ($this->getLastResponse()->getBody() === '') {
58+
if ($this->getLastResponse()->getContent() === '') {
5959
return false;
6060
}
6161

src/Redmine/Api/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function all(array $params = [])
5858
try {
5959
$this->users = $this->list($params);
6060
} catch (Exception $e) {
61-
if ($this->getLastResponse()->getBody() === '') {
61+
if ($this->getLastResponse()->getContent() === '') {
6262
return false;
6363
}
6464

src/Redmine/Api/Version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function all($project, array $params = [])
6767
try {
6868
$this->versions = $this->listByProject(strval($project), $params);
6969
} catch (Exception $e) {
70-
if ($this->getLastResponse()->getBody() === '') {
70+
if ($this->getLastResponse()->getContent() === '') {
7171
return false;
7272
}
7373

src/Redmine/Api/Wiki.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function all($project, array $params = [])
6767
try {
6868
$this->wikiPages = $this->listByProject(strval($project), $params);
6969
} catch (Exception $e) {
70-
if ($this->getLastResponse()->getBody() === '') {
70+
if ($this->getLastResponse()->getContent() === '') {
7171
return false;
7272
}
7373

0 commit comments

Comments
 (0)