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 Dictionary defaultHeaderMap = new Dictionary(); /// /// Add default header /// /// Header field name /// Header field value /// public static void AddDefaultHeader(string key, string value) { defaultHeaderMap.Add(key, value); } /// /// Get default header /// /// Dictionary of default header public static Dictionary GetDefaultHeader() { return defaultHeaderMap; } /// /// escape string (url-encoded) /// /// String to be escaped /// Escaped string public static string EscapeString(string str) { return str; } /// /// if parameter is DateTime, output in ISO8601 format, otherwise just return the string /// /// The parameter (header, path, query, form) /// Formatted string public static string ParameterToString(object obj) { return (obj is DateTime) ? ((DateTime)obj).ToString ("u") : Convert.ToString (obj); } /// /// Deserialize the JSON string into a proper object /// /// JSON string /// Object type /// Object representation of the JSON string public static 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 static string Serialize(object obj) { try { return obj != null ? JsonConvert.SerializeObject(obj) : null; } catch (Exception e) { throw new ApiException(500, e.Message); } } } }