Skip to content

Instantly share code, notes, and snippets.

@erikfried
Created April 9, 2013 12:43
Show Gist options
  • Save erikfried/5345407 to your computer and use it in GitHub Desktop.
Save erikfried/5345407 to your computer and use it in GitHub Desktop.
Test/spike implementation of api client for spid with Jersey.
package no.spp.sdk.client;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandler;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.LoggingFilter;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.client.urlconnection.URLConnectionClientHandler;
import no.spp.sdk.client.config.IgnoreUnknownPropsJacksonProvider;
import no.spp.sdk.exception.SPPClientException;
import no.spp.sdk.exception.SPPClientResponseException;
import java.util.HashMap;
import java.util.Map;
public class SppJerseyApi {
public static final String OAUTH_TOKEN_PARAM_NAME = "oauth_token";
private String apiVersion = "2";
private String apiBaseUrl;
public SppJerseyApi(String apiVersion, String baseUrl) {
this.apiVersion = apiVersion;
this.apiBaseUrl = ensureTrailingSlash(baseUrl);// TODO + "api/" + apiVersion;
}
public WebResource getResource(String baseUrl){
Client jerseyClient = getConfiguredClient((new URLConnectionClientHandler()));
return jerseyClient.resource(baseUrl);
}
private Client getConfiguredClient(ClientHandler clientHandler) {
Client client = new Client(clientHandler, new SppApiClientConfig());
client.addFilter(new LoggingFilter());
client.setReadTimeout(30000);
client.setConnectTimeout(5000);
client.setFollowRedirects(true);
return client;
}
public <T> T GET(String endPoint , String oauthToken, Class<T> clazz) throws SPPClientException, SPPClientResponseException {
T response;
Map<String, String> parameters = new HashMap<String, String>(); //Create new map in case provided map is immutable
parameters.put(OAUTH_TOKEN_PARAM_NAME, oauthToken);
long startTime = System.currentTimeMillis();
try {
String url = this.apiBaseUrl + endPoint;
response = getResource(url).get(clazz);
} catch (UniformInterfaceException e) {
throw new SPPClientException(e);
}
long endTime = System.currentTimeMillis();
//return new SPPClientResponse(response.getResponseCode(), response.getResponseBody(), "2", endTime - startTime);
//We should possibly return this response in some generic container e g SppJerseyClientResponse
return response;
}
private String ensureTrailingSlash(String in) {
return in + (in.endsWith("/") ? "" : "/" );
}
public static class SppApiClientConfig extends DefaultClientConfig {
public SppApiClientConfig(){
getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); //Use Jackson POJO mapping
getClasses().add(IgnoreUnknownPropsJacksonProvider.class); //To not throw exceptions if not all properties in json are present i POJO
}
}
}
package no.spp.sdk.client;
import no.spp.sdk.client.response.SPPResponse;
import no.spp.sdk.exception.SPPClientException;
import no.spp.sdk.exception.SPPClientRefreshTokenException;
import no.spp.sdk.exception.SPPClientResponseException;
import org.junit.Test;
public class JerseyTest {
@Test
public void test() throws SPPClientResponseException, SPPClientException, SPPClientRefreshTokenException {
SppJerseyApi api = new SppJerseyApi("2", "http://localhost:8000");
SPPResponse res = api.GET("campaign_10.json", "token", SPPResponse.class);
System.out.println(res);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment