Skip to content

Commit

Permalink
Merge pull request #71 from Asana/update-2018-07-23
Browse files Browse the repository at this point in the history
Support headers, clean up 402s, remove API key
  • Loading branch information
joetrollo authored Aug 13, 2018
2 parents dff1f08 + 96e2df3 commit f1e5b09
Show file tree
Hide file tree
Showing 17 changed files with 122 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ If you use [Maven](http://maven.apache.org/) to manage dependencies you can incl
<dependency>
<groupId>com.asana</groupId>
<artifactId>asana</artifactId>
<version>0.5.2</version>
<version>0.8.0</version>
</dependency>

Or, you can build the artifact and install it to your local Maven repository:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
import java.util.List;

/**
* API Key Instructions:
* PAT Instructions:
* <p>
* 1. set your ASANA_API_KEY environment variable to the API key found in Asana Account Settings
* 1. set your ASANA_ACCESS_TOKEN environment variable to a personal access token
*/
public class ExampleCreateProjectAndStreamEvents {

public static void main(String[] args) throws Exception {
if (System.getenv("ASANA_API_KEY") == null) {
throw new Error("Please set the ASANA_API_KEY environment variable.");
if (System.getenv("ASANA_ACCESS_TOKEN") == null) {
throw new Error("Please set the ASANA_ACCESS_TOKEN environment variable.");
}

// create a client with your Asana API key
Client client = Client.basicAuth(System.getenv("ASANA_API_KEY"));
// create a client with your Asana PAT
Client client = Client.accessToken(System.getenv("ASANA_ACCESS_TOKEN"));

// find your "Personal Projects" project
Workspace personalProjects = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
import java.util.List;

/**
* API Key Instructions:
* PAT Instructions:
* <p>
* 1. set your ASANA_API_KEY environment variable to the API key found in Asana Account Settings
* 1. set your ASANA_ACCESS_TOKEN environment variable to a personal access token
*/
public class ExampleCreateTaskAndUpload {

public static void main(String[] args) throws Exception {
if (System.getenv("ASANA_API_KEY") == null) {
throw new Error("Please set the ASANA_API_KEY environment variable.");
if (System.getenv("ASANA_ACCESS_TOKEN") == null) {
throw new Error("Please set the ASANA_ACCESS_TOKEN environment variable.");
}

// create a client with your Asana API key
Client client = Client.basicAuth(System.getenv("ASANA_API_KEY"));
// create a client with your Asana PAT
Client client = Client.accessToken(System.getenv("ASANA_ACCESS_TOKEN"));

// find your "Personal Projects" project
Workspace personalProjects = null;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import com.asana.models.User;

/**
* API Key Instructions:
* PAT Instructions:
* <p>
* 1. set your ASANA_ACCESS_TOKEN environment variable to a Personal Access Token found in Asana Account Settings
*/
public class ExamplePersonalAccessToken {

public static void main(String[] args) throws Exception {
if (System.getenv("ASANA_ACCESS_TOKEN") == null) {
throw new Error("Please set the ASANA_API_KEY environment variable.");
throw new Error("Please set the ASANA_ACCESS_TOKEN environment variable.");
}

System.out.println("== Example using a personal access token:");
Expand Down
File renamed without changes.
7 changes: 1 addition & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.asana</groupId>
<artifactId>asana</artifactId>
<packaging>jar</packaging>
<version>0.5.2</version>
<version>0.8.0</version>
<url>http://maven.apache.org</url>
<name>java-asana</name>
<description>A Java client for the Asana API.</description>
Expand Down Expand Up @@ -89,11 +89,6 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
31 changes: 28 additions & 3 deletions src/main/java/com/asana/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public class Client {
put("max_retries", 5);
}};

public HashMap<String, Object> options;
public Map<String, Object> options;
public Map<String, String> headers;

public Dispatcher dispatcher;

Expand Down Expand Up @@ -73,14 +74,23 @@ public class Client {
* @param dispatcher Dispatcher to handle authentication
*/
public Client(Dispatcher dispatcher) {
this(dispatcher, null);
this(dispatcher, null, null);
}

/**
* @param dispatcher Dispatcher to handle authentication
* @param options Map of client options, overrides Client.DEFAULTS, overridden by request options
*/
public Client(Dispatcher dispatcher, Map<String, Object> options) {
this(dispatcher, null, null);
}

/**
* @param dispatcher Dispatcher to handle authentication
* @param options Map of client options, overrides Client.DEFAULTS, overridden by request options
* @param headers Map of default headers to use for requests, overridden by request headers
*/
public Client(Dispatcher dispatcher, Map<String, Object> options, Map<String, String> headers) {
this.dispatcher = dispatcher;

this.options = new HashMap<String, Object>();
Expand All @@ -89,6 +99,11 @@ public Client(Dispatcher dispatcher, Map<String, Object> options) {
this.options.putAll(options);
}

this.headers = new HashMap<String, String>();
if (headers != null) {
this.headers.putAll(headers);
}

this.attachments = new Attachments(this);
this.customFields = new CustomFields(this);
this.customFieldSettings = new CustomFieldSettings(this);
Expand Down Expand Up @@ -153,6 +168,10 @@ public HttpResponse request(Request request) throws IOException {
url.put(entry.getKey(), value);
}

// Headers
Map <String, String> headers = new HashMap<String, String>(this.headers);
headers.putAll(request.headers);

if (request.content != null) {
// Multipart, etc body
content = request.content;
Expand All @@ -168,8 +187,8 @@ public HttpResponse request(Request request) throws IOException {
while (true) {
try {
HttpRequest httpRequest = this.dispatcher.buildRequest(request.method, url, content);

httpRequest.getHeaders().set(CLIENT_VERSION_HEADER_NAME, versionHeader());
httpRequest.getHeaders().putAll(headers);

try {
return httpRequest.execute();
Expand Down Expand Up @@ -229,16 +248,22 @@ private String versionHeader() {
}

/**
* WARNING: API Keys are deprecated and have been removed from Asana's API.
* Prefer using {@link #accessToken(String) accessToken method}.
* @param apiKey Basic Auth API key
* @deprecated
* @return Client instance
*/
public static Client basicAuth(String apiKey) {
return new Client(new BasicAuthDispatcher(apiKey));
}

/**
* WARNING: API Keys are deprecated and have been removed from Asana's API.
* Prefer using {@link #accessToken(String, HttpTransport) accessToken method}.
* @param apiKey Basic Auth API key
* @param httpTransport HttpTransport implementation to use for requests
* @deprecated
* @return Client instance
*/
public static Client basicAuth(String apiKey, HttpTransport httpTransport) {
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/asana/dispatcher/MockDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.*;

public class MockDispatcher extends Dispatcher {
public class Call {
public LowLevelHttpRequest request;
public LowLevelHttpResponse response;
public Map<String, List<String>> headers;
public String requestBody;
public JsonElement parsedRequestBody;

public Call(LowLevelHttpRequest request, LowLevelHttpResponse response, String requestBody) {
public Call(LowLevelHttpRequest request, LowLevelHttpResponse response, String requestBody, Map<String, List<String>> headers) {
this.request = request;
this.response = response;
this.requestBody = requestBody;
this.headers = headers;
try {
this.parsedRequestBody = new JsonParser().parse(requestBody);
} catch (Exception e) {
Expand Down Expand Up @@ -57,7 +56,7 @@ public LowLevelHttpResponse execute() throws IOException {
if (this.getStreamingContent() != null) {
this.getStreamingContent().writeTo(buffer);
}
calls.add(new Call(this, response, buffer.toString()));
calls.add(new Call(this, response, buffer.toString(), this.getHeaders()));

return response;
} else {
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/asana/errors/AsanaError.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ public AsanaError(String message, int status, HttpResponseException exception) {
public static AsanaError mapException(HttpResponseException exception) throws AsanaError {
switch (exception.getStatusCode()) {
case ForbiddenError.STATUS:
String constructedErrorMsg = constructMessage("", exception);
String premiumOnlyStr = "not available for free";
if (constructedErrorMsg.contains(premiumOnlyStr)) {
return new PremiumOnlyError(exception);
}
return new ForbiddenError(exception);
case InvalidRequestError.STATUS:
return new InvalidRequestError(exception);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/asana/models/Team.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.asana.models;

import com.google.gson.annotations.SerializedName;

public class Team extends Resource {
public String name;

public Workspace organization;

public String description;
@SerializedName("html_description")
public String htmlDescription;
}
4 changes: 4 additions & 0 deletions src/main/java/com/asana/requests/CollectionRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,8 @@ public CollectionRequest<T> data(String key, Object value) {
public CollectionRequest<T> option(String key, Object value) {
return (CollectionRequest<T>) super.option(key, value);
}

public CollectionRequest<T> header(String key, String value) {
return (CollectionRequest<T>) super.header(key, value);
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/asana/requests/EventsRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ public EventsRequest<T> data(String key, Object value) {
public EventsRequest<T> option(String key, Object value) {
return (EventsRequest<T>) super.option(key, value);
}

public EventsRequest<T> header(String key, String value) {
return (EventsRequest<T>) super.header(key, value);
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/asana/requests/ItemRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,8 @@ public ItemRequest<T> option(String key, Object value) {
return (ItemRequest<T>) super.option(key, value);
}

public ItemRequest<T> header(String key, String value) {
return (ItemRequest<T>) super.header(key, value);
}

}
14 changes: 14 additions & 0 deletions src/main/java/com/asana/requests/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public abstract class Request {
public Map<String, Object> data;
public Map<String, Object> query;
public Map<String, Object> options;
public Map<String, String> headers;
public HttpContent content;

protected Client client;
Expand All @@ -41,6 +42,7 @@ public Request(Resource resource, Class elementClass, String path, String method
this.data = new HashMap<String, Object>();
this.query = new HashMap<String, Object>();
this.options = new HashMap<String, Object>();
this.headers = new HashMap<String, String>();

this.options.putAll(this.client.options);
}
Expand Down Expand Up @@ -113,4 +115,16 @@ public Request option(String key, Object value) {
this.options.put(key, value);
return this;
}

/**
* Sets a header per-request
*
* @param key Header key
* @param value Header value
* @return The request itself
*/
public Request header(String key, String value) {
this.headers.put(key, value);
return this;
}
}
10 changes: 0 additions & 10 deletions src/test/java/com/asana/ErrorsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,4 @@ public void testPremiumOnlyError402() throws IOException

client.users.me().execute();
}

@Test(expected = PremiumOnlyError.class)
public void testPremiumOnlyError403() throws IOException
{
// Handles 403 errors with a premium message
dispatcher.registerResponse("GET", "http://app/users/me").code(403).content(
"{ \"errors\": [ { \"message\": \"Custom Fields are not available for free users or guests.\" } ] }");

client.users.me().execute();
}
}
41 changes: 41 additions & 0 deletions src/test/java/com/asana/HeadersTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.asana;

import org.junit.Test;

import java.io.IOException;
import java.util.Collections;

import static org.junit.Assert.assertEquals;

public class HeadersTest extends AsanaTest
{
@Test
public void testDefaultHeaders() throws IOException
{
dispatcher.registerResponse("GET", "http://app/users/me").code(200).content("{}");
client.headers.put("key", "value");
client.users.me().execute();
assertEquals(dispatcher.calls.get(0).headers.get("key"), Collections.singletonList("value"));
}

@Test
public void testRequestHeaders() throws IOException
{
dispatcher.registerResponse("GET", "http://app/users/me").code(200).content("{}");
client.users.me().header("key", "value").execute();

assertEquals(dispatcher.calls.get(0).headers.get("key"), Collections.singletonList("value"));
}

@Test
public void testOverridingHeaders() throws IOException
{
dispatcher.registerResponse("GET", "http://app/users/me").code(200).content("{}");
client.headers.put("key", "value");
client.headers.put("key2", "value2");
client.users.me().header("key2", "value3").execute();

assertEquals(dispatcher.calls.get(0).headers.get("key"), Collections.singletonList("value"));
assertEquals(dispatcher.calls.get(0).headers.get("key2"), Collections.singletonList("value3"));
}
}

0 comments on commit f1e5b09

Please sign in to comment.