Skip to content
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

Deprecate AbstractApi::post() #383

Merged
merged 30 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
f8bd205
Improve tests for Version::create()
Art4 Feb 16, 2024
b72fc4a
Improve tests for User::create()
Art4 Feb 17, 2024
9f71d86
fix return type on User::create()
Art4 Feb 17, 2024
c30bc8b
Improve tests for TimeEntry::create()
Art4 Feb 17, 2024
e05c18e
Improve tests for Project::create()
Art4 Feb 19, 2024
0ee55e0
Improve tests for Membership::create()
Art4 Feb 19, 2024
c93af7e
Add test scenario for creating an issue with minimal data
Art4 Feb 20, 2024
f02e383
Add test scenario for creating issue relations
Art4 Feb 20, 2024
ef18d86
throw MissingParameterException in IssueRelation::create()
Art4 Feb 20, 2024
9b99d84
Improve tests for IssueCategory::create()
Art4 Feb 20, 2024
731caae
Improve tests for Issue::create()
Art4 Feb 21, 2024
dfedb47
Add tests for creating issues with and without parameters
Art4 Feb 22, 2024
baea978
Move tests for Issue::create() in CreateTest
Art4 Feb 22, 2024
95694d0
Improve tests for Issue::addWatcher()
Art4 Feb 22, 2024
e0533fa
Restore tests for create with old client for BC
Art4 Feb 22, 2024
c11a595
improve code coverage
Art4 Feb 22, 2024
6dcf162
Improve tests for Group::addUser()
Art4 Feb 22, 2024
f930e16
Improve tests for Attachment::upload()
Art4 Feb 22, 2024
24c1767
Improve code coverage in NativeCurlClient
Art4 Feb 22, 2024
660aadf
Create HttpFactory::makeXmlRequest()
Art4 Feb 22, 2024
143f09a
Improve tests for Version::create()
Art4 Feb 22, 2024
bd454c7
Remove old tests for User::create()
Art4 Feb 22, 2024
c635cff
Remove old duplicate tests
Art4 Feb 22, 2024
95cbec4
Remove old duplicate tests, improve covers definition
Art4 Feb 22, 2024
523f2c2
Replace AbstractApi::post() with HttpClient in Version, User, TimeEnt…
Art4 Feb 22, 2024
36d3e8c
Merge branch 'v2.x' into deprecate-abstractapi-post
Art4 Feb 22, 2024
1bf8839
improve code coverage
Art4 Feb 22, 2024
e81ea9f
Improve tests for Attachment, Group, Issue, IssueCategory and IssueRe…
Art4 Feb 26, 2024
9b03c9d
Replace AbstractApi::post() with HttpClient in Attachment, Group, Iss…
Art4 Feb 26, 2024
c516448
Deprecate AbstractApi::post()
Art4 Feb 26, 2024
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- New interface `Redmine\Http\Request` for sending data with new minimalistic HTTP clients.
- New method `Redmine\Api\...::getLastResponse()` to get the last response made by the API class.

### Changed

- Calling `Redmine\Api\IssueRelation::create()` without the mandatory parameter `issue_to_id` throws a `Redmine\Exception\MissingParameterException` instead of returning a error array

### Fixed

- Parameter types for IDs were fixed in API for attachments, groups, issues, project, users and versions.
Expand All @@ -23,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Deprecated

- `Redmine\Api\AbstractApi::get()` is deprecated, use `\Redmine\Http\HttpClient::request()` instead.
- `Redmine\Api\AbstractApi::post()` is deprecated, use `\Redmine\Http\HttpClient::request()` instead.

## [v2.5.0](https://github.com/kbsali/php-redmine-api/compare/v2.4.0...v2.5.0) - 2024-02-05

Expand Down
5 changes: 4 additions & 1 deletion src/Redmine/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Redmine\Api;

