Skip to content

Commit

Permalink
Support content type text/csv and stream response (#99)
Browse files Browse the repository at this point in the history
* Support content type text/csv and stream response
  • Loading branch information
mkafadarov authored Jan 21, 2021
1 parent 145e3d4 commit ce58d9b
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 11.4.0
### Changed
- Support content type `text/csv`
- Support stream response by using annotation type `(stream_response)` in raml method

## 11.3.1
### Fixed
- Wrong new release version for javascript clients
Expand Down
11 changes: 11 additions & 0 deletions src/Paysera/Bundle/CodeGeneratorBundle/Service/BodyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class BodyResolver
const BODY_JSON = 'application/json';
const BODY_JAVASCRIPT = 'application/javascript';
const BODY_OCTET_STREAM = 'application/octet-stream';
const BODY_TEXT_CSV = 'text/csv';
const ANNOTATION_STREAM_RESPONSE = '(stream_response)';

/**
* @param Method $method
Expand Down Expand Up @@ -60,6 +62,10 @@ public function getResponseBody(Method $method)
return $okResponse->getBodyByType(self::BODY_OCTET_STREAM);
} catch (Exception $exception) {}

try {
return $okResponse->getBodyByType(self::BODY_TEXT_CSV);
} catch (Exception $exception) {}

throw new Exception('No body found');
}

Expand All @@ -73,6 +79,11 @@ public function isRawResponse(Method $method)
return false;
}

public function isStreamResponse(Method $method) : bool
{
return array_key_exists(self::ANNOTATION_STREAM_RESPONSE, $method->getAnnotations());
}

public function isIterableResponse(Method $method, ApiDefinition $api)
{
$body = $this->getResponseBody($method);
Expand Down
6 changes: 6 additions & 0 deletions src/Paysera/Bundle/CodeGeneratorBundle/Twig/BaseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public function getFunctions()
new Twig_SimpleFunction('generate_method_arguments', [$this, 'generateMethodArguments']),
new Twig_SimpleFunction('generate_body', [$this, 'generateBody']),
new Twig_SimpleFunction('is_raw_response', [$this, 'isRawResponse']),
new Twig_SimpleFunction('is_stream_response', [$this, 'isStreamResponse']),
new Twig_SimpleFunction('get_argument_names', [$this, 'getArgumentNames']),
new Twig_SimpleFunction('get_method_entity_name', [$this, 'getMethodEntityName']),
new Twig_SimpleFunction('method_returns_result', [$this, 'methodReturnsResult']),
Expand Down Expand Up @@ -455,6 +456,11 @@ public function isRawResponse(Method $method)
return $this->bodyResolver->isRawResponse($method);
}

public function isStreamResponse(Method $method) : bool
{
return $this->bodyResolver->isStreamResponse($method);
}

/**
* @param Method $method
* @param ApiDefinition $api
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
{{ php_generate_uri(resource) }},
{{ php_generate_entity_converter(method, api) }}
);
{% if is_raw_response(method) -%}
{% if is_stream_response(method) -%}
return $this->apiClient->makeRawRequest($request, ['stream' => true])->getBody();
{% elseif is_raw_response(method) -%}
return $this->apiClient->makeRawRequest($request)->getBody()->getContents();
{% else -%}
$data = $this->apiClient->makeRequest($request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Paysera\Bundle\PhpGeneratorBundle\Service\NamespaceHelper;
use Paysera\Component\StringHelper;
use Paysera\Component\TypeHelper;
use Psr\Http\Message\StreamInterface;
use Raml\Method;
use Raml\Resource;
use Raml\Types\ArrayType;
Expand Down Expand Up @@ -124,6 +125,10 @@ public function getReturnType(array $context, Method $method, ApiDefinition $api
return $bodyTypeName;
}

if ($this->bodyResolver->isStreamResponse($method)) {
return '\\' . StreamInterface::class;
}

return 'null';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ $result = $accountClient->getAccountScripts($scriptFilter);
---


Get Account Statements CSV


```php

$result = $accountClient->getAccountStatementsCsv($accountNumber);
```
---



Standard SQL-style Result filtering


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ public function getAccountScripts(Entities\ScriptFilter $scriptFilter)
return $this->apiClient->makeRawRequest($request)->getBody()->getContents();
}

/**
* Get Account Statements CSV
* GET /accounts/{accountNumber}/statements-csv
*
* @param string $accountNumber
* @return \Psr\Http\Message\StreamInterface
*/
public function getAccountStatementsCsv($accountNumber)
{
$request = $this->apiClient->createRequest(
RequestMethodInterface::METHOD_GET,
sprintf('accounts/%s/statements-csv', rawurlencode($accountNumber)),
null
);
return $this->apiClient->makeRawRequest($request, ['stream' => true])->getBody();
}

/**
* Standard SQL-style Result filtering
* GET /accounts
Expand Down
16 changes: 16 additions & 0 deletions tests/PhpGeneratorBundle/RestClient/Fixtures/raml/account/api.raml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ baseUri: https://my-api.example.com/rest/v1
uses:
Paysera: ../__library/rest.raml

annotationTypes:
stream_response: nil

traits:
AccountFilter: !include traits/account-filter.raml
ScriptFilter: !include traits/script-filter.raml
Expand Down Expand Up @@ -87,6 +90,19 @@ types:
body:
application/javascript:
description: Generated JS code
/{accountNumber}:
uriParameters:
accountNumber:
required: true
type: string
/statements-csv:
get:
(stream_response):
description: Get Account Statements CSV
responses:
200:
body:
text/csv:
/refund:
/{requestId}:
/price:
Expand Down

0 comments on commit ce58d9b

Please sign in to comment.