@@ -36,21 +36,18 @@ import java.text.ParseException;
36
36
public class ApiClient {
37
37
private Map< String, Client> hostMap = new HashMap< String, Client> ();
38
38
private Map< String, String> defaultHeaderMap = new HashMap< String, String> ();
39
- private boolean isDebug = false ;
39
+ private boolean debugging = false ;
40
40
private String basePath = " {{basePath}}" ;
41
41
42
42
private DateFormat dateFormat;
43
- private DateFormat datetimeFormat;
44
43
45
44
public ApiClient() {
46
45
// Use ISO 8601 format for date and datetime.
47
46
// See https://en.wikipedia.org/wiki/ISO_8601
48
- this.dateFormat = new SimpleDateFormat(" yyyy-MM-dd" );
49
- this.datetimeFormat = new SimpleDateFormat(" yyyy-MM-dd'T'HH:mm:ss.SSSZ" );
47
+ this.dateFormat = new SimpleDateFormat(" yyyy-MM-dd'T'HH:mm:ss.SSSZ" );
50
48
51
49
// Use UTC as the default time zone.
52
50
this.dateFormat.setTimeZone(TimeZone.getTimeZone(" UTC" ));
53
- this.datetimeFormat.setTimeZone(TimeZone.getTimeZone(" UTC" ));
54
51
55
52
// Set default User-Agent.
56
53
setUserAgent(" Java-Swagger" );
@@ -65,33 +62,60 @@ public class ApiClient {
65
62
return this;
66
63
}
67
64
65
+ /**
66
+ * Set the User-Agent header's value (by adding to the default header map).
67
+ */
68
68
public ApiClient setUserAgent(String userAgent) {
69
69
addDefaultHeader(" User-Agent" , userAgent);
70
70
return this;
71
71
}
72
72
73
+ /**
74
+ * Add a default header.
75
+ *
76
+ * @param key The header's key
77
+ * @param value The header's value
78
+ */
73
79
public ApiClient addDefaultHeader(String key, String value) {
74
80
defaultHeaderMap.put(key, value);
75
81
return this;
76
82
}
77
83
78
- public boolean isDebug() {
79
- return isDebug;
84
+ /**
85
+ * Check that whether debugging is enabled for this API client.
86
+ */
87
+ public boolean isDebugging() {
88
+ return debugging;
80
89
}
81
90
82
- public ApiClient enableDebug() {
83
- isDebug = true ;
91
+ /**
92
+ * Enable/disable debugging for this API client.
93
+ *
94
+ * @param debugging To enable (true) or disable (false) debugging
95
+ */
96
+ public ApiClient setDebugging(boolean debugging) {
97
+ this.debugging = debugging;
84
98
return this;
85
99
}
86
100
87
- public Date parseDateTime(String str) {
88
- try {
89
- return datetimeFormat.parse(str);
90
- } catch (java.text.ParseException e) {
91
- throw new RuntimeException(e);
92
- }
101
+ /**
102
+ * Get the date format used to parse/format date parameters.
103
+ */
104
+ public DateFormat getDateFormat() {
105
+ return dateFormat;
106
+ }
107
+
108
+ /**
109
+ * Set the date format used to parse/format date parameters.
110
+ */
111
+ public ApiClient getDateFormat(DateFormat dateFormat) {
112
+ this.dateFormat = dateFormat;
113
+ return this;
93
114
}
94
115
116
+ /**
117
+ * Parse the given string into Date object.
118
+ */
95
119
public Date parseDate(String str) {
96
120
try {
97
121
return dateFormat.parse(str);
@@ -100,19 +124,21 @@ public class ApiClient {
100
124
}
101
125
}
102
126
103
- public String formatDateTime(Date datetime) {
104
- return datetimeFormat.format(datetime);
105
- }
106
-
127
+ /**
128
+ * Format the given Date object into string.
129
+ */
107
130
public String formatDate(Date date) {
108
131
return dateFormat.format(date);
109
132
}
110
133
134
+ /**
135
+ * Format the given parameter object into string.
136
+ */
111
137
public String parameterToString(Object param) {
112
138
if (param == null) {
113
139
return " " ;
114
140
} else if (param instanceof Date) {
115
- return formatDateTime ((Date) param);
141
+ return formatDate ((Date) param);
116
142
} else if (param instanceof Collection) {
117
143
StringBuilder b = new StringBuilder();
118
144
for(Object o : (Collection)param) {
@@ -127,15 +153,25 @@ public class ApiClient {
127
153
}
128
154
}
129
155
156
+ /**
157
+ * Escape the given string to be used as URL query value.
158
+ */
130
159
public String escapeString(String str) {
131
- try{
160
+ try {
132
161
return URLEncoder.encode(str, " utf8" ).replaceAll(" \\ +" , " %20" );
133
- }
134
- catch(UnsupportedEncodingException e) {
162
+ } catch (UnsupportedEncodingException e) {
135
163
return str;
136
164
}
137
165
}
138
166
167
+ /**
168
+ * Deserialize the given JSON string to Java object.
169
+ *
170
+ * @param json The JSON string
171
+ * @param containerType The container type, one of "list", "array" or ""
172
+ * @param cls The type of the Java object
173
+ * @return The deserialized Java object
174
+ */
139
175
public Object deserialize(String json, String containerType, Class cls) throws ApiException {
140
176
if (null != containerType) {
141
177
containerType = containerType.toLowerCase();
@@ -161,6 +197,9 @@ public class ApiClient {
161
197
}
162
198
}
163
199
200
+ /**
201
+ * Serialize the given Java object into JSON string.
202
+ */
164
203
public String serialize(Object obj) throws ApiException {
165
204
try {
166
205
if (obj != null)
@@ -173,6 +212,18 @@ public class ApiClient {
173
212
}
174
213
}
175
214
215
+ /**
216
+ * Invoke API by sending HTTP request with the given options.
217
+ *
218
+ * @param path The sub-path of the HTTP URL
219
+ * @param method The request method, one of "GET", "POST", "PUT", and "DELETE"
220
+ * @param queryParams The query parameters
221
+ * @param body The request body object
222
+ * @param headerParams The header parameters
223
+ * @param formParams The form parameters
224
+ * @param contentType The request Content-Type
225
+ * @return The response body in type of string
226
+ */
176
227
public String invokeAPI(String path, String method, Map<String , String > queryParams, Object body, Map<String , String > headerParams, Map<String , String > formParams, String contentType) throws ApiException {
177
228
Client client = getClient();
178
229
@@ -278,6 +329,9 @@ public class ApiClient {
278
329
}
279
330
}
280
331
332
+ /**
333
+ * Encode the given form parameters as request body.
334
+ */
281
335
private String getXWWWFormUrlencodedParams(Map<String , String > formParams) {
282
336
StringBuilder formParamBuilder = new StringBuilder();
283
337
@@ -302,10 +356,13 @@ public class ApiClient {
302
356
return encodedFormParams;
303
357
}
304
358
359
+ /**
360
+ * Get an existing client or create a new client to handle HTTP request.
361
+ */
305
362
private Client getClient() {
306
363
if (! hostMap.containsKey(basePath)) {
307
364
Client client = Client.create();
308
- if (isDebug )
365
+ if (debugging )
309
366
client.addFilter(new LoggingFilter());
310
367
hostMap.put(basePath, client);
311
368
}
0 commit comments