use Closure;
use InvalidArgumentException;
use Redmine\Api;
use Redmine\Client\Client;
Expand Down Expand Up @@ -142,13 +141,17 @@ protected function get($path, $decodeIfJson = true)
/**
* Perform the client post() method.
*
* @deprecated since v2.6.0, use `\Redmine\Http\HttpClient::request()` instead
*
* @param string $path
* @param string $data
*
* @return string|SimpleXMLElement|false
*/
protected function post($path, $data)
{
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.6.0, use `\Redmine\Http\HttpClient::request()` instead.', E_USER_DEPRECATED);

$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeRequest(
'POST',
strval($path),
Expand Down
11 changes: 8 additions & 3 deletions src/Redmine/Api/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Redmine\Http\HttpFactory;
use Redmine\Serializer\JsonSerializer;
use Redmine\Serializer\PathSerializer;
use SimpleXMLElement;

/**
* Attachment details.
Expand Down Expand Up @@ -80,10 +81,14 @@ public function download($id)
*/
public function upload($attachment, $params = [])
{
return $this->post(
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeRequest(
'POST',
PathSerializer::create('/uploads.json', $params)->getPath(),
'application/octet-stream',
$attachment
);
));

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

/**
Expand All @@ -93,7 +98,7 @@ public function upload($attachment, $params = [])
*
* @param int $id id of the attachment
*
* @return false|\SimpleXMLElement|string
* @return false|SimpleXMLElement|string
*/
public function remove($id)
{
Expand Down
28 changes: 23 additions & 5 deletions src/Redmine/Api/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,19 @@ public function create(array $params = [])
throw new MissingParameterException('Theses parameters are mandatory: `name`');
}

return $this->post(
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeXmlRequest(
'POST',
'/groups.xml',
XmlSerializer::createFromArray(['group' => $params])->getEncoded()
);
));

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

if ($body === '') {
return $body;
}

return new SimpleXMLElement($body);
}

/**
Expand Down Expand Up @@ -205,14 +214,23 @@ public function remove($id)
* @param int $id id of the group
* @param int $userId id of the user
*
* @return string
* @return SimpleXMLElement|string
*/
public function addUser($id, $userId)
{
return $this->post(
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeXmlRequest(
'POST',
'/groups/' . $id . '/users.xml',
XmlSerializer::createFromArray(['user_id' => $userId])->getEncoded()
);
));

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

if ($body === '') {
return $body;
}

return new SimpleXMLElement($body);
}

/**
Expand Down
37 changes: 30 additions & 7 deletions src/Redmine/Api/Issue.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,24 @@ public function create(array $params = [])
$params = $this->cleanParams($params);
$params = $this->sanitizeParams($defaults, $params);

return $this->post(
// FIXME: Throw exception on missing mandatory parameters
// if (!isset($params['subject']) || !isset($params['project_id']) || !isset($params['tracker_id']) || !isset($params['priority_id']) || !isset($params['status_id'])) {
// throw new MissingParameterException('Theses parameters are mandatory: `subject`, `project_id|project`, `tracker_id|tracker`, `priority_id|priority`, `status_id|status`');
// }

$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeXmlRequest(
'POST',
'/issues.xml',
XmlSerializer::createFromArray(['issue' => $params])->getEncoded()
);
));

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

if ($body === '') {
return $body;
}

return new SimpleXMLElement($body);
}

/**
Expand Down Expand Up @@ -236,21 +250,30 @@ public function update($id, array $params)
* @param int $id
* @param int $watcherUserId
*
* @return false|string
* @return SimpleXMLElement|string
*/
public function addWatcher($id, $watcherUserId)
{
return $this->post(
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeXmlRequest(
'POST',
'/issues/' . urlencode(strval($id)) . '/watchers.xml',
XmlSerializer::createFromArray(['user_id' => urlencode(strval($watcherUserId))])->getEncoded()
);
));

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

if ($body === '') {
return $body;
}

return new SimpleXMLElement($body);
}

/**
* @param int $id
* @param int $watcherUserId
*
* @return false|\SimpleXMLElement|string
* @return false|SimpleXMLElement|string
*/
public function removeWatcher($id, $watcherUserId)
{
Expand Down Expand Up @@ -385,7 +408,7 @@ public function attachMany($id, array $attachments)
*
* @param int $id the issue number
*
* @return false|\SimpleXMLElement|string
* @return false|SimpleXMLElement|string
*/
public function remove($id)
{
Expand Down
16 changes: 13 additions & 3 deletions src/Redmine/Api/IssueCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Redmine\Serializer\JsonSerializer;
use Redmine\Serializer\PathSerializer;
use Redmine\Serializer\XmlSerializer;
use SimpleXMLElement;

/**
* Listing issue categories, creating, editing.
Expand Down Expand Up @@ -161,7 +162,7 @@ public function show($id)
*
* @throws MissingParameterException Missing mandatory parameters
*
* @return string|false
* @return SimpleXMLElement|string
*/
public function create($project, array $params = [])
{
Expand All @@ -177,10 +178,19 @@ public function create($project, array $params = [])
throw new MissingParameterException('Theses parameters are mandatory: `name`');
}

return $this->post(
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeXmlRequest(
'POST',
'/projects/' . $project . '/issue_categories.xml',
XmlSerializer::createFromArray(['issue_category' => $params])->getEncoded()
);
));

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

if ($body === '') {
return $body;
}

return new SimpleXMLElement($body);
}

/**
Expand Down
21 changes: 18 additions & 3 deletions src/Redmine/Api/IssueRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Redmine\Api;

use Redmine\Exception;
use Redmine\Exception\MissingParameterException;
use Redmine\Exception\SerializerException;
use Redmine\Exception\UnexpectedResponseException;
use Redmine\Http\HttpFactory;
Expand Down Expand Up @@ -127,10 +128,17 @@ public function remove($id)
* Create a new issue relation.
*
* @see http://www.redmine.org/projects/redmine/wiki/Rest_IssueRelations#POST
* available $params:
* - issue_to_id (required): the id of the related issue
* - relation_type (required to explicit : default "relates"): the type of relation
* (in: "relates", "duplicates", "duplicated", "blocks", "blocked", "precedes", "follows", "copied_to", "copied_from")
* - delay (optional): the delay for a "precedes" or "follows" relation
*
* @param int $issueId the ID of the issue we are creating the relation on
* @param array $params the new issue relation data
*
* @throws MissingParameterException Missing mandatory parameters
*
* @return array
*/
public function create($issueId, array $params = [])
Expand All @@ -143,11 +151,18 @@ public function create($issueId, array $params = [])

$params = $this->sanitizeParams($defaults, $params);

$response = $this->post(
if (!isset($params['issue_to_id'])) {
throw new MissingParameterException('Theses parameters are mandatory: `issue_to_id`');
}

$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeJsonRequest(
'POST',
'/issues/' . urlencode(strval($issueId)) . '/relations.json',
JsonSerializer::createFromArray(['relation' => $params])->getEncoded()
);
));

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

return JsonSerializer::createFromString($response)->getNormalized();
return JsonSerializer::createFromString($body)->getNormalized();
}
}
17 changes: 14 additions & 3 deletions src/Redmine/Api/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use Redmine\Exception\MissingParameterException;
use Redmine\Exception\SerializerException;
use Redmine\Exception\UnexpectedResponseException;
use Redmine\Http\HttpFactory;
use Redmine\Serializer\XmlSerializer;
use SimpleXMLElement;

/**
* Handling project memberships.
Expand Down Expand Up @@ -92,7 +94,7 @@ public function all($project, array $params = [])
*
* @throws MissingParameterException Missing mandatory parameters
*
* @return string|false
* @return SimpleXMLElement|string
*/
public function create($project, array $params = [])
{
Expand All @@ -106,10 +108,19 @@ public function create($project, array $params = [])
throw new MissingParameterException('Theses parameters are mandatory: `user_id`, `role_ids`');
}

return $this->post(
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeXmlRequest(
'POST',
'/projects/' . $project . '/memberships.xml',
XmlSerializer::createFromArray(['membership' => $params])->getEncoded()
);
));

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

if ('' !== $body) {
return new SimpleXMLElement($body);
}

return $body;
}

/**
Expand Down
15 changes: 12 additions & 3 deletions src/Redmine/Api/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function show($id, array $params = [])
*
* @throws MissingParameterException
*
* @return string|SimpleXMLElement|false
* @return SimpleXMLElement|string
*/
public function create(array $params = [])
{
Expand All @@ -180,10 +180,19 @@ public function create(array $params = [])
throw new MissingParameterException('Theses parameters are mandatory: `name`, `identifier`');
}

return $this->post(
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeXmlRequest(
'POST',
'/projects.xml',
XmlSerializer::createFromArray(['project' => $params])->getEncoded()
);
));

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

if ('' !== $body) {
return new SimpleXMLElement($body);
}

return $body;
}

/**
Expand Down
16 changes: 13 additions & 3 deletions src/Redmine/Api/TimeEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Redmine\Http\HttpFactory;
use Redmine\Serializer\JsonSerializer;
use Redmine\Serializer\XmlSerializer;
use SimpleXMLElement;

/**
* Listing time entries, creating, editing.
Expand Down Expand Up @@ -111,7 +112,7 @@ public function show($id)
*
* @throws MissingParameterException Missing mandatory parameters
*
* @return string|false
* @return SimpleXMLElement|string
*/
public function create(array $params = [])
{
Expand All @@ -132,10 +133,19 @@ public function create(array $params = [])
throw new MissingParameterException('Theses parameters are mandatory: `issue_id` or `project_id`, `hours`');
}

return $this->post(
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeXmlRequest(
'POST',
'/time_entries.xml',
XmlSerializer::createFromArray(['time_entry' => $params])->getEncoded()
);
));

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

if ('' !== $body) {
return new SimpleXMLElement($body);
}

return $body;
}

/**
Expand Down
Loading
Loading