Merge pull request #1414 from dgreenbean/feature/csharp_binary_files

Raw Binary for File Output
This commit is contained in:
wing328 2015-10-26 23:35:52 +08:00
commit 5964f7b841
2 changed files with 17 additions and 14 deletions

View File

@ -214,12 +214,14 @@ namespace {{packageName}}.Client
/// <summary> /// <summary>
/// Deserialize the JSON string into a proper object. /// Deserialize the JSON string into a proper object.
/// </summary> /// </summary>
/// <param name="content">HTTP body (e.g. string, JSON).</param> /// <param name="response">The HTTP response.</param>
/// <param name="type">Object type.</param> /// <param name="type">Object type.</param>
/// <param name="headers"></param>
/// <returns>Object representation of the JSON string.</returns> /// <returns>Object representation of the JSON string.</returns>
public object Deserialize(string content, Type type, IList<Parameter> headers=null) public object Deserialize(IRestResponse response, Type type)
{ {
byte[] data = response.RawBytes;
string content = response.Content;
IList<Parameter> headers = response.Headers;
if (type == typeof(Object)) // return an object if (type == typeof(Object)) // return an object
{ {
return content; return content;
@ -227,21 +229,22 @@ namespace {{packageName}}.Client
if (type == typeof(Stream)) if (type == typeof(Stream))
{ {
var filePath = String.IsNullOrEmpty(Configuration.TempFolderPath)
? Path.GetTempPath()
: Configuration.TempFolderPath;
var fileName = filePath + Guid.NewGuid();
if (headers != null) if (headers != null)
{ {
var filePath = String.IsNullOrEmpty(Configuration.TempFolderPath)
? Path.GetTempPath()
: Configuration.TempFolderPath;
var regex = new Regex(@"Content-Disposition:.*filename=['""]?([^'""\s]+)['""]?$"); var regex = new Regex(@"Content-Disposition:.*filename=['""]?([^'""\s]+)['""]?$");
var match = regex.Match(headers.ToString()); var match = regex.Match(headers.ToString());
if (match.Success) if (match.Success)
fileName = filePath + match.Value.Replace("\"", "").Replace("'", ""); {
string fileName = filePath + match.Value.Replace("\"", "").Replace("'", "");
File.WriteAllBytes(fileName, data);
return new FileStream(fileName, FileMode.Open);
}
} }
File.WriteAllText(fileName, content); var stream = new MemoryStream(data);
return new FileStream(fileName, FileMode.Open); return stream;
} }
if (type.Name.StartsWith("System.Nullable`1[[System.DateTime")) // return a datetime object if (type.Name.StartsWith("System.Nullable`1[[System.DateTime")) // return a datetime object

View File

@ -144,7 +144,7 @@ namespace {{packageName}}.Api
else if (((int)response.StatusCode) == 0) else if (((int)response.StatusCode) == 0)
throw new ApiException ((int)response.StatusCode, "Error calling {{nickname}}: " + response.ErrorMessage, response.ErrorMessage); throw new ApiException ((int)response.StatusCode, "Error calling {{nickname}}: " + response.ErrorMessage, response.ErrorMessage);
{{#returnType}}return ({{{returnType}}}) ApiClient.Deserialize(response.Content, typeof({{{returnType}}}), response.Headers);{{/returnType}}{{^returnType}}return;{{/returnType}} {{#returnType}}return ({{{returnType}}}) ApiClient.Deserialize(response, typeof({{{returnType}}}));{{/returnType}}{{^returnType}}return;{{/returnType}}
} }
/// <summary> /// <summary>
@ -197,7 +197,7 @@ namespace {{packageName}}.Api
if (((int)response.StatusCode) >= 400) if (((int)response.StatusCode) >= 400)
throw new ApiException ((int)response.StatusCode, "Error calling {{nickname}}: " + response.Content, response.Content); throw new ApiException ((int)response.StatusCode, "Error calling {{nickname}}: " + response.Content, response.Content);
{{#returnType}}return ({{{returnType}}}) ApiClient.Deserialize(response.Content, typeof({{{returnType}}}), response.Headers);{{/returnType}}{{^returnType}} {{#returnType}}return ({{{returnType}}}) ApiClient.Deserialize(response, typeof({{{returnType}}}));{{/returnType}}{{^returnType}}
return;{{/returnType}} return;{{/returnType}}
} }
{{/operation}} {{/operation}}