Skip to content
Browse files

Refactoring and complete task functionality for Client.

  • Loading branch information...
1 parent c7a0471 commit 2d7e313156cd1abe55f57280a07d4c2448b5343d Andrew Kirilenko committed Mar 22, 2012
View
68 client/src/main/java/io/iron/ironworker/client/APIClient.java
@@ -3,6 +3,7 @@
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
+import io.iron.ironworker.client.builders.Params;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
@@ -24,7 +25,6 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -273,66 +273,58 @@ private JsonObject parseResponseAsJson(HttpResponse response) throws APIExceptio
return (new JsonParser()).parse(new InputStreamReader(parseResponseGeneral(response))).getAsJsonObject();
}
- public JsonObject codesList(Map<String, Object> params) throws APIException {
- return parseResponseAsJson(doGetRequest(String.format("projects/%s/codes", projectId), params));
+ public JsonObject codesList(Map<String, Object> options) throws APIException {
+ return parseResponseAsJson(doGetRequest(String.format("projects/%s/codes", projectId), options));
}
public JsonObject codesGet(String id) throws APIException {
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, 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(String.format("projects/%s/codes", projectId), (new Gson()).toJson(params), f));
+ String data = (new Gson()).toJson(Params.create("name", name, "runtime", runtime, "file_name", runner));
+
+ return parseResponseAsJson(doFileRequest(String.format("projects/%s/codes", projectId), data, f));
}
public JsonObject codesDelete(String id) throws APIException {
return parseResponseAsJson(doDeleteRequest(String.format("projects/%s/codes/%s", projectId, id), null));
}
- 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 JsonObject codesRevisions(String id, Map<String, Object> options) throws APIException {
+ return parseResponseAsJson(doGetRequest(String.format("projects/%s/codes/%s/revisions", projectId, id), options));
}
- 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 byte[] codesDownload(String id, Map<String, Object> options) throws APIException {
+ return parseResponseAsByteArray(doGetRequest(String.format("projects/%s/codes/%s/download", projectId, id), options));
}
- public JsonObject tasksList(Map<String, Object> params) throws APIException {
- return parseResponseAsJson(doGetRequest(String.format("projects/%s/tasks", projectId), params));
+ public JsonObject tasksList(Map<String, Object> options) throws APIException {
+ return parseResponseAsJson(doGetRequest(String.format("projects/%s/tasks", projectId), options));
}
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>();
+ public JsonObject tasksCreate(String code_name, String payload, Map<String, Object> options) throws APIException {
+ Map<String, Object> task = Params.create("code_name", code_name, "payload", payload);
- task.put("code_name", code_name);
- task.put("payload", payload);
- task.putAll(params);
+ if (options != null) {
+ task.putAll(options);
+ }
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)));
+ return parseResponseAsJson(doPostRequest(String.format("projects/%s/tasks", projectId), (new Gson()).toJson(Params.create("tasks", tasks))));
}
public JsonObject tasksCancel(String id) throws APIException {
@@ -347,34 +339,30 @@ 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 tasksSetProgress(String id, Map<String, Object> options) throws APIException {
+ return parseResponseAsJson(doPostRequest(String.format("projects/%s/tasks/%s/progress", projectId, id), (new Gson()).toJson(options)));
}
- public JsonObject schedulesList(Map<String, Object> params) throws APIException {
- return parseResponseAsJson(doGetRequest(String.format("projects/%s/schedules", projectId), params));
+ public JsonObject schedulesList(Map<String, Object> options) throws APIException {
+ return parseResponseAsJson(doGetRequest(String.format("projects/%s/schedules", projectId), options));
}
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>();
+ public JsonObject schedulesCreate(String code_name, String payload, Map<String, Object> options) throws APIException {
+ Map<String, Object> schedule = Params.create("code_name", code_name, "payload", payload);
- schedule.put("code_name", code_name);
- schedule.put("payload", payload);
- schedule.putAll(params);
+ if (options != null) {
+ schedule.putAll(options);
+ }
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)));
+ return parseResponseAsJson(doPostRequest(String.format("projects/%s/schedules", projectId), (new Gson()).toJson(Params.create("schedules", schedule))));
}
public JsonObject schedulesCancel(String id) throws APIException {
View
108 client/src/main/java/io/iron/ironworker/client/Client.java
@@ -1,10 +1,13 @@
package io.iron.ironworker.client;
+import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
+import io.iron.ironworker.client.builders.Params;
import io.iron.ironworker.client.codes.BaseCode;
import io.iron.ironworker.client.entities.CodeEntity;
import io.iron.ironworker.client.entities.CodeRevisionEntity;
+import io.iron.ironworker.client.entities.TaskEntity;
import java.util.ArrayList;
import java.util.HashMap;
@@ -21,9 +24,9 @@ public Client(String token, String projectId) {
public APIClient getAPI() {
return api;
}
-
- private List<CodeEntity> getCodes(Map<String, Object> params) throws APIException {
- JsonObject codes = api.codesList(params);
+
+ public List<CodeEntity> getCodes(Map<String, Object> options) throws APIException {
+ JsonObject codes = api.codesList(options);
List<CodeEntity> codesList = new ArrayList<CodeEntity>();
@@ -38,23 +41,6 @@ public APIClient getAPI() {
return getCodes(null);
}
- public List<CodeEntity> getCodes(int page) throws APIException {
- Map<String, Object> params = new HashMap<String, Object>();
-
- params.put("page", page);
-
- return getCodes(params);
- }
-
- public List<CodeEntity> getCodes(int page, int perPage) throws APIException {
- Map<String, Object> params = new HashMap<String, Object>();
-
- params.put("page", page);
- params.put("per_page", perPage);
-
- return getCodes(params);
- }
-
public CodeEntity getCode(String codeId) throws APIException {
return CodeEntity.fromJsonObject(api.codesGet(codeId));
}
@@ -67,8 +53,8 @@ public void deleteCode(String codeId) throws APIException {
api.codesDelete(codeId);
}
- private List<CodeRevisionEntity> getCodeRevisions(String codeId, Map<String, Object> params) throws APIException {
- JsonObject codeRevisions = api.codesRevisions(codeId, params);
+ public List<CodeRevisionEntity> getCodeRevisions(String codeId, Map<String, Object> options) throws APIException {
+ JsonObject codeRevisions = api.codesRevisions(codeId, options);
List<CodeRevisionEntity> codeRevisionsList = new ArrayList<CodeRevisionEntity>();
@@ -83,36 +69,78 @@ public void deleteCode(String codeId) throws APIException {
return getCodeRevisions(codeId, null);
}
- public List<CodeRevisionEntity> getCodeRevisions(String codeId, int page) throws APIException {
- Map<String, Object> params = new HashMap<String, Object>();
+ private byte[] downloadCode(String codeId, Map<String, Object> options) throws APIException {
+ return api.codesDownload(codeId, options);
+ }
+
+ public byte[] downloadCode(String codeId) throws APIException {
+ return downloadCode(codeId, null);
+ }
- params.put("page", page);
+ private List<TaskEntity> getTasks(Map<String, Object> options) throws APIException {
+ JsonObject tasks = api.tasksList(options);
- return getCodeRevisions(codeId, params);
- }
+ List<TaskEntity> tasksList = new ArrayList<TaskEntity>();
- public List<CodeRevisionEntity> getCodeRevisions(String codeId, int page, int perPage) throws APIException {
- Map<String, Object> params = new HashMap<String, Object>();
+ for (JsonElement task : tasks.get("tasks").getAsJsonArray()) {
+ tasksList.add(TaskEntity.fromJsonObject(task.getAsJsonObject()));
+ }
- params.put("page", page);
- params.put("per_page", perPage);
+ return tasksList;
+ }
- return getCodeRevisions(codeId, params);
+ public List<TaskEntity> getTasks() throws APIException {
+ return getTasks(null);
}
- private byte[] downloadCode(String codeId, Map<String, Object> params) throws APIException {
- return api.codesDownload(codeId, params);
+ public TaskEntity getTask(String taskId) throws APIException {
+ return TaskEntity.fromJsonObject(api.tasksGet(taskId));
}
- public byte[] downloadCode(String codeId) throws APIException {
- return downloadCode(codeId, null);
+ public TaskEntity createTask(String codeName, Map<String, Object> params, Map<String, Object> options) throws APIException {
+ if (params == null) {
+ params = new HashMap<String, Object>();
+ }
+
+ JsonObject tasks = api.tasksCreate(codeName, (new Gson()).toJson(Params.create("token", api.getToken(), "project_id", api.getProjectId(), "params", params)), options);
+ JsonObject task = tasks.get("tasks").getAsJsonArray().get(0).getAsJsonObject();
+
+ return TaskEntity.fromJsonObject(task);
+ }
+
+ public TaskEntity createTask(String codeName, Params params, Map<String, Object> options) throws APIException {
+ return createTask(codeName, params.create(), options);
+ }
+
+ public TaskEntity createTask(String codeName, Map<String, Object> params) throws APIException {
+ return createTask(codeName, params, null);
+ }
+
+ public TaskEntity createTask(String codeName, Params params) throws APIException {
+ return createTask(codeName, params.create(), null);
}
- public byte[] downloadCode(String codeId, int revision) throws APIException {
- Map<String, Object> params = new HashMap<String, Object>();
+ public TaskEntity createTask(String codeName) throws APIException {
+ return createTask(codeName, (Map<String, Object>) null, null);
+ }
+
+ public void cancelTask(String taskId) throws APIException {
+ api.tasksCancel(taskId);
+ }
- params.put("revision", revision);
+ public void cancelAllTasks(String codeId) throws APIException {
+ api.tasksCancelAll(codeId);
+ }
+
+ public String getTaskLog(String taskId) throws APIException {
+ return api.tasksLog(taskId);
+ }
+
+ public void setTaskProgress(String taskId, Map<String, Object> options) throws APIException {
+ api.tasksSetProgress(taskId, options);
+ }
- return downloadCode(codeId, params);
+ public void setTaskProgress(String taskId) throws APIException {
+ api.tasksSetProgress(taskId, null);
}
}
View
38 client/src/main/java/io/iron/ironworker/client/builders/Params.java
@@ -0,0 +1,38 @@
+package io.iron.ironworker.client.builders;
+
+import io.iron.ironworker.client.APIException;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class Params {
+ private Map<String, Object> params;
+
+ public static Map<String, Object> create(Object... os) throws APIException {
+ if (os.length % 2 != 0) {
+ throw new APIException("Odd params number", null);
+ }
+
+ Map<String, Object> params = new HashMap<String, Object>();
+
+ for (int i = 0; i < os.length; i += 2) {
+ params.put(os[i].toString(), os[i + 1]);
+ }
+
+ return params;
+ }
+
+ public Params() {
+ params = new HashMap<String, Object>();
+ }
+
+ public Params add(String key, String value) {
+ params.put(key, value);
+
+ return this;
+ }
+
+ public Map<String, Object> create() {
+ return params;
+ }
+}
View
25 client/src/main/java/io/iron/ironworker/client/entities/BaseEntity.java
@@ -1,12 +1,33 @@
package io.iron.ironworker.client.entities;
+import com.google.gson.JsonObject;
import org.joda.time.format.ISODateTimeFormat;
import java.util.Date;
public class BaseEntity {
- public static Date parseDate(String date) {
- return ISODateTimeFormat.dateTimeNoMillis().parseDateTime(date).toDate();
+ public static String parseString(JsonObject o, String field, String def) {
+ return o.has(field) ? o.get(field).getAsString() : def;
+ }
+
+ public static String parseString(JsonObject o, String field) {
+ return parseString(o, field, null);
+ }
+
+ public static int parseInt(JsonObject o, String field, int def) {
+ return o.has(field) ? o.get(field).getAsInt() : def;
+ }
+
+ public static int parseInt(JsonObject o, String field) {
+ return parseInt(o, field, 0);
+ }
+
+ public static Date parseDate(JsonObject o, String field, Date def) {
+ return o.has(field) ? ISODateTimeFormat.dateTimeNoMillis().parseDateTime(o.get(field).getAsString()).toDate() : def;
+ }
+
+ public static Date parseDate(JsonObject o, String field) {
+ return parseDate(o, field, null);
}
protected BaseEntity() {
View
20 client/src/main/java/io/iron/ironworker/client/entities/CodeEntity.java
@@ -19,16 +19,16 @@
public static CodeEntity fromJsonObject(JsonObject o) {
CodeEntity c = new CodeEntity();
- c.id = o.get("id").getAsString();
- c.projectId = o.get("project_id").getAsString();
- c.name = o.get("name").getAsString();
- c.runtime = o.get("runtime").getAsString();
- c.revision = o.get("rev").getAsInt();
- c.latestHistoryId = o.get("latest_history_id").getAsString();
- c.latestChecksum = o.get("latest_checksum").getAsString();
- c.latestChange = parseDate(o.get("latest_change").getAsString());
- c.createdAt = parseDate(o.get("created_at").getAsString());
- c.updatedAt = parseDate(o.get("updated_at").getAsString());
+ c.id = parseString(o, "id");
+ c.projectId = parseString(o, "project_id");
+ c.name = parseString(o, "name");
+ c.runtime = parseString(o, "runtime");
+ c.revision = parseInt(o, "rev");
+ c.latestHistoryId = parseString(o, "latest_history_id");
+ c.latestChecksum = parseString(o, "latest_checksum");
+ c.latestChange = parseDate(o, "latest_change");
+ c.createdAt = parseDate(o, "created_at");
+ c.updatedAt = parseDate(o, "updated_at");
return c;
}
View
32 client/src/main/java/io/iron/ironworker/client/entities/CodeRevisionEntity.java
@@ -10,25 +10,25 @@
String projectId;
String name;
String runtime;
- String fileName;
+ String runner;
int revision;
Date createdAt;
Date updatedAt;
public static CodeRevisionEntity fromJsonObject(JsonObject o) {
- CodeRevisionEntity c = new CodeRevisionEntity();
-
- c.id = o.get("id").getAsString();
- c.codeId = o.get("code_id").getAsString();
- c.projectId = o.get("project_id").getAsString();
- c.name = o.get("name").getAsString();
- c.runtime = o.get("runtime").getAsString();
- c.fileName = o.get("file_name").getAsString();
- c.revision = o.get("rev").getAsInt();
- c.createdAt = parseDate(o.get("created_at").getAsString());
- c.updatedAt = parseDate(o.get("updated_at").getAsString());
-
- return c;
+ CodeRevisionEntity cr = new CodeRevisionEntity();
+
+ cr.id = parseString(o, "id");
+ cr.codeId = parseString(o, "code_id");
+ cr.projectId = parseString(o, "project_id");
+ cr.name = parseString(o, "name");
+ cr.runtime = parseString(o, "runtime");
+ cr.runner = parseString(o, "file_name");
+ cr.revision = parseInt(o, "rev");
+ cr.createdAt = parseDate(o, "created_at");
+ cr.updatedAt = parseDate(o, "updated_at");
+
+ return cr;
}
protected CodeRevisionEntity() {
@@ -54,8 +54,8 @@ public String getRuntime() {
return runtime;
}
- public String getFileName() {
- return fileName;
+ public String getRunner() {
+ return runner;
}
public int getRevision() {
View
108 client/src/main/java/io/iron/ironworker/client/entities/TaskEntity.java
@@ -0,0 +1,108 @@
+package io.iron.ironworker.client.entities;
+
+import com.google.gson.JsonObject;
+
+import java.util.Date;
+
+public class TaskEntity extends BaseEntity {
+ String id;
+ String projectId;
+ String codeId;
+ String codeName;
+ String status;
+ Date startTime;
+ Date endTime;
+ int duration;
+ int runTimes;
+ int timeout;
+ String payload;
+ int percent;
+ String msg;
+ Date createdAt;
+ Date updatedAt;
+
+ public static TaskEntity fromJsonObject(JsonObject o) {
+ TaskEntity t = new TaskEntity();
+
+ t.id = parseString(o, "id");
+ t.projectId = parseString(o, "project_id");
+ t.codeId = parseString(o, "code_id");
+ t.codeName = parseString(o, "code_name");
+ t.status = parseString(o, "status");
+ t.startTime = parseDate(o, "start_time");
+ t.endTime = parseDate(o, "end_time");
+ t.duration = parseInt(o, "duration");
+ t.runTimes = parseInt(o, "run_times");
+ t.timeout = parseInt(o, "timeout");
+ t.payload = parseString(o, "payload");
+ t.percent = parseInt(o, "percent");
+ t.msg = parseString(o, "msg");
+ t.createdAt = parseDate(o, "created_at");
+ t.updatedAt = parseDate(o, "updated_at");
+
+ return t;
+ }
+
+ protected TaskEntity() {
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public String getProjectId() {
+ return projectId;
+ }
+
+ public String getCodeId() {
+ return codeId;
+ }
+
+ public String getCodeName() {
+ return codeName;
+ }
+
+ public String getStatus() {
+ return status;
+ }
+
+ public Date getStartTime() {
+ return startTime;
+ }
+
+ public Date getEndTime() {
+ return endTime;
+ }
+
+ public int getDuration() {
+ return duration;
+ }
+
+ public int getRunTimes() {
+ return runTimes;
+ }
+
+ public int getTimeout() {
+ return timeout;
+ }
+
+ public String getPayload() {
+ return payload;
+ }
+
+ public int getPercent() {
+ return percent;
+ }
+
+ public String getMsg() {
+ return msg;
+ }
+
+ public Date getCreatedAt() {
+ return createdAt;
+ }
+
+ public Date getUpdatedAt() {
+ return updatedAt;
+ }
+}

0 comments on commit 2d7e313

Please sign in to comment.
Something went wrong with that request. Please try again.