Skip to content

Commit

Permalink
[PHP] Improve Api template (#6507)
Browse files Browse the repository at this point in the history
* Improve spacing in doc comment

* Improve grouping of parameter tags

* Improve line length

* Fix undefined variable $_tempBody

* Improve indent
  • Loading branch information
ackintosh authored and wing328 committed Sep 24, 2017
1 parent ac99fe6 commit b31a804
Show file tree
Hide file tree
Showing 12 changed files with 2,560 additions and 1,232 deletions.
131 changes: 81 additions & 50 deletions modules/swagger-codegen/src/main/resources/php/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ use {{invokerPackage}}\ObjectSerializer;
/**
* @param ClientInterface $client
* @param Configuration $config
* @param HeaderSelector $selector
* @param Configuration $config
* @param HeaderSelector $selector
*/
public function __construct(
ClientInterface $client = null,
Expand Down Expand Up @@ -84,8 +84,9 @@ use {{invokerPackage}}\ObjectSerializer;
*
{{/description}}
{{#allParams}}
* @param {{dataType}} ${{paramName}} {{description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
* @param {{dataType}} ${{paramName}}{{#description}} {{description}}{{/description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}}
*
* @throws \{{invokerPackage}}\ApiException on non-2xx response
* @throws \InvalidArgumentException
* @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
Expand All @@ -108,8 +109,9 @@ use {{invokerPackage}}\ObjectSerializer;
*
{{/description}}
{{#allParams}}
* @param {{dataType}} ${{paramName}} {{description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
* @param {{dataType}} ${{paramName}}{{#description}} {{description}}{{/description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}}
*
* @throws \{{invokerPackage}}\ApiException on non-2xx response
* @throws \InvalidArgumentException
* @return array of {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}null{{/returnType}}, HTTP status code, HTTP response headers (array of strings)
Expand All @@ -135,7 +137,11 @@ use {{invokerPackage}}\ObjectSerializer;

if ($statusCode < 200 || $statusCode > 299) {
throw new ApiException(
"[$statusCode] Error connecting to the API ({$request->getUri()})",
sprintf(
'[%d] Error connecting to the API (%s)',
$statusCode,
$request->getUri()
),
$statusCode,
$response->getHeaders(),
$response->getBody()
Expand Down Expand Up @@ -168,7 +174,11 @@ use {{invokerPackage}}\ObjectSerializer;
{{#responses}}
{{#dataType}}
{{^isWildcard}}case {{code}}:{{/isWildcard}}{{#isWildcard}}default:{{/isWildcard}}
$data = ObjectSerializer::deserialize($e->getResponseBody(), '{{dataType}}', $e->getResponseHeaders());
$data = ObjectSerializer::deserialize(
$e->getResponseBody(),
'{{dataType}}',
$e->getResponseHeaders()
);
$e->setResponseObject($data);
break;
{{/dataType}}
Expand All @@ -188,16 +198,20 @@ use {{invokerPackage}}\ObjectSerializer;
*
{{/description}}
{{#allParams}}
* @param {{dataType}} ${{paramName}} {{description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
* @param {{dataType}} ${{paramName}}{{#description}} {{description}}{{/description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}}
*
* @throws \InvalidArgumentException
* @return \GuzzleHttp\Promise\PromiseInterface
*/
public function {{operationId}}Async({{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}'{{{.}}}'{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
{
return $this->{{operationId}}AsyncWithHttpInfo({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}})->then(function ($response) {
return $response[0];
});
return $this->{{operationId}}AsyncWithHttpInfo({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
->then(
function ($response) {
return $response[0];
}
);
}

/**
Expand All @@ -210,8 +224,9 @@ use {{invokerPackage}}\ObjectSerializer;
*
{{/description}}
{{#allParams}}
* @param {{dataType}} ${{paramName}} {{description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
* @param {{dataType}} ${{paramName}}{{#description}} {{description}}{{/description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}}
*
* @throws \InvalidArgumentException
* @return \GuzzleHttp\Promise\PromiseInterface
*/
Expand All @@ -220,45 +235,55 @@ use {{invokerPackage}}\ObjectSerializer;
$returnType = '{{returnType}}';
$request = $this->{{operationId}}Request({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});

return $this->client->sendAsync($request)->then(function ($response) use ($returnType) {
{{#returnType}}
$responseBody = $response->getBody();
if ($returnType === '\SplFileObject') {
$content = $responseBody; //stream goes to serializer
} else {
$content = $responseBody->getContents();
if ($returnType !== 'string') {
$content = json_decode($content);
return $this->client
->sendAsync($request)
->then(
function ($response) use ($returnType) {
{{#returnType}}
$responseBody = $response->getBody();
if ($returnType === '\SplFileObject') {
$content = $responseBody; //stream goes to serializer
} else {
$content = $responseBody->getContents();
if ($returnType !== 'string') {
$content = json_decode($content);
}
}

return [
ObjectSerializer::deserialize($content, $returnType, []),
$response->getStatusCode(),
$response->getHeaders()
];
{{/returnType}}
{{^returnType}}
return [null, $response->getStatusCode(), $response->getHeaders()];
{{/returnType}}
},
function ($exception) {
$response = $exception->getResponse();
$statusCode = $response->getStatusCode();
throw new ApiException(
sprintf(
'[%d] Error connecting to the API (%s)',
$statusCode,
$exception->getRequest()->getUri()
),
$statusCode,
$response->getHeaders(),
$response->getBody()
);
}
}

return [
ObjectSerializer::deserialize($content, $returnType, []),
$response->getStatusCode(),
$response->getHeaders()
];
{{/returnType}}
{{^returnType}}
return [null, $response->getStatusCode(), $response->getHeaders()];
{{/returnType}}
}, function ($exception) {
$response = $exception->getResponse();
$statusCode = $response->getStatusCode();
throw new ApiException(
"[$statusCode] Error connecting to the API ({$exception->getRequest()->getUri()})",
$statusCode,
$response->getHeaders(),
$response->getBody()
);
});
}

/**
* Create request for operation '{{{operationId}}}'
*
{{#allParams}}
* @param {{dataType}} ${{paramName}} {{description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
* @param {{dataType}} ${{paramName}}{{#description}} {{description}}{{/description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}}
*
* @throws \InvalidArgumentException
* @return \GuzzleHttp\Psr7\Request
*/
Expand All @@ -268,7 +293,9 @@ use {{invokerPackage}}\ObjectSerializer;
{{#required}}
// verify the required parameter '{{paramName}}' is set
if (${{paramName}} === null) {
throw new \InvalidArgumentException('Missing the required parameter ${{paramName}} when calling {{operationId}}');
throw new \InvalidArgumentException(
'Missing the required parameter ${{paramName}} when calling {{operationId}}'
);
}
{{/required}}
{{#hasValidation}}
Expand Down Expand Up @@ -349,7 +376,11 @@ use {{invokerPackage}}\ObjectSerializer;
}
{{/collectionFormat}}
if (${{paramName}} !== null) {
$resourcePath = str_replace('{' . '{{baseName}}' . '}', ObjectSerializer::toPathValue(${{paramName}}), $resourcePath);
$resourcePath = str_replace(
'{' . '{{baseName}}' . '}',
ObjectSerializer::toPathValue(${{paramName}}),
$resourcePath
);
}
{{/pathParams}}

Expand All @@ -365,9 +396,9 @@ use {{invokerPackage}}\ObjectSerializer;
{{/isFile}}
}
{{/formParams}}
{{#bodyParams}}
// body params
$_tempBody = null;
{{#bodyParams}}
if (isset(${{paramName}})) {
$_tempBody = ${{paramName}};
}
Expand Down Expand Up @@ -397,13 +428,15 @@ use {{invokerPackage}}\ObjectSerializer;
'contents' => $formParamValue
];
}
$httpBody = new MultipartStream($multipartContents); // for HTTP post (form)
// for HTTP post (form)
$httpBody = new MultipartStream($multipartContents);

} elseif ($headers['Content-Type'] === 'application/json') {
$httpBody = \GuzzleHttp\json_encode($formParams);
} else {
$httpBody = \GuzzleHttp\Psr7\build_query($formParams); // for HTTP post (form)
// for HTTP post (form)
$httpBody = \GuzzleHttp\Psr7\build_query($formParams);
}
}

Expand All @@ -429,9 +462,6 @@ use {{invokerPackage}}\ObjectSerializer;
{{/isOAuth}}
{{/authMethods}}

$query = \GuzzleHttp\Psr7\build_query($queryParams);
$url = $this->config->getHost() . $resourcePath . ($query ? '?' . $query : '');

$defaultHeaders = [];
if ($this->config->getUserAgent()) {
$defaultHeaders['User-Agent'] = $this->config->getUserAgent();
Expand All @@ -443,9 +473,10 @@ use {{invokerPackage}}\ObjectSerializer;
$headers
);

$query = \GuzzleHttp\Psr7\build_query($queryParams);
return new Request(
'{{httpMethod}}',
$url,
$this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''),
$headers,
$httpBody
);
Expand Down
Loading

0 comments on commit b31a804

Please sign in to comment.