Permalink
Browse files

Merge from origin

  • Loading branch information...
2 parents 2e19478 + 455fb28 commit baeb3a07887a40ee5497b7a1dd3a3284447e9e45 @richard-julien committed Apr 8, 2012
@@ -24,9 +24,8 @@
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
+import java.text.SimpleDateFormat;
+import java.util.*;
public class APIClient {
public static final String AWS_US_EAST_HOST = "worker-aws-us-east-1.iron.io";
@@ -40,6 +39,8 @@
private String userAgent;
private int maxRetries;
+ private Gson gson;
+
public APIClient(String token, String projectId) {
this.token = token;
this.projectId = projectId;
@@ -49,6 +50,8 @@ public APIClient(String token, String projectId) {
this.apiVersion = 2;
this.userAgent = "iron_worker_java-1.0.0";
maxRetries = 5;
+
+ gson = new Gson();
}
public String getToken() {
@@ -288,7 +291,7 @@ public JsonObject codesCreate(String name, String file, String runtime, String r
throw new APIException("File " + file + " not found", null);
}
- String data = (new Gson()).toJson(Params.create("name", name, "runtime", runtime, "file_name", runner));
+ String data = gson.toJson(Params.create("name", name, "runtime", runtime, "file_name", runner));
return parseResponseAsJson(doFileRequest(String.format("projects/%s/codes", projectId), data, f));
}
@@ -324,7 +327,7 @@ public JsonObject tasksCreate(String code_name, String payload, Map<String, Obje
tasks.add(task);
- return parseResponseAsJson(doPostRequest(String.format("projects/%s/tasks", projectId), (new Gson()).toJson(Params.create("tasks", tasks))));
+ return parseResponseAsJson(doPostRequest(String.format("projects/%s/tasks", projectId), gson.toJson(Params.create("tasks", tasks))));
}
public JsonObject tasksCancel(String id) throws APIException {
@@ -340,7 +343,7 @@ public String tasksLog(String id) throws APIException {
}
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)));
+ return parseResponseAsJson(doPostRequest(String.format("projects/%s/tasks/%s/progress", projectId, id), gson.toJson(options)));
}
public JsonObject schedulesList(Map<String, Object> options) throws APIException {
@@ -355,14 +358,27 @@ public JsonObject schedulesCreate(String code_name, String payload, Map<String,
Map<String, Object> schedule = Params.create("code_name", code_name, "payload", payload);
if (options != null) {
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
+ format.setTimeZone(TimeZone.getTimeZone("UTC"));
+
+ if (options.containsKey("start_at") && options.get("start_at").getClass() == Date.class) {
+ Date d = (Date) options.get("start_at");
+ options.put("start_at", format.format(d));
+ }
+
+ if (options.containsKey("end_at") && options.get("end_at").getClass() == Date.class) {
+ Date d = (Date) options.get("end_at");
+ options.put("end_at", format.format(d));
+ }
+
schedule.putAll(options);
}
List<Map<String, Object>> schedules = new ArrayList<Map<String, Object>>();
schedules.add(schedule);
- return parseResponseAsJson(doPostRequest(String.format("projects/%s/schedules", projectId), (new Gson()).toJson(Params.create("schedules", schedules))));
+ return parseResponseAsJson(doPostRequest(String.format("projects/%s/schedules", projectId), gson.toJson(Params.create("schedules", schedules))));
}
public JsonObject schedulesCancel(String id) throws APIException {
@@ -7,7 +7,7 @@
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.Codes;
+import io.iron.ironworker.client.entities.ScheduleEntity;
import io.iron.ironworker.client.entities.TaskEntity;
import java.util.ArrayList;
@@ -17,32 +17,40 @@
public class Client {
private APIClient api;
-
+ private Gson gson;
+
public Client(String token, String projectId) {
api = new APIClient(token, projectId);
+ gson = new Gson();
}
public APIClient getAPI() {
return api;
}
-
- public Codes getCodes(Map<String, Object> options) throws APIException {
+
+ public List<CodeEntity> getCodes(Map<String, Object> options) throws APIException {
JsonObject codes = api.codesList(options);
- return new Gson().fromJson(codes, Codes.class);
+ List<CodeEntity> codesList = new ArrayList<CodeEntity>();
+
+ for (JsonElement code : codes.get("codes").getAsJsonArray()) {
+ codesList.add(gson.fromJson(code, CodeEntity.class));
+ }
+
+ return codesList;
}
- public Codes getCodes(PaginationOptionsObject options) throws APIException {
+ public List<CodeEntity> getCodes(PaginationOptionsObject options) throws APIException {
return getCodes(options.create());
}
- public Codes getCodes() throws APIException {
+ public List<CodeEntity> getCodes() throws APIException {
return getCodes((Map<String, Object>) null);
}
public CodeEntity getCode(String codeId) throws APIException {
- return CodeEntity.fromJsonObject(api.codesGet(codeId));
+ return gson.fromJson(api.codesGet(codeId), CodeEntity.class);
}
-
+
public void createCode(BaseCode code) throws APIException {
api.codesCreate(code.getName(), code.getFile(), code.getRuntime(), code.getRunner());
}
@@ -57,7 +65,7 @@ public void deleteCode(String codeId) throws APIException {
List<CodeRevisionEntity> codeRevisionsList = new ArrayList<CodeRevisionEntity>();
for (JsonElement codeRevision : codeRevisions.get("revisions").getAsJsonArray()) {
- codeRevisionsList.add(CodeRevisionEntity.fromJsonObject(codeRevision.getAsJsonObject()));
+ codeRevisionsList.add(gson.fromJson(codeRevision, CodeRevisionEntity.class));
}
return codeRevisionsList;
@@ -89,7 +97,7 @@ public void deleteCode(String codeId) throws APIException {
List<TaskEntity> tasksList = new ArrayList<TaskEntity>();
for (JsonElement task : tasks.get("tasks").getAsJsonArray()) {
- tasksList.add(TaskEntity.fromJsonObject(task.getAsJsonObject()));
+ tasksList.add(gson.fromJson(task, TaskEntity.class));
}
return tasksList;
@@ -104,18 +112,18 @@ public void deleteCode(String codeId) throws APIException {
}
public TaskEntity getTask(String taskId) throws APIException {
- return TaskEntity.fromJsonObject(api.tasksGet(taskId));
+ return gson.fromJson(api.tasksGet(taskId), TaskEntity.class);
}
-
+
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 tasks = api.tasksCreate(codeName, 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);
+ return gson.fromJson(task, TaskEntity.class);
}
public TaskEntity createTask(String codeName, Map<String, Object> params, TaskOptionsObject options) throws APIException {
@@ -141,19 +149,19 @@ public TaskEntity createTask(String codeName, ParamsObject params) throws APIExc
public TaskEntity createTask(String codeName) throws APIException {
return createTask(codeName, (Map<String, Object>) null, (Map<String, Object>) null);
}
-
+
public void cancelTask(String taskId) throws APIException {
api.tasksCancel(taskId);
}
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);
}
@@ -165,4 +173,67 @@ public void setTaskProgress(String taskId, TaskProgressOptionsObject options) th
public void setTaskProgress(String taskId) throws APIException {
setTaskProgress(taskId, (Map<String, Object>) null);
}
+
+ public List<ScheduleEntity> getSchedules(Map<String, Object> options) throws APIException {
+ JsonObject schedules = api.schedulesList(options);
+
+ List<ScheduleEntity> schedulesList = new ArrayList<ScheduleEntity>();
+
+ for (JsonElement schedule : schedules.get("schedules").getAsJsonArray()) {
+ schedulesList.add(gson.fromJson(schedule, ScheduleEntity.class));
+ }
+
+ return schedulesList;
+ }
+
+ public List<ScheduleEntity> getSchedules(PaginationOptionsObject options) throws APIException {
+ return getSchedules(options.create());
+ }
+
+ public List<ScheduleEntity> getSchedules() throws APIException {
+ return getSchedules((Map<String, Object>) null);
+ }
+
+ public ScheduleEntity getSchedule(String scheduleId) throws APIException {
+ return gson.fromJson(api.schedulesGet(scheduleId), ScheduleEntity.class);
+ }
+
+ public ScheduleEntity createSchedule(String codeName, Map<String, Object> params, Map<String, Object> options) throws APIException {
+ if (params == null) {
+ params = new HashMap<String, Object>();
+ }
+
+ JsonObject schedules = api.schedulesCreate(codeName, gson.toJson(Params.create("token", api.getToken(), "project_id", api.getProjectId(), "params", params)), options);
+ JsonObject schedule = schedules.get("schedules").getAsJsonArray().get(0).getAsJsonObject();
+
+ return gson.fromJson(schedule, ScheduleEntity.class);
+ }
+
+ public ScheduleEntity createSchedule(String codeName, Map<String, Object> params, ScheduleOptionsObject options) throws APIException {
+ return createSchedule(codeName, params, options.create());
+ }
+
+ public ScheduleEntity createSchedule(String codeName, ParamsObject params, Map<String, Object> options) throws APIException {
+ return createSchedule(codeName, params.create(), options);
+ }
+
+ public ScheduleEntity createSchedule(String codeName, ParamsObject params, ScheduleOptionsObject options) throws APIException {
+ return createSchedule(codeName, params.create(), options.create());
+ }
+
+ public ScheduleEntity createSchedule(String codeName, Map<String, Object> params) throws APIException {
+ return createSchedule(codeName, params, (Map<String, Object>) null);
+ }
+
+ public ScheduleEntity createSchedule(String codeName, ParamsObject params) throws APIException {
+ return createSchedule(codeName, params.create(), (Map<String, Object>) null);
+ }
+
+ public ScheduleEntity createSchedule(String codeName) throws APIException {
+ return createSchedule(codeName, (Map<String, Object>) null, (Map<String, Object>) null);
+ }
+
+ public void cancelSchedule(String scheduleId) throws APIException {
+ api.schedulesCancel(scheduleId);
+ }
}
@@ -0,0 +1,32 @@
+package io.iron.ironworker.client.builders;
+
+import java.util.Date;
+
+public class ScheduleOptions {
+ public static ScheduleOptionsObject priority(int priority) {
+ return (new ScheduleOptionsObject()).priority(priority);
+ }
+
+ public static ScheduleOptionsObject startAt(Date startAt) {
+ return (new ScheduleOptionsObject()).startAt(startAt);
+ }
+
+ public static ScheduleOptionsObject endAt(Date endAt) {
+ return (new ScheduleOptionsObject()).endAt(endAt);
+ }
+
+ public static ScheduleOptionsObject delay(int delay) {
+ return (new ScheduleOptionsObject()).delay(delay);
+ }
+
+ public static ScheduleOptionsObject runEvery(int runEvery) {
+ return (new ScheduleOptionsObject()).runEvery(runEvery);
+ }
+
+ public static ScheduleOptionsObject runTimes(int runTimes) {
+ return (new ScheduleOptionsObject()).runTimes(runTimes);
+ }
+
+ protected ScheduleOptions() {
+ }
+}
@@ -0,0 +1,53 @@
+package io.iron.ironworker.client.builders;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+public class ScheduleOptionsObject {
+ private Map<String, Object> options;
+
+ public ScheduleOptionsObject() {
+ options = new HashMap<String, Object>();
+ }
+
+ public ScheduleOptionsObject priority(int priority) {
+ options.put("priority", priority);
+
+ return this;
+ }
+
+ public ScheduleOptionsObject startAt(Date startAt) {
+ options.put("start_at", startAt);
+
+ return this;
+ }
+
+ public ScheduleOptionsObject endAt(Date endAt) {
+ options.put("end_at", endAt);
+
+ return this;
+ }
+
+ public ScheduleOptionsObject delay(int delay) {
+ options.put("delay", delay);
+
+ return this;
+ }
+
+ public ScheduleOptionsObject runEvery(int runEvery) {
+ options.put("run_every", runEvery);
+
+ return this;
+ }
+
+ public ScheduleOptionsObject runTimes(int runTimes) {
+ options.put("run_times", runTimes);
+
+ return this;
+ }
+
+ public Map<String, Object> create() {
+ return options;
+ }
+}
@@ -1,35 +0,0 @@
-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 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() {
- }
-}
Oops, something went wrong.

0 comments on commit baeb3a0

Please sign in to comment.