summaryrefslogtreecommitdiffstats
path: root/hosted-api
diff options
context:
space:
mode:
authorJon Marius Venstad <jvenstad@yahoo-inc.com>2019-05-07 11:37:23 +0200
committerJon Marius Venstad <jvenstad@yahoo-inc.com>2019-05-07 11:37:23 +0200
commit1aad5e00519896846066fe525ab61f77f98f9049 (patch)
tree865117c9e64ea70739b387f4e0fb223e22de2c56 /hosted-api
parent15c4c036ae751c61b39b1d8edda2ed43e12a55c7 (diff)
Refactor out some common code
Diffstat (limited to 'hosted-api')
-rw-r--r--hosted-api/src/main/java/ai/vespa/hosted/api/ControllerHttpClient.java55
-rw-r--r--hosted-api/src/main/java/ai/vespa/hosted/api/Submission.java2
2 files changed, 32 insertions, 25 deletions
diff --git a/hosted-api/src/main/java/ai/vespa/hosted/api/ControllerHttpClient.java b/hosted-api/src/main/java/ai/vespa/hosted/api/ControllerHttpClient.java
index e76115931a3..b4904f7915b 100644
--- a/hosted-api/src/main/java/ai/vespa/hosted/api/ControllerHttpClient.java
+++ b/hosted-api/src/main/java/ai/vespa/hosted/api/ControllerHttpClient.java
@@ -60,7 +60,7 @@ public abstract class ControllerHttpClient {
return new MutualTlsControllerHttpClient(endpoint, privateKeyFile, certificateFile);
}
- /** Sends submission to the remote controller and returns the version of the accepted package, or throws if this fails. */
+ /** Sends the given submission to the remote controller and returns the version of the accepted package, or throws if this fails. */
public String submit(Submission submission, TenantName tenant, ApplicationName application) {
return toMessage(send(request(HttpRequest.newBuilder(applicationPath(tenant, application).resolve("submit"))
.timeout(Duration.ofMinutes(30)),
@@ -86,12 +86,12 @@ public abstract class ControllerHttpClient {
return request(request.setHeader("Content-Type", data.contentType()), method, data::data);
}
- private URI apiPath() {
+ private URI applicationApiPath() {
return concatenated(endpoint, "application", "v4");
}
private URI tenantPath(TenantName tenant) {
- return concatenated(apiPath(), "tenant", tenant.value());
+ return concatenated(applicationApiPath(), "tenant", tenant.value());
}
private URI applicationPath(TenantName tenant, ApplicationName application) {
@@ -121,39 +121,46 @@ public abstract class ControllerHttpClient {
/** Returns a JSON representation of the submission meta data. */
private static String metaToJson(Submission submission) {
- try {
- Slime slime = new Slime();
- Cursor rootObject = slime.setObject();
- rootObject.setString("repository", submission.repository());
- rootObject.setString("branch", submission.branch());
- rootObject.setString("commit", submission.commit());
- rootObject.setString("authorEmail", submission.authorEmail());
- ByteArrayOutputStream buffer = new ByteArrayOutputStream();
- new JsonFormat(true).encode(buffer, slime);
- return buffer.toString(UTF_8);
- }
- catch (IOException e) {
- throw new UncheckedIOException(e);
- }
+ Slime slime = new Slime();
+ Cursor rootObject = slime.setObject();
+ rootObject.setString("repository", submission.repository());
+ rootObject.setString("branch", submission.branch());
+ rootObject.setString("commit", submission.commit());
+ rootObject.setString("authorEmail", submission.authorEmail());
+ return toJson(slime);
}
- /** Returns the "message" element contained in the JSON formatted response, if 2XX status code, or throws otherwise. */
- private static String toMessage(HttpResponse<byte[]> response) {
+ /** Returns an {@link Inspector} for the assumed JSON formatted response, or throws if the status code is non-2XX. */
+ private static Inspector toInspector(HttpResponse<byte[]> response) {
Inspector rootObject = toSlime(response.body()).get();
- if (response.statusCode() / 100 == 2)
- return rootObject.field("message").asString();
-
- else {
+ if (response.statusCode() / 100 != 2)
throw new RuntimeException(response.request() + " returned code " + response.statusCode() +
" (" + rootObject.field("error-code").asString() + "): " +
rootObject.field("message").asString());
- }
+
+ return rootObject;
+ }
+
+ /** Returns the "message" element contained in the JSON formatted response, if 2XX status code, or throws otherwise. */
+ private static String toMessage(HttpResponse<byte[]> response) {
+ return toInspector(response).field("message").asString();
}
private static Slime toSlime(byte[] data) {
return new JsonDecoder().decode(new Slime(), data);
}
+ private static String toJson(Slime slime) {
+ try {
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ new JsonFormat(true).encode(buffer, slime);
+ return buffer.toString(UTF_8);
+ }
+ catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+
/** Client that signs requests with a private key whose public part is assigned to an application in the remote controller. */
private static class SigningControllerHttpClient extends ControllerHttpClient {
diff --git a/hosted-api/src/main/java/ai/vespa/hosted/api/Submission.java b/hosted-api/src/main/java/ai/vespa/hosted/api/Submission.java
index f4cb90176da..f781c942fc9 100644
--- a/hosted-api/src/main/java/ai/vespa/hosted/api/Submission.java
+++ b/hosted-api/src/main/java/ai/vespa/hosted/api/Submission.java
@@ -6,7 +6,7 @@ import java.io.InputStream;
import java.nio.file.Path;
/**
- * A submission intended for hosted Vespa containing an application package with tests and meta data.
+ * A submission intended for hosted Vespa, containing an application package with tests, and meta data.
*
* @author jonmv
*/