@@ -5,26 +5,70 @@ using System.Linq;
5
5
using System.Net;
6
6
using System.Text;
7
7
using Newtonsoft.Json;
8
+ using RestSharp;
8
9
9
10
namespace { {invokerPackage} } {
10
11
public class ApiInvoker {
11
- private static Dictionary< String, String> defaultHeaderMap = new Dictionary< String, String> ();
12
+ public ApiInvoker() {
13
+ this.basePath = " {{basePath}}" ;
14
+ }
15
+
16
+ public ApiInvoker(String basePath) {
17
+ this.basePath = basePath;
18
+ }
19
+
20
+ public string basePath { get; set; }
21
+ public RestClient restClient { get; set; }
22
+ private Dictionary<String , String > defaultHeaderMap = new Dictionary<String , String >();
23
+
24
+ public Object CallApi(String Path, RestSharp.Method Method, Dictionary<String , String > QueryParams, String PostBody,
25
+ Dictionary<String , String > HeaderParams, Dictionary<String , String > FormParams, Dictionary<String , String > FileParams) {
26
+
27
+ var request = new RestRequest(Path, Method);
28
+
29
+ // add default header, if any
30
+ foreach (KeyValuePair< string, string> defaultHeader in this.defaultHeaderMap)
31
+ request.AddHeader(defaultHeader.Key, defaultHeader.Value);
32
+
33
+ // add header parameter, if any
34
+ foreach (KeyValuePair< string, string> param in HeaderParams)
35
+ request.AddHeader(param.Key, param.Value);
36
+
37
+ // add query parameter, if any
38
+ foreach (KeyValuePair< string, string> param in QueryParams)
39
+ request.AddUrlSegment(param.Key, param.Value);
40
+
41
+ // add form parameter, if any
42
+ foreach (KeyValuePair< string, string> param in FormParams)
43
+ request.AddParameter(param.Key, param.Value);
44
+
45
+ // add file parameter, if any
46
+ foreach (KeyValuePair< string, string> param in FormParams)
47
+ request.AddFile(param.Key, param.Value);
48
+
49
+ if (PostBody == null) {
50
+ request.AddParameter(" application/json" , PostBody, ParameterType.RequestBody); // http body (model) parameter
51
+ }
52
+
53
+ return (Object)restClient.Execute(request);
54
+
55
+ }
12
56
13
57
/// <summary >
14
58
/// Add default header
15
59
/// </summary >
16
60
/// <param name =" key" > Header field name
17
61
/// <param name =" value" > Header field value
18
62
/// <returns ></returns >
19
- public static void AddDefaultHeader(string key, string value) {
63
+ public void AddDefaultHeader(string key, string value) {
20
64
defaultHeaderMap.Add(key, value);
21
65
}
22
66
23
67
/// <summary >
24
68
/// Get default header
25
69
/// </summary >
26
70
/// <returns >Dictionary of default header</returns >
27
- public static Dictionary<String , String > GetDefaultHeader() {
71
+ public Dictionary<String , String > GetDefaultHeader() {
28
72
return defaultHeaderMap;
29
73
}
30
74
@@ -33,7 +77,7 @@ namespace {{invokerPackage}} {
33
77
/// </summary >
34
78
/// <param name =" str" > String to be escaped
35
79
/// <returns >Escaped string</returns >
36
- public static string EscapeString(string str) {
80
+ public string EscapeString(string str) {
37
81
return str;
38
82
}
39
83
@@ -42,7 +86,7 @@ namespace {{invokerPackage}} {
42
86
/// </summary >
43
87
/// <param name =" obj" > The parameter (header, path, query, form)
44
88
/// <returns >Formatted string</returns >
45
- public static string ParameterToString(object obj)
89
+ public string ParameterToString(object obj)
46
90
{
47
91
return (obj is DateTime) ? ((DateTime)obj).ToString (" u" ) : Convert.ToString (obj);
48
92
}
@@ -53,7 +97,7 @@ namespace {{invokerPackage}} {
53
97
/// <param name =" json" > JSON string
54
98
/// <param name =" type" > Object type
55
99
/// <returns >Object representation of the JSON string</returns >
56
- public static object Deserialize(string content, Type type) {
100
+ public object Deserialize(string content, Type type) {
57
101
if (type.GetType() == typeof(Object))
58
102
return (Object)content;
59
103
@@ -71,7 +115,7 @@ namespace {{invokerPackage}} {
71
115
/// </summary >
72
116
/// <param name =" obj" > Object
73
117
/// <returns >JSON string</returns >
74
- public static string Serialize(object obj) {
118
+ public string Serialize(object obj) {
75
119
try
76
120
{
77
121
return obj != null ? JsonConvert.SerializeObject(obj) : null;
0 commit comments