Skip to content

Commit

Permalink
Fix #126 Add API version downgrading
Browse files Browse the repository at this point in the history
RD_API_DOWNGRADE=true will enable automatic downgrading
RD_API_VERSION=X will force an api version independent of URL
  • Loading branch information
gschueler committed Oct 20, 2017
1 parent 2a41570 commit 5ba2972
Show file tree
Hide file tree
Showing 16 changed files with 430 additions and 83 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ repositories {
maven { url "https://jitpack.io" }
}

ext.toolbeltVersion = "0.1.19"
ext.toolbeltVersion = "0.1.20"
ext.toolbeltGroup = "com${toolbeltVersion.contains('SNAPSHOT')?'':'.github'}.simplifyops.cli-toolbelt"

dependencies {
Expand Down
70 changes: 54 additions & 16 deletions src/main/java/org/rundeck/client/RundeckClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static org.rundeck.client.tool.Main.ENV_CONNECT_RETRY;
import static org.rundeck.client.tool.Main.ENV_DEBUG;
import static org.rundeck.client.tool.Main.ENV_HTTP_TIMEOUT;
import static org.rundeck.client.tool.Main.*;

/**
* Build a {@link Client} for {@link RundeckApi} using {@link #builder()}.
Expand All @@ -55,13 +53,17 @@ public class RundeckClient {
private RundeckClient() {
}


@SuppressWarnings("UnusedReturnValue")
public static class Builder {
final OkHttpClient.Builder okhttp;
String baseUrl;
String appBaseUrl;
int httpLogging;
HttpUrl parseUrl;
Integer apiVersion;
boolean allowVersionDowngrade;
Client.Logger logger;

Builder() {
this.okhttp = new OkHttpClient.Builder();
Expand All @@ -73,13 +75,14 @@ <T> Builder accept(BuildWith<Builder, T> bw, T i) {
}

public Builder config(AppConfig config) {
logging(config.getInt(ENV_DEBUG, 0));
logging(config.getDebugLevel());
retryConnect(config.getBool(ENV_CONNECT_RETRY, true));
timeout(config.getLong(ENV_HTTP_TIMEOUT, null));
bypassUrl(config.getString(ENV_BYPASS_URL, null));
insecureSSL(config.getBool(ENV_INSECURE_SSL, false));
insecureSSLHostname(config.getBool(ENV_INSECURE_SSL_HOSTNAME, false));
alternateSSLHostname(config.getString(ENV_ALT_SSL_HOSTNAME, null));
allowVersionDowngrade(config.getBool(RD_API_DOWNGRADE, false));
return this;
}

Expand Down Expand Up @@ -115,6 +118,16 @@ public Builder baseUrl(final String baseUrl) {
return this;
}

public Builder apiVersion(final int version) {
this.apiVersion = version;
return this;
}

public Builder allowVersionDowngrade(final boolean allow) {
this.allowVersionDowngrade = allow;
return this;
}

public Builder tokenAuth(final String authToken) {
buildTokenAuth(okhttp, baseUrl, authToken);
return this;
Expand All @@ -126,14 +139,19 @@ public Builder passwordAuth(final String username, final String password) {
}

public Client<RundeckApi> build() {
return buildRundeckClient(okhttp, baseUrl, API_VERS);
return buildRundeckClient();
}

public Builder logging(final int p) {
httpLogging = p;
return accept(RundeckClient::configLogging, p);
}

public Builder logger(Client.Logger logger) {
this.logger = logger;
return this;
}

private static void buildTokenAuth(
final OkHttpClient.Builder builder,
final String baseUrl,
Expand Down Expand Up @@ -196,29 +214,42 @@ private static void buildFormAuth(

}

private static Client<RundeckApi> buildRundeckClient(
final OkHttpClient.Builder builder,
final String baseUrl,
final int apiVers
)
{
String apiBaseUrl = buildApiUrlForVersion(baseUrl, apiVers);
int usedApiVers = apiVersionForUrl(baseUrl, apiVers);
private Client<RundeckApi> buildRundeckClient() {
//url without version
String appBaseUrl = buildBaseAppUrlForVersion(baseUrl);
final String apiBaseUrl;
if (null != apiVersion) {
//construct api url using requested version
apiBaseUrl = buildApiUrlForVersion(appBaseUrl, apiVersion);
} else {
//if url has no version, use default
apiBaseUrl = buildApiUrlForVersion(baseUrl, API_VERS);
}
//detected final version
int usedApiVers = apiVersionForUrl(apiBaseUrl, API_VERS);

builder.addInterceptor(new StaticHeaderInterceptor("User-Agent", USER_AGENT));
okhttp.addInterceptor(new StaticHeaderInterceptor("User-Agent", USER_AGENT));


Retrofit build = new Retrofit.Builder()
.baseUrl(apiBaseUrl)
.client(builder.build())
.client(okhttp.build())
.addConverterFactory(new QualifiedTypeConverterFactory(
JacksonConverterFactory.create(),
SimpleXmlConverterFactory.create(),
true
))
.build();

return new Client<>(build.create(RundeckApi.class), build, usedApiVers);
return new Client<>(
build.create(RundeckApi.class),
build,
appBaseUrl,
apiBaseUrl,
usedApiVers,
allowVersionDowngrade,
logger
);
}

private static void validateNotempty(final String authToken, final String s) {
Expand Down Expand Up @@ -384,6 +415,13 @@ private static Builder configLogging(final Builder builder, final int httpLoggin
}


/**
* Normalize a url by appending a / if not present
*
* @param baseUrl
*
* @return
*/
private static String normalizeUrlPath(String baseUrl) {
if (!baseUrl.matches(".*/$")) {
return baseUrl + "/";
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/rundeck/client/api/RequestFailed.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
* Http request failure
*/
public class RequestFailed extends RuntimeException {
final int statusCode;
final String status;
private final int statusCode;
private final String status;

public RequestFailed(final int statusCode, final String status) {
this.statusCode = statusCode;
Expand All @@ -45,4 +45,12 @@ public RequestFailed(final Throwable cause, final int statusCode, final String s
this.statusCode = statusCode;
this.status = status;
}

public int getStatusCode() {
return statusCode;
}

public String getStatus() {
return status;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public int getApiVersion() {
@Override
public String toString() {
return String.format(
"%s%n%s%n",
"%s%n%s",
getErrorMessage() != null ? getErrorMessage() : "(no message)",
toCodeString()
);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/rundeck/client/tool/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
public interface AppConfig extends ConfigSource {
boolean isAnsiEnabled();
int getDebugLevel();

String getDateFormat();
}
Loading

0 comments on commit 5ba2972

Please sign in to comment.