Skip to content

Commit 62c8f9e

Browse files
committed
Rebuild Java Petstore sample
1 parent ac134c0 commit 62c8f9e

File tree

6 files changed

+156
-189
lines changed

6 files changed

+156
-189
lines changed

samples/client/petstore/java/src/main/java/io/swagger/client/ApiInvoker.java renamed to samples/client/petstore/java/src/main/java/io/swagger/client/ApiClient.java

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -29,65 +29,82 @@
2929
import java.io.IOException;
3030
import java.io.UnsupportedEncodingException;
3131

32+
import java.text.DateFormat;
3233
import java.text.SimpleDateFormat;
3334
import java.text.ParseException;
3435

35-
public class ApiInvoker {
36-
private static ApiInvoker INSTANCE = new ApiInvoker();
36+
public class ApiClient {
3737
private Map<String, Client> hostMap = new HashMap<String, Client>();
3838
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
3939
private boolean isDebug = false;
40+
private String basePath = "http://petstore.swagger.io/v2";
4041

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;
4644

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");
5250

53-
static {
5451
// 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"));
5754

5855
// Set default User-Agent.
5956
setUserAgent("Java-Swagger");
6057
}
6158

62-
public static void setUserAgent(String userAgent) {
63-
INSTANCE.addDefaultHeader("User-Agent", userAgent);
59+
public String getBasePath() {
60+
return basePath;
6461
}
6562

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) {
6784
try {
68-
return DATE_TIME_FORMAT.parse(str);
85+
return datetimeFormat.parse(str);
6986
} catch (java.text.ParseException e) {
7087
throw new RuntimeException(e);
7188
}
7289
}
7390

74-
public static Date parseDate(String str) {
91+
public Date parseDate(String str) {
7592
try {
76-
return DATE_FORMAT.parse(str);
93+
return dateFormat.parse(str);
7794
} catch (java.text.ParseException e) {
7895
throw new RuntimeException(e);
7996
}
8097
}
8198

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);
84101
}
85102

86-
public static String formatDate(Date date) {
87-
return DATE_FORMAT.format(date);
103+
public String formatDate(Date date) {
104+
return dateFormat.format(date);
88105
}
89106

90-
public static String parameterToString(Object param) {
107+
public String parameterToString(Object param) {
91108
if (param == null) {
92109
return "";
93110
} else if (param instanceof Date) {
@@ -105,17 +122,6 @@ public static String parameterToString(Object param) {
105122
return String.valueOf(param);
106123
}
107124
}
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-
}
119125

120126
public String escapeString(String str) {
121127
try{
@@ -126,7 +132,7 @@ public String escapeString(String str) {
126132
}
127133
}
128134

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 {
130136
if(null != containerType) {
131137
containerType = containerType.toLowerCase();
132138
}
@@ -151,7 +157,7 @@ else if(String.class.equals(cls)) {
151157
}
152158
}
153159

154-
public static String serialize(Object obj) throws ApiException {
160+
public String serialize(Object obj) throws ApiException {
155161
try {
156162
if (obj != null)
157163
return JsonUtil.getJsonMapper().writeValueAsString(obj);
@@ -163,8 +169,8 @@ public static String serialize(Object obj) throws ApiException {
163169
}
164170
}
165171

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();
168174

169175
StringBuilder b = new StringBuilder();
170176

@@ -180,7 +186,7 @@ public String invokeAPI(String host, String path, String method, Map<String, Str
180186
}
181187
String querystring = b.toString();
182188

183-
Builder builder = client.resource(host + path + querystring).accept("application/json");
189+
Builder builder = client.resource(basePath + path + querystring).accept("application/json");
184190
for(String key : headerParams.keySet()) {
185191
builder = builder.header(key, headerParams.get(key));
186192
}
@@ -236,6 +242,7 @@ else if ("DELETE".equals(method)) {
236242
else {
237243
throw new ApiException(500, "unknown method type " + method);
238244
}
245+
239246
if(response.getClientResponseStatus() == ClientResponse.Status.NO_CONTENT) {
240247
return null;
241248
}
@@ -267,8 +274,8 @@ private String getXWWWFormUrlencodedParams(Map<String, String> formParams) {
267274
StringBuilder formParamBuilder = new StringBuilder();
268275

269276
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());
272279

273280
try {
274281
formParamBuilder.append(URLEncoder.encode(keyStr, "utf8"))
@@ -287,14 +294,13 @@ private String getXWWWFormUrlencodedParams(Map<String, String> formParams) {
287294
return encodedFormParams;
288295
}
289296

290-
291-
private Client getClient(String host) {
292-
if(!hostMap.containsKey(host)) {
297+
private Client getClient() {
298+
if(!hostMap.containsKey(basePath)) {
293299
Client client = Client.create();
294300
if(isDebug)
295301
client.addFilter(new LoggingFilter());
296-
hostMap.put(host, client);
302+
hostMap.put(basePath, client);
297303
}
298-
return hostMap.get(host);
304+
return hostMap.get(basePath);
299305
}
300-
}
306+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.swagger.client;
2+
3+
public class Configuration {
4+
private static ApiClient defaultApiClient = new ApiClient();
5+
6+
public static ApiClient getDefaultApiClient() {
7+
return defaultApiClient;
8+
}
9+
10+
public static void setDefaultApiClient(ApiClient apiClient) {
11+
defaultApiClient = apiClient;
12+
}
13+
}

0 commit comments

Comments
 (0)