cpprest.api-source.mustache Maven / Gradle / Ivy
The newest version!
{{>licenseInfo}}
{{#operations}}
#include "{{classname}}.h"
#include "IHttpBody.h"
#include "JsonBody.h"
#include "MultipartFormData.h"
#include
#include
{{#apiNamespaceDeclarations}}
namespace {{this}} {
{{/apiNamespaceDeclarations}}
using namespace {{modelNamespace}};
{{classname}}::{{classname}}( std::shared_ptr apiClient )
: m_ApiClient(apiClient)
{
}
{{classname}}::~{{classname}}()
{
}
{{#operation}}
pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {{classname}}::{{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
{
{{#allParams}}{{#required}}{{^isPrimitiveType}}{{^isContainer}}
// verify the required parameter '{{paramName}}' is set
if ({{paramName}} == nullptr)
{
throw ApiException(400, U("Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}"));
}
{{/isContainer}}{{/isPrimitiveType}}{{/required}}{{/allParams}}
std::shared_ptr apiConfiguration( m_ApiClient->getConfiguration() );
utility::string_t path = U("{{path}}");
{{#pathParams}}boost::replace_all(path, U("{") U("{{baseName}}") U("}"), ApiClient::parameterToString({{{paramName}}}));
{{/pathParams}}
std::map queryParams;
std::map headerParams( apiConfiguration->getDefaultHeaders() );
std::map formParams;
std::map> fileParams;
std::unordered_set responseHttpContentTypes;
{{#produces}}responseHttpContentTypes.insert( U("{{mediaType}}") );
{{/produces}}
utility::string_t responseHttpContentType;
// use JSON if possible
if ( responseHttpContentTypes.size() == 0 || responseHttpContentTypes.find(U("application/json")) != responseHttpContentTypes.end() )
{
responseHttpContentType = U("application/json");
}
// multipart formdata
else if( responseHttpContentTypes.find(U("multipart/form-data")) != responseHttpContentTypes.end() )
{
responseHttpContentType = U("multipart/form-data");
}
else
{
throw ApiException(400, U("{{classname}}->{{operationId}} does not produce any supported media type"));
}
headerParams[U("Accept")] = responseHttpContentType;
std::unordered_set consumeHttpContentTypes;
{{#consumes}}consumeHttpContentTypes.insert( U("{{mediaType}}") );
{{/consumes}}
{{#allParams}}{{^isBodyParam}}{{^isPrimitiveType}}{{^isContainer}}if ({{paramName}} != nullptr){{/isContainer}}{{/isPrimitiveType}}
{
{{#isContainer}}{{#isQueryParam}}queryParams[U("{{baseName}}")] = ApiClient::parameterToArrayString<{{items.datatype}}>({{paramName}});
{{/isQueryParam}}{{#isHeaderParam}}headerParams[U("{{baseName}}")] = ApiClient::parameterToArrayString<{{items.datatype}}>({{paramName}});
{{/isHeaderParam}}{{#isFormParam}}{{^isFile}}formParams[ U("{{baseName}}") ] = ApiClient::parameterToArrayString<{{items.datatype}}>({{paramName}});
{{/isFile}}{{/isFormParam}}{{/isContainer}}{{^isContainer}}{{#isQueryParam}}queryParams[U("{{baseName}}")] = ApiClient::parameterToString({{paramName}});
{{/isQueryParam}}{{#isHeaderParam}}headerParams[U("{{baseName}}")] = ApiClient::parameterToString({{paramName}});
{{/isHeaderParam}}{{#isFormParam}}{{#isFile}}fileParams[ U("{{baseName}}") ] = {{paramName}};
{{/isFile}}{{^isFile}}formParams[ U("{{baseName}}") ] = ApiClient::parameterToString({{paramName}});
{{/isFile}}{{/isFormParam}}{{/isContainer}}
}
{{/isBodyParam}}{{/allParams}}
std::shared_ptr httpBody;
utility::string_t requestHttpContentType;
// use JSON if possible
if ( consumeHttpContentTypes.size() == 0 || consumeHttpContentTypes.find(U("application/json")) != consumeHttpContentTypes.end() )
{
requestHttpContentType = U("application/json");
{{#bodyParam}}
web::json::value json;
{{#isPrimitiveType}} json = ModelBase::toJson({{paramName}});
{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isListContainer}}
{
std::vector jsonArray;
for( auto& item : {{paramName}} )
{
{{#items.isPrimitiveType}}jsonArray.push_back(ModelBase::toJson(item));
{{/items.isPrimitiveType}}{{^items.isPrimitiveType}}{{#items.isString}}jsonArray.push_back(ModelBase::toJson(item));
{{/items.isString}}{{^items.isString}}{{#items.isDateTime}}jsonArray.push_back(ModelBase::toJson(item));
{{/items.isDateTime}}{{^items.isDateTime}}jsonArray.push_back( item.get() ? item->toJson() : web::json::value::null() );
{{/items.isDateTime}}{{/items.isString}}{{/items.isPrimitiveType}}
}
json = web::json::value::array(jsonArray);
}
{{/isListContainer}}{{^isListContainer}}json = ModelBase::toJson({{paramName}});
{{/isListContainer}}{{/isPrimitiveType}}
httpBody = std::shared_ptr( new JsonBody( json ) );
{{/bodyParam}}
}
// multipart formdata
else if( consumeHttpContentTypes.find(U("multipart/form-data")) != consumeHttpContentTypes.end() )
{
requestHttpContentType = U("multipart/form-data");
{{#bodyParam}}
std::shared_ptr multipart(new MultipartFormData);
{{#isPrimitiveType}} multipart->add(ModelBase::toHttpContent("{{paramName}}", {{paramName}}));
{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isListContainer}}
{
std::vector jsonArray;
for( auto& item : {{paramName}} )
{
{{#items.isPrimitiveType}}jsonArray.push_back(ModelBase::toJson(item));
{{/items.isPrimitiveType}}{{^items.isPrimitiveType}}{{#items.isString}}jsonArray.push_back(ModelBase::toJson(item));
{{/items.isString}}{{^items.isString}}{{#items.isDateTime}}jsonArray.push_back(ModelBase::toJson(item));
{{/items.isDateTime}}{{^items.isDateTime}}jsonArray.push_back( item.get() ? item->toJson() : web::json::value::null() );
{{/items.isDateTime}}{{/items.isString}}{{/items.isPrimitiveType}}
}
multipart->add(ModelBase::toHttpContent(U("{{paramName}}"), web::json::value::array(jsonArray), U("application/json")));
}
{{/isListContainer}}{{^isListContainer}}{{#isString}}multipart->add(ModelBase::toHttpContent(U("{{paramName}}"), {{paramName}}));
{{/isString}}{{^isString}}
if({{paramName}}.get())
{
{{paramName}}->toMultipart(multipart, U("{{paramName}}"));
}
{{/isString}}{{/isListContainer}}{{/isPrimitiveType}}
httpBody = multipart;
requestHttpContentType += U("; boundary=") + multipart->getBoundary();
{{/bodyParam}}
}
else
{
throw ApiException(415, U("{{classname}}->{{operationId}} does not consume any supported media type"));
}
{{#authMethods}}
// authentication ({{name}}) required
{{#isApiKey}}
{{#isKeyInHeader}}
{
utility::string_t apiKey = apiConfiguration->getApiKey(U("{{keyParamName}}"));
if ( apiKey.size() > 0 )
{
headerParams[U("{{keyParamName}}")] = apiKey;
}
}
{{/isKeyInHeader}}
{{#isKeyInQuery}}
{
utility::string_t apiKey = apiConfiguration->getApiKey(U("{{keyParamName}}"));
if ( apiKey.size() > 0 )
{
queryParams[U("{{keyParamName}}")] = apiKey;
}
}
{{/isKeyInQuery}}
{{/isApiKey}}
{{#isBasic}}
// Basic authentication is added automatically as part of the http_client_config
{{/isBasic}}
{{#isOAuth}}
// oauth2 authentication is added automatically as part of the http_client_config
{{/isOAuth}}
{{/authMethods}}
return m_ApiClient->callApi(path, U("{{httpMethod}}"), queryParams, httpBody, headerParams, formParams, fileParams, requestHttpContentType)
.then([=](web::http::http_response response)
{
// 1xx - informational : OK
// 2xx - successful : OK
// 3xx - redirection : OK
// 4xx - client error : not OK
// 5xx - client error : not OK
if (response.status_code() >= 400)
{
throw ApiException(response.status_code()
, U("error calling {{operationId}}: ") + response.reason_phrase()
, std::make_shared(response.extract_utf8string(true).get()));
}
// check response content type
if(response.headers().has(U("Content-Type")))
{
utility::string_t contentType = response.headers()[U("Content-Type")];
if( contentType.find(responseHttpContentType) == std::string::npos )
{
throw ApiException(500
, U("error calling {{operationId}}: unexpected response type: ") + contentType
, std::make_shared(response.extract_utf8string(true).get()));
}
}
return response.extract_string();
})
.then([=](utility::string_t response)
{
{{^returnType}}return void();
{{/returnType}}{{#returnType}}{{#returnContainer}}{{{returnType}}} result;
{{/returnContainer}}{{^returnContainer}}{{{returnType}}} result({{{defaultResponse}}});{{/returnContainer}}
if(responseHttpContentType == U("application/json"))
{
web::json::value json = web::json::value::parse(response);
{{#isListContainer}}for( auto& item : json.as_array() )
{
{{#vendorExtensions.x-codegen-response.items.isPrimitiveType}}result.push_back(ModelBase::{{vendorExtensions.x-codegen-response.items.datatype}}FromJson(item));
{{/vendorExtensions.x-codegen-response.items.isPrimitiveType}}{{^vendorExtensions.x-codegen-response.items.isPrimitiveType}}{{#vendorExtensions.x-codegen-response.items.isString}}result.push_back(ModelBase::stringFromJson(item));
{{/vendorExtensions.x-codegen-response.items.isString}}{{^vendorExtensions.x-codegen-response.items.isString}}{{{vendorExtensions.x-codegen-response.items.datatype}}} itemObj({{{vendorExtensions.x-codegen-response.items.defaultValue}}});
itemObj->fromJson(item);
result.push_back(itemObj);
{{/vendorExtensions.x-codegen-response.items.isString}}{{/vendorExtensions.x-codegen-response.items.isPrimitiveType}}
}
{{/isListContainer}}{{^isListContainer}}{{#isMapContainer}}for( auto& item : json.as_object() )
{
{{#vendorExtensions.x-codegen-response.items.isPrimitiveType}}result[item.first] = ModelBase::{{vendorExtensions.x-codegen-response.items.datatype}}FromJson(item.second);
{{/vendorExtensions.x-codegen-response.items.isPrimitiveType}}{{^vendorExtensions.x-codegen-response.items.isPrimitiveType}}{{#vendorExtensions.x-codegen-response.items.isString}}result[item.first] = ModelBase::stringFromJson(item.second);
{{/vendorExtensions.x-codegen-response.items.isString}}{{^vendorExtensions.x-codegen-response.items.isString}}{{{vendorExtensions.x-codegen-response.items.datatype}}} itemObj({{{vendorExtensions.x-codegen-response.items.defaultValue}}});
itemObj->fromJson(item);
result[item.first] = itemObj;
{{/vendorExtensions.x-codegen-response.items.isString}}{{/vendorExtensions.x-codegen-response.items.isPrimitiveType}}
}
{{/isMapContainer}}{{^isMapContainer}}{{#vendorExtensions.x-codegen-response.isPrimitiveType}}result = ModelBase::{{vendorExtensions.x-codegen-response.items.datatype}}FromJson(json);
{{/vendorExtensions.x-codegen-response.isPrimitiveType}}{{^vendorExtensions.x-codegen-response.isPrimitiveType}}{{#vendorExtensions.x-codegen-response.isString}}result = ModelBase::stringFromJson(json);
{{/vendorExtensions.x-codegen-response.isString}}{{^vendorExtensions.x-codegen-response.isString}}result->fromJson(json);{{/vendorExtensions.x-codegen-response.isString}}{{/vendorExtensions.x-codegen-response.isPrimitiveType}}{{/isMapContainer}}{{/isListContainer}}
}
// else if(responseHttpContentType == U("multipart/form-data"))
// {
// TODO multipart response parsing
// }
else
{
throw ApiException(500
, U("error calling findPetsByStatus: unsupported response type"));
}
return result;
{{/returnType}}
});
}
{{/operation}}
{{#apiNamespaceDeclarations}}
}
{{/apiNamespaceDeclarations}}
{{/operations}}