add binary support to C# client

This commit is contained in:
wing328
2016-01-11 00:39:22 +08:00
parent ce82aa41fc
commit e0f43c1c58
12 changed files with 1363 additions and 319 deletions

View File

@@ -92,6 +92,7 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig
typeMapping = new HashMap<String, String>();
typeMapping.put("string", "string");
typeMapping.put("binary", "byte[]");
typeMapping.put("boolean", "bool?");
typeMapping.put("integer", "int?");
typeMapping.put("float", "float?");

View File

@@ -77,9 +77,10 @@ namespace {{packageName}}.Client
// Creates and sets up a RestRequest prior to a call.
private RestRequest PrepareRequest(
String path, RestSharp.Method method, Dictionary<String, String> queryParams, String postBody,
String path, RestSharp.Method method, Dictionary<String, String> queryParams, Object postBody,
Dictionary<String, String> headerParams, Dictionary<String, String> formParams,
Dictionary<String, FileParameter> fileParams, Dictionary<String, String> pathParams)
Dictionary<String, FileParameter> fileParams, Dictionary<String, String> pathParams,
String contentType)
{
var request = new RestRequest(path, method);
@@ -103,8 +104,17 @@ namespace {{packageName}}.Client
foreach(var param in fileParams)
request.AddFile(param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentType);
if (postBody != null) // http body (model) parameter
request.AddParameter("application/json", postBody, ParameterType.RequestBody);
if (postBody != null) // http body (model or byte[]) parameter
{
if (postBody.GetType() == typeof(String))
{
request.AddParameter("application/json", postBody, ParameterType.RequestBody);
}
else if (postBody.GetType() == typeof(byte[]))
{
request.AddParameter(contentType, postBody, ParameterType.RequestBody);
}
}
return request;
}
@@ -120,14 +130,18 @@ namespace {{packageName}}.Client
/// <param name="formParams">Form parameters.</param>
/// <param name="fileParams">File parameters.</param>
/// <param name="pathParams">Path parameters.</param>
/// <param name="contentType">Content Type of the request</param>
/// <returns>Object</returns>
public Object CallApi(
String path, RestSharp.Method method, Dictionary<String, String> queryParams, String postBody,
String path, RestSharp.Method method, Dictionary<String, String> queryParams, Object postBody,
Dictionary<String, String> headerParams, Dictionary<String, String> formParams,
Dictionary<String, FileParameter> fileParams, Dictionary<String, String> pathParams)
Dictionary<String, FileParameter> fileParams, Dictionary<String, String> pathParams,
String contentType)
{
var request = PrepareRequest(
path, method, queryParams, postBody, headerParams, formParams, fileParams, pathParams);
path, method, queryParams, postBody, headerParams, formParams, fileParams,
pathParams, contentType);
var response = RestClient.Execute(request);
return (Object) response;
}
@@ -143,14 +157,17 @@ namespace {{packageName}}.Client
/// <param name="formParams">Form parameters.</param>
/// <param name="fileParams">File parameters.</param>
/// <param name="pathParams">Path parameters.</param>
/// <param name="contentType">Content type.</param>
/// <returns>The Task instance.</returns>
public async System.Threading.Tasks.Task<Object> CallApiAsync(
String path, RestSharp.Method method, Dictionary<String, String> queryParams, String postBody,
String path, RestSharp.Method method, Dictionary<String, String> queryParams, Object postBody,
Dictionary<String, String> headerParams, Dictionary<String, String> formParams,
Dictionary<String, FileParameter> fileParams, Dictionary<String, String> pathParams)
Dictionary<String, FileParameter> fileParams, Dictionary<String, String> pathParams,
String contentType)
{
var request = PrepareRequest(
path, method, queryParams, postBody, headerParams, formParams, fileParams, pathParams);
path, method, queryParams, postBody, headerParams, formParams, fileParams,
pathParams, contentType);
var response = await RestClient.ExecuteTaskAsync(request);
return (Object)response;
}
@@ -230,6 +247,10 @@ namespace {{packageName}}.Client
{
return content;
}
else if (type == typeof(byte[])) // return byte array
{
return data;
}
if (type == typeof(Stream))
{
@@ -276,11 +297,11 @@ namespace {{packageName}}.Client
}
/// <summary>
/// Serialize an object into JSON string.
/// Serialize an input (model) into JSON string
/// </summary>
/// <param name="obj">Object.</param>
/// <returns>JSON string.</returns>
public string Serialize(object obj)
public String Serialize(object obj)
{
try
{
@@ -292,6 +313,24 @@ namespace {{packageName}}.Client
}
}
/// <summary>
/// Select the Content-Type header's value from the given content-type array:
/// if JSON exists in the given array, use it;
/// otherwise use the first one defined in 'consumes'
/// </summary>
/// <param name="contentTypes">The Content-Type array to select from.</param>
/// <returns>The Content-Type header to use.</returns>
public String SelectHeaderContentType(String[] contentTypes)
{
if (contentTypes.Length == 0)
return null;
if (contentTypes.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";
return contentTypes[0]; // use the first content type specified in 'consumes'
}
/// <summary>
/// Select the Accept header's value from the given accepts array:
/// if JSON exists in the given array, use it;

View File

@@ -154,7 +154,8 @@ namespace {{packageName}}.Api
{
{{#allParams}}{{#required}}
// verify the required parameter '{{paramName}}' is set
if ({{paramName}} == null) throw new ApiException(400, "Missing required parameter '{{paramName}}' when calling {{operationId}}");
if ({{paramName}} == null)
throw new ApiException(400, "Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}");
{{/required}}{{/allParams}}
var path_ = "{{path}}";
@@ -164,15 +165,21 @@ namespace {{packageName}}.Api
var headerParams = new Dictionary<String, String>(Configuration.DefaultHeader);
var formParams = new Dictionary<String, String>();
var fileParams = new Dictionary<String, FileParameter>();
String postBody = null;
Object postBody = null;
// to determine the Content-Type header
String[] httpContentTypes = new String[] {
{{#consumes}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}}
};
String httpContentType = Configuration.ApiClient.SelectHeaderContentType(httpContentTypes);
// to determine the Accept header
String[] http_header_accepts = new String[] {
String[] httpHeaderAccepts = new String[] {
{{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}}
};
String http_header_accept = Configuration.ApiClient.SelectHeaderAccept(http_header_accepts);
if (http_header_accept != null)
headerParams.Add("Accept", Configuration.ApiClient.SelectHeaderAccept(http_header_accepts));
String httpHeaderAccept = Configuration.ApiClient.SelectHeaderAccept(httpHeaderAccepts);
if (httpHeaderAccept != null)
headerParams.Add("Accept", httpHeaderAccept);
// set "format" to json by default
// e.g. /pet/{petId}.{format} becomes /pet/{petId}.json
@@ -185,11 +192,16 @@ namespace {{packageName}}.Api
{{/headerParams}}
{{#formParams}}if ({{paramName}} != null) {{#isFile}}fileParams.Add("{{baseName}}", Configuration.ApiClient.ParameterToFile("{{baseName}}", {{paramName}}));{{/isFile}}{{^isFile}}formParams.Add("{{baseName}}", Configuration.ApiClient.ParameterToString({{paramName}})); // form parameter{{/isFile}}
{{/formParams}}
{{#bodyParam}}postBody = Configuration.ApiClient.Serialize({{paramName}}); // http body (model) parameter
{{/bodyParam}}
{{#bodyParam}}if ({{paramName}}.GetType() != typeof(byte[]))
{
postBody = Configuration.ApiClient.Serialize({{paramName}}); // http body (model) parameter
}
else
{
postBody = {{paramName}}; // byte array
}{{/bodyParam}}
{{#authMethods}}
// authentication ({{name}}) required
{{#authMethods}}// authentication ({{name}}) required
{{#isApiKey}}{{#isKeyInHeader}}
var apiKeyValue = Configuration.GetApiKeyWithPrefix("{{keyParamName}}");
if (!String.IsNullOrEmpty(apiKeyValue))
@@ -214,7 +226,9 @@ namespace {{packageName}}.Api
{{/authMethods}}
// make the HTTP request
IRestResponse response = (IRestResponse) Configuration.ApiClient.CallApi(path_, Method.{{httpMethod}}, queryParams, postBody, headerParams, formParams, fileParams, pathParams);
IRestResponse response = (IRestResponse) Configuration.ApiClient.CallApi(path_,
Method.{{httpMethod}}, queryParams, postBody, headerParams, formParams, fileParams,
pathParams, httpContentType);
int statusCode = (int) response.StatusCode;
@@ -261,15 +275,21 @@ namespace {{packageName}}.Api
var headerParams = new Dictionary<String, String>();
var formParams = new Dictionary<String, String>();
var fileParams = new Dictionary<String, FileParameter>();
String postBody = null;
Object postBody = null;
// to determine the Content-Type header
String[] httpContentTypes = new String[] {
{{#consumes}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}}
};
String httpContentType = Configuration.ApiClient.SelectHeaderContentType(httpContentTypes);
// to determine the Accept header
String[] http_header_accepts = new String[] {
String[] httpHeaderAccepts = new String[] {
{{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}}
};
String http_header_accept = Configuration.ApiClient.SelectHeaderAccept(http_header_accepts);
if (http_header_accept != null)
headerParams.Add("Accept", Configuration.ApiClient.SelectHeaderAccept(http_header_accepts));
String httpHeaderAccept = Configuration.ApiClient.SelectHeaderAccept(httpHeaderAccepts);
if (httpHeaderAccept != null)
headerParams.Add("Accept", httpHeaderAccept);
// set "format" to json by default
// e.g. /pet/{petId}.{format} becomes /pet/{petId}.json
@@ -311,7 +331,9 @@ namespace {{packageName}}.Api
{{/authMethods}}
// make the HTTP request
IRestResponse response = (IRestResponse) await Configuration.ApiClient.CallApiAsync(path_, Method.{{httpMethod}}, queryParams, postBody, headerParams, formParams, fileParams, pathParams);
IRestResponse response = (IRestResponse) await Configuration.ApiClient.CallApiAsync(path_,
Method.{{httpMethod}}, queryParams, postBody, headerParams, formParams, fileParams,
pathParams, httpContentType);
int statusCode = (int) response.StatusCode;

View File

@@ -19,6 +19,49 @@
"http"
],
"paths": {
"/pet?testing_byte_array=true": {
"post": {
"tags": [
"pet"
],
"summary": "Fake endpoint to test byte array in body parameter for adding a new pet to the store",
"description": "",
"operationId": "addPetUsingByteArray",
"consumes": [
"application/json",
"application/xml"
],
"produces": [
"application/json",
"application/xml"
],
"parameters": [
{
"in": "body",
"name": "body",
"description": "Pet object in the form of byte array",
"required": false,
"schema": {
"type": "string",
"format": "binary"
}
}
],
"responses": {
"405": {
"description": "Invalid input"
}
},
"security": [
{
"petstore_auth": [
"write:pets",
"read:pets"
]
}
]
}
},
"/pet": {
"post": {
"tags": [
@@ -206,6 +249,56 @@
]
}
},
"/pet/{petId}?testing_byte_array=true": {
"get": {
"tags": [
"pet"
],
"summary": "Fake endpoint to test byte array return by 'Find pet by ID'",
"description": "Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions",
"operationId": "getPetByIdWithByteArray",
"produces": [
"application/json",
"application/xml"
],
"parameters": [
{
"name": "petId",
"in": "path",
"description": "ID of pet that needs to be fetched",
"required": true,
"type": "integer",
"format": "int64"
}
],
"responses": {
"404": {
"description": "Pet not found"
},
"200": {
"description": "successful operation",
"schema": {
"type": "string",
"format": "binary"
}
},
"400": {
"description": "Invalid ID supplied"
}
},
"security": [
{
"api_key": []
},
{
"petstore_auth": [
"write:pets",
"read:pets"
]
}
]
}
},
"/pet/{petId}": {
"get": {
"tags": [