Skip to content

Commit 7dbfc1c

Browse files
authored
Merge pull request #125 from Laurgrin/SUPPORT-106898
Update util raml generator to allow for 202 response to return a body.
2 parents ce8b8ff + 28fc522 commit 7dbfc1c

File tree

12 files changed

+402
-8
lines changed

12 files changed

+402
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## 11.11.0
8+
### Added
9+
- 202 responses can now return a body.
10+
711
## 11.10.1
812
### Removed
913
- Removed PHP `7.2` and `7.3` from Travis includes

src/Paysera/Bundle/CodeGeneratorBundle/Service/BodyResolver.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Raml\Body;
1010
use Raml\BodyInterface;
1111
use Raml\Method;
12+
use Raml\Response;
1213
use Raml\Types\ArrayType;
1314
use Raml\Types\StringType;
1415

@@ -41,32 +42,31 @@ public function getRequestBody(Method $method)
4142
*/
4243
public function getResponseBody(Method $method)
4344
{
44-
$okResponse = $method->getResponse(StatusCodeInterface::STATUS_OK);
45-
46-
if ($okResponse === null) {
45+
$successfulResponse = $this->getSuccessfulResponse($method);
46+
if ($successfulResponse === null) {
4747
return null;
4848
}
4949

5050
try {
51-
return $okResponse->getBodyByType(self::BODY_JSON);
51+
return $successfulResponse->getBodyByType(self::BODY_JSON);
5252
} catch (Exception $exception) {}
5353

5454
try {
55-
$body = $okResponse->getBodyByType(self::BODY_JAVASCRIPT);
55+
$body = $successfulResponse->getBodyByType(self::BODY_JAVASCRIPT);
5656
$body->setType(new StringType('string'));
5757

5858
return $body;
5959
} catch (Exception $exception) {}
6060

6161
try {
62-
return $okResponse->getBodyByType(self::BODY_OCTET_STREAM);
62+
return $successfulResponse->getBodyByType(self::BODY_OCTET_STREAM);
6363
} catch (Exception $exception) {}
6464

6565
try {
66-
return $okResponse->getBodyByType(self::BODY_TEXT_CSV);
66+
return $successfulResponse->getBodyByType(self::BODY_TEXT_CSV);
6767
} catch (Exception $exception) {}
6868

69-
throw new Exception('No body found');
69+
return null;
7070
}
7171

7272
public function isRawResponse(Method $method)
@@ -103,4 +103,16 @@ public function isIterableResponse(Method $method, ApiDefinition $api)
103103

104104
return false;
105105
}
106+
107+
private function getSuccessfulResponse(Method $method): ?Response
108+
{
109+
foreach ($method->getResponses() as $response) {
110+
$statusCode = $response->getStatusCode();
111+
if (in_array($statusCode, [200, 202], true)) {
112+
return $method->getResponse($statusCode);
113+
}
114+
}
115+
116+
return null;
117+
}
106118
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
## vendor-sky-net-client
3+
4+
Provides methods to manipulate `SkyNetClient` API.
5+
It automatically authenticates all requests and maps required data structure for you.
6+
7+
#### Usage
8+
9+
This library provides `ClientFactory` class, which you should use to get the API client itself:
10+
11+
```php
12+
use Paysera\Test\SkyNetClient\ClientFactory;
13+
14+
$clientFactory = new ClientFactory([
15+
'base_url' => 'http://example.com/sky-net/rest/v1/', // optional, in case you need a custom one.
16+
'mac' => [ // use this, if API requires Mac authentication.
17+
'mac_id' => 'my-mac-id',
18+
'mac_secret' => 'my-mac-secret',
19+
],
20+
'basic' => [ // use this, if API requires Basic authentication.
21+
'username' => 'username',
22+
'password' => 'password',
23+
],
24+
'oauth' => [ // use this, if API requires OAuth v2 authentication.
25+
'token' => [
26+
'access_token' => 'my-access-token',
27+
'refresh_token' => 'my-refresh-token',
28+
],
29+
],
30+
// other configuration options, if needed
31+
]);
32+
33+
$skyNetClient = $clientFactory->getSkyNetClient();
34+
```
35+
36+
Please use only one authentication mechanism, provided by `Vendor`.
37+
38+
Now, that you have instance of `SkyNetClient`, you can use following methods
39+
### Methods
40+
41+
42+
Set the target of termination
43+
44+
45+
```php
46+
use Paysera\Test\SkyNetClient\Entity as Entities;
47+
48+
$terminationInput = new Entities\TerminationInput();
49+
50+
$terminationInput->setTargetName($targetName);
51+
52+
$result = $skyNetClient->createTermination($terminationInput);
53+
```
54+
---
55+
56+
Change the target of termination
57+
58+
59+
```php
60+
use Paysera\Test\SkyNetClient\Entity as Entities;
61+
62+
$terminationInput = new Entities\TerminationInput();
63+
64+
$terminationInput->setTargetName($targetName);
65+
66+
$skyNetClient->updateTermination($terminationInput);
67+
```
68+
---
69+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "vendor/lib-sky-net-client",
3+
"description": "SkyNetClient",
4+
"version": "1.0",
5+
"autoload": {
6+
"psr-4": {
7+
"Paysera\\Test\\SkyNetClient\\": "src"
8+
}
9+
},
10+
"require": {
11+
"php": ">=5.5",
12+
"paysera/lib-rest-client-common": "^1.0 | ^2.5",
13+
"fig/http-message-util": "^1.0"
14+
},
15+
"minimum-stability": "stable"
16+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Paysera\Test\SkyNetClient;
4+
5+
use Paysera\Component\RestClientCommon\Util\ClientFactoryAbstract;
6+
use Paysera\Component\RestClientCommon\Client\ApiClient;
7+
8+
class ClientFactory extends ClientFactoryAbstract
9+
{
10+
const DEFAULT_BASE_URL = 'http://example.com/sky-net/rest/v1/';
11+
12+
protected $apiClient;
13+
14+
public function __construct($options)
15+
{
16+
if ($options instanceof ApiClient) {
17+
$this->apiClient = $options;
18+
return;
19+
}
20+
21+
$defaultUrlParameters = [];
22+
23+
$options['url_parameters'] = $this->resolveDefaultUrlParameters($defaultUrlParameters, $options);
24+
$this->apiClient = $this->createApiClient($options);
25+
}
26+
27+
public function getSkyNetClient()
28+
{
29+
return new SkyNetClient($this->apiClient);
30+
}
31+
32+
private function resolveDefaultUrlParameters(array $defaults, array $options)
33+
{
34+
$params = [];
35+
if (isset($options['url_parameters'])) {
36+
$params = $options['url_parameters'];
37+
}
38+
39+
return $params + $defaults;
40+
}
41+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Paysera\Test\SkyNetClient\Entity;
4+
5+
use Paysera\Component\RestClientCommon\Entity\Entity;
6+
7+
class TerminationInput extends Entity
8+
{
9+
public function __construct(array $data = [])
10+
{
11+
parent::__construct($data);
12+
}
13+
14+
/**
15+
* @return string
16+
*/
17+
public function getTargetName()
18+
{
19+
return $this->get('target_name');
20+
}
21+
/**
22+
* @param string $targetName
23+
* @return $this
24+
*/
25+
public function setTargetName($targetName)
26+
{
27+
$this->set('target_name', $targetName);
28+
return $this;
29+
}
30+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace Paysera\Test\SkyNetClient\Entity;
4+
5+
use Paysera\Component\RestClientCommon\Entity\Entity;
6+
7+
class TerminationOutput extends Entity
8+
{
9+
const REFERENCE_TYPE_INFORMATION_REQUEST = 'information_request';
10+
const STATUS_ACCEPTED = 'accepted';
11+
const STATUS_PROCESSING = 'processing';
12+
const STATUS_COMPLETED = 'completed';
13+
14+
public function __construct(array $data = [])
15+
{
16+
parent::__construct($data);
17+
}
18+
19+
/**
20+
* @return string
21+
*/
22+
public function getId()
23+
{
24+
return $this->get('id');
25+
}
26+
/**
27+
* @param string $id
28+
* @return $this
29+
*/
30+
public function setId($id)
31+
{
32+
$this->set('id', $id);
33+
return $this;
34+
}
35+
/**
36+
* @return string
37+
*/
38+
public function getReferenceId()
39+
{
40+
return $this->get('reference_id');
41+
}
42+
/**
43+
* @param string $referenceId
44+
* @return $this
45+
*/
46+
public function setReferenceId($referenceId)
47+
{
48+
$this->set('reference_id', $referenceId);
49+
return $this;
50+
}
51+
/**
52+
* @return string
53+
*/
54+
public function getReferenceType()
55+
{
56+
return $this->get('reference_type');
57+
}
58+
/**
59+
* @param string $referenceType
60+
* @return $this
61+
*/
62+
public function setReferenceType($referenceType)
63+
{
64+
$this->set('reference_type', $referenceType);
65+
return $this;
66+
}
67+
/**
68+
* @return string
69+
*/
70+
public function getStatus()
71+
{
72+
return $this->get('status');
73+
}
74+
/**
75+
* @param string $status
76+
* @return $this
77+
*/
78+
public function setStatus($status)
79+
{
80+
$this->set('status', $status);
81+
return $this;
82+
}
83+
/**
84+
* @return \DateTimeImmutable
85+
*/
86+
public function getCreatedAt()
87+
{
88+
return (new \DateTimeImmutable())->setTimestamp($this->get('created_at'));
89+
}
90+
/**
91+
* @param \DateTimeInterface $createdAt
92+
* @return $this
93+
*/
94+
public function setCreatedAt(\DateTimeInterface $createdAt)
95+
{
96+
$this->set('created_at', $createdAt->getTimestamp());
97+
return $this;
98+
}
99+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Paysera\Test\SkyNetClient;
4+
5+
use Paysera\Test\SkyNetClient\Entity as Entities;
6+
use Fig\Http\Message\RequestMethodInterface;
7+
use Paysera\Component\RestClientCommon\Entity\Entity;
8+
use Paysera\Component\RestClientCommon\Client\ApiClient;
9+
10+
class SkyNetClient
11+
{
12+
private $apiClient;
13+
14+
public function __construct(ApiClient $apiClient)
15+
{
16+
$this->apiClient = $apiClient;
17+
}
18+
19+
public function withOptions(array $options)
20+
{
21+
return new SkyNetClient($this->apiClient->withOptions($options));
22+
}
23+
24+
/**
25+
* Set the target of termination
26+
* POST /termination
27+
*
28+
* @param Entities\TerminationInput $terminationInput
29+
* @return Entities\TerminationOutput
30+
*/
31+
public function createTermination(Entities\TerminationInput $terminationInput)
32+
{
33+
$request = $this->apiClient->createRequest(
34+
RequestMethodInterface::METHOD_POST,
35+
'termination',
36+
$terminationInput
37+
);
38+
$data = $this->apiClient->makeRequest($request);
39+
40+
return new Entities\TerminationOutput($data);
41+
}
42+
43+
/**
44+
* Change the target of termination
45+
* PUT /termination
46+
*
47+
* @param Entities\TerminationInput $terminationInput
48+
* @return null
49+
*/
50+
public function updateTermination(Entities\TerminationInput $terminationInput)
51+
{
52+
$request = $this->apiClient->createRequest(
53+
RequestMethodInterface::METHOD_PUT,
54+
'termination',
55+
$terminationInput
56+
);
57+
$data = $this->apiClient->makeRequest($request);
58+
59+
return null;
60+
}
61+
}

0 commit comments

Comments
 (0)