mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-08 08:30:56 +00:00
Merge branch 'develop_2.0' of github.com:swagger-api/swagger-codegen into develop_2.0
This commit is contained in:
commit
ab23cccab4
@ -7,7 +7,7 @@ import java.util.*;
|
||||
import java.io.File;
|
||||
|
||||
public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
protected String invokerPackage = "io.swagger.client";
|
||||
protected String invokerPackage = "IO.Swagger.Client";
|
||||
protected String groupId = "io.swagger";
|
||||
protected String artifactId = "swagger-csharp-client";
|
||||
protected String artifactVersion = "1.0.0";
|
||||
@ -31,8 +31,8 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
modelTemplateFiles.put("model.mustache", ".cs");
|
||||
apiTemplateFiles.put("api.mustache", ".cs");
|
||||
templateDir = "csharp";
|
||||
apiPackage = "io.swagger.Api";
|
||||
modelPackage = "io.swagger.Model";
|
||||
apiPackage = "IO.Swagger.Api";
|
||||
modelPackage = "IO.Swagger.Model";
|
||||
|
||||
reservedWords = new HashSet<String> (
|
||||
Arrays.asList(
|
||||
@ -80,7 +80,7 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
typeMapping.put("double", "double?");
|
||||
typeMapping.put("number", "double?");
|
||||
typeMapping.put("Date", "DateTime");
|
||||
typeMapping.put("file", "byte[]");
|
||||
typeMapping.put("file", "string"); // path to file
|
||||
typeMapping.put("array", "List");
|
||||
typeMapping.put("map", "Dictionary");
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using RestSharp;
|
||||
using {{invokerPackage}};
|
||||
using {{modelPackage}};
|
||||
{{#imports}}
|
||||
@ -9,101 +10,73 @@ namespace {{package}} {
|
||||
{{#operations}}
|
||||
public class {{classname}} {
|
||||
string basePath;
|
||||
private readonly ApiInvoker apiInvoker = ApiInvoker.GetInstance();
|
||||
protected RestClient restClient;
|
||||
|
||||
public {{classname}}(String basePath = "{{basePath}}")
|
||||
{
|
||||
this.basePath = basePath;
|
||||
this.restClient = new RestClient(basePath);
|
||||
}
|
||||
|
||||
public ApiInvoker getInvoker() {
|
||||
return apiInvoker;
|
||||
}
|
||||
|
||||
// Sets the endpoint base url for the services being accessed
|
||||
public void setBasePath(string basePath) {
|
||||
/// <summary>
|
||||
/// Sets the endpoint base url for the services being accessed
|
||||
/// </summary>
|
||||
/// <param name="basePath"> Base URL
|
||||
/// <returns></returns>
|
||||
public void SetBasePath(string basePath) {
|
||||
this.basePath = basePath;
|
||||
}
|
||||
|
||||
// Gets the endpoint base url for the services being accessed
|
||||
public String getBasePath() {
|
||||
return basePath;
|
||||
/// <summary>
|
||||
/// Gets the endpoint base url for the services being accessed
|
||||
/// <returns>Base URL</returns>
|
||||
/// </summary>
|
||||
public String GetBasePath() {
|
||||
return this.basePath;
|
||||
}
|
||||
|
||||
{{#operation}}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// {{summary}} {{notes}}
|
||||
/// </summary>
|
||||
{{#allParams}}/// <param name="{{paramName}}">{{description}}</param>
|
||||
{{#hasMore}} {{/hasMore}}{{/allParams}}
|
||||
/// <returns></returns>
|
||||
public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}} {{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {
|
||||
// create path and map variables
|
||||
var path = "{{path}}".Replace("{format}","json"){{#pathParams}}.Replace("{" + "{{baseName}}" + "}", apiInvoker.ParameterToString({{{paramName}}})){{/pathParams}};
|
||||
{{#allParams}} /// <param name="{{paramName}}">{{description}}</param>
|
||||
{{/allParams}}
|
||||
/// <returns>{{#returnType}}{{{returnType}}}{{/returnType}}</returns>
|
||||
public {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} {{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("{{path}}", Method.{{httpMethod}});
|
||||
|
||||
{{#requiredParamCount}}
|
||||
// verify required params are set
|
||||
if ({{/requiredParamCount}}{{#requiredParams}} {{paramName}} == null {{#hasMore}}|| {{/hasMore}}{{/requiredParams}}{{#requiredParamCount}}) {
|
||||
throw new ApiException(400, "missing required params");
|
||||
{{#allParams}}{{#required}}
|
||||
// verify the required parameter '{{paramName}}' is set
|
||||
if ({{paramName}} == null) throw new ApiException(400, "Missing required parameter '{{paramName}}' when calling {{nickname}}");
|
||||
{{/required}}{{/allParams}}
|
||||
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
{{/requiredParamCount}}
|
||||
|
||||
{{#queryParams}}if ({{paramName}} != null){
|
||||
queryParams.Add("{{baseName}}", apiInvoker.ParameterToString({{paramName}}));
|
||||
}
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
{{#pathParams}}_request.AddUrlSegment("{{baseName}}", ApiInvoker.ParameterToString({{{paramName}}})); // path (url segment) parameter
|
||||
{{/pathParams}}
|
||||
{{#queryParams}} if ({{paramName}} != null) _request.AddParameter("{{baseName}}", ApiInvoker.ParameterToString({{paramName}})); // query parameter
|
||||
{{/queryParams}}
|
||||
|
||||
{{#headerParams}}headerParams.Add("{{baseName}}", apiInvoker.ParameterToString({{paramName}}));
|
||||
{{#headerParams}} if ({{paramName}} != null) _request.AddHeader("{{baseName}}", ApiInvoker.ParameterToString({{paramName}})); // header parameter
|
||||
{{/headerParams}}
|
||||
|
||||
{{#formParams}}if ({{paramName}} != null){
|
||||
if({{paramName}} is byte[]) {
|
||||
formParams.Add("{{baseName}}", {{paramName}});
|
||||
} else {
|
||||
formParams.Add("{{baseName}}", apiInvoker.ParameterToString({{paramName}}));
|
||||
}
|
||||
}
|
||||
{{#formParams}}if ({{paramName}} != null) {{#isFile}}_request.AddFile("{{baseName}}", {{paramName}});{{/isFile}}{{^isFile}}_request.AddParameter("{{baseName}}", ApiInvoker.ParameterToString({{paramName}})); // form parameter{{/isFile}}
|
||||
{{/formParams}}
|
||||
{{#bodyParam}}_request.AddParameter("application/json", ApiInvoker.Serialize({{paramName}}), ParameterType.RequestBody); // http body (model) parameter
|
||||
{{/bodyParam}}
|
||||
|
||||
try {
|
||||
if (typeof({{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}) == typeof(byte[])) {
|
||||
{{#returnType}}
|
||||
var response = apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return ((object)response) as {{{returnType}}};
|
||||
{{/returnType}}
|
||||
{{^returnType}}
|
||||
apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return;
|
||||
{{/returnType}}
|
||||
} else {
|
||||
{{#returnType}}
|
||||
var response = apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}, headerParams, formParams);
|
||||
if (response != null){
|
||||
return ({{{returnType}}}) ApiInvoker.deserialize(response, typeof({{{returnType}}}));
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
{{/returnType}}
|
||||
{{^returnType}}
|
||||
apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}, headerParams, formParams);
|
||||
return;
|
||||
{{/returnType}}
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return {{#returnType}}null{{/returnType}};
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling {{nickname}}: " + response.Content);
|
||||
}
|
||||
{{#returnType}}return ({{{returnType}}}) ApiInvoker.Deserialize(response.Content, typeof({{{returnType}}}));{{/returnType}}{{^returnType}}
|
||||
return;{{/returnType}}
|
||||
}
|
||||
{{/operation}}
|
||||
}
|
||||
|
@ -1,21 +1,17 @@
|
||||
using System;
|
||||
|
||||
namespace {{invokerPackage}} {
|
||||
|
||||
public class ApiException : Exception {
|
||||
|
||||
private int errorCode = 0;
|
||||
|
||||
public int ErrorCode { get; set; }
|
||||
|
||||
public ApiException() {}
|
||||
|
||||
public int ErrorCode {
|
||||
get
|
||||
{
|
||||
return errorCode;
|
||||
}
|
||||
public ApiException(int errorCode, string message) : base(message) {
|
||||
this.ErrorCode = errorCode;
|
||||
}
|
||||
|
||||
public ApiException(int errorCode, string message) : base(message) {
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,235 +1,81 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace {{invokerPackage}} {
|
||||
public class ApiInvoker {
|
||||
private static readonly ApiInvoker _instance = new ApiInvoker();
|
||||
private Dictionary<String, String> defaultHeaderMap = new Dictionary<String, String>();
|
||||
namespace {{invokerPackage}} {
|
||||
public class ApiInvoker {
|
||||
private static Dictionary<String, String> defaultHeaderMap = new Dictionary<String, String>();
|
||||
|
||||
public static ApiInvoker GetInstance() {
|
||||
return _instance;
|
||||
}
|
||||
/// <summary>
|
||||
/// Add default header
|
||||
/// </summary>
|
||||
/// <param name="key"> Header field name
|
||||
/// <param name="value"> Header field value
|
||||
/// <returns></returns>
|
||||
public static void AddDefaultHeader(string key, string value) {
|
||||
defaultHeaderMap.Add(key, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add default header
|
||||
/// </summary>
|
||||
/// <param name="key"> Header field name
|
||||
/// <param name="value"> Header field value
|
||||
/// <returns></returns>
|
||||
public void addDefaultHeader(string key, string value) {
|
||||
defaultHeaderMap.Add(key, value);
|
||||
}
|
||||
/// <summary>
|
||||
/// Get default header
|
||||
/// </summary>
|
||||
/// <returns>Dictionary of default header</returns>
|
||||
public static Dictionary<String, String> GetDefaultHeader() {
|
||||
return defaultHeaderMap;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// escape string (url-encoded)
|
||||
/// </summary>
|
||||
/// <param name="str"> String to be escaped
|
||||
/// <returns>Escaped string</returns>
|
||||
public string escapeString(string str) {
|
||||
return str;
|
||||
}
|
||||
/// <summary>
|
||||
/// escape string (url-encoded)
|
||||
/// </summary>
|
||||
/// <param name="str"> String to be escaped
|
||||
/// <returns>Escaped string</returns>
|
||||
public static string EscapeString(string str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// if parameter is DateTime, output in ISO8601 format, otherwise just return the string
|
||||
/// </summary>
|
||||
/// <param name="obj"> The parameter (header, path, query, form)
|
||||
/// <returns>Formatted string</returns>
|
||||
public string ParameterToString(object obj)
|
||||
/// <summary>
|
||||
/// if parameter is DateTime, output in ISO8601 format, otherwise just return the string
|
||||
/// </summary>
|
||||
/// <param name="obj"> The parameter (header, path, query, form)
|
||||
/// <returns>Formatted string</returns>
|
||||
public static string ParameterToString(object obj)
|
||||
{
|
||||
return (obj is DateTime) ? ((DateTime)obj).ToString ("u") : Convert.ToString (obj);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserialize the JSON string into a proper object
|
||||
/// </summary>
|
||||
/// <param name="json"> JSON string
|
||||
/// <param name="type"> Object type
|
||||
/// <returns>Object representation of the JSON string</returns>
|
||||
public static object Deserialize(string json, Type type) {
|
||||
try
|
||||
{
|
||||
return (obj is DateTime) ? ((DateTime)obj).ToString ("u") : Convert.ToString (obj);
|
||||
return JsonConvert.DeserializeObject(json, type);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserialize the JSON string into a proper object
|
||||
/// </summary>
|
||||
/// <param name="json"> JSON string
|
||||
/// <param name="type"> Object type
|
||||
/// <returns>Object representation of the JSON string</returns>
|
||||
public static object deserialize(string json, Type type) {
|
||||
try
|
||||
{
|
||||
return JsonConvert.DeserializeObject(json, type);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new ApiException(500, e.Message);
|
||||
}
|
||||
|
||||
catch (IOException e) {
|
||||
throw new ApiException(500, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public static string serialize(object obj) {
|
||||
try
|
||||
{
|
||||
return obj != null ? JsonConvert.SerializeObject(obj) : null;
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new ApiException(500, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public string invokeAPI(string host, string path, string method, Dictionary<String, String> queryParams, object body, Dictionary<String, String> headerParams, Dictionary<String, object> formParams)
|
||||
/// <summary>
|
||||
/// Serialize an object into JSON string
|
||||
/// </summary>
|
||||
/// <param name="obj"> Object
|
||||
/// <returns>JSON string</returns>
|
||||
public static string Serialize(object obj) {
|
||||
try
|
||||
{
|
||||
return invokeAPIInternal(host, path, method, false, queryParams, body, headerParams, formParams) as string;
|
||||
return obj != null ? JsonConvert.SerializeObject(obj) : null;
|
||||
}
|
||||
|
||||
public byte[] invokeBinaryAPI(string host, string path, string method, Dictionary<String, String> queryParams, object body, Dictionary<String, String> headerParams, Dictionary<String, object> formParams)
|
||||
{
|
||||
return invokeAPIInternal(host, path, method, true, queryParams, body, headerParams, formParams) as byte[];
|
||||
}
|
||||
|
||||
private object invokeAPIInternal(string host, string path, string method, bool binaryResponse, Dictionary<String, String> queryParams, object body, Dictionary<String, String> headerParams, Dictionary<String, object> formParams) {
|
||||
var b = new StringBuilder();
|
||||
|
||||
foreach (var queryParamItem in queryParams)
|
||||
{
|
||||
var value = queryParamItem.Value;
|
||||
if (value == null) continue;
|
||||
b.Append(b.ToString().Length == 0 ? "?" : "&");
|
||||
b.Append(escapeString(queryParamItem.Key)).Append("=").Append(escapeString(value));
|
||||
}
|
||||
|
||||
var querystring = b.ToString();
|
||||
|
||||
host = host.EndsWith("/") ? host.Substring(0, host.Length - 1) : host;
|
||||
|
||||
var client = WebRequest.Create(host + path + querystring);
|
||||
client.Method = method;
|
||||
|
||||
byte[] formData = null;
|
||||
if (formParams.Count > 0)
|
||||
{
|
||||
string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());
|
||||
client.ContentType = "multipart/form-data; boundary=" + formDataBoundary;
|
||||
formData = GetMultipartFormData(formParams, formDataBoundary);
|
||||
client.ContentLength = formData.Length;
|
||||
}
|
||||
else
|
||||
{
|
||||
client.ContentType = "application/json";
|
||||
}
|
||||
|
||||
foreach (var headerParamsItem in headerParams)
|
||||
{
|
||||
client.Headers.Add(headerParamsItem.Key, headerParamsItem.Value);
|
||||
}
|
||||
foreach (var defaultHeaderMapItem in defaultHeaderMap.Where(defaultHeaderMapItem => !headerParams.ContainsKey(defaultHeaderMapItem.Key)))
|
||||
{
|
||||
client.Headers.Add(defaultHeaderMapItem.Key, defaultHeaderMapItem.Value);
|
||||
}
|
||||
|
||||
switch (method)
|
||||
{
|
||||
case "GET":
|
||||
break;
|
||||
case "POST":
|
||||
case "PATCH":
|
||||
case "PUT":
|
||||
case "DELETE":
|
||||
using (Stream requestStream = client.GetRequestStream())
|
||||
{
|
||||
if (formData != null)
|
||||
{
|
||||
requestStream.Write(formData, 0, formData.Length);
|
||||
}
|
||||
|
||||
var swRequestWriter = new StreamWriter(requestStream);
|
||||
swRequestWriter.Write(serialize(body));
|
||||
swRequestWriter.Close();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new ApiException(500, "unknown method type " + method);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var webResponse = (HttpWebResponse)client.GetResponse();
|
||||
if (webResponse.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
webResponse.Close();
|
||||
throw new ApiException((int)webResponse.StatusCode, webResponse.StatusDescription);
|
||||
}
|
||||
|
||||
if (binaryResponse)
|
||||
{
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
webResponse.GetResponseStream().CopyTo(memoryStream);
|
||||
return memoryStream.ToArray();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
using (var responseReader = new StreamReader(webResponse.GetResponseStream()))
|
||||
{
|
||||
var responseData = responseReader.ReadToEnd();
|
||||
return responseData;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(WebException ex)
|
||||
{
|
||||
var response = ex.Response as HttpWebResponse;
|
||||
int statusCode = 0;
|
||||
if (response != null)
|
||||
{
|
||||
statusCode = (int)response.StatusCode;
|
||||
response.Close();
|
||||
}
|
||||
throw new ApiException(statusCode, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] GetMultipartFormData(Dictionary<string, object> postParameters, string boundary)
|
||||
{
|
||||
Stream formDataStream = new System.IO.MemoryStream();
|
||||
bool needsCLRF = false;
|
||||
|
||||
foreach (var param in postParameters)
|
||||
{
|
||||
// Thanks to feedback from commenters, add a CRLF to allow multiple parameters to be added.
|
||||
// Skip it on the first parameter, add it to subsequent parameters.
|
||||
if (needsCLRF)
|
||||
formDataStream.Write(Encoding.UTF8.GetBytes("\r\n"), 0, Encoding.UTF8.GetByteCount("\r\n"));
|
||||
|
||||
needsCLRF = true;
|
||||
|
||||
if (param.Value is byte[])
|
||||
{
|
||||
string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n",
|
||||
boundary,
|
||||
param.Key,
|
||||
"application/octet-stream");
|
||||
formDataStream.Write(Encoding.UTF8.GetBytes(postData), 0, Encoding.UTF8.GetByteCount(postData));
|
||||
|
||||
// Write the file data directly to the Stream, rather than serializing it to a string.
|
||||
formDataStream.Write((param.Value as byte[]), 0, (param.Value as byte[]).Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}",
|
||||
boundary,
|
||||
param.Key,
|
||||
param.Value);
|
||||
formDataStream.Write(Encoding.UTF8.GetBytes(postData), 0, Encoding.UTF8.GetByteCount(postData));
|
||||
}
|
||||
}
|
||||
|
||||
// Add the end of the request. Start with a newline
|
||||
string footer = "\r\n--" + boundary + "--\r\n";
|
||||
formDataStream.Write(Encoding.UTF8.GetBytes(footer), 0, Encoding.UTF8.GetByteCount(footer));
|
||||
|
||||
// Dump the Stream into a byte[]
|
||||
formDataStream.Position = 0;
|
||||
byte[] formData = new byte[formDataStream.Length];
|
||||
formDataStream.Read(formData, 0, formData.Length);
|
||||
formDataStream.Close();
|
||||
|
||||
return formData;
|
||||
catch (Exception e) {
|
||||
throw new ApiException(500, e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,19 +2,19 @@ using System;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
namespace {{package}} {
|
||||
[DataContract]
|
||||
public class {{classname}} {
|
||||
{{#vars}}
|
||||
|
||||
{{#description}}/* {{{description}}} */
|
||||
{{/description}}
|
||||
{{#description}}/* {{{description}}} */{{/description}}
|
||||
[DataMember(Name="{{baseName}}", EmitDefaultValue=false)]
|
||||
public {{{datatype}}} {{name}} { get; set; }
|
||||
|
||||
{{/vars}}
|
||||
|
||||
public override string ToString() {
|
||||
var sb = new StringBuilder();
|
||||
sb.Append("class {{classname}} {\n");
|
||||
@ -27,4 +27,4 @@ namespace {{package}} {
|
||||
}
|
||||
{{/model}}
|
||||
{{/models}}
|
||||
}
|
||||
}
|
||||
|
@ -1,2 +1,2 @@
|
||||
SET CSCPATH=%SYSTEMROOT%\Microsoft.NET\Framework\v4.0.30319
|
||||
%CSCPATH%\csc /reference:bin/Newtonsoft.Json.dll /target:library /out:bin/io.swagger.client.dll /recurse:src\*.cs /doc:bin/io.swagger.client.xml
|
||||
%CSCPATH%\csc /reference:bin/Newtonsoft.Json.dll /target:library /out:bin/IO.Swagger.Client.dll /recurse:src\*.cs /doc:bin/IO.Swagger.Client.xml
|
@ -1,471 +1,336 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using io.swagger.client;
|
||||
using io.swagger.Model;
|
||||
using RestSharp;
|
||||
using IO.Swagger.Client;
|
||||
using IO.Swagger.Model;
|
||||
|
||||
namespace io.swagger.Api {
|
||||
namespace IO.Swagger.Api {
|
||||
|
||||
public class PetApi {
|
||||
string basePath;
|
||||
private readonly ApiInvoker apiInvoker = ApiInvoker.GetInstance();
|
||||
protected RestClient restClient;
|
||||
|
||||
public PetApi(String basePath = "http://petstore.swagger.io/v2")
|
||||
{
|
||||
this.basePath = basePath;
|
||||
this.restClient = new RestClient(basePath);
|
||||
}
|
||||
|
||||
public ApiInvoker getInvoker() {
|
||||
return apiInvoker;
|
||||
}
|
||||
|
||||
// Sets the endpoint base url for the services being accessed
|
||||
public void setBasePath(string basePath) {
|
||||
/// <summary>
|
||||
/// Sets the endpoint base url for the services being accessed
|
||||
/// </summary>
|
||||
/// <param name="basePath"> Base URL
|
||||
/// <returns></returns>
|
||||
public void SetBasePath(string basePath) {
|
||||
this.basePath = basePath;
|
||||
}
|
||||
|
||||
// Gets the endpoint base url for the services being accessed
|
||||
public String getBasePath() {
|
||||
return basePath;
|
||||
/// <summary>
|
||||
/// Gets the endpoint base url for the services being accessed
|
||||
/// <returns>Base URL</returns>
|
||||
/// </summary>
|
||||
public String GetBasePath() {
|
||||
return this.basePath;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Update an existing pet
|
||||
/// </summary>
|
||||
/// <param name="Body">Pet object that needs to be added to the store</param>
|
||||
|
||||
/// <returns></returns>
|
||||
public void UpdatePet (Pet Body) {
|
||||
// create path and map variables
|
||||
var path = "/pet".Replace("{format}","json");
|
||||
public void UpdatePet (Pet Body) {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("/pet", Method.PUT);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try {
|
||||
if (typeof(void) == typeof(byte[])) {
|
||||
|
||||
|
||||
apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return;
|
||||
|
||||
} else {
|
||||
|
||||
|
||||
apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, Body, headerParams, formParams);
|
||||
return;
|
||||
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return ;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
|
||||
|
||||
|
||||
|
||||
_request.AddParameter("application/json", ApiInvoker.Serialize(Body), ParameterType.RequestBody); // http body (model) parameter
|
||||
|
||||
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling UpdatePet: " + response.Content);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Add a new pet to the store
|
||||
/// </summary>
|
||||
/// <param name="Body">Pet object that needs to be added to the store</param>
|
||||
|
||||
/// <returns></returns>
|
||||
public void AddPet (Pet Body) {
|
||||
// create path and map variables
|
||||
var path = "/pet".Replace("{format}","json");
|
||||
public void AddPet (Pet Body) {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("/pet", Method.POST);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try {
|
||||
if (typeof(void) == typeof(byte[])) {
|
||||
|
||||
|
||||
apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return;
|
||||
|
||||
} else {
|
||||
|
||||
|
||||
apiInvoker.invokeAPI(basePath, path, "POST", queryParams, Body, headerParams, formParams);
|
||||
return;
|
||||
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return ;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
|
||||
|
||||
|
||||
|
||||
_request.AddParameter("application/json", ApiInvoker.Serialize(Body), ParameterType.RequestBody); // http body (model) parameter
|
||||
|
||||
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling AddPet: " + response.Content);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Finds Pets by status Multiple status values can be provided with comma seperated strings
|
||||
/// </summary>
|
||||
/// <param name="Status">Status values that need to be considered for filter</param>
|
||||
|
||||
/// <returns></returns>
|
||||
public List<Pet> FindPetsByStatus (List<string> Status) {
|
||||
// create path and map variables
|
||||
var path = "/pet/findByStatus".Replace("{format}","json");
|
||||
/// <returns>List<Pet></returns>
|
||||
public List<Pet> FindPetsByStatus (List<string> Status) {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("/pet/findByStatus", Method.GET);
|
||||
|
||||
|
||||
|
||||
if (Status != null){
|
||||
queryParams.Add("status", apiInvoker.ParameterToString(Status));
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
|
||||
if (Status != null) _request.AddParameter("status", ApiInvoker.ParameterToString(Status)); // query parameter
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try {
|
||||
if (typeof(List<Pet>) == typeof(byte[])) {
|
||||
|
||||
var response = apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return ((object)response) as List<Pet>;
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
var response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
if (response != null){
|
||||
return (List<Pet>) ApiInvoker.deserialize(response, typeof(List<Pet>));
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling FindPetsByStatus: " + response.Content);
|
||||
}
|
||||
return (List<Pet>) ApiInvoker.Deserialize(response.Content, typeof(List<Pet>));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Finds Pets by tags Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.
|
||||
/// </summary>
|
||||
/// <param name="Tags">Tags to filter by</param>
|
||||
|
||||
/// <returns></returns>
|
||||
public List<Pet> FindPetsByTags (List<string> Tags) {
|
||||
// create path and map variables
|
||||
var path = "/pet/findByTags".Replace("{format}","json");
|
||||
/// <returns>List<Pet></returns>
|
||||
public List<Pet> FindPetsByTags (List<string> Tags) {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("/pet/findByTags", Method.GET);
|
||||
|
||||
|
||||
|
||||
if (Tags != null){
|
||||
queryParams.Add("tags", apiInvoker.ParameterToString(Tags));
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
|
||||
if (Tags != null) _request.AddParameter("tags", ApiInvoker.ParameterToString(Tags)); // query parameter
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try {
|
||||
if (typeof(List<Pet>) == typeof(byte[])) {
|
||||
|
||||
var response = apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return ((object)response) as List<Pet>;
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
var response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
if (response != null){
|
||||
return (List<Pet>) ApiInvoker.deserialize(response, typeof(List<Pet>));
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling FindPetsByTags: " + response.Content);
|
||||
}
|
||||
return (List<Pet>) ApiInvoker.Deserialize(response.Content, typeof(List<Pet>));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Find pet by ID Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions
|
||||
/// </summary>
|
||||
/// <param name="PetId">ID of pet that needs to be fetched</param>
|
||||
|
||||
/// <returns></returns>
|
||||
public Pet GetPetById (long? PetId) {
|
||||
// create path and map variables
|
||||
var path = "/pet/{petId}".Replace("{format}","json").Replace("{" + "petId" + "}", apiInvoker.ParameterToString(PetId));
|
||||
/// <returns>Pet</returns>
|
||||
public Pet GetPetById (long? PetId) {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("/pet/{petId}", Method.GET);
|
||||
|
||||
|
||||
|
||||
// verify the required parameter 'PetId' is set
|
||||
if (PetId == null) throw new ApiException(400, "Missing required parameter 'PetId' when calling GetPetById");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try {
|
||||
if (typeof(Pet) == typeof(byte[])) {
|
||||
|
||||
var response = apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return ((object)response) as Pet;
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
var response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
if (response != null){
|
||||
return (Pet) ApiInvoker.deserialize(response, typeof(Pet));
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
_request.AddUrlSegment("petId", ApiInvoker.ParameterToString(PetId)); // path (url segment) parameter
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling GetPetById: " + response.Content);
|
||||
}
|
||||
return (Pet) ApiInvoker.Deserialize(response.Content, typeof(Pet));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Updates a pet in the store with form data
|
||||
/// </summary>
|
||||
/// <param name="PetId">ID of pet that needs to be updated</param>
|
||||
/// <param name="Name">Updated name of the pet</param>
|
||||
/// <param name="Status">Updated status of the pet</param>
|
||||
|
||||
/// <param name="Name">Updated name of the pet</param>
|
||||
/// <param name="Status">Updated status of the pet</param>
|
||||
/// <returns></returns>
|
||||
public void UpdatePetWithForm (string PetId, string Name, string Status) {
|
||||
// create path and map variables
|
||||
var path = "/pet/{petId}".Replace("{format}","json").Replace("{" + "petId" + "}", apiInvoker.ParameterToString(PetId));
|
||||
public void UpdatePetWithForm (string PetId, string Name, string Status) {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("/pet/{petId}", Method.POST);
|
||||
|
||||
|
||||
|
||||
// verify the required parameter 'PetId' is set
|
||||
if (PetId == null) throw new ApiException(400, "Missing required parameter 'PetId' when calling UpdatePetWithForm");
|
||||
|
||||
|
||||
|
||||
|
||||
if (Name != null){
|
||||
if(Name is byte[]) {
|
||||
formParams.Add("name", Name);
|
||||
} else {
|
||||
formParams.Add("name", apiInvoker.ParameterToString(Name));
|
||||
}
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
if (Status != null){
|
||||
if(Status is byte[]) {
|
||||
formParams.Add("status", Status);
|
||||
} else {
|
||||
formParams.Add("status", apiInvoker.ParameterToString(Status));
|
||||
}
|
||||
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
_request.AddUrlSegment("petId", ApiInvoker.ParameterToString(PetId)); // path (url segment) parameter
|
||||
|
||||
|
||||
|
||||
if (Name != null) _request.AddParameter("name", ApiInvoker.ParameterToString(Name)); // form parameter
|
||||
if (Status != null) _request.AddParameter("status", ApiInvoker.ParameterToString(Status)); // form parameter
|
||||
|
||||
|
||||
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling UpdatePetWithForm: " + response.Content);
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
if (typeof(void) == typeof(byte[])) {
|
||||
|
||||
|
||||
apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return;
|
||||
|
||||
} else {
|
||||
|
||||
|
||||
apiInvoker.invokeAPI(basePath, path, "POST", queryParams, null, headerParams, formParams);
|
||||
return;
|
||||
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return ;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a pet
|
||||
/// </summary>
|
||||
/// <param name="ApiKey"></param>
|
||||
/// <param name="PetId">Pet id to delete</param>
|
||||
|
||||
/// <param name="PetId">Pet id to delete</param>
|
||||
/// <returns></returns>
|
||||
public void DeletePet (string ApiKey, long? PetId) {
|
||||
// create path and map variables
|
||||
var path = "/pet/{petId}".Replace("{format}","json").Replace("{" + "petId" + "}", apiInvoker.ParameterToString(PetId));
|
||||
public void DeletePet (string ApiKey, long? PetId) {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("/pet/{petId}", Method.DELETE);
|
||||
|
||||
|
||||
|
||||
// verify the required parameter 'PetId' is set
|
||||
if (PetId == null) throw new ApiException(400, "Missing required parameter 'PetId' when calling DeletePet");
|
||||
|
||||
|
||||
headerParams.Add("api_key", apiInvoker.ParameterToString(ApiKey));
|
||||
|
||||
|
||||
|
||||
|
||||
try {
|
||||
if (typeof(void) == typeof(byte[])) {
|
||||
|
||||
|
||||
apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return;
|
||||
|
||||
} else {
|
||||
|
||||
|
||||
apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, formParams);
|
||||
return;
|
||||
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return ;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
_request.AddUrlSegment("petId", ApiInvoker.ParameterToString(PetId)); // path (url segment) parameter
|
||||
|
||||
|
||||
if (ApiKey != null) _request.AddHeader("api_key", ApiInvoker.ParameterToString(ApiKey)); // header parameter
|
||||
|
||||
|
||||
|
||||
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling DeletePet: " + response.Content);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// uploads an image
|
||||
/// </summary>
|
||||
/// <param name="PetId">ID of pet to update</param>
|
||||
/// <param name="AdditionalMetadata">Additional data to pass to server</param>
|
||||
/// <param name="File">file to upload</param>
|
||||
|
||||
/// <param name="AdditionalMetadata">Additional data to pass to server</param>
|
||||
/// <param name="File">file to upload</param>
|
||||
/// <returns></returns>
|
||||
public void UploadFile (long? PetId, string AdditionalMetadata, byte[] File) {
|
||||
// create path and map variables
|
||||
var path = "/pet/{petId}/uploadImage".Replace("{format}","json").Replace("{" + "petId" + "}", apiInvoker.ParameterToString(PetId));
|
||||
public void UploadFile (long? PetId, string AdditionalMetadata, string File) {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("/pet/{petId}/uploadImage", Method.POST);
|
||||
|
||||
|
||||
|
||||
// verify the required parameter 'PetId' is set
|
||||
if (PetId == null) throw new ApiException(400, "Missing required parameter 'PetId' when calling UploadFile");
|
||||
|
||||
|
||||
|
||||
|
||||
if (AdditionalMetadata != null){
|
||||
if(AdditionalMetadata is byte[]) {
|
||||
formParams.Add("additionalMetadata", AdditionalMetadata);
|
||||
} else {
|
||||
formParams.Add("additionalMetadata", apiInvoker.ParameterToString(AdditionalMetadata));
|
||||
}
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
if (File != null){
|
||||
if(File is byte[]) {
|
||||
formParams.Add("file", File);
|
||||
} else {
|
||||
formParams.Add("file", apiInvoker.ParameterToString(File));
|
||||
}
|
||||
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
_request.AddUrlSegment("petId", ApiInvoker.ParameterToString(PetId)); // path (url segment) parameter
|
||||
|
||||
|
||||
|
||||
if (AdditionalMetadata != null) _request.AddParameter("additionalMetadata", ApiInvoker.ParameterToString(AdditionalMetadata)); // form parameter
|
||||
if (File != null) _request.AddFile("file", File);
|
||||
|
||||
|
||||
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling UploadFile: " + response.Content);
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
if (typeof(void) == typeof(byte[])) {
|
||||
|
||||
|
||||
apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return;
|
||||
|
||||
} else {
|
||||
|
||||
|
||||
apiInvoker.invokeAPI(basePath, path, "POST", queryParams, null, headerParams, formParams);
|
||||
return;
|
||||
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return ;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,238 +1,178 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using io.swagger.client;
|
||||
using io.swagger.Model;
|
||||
using RestSharp;
|
||||
using IO.Swagger.Client;
|
||||
using IO.Swagger.Model;
|
||||
|
||||
namespace io.swagger.Api {
|
||||
namespace IO.Swagger.Api {
|
||||
|
||||
public class StoreApi {
|
||||
string basePath;
|
||||
private readonly ApiInvoker apiInvoker = ApiInvoker.GetInstance();
|
||||
protected RestClient restClient;
|
||||
|
||||
public StoreApi(String basePath = "http://petstore.swagger.io/v2")
|
||||
{
|
||||
this.basePath = basePath;
|
||||
this.restClient = new RestClient(basePath);
|
||||
}
|
||||
|
||||
public ApiInvoker getInvoker() {
|
||||
return apiInvoker;
|
||||
}
|
||||
|
||||
// Sets the endpoint base url for the services being accessed
|
||||
public void setBasePath(string basePath) {
|
||||
/// <summary>
|
||||
/// Sets the endpoint base url for the services being accessed
|
||||
/// </summary>
|
||||
/// <param name="basePath"> Base URL
|
||||
/// <returns></returns>
|
||||
public void SetBasePath(string basePath) {
|
||||
this.basePath = basePath;
|
||||
}
|
||||
|
||||
// Gets the endpoint base url for the services being accessed
|
||||
public String getBasePath() {
|
||||
return basePath;
|
||||
/// <summary>
|
||||
/// Gets the endpoint base url for the services being accessed
|
||||
/// <returns>Base URL</returns>
|
||||
/// </summary>
|
||||
public String GetBasePath() {
|
||||
return this.basePath;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns pet inventories by status Returns a map of status codes to quantities
|
||||
/// </summary>
|
||||
|
||||
/// <returns></returns>
|
||||
public Dictionary<String, int?> GetInventory () {
|
||||
// create path and map variables
|
||||
var path = "/store/inventory".Replace("{format}","json");
|
||||
/// <returns>Dictionary<String, int?></returns>
|
||||
public Dictionary<String, int?> GetInventory () {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("/store/inventory", Method.GET);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try {
|
||||
if (typeof(Dictionary<String, int?>) == typeof(byte[])) {
|
||||
|
||||
var response = apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return ((object)response) as Dictionary<String, int?>;
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
var response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
if (response != null){
|
||||
return (Dictionary<String, int?>) ApiInvoker.deserialize(response, typeof(Dictionary<String, int?>));
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling GetInventory: " + response.Content);
|
||||
}
|
||||
return (Dictionary<String, int?>) ApiInvoker.Deserialize(response.Content, typeof(Dictionary<String, int?>));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Place an order for a pet
|
||||
/// </summary>
|
||||
/// <param name="Body">order placed for purchasing the pet</param>
|
||||
|
||||
/// <returns></returns>
|
||||
public Order PlaceOrder (Order Body) {
|
||||
// create path and map variables
|
||||
var path = "/store/order".Replace("{format}","json");
|
||||
/// <returns>Order</returns>
|
||||
public Order PlaceOrder (Order Body) {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("/store/order", Method.POST);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try {
|
||||
if (typeof(Order) == typeof(byte[])) {
|
||||
|
||||
var response = apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return ((object)response) as Order;
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
var response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, Body, headerParams, formParams);
|
||||
if (response != null){
|
||||
return (Order) ApiInvoker.deserialize(response, typeof(Order));
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
|
||||
|
||||
|
||||
|
||||
_request.AddParameter("application/json", ApiInvoker.Serialize(Body), ParameterType.RequestBody); // http body (model) parameter
|
||||
|
||||
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling PlaceOrder: " + response.Content);
|
||||
}
|
||||
return (Order) ApiInvoker.Deserialize(response.Content, typeof(Order));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Find purchase order by ID For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions
|
||||
/// </summary>
|
||||
/// <param name="OrderId">ID of pet that needs to be fetched</param>
|
||||
|
||||
/// <returns></returns>
|
||||
public Order GetOrderById (string OrderId) {
|
||||
// create path and map variables
|
||||
var path = "/store/order/{orderId}".Replace("{format}","json").Replace("{" + "orderId" + "}", apiInvoker.ParameterToString(OrderId));
|
||||
/// <returns>Order</returns>
|
||||
public Order GetOrderById (string OrderId) {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("/store/order/{orderId}", Method.GET);
|
||||
|
||||
|
||||
|
||||
// verify the required parameter 'OrderId' is set
|
||||
if (OrderId == null) throw new ApiException(400, "Missing required parameter 'OrderId' when calling GetOrderById");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try {
|
||||
if (typeof(Order) == typeof(byte[])) {
|
||||
|
||||
var response = apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return ((object)response) as Order;
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
var response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
if (response != null){
|
||||
return (Order) ApiInvoker.deserialize(response, typeof(Order));
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
_request.AddUrlSegment("orderId", ApiInvoker.ParameterToString(OrderId)); // path (url segment) parameter
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling GetOrderById: " + response.Content);
|
||||
}
|
||||
return (Order) ApiInvoker.Deserialize(response.Content, typeof(Order));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Delete purchase order by ID For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
|
||||
/// </summary>
|
||||
/// <param name="OrderId">ID of the order that needs to be deleted</param>
|
||||
|
||||
/// <returns></returns>
|
||||
public void DeleteOrder (string OrderId) {
|
||||
// create path and map variables
|
||||
var path = "/store/order/{orderId}".Replace("{format}","json").Replace("{" + "orderId" + "}", apiInvoker.ParameterToString(OrderId));
|
||||
public void DeleteOrder (string OrderId) {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("/store/order/{orderId}", Method.DELETE);
|
||||
|
||||
|
||||
|
||||
// verify the required parameter 'OrderId' is set
|
||||
if (OrderId == null) throw new ApiException(400, "Missing required parameter 'OrderId' when calling DeleteOrder");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try {
|
||||
if (typeof(void) == typeof(byte[])) {
|
||||
|
||||
|
||||
apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return;
|
||||
|
||||
} else {
|
||||
|
||||
|
||||
apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, formParams);
|
||||
return;
|
||||
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return ;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
_request.AddUrlSegment("orderId", ApiInvoker.ParameterToString(OrderId)); // path (url segment) parameter
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling DeleteOrder: " + response.Content);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,433 +1,326 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using io.swagger.client;
|
||||
using io.swagger.Model;
|
||||
using RestSharp;
|
||||
using IO.Swagger.Client;
|
||||
using IO.Swagger.Model;
|
||||
|
||||
namespace io.swagger.Api {
|
||||
namespace IO.Swagger.Api {
|
||||
|
||||
public class UserApi {
|
||||
string basePath;
|
||||
private readonly ApiInvoker apiInvoker = ApiInvoker.GetInstance();
|
||||
protected RestClient restClient;
|
||||
|
||||
public UserApi(String basePath = "http://petstore.swagger.io/v2")
|
||||
{
|
||||
this.basePath = basePath;
|
||||
this.restClient = new RestClient(basePath);
|
||||
}
|
||||
|
||||
public ApiInvoker getInvoker() {
|
||||
return apiInvoker;
|
||||
}
|
||||
|
||||
// Sets the endpoint base url for the services being accessed
|
||||
public void setBasePath(string basePath) {
|
||||
/// <summary>
|
||||
/// Sets the endpoint base url for the services being accessed
|
||||
/// </summary>
|
||||
/// <param name="basePath"> Base URL
|
||||
/// <returns></returns>
|
||||
public void SetBasePath(string basePath) {
|
||||
this.basePath = basePath;
|
||||
}
|
||||
|
||||
// Gets the endpoint base url for the services being accessed
|
||||
public String getBasePath() {
|
||||
return basePath;
|
||||
/// <summary>
|
||||
/// Gets the endpoint base url for the services being accessed
|
||||
/// <returns>Base URL</returns>
|
||||
/// </summary>
|
||||
public String GetBasePath() {
|
||||
return this.basePath;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Create user This can only be done by the logged in user.
|
||||
/// </summary>
|
||||
/// <param name="Body">Created user object</param>
|
||||
|
||||
/// <returns></returns>
|
||||
public void CreateUser (User Body) {
|
||||
// create path and map variables
|
||||
var path = "/user".Replace("{format}","json");
|
||||
public void CreateUser (User Body) {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("/user", Method.POST);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try {
|
||||
if (typeof(void) == typeof(byte[])) {
|
||||
|
||||
|
||||
apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return;
|
||||
|
||||
} else {
|
||||
|
||||
|
||||
apiInvoker.invokeAPI(basePath, path, "POST", queryParams, Body, headerParams, formParams);
|
||||
return;
|
||||
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return ;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
|
||||
|
||||
|
||||
|
||||
_request.AddParameter("application/json", ApiInvoker.Serialize(Body), ParameterType.RequestBody); // http body (model) parameter
|
||||
|
||||
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling CreateUser: " + response.Content);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates list of users with given input array
|
||||
/// </summary>
|
||||
/// <param name="Body">List of user object</param>
|
||||
|
||||
/// <returns></returns>
|
||||
public void CreateUsersWithArrayInput (List<User> Body) {
|
||||
// create path and map variables
|
||||
var path = "/user/createWithArray".Replace("{format}","json");
|
||||
public void CreateUsersWithArrayInput (List<User> Body) {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("/user/createWithArray", Method.POST);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try {
|
||||
if (typeof(void) == typeof(byte[])) {
|
||||
|
||||
|
||||
apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return;
|
||||
|
||||
} else {
|
||||
|
||||
|
||||
apiInvoker.invokeAPI(basePath, path, "POST", queryParams, Body, headerParams, formParams);
|
||||
return;
|
||||
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return ;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
|
||||
|
||||
|
||||
|
||||
_request.AddParameter("application/json", ApiInvoker.Serialize(Body), ParameterType.RequestBody); // http body (model) parameter
|
||||
|
||||
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling CreateUsersWithArrayInput: " + response.Content);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates list of users with given input array
|
||||
/// </summary>
|
||||
/// <param name="Body">List of user object</param>
|
||||
|
||||
/// <returns></returns>
|
||||
public void CreateUsersWithListInput (List<User> Body) {
|
||||
// create path and map variables
|
||||
var path = "/user/createWithList".Replace("{format}","json");
|
||||
public void CreateUsersWithListInput (List<User> Body) {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("/user/createWithList", Method.POST);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try {
|
||||
if (typeof(void) == typeof(byte[])) {
|
||||
|
||||
|
||||
apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return;
|
||||
|
||||
} else {
|
||||
|
||||
|
||||
apiInvoker.invokeAPI(basePath, path, "POST", queryParams, Body, headerParams, formParams);
|
||||
return;
|
||||
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return ;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
|
||||
|
||||
|
||||
|
||||
_request.AddParameter("application/json", ApiInvoker.Serialize(Body), ParameterType.RequestBody); // http body (model) parameter
|
||||
|
||||
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling CreateUsersWithListInput: " + response.Content);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Logs user into the system
|
||||
/// </summary>
|
||||
/// <param name="Username">The user name for login</param>
|
||||
/// <param name="Password">The password for login in clear text</param>
|
||||
|
||||
/// <returns></returns>
|
||||
public string LoginUser (string Username, string Password) {
|
||||
// create path and map variables
|
||||
var path = "/user/login".Replace("{format}","json");
|
||||
/// <param name="Password">The password for login in clear text</param>
|
||||
/// <returns>string</returns>
|
||||
public string LoginUser (string Username, string Password) {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("/user/login", Method.GET);
|
||||
|
||||
|
||||
|
||||
if (Username != null){
|
||||
queryParams.Add("username", apiInvoker.ParameterToString(Username));
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
if (Password != null){
|
||||
queryParams.Add("password", apiInvoker.ParameterToString(Password));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try {
|
||||
if (typeof(string) == typeof(byte[])) {
|
||||
|
||||
var response = apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return ((object)response) as string;
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
var response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
if (response != null){
|
||||
return (string) ApiInvoker.deserialize(response, typeof(string));
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
|
||||
if (Username != null) _request.AddParameter("username", ApiInvoker.ParameterToString(Username)); // query parameter
|
||||
if (Password != null) _request.AddParameter("password", ApiInvoker.ParameterToString(Password)); // query parameter
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling LoginUser: " + response.Content);
|
||||
}
|
||||
return (string) ApiInvoker.Deserialize(response.Content, typeof(string));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Logs out current logged in user session
|
||||
/// </summary>
|
||||
|
||||
/// <returns></returns>
|
||||
public void LogoutUser () {
|
||||
// create path and map variables
|
||||
var path = "/user/logout".Replace("{format}","json");
|
||||
public void LogoutUser () {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("/user/logout", Method.GET);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try {
|
||||
if (typeof(void) == typeof(byte[])) {
|
||||
|
||||
|
||||
apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return;
|
||||
|
||||
} else {
|
||||
|
||||
|
||||
apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return;
|
||||
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return ;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling LogoutUser: " + response.Content);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get user by user name
|
||||
/// </summary>
|
||||
/// <param name="Username">The name that needs to be fetched. Use user1 for testing. </param>
|
||||
|
||||
/// <returns></returns>
|
||||
public User GetUserByName (string Username) {
|
||||
// create path and map variables
|
||||
var path = "/user/{username}".Replace("{format}","json").Replace("{" + "username" + "}", apiInvoker.ParameterToString(Username));
|
||||
/// <returns>User</returns>
|
||||
public User GetUserByName (string Username) {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("/user/{username}", Method.GET);
|
||||
|
||||
|
||||
|
||||
// verify the required parameter 'Username' is set
|
||||
if (Username == null) throw new ApiException(400, "Missing required parameter 'Username' when calling GetUserByName");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try {
|
||||
if (typeof(User) == typeof(byte[])) {
|
||||
|
||||
var response = apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return ((object)response) as User;
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
var response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
if (response != null){
|
||||
return (User) ApiInvoker.deserialize(response, typeof(User));
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
_request.AddUrlSegment("username", ApiInvoker.ParameterToString(Username)); // path (url segment) parameter
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling GetUserByName: " + response.Content);
|
||||
}
|
||||
return (User) ApiInvoker.Deserialize(response.Content, typeof(User));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Updated user This can only be done by the logged in user.
|
||||
/// </summary>
|
||||
/// <param name="Username">name that need to be deleted</param>
|
||||
/// <param name="Body">Updated user object</param>
|
||||
|
||||
/// <param name="Body">Updated user object</param>
|
||||
/// <returns></returns>
|
||||
public void UpdateUser (string Username, User Body) {
|
||||
// create path and map variables
|
||||
var path = "/user/{username}".Replace("{format}","json").Replace("{" + "username" + "}", apiInvoker.ParameterToString(Username));
|
||||
public void UpdateUser (string Username, User Body) {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("/user/{username}", Method.PUT);
|
||||
|
||||
|
||||
|
||||
// verify the required parameter 'Username' is set
|
||||
if (Username == null) throw new ApiException(400, "Missing required parameter 'Username' when calling UpdateUser");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try {
|
||||
if (typeof(void) == typeof(byte[])) {
|
||||
|
||||
|
||||
apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return;
|
||||
|
||||
} else {
|
||||
|
||||
|
||||
apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, Body, headerParams, formParams);
|
||||
return;
|
||||
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return ;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
_request.AddUrlSegment("username", ApiInvoker.ParameterToString(Username)); // path (url segment) parameter
|
||||
|
||||
|
||||
|
||||
|
||||
_request.AddParameter("application/json", ApiInvoker.Serialize(Body), ParameterType.RequestBody); // http body (model) parameter
|
||||
|
||||
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling UpdateUser: " + response.Content);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Delete user This can only be done by the logged in user.
|
||||
/// </summary>
|
||||
/// <param name="Username">The name that needs to be deleted</param>
|
||||
|
||||
/// <returns></returns>
|
||||
public void DeleteUser (string Username) {
|
||||
// create path and map variables
|
||||
var path = "/user/{username}".Replace("{format}","json").Replace("{" + "username" + "}", apiInvoker.ParameterToString(Username));
|
||||
public void DeleteUser (string Username) {
|
||||
|
||||
// query params
|
||||
var queryParams = new Dictionary<String, String>();
|
||||
var headerParams = new Dictionary<String, String>();
|
||||
var formParams = new Dictionary<String, object>();
|
||||
var _request = new RestRequest("/user/{username}", Method.DELETE);
|
||||
|
||||
|
||||
|
||||
// verify the required parameter 'Username' is set
|
||||
if (Username == null) throw new ApiException(400, "Missing required parameter 'Username' when calling DeleteUser");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try {
|
||||
if (typeof(void) == typeof(byte[])) {
|
||||
|
||||
|
||||
apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
|
||||
return;
|
||||
|
||||
} else {
|
||||
|
||||
|
||||
apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, formParams);
|
||||
return;
|
||||
|
||||
}
|
||||
} catch (ApiException ex) {
|
||||
if(ex.ErrorCode == 404) {
|
||||
return ;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
// add default header, if any
|
||||
foreach(KeyValuePair<string, string> defaultHeader in ApiInvoker.GetDefaultHeader())
|
||||
{
|
||||
_request.AddHeader(defaultHeader.Key, defaultHeader.Value);
|
||||
}
|
||||
|
||||
_request.AddUrlSegment("format", "json"); // set format to json by default
|
||||
_request.AddUrlSegment("username", ApiInvoker.ParameterToString(Username)); // path (url segment) parameter
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// make the HTTP request
|
||||
IRestResponse response = restClient.Execute(_request);
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling DeleteUser: " + response.Content);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,21 +2,22 @@ using System;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace io.swagger.Model {
|
||||
namespace IO.Swagger.Model {
|
||||
[DataContract]
|
||||
public class Category {
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="id", EmitDefaultValue=false)]
|
||||
public long? Id { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="name", EmitDefaultValue=false)]
|
||||
public string Name { get; set; }
|
||||
|
||||
|
||||
|
||||
public override string ToString() {
|
||||
var sb = new StringBuilder();
|
||||
sb.Append("class Category {\n");
|
||||
@ -31,4 +32,4 @@ namespace io.swagger.Model {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -2,42 +2,42 @@ using System;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace io.swagger.Model {
|
||||
namespace IO.Swagger.Model {
|
||||
[DataContract]
|
||||
public class Order {
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="id", EmitDefaultValue=false)]
|
||||
public long? Id { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="petId", EmitDefaultValue=false)]
|
||||
public long? PetId { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="quantity", EmitDefaultValue=false)]
|
||||
public int? Quantity { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="shipDate", EmitDefaultValue=false)]
|
||||
public DateTime ShipDate { get; set; }
|
||||
|
||||
|
||||
|
||||
/* Order Status */
|
||||
|
||||
[DataMember(Name="status", EmitDefaultValue=false)]
|
||||
public string Status { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="complete", EmitDefaultValue=false)]
|
||||
public bool? Complete { get; set; }
|
||||
|
||||
|
||||
|
||||
public override string ToString() {
|
||||
var sb = new StringBuilder();
|
||||
sb.Append("class Order {\n");
|
||||
@ -60,4 +60,4 @@ namespace io.swagger.Model {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -2,42 +2,42 @@ using System;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace io.swagger.Model {
|
||||
namespace IO.Swagger.Model {
|
||||
[DataContract]
|
||||
public class Pet {
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="id", EmitDefaultValue=false)]
|
||||
public long? Id { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="category", EmitDefaultValue=false)]
|
||||
public Category Category { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="name", EmitDefaultValue=false)]
|
||||
public string Name { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="photoUrls", EmitDefaultValue=false)]
|
||||
public List<string> PhotoUrls { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="tags", EmitDefaultValue=false)]
|
||||
public List<Tag> Tags { get; set; }
|
||||
|
||||
|
||||
|
||||
/* pet status in the store */
|
||||
|
||||
[DataMember(Name="status", EmitDefaultValue=false)]
|
||||
public string Status { get; set; }
|
||||
|
||||
|
||||
|
||||
public override string ToString() {
|
||||
var sb = new StringBuilder();
|
||||
sb.Append("class Pet {\n");
|
||||
@ -60,4 +60,4 @@ namespace io.swagger.Model {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -2,21 +2,22 @@ using System;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace io.swagger.Model {
|
||||
namespace IO.Swagger.Model {
|
||||
[DataContract]
|
||||
public class Tag {
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="id", EmitDefaultValue=false)]
|
||||
public long? Id { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="name", EmitDefaultValue=false)]
|
||||
public string Name { get; set; }
|
||||
|
||||
|
||||
|
||||
public override string ToString() {
|
||||
var sb = new StringBuilder();
|
||||
sb.Append("class Tag {\n");
|
||||
@ -31,4 +32,4 @@ namespace io.swagger.Model {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -2,52 +2,52 @@ using System;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace io.swagger.Model {
|
||||
namespace IO.Swagger.Model {
|
||||
[DataContract]
|
||||
public class User {
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="id", EmitDefaultValue=false)]
|
||||
public long? Id { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="username", EmitDefaultValue=false)]
|
||||
public string Username { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="firstName", EmitDefaultValue=false)]
|
||||
public string FirstName { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="lastName", EmitDefaultValue=false)]
|
||||
public string LastName { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="email", EmitDefaultValue=false)]
|
||||
public string Email { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="password", EmitDefaultValue=false)]
|
||||
public string Password { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[DataMember(Name="phone", EmitDefaultValue=false)]
|
||||
public string Phone { get; set; }
|
||||
|
||||
|
||||
|
||||
/* User Status */
|
||||
|
||||
[DataMember(Name="userStatus", EmitDefaultValue=false)]
|
||||
public int? UserStatus { get; set; }
|
||||
|
||||
|
||||
|
||||
public override string ToString() {
|
||||
var sb = new StringBuilder();
|
||||
sb.Append("class User {\n");
|
||||
@ -74,4 +74,4 @@ namespace io.swagger.Model {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,21 +1,17 @@
|
||||
using System;
|
||||
|
||||
namespace io.swagger.client {
|
||||
namespace IO.Swagger.Client {
|
||||
|
||||
public class ApiException : Exception {
|
||||
|
||||
private int errorCode = 0;
|
||||
|
||||
public int ErrorCode { get; set; }
|
||||
|
||||
public ApiException() {}
|
||||
|
||||
public int ErrorCode {
|
||||
get
|
||||
{
|
||||
return errorCode;
|
||||
}
|
||||
public ApiException(int errorCode, string message) : base(message) {
|
||||
this.ErrorCode = errorCode;
|
||||
}
|
||||
|
||||
public ApiException(int errorCode, string message) : base(message) {
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,235 +1,81 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace io.swagger.client {
|
||||
public class ApiInvoker {
|
||||
private static readonly ApiInvoker _instance = new ApiInvoker();
|
||||
private Dictionary<String, String> defaultHeaderMap = new Dictionary<String, String>();
|
||||
namespace IO.Swagger.Client {
|
||||
public class ApiInvoker {
|
||||
private static Dictionary<String, String> defaultHeaderMap = new Dictionary<String, String>();
|
||||
|
||||
public static ApiInvoker GetInstance() {
|
||||
return _instance;
|
||||
}
|
||||
/// <summary>
|
||||
/// Add default header
|
||||
/// </summary>
|
||||
/// <param name="key"> Header field name
|
||||
/// <param name="value"> Header field value
|
||||
/// <returns></returns>
|
||||
public static void AddDefaultHeader(string key, string value) {
|
||||
defaultHeaderMap.Add(key, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add default header
|
||||
/// </summary>
|
||||
/// <param name="key"> Header field name
|
||||
/// <param name="value"> Header field value
|
||||
/// <returns></returns>
|
||||
public void addDefaultHeader(string key, string value) {
|
||||
defaultHeaderMap.Add(key, value);
|
||||
}
|
||||
/// <summary>
|
||||
/// Get default header
|
||||
/// </summary>
|
||||
/// <returns>Dictionary of default header</returns>
|
||||
public static Dictionary<String, String> GetDefaultHeader() {
|
||||
return defaultHeaderMap;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// escape string (url-encoded)
|
||||
/// </summary>
|
||||
/// <param name="str"> String to be escaped
|
||||
/// <returns>Escaped string</returns>
|
||||
public string escapeString(string str) {
|
||||
return str;
|
||||
}
|
||||
/// <summary>
|
||||
/// escape string (url-encoded)
|
||||
/// </summary>
|
||||
/// <param name="str"> String to be escaped
|
||||
/// <returns>Escaped string</returns>
|
||||
public static string EscapeString(string str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// if parameter is DateTime, output in ISO8601 format, otherwise just return the string
|
||||
/// </summary>
|
||||
/// <param name="obj"> The parameter (header, path, query, form)
|
||||
/// <returns>Formatted string</returns>
|
||||
public string ParameterToString(object obj)
|
||||
/// <summary>
|
||||
/// if parameter is DateTime, output in ISO8601 format, otherwise just return the string
|
||||
/// </summary>
|
||||
/// <param name="obj"> The parameter (header, path, query, form)
|
||||
/// <returns>Formatted string</returns>
|
||||
public static string ParameterToString(object obj)
|
||||
{
|
||||
return (obj is DateTime) ? ((DateTime)obj).ToString ("u") : Convert.ToString (obj);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserialize the JSON string into a proper object
|
||||
/// </summary>
|
||||
/// <param name="json"> JSON string
|
||||
/// <param name="type"> Object type
|
||||
/// <returns>Object representation of the JSON string</returns>
|
||||
public static object Deserialize(string json, Type type) {
|
||||
try
|
||||
{
|
||||
return (obj is DateTime) ? ((DateTime)obj).ToString ("u") : Convert.ToString (obj);
|
||||
return JsonConvert.DeserializeObject(json, type);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserialize the JSON string into a proper object
|
||||
/// </summary>
|
||||
/// <param name="json"> JSON string
|
||||
/// <param name="type"> Object type
|
||||
/// <returns>Object representation of the JSON string</returns>
|
||||
public static object deserialize(string json, Type type) {
|
||||
try
|
||||
{
|
||||
return JsonConvert.DeserializeObject(json, type);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new ApiException(500, e.Message);
|
||||
}
|
||||
|
||||
catch (IOException e) {
|
||||
throw new ApiException(500, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public static string serialize(object obj) {
|
||||
try
|
||||
{
|
||||
return obj != null ? JsonConvert.SerializeObject(obj) : null;
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new ApiException(500, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public string invokeAPI(string host, string path, string method, Dictionary<String, String> queryParams, object body, Dictionary<String, String> headerParams, Dictionary<String, object> formParams)
|
||||
/// <summary>
|
||||
/// Serialize an object into JSON string
|
||||
/// </summary>
|
||||
/// <param name="obj"> Object
|
||||
/// <returns>JSON string</returns>
|
||||
public static string Serialize(object obj) {
|
||||
try
|
||||
{
|
||||
return invokeAPIInternal(host, path, method, false, queryParams, body, headerParams, formParams) as string;
|
||||
return obj != null ? JsonConvert.SerializeObject(obj) : null;
|
||||
}
|
||||
|
||||
public byte[] invokeBinaryAPI(string host, string path, string method, Dictionary<String, String> queryParams, object body, Dictionary<String, String> headerParams, Dictionary<String, object> formParams)
|
||||
{
|
||||
return invokeAPIInternal(host, path, method, true, queryParams, body, headerParams, formParams) as byte[];
|
||||
}
|
||||
|
||||
private object invokeAPIInternal(string host, string path, string method, bool binaryResponse, Dictionary<String, String> queryParams, object body, Dictionary<String, String> headerParams, Dictionary<String, object> formParams) {
|
||||
var b = new StringBuilder();
|
||||
|
||||
foreach (var queryParamItem in queryParams)
|
||||
{
|
||||
var value = queryParamItem.Value;
|
||||
if (value == null) continue;
|
||||
b.Append(b.ToString().Length == 0 ? "?" : "&");
|
||||
b.Append(escapeString(queryParamItem.Key)).Append("=").Append(escapeString(value));
|
||||
}
|
||||
|
||||
var querystring = b.ToString();
|
||||
|
||||
host = host.EndsWith("/") ? host.Substring(0, host.Length - 1) : host;
|
||||
|
||||
var client = WebRequest.Create(host + path + querystring);
|
||||
client.Method = method;
|
||||
|
||||
byte[] formData = null;
|
||||
if (formParams.Count > 0)
|
||||
{
|
||||
string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());
|
||||
client.ContentType = "multipart/form-data; boundary=" + formDataBoundary;
|
||||
formData = GetMultipartFormData(formParams, formDataBoundary);
|
||||
client.ContentLength = formData.Length;
|
||||
}
|
||||
else
|
||||
{
|
||||
client.ContentType = "application/json";
|
||||
}
|
||||
|
||||
foreach (var headerParamsItem in headerParams)
|
||||
{
|
||||
client.Headers.Add(headerParamsItem.Key, headerParamsItem.Value);
|
||||
}
|
||||
foreach (var defaultHeaderMapItem in defaultHeaderMap.Where(defaultHeaderMapItem => !headerParams.ContainsKey(defaultHeaderMapItem.Key)))
|
||||
{
|
||||
client.Headers.Add(defaultHeaderMapItem.Key, defaultHeaderMapItem.Value);
|
||||
}
|
||||
|
||||
switch (method)
|
||||
{
|
||||
case "GET":
|
||||
break;
|
||||
case "POST":
|
||||
case "PATCH":
|
||||
case "PUT":
|
||||
case "DELETE":
|
||||
using (Stream requestStream = client.GetRequestStream())
|
||||
{
|
||||
if (formData != null)
|
||||
{
|
||||
requestStream.Write(formData, 0, formData.Length);
|
||||
}
|
||||
|
||||
var swRequestWriter = new StreamWriter(requestStream);
|
||||
swRequestWriter.Write(serialize(body));
|
||||
swRequestWriter.Close();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new ApiException(500, "unknown method type " + method);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var webResponse = (HttpWebResponse)client.GetResponse();
|
||||
if (webResponse.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
webResponse.Close();
|
||||
throw new ApiException((int)webResponse.StatusCode, webResponse.StatusDescription);
|
||||
}
|
||||
|
||||
if (binaryResponse)
|
||||
{
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
webResponse.GetResponseStream().CopyTo(memoryStream);
|
||||
return memoryStream.ToArray();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
using (var responseReader = new StreamReader(webResponse.GetResponseStream()))
|
||||
{
|
||||
var responseData = responseReader.ReadToEnd();
|
||||
return responseData;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(WebException ex)
|
||||
{
|
||||
var response = ex.Response as HttpWebResponse;
|
||||
int statusCode = 0;
|
||||
if (response != null)
|
||||
{
|
||||
statusCode = (int)response.StatusCode;
|
||||
response.Close();
|
||||
}
|
||||
throw new ApiException(statusCode, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] GetMultipartFormData(Dictionary<string, object> postParameters, string boundary)
|
||||
{
|
||||
Stream formDataStream = new System.IO.MemoryStream();
|
||||
bool needsCLRF = false;
|
||||
|
||||
foreach (var param in postParameters)
|
||||
{
|
||||
// Thanks to feedback from commenters, add a CRLF to allow multiple parameters to be added.
|
||||
// Skip it on the first parameter, add it to subsequent parameters.
|
||||
if (needsCLRF)
|
||||
formDataStream.Write(Encoding.UTF8.GetBytes("\r\n"), 0, Encoding.UTF8.GetByteCount("\r\n"));
|
||||
|
||||
needsCLRF = true;
|
||||
|
||||
if (param.Value is byte[])
|
||||
{
|
||||
string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n",
|
||||
boundary,
|
||||
param.Key,
|
||||
"application/octet-stream");
|
||||
formDataStream.Write(Encoding.UTF8.GetBytes(postData), 0, Encoding.UTF8.GetByteCount(postData));
|
||||
|
||||
// Write the file data directly to the Stream, rather than serializing it to a string.
|
||||
formDataStream.Write((param.Value as byte[]), 0, (param.Value as byte[]).Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}",
|
||||
boundary,
|
||||
param.Key,
|
||||
param.Value);
|
||||
formDataStream.Write(Encoding.UTF8.GetBytes(postData), 0, Encoding.UTF8.GetByteCount(postData));
|
||||
}
|
||||
}
|
||||
|
||||
// Add the end of the request. Start with a newline
|
||||
string footer = "\r\n--" + boundary + "--\r\n";
|
||||
formDataStream.Write(Encoding.UTF8.GetBytes(footer), 0, Encoding.UTF8.GetByteCount(footer));
|
||||
|
||||
// Dump the Stream into a byte[]
|
||||
formDataStream.Position = 0;
|
||||
byte[] formData = new byte[formDataStream.Length];
|
||||
formDataStream.Read(formData, 0, formData.Length);
|
||||
formDataStream.Close();
|
||||
|
||||
return formData;
|
||||
catch (Exception e) {
|
||||
throw new ApiException(500, e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user