Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Complete schedule functionality for Client.

  • Loading branch information...
commit 455fb28a9130bd415120174b47bb61be79ffb4bb 1 parent 27fdb14
@iced iced authored
View
30 client/src/main/java/io/iron/ironworker/client/APIClient.java
@@ -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,6 +358,19 @@ 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);
}
@@ -362,7 +378,7 @@ public JsonObject schedulesCreate(String code_name, String payload, Map<String,
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 {
View
82 client/src/main/java/io/iron/ironworker/client/Client.java
@@ -7,6 +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.ScheduleEntity;
import io.iron.ironworker.client.entities.TaskEntity;
import java.util.ArrayList;
@@ -16,9 +17,11 @@
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() {
@@ -30,8 +33,6 @@ public APIClient getAPI() {
List<CodeEntity> codesList = new ArrayList<CodeEntity>();
- Gson gson = new Gson();
-
for (JsonElement code : codes.get("codes").getAsJsonArray()) {
codesList.add(gson.fromJson(code, CodeEntity.class));
}
@@ -48,7 +49,7 @@ public APIClient getAPI() {
}
public CodeEntity getCode(String codeId) throws APIException {
- return (new Gson()).fromJson(api.codesGet(codeId), CodeEntity.class);
+ return gson.fromJson(api.codesGet(codeId), CodeEntity.class);
}
public void createCode(BaseCode code) throws APIException {
@@ -64,8 +65,6 @@ public void deleteCode(String codeId) throws APIException {
List<CodeRevisionEntity> codeRevisionsList = new ArrayList<CodeRevisionEntity>();
- Gson gson = new Gson();
-
for (JsonElement codeRevision : codeRevisions.get("revisions").getAsJsonArray()) {
codeRevisionsList.add(gson.fromJson(codeRevision, CodeRevisionEntity.class));
}
@@ -98,8 +97,6 @@ public void deleteCode(String codeId) throws APIException {
List<TaskEntity> tasksList = new ArrayList<TaskEntity>();
- Gson gson = new Gson();
-
for (JsonElement task : tasks.get("tasks").getAsJsonArray()) {
tasksList.add(gson.fromJson(task, TaskEntity.class));
}
@@ -116,18 +113,18 @@ public void deleteCode(String codeId) throws APIException {
}
public TaskEntity getTask(String taskId) throws APIException {
- return (new Gson()).fromJson(api.tasksGet(taskId), TaskEntity.class);
+ 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 (new Gson()).fromJson(task, TaskEntity.class);
+ return gson.fromJson(task, TaskEntity.class);
}
public TaskEntity createTask(String codeName, Map<String, Object> params, TaskOptionsObject options) throws APIException {
@@ -177,4 +174,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);
+ }
}
View
32 client/src/main/java/io/iron/ironworker/client/builders/ScheduleOptions.java
@@ -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() {
+ }
+}
View
53 client/src/main/java/io/iron/ironworker/client/builders/ScheduleOptionsObject.java
@@ -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;
+ }
+}
View
113 client/src/main/java/io/iron/ironworker/client/entities/ScheduleEntity.java
@@ -0,0 +1,113 @@
+package io.iron.ironworker.client.entities;
+
+import com.google.gson.annotations.SerializedName;
+
+import java.util.Date;
+
+public class ScheduleEntity {
+ @SerializedName("id")
+ String id;
+ @SerializedName("project_id")
+ String projectId;
+ @SerializedName("code_name")
+ String codeName;
+ @SerializedName("priority")
+ int priority;
+ @SerializedName("start_at")
+ Date startAt;
+ @SerializedName("end_at")
+ Date endAt;
+ @SerializedName("delay")
+ int delay;
+ @SerializedName("run_every")
+ int runEvery;
+ @SerializedName("run_times")
+ int runTimes;
+ @SerializedName("next_start")
+ Date nextStart;
+ @SerializedName("status")
+ String status;
+ @SerializedName("last_run_time")
+ long lastRunTime;
+ @SerializedName("run_count")
+ int runCount;
+ @SerializedName("payload")
+ String payload;
+ @SerializedName("msg")
+ String msg;
+ @SerializedName("created_at")
+ long createdAt;
+ @SerializedName("updated_at")
+ long updatedAt;
+
+ protected ScheduleEntity() {
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public String getProjectId() {
+ return projectId;
+ }
+
+ public String getCodeName() {
+ return codeName;
+ }
+
+ public int getPriority() {
+ return priority;
+ }
+
+ public Date getStartAt() {
+ return startAt;
+ }
+
+ public Date getEndAt() {
+ return endAt;
+ }
+
+ public int getDelay() {
+ return delay;
+ }
+
+ public int getRunEvery() {
+ return runEvery;
+ }
+
+ public int getRunTimes() {
+ return runTimes;
+ }
+
+ public Date getNextStart() {
+ return nextStart;
+ }
+
+ public String getStatus() {
+ return status;
+ }
+
+ public Date getLastRunTime() {
+ return lastRunTime == 0 ? null : new Date(lastRunTime / 1000000);
+ }
+
+ public int getRunCount() {
+ return runCount;
+ }
+
+ public String getPayload() {
+ return payload;
+ }
+
+ public String getMsg() {
+ return msg;
+ }
+
+ public Date getCreatedAt() {
+ return createdAt == 0 ? null : new Date(createdAt / 1000000);
+ }
+
+ public Date getUpdatedAt() {
+ return updatedAt == 0 ? null : new Date(updatedAt / 1000000);
+ }
+}
View
18 client/src/main/java/io/iron/ironworker/client/entities/TaskEntity.java
@@ -13,6 +13,10 @@
String codeId;
@SerializedName("code_name")
String codeName;
+ @SerializedName("priority")
+ int priority;
+ @SerializedName("timeout")
+ int timeout;
@SerializedName("status")
String status;
@SerializedName("start_time")
@@ -23,8 +27,6 @@
int duration;
@SerializedName("run_times")
int runTimes;
- @SerializedName("timeout")
- int timeout;
@SerializedName("payload")
String payload;
@SerializedName("percent")
@@ -55,6 +57,14 @@ public String getCodeName() {
return codeName;
}
+ public int getPriority() {
+ return priority;
+ }
+
+ public int getTimeout() {
+ return timeout;
+ }
+
public String getStatus() {
return status;
}
@@ -75,10 +85,6 @@ public int getRunTimes() {
return runTimes;
}
- public int getTimeout() {
- return timeout;
- }
-
public String getPayload() {
return payload;
}
Please sign in to comment.
Something went wrong with that request. Please try again.