29
29
import java .io .IOException ;
30
30
import java .io .UnsupportedEncodingException ;
31
31
32
+ import java .text .DateFormat ;
32
33
import java .text .SimpleDateFormat ;
33
34
import java .text .ParseException ;
34
35
35
- public class ApiInvoker {
36
- private static ApiInvoker INSTANCE = new ApiInvoker ();
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
39
private boolean isDebug = false ;
40
+ private String basePath = "http://petstore.swagger.io/v2" ;
40
41
41
- /**
42
- * ISO 8601 date time format.
43
- * @see https://en.wikipedia.org/wiki/ISO_8601
44
- */
45
- public static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSSZ" );
42
+ private DateFormat dateFormat ;
43
+ private DateFormat datetimeFormat ;
46
44
47
- /**
48
- * ISO 8601 date format .
49
- * @see https://en.wikipedia.org/wiki/ISO_8601
50
- */
51
- public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat ("yyyy-MM-dd" );
45
+ public ApiClient () {
46
+ // Use ISO 8601 format for date and datetime .
47
+ // 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 " );
52
50
53
- static {
54
51
// Use UTC as the default time zone.
55
- DATE_TIME_FORMAT .setTimeZone (TimeZone .getTimeZone ("UTC" ));
56
- DATE_FORMAT .setTimeZone (TimeZone .getTimeZone ("UTC" ));
52
+ this . dateFormat .setTimeZone (TimeZone .getTimeZone ("UTC" ));
53
+ this . datetimeFormat .setTimeZone (TimeZone .getTimeZone ("UTC" ));
57
54
58
55
// Set default User-Agent.
59
56
setUserAgent ("Java-Swagger" );
60
57
}
61
58
62
- public static void setUserAgent ( String userAgent ) {
63
- INSTANCE . addDefaultHeader ( "User-Agent" , userAgent ) ;
59
+ public String getBasePath ( ) {
60
+ return basePath ;
64
61
}
65
62
66
- public static Date parseDateTime (String str ) {
63
+ public ApiClient setBasePath (String basePath ) {
64
+ this .basePath = basePath ;
65
+ return this ;
66
+ }
67
+
68
+ public ApiClient setUserAgent (String userAgent ) {
69
+ addDefaultHeader ("User-Agent" , userAgent );
70
+ return this ;
71
+ }
72
+
73
+ public ApiClient addDefaultHeader (String key , String value ) {
74
+ defaultHeaderMap .put (key , value );
75
+ return this ;
76
+ }
77
+
78
+ public ApiClient enableDebug () {
79
+ isDebug = true ;
80
+ return this ;
81
+ }
82
+
83
+ public Date parseDateTime (String str ) {
67
84
try {
68
- return DATE_TIME_FORMAT .parse (str );
85
+ return datetimeFormat .parse (str );
69
86
} catch (java .text .ParseException e ) {
70
87
throw new RuntimeException (e );
71
88
}
72
89
}
73
90
74
- public static Date parseDate (String str ) {
91
+ public Date parseDate (String str ) {
75
92
try {
76
- return DATE_FORMAT .parse (str );
93
+ return dateFormat .parse (str );
77
94
} catch (java .text .ParseException e ) {
78
95
throw new RuntimeException (e );
79
96
}
80
97
}
81
98
82
- public static String formatDateTime (Date datetime ) {
83
- return DATE_TIME_FORMAT .format (datetime );
99
+ public String formatDateTime (Date datetime ) {
100
+ return datetimeFormat .format (datetime );
84
101
}
85
102
86
- public static String formatDate (Date date ) {
87
- return DATE_FORMAT .format (date );
103
+ public String formatDate (Date date ) {
104
+ return dateFormat .format (date );
88
105
}
89
106
90
- public static String parameterToString (Object param ) {
107
+ public String parameterToString (Object param ) {
91
108
if (param == null ) {
92
109
return "" ;
93
110
} else if (param instanceof Date ) {
@@ -105,17 +122,6 @@ public static String parameterToString(Object param) {
105
122
return String .valueOf (param );
106
123
}
107
124
}
108
- public void enableDebug () {
109
- isDebug = true ;
110
- }
111
-
112
- public static ApiInvoker getInstance () {
113
- return INSTANCE ;
114
- }
115
-
116
- public void addDefaultHeader (String key , String value ) {
117
- defaultHeaderMap .put (key , value );
118
- }
119
125
120
126
public String escapeString (String str ) {
121
127
try {
@@ -126,7 +132,7 @@ public String escapeString(String str) {
126
132
}
127
133
}
128
134
129
- public static Object deserialize (String json , String containerType , Class cls ) throws ApiException {
135
+ public Object deserialize (String json , String containerType , Class cls ) throws ApiException {
130
136
if (null != containerType ) {
131
137
containerType = containerType .toLowerCase ();
132
138
}
@@ -151,7 +157,7 @@ else if(String.class.equals(cls)) {
151
157
}
152
158
}
153
159
154
- public static String serialize (Object obj ) throws ApiException {
160
+ public String serialize (Object obj ) throws ApiException {
155
161
try {
156
162
if (obj != null )
157
163
return JsonUtil .getJsonMapper ().writeValueAsString (obj );
@@ -163,8 +169,8 @@ public static String serialize(Object obj) throws ApiException {
163
169
}
164
170
}
165
171
166
- public String invokeAPI (String host , String path , String method , Map <String , String > queryParams , Object body , Map <String , String > headerParams , Map <String , String > formParams , String contentType ) throws ApiException {
167
- Client client = getClient (host );
172
+ 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 {
173
+ Client client = getClient ();
168
174
169
175
StringBuilder b = new StringBuilder ();
170
176
@@ -180,7 +186,7 @@ public String invokeAPI(String host, String path, String method, Map<String, Str
180
186
}
181
187
String querystring = b .toString ();
182
188
183
- Builder builder = client .resource (host + path + querystring ).accept ("application/json" );
189
+ Builder builder = client .resource (basePath + path + querystring ).accept ("application/json" );
184
190
for (String key : headerParams .keySet ()) {
185
191
builder = builder .header (key , headerParams .get (key ));
186
192
}
@@ -236,6 +242,7 @@ else if ("DELETE".equals(method)) {
236
242
else {
237
243
throw new ApiException (500 , "unknown method type " + method );
238
244
}
245
+
239
246
if (response .getClientResponseStatus () == ClientResponse .Status .NO_CONTENT ) {
240
247
return null ;
241
248
}
@@ -267,8 +274,8 @@ private String getXWWWFormUrlencodedParams(Map<String, String> formParams) {
267
274
StringBuilder formParamBuilder = new StringBuilder ();
268
275
269
276
for (Entry <String , String > param : formParams .entrySet ()) {
270
- String keyStr = ApiInvoker . parameterToString (param .getKey ());
271
- String valueStr = ApiInvoker . parameterToString (param .getValue ());
277
+ String keyStr = parameterToString (param .getKey ());
278
+ String valueStr = parameterToString (param .getValue ());
272
279
273
280
try {
274
281
formParamBuilder .append (URLEncoder .encode (keyStr , "utf8" ))
@@ -287,14 +294,13 @@ private String getXWWWFormUrlencodedParams(Map<String, String> formParams) {
287
294
return encodedFormParams ;
288
295
}
289
296
290
-
291
- private Client getClient (String host ) {
292
- if (!hostMap .containsKey (host )) {
297
+ private Client getClient () {
298
+ if (!hostMap .containsKey (basePath )) {
293
299
Client client = Client .create ();
294
300
if (isDebug )
295
301
client .addFilter (new LoggingFilter ());
296
- hostMap .put (host , client );
302
+ hostMap .put (basePath , client );
297
303
}
298
- return hostMap .get (host );
304
+ return hostMap .get (basePath );
299
305
}
300
- }
306
+ }
0 commit comments