Skip to content

Commit eb49732

Browse files
committed
Add comments to code
1 parent 24c29ac commit eb49732

File tree

8 files changed

+197
-87
lines changed

8 files changed

+197
-87
lines changed

modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache

Lines changed: 81 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,18 @@ import java.text.ParseException;
3636
public class ApiClient {
3737
private Map<String, Client> hostMap = new HashMap<String, Client>();
3838
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
39-
private boolean isDebug = false;
39+
private boolean debugging = false;
4040
private String basePath = "{{basePath}}";
4141
4242
private DateFormat dateFormat;
43-
private DateFormat datetimeFormat;
4443
4544
public ApiClient() {
4645
// Use ISO 8601 format for date and datetime.
4746
// 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");
5048
5149
// Use UTC as the default time zone.
5250
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
53-
this.datetimeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
5451
5552
// Set default User-Agent.
5653
setUserAgent("Java-Swagger");
@@ -65,33 +62,60 @@ public class ApiClient {
6562
return this;
6663
}
6764

65+
/**
66+
* Set the User-Agent header's value (by adding to the default header map).
67+
*/
6868
public ApiClient setUserAgent(String userAgent) {
6969
addDefaultHeader("User-Agent", userAgent);
7070
return this;
7171
}
7272

73+
/**
74+
* Add a default header.
75+
*
76+
* @param key The header's key
77+
* @param value The header's value
78+
*/
7379
public ApiClient addDefaultHeader(String key, String value) {
7480
defaultHeaderMap.put(key, value);
7581
return this;
7682
}
7783

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;
8089
}
8190

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;
8498
return this;
8599
}
86100

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;
93114
}
94115

116+
/**
117+
* Parse the given string into Date object.
118+
*/
95119
public Date parseDate(String str) {
96120
try {
97121
return dateFormat.parse(str);
@@ -100,19 +124,21 @@ public class ApiClient {
100124
}
101125
}
102126

103-
public String formatDateTime(Date datetime) {
104-
return datetimeFormat.format(datetime);
105-
}
106-
127+
/**
128+
* Format the given Date object into string.
129+
*/
107130
public String formatDate(Date date) {
108131
return dateFormat.format(date);
109132
}
110133

134+
/**
135+
* Format the given parameter object into string.
136+
*/
111137
public String parameterToString(Object param) {
112138
if (param == null) {
113139
return "";
114140
} else if (param instanceof Date) {
115-
return formatDateTime((Date) param);
141+
return formatDate((Date) param);
116142
} else if (param instanceof Collection) {
117143
StringBuilder b = new StringBuilder();
118144
for(Object o : (Collection)param) {
@@ -127,15 +153,25 @@ public class ApiClient {
127153
}
128154
}
129155

156+
/**
157+
* Escape the given string to be used as URL query value.
158+
*/
130159
public String escapeString(String str) {
131-
try{
160+
try {
132161
return URLEncoder.encode(str, "utf8").replaceAll("\\+", "%20");
133-
}
134-
catch(UnsupportedEncodingException e) {
162+
} catch (UnsupportedEncodingException e) {
135163
return str;
136164
}
137165
}
138166

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+
*/
139175
public Object deserialize(String json, String containerType, Class cls) throws ApiException {
140176
if(null != containerType) {
141177
containerType = containerType.toLowerCase();
@@ -161,6 +197,9 @@ public class ApiClient {
161197
}
162198
}
163199

200+
/**
201+
* Serialize the given Java object into JSON string.
202+
*/
164203
public String serialize(Object obj) throws ApiException {
165204
try {
166205
if (obj != null)
@@ -173,6 +212,18 @@ public class ApiClient {
173212
}
174213
}
175214

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+
*/
176227
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 {
177228
Client client = getClient();
178229
@@ -278,6 +329,9 @@ public class ApiClient {
278329
}
279330
}
280331

332+
/**
333+
* Encode the given form parameters as request body.
334+
*/
281335
private String getXWWWFormUrlencodedParams(Map<String, String> formParams) {
282336
StringBuilder formParamBuilder = new StringBuilder();
283337
@@ -302,10 +356,13 @@ public class ApiClient {
302356
return encodedFormParams;
303357
}
304358

359+
/**
360+
* Get an existing client or create a new client to handle HTTP request.
361+
*/
305362
private Client getClient() {
306363
if(!hostMap.containsKey(basePath)) {
307364
Client client = Client.create();
308-
if(isDebug)
365+
if (debugging)
309366
client.addFilter(new LoggingFilter());
310367
hostMap.put(basePath, client);
311368
}

modules/swagger-codegen/src/main/resources/Java/Configuration.mustache

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@ package {{invokerPackage}};
33
public class Configuration {
44
private static ApiClient defaultApiClient = new ApiClient();
55
6+
/**
7+
* Get the default API client, which would be used when creating API
8+
* instances without providing an API client.
9+
*/
610
public static ApiClient getDefaultApiClient() {
711
return defaultApiClient;
812
}
913

14+
/**
15+
* Set the default API client, which would be used when creating API
16+
* instances without providing an API client.
17+
*/
1018
public static void setDefaultApiClient(ApiClient apiClient) {
1119
defaultApiClient = apiClient;
1220
}

modules/swagger-codegen/src/main/resources/Java/apiException.mustache

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,21 @@ public class ApiException extends Exception {
2727
return code;
2828
}
2929

30-
public void setCode(int code) {
31-
this.code = code;
32-
}
33-
3430
public String getMessage() {
3531
return message;
3632
}
3733

38-
public void setMessage(String message) {
39-
this.message = message;
40-
}
41-
34+
/**
35+
* Get the HTTP response headers.
36+
*/
4237
public Map<String, List<String>> getResponseHeaders() {
4338
return responseHeaders;
4439
}
4540

46-
public void setResponseHeaders(Map<String, List<String>> responseHeaders) {
47-
this.responseHeaders = responseHeaders;
48-
}
49-
41+
/**
42+
* Get the HTTP response body.
43+
*/
5044
public String getResponseBody() {
5145
return responseBody;
5246
}
53-
54-
public void setResponseBody(String responseBody) {
55-
this.responseBody = responseBody;
56-
}
5747
}

0 commit comments

Comments
 (0)