forked from iron-io/iron_worker_java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.java
174 lines (129 loc) · 6.25 KB
/
Client.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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.*;
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;
import java.util.List;
import java.util.Map;
public class Client {
private APIClient api;
public Client(String token, String projectId) {
api = new APIClient(token, projectId);
}
public APIClient getAPI() {
return api;
}
public List<CodeEntity> getCodes(Map<String, Object> options) throws APIException {
JsonObject codes = api.codesList(options);
List<CodeEntity> codesList = new ArrayList<CodeEntity>();
for (JsonElement code : codes.get("codes").getAsJsonArray()) {
codesList.add(CodeEntity.fromJsonObject(code.getAsJsonObject()));
}
return codesList;
}
public List<CodeEntity> getCodes(PaginationOptionsObject options) throws APIException {
return getCodes(options.create());
}
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));
}
public void createCode(BaseCode code) throws APIException {
api.codesCreate(code.getName(), code.getFile(), code.getRuntime(), code.getRunner());
}
public void deleteCode(String codeId) throws APIException {
api.codesDelete(codeId);
}
public List<CodeRevisionEntity> getCodeRevisions(String codeId, Map<String, Object> options) throws APIException {
JsonObject codeRevisions = api.codesRevisions(codeId, options);
List<CodeRevisionEntity> codeRevisionsList = new ArrayList<CodeRevisionEntity>();
for (JsonElement codeRevision : codeRevisions.get("revisions").getAsJsonArray()) {
codeRevisionsList.add(CodeRevisionEntity.fromJsonObject(codeRevision.getAsJsonObject()));
}
return codeRevisionsList;
}
public List<CodeRevisionEntity> getCodeRevisions(String codeId, PaginationOptionsObject options) throws APIException {
return getCodeRevisions(codeId, options.create());
}
public List<CodeRevisionEntity> getCodeRevisions(String codeId) throws APIException {
return getCodeRevisions(codeId, (Map<String, Object>) null);
}
public byte[] downloadCode(String codeId, Map<String, Object> options) throws APIException {
return api.codesDownload(codeId, options);
}
public byte[] downloadCode(String codeId, int revision) throws APIException {
return downloadCode(codeId, Params.create("revision", revision));
}
public byte[] downloadCode(String codeId) throws APIException {
return downloadCode(codeId, null);
}
public List<TaskEntity> getTasks(Map<String, Object> options) throws APIException {
JsonObject tasks = api.tasksList(options);
List<TaskEntity> tasksList = new ArrayList<TaskEntity>();
for (JsonElement task : tasks.get("tasks").getAsJsonArray()) {
tasksList.add(TaskEntity.fromJsonObject(task.getAsJsonObject()));
}
return tasksList;
}
public List<TaskEntity> getTasks(PaginationOptionsObject options) throws APIException {
return getTasks(options.create());
}
public List<TaskEntity> getTasks() throws APIException {
return getTasks((Map<String, Object>) null);
}
public TaskEntity getTask(String taskId) throws APIException {
return TaskEntity.fromJsonObject(api.tasksGet(taskId));
}
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, Map<String, Object> params, TaskOptionsObject options) throws APIException {
return createTask(codeName, params, options.create());
}
public TaskEntity createTask(String codeName, ParamsObject params, Map<String, Object> options) throws APIException {
return createTask(codeName, params.create(), options);
}
public TaskEntity createTask(String codeName, ParamsObject params, TaskOptionsObject options) throws APIException {
return createTask(codeName, params.create(), options.create());
}
public TaskEntity createTask(String codeName, Map<String, Object> params) throws APIException {
return createTask(codeName, params, (Map<String, Object>) null);
}
public TaskEntity createTask(String codeName, ParamsObject params) throws APIException {
return createTask(codeName, params.create(), (Map<String, Object>) null);
}
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);
}
public void setTaskProgress(String taskId, TaskProgressOptionsObject options) throws APIException {
api.tasksSetProgress(taskId, options.create());
}
public void setTaskProgress(String taskId) throws APIException {
setTaskProgress(taskId, (Map<String, Object>) null);
}
}