Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 3 commits
  • 9 files changed
  • 0 commit comments
  • 1 contributor
2  .gitignore
View
@@ -0,0 +1,2 @@
+.idea
+*.iml
5 client/pom.xml
View
@@ -29,6 +29,11 @@
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
+ <dependency>
+ <groupId>joda-time</groupId>
+ <artifactId>joda-time</artifactId>
+ <version>2.1</version>
+ </dependency>
</dependencies>
</project>
165 client/src/main/java/io/iron/ironworker/client/APIClient.java
View
@@ -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));
}
}
118 client/src/main/java/io/iron/ironworker/client/Client.java
View
@@ -0,0 +1,118 @@
+package io.iron.ironworker.client;
+
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import io.iron.ironworker.client.codes.BaseCode;
+import io.iron.ironworker.client.entities.CodeEntity;
+import io.iron.ironworker.client.entities.CodeRevisionEntity;
+
+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;
+ }
+
+ private List<CodeEntity> getCodes(Map<String, Object> params) throws APIException {
+ JsonObject codes = api.codesList(params);
+
+ 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() throws APIException {
+ 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));
+ }
+
+ 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);
+ }
+
+ private List<CodeRevisionEntity> getCodeRevisions(String codeId, Map<String, Object> params) throws APIException {
+ JsonObject codeRevisions = api.codesRevisions(codeId, params);
+
+ 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) throws APIException {
+ return getCodeRevisions(codeId, null);
+ }
+
+ public List<CodeRevisionEntity> getCodeRevisions(String codeId, int page) throws APIException {
+ Map<String, Object> params = new HashMap<String, Object>();
+
+ params.put("page", page);
+
+ return getCodeRevisions(codeId, params);
+ }
+
+ public List<CodeRevisionEntity> getCodeRevisions(String codeId, int page, int perPage) throws APIException {
+ Map<String, Object> params = new HashMap<String, Object>();
+
+ params.put("page", page);
+ params.put("per_page", perPage);
+
+ return getCodeRevisions(codeId, params);
+ }
+
+ private byte[] downloadCode(String codeId, Map<String, Object> params) throws APIException {
+ return api.codesDownload(codeId, params);
+ }
+
+ public byte[] downloadCode(String codeId) throws APIException {
+ return downloadCode(codeId, null);
+ }
+
+ public byte[] downloadCode(String codeId, int revision) throws APIException {
+ Map<String, Object> params = new HashMap<String, Object>();
+
+ params.put("revision", revision);
+
+ return downloadCode(codeId, params);
+ }
+}
31 client/src/main/java/io/iron/ironworker/client/codes/BaseCode.java
View
@@ -0,0 +1,31 @@
+package io.iron.ironworker.client.codes;
+
+public class BaseCode {
+ private String name;
+ private String file;
+ private String runtime;
+ private String runner;
+
+ public BaseCode(String name, String file, String runtime, String runner) {
+ this.name = name;
+ this.file = file;
+ this.runtime = runtime;
+ this.runner = runner;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getFile() {
+ return file;
+ }
+
+ public String getRuntime() {
+ return runtime;
+ }
+
+ public String getRunner() {
+ return runner;
+ }
+}
7 client/src/main/java/io/iron/ironworker/client/codes/JavaCode.java
View
@@ -0,0 +1,7 @@
+package io.iron.ironworker.client.codes;
+
+public class JavaCode extends BaseCode {
+ public JavaCode(String name, String file) {
+ super(name, file, "ruby", "runner.rb");
+ }
+}
14 client/src/main/java/io/iron/ironworker/client/entities/BaseEntity.java
View
@@ -0,0 +1,14 @@
+package io.iron.ironworker.client.entities;
+
+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();
+ }
+
+ protected BaseEntity() {
+ }
+}
78 client/src/main/java/io/iron/ironworker/client/entities/CodeEntity.java
View
@@ -0,0 +1,78 @@
+package io.iron.ironworker.client.entities;
+
+import com.google.gson.JsonObject;
+
+import java.util.Date;
+
+public class CodeEntity extends BaseEntity {
+ String id;
+ String projectId;
+ String name;
+ String runtime;
+ int revision;
+ String latestHistoryId;
+ String latestChecksum;
+ Date latestChange;
+ Date createdAt;
+ Date updatedAt;
+
+ 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());
+
+ return c;
+ }
+
+ protected CodeEntity() {
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public String getProjectId() {
+ return projectId;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getRuntime() {
+ return runtime;
+ }
+
+ public int getRevision() {
+ return revision;
+ }
+
+ public String getLatestHistoryId() {
+ return latestHistoryId;
+ }
+
+ public String getLatestChecksum() {
+ return latestChecksum;
+ }
+
+ public Date getLatestChange() {
+ return latestChange;
+ }
+
+ public Date getCreatedAt() {
+ return createdAt;
+ }
+
+ public Date getUpdatedAt() {
+ return updatedAt;
+ }
+}
72 client/src/main/java/io/iron/ironworker/client/entities/CodeRevisionEntity.java
View
@@ -0,0 +1,72 @@
+package io.iron.ironworker.client.entities;
+
+import com.google.gson.JsonObject;
+
+import java.util.Date;
+
+public class CodeRevisionEntity extends BaseEntity {
+ String id;
+ String codeId;
+ String projectId;
+ String name;
+ String runtime;
+ String fileName;
+ 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;
+ }
+
+ protected CodeRevisionEntity() {
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public String getCodeId() {
+ return codeId;
+ }
+
+ public String getProjectId() {
+ return projectId;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getRuntime() {
+ return runtime;
+ }
+
+ public String getFileName() {
+ return fileName;
+ }
+
+ public int getRevision() {
+ return revision;
+ }
+
+ public Date getCreatedAt() {
+ return createdAt;
+ }
+
+ public Date getUpdatedAt() {
+ return updatedAt;
+ }
+}

No commit comments for this range

Something went wrong with that request. Please try again.