|
| 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 | +} |
0 commit comments