Skip to content

Commit cffff37

Browse files
authored
Merge pull request #383 from Art4/deprecate-abstractapi-post
Deprecate `AbstractApi::post()`
2 parents 3f6474f + c516448 commit cffff37

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+3002
-2208
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- New interface `Redmine\Http\Request` for sending data with new minimalistic HTTP clients.
1414
- New method `Redmine\Api\...::getLastResponse()` to get the last response made by the API class.
1515

16+
### Changed
17+
18+
- Calling `Redmine\Api\IssueRelation::create()` without the mandatory parameter `issue_to_id` throws a `Redmine\Exception\MissingParameterException` instead of returning a error array
19+
1620
### Fixed
1721

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

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

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

src/Redmine/Api/AbstractApi.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Redmine\Api;
44

5-
use Closure;
65
use InvalidArgumentException;
76
use Redmine\Api;
87
use Redmine\Client\Client;
@@ -142,13 +141,17 @@ protected function get($path, $decodeIfJson = true)
142141
/**
143142
* Perform the client post() method.
144143
*
144+
* @deprecated since v2.6.0, use `\Redmine\Http\HttpClient::request()` instead
145+
*
145146
* @param string $path
146147
* @param string $data
147148
*
148149
* @return string|SimpleXMLElement|false
149150
*/
150151
protected function post($path, $data)
151152
{
153+
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.6.0, use `\Redmine\Http\HttpClient::request()` instead.', E_USER_DEPRECATED);
154+
152155
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeRequest(
153156
'POST',
154157
strval($path),

src/Redmine/Api/Attachment.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Redmine\Http\HttpFactory;
77
use Redmine\Serializer\JsonSerializer;
88
use Redmine\Serializer\PathSerializer;
9+
use SimpleXMLElement;
910

1011
/**
1112
* Attachment details.
@@ -80,10 +81,14 @@ public function download($id)
8081
*/
8182
public function upload($attachment, $params = [])
8283
{
83-
return $this->post(
84+
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeRequest(
85+
'POST',
8486
PathSerializer::create('/uploads.json', $params)->getPath(),
87+
'application/octet-stream',
8588
$attachment
86-
);
89+
));
90+
91+
return $this->lastResponse->getContent();
8792
}
8893

8994
/**
@@ -93,7 +98,7 @@ public function upload($attachment, $params = [])
9398
*
9499
* @param int $id id of the attachment
95100
*
96-
* @return false|\SimpleXMLElement|string
101+
* @return false|SimpleXMLElement|string
97102
*/
98103
public function remove($id)
99104
{

src/Redmine/Api/Group.php

+23-5
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,19 @@ public function create(array $params = [])
120120
throw new MissingParameterException('Theses parameters are mandatory: `name`');
121121
}
122122

123-
return $this->post(
123+
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeXmlRequest(
124+
'POST',
124125
'/groups.xml',
125126
XmlSerializer::createFromArray(['group' => $params])->getEncoded()
126-
);
127+
));
128+
129+
$body = $this->lastResponse->getContent();
130+
131+
if ($body === '') {
132+
return $body;
133+
}
134+
135+
return new SimpleXMLElement($body);
127136
}
128137

129138
/**
@@ -205,14 +214,23 @@ public function remove($id)
205214
* @param int $id id of the group
206215
* @param int $userId id of the user
207216
*
208-
* @return string
217+
* @return SimpleXMLElement|string
209218
*/
210219
public function addUser($id, $userId)
211220
{
212-
return $this->post(
221+
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeXmlRequest(
222+
'POST',
213223
'/groups/' . $id . '/users.xml',
214224
XmlSerializer::createFromArray(['user_id' => $userId])->getEncoded()
215-
);
225+
));
226+
227+
$body = $this->lastResponse->getContent();
228+
229+
if ($body === '') {
230+
return $body;
231+
}
232+
233+
return new SimpleXMLElement($body);
216234
}
217235

