Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Complete APIClient.

  • Loading branch information...
commit e97a5c192a35fd5d72948adc228a97a2cf31bb42 1 parent 8b53469
@iced iced authored
Showing with 136 additions and 29 deletions.
  1. +136 −29 client/src/main/java/io/iron/ironworker/client/APIClient.java
View
165 client/src/main/java/io/iron/ironworker/client/APIClient.java
@@ -13,6 +13,7 @@
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
+import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
@@ -134,7 +135,7 @@ private HttpResponse doRequestExecute(HttpRequestBase request) throws APIExcepti
}
try {
- Thread.sleep((long)(Math.pow(4, currentTry) * 100 * Math.random()));
+ Thread.sleep((long) (Math.pow(4, currentTry) * 100 * Math.random()));
} catch (InterruptedException e) {
throw new APIException(null, e);
}
@@ -145,19 +146,17 @@ private HttpResponse doRequestExecute(HttpRequestBase request) throws APIExcepti
return response;
}
- private void setRequestParams(HttpRequestBase request, String method, Map<String, String> params) throws APIException {
- List<NameValuePair> qParams = new ArrayList<NameValuePair>();
+ private void setRequestParams(HttpRequestBase request, String method, Map<String, Object> params) throws APIException {
+ String query = null;
+
+ if (params != null && !params.isEmpty()) {
+ List<NameValuePair> qParams = new ArrayList<NameValuePair>();
- if (params != null) {
for (String key : params.keySet()) {
- qParams.add(new BasicNameValuePair(key, params.get(key)));
+ qParams.add(new BasicNameValuePair(key, params.get(key).toString()));
}
- }
-
- String query = URLEncodedUtils.format(qParams, "UTF-8");
- if (query.equals("")) {
- query = null;
+ query = URLEncodedUtils.format(qParams, "UTF-8");
}
URI uri;
@@ -174,26 +173,61 @@ private void setRequestParams(HttpRequestBase request, String method, Map<String
request.addHeader("User-Agent", userAgent);
}
- private HttpResponse doRequest(HttpRequestBase request, String method, Map<String, String> params) throws APIException {
+ private HttpResponse doGetRequest(String method, Map<String, Object> params) throws APIException {
+ HttpGet request = new HttpGet();
+
+ setRequestParams(request, method, params);
+
+ return doRequestExecute(request);
+ }
+
+ private HttpResponse doPostRequest(String method, String data) throws APIException {
+ HttpPost request = new HttpPost();
+
+ setRequestParams(request, method, null);
+
+ if (data != null) {
+ request.addHeader("Content-Type", "application/json");
+
+ StringEntity entity;
+
+ try {
+ entity = new StringEntity(data);
+ } catch (UnsupportedEncodingException e) {
+ throw new APIException(null, e);
+ }
+
+ request.setEntity(entity);
+ }
+
+ return doRequestExecute(request);
+ }
+
+ private HttpResponse doDeleteRequest(String method, Map<String, Object> params) throws APIException {
+ HttpDelete request = new HttpDelete();
+
setRequestParams(request, method, params);
return doRequestExecute(request);
}
- private HttpResponse doFileRequest(File file, String method, Map<String, String> params) throws APIException {
+ private HttpResponse doFileRequest(String method, String data, File file) throws APIException {
HttpPost request = new HttpPost();
setRequestParams(request, method, null);
MultipartEntity entity = new MultipartEntity();
- try {
- entity.addPart("data", new StringBody((new Gson()).toJson(params)));
- entity.addPart("file", new FileBody(file));
- } catch (UnsupportedEncodingException e) {
- throw new APIException(null, e);
+ if (data != null) {
+ try {
+ entity.addPart("data", new StringBody(data));
+ } catch (UnsupportedEncodingException e) {
+ throw new APIException(null, e);
+ }
}
+ entity.addPart("file", new FileBody(file));
+
request.setEntity(entity);
return doRequestExecute(request);
@@ -226,6 +260,7 @@ private InputStream parseResponseGeneral(HttpResponse response) throws APIExcept
throw new APIException(null, e);
}
}
+
private String parseResponseAsString(HttpResponse response) throws APIException {
try {
return IOUtils.toString(parseResponseGeneral(response));
@@ -238,39 +273,111 @@ private JsonObject parseResponseAsJson(HttpResponse response) throws APIExceptio
return (new JsonParser()).parse(new InputStreamReader(parseResponseGeneral(response))).getAsJsonObject();
}
- public JsonObject codesList(Map<String, String> params) throws APIException {
- return parseResponseAsJson(doRequest(new HttpGet(), String.format("projects/%s/codes", projectId), params));
+ public JsonObject codesList(Map<String, Object> params) throws APIException {
+ return parseResponseAsJson(doGetRequest(String.format("projects/%s/codes", projectId), params));
}
public JsonObject codesGet(String id) throws APIException {
- return parseResponseAsJson(doRequest(new HttpGet(), String.format("projects/%s/codes/%s", projectId, id), null));
+ return parseResponseAsJson(doGetRequest(String.format("projects/%s/codes/%s", projectId, id), null));
}
-
+
public JsonObject codesCreate(String name, String file, String runtime, String runner) throws APIException {
- Map<String, String> params = new HashMap<String, String>();
+ Map<String, Object> params = new HashMap<String, Object>();
params.put("name", name);
params.put("runtime", runtime);
params.put("file_name", runner);
-
+
File f = new File(file);
if (!f.exists()) {
throw new APIException("File " + file + " not found", null);
}
- return parseResponseAsJson(doFileRequest(f, String.format("projects/%s/codes", projectId), params));
+ return parseResponseAsJson(doFileRequest(String.format("projects/%s/codes", projectId), (new Gson()).toJson(params), f));
}
public JsonObject codesDelete(String id) throws APIException {
- return parseResponseAsJson(doRequest(new HttpDelete(), String.format("projects/%s/codes/%s", projectId, id), null));
+ return parseResponseAsJson(doDeleteRequest(String.format("projects/%s/codes/%s", projectId, id), null));
}
- public JsonObject codesRevisions(String id, Map<String, String> params) throws APIException {
- return parseResponseAsJson(doRequest(new HttpGet(), String.format("projects/%s/codes/%s/revisions", projectId, id), params));
+ public JsonObject codesRevisions(String id, Map<String, Object> params) throws APIException {
+ return parseResponseAsJson(doGetRequest(String.format("projects/%s/codes/%s/revisions", projectId, id), params));
+ }
+
+ public byte[] codesDownload(String id, Map<String, Object> params) throws APIException {
+ return parseResponseAsByteArray(doGetRequest(String.format("projects/%s/codes/%s/download", projectId, id), params));
+ }
+
+ public JsonObject tasksList(Map<String, Object> params) throws APIException {
+ return parseResponseAsJson(doGetRequest(String.format("projects/%s/tasks", projectId), params));
+ }
+
+ public JsonObject tasksGet(String id) throws APIException {
+ return parseResponseAsJson(doGetRequest(String.format("projects/%s/tasks/%s", projectId, id), null));
+ }
+
+ public JsonObject tasksCreate(String code_name, String payload, Map<String, Object> params) throws APIException {
+ Map<String, Object> task = new HashMap<String, Object>();
+
+ task.put("code_name", code_name);
+ task.put("payload", payload);
+ task.putAll(params);
+
+ List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
+
+ tasks.add(task);
+
+ Map<String, List<Map<String, Object>>> fullParams = new HashMap<String, List<Map<String, Object>>>();
+
+ fullParams.put("tasks", tasks);
+
+ return parseResponseAsJson(doPostRequest(String.format("projects/%s/tasks", projectId), (new Gson()).toJson(fullParams)));
+ }
+
+ public JsonObject tasksCancel(String id) throws APIException {
+ return parseResponseAsJson(doPostRequest(String.format("projects/%s/tasks/%s/cancel", projectId, id), null));
+ }
+
+ public JsonObject tasksCancelAll(String code_id) throws APIException {
+ return parseResponseAsJson(doPostRequest(String.format("projects/%s/codes/%s/cancel_all", projectId, code_id), null));
+ }
+
+ public String tasksLog(String id) throws APIException {
+ return parseResponseAsString(doGetRequest(String.format("projects/%s/tasks/%s/log", projectId, id), null));
+ }
+
+ public JsonObject tasksSetProgress(String id, Map<String, Object> params) throws APIException {
+ return parseResponseAsJson(doPostRequest(String.format("projects/%s/tasks/%s/progress", projectId, id), (new Gson()).toJson(params)));
+ }
+
+ public JsonObject schedulesList(Map<String, Object> params) throws APIException {
+ return parseResponseAsJson(doGetRequest(String.format("projects/%s/schedules", projectId), params));
+ }
+
+ public JsonObject schedulesGet(String id) throws APIException {
+ return parseResponseAsJson(doGetRequest(String.format("projects/%s/schedules/%s", projectId, id), null));
+ }
+
+ public JsonObject schedulesCreate(String code_name, String payload, Map<String, Object> params) throws APIException {
+ Map<String, Object> schedule = new HashMap<String, Object>();
+
+ schedule.put("code_name", code_name);
+ schedule.put("payload", payload);
+ schedule.putAll(params);
+
+ List<Map<String, Object>> schedules = new ArrayList<Map<String, Object>>();
+
+ schedules.add(schedule);
+
+ Map<String, List<Map<String, Object>>> fullParams = new HashMap<String, List<Map<String, Object>>>();
+
+ fullParams.put("schedules", schedules);
+
+ return parseResponseAsJson(doPostRequest(String.format("projects/%s/schedules", projectId), (new Gson()).toJson(fullParams)));
}
- public byte[] codesDownload(String id, Map<String, String> params) throws APIException {
- return parseResponseAsByteArray(doRequest(new HttpGet(), String.format("projects/%s/codes/%s/download", projectId, id), params));
+ public JsonObject schedulesCancel(String id) throws APIException {
+ return parseResponseAsJson(doPostRequest(String.format("projects/%s/schedules/%s/cancel", projectId, id), null));
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.