Skip to content

Commit 35176eb

Browse files
author
Joe Linn
committed
Initial commit
1 parent 4b1cfa0 commit 35176eb

42 files changed

Lines changed: 1523 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.iml
2+
/.idea
3+
4+
# build files
5+
/data
6+
/target

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,12 @@ asana-api-java
22
==============
33

44
A Java client library for Asana's API
5+
6+
Usage
7+
-----
8+
Creating a task:
9+
```java
10+
Asana asana = new Asana("your api key");
11+
asana.tasks().createTask(new TaskRequestBuilder(4440299545542L, "New task!").addFollower(4858211767376L)
12+
.addFollower(4440682739786L).notes("This is super important.").assignee(4440682739795L));
13+
```

pom.xml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>net.joelinn</groupId>
8+
<artifactId>asana</artifactId>
9+
<version>0.5.0</version>
10+
<packaging>jar</packaging>
11+
<description>A Java client library for Asana's API</description>
12+
<licenses>
13+
<license>
14+
<name>The Apache Software License, Version 2.0</name>
15+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
16+
<distribution>repo</distribution>
17+
</license>
18+
</licenses>
19+
20+
<scm>
21+
<connection>scm:git:git@github.com:jlinn/asana-api-java.git</connection>
22+
<developerConnection>scm:git:git@github.com:jlinn/asana-api-java.git</developerConnection>
23+
<url>http://github.com/jlinn/asana-api-java</url>
24+
</scm>
25+
26+
<properties>
27+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
28+
</properties>
29+
30+
<repositories>
31+
<repository>
32+
<id>sonatype</id>
33+
<url>http://oss.sonatype.org/content/repositories/releases/</url>
34+
</repository>
35+
</repositories>
36+
37+
<dependencies>
38+
<dependency>
39+
<groupId>com.google.guava</groupId>
40+
<artifactId>guava</artifactId>
41+
<version>15.0</version>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.codehaus.jackson</groupId>
46+
<artifactId>jackson-jaxrs</artifactId>
47+
<version>1.9.13</version>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>com.sun.jersey</groupId>
52+
<artifactId>jersey-client</artifactId>
53+
<version>1.17.1</version>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>junit</groupId>
58+
<artifactId>junit</artifactId>
59+
<version>4.11</version>
60+
<scope>test</scope>
61+
</dependency>
62+
63+
</dependencies>
64+
65+
66+
<build>
67+
<plugins>
68+
<plugin>
69+
<groupId>org.apache.maven.plugins</groupId>
70+
<artifactId>maven-dependency-plugin</artifactId>
71+
<version>2.8</version>
72+
</plugin>
73+
74+
<plugin>
75+
<groupId>org.apache.maven.plugins</groupId>
76+
<artifactId>maven-source-plugin</artifactId>
77+
<version>2.2.1</version>
78+
<executions>
79+
<execution>
80+
<id>attach-sources</id>
81+
<goals>
82+
<goal>jar</goal>
83+
</goals>
84+
</execution>
85+
</executions>
86+
</plugin>
87+
</plugins>
88+
89+
<pluginManagement>
90+
<plugins>
91+
<plugin>
92+
<groupId>org.apache.maven.plugins</groupId>
93+
<artifactId>maven-compiler-plugin</artifactId>
94+
<configuration>
95+
<source>1.7</source>
96+
<target>1.7</target>
97+
</configuration>
98+
</plugin>
99+
</plugins>
100+
</pluginManagement>
101+
</build>
102+
103+
</project>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package net.joelinn.asana;
2+
3+
import com.sun.jersey.api.client.Client;
4+
import com.sun.jersey.api.client.ClientResponse;
5+
import com.sun.jersey.api.client.WebResource;
6+
import com.sun.jersey.api.client.config.ClientConfig;
7+
import com.sun.jersey.api.client.config.DefaultClientConfig;
8+
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
9+
import com.sun.jersey.core.util.MultivaluedMapImpl;
10+
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
11+
import org.codehaus.jackson.map.DeserializationConfig;
12+
import org.codehaus.jackson.map.ObjectMapper;
13+
14+
import javax.ws.rs.core.MediaType;
15+
import javax.ws.rs.core.UriBuilder;
16+
17+
/**
18+
* Joe Linn
19+
* 11/16/13
20+
*/
21+
public abstract class AbstractClient {
22+
public static final String BASE_URL = "https://app.asana.com/api/1.0/";
23+
24+
protected String apiKey;
25+
26+
protected WebResource service;
27+
28+
public AbstractClient(String apiKey){
29+
this.apiKey = apiKey;
30+
31+
ClientConfig config = new DefaultClientConfig();
32+
ObjectMapper mapper = new ObjectMapper();
33+
mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
34+
JacksonJsonProvider provider = new JacksonJsonProvider(mapper);
35+
config.getSingletons().add(provider);
36+
//config.getClasses().add(JacksonJsonProvider.class);
37+
Client client = Client.create(config);
38+
client.addFilter(new HTTPBasicAuthFilter(apiKey, ""));
39+
service = client.resource(UriBuilder.fromUri(BASE_URL).build());
40+
}
41+
42+
protected ClientResponse get(String url){
43+
return get(url, null);
44+
}
45+
46+
protected ClientResponse get(String url, MultivaluedMapImpl queryParams){
47+
return request("GET", url, queryParams);
48+
}
49+
50+
protected ClientResponse post(String url, MultivaluedMapImpl data){
51+
return request("POST", url, null, data);
52+
}
53+
54+
protected ClientResponse put(String url, MultivaluedMapImpl data){
55+
return request("PUT", url, null, data);
56+
}
57+
58+
protected ClientResponse delete(String url){
59+
return request("DELETE", url);
60+
}
61+
62+
protected ClientResponse request(String method, String url){
63+
return request(method, url, null);
64+
}
65+
66+
protected ClientResponse request(String method, String url, MultivaluedMapImpl queryParams){
67+
return request(method, url, queryParams, null);
68+
}
69+
70+
protected ClientResponse request(String method, String url, MultivaluedMapImpl queryParams, MultivaluedMapImpl data){
71+
ClientResponse clientResponse = getResourceBuilder(url, queryParams).method(method, ClientResponse.class, data);
72+
checkForErrors(clientResponse);
73+
return clientResponse;
74+
}
75+
76+
private void checkForErrors(ClientResponse clientResponse){
77+
if(clientResponse.getClientResponseStatus().getStatusCode() != ClientResponse.Status.OK.getStatusCode()
78+
&& clientResponse.getClientResponseStatus().getStatusCode() != ClientResponse.Status.CREATED.getStatusCode()){
79+
throw new ApiException(clientResponse.getClientResponseStatus(), clientResponse.getEntity(Errors.class).get(0).message);
80+
}
81+
}
82+
83+
private WebResource.Builder getResourceBuilder(String url, MultivaluedMapImpl queryParams){
84+
WebResource webResource = service.path(url);
85+
if(queryParams != null){
86+
webResource.queryParams(queryParams);
87+
}
88+
return webResource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_FORM_URLENCODED);
89+
}
90+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package net.joelinn.asana;
2+
3+
import com.sun.jersey.core.util.MultivaluedMapImpl;
4+
5+
/**
6+
* Joe Linn
7+
* 11/20/13
8+
*/
9+
public abstract class AbstractRequestBuilder {
10+
protected MultivaluedMapImpl params = new MultivaluedMapImpl();
11+
12+
public AbstractRequestBuilder setParam(String name, Object value){
13+
params.add(name, value);
14+
return this;
15+
}
16+
17+
public MultivaluedMapImpl build(){
18+
return params;
19+
}
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package net.joelinn.asana;
2+
3+
import com.sun.jersey.api.client.ClientResponse;
4+
5+
/**
6+
* Joe Linn
7+
* 11/17/13
8+
*/
9+
public class ApiException extends RuntimeException{
10+
protected ClientResponse.Status status;
11+
protected String message;
12+
13+
public ApiException(ClientResponse.Status status, String message){
14+
super(message);
15+
this.status = status;
16+
this.message = message;
17+
}
18+
19+
public ClientResponse.Status getStatus(){
20+
return status;
21+
}
22+
23+
@Override
24+
public String toString() {
25+
String s = getClass().getName();
26+
String message = getLocalizedMessage();
27+
return String.format("%s: %s %s", s, status.getStatusCode(), message);
28+
}
29+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package net.joelinn.asana;
2+
3+
import net.joelinn.asana.projects.ProjectsClient;
4+
import net.joelinn.asana.stories.StoriesClient;
5+
import net.joelinn.asana.tags.TagsClient;
6+
import net.joelinn.asana.tasks.TasksClient;
7+
import net.joelinn.asana.teams.TeamsClient;
8+
import net.joelinn.asana.users.UsersClient;
9+
import net.joelinn.asana.workspaces.WorkspacesClient;
10+
11+
/**
12+
* Joe Linn
13+
* 11/20/13
14+
*/
15+
public class Asana {
16+
protected String apiKey;
17+
18+
protected ProjectsClient projectsClient;
19+
20+
protected StoriesClient storiesClient;
21+
22+
protected TagsClient tagsClient;
23+
24+
protected TasksClient tasksClient;
25+
26+
protected TeamsClient teamsClient;
27+
28+
protected UsersClient usersClient;
29+
30+
protected WorkspacesClient workspacesClient;
31+
32+
public Asana(String apiKey){
33+
this.apiKey = apiKey;
34+
}
35+
36+
public ProjectsClient projects(){
37+
if(projectsClient == null){
38+
projectsClient = new ProjectsClient(apiKey);
39+
}
40+
return projectsClient;
41+
}
42+
43+
public StoriesClient stories(){
44+
if(storiesClient == null){
45+
storiesClient = new StoriesClient(apiKey);
46+
}
47+
return storiesClient;
48+
}
49+
50+
public TagsClient tags(){
51+
if(tagsClient == null){
52+
tagsClient = new TagsClient(apiKey);
53+
}
54+
return tagsClient;
55+
}
56+
57+
public TasksClient tasks(){
58+
if(tasksClient == null){
59+
tasksClient = new TasksClient(apiKey);
60+
}
61+
return tasksClient;
62+
}
63+
64+
public TeamsClient teams(){
65+
if(teamsClient == null){
66+
teamsClient = new TeamsClient(apiKey);
67+
}
68+
return teamsClient;
69+
}
70+
71+
public UsersClient users(){
72+
if(usersClient == null){
73+
usersClient = new UsersClient(apiKey);
74+
}
75+
return usersClient;
76+
}
77+
78+
public WorkspacesClient workspaces(){
79+
if(workspacesClient == null){
80+
workspacesClient = new WorkspacesClient(apiKey);
81+
}
82+
return workspacesClient;
83+
}
84+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package net.joelinn.asana;
2+
3+
/**
4+
* Joe Linn
5+
* 11/20/13
6+
*/
7+
public class Colors {
8+
public static final String DARK_PINK = "dark-pink";
9+
public static final String DARK_GREEN = "dark-green";
10+
public static final String DARK_BLUE = "dark-blue";
11+
public static final String DARK_RED = "dark-red";
12+
public static final String DARK_TEAL = "dark-teal";
13+
public static final String DARK_BROWN = "dark-brown";
14+
public static final String DARK_ORANGE = "dark-orange";
15+
public static final String DARK_PURPLE = "dark-purple";
16+
public static final String DARK_WARM_GRAY = "dark-warm-gray";
17+
public static final String LIGHT_PINK = "light-pink";
18+
public static final String LIGHT_GREEN = "light-green";
19+
public static final String LIGHT_BLUE = "light-blue";
20+
public static final String LIGHT_RED = "light-red";
21+
public static final String LIGHT_TEAL = "light-teal";
22+
public static final String LIGHT_YELLOW = "light-yellow";
23+
public static final String LIGHT_ORANGE = "light-orange";
24+
public static final String LIGHT_PURPLE = "light-purple";
25+
public static final String LIGHT_WARM_GRAY = "light-warm-gray";
26+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package net.joelinn.asana;
2+
3+
/**
4+
* Joe Linn
5+
* 11/17/13
6+
*/
7+
public class Error {
8+
public String message;
9+
}

0 commit comments

Comments
 (0)