Permalink
Browse files

Basic Client with full code functionality.

  • Loading branch information...
Andrew Kirilenko authored and richard-julien committed Mar 22, 2012
1 parent a880d55 commit 1c7f345caaf33ea59a5ca70b800bf8ecbcee17f8
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>
@@ -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);
+ }
+}
@@ -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;
+ }
+}
@@ -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");
+ }
+}
@@ -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() {
+ }
+}
@@ -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;
+ }
+}
@@ -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;
+ }
+}

0 comments on commit 1c7f345

Please sign in to comment.