Skip to content

Java template: Make ApiInvoker more pluggable #683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions modules/swagger-codegen/src/main/resources/Java/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ import java.util.HashMap;

{{#operations}}
public class {{classname}} {
String basePath = "{{basePath}}";
ApiInvoker apiInvoker = ApiInvoker.getInstance();
private String basePath = "{{basePath}}";
private ApiInvoker apiInvoker;

public {{classname}}(ApiInvoker apiInvoker) {
this.apiInvoker = apiInvoker;
}

public ApiInvoker getInvoker() {
return apiInvoker;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import java.text.SimpleDateFormat;
import java.text.ParseException;

public class ApiInvoker {
private static ApiInvoker INSTANCE = new ApiInvoker();
private Map<String, Client> hostMap = new HashMap<String, Client>();
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
private boolean isDebug = false;
Expand All @@ -52,14 +51,16 @@ public class ApiInvoker {
static {
// Use UTC as the default time zone.
DATE_TIME_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
}

public ApiInvoker() {
// Set default User-Agent.
setUserAgent("Java-Swagger");
}

public static void setUserAgent(String userAgent) {
INSTANCE.addDefaultHeader("User-Agent", userAgent);
public void setUserAgent(String userAgent) {
this.addDefaultHeader("User-Agent", userAgent);
}

public static Date parseDateTime(String str) {
Expand Down Expand Up @@ -108,10 +109,6 @@ public class ApiInvoker {
isDebug = true;
}

public static ApiInvoker getInstance() {
return INSTANCE;
}

public void addDefaultHeader(String key, String value) {
defaultHeaderMap.put(key, value);
}
Expand Down