Skip to content

Commit 599f589

Browse files
authored
Merge pull request #429 from Art4/428-publish-httpfactory
Publish `HttpFactory`
2 parents acb5589 + f6b0626 commit 599f589

File tree

4 files changed

+67
-45
lines changed

4 files changed

+67
-45
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased](https://github.com/kbsali/php-redmine-api/compare/v2.7.0...v2.x)
99

10+
### Added
11+
12+
- New class `Redmine\Http\HttpFactory` to create `Redmine\Http\Request` and `Redmine\Http\Response` instances.
13+
1014
## [v2.7.0](https://github.com/kbsali/php-redmine-api/compare/v2.6.0...v2.7.0) - 2024-07-10
1115

1216
### Added

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ like [Guzzle](https://github.com/guzzle/guzzle) for handling http connections
2222
```
2323
* [low-level API](docs/usage.md#low-level-api) e.g.
2424
```php
25-
$client->requestPost('/issues.json', '{"issue":{"project_id":1,"subject":"issue title"}}');
25+
$response = $client->request(
26+
HttpFactory::makeJsonRequest(
27+
'POST',
28+
'/issues.json',
29+
'{"issue":{"project_id":1,"subject":"issue title"}}',
30+
),
31+
);
2632
```
2733

2834
## Supported Redmine versions

docs/usage.md

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,15 @@ try {
219219

220220
### Mid-level API
221221

222-
You can now use the `getApi()` method to create and get a specific Redmine API. This simplifies common use-cases and gives you some features like caching and assigning a user to an issue by username instead of the user ID.
222+
You can now use the `getApi()` method to create and get a specific Redmine API.
223223

224-
To check for failed requests you can afterwards check the status code via `$client->getLastResponseStatusCode()`.
224+
```php
225+
$api = $client->getApi('issue');
226+
```
227+
228+
This simplifies common use-cases and gives you some features like caching and assigning a user to an issue by username instead of the user ID.
229+
230+
To check for failed requests you can afterwards check the status code via `$api->getLastResponse()->getStatusCode()`.
225231

226232
#### Tracker API
227233

@@ -646,68 +652,76 @@ The low-level API allows you to send highly customized requests to the Redmine s
646652

647653
> :bulb: See the [Redmine REST-API docs](https://www.redmine.org/projects/redmine/wiki/Rest_api) for available endpoints and required parameters.
648654
649-
The client has 4 methods for requests:
655+
The client has a method for requests:
650656

651-
- `requestGet()`
652-
- `requestPost()`
653-
- `requestPut()`
654-
- `requestDelete()`
657+
- `request(\Redmine\Http\Request $request): \Redmine\Http\Response`
655658

656-
Using this methods you can use every Redmine API endpoint. The following example shows you how to rename a project and add a custom field. To build the XML body you can use the `XmlSerializer`.
659+
There is also a `HttpFactory` to create `Request` objects:
660+
661+
- `\Redmine\Http\HttpFactory::makeJsonRequest()` creates a `\Redmine\Http\Request` instance for JSON requests
662+
- `\Redmine\Http\HttpFactory::makeXmlRequest()` creates a `\Redmine\Http\Request` instance for XML requests
663+
664+
Using this method and the `HttpFactory` you can use every Redmine API endpoint. The following example shows you how to rename a project and add a custom field. To build the XML body you can use the `XmlSerializer`.
657665

658666
```php
659-
$client->requestPut(
660-
'/projects/1.xml',
661-
(string) \Redmine\Serializer\XmlSerializer::createFromArray([
662-
'project' => [
663-
'name' => 'renamed project',
664-
'custom_fields' => [
665-
[
666-
'id' => 123,
667-
'name' => 'cf_name',
668-
'field_format' => 'string',
669-
'value' => [1, 2, 3],
667+
$response = $client->request(
668+
\Redmine\Http\HttpFactory::makeXmlRequest(
669+
'PUT',
670+
'/projects/1.xml',
671+
(string) \Redmine\Serializer\XmlSerializer::createFromArray([
672+
'project' => [
673+
'name' => 'renamed project',
674+
'custom_fields' => [
675+
[
676+
'id' => 123,
677+
'name' => 'cf_name',
678+
'field_format' => 'string',
679+
'value' => [1, 2, 3],
680+
],
670681
],
671682
],
672-
]
673-
])
683+
]),
684+
),
674685
);
675686
```
676687

677-
> :bulb: Use `\Redmine\Serializer\JsonSerializer` if you want to use the JSON endpoint.
688+
> :bulb: Use `\Redmine\Serializer\JsonSerializer` and `HttpFactory::makeJsonRequest()` if you want to use the JSON endpoint.
678689
679-
Or to fetch data with complex query parameters you can use `requestGet()` with the `PathSerializer`:
690+
Or to fetch data with complex query parameters you can use the `PathSerializer`:
680691

681692
```php
682-
$client->requestGet(
683-
(string) \Redmine\Serializer\PathSerializer::create(
684-
'/time_entries.json',
685-
[
686-
'f' => ['spent_on'],
687-
'op' => ['spent_on' => '><'],
688-
'v' => [
689-
'spent_on' => [
690-
'2016-01-18',
691-
'2016-01-22',
693+
$response = $client->request(
694+
\Redmine\Http\HttpFactory::makeJsonRequest(
695+
'GET',
696+
(string) \Redmine\Serializer\PathSerializer::create(
697+
'/time_entries.json',
698+
[
699+
'f' => ['spent_on'],
700+
'op' => ['spent_on' => '><'],
701+
'v' => [
702+
'spent_on' => [
703+
'2016-01-18',
704+
'2016-01-22',
705+
],
692706
],
693707
],
694-
],
695-
)
708+
),
709+
),
696710
);
697711
```
698712

699-
After the request you can use these 3 methods to work with the response:
713+
After the request you can use these 3 methods to work with the `\Redmine\Http\Response` instance:
700714

701-
- `getLastResponseStatusCode()`
702-
- `getLastResponseContentType()`
703-
- `getLastResponseBody()`
715+
- `getStatusCode()`
716+
- `getContentType()`
717+
- `getContent()`
704718

705-
To parse the response body from the last example to an array you can use `XmlSerializer`:
719+
To parse the response body to an array you can use `XmlSerializer`:
706720

707721
```php
708-
if ($client->getLastResponseStatusCode() === 200) {
722+
if ($response->getStatusCode() === 200) {
709723
$responseAsArray = \Redmine\Serializer\XmlSerializer::createFromString(
710-
$client->getLastResponseBody()
724+
$response->getContent(),
711725
)->getNormalized();
712726
}
713727
```

src/Redmine/Http/HttpFactory.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
/**
88
* Factory for HTTP objects.
9-
*
10-
* @internal
119
*/
1210
final class HttpFactory
1311
{

0 commit comments

Comments
 (0)