218236
/**

src/Redmine/Api/Issue.php

+30-7
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,24 @@ public function create(array $params = [])
189189
$params = $this->cleanParams($params);
190190
$params = $this->sanitizeParams($defaults, $params);
191191

192-
return $this->post(
192+
// FIXME: Throw exception on missing mandatory parameters
193+
// if (!isset($params['subject']) || !isset($params['project_id']) || !isset($params['tracker_id']) || !isset($params['priority_id']) || !isset($params['status_id'])) {
194+
// throw new MissingParameterException('Theses parameters are mandatory: `subject`, `project_id|project`, `tracker_id|tracker`, `priority_id|priority`, `status_id|status`');
195+
// }
196+
197+
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeXmlRequest(
198+
'POST',
193199
'/issues.xml',
194200
XmlSerializer::createFromArray(['issue' => $params])->getEncoded()
195-
);
201+
));
202+
203+
$body = $this->lastResponse->getContent();
204+
205+
if ($body === '') {
206+
return $body;
207+
}
208+
209+
return new SimpleXMLElement($body);
196210
}
197211

198212
/**
@@ -236,21 +250,30 @@ public function update($id, array $params)
236250
* @param int $id
237251
* @param int $watcherUserId
238252
*
239-
* @return false|string
253+
* @return SimpleXMLElement|string
240254
*/
241255
public function addWatcher($id, $watcherUserId)
242256
{
243-
return $this->post(
257+
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeXmlRequest(
258+
'POST',
244259
'/issues/' . urlencode(strval($id)) . '/watchers.xml',
245260
XmlSerializer::createFromArray(['user_id' => urlencode(strval($watcherUserId))])->getEncoded()
246-
);
261+
));
262+
263+
$body = $this->lastResponse->getContent();
264+
265+
if ($body === '') {
266+
return $body;
267+
}
268+
269+
return new SimpleXMLElement($body);
247270
}
248271

249272
/**
250273
* @param int $id
251274
* @param int $watcherUserId
252275
*
253-
* @return false|\SimpleXMLElement|string
276+
* @return false|SimpleXMLElement|string
254277
*/
255278
public function removeWatcher($id, $watcherUserId)
256279
{
@@ -385,7 +408,7 @@ public function attachMany($id, array $attachments)
385408
*
386409
* @param int $id the issue number
387410
*
388-
* @return false|\SimpleXMLElement|string
411+
* @return false|SimpleXMLElement|string
389412
*/
390413
public function remove($id)
391414
{

src/Redmine/Api/IssueCategory.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Redmine\Serializer\JsonSerializer;
1212
use Redmine\Serializer\PathSerializer;
1313
use Redmine\Serializer\XmlSerializer;
14+
use SimpleXMLElement;
1415

1516
/**
1617
* Listing issue categories, creating, editing.
@@ -161,7 +162,7 @@ public function show($id)
161162
*
162163
* @throws MissingParameterException Missing mandatory parameters
163164
*
164-
* @return string|false
165+
* @return SimpleXMLElement|string
165166
*/
166167
public function create($project, array $params = [])
167168
{
@@ -177,10 +178,19 @@ public function create($project, array $params = [])
177178
throw new MissingParameterException('Theses parameters are mandatory: `name`');
178179
}
179180

180-
return $this->post(
181+
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeXmlRequest(
182+
'POST',
181183
'/projects/' . $project . '/issue_categories.xml',
182184
XmlSerializer::createFromArray(['issue_category' => $params])->getEncoded()
183-
);
185+
));
186+
187+
$body = $this->lastResponse->getContent();
188+
189+
if ($body === '') {
190+
return $body;
191+
}
192+
193+
return new SimpleXMLElement($body);
184194
}
185195

186196
/**

src/Redmine/Api/IssueRelation.php

+18-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Redmine\Api;
44

55
use Redmine\Exception;
6+
use Redmine\Exception\MissingParameterException;
67
use Redmine\Exception\SerializerException;
78
use Redmine\Exception\UnexpectedResponseException;
89
use Redmine\Http\HttpFactory;
@@ -127,10 +128,17 @@ public function remove($id)
127128
* Create a new issue relation.
128129
*
129130
* @see http://www.redmine.org/projects/redmine/wiki/Rest_IssueRelations#POST
131+
* available $params:
132+
* - issue_to_id (required): the id of the related issue
133+
* - relation_type (required to explicit : default "relates"): the type of relation
134+
* (in: "relates", "duplicates", "duplicated", "blocks", "blocked", "precedes", "follows", "copied_to", "copied_from")
135+
* - delay (optional): the delay for a "precedes" or "follows" relation
130136
*
131137
* @param int $issueId the ID of the issue we are creating the relation on
132138
* @param array $params the new issue relation data
133139
*
140+
* @throws MissingParameterException Missing mandatory parameters
141+
*
134142
* @return array
135143
*/
136144
public function create($issueId, array $params = [])
@@ -143,11 +151,18 @@ public function create($issueId, array $params = [])
143151

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

146-
$response = $this->post(
154+
if (!isset($params['issue_to_id'])) {
155+
throw new MissingParameterException('Theses parameters are mandatory: `issue_to_id`');
156+
}
157+
158+
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeJsonRequest(
159+
'POST',
147160
'/issues/' . urlencode(strval($issueId)) . '/relations.json',
148161
JsonSerializer::createFromArray(['relation' => $params])->getEncoded()
149-
);
162+
));
163+
164+
$body = $this->lastResponse->getContent();
150165

151-
return JsonSerializer::createFromString($response)->getNormalized();
166+
return JsonSerializer::createFromString($body)->getNormalized();
152167
}
153168
}

src/Redmine/Api/Membership.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use Redmine\Exception\MissingParameterException;
88
use Redmine\Exception\SerializerException;
99
use Redmine\Exception\UnexpectedResponseException;
10+
use Redmine\Http\HttpFactory;
1011
use Redmine\Serializer\XmlSerializer;
12+
use SimpleXMLElement;
1113

1214
/**
1315
* Handling project memberships.
@@ -92,7 +94,7 @@ public function all($project, array $params = [])
9294
*
9395
* @throws MissingParameterException Missing mandatory parameters
9496
*
95-
* @return string|false
97+
* @return SimpleXMLElement|string
9698
*/
9799
public function create($project, array $params = [])
98100
{
@@ -106,10 +108,19 @@ public function create($project, array $params = [])
106108
throw new MissingParameterException('Theses parameters are mandatory: `user_id`, `role_ids`');
107109
}
108110

109-
return $this->post(
111+
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeXmlRequest(
112+
'POST',
110113
'/projects/' . $project . '/memberships.xml',
111114
XmlSerializer::createFromArray(['membership' => $params])->getEncoded()
112-
);
115+
));
116+
117+
$body = $this->lastResponse->getContent();
118+
119+
if ('' !== $body) {
120+
return new SimpleXMLElement($body);
121+
}
122+
123+
return $body;
113124
}
114125

115126
/**

src/Redmine/Api/Project.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function show($id, array $params = [])
162162
*
163163
* @throws MissingParameterException
164164
*
165-
* @return string|SimpleXMLElement|false
165+
* @return SimpleXMLElement|string
166166
*/
167167
public function create(array $params = [])
168168
{
@@ -180,10 +180,19 @@ public function create(array $params = [])
180180
throw new MissingParameterException('Theses parameters are mandatory: `name`, `identifier`');
181181
}
182182

183-
return $this->post(
183+
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeXmlRequest(
184+
'POST',
184185
'/projects.xml',
185186
XmlSerializer::createFromArray(['project' => $params])->getEncoded()
186-
);
187+
));
188+
189+
$body = $this->lastResponse->getContent();
190+
191+
if ('' !== $body) {
192+
return new SimpleXMLElement($body);
193+
}
194+
195+
return $body;
187196
}
188197

189198
/**

src/Redmine/Api/TimeEntry.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Redmine\Http\HttpFactory;
1010
use Redmine\Serializer\JsonSerializer;
1111
use Redmine\Serializer\XmlSerializer;
12+
use SimpleXMLElement;
1213

1314
/**
1415
* Listing time entries, creating, editing.
@@ -111,7 +112,7 @@ public function show($id)
111112
*
112113
* @throws MissingParameterException Missing mandatory parameters
113114
*
114-
* @return string|false
115+
* @return SimpleXMLElement|string
115116
*/
116117
public function create(array $params = [])
117118
{
@@ -132,10 +133,19 @@ public function create(array $params = [])
132133
throw new MissingParameterException('Theses parameters are mandatory: `issue_id` or `project_id`, `hours`');
133134
}
134135

135-
return $this->post(
136+
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeXmlRequest(
137+
'POST',
136138
'/time_entries.xml',
137139
XmlSerializer::createFromArray(['time_entry' => $params])->getEncoded()
138-
);
140+
));
141+
142+
$body = $this->lastResponse->getContent();
143+
144+
if ('' !== $body) {
145+
return new SimpleXMLElement($body);
146+
}
147+
148+
return $body;
139149
}
140150

141151
/**

0 commit comments

Comments
 (0)