Skip to content

Add Request interface and rename Response::getBody() #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 104 additions & 25 deletions src/Redmine/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Redmine\Exception;
use Redmine\Exception\SerializerException;
use Redmine\Http\HttpClient;
use Redmine\Http\Request;
use Redmine\Http\Response;
use Redmine\Serializer\JsonSerializer;
use Redmine\Serializer\PathSerializer;
Expand Down Expand Up @@ -108,9 +109,13 @@ public function lastCallFailed()
*/
protected function get($path, $decodeIfJson = true)
{
$this->lastResponse = $this->getHttpClient()->request('GET', strval($path));
$this->lastResponse = $this->getHttpClient()->request($this->createRequest(
'GET',
strval($path),
$this->getContentTypeFromPath(strval($path))
));

$body = $this->lastResponse->getBody();
$body = $this->lastResponse->getContent();
$contentType = $this->lastResponse->getContentType();

// if response is XML, return a SimpleXMLElement object
Expand Down Expand Up @@ -139,9 +144,14 @@ protected function get($path, $decodeIfJson = true)
*/
protected function post($path, $data)
{
$this->lastResponse = $this->getHttpClient()->request('POST', strval($path), $data);

$body = $this->lastResponse->getBody();
$this->lastResponse = $this->getHttpClient()->request($this->createRequest(
'POST',
strval($path),
$this->getContentTypeFromPath(strval($path)),
$data
));

$body = $this->lastResponse->getContent();
$contentType = $this->lastResponse->getContentType();

// if response is XML, return a SimpleXMLElement object
Expand All @@ -162,9 +172,14 @@ protected function post($path, $data)
*/
protected function put($path, $data)
{
$this->lastResponse = $this->getHttpClient()->request('PUT', strval($path), $data);

$body = $this->lastResponse->getBody();
$this->lastResponse = $this->getHttpClient()->request($this->createRequest(
'PUT',
strval($path),
$this->getContentTypeFromPath(strval($path)),
$data
));

$body = $this->lastResponse->getContent();
$contentType = $this->lastResponse->getContentType();

// if response is XML, return a SimpleXMLElement object
Expand All @@ -184,9 +199,13 @@ protected function put($path, $data)
*/
protected function delete($path)
{
$this->lastResponse = $this->getHttpClient()->request('DELETE', strval($path));
$this->lastResponse = $this->getHttpClient()->request($this->createRequest(
'DELETE',
strval($path),
$this->getContentTypeFromPath(strval($path))
));

return $this->lastResponse->getBody();
return $this->lastResponse->getContent();
}

/**
Expand Down Expand Up @@ -234,7 +253,7 @@ protected function retrieveAll($endpoint, array $params = [])
try {
$data = $this->retrieveData(strval($endpoint), $params);
} catch (Exception $e) {
if ($this->getLastResponse()->getBody() === '') {
if ($this->getLastResponse()->getContent() === '') {
return false;
}

Expand All @@ -258,7 +277,11 @@ protected function retrieveAll($endpoint, array $params = [])
protected function retrieveData(string $endpoint, array $params = []): array
{
if (empty($params)) {
$this->lastResponse = $this->getHttpClient()->request('GET', strval($endpoint));
$this->lastResponse = $this->getHttpClient()->request($this->createRequest(
'GET',
strval($endpoint),
$this->getContentTypeFromPath(strval($endpoint))
));

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

$this->lastResponse = $this->getHttpClient()->request(
$this->lastResponse = $this->getHttpClient()->request($this->createRequest(
'GET',
PathSerializer::create($endpoint, $params)->getPath()
);
PathSerializer::create($endpoint, $params)->getPath(),
$this->getContentTypeFromPath($endpoint)
));

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

Expand Down Expand Up @@ -368,7 +392,7 @@ protected function attachCustomFieldXML(SimpleXMLElement $xml, array $fields)
*/
private function getResponseAsArray(Response $response): array
{
$body = $response->getBody();
$body = $response->getContent();
$contentType = $response->getContentType();
$returnData = null;

Expand Down Expand Up @@ -400,16 +424,16 @@ public function __construct(Client $client, Closure $responseFactory)
$this->responseFactory = $responseFactory;
}

public function request(string $method, string $path, string $body = ''): Response
public function request(Request $request): Response
{
if ($method === 'POST') {
$this->client->requestPost($path, $body);
} elseif ($method === 'PUT') {
$this->client->requestPut($path, $body);
} elseif ($method === 'DELETE') {
$this->client->requestDelete($path);
if ($request->getMethod() === 'POST') {
$this->client->requestPost($request->getPath(), $request->getContent());
} elseif ($request->getMethod() === 'PUT') {
$this->client->requestPut($request->getPath(), $request->getContent());
} elseif ($request->getMethod() === 'DELETE') {
$this->client->requestDelete($request->getPath());
} else {
$this->client->requestGet($path);
$this->client->requestGet($request->getPath());
}

return ($this->responseFactory)(
Expand Down Expand Up @@ -445,10 +469,65 @@ public function getContentType(): string
return $this->contentType;
}

public function getBody(): string
public function getContent(): string
{
return $this->body;
}
};
}

private function createRequest(string $method, string $path, string $contentType, string $content = ''): Request
{
return new class ($method, $path, $contentType, $content) implements Request {
private $method;
private $path;
private $contentType;
private $content;

public function __construct(string $method, string $path, string $contentType, string $content)
{
$this->method = $method;
$this->path = $path;
$this->contentType = $contentType;
$this->content = $content;
}

public function getMethod(): string
{
return $this->method;
}

public function getPath(): string
{
return $this->path;
}

public function getContentType(): string
{
return $this->contentType;
}

public function getContent(): string
{
return $this->content;
}
};
}

private function getContentTypeFromPath(string $path): string
{
$tmp = parse_url($path);

$path = strtolower($path);

if (false !== strpos($path, '/uploads.json') || false !== strpos($path, '/uploads.xml')) {
return 'application/octet-stream';
} elseif ('json' === substr($tmp['path'], -4)) {
return 'application/json';
} elseif ('xml' === substr($tmp['path'], -3)) {
return 'application/xml';
} else {
return '';
}
}
}
2 changes: 1 addition & 1 deletion src/Redmine/Api/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function all(array $params = [])
try {
$this->customFields = $this->list($params);
} catch (Exception $e) {
if ($this->getLastResponse()->getBody() === '') {
if ($this->getLastResponse()->getContent() === '') {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function all(array $params = [])
try {
$this->groups = $this->list($params);
} catch (Exception $e) {
if ($this->getLastResponse()->getBody() === '') {
if ($this->getLastResponse()->getContent() === '') {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/Issue.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function all(array $params = [])
try {
return $this->list($params);
} catch (Exception $e) {
if ($this->getLastResponse()->getBody() === '') {
if ($this->getLastResponse()->getContent() === '') {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/IssueCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function all($project, array $params = [])
try {
return $this->listByProject(strval($project), $params);
} catch (Exception $e) {
if ($this->getLastResponse()->getBody() === '') {
if ($this->getLastResponse()->getContent() === '') {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/IssuePriority.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function all(array $params = [])
try {
$this->issuePriorities = $this->list($params);
} catch (Exception $e) {
if ($this->getLastResponse()->getBody() === '') {
if ($this->getLastResponse()->getContent() === '') {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/IssueRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function all($issueId, array $params = [])
try {
$this->relations = $this->listByIssueId($issueId, $params);
} catch (Exception $e) {
if ($this->getLastResponse()->getBody() === '') {
if ($this->getLastResponse()->getContent() === '') {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/IssueStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function all(array $params = [])
try {
$this->issueStatuses = $this->list($params);
} catch (Exception $e) {
if ($this->getLastResponse()->getBody() === '') {
if ($this->getLastResponse()->getContent() === '') {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function all($project, array $params = [])
try {
$this->memberships = $this->listByProject(strval($project), $params);
} catch (Exception $e) {
if ($this->getLastResponse()->getBody() === '') {
if ($this->getLastResponse()->getContent() === '') {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function all($project = null, array $params = [])
$this->news = $this->listByProject(strval($project), $params);
}
} catch (Exception $e) {
if ($this->getLastResponse()->getBody() === '') {
if ($this->getLastResponse()->getContent() === '') {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function all(array $params = [])
try {
$this->projects = $this->list($params);
} catch (Exception $e) {
if ($this->getLastResponse()->getBody() === '') {
if ($this->getLastResponse()->getContent() === '') {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function all(array $params = [])
try {
$this->query = $this->list($params);
} catch (Exception $e) {
if ($this->getLastResponse()->getBody() === '') {
if ($this->getLastResponse()->getContent() === '') {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function all(array $params = [])
try {
$this->roles = $this->list($params);
} catch (Exception $e) {
if ($this->getLastResponse()->getBody() === '') {
if ($this->getLastResponse()->getContent() === '') {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function search($query, array $params = [])
try {
$this->results = $this->listByQuery($query, $params);
} catch (Exception $e) {
if ($this->getLastResponse()->getBody() === '') {
if ($this->getLastResponse()->getContent() === '') {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/TimeEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function all(array $params = [])
try {
$this->timeEntries = $this->list($params);
} catch (Exception $e) {
if ($this->getLastResponse()->getBody() === '') {
if ($this->getLastResponse()->getContent() === '') {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/TimeEntryActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function all(array $params = [])
try {
$this->timeEntryActivities = $this->list($params);
} catch (Exception $e) {
if ($this->getLastResponse()->getBody() === '') {
if ($this->getLastResponse()->getContent() === '') {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/Tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function all(array $params = [])
try {
$this->trackers = $this->list($params);
} catch (Exception $e) {
if ($this->getLastResponse()->getBody() === '') {
if ($this->getLastResponse()->getContent() === '') {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function all(array $params = [])
try {
$this->users = $this->list($params);
} catch (Exception $e) {
if ($this->getLastResponse()->getBody() === '') {
if ($this->getLastResponse()->getContent() === '') {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function all($project, array $params = [])
try {
$this->versions = $this->listByProject(strval($project), $params);
} catch (Exception $e) {
if ($this->getLastResponse()->getBody() === '') {
if ($this->getLastResponse()->getContent() === '') {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redmine/Api/Wiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function all($project, array $params = [])
try {
$this->wikiPages = $this->listByProject(strval($project), $params);
} catch (Exception $e) {
if ($this->getLastResponse()->getBody() === '') {
if ($this->getLastResponse()->getContent() === '') {
return false;
}

Expand Down
Loading