|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Net; |
| 6 | +using System.Text; |
| 7 | +using Newtonsoft.Json; |
| 8 | +using RestSharp; |
| 9 | + |
| 10 | +namespace {{invokerPackage}} { |
| 11 | + /// <summary> |
| 12 | + /// API client is mainly responible for making the HTTP call to the API backend |
| 13 | + /// </summary> |
| 14 | + public class ApiClient { |
| 15 | +
|
| 16 | + /// <summary> |
| 17 | + /// Initializes a new instance of the <see cref="ApiClient"/> class. |
| 18 | + /// </summary> |
| 19 | + /// <param name="basePath">The base path.</param> |
| 20 | + public ApiClient(String basePath="{{basePath}}") { |
| 21 | + this.basePath = basePath; |
| 22 | + this.restClient = new RestClient(this.basePath); |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Gets or sets the base path. |
| 27 | + /// </summary> |
| 28 | + /// <value>The base path.</value> |
| 29 | + public string basePath { get; set; } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Gets or sets the RestClient |
| 33 | + /// </summary> |
| 34 | + /// <value>The RestClient.</value> |
| 35 | + public RestClient restClient { get; set; } |
| 36 | + |
| 37 | + private Dictionary<String, String> defaultHeaderMap = new Dictionary<String, String>(); |
| 38 | + |
| 39 | + public Object CallApi(String Path, RestSharp.Method Method, Dictionary<String, String> QueryParams, String PostBody, |
| 40 | + Dictionary<String, String> HeaderParams, Dictionary<String, String> FormParams, Dictionary<String, String> FileParams, String[] AuthSettings) { |
| 41 | +
|
| 42 | + var request = new RestRequest(Path, Method); |
| 43 | +
|
| 44 | + UpdateParamsForAuth(QueryParams, HeaderParams, AuthSettings); |
| 45 | +
|
| 46 | + // add default header, if any |
| 47 | + foreach(KeyValuePair<string, string> defaultHeader in this.defaultHeaderMap) |
| 48 | + request.AddHeader(defaultHeader.Key, defaultHeader.Value); |
| 49 | +
|
| 50 | + // add header parameter, if any |
| 51 | + foreach(KeyValuePair<string, string> param in HeaderParams) |
| 52 | + request.AddHeader(param.Key, param.Value); |
| 53 | + |
| 54 | + // add query parameter, if any |
| 55 | + foreach(KeyValuePair<string, string> param in QueryParams) |
| 56 | + request.AddQueryParameter(param.Key, param.Value); |
| 57 | +
|
| 58 | + // add form parameter, if any |
| 59 | + foreach(KeyValuePair<string, string> param in FormParams) |
| 60 | + request.AddParameter(param.Key, param.Value); |
| 61 | +
|
| 62 | + // add file parameter, if any |
| 63 | + foreach(KeyValuePair<string, string> param in FileParams) |
| 64 | + request.AddFile(param.Key, param.Value); |
| 65 | +
|
| 66 | + if (PostBody != null) { |
| 67 | + request.AddParameter("application/json", PostBody, ParameterType.RequestBody); // http body (model) parameter |
| 68 | + } |
| 69 | + |
| 70 | + return (Object)restClient.Execute(request); |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Add default header |
| 76 | + /// </summary> |
| 77 | + /// <param name="key"> Header field name |
| 78 | + /// <param name="value"> Header field value |
| 79 | + /// <returns></returns> |
| 80 | + public void AddDefaultHeader(string key, string value) { |
| 81 | + defaultHeaderMap.Add(key, value); |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Get default header |
| 86 | + /// </summary> |
| 87 | + /// <returns>Dictionary of default header</returns> |
| 88 | + public Dictionary<String, String> GetDefaultHeader() { |
| 89 | + return defaultHeaderMap; |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// escape string (url-encoded) |
| 94 | + /// </summary> |
| 95 | + /// <param name="str"> String to be escaped |
| 96 | + /// <returns>Escaped string</returns> |
| 97 | + public string EscapeString(string str) { |
| 98 | + return str; |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// if parameter is DateTime, output in ISO8601 format |
| 103 | + /// if parameter is a list of string, join the list with "," |
| 104 | + /// otherwise just return the string |
| 105 | + /// </summary> |
| 106 | + /// <param name="obj"> The parameter (header, path, query, form) |
| 107 | + /// <returns>Formatted string</returns> |
| 108 | + public string ParameterToString(object obj) |
| 109 | + { |
| 110 | + if (obj is DateTime) { |
| 111 | + return ((DateTime)obj).ToString ("u"); |
| 112 | + } else if (obj is List<string>) { |
| 113 | + return String.Join(",", obj as List<string>); |
| 114 | + } else { |
| 115 | + return Convert.ToString (obj); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Deserialize the JSON string into a proper object |
| 121 | + /// </summary> |
| 122 | + /// <param name="json"> JSON string |
| 123 | + /// <param name="type"> Object type |
| 124 | + /// <returns>Object representation of the JSON string</returns> |
| 125 | + public object Deserialize(string content, Type type) { |
| 126 | + if (type.GetType() == typeof(Object)) |
| 127 | + return (Object)content; |
| 128 | +
|
| 129 | + try |
| 130 | + { |
| 131 | + return JsonConvert.DeserializeObject(content, type); |
| 132 | + } |
| 133 | + catch (IOException e) { |
| 134 | + throw new ApiException(500, e.Message); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Serialize an object into JSON string |
| 140 | + /// </summary> |
| 141 | + /// <param name="obj"> Object |
| 142 | + /// <returns>JSON string</returns> |
| 143 | + public string Serialize(object obj) { |
| 144 | + try |
| 145 | + { |
| 146 | + return obj != null ? JsonConvert.SerializeObject(obj) : null; |
| 147 | + } |
| 148 | + catch (Exception e) { |
| 149 | + throw new ApiException(500, e.Message); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /// <summary> |
| 154 | + /// Get the API key with prefix |
| 155 | + /// </summary> |
| 156 | + /// <param name="obj"> Object |
| 157 | + /// <returns>API key with prefix</returns> |
| 158 | + public string GetApiKeyWithPrefix (string apiKey) |
| 159 | + { |
| 160 | + var apiKeyValue = ""; |
| 161 | + Configuration.apiKey.TryGetValue (apiKey, out apiKeyValue); |
| 162 | + var apiKeyPrefix = ""; |
| 163 | + if (Configuration.apiKeyPrefix.TryGetValue (apiKey, out apiKeyPrefix)) { |
| 164 | + return apiKeyPrefix + " " + apiKeyValue; |
| 165 | + } else { |
| 166 | + return apiKeyValue; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /// <summary> |
| 171 | + /// Update parameters based on authentication |
| 172 | + /// </summary> |
| 173 | + /// <param name="QueryParams">Query parameters</param> |
| 174 | + /// <param name="HeaderParams">Header parameters</param> |
| 175 | + /// <param name="AuthSettings">Authentication settings</param> |
| 176 | + public void UpdateParamsForAuth(Dictionary<String, String> QueryParams, Dictionary<String, String> HeaderParams, string[] AuthSettings) { |
| 177 | + if (AuthSettings == null || AuthSettings.Length == 0) |
| 178 | + return; |
| 179 | + |
| 180 | + foreach (string auth in AuthSettings) { |
| 181 | + // determine which one to use |
| 182 | + switch(auth) { |
| 183 | + {{#authMethods}} |
| 184 | + case "{{name}}": |
| 185 | + {{#isApiKey}}{{#isKeyInHeader}}HeaderParams["{{keyParamName}}"] = GetApiKeyWithPrefix("{{keyParamName}}");{{/isKeyInHeader}}{{#isKeyInQuery}}QueryParams["{{keyParamName}}"] = GetApiKeyWithPrefix("{{keyParamName}}");{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}HeaderParams["Authorization"] = "Basic " + Base64Encode(Configuration.username + ":" + Configuration.password);{{/isBasic}} |
| 186 | + {{#isOAuth}}//TODO support oauth{{/isOAuth}} |
| 187 | + break; |
| 188 | + {{/authMethods}} |
| 189 | + default: |
| 190 | + //TODO show warning about security definition not found |
| 191 | + break; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + } |
| 196 | + |
| 197 | + /// <summary> |
| 198 | + /// Encode string in base64 format |
| 199 | + /// </summary> |
| 200 | + /// <param name="text">String to be encoded</param> |
| 201 | + public static string Base64Encode(string text) { |
| 202 | + var textByte = System.Text.Encoding.UTF8.GetBytes(text); |
| 203 | + return System.Convert.ToBase64String(textByte); |
| 204 | + } |
| 205 | + |
| 206 | + } |
| 207 | +} |
0 commit comments