Skip to content
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

Enable working with API served on a sub-path #241

Merged
merged 1 commit into from
Oct 22, 2018
Merged
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
21 changes: 8 additions & 13 deletions src/main/java/com/suse/salt/netapi/client/SaltClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class SaltClient {
* @param url the Salt API URL
*/
public SaltClient(URI url, AsyncHttpClient asyncHttpClient) {
this.uri = url;
this.uri = url.resolve("/");
this.asyncHttpClient = asyncHttpClient;
}

Expand All @@ -70,7 +70,7 @@ public CompletionStage<Token> login(final String username,
String payload = gson.toJson(props);

CompletionStage<Token> result = asyncHttpClient
.post(uri.resolve("/login"), payload, JsonParser.TOKEN)
.post(uri.resolve("login"), payload, JsonParser.TOKEN)
.thenApply(r -> {
// They return a list of tokens here, take the first one
Token token = r.getResult().get(0);
Expand All @@ -89,7 +89,7 @@ public CompletionStage<Token> login(final String username,
*/
public CompletionStage<Boolean> logout() {
return asyncHttpClient
.post(uri.resolve("/logout"), "", JsonParser.STRING)
.post(uri.resolve("logout"), "", JsonParser.STRING)
.thenApply(s -> "Your token has been cleared".contentEquals(s.getResult()));
}

Expand Down Expand Up @@ -127,7 +127,7 @@ public <T> CompletionStage<Map<String, Object>> run(final String username, final
String payload = gson.toJson(list);

CompletionStage<Map<String, Object>> result = asyncHttpClient
.post(uri.resolve("/run"), payload, JsonParser.RUN_RESULTS)
.post(uri.resolve("run"), payload, JsonParser.RUN_RESULTS)
.thenApply(s -> s.getResult().get(0));
return result;
}
Expand Down Expand Up @@ -158,7 +158,7 @@ public <T> CompletionStage<Map<String, Result<SSHRawResult>>> runRawSSHCommand(f
String payload = gson.toJson(list);

CompletionStage<Map<String, Result<SSHRawResult>>> result = asyncHttpClient
.post(uri.resolve("/run"), payload, JsonParser.RUNSSHRAW_RESULTS)
.post(uri.resolve("run"), payload, JsonParser.RUNSSHRAW_RESULTS)
.thenApply(r -> r.getResult().get(0));

return result;
Expand All @@ -172,7 +172,7 @@ public <T> CompletionStage<Map<String, Result<SSHRawResult>>> runRawSSHCommand(f
* @return the stats
*/
public CompletionStage<Stats> stats() {
return asyncHttpClient.get(uri.resolve("/stats"), JsonParser.STATS);
return asyncHttpClient.get(uri.resolve("stats"), JsonParser.STATS);
}

/**
Expand Down Expand Up @@ -217,16 +217,11 @@ public <R> CompletionStage<R> call(Call<?> call, Client client, Optional<Target<
props.put("client", client.getValue());
props.putAll(call.getPayload());
props.putAll(custom);


String endpoint = auth.getInternal().isRight() ? "/run" : "/";

List<Map<String, Object>> list = Collections.singletonList(props);
String payload = gson.toJson(list);
CompletionStage<R> result = asyncHttpClient
.post(uri.resolve(endpoint), headers, payload, new JsonParser<>(type));

return result;
URI endpoint = auth.getInternal().isRight() ? uri.resolve("run") : uri;
return asyncHttpClient.post(endpoint, headers, payload, new JsonParser<>(type));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public WebSocketEventStream(URI uri, Token token, long sessionIdleTimeout, long
maxMessageLength = maxMsgSize > 0 ?
maxMsgSize : Integer.MAX_VALUE;
Arrays.asList(listeners).forEach(this::addEventListener);
initializeStream(uri, token, sessionIdleTimeout, idleTimeout);
initializeStream(uri.resolve("/"), token, sessionIdleTimeout, idleTimeout);
}

/**
Expand All @@ -90,7 +90,7 @@ private void initializeStream(URI uri, Token token, long sessionIdleTimeout, lon
try {
URI adjustedURI = new URI(uri.getScheme() == "https" ? "wss" : "ws",
uri.getSchemeSpecificPart(), uri.getFragment())
.resolve("/ws/" + token.getToken());
.resolve("ws/" + token.getToken());
websocketContainer.setDefaultMaxSessionIdleTimeout(sessionIdleTimeout);

// Initiate the websocket handshake
Expand Down