using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using RestSharp;
namespace {{invokerPackage}} {
///
/// API client is mainly responible for making the HTTP call to the API backend
///
public class ApiClient {
///
/// Initializes a new instance of the class.
///
/// The base path.
public ApiClient(String basePath="{{basePath}}") {
this.basePath = basePath;
this.restClient = new RestClient(this.basePath);
}
///
/// Gets or sets the base path.
///
/// The base path.
public string basePath { get; set; }
///
/// Gets or sets the RestClient
///
/// The RestClient.
public RestClient restClient { get; set; }
private Dictionary defaultHeaderMap = new Dictionary();
public Object CallApi(String Path, RestSharp.Method Method, Dictionary QueryParams, String PostBody,
Dictionary HeaderParams, Dictionary FormParams, Dictionary FileParams, String[] AuthSettings) {
var request = new RestRequest(Path, Method);
UpdateParamsForAuth(QueryParams, HeaderParams, AuthSettings);
// add default header, if any
foreach(KeyValuePair defaultHeader in this.defaultHeaderMap)
request.AddHeader(defaultHeader.Key, defaultHeader.Value);
// add header parameter, if any
foreach(KeyValuePair param in HeaderParams)
request.AddHeader(param.Key, param.Value);
// add query parameter, if any
foreach(KeyValuePair param in QueryParams)
request.AddQueryParameter(param.Key, param.Value);
// add form parameter, if any
foreach(KeyValuePair param in FormParams)
request.AddParameter(param.Key, param.Value);
// add file parameter, if any
foreach(KeyValuePair param in FileParams)
request.AddFile(param.Key, param.Value);
if (PostBody != null) {
request.AddParameter("application/json", PostBody, ParameterType.RequestBody); // http body (model) parameter
}
return (Object)restClient.Execute(request);
}
///
/// Add default header
///
/// Header field name
/// Header field value
///
public void AddDefaultHeader(string key, string value) {
defaultHeaderMap.Add(key, value);
}
///
/// Get default header
///
/// Dictionary of default header
public Dictionary GetDefaultHeader() {
return defaultHeaderMap;
}
///
/// escape string (url-encoded)
///
/// String to be escaped
/// Escaped string
public string EscapeString(string str) {
return str;
}
///
/// if parameter is DateTime, output in ISO8601 format
/// if parameter is a list of string, join the list with ","
/// otherwise just return the string
///
/// The parameter (header, path, query, form)
/// Formatted string
public string ParameterToString(object obj)
{
if (obj is DateTime) {
return ((DateTime)obj).ToString ("u");
} else if (obj is List) {
return String.Join(",", obj as List);
} else {
return Convert.ToString (obj);
}
}
///
/// Deserialize the JSON string into a proper object
///
/// JSON string
/// Object type
/// Object representation of the JSON string
public object Deserialize(string content, Type type) {
if (type.GetType() == typeof(Object))
return (Object)content;
try
{
return JsonConvert.DeserializeObject(content, type);
}
catch (IOException e) {
throw new ApiException(500, e.Message);
}
}
///
/// Serialize an object into JSON string
///
/// Object
/// JSON string
public string Serialize(object obj) {
try
{
return obj != null ? JsonConvert.SerializeObject(obj) : null;
}
catch (Exception e) {
throw new ApiException(500, e.Message);
}
}
///
/// Get the API key with prefix
///
/// Object
/// API key with prefix
public string GetApiKeyWithPrefix (string apiKey)
{
var apiKeyValue = "";
Configuration.apiKey.TryGetValue (apiKey, out apiKeyValue);
var apiKeyPrefix = "";
if (Configuration.apiKeyPrefix.TryGetValue (apiKey, out apiKeyPrefix)) {
return apiKeyPrefix + " " + apiKeyValue;
} else {
return apiKeyValue;
}
}
///
/// Update parameters based on authentication
///
/// Query parameters
/// Header parameters
/// Authentication settings
public void UpdateParamsForAuth(Dictionary QueryParams, Dictionary HeaderParams, string[] AuthSettings) {
if (AuthSettings == null || AuthSettings.Length == 0)
return;
foreach (string auth in AuthSettings) {
// determine which one to use
switch(auth) {
{{#authMethods}}
case "{{name}}":
{{#isApiKey}}{{#isKeyInHeader}}HeaderParams["{{keyParamName}}"] = GetApiKeyWithPrefix("{{keyParamName}}");{{/isKeyInHeader}}{{#isKeyInQuery}}QueryParams["{{keyParamName}}"] = GetApiKeyWithPrefix("{{keyParamName}}");{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}HeaderParams["Authorization"] = "Basic " + Base64Encode(Configuration.username + ":" + Configuration.password);{{/isBasic}}
{{#isOAuth}}//TODO support oauth{{/isOAuth}}
break;
{{/authMethods}}
default:
//TODO show warning about security definition not found
break;
}
}
}
///
/// Encode string in base64 format
///
/// String to be encoded
public static string Base64Encode(string text) {
var textByte = System.Text.Encoding.UTF8.GetBytes(text);
return System.Convert.ToBase64String(textByte);
}
}
}