Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHP] Add support for async requests #5771

Merged
210 changes: 150 additions & 60 deletions modules/swagger-codegen/src/main/resources/php/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,154 @@ use {{invokerPackage}}\ObjectSerializer;
* @return array of {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}null{{/returnType}}, HTTP status code, HTTP response headers (array of strings)
*/
public function {{operationId}}WithHttpInfo({{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}'{{{.}}}'{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
{
$returnType = '{{returnType}}';
$request = $this->{{operationId}}Request({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});

try {

try {
$response = $this->client->send($request);
} catch (RequestException $e) {
throw new ApiException(
"[{$e->getCode()}] {$e->getMessage()}",
$e->getCode(),
$e->getResponse() ? $e->getResponse()->getHeaders() : null
);
}

$statusCode = $response->getStatusCode();

if ($statusCode < 200 || $statusCode > 299) {
throw new ApiException(
"[$statusCode] Error connecting to the API ({$request->getUri()})",
$statusCode,
$response->getHeaders(),
$response->getBody()
);
}

{{#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, $statusCode, $response->getHeaders()];
{{/returnType}}

} catch (ApiException $e) {
switch ($e->getCode()) {
{{#responses}}
{{#dataType}}
{{^isWildcard}}case {{code}}:{{/isWildcard}}{{#isWildcard}}default:{{/isWildcard}}
$data = ObjectSerializer::deserialize($e->getResponseBody(), '{{dataType}}', $e->getResponseHeaders());
$e->setResponseObject($data);
break;
{{/dataType}}
{{/responses}}
}
throw $e;
}
}

/**
* Operation {{{operationId}}}Async
*
* {{{summary}}}
*
{{#description}}
* {{.}}
*
{{/description}}
{{#allParams}}
* @param {{dataType}} ${{paramName}} {{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];
});
}

/**
* Operation {{{operationId}}}AsyncWithHttpInfo
*
* {{{summary}}}
*
{{#description}}
* {{.}}
*
{{/description}}
{{#allParams}}
* @param {{dataType}} ${{paramName}} {{description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}}
* @throws \InvalidArgumentException
* @return \GuzzleHttp\Promise\PromiseInterface
*/
public function {{operationId}}AsyncWithHttpInfo({{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}'{{{.}}}'{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
{
$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 [
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}}
{{/allParams}}
* @throws \InvalidArgumentException
* @return \GuzzleHttp\Psr7\Request
*/
protected function {{operationId}}Request({{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}'{{{.}}}'{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
{
{{#allParams}}
{{#required}}
Expand Down Expand Up @@ -165,7 +313,6 @@ use {{invokerPackage}}\ObjectSerializer;
$headerParams = [];
$httpBody = '';
$multipart = false;
$returnType = '{{returnType}}';

{{#queryParams}}
// query params
Expand Down Expand Up @@ -292,71 +439,14 @@ use {{invokerPackage}}\ObjectSerializer;
$headers
);

$request = new Request(
return new Request(
'{{httpMethod}}',
$url,
$headers,
$httpBody
);

try {

try {
$response = $this->client->send($request);
} catch (RequestException $e) {
throw new ApiException(
"[{$e->getCode()}] {$e->getMessage()}",
$e->getCode(),
$e->getResponse() ? $e->getResponse()->getHeaders() : null
);
}

$statusCode = $response->getStatusCode();

if ($statusCode < 200 || $statusCode > 299) {
throw new ApiException(
"[$statusCode] Error connecting to the API ($url)",
$statusCode,
$response->getHeaders(),
$response->getBody()
);
}

{{#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, $statusCode, $response->getHeaders()];
{{/returnType}}

} catch (ApiException $e) {
switch ($e->getCode()) {
{{#responses}}
{{#dataType}}
{{^isWildcard}}case {{code}}:{{/isWildcard}}{{#isWildcard}}default:{{/isWildcard}}
$data = ObjectSerializer::deserialize($e->getResponseBody(), '{{dataType}}', $e->getResponseHeaders());
$e->setResponseObject($data);
break;
{{/dataType}}
{{/responses}}
}
throw $e;
}
}

{{/operation}}
}
{{/operations}}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,94 @@ public function testCodeInjectEndRnNR($test_code_inject____end____rn_n_r = null)
* @return array of null, HTTP status code, HTTP response headers (array of strings)
*/
public function testCodeInjectEndRnNRWithHttpInfo($test_code_inject____end____rn_n_r = null)
{
$returnType = '';
$request = $this->testCodeInjectEndRnNRRequest($test_code_inject____end____rn_n_r);

try {

try {
$response = $this->client->send($request);
} catch (RequestException $e) {
throw new ApiException(
"[{$e->getCode()}] {$e->getMessage()}",
$e->getCode(),
$e->getResponse() ? $e->getResponse()->getHeaders() : null
);
}

$statusCode = $response->getStatusCode();

if ($statusCode < 200 || $statusCode > 299) {
throw new ApiException(
"[$statusCode] Error connecting to the API ({$request->getUri()})",
$statusCode,
$response->getHeaders(),
$response->getBody()
);
}

return [null, $statusCode, $response->getHeaders()];

} catch (ApiException $e) {
switch ($e->getCode()) {
}
throw $e;
}
}

/**
* Operation testCodeInjectEndRnNRAsync
*
* To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
*
* @param string $test_code_inject____end____rn_n_r To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r (optional)
* @throws \InvalidArgumentException
* @return \GuzzleHttp\Promise\PromiseInterface
*/
public function testCodeInjectEndRnNRAsync($test_code_inject____end____rn_n_r = null)
{
return $this->testCodeInjectEndRnNRAsyncWithHttpInfo($test_code_inject____end____rn_n_r)->then(function ($response) {
return $response[0];
});
}

/**
* Operation testCodeInjectEndRnNRAsyncWithHttpInfo
*
* To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
*
* @param string $test_code_inject____end____rn_n_r To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r (optional)
* @throws \InvalidArgumentException
* @return \GuzzleHttp\Promise\PromiseInterface
*/
public function testCodeInjectEndRnNRAsyncWithHttpInfo($test_code_inject____end____rn_n_r = null)
{
$returnType = '';
$request = $this->testCodeInjectEndRnNRRequest($test_code_inject____end____rn_n_r);

return $this->client->sendAsync($request)->then(function ($response) use ($returnType) {
return [null, $response->getStatusCode(), $response->getHeaders()];
}, 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 'testCodeInjectEndRnNR'
*
* @param string $test_code_inject____end____rn_n_r To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r (optional)
* @throws \InvalidArgumentException
* @return \GuzzleHttp\Psr7\Request
*/
protected function testCodeInjectEndRnNRRequest($test_code_inject____end____rn_n_r = null)
{

$resourcePath = '/fake';
Expand All @@ -115,7 +203,6 @@ public function testCodeInjectEndRnNRWithHttpInfo($test_code_inject____end____rn
$headerParams = [];
$httpBody = '';
$multipart = false;
$returnType = '';



Expand Down Expand Up @@ -173,42 +260,12 @@ public function testCodeInjectEndRnNRWithHttpInfo($test_code_inject____end____rn
$headers
);

$request = new Request(
return new Request(
'PUT',
$url,
$headers,
$httpBody
);

try {

try {
$response = $this->client->send($request);
} catch (RequestException $e) {
throw new ApiException(
"[{$e->getCode()}] {$e->getMessage()}",
$e->getCode(),
$e->getResponse() ? $e->getResponse()->getHeaders() : null
);
}

$statusCode = $response->getStatusCode();

if ($statusCode < 200 || $statusCode > 299) {
throw new ApiException(
"[$statusCode] Error connecting to the API ($url)",
$statusCode,
$response->getHeaders(),
$response->getBody()
);
}

return [null, $statusCode, $response->getHeaders()];

} catch (ApiException $e) {
switch ($e->getCode()) {
}
throw $e;
}
}

}
Loading