Navigation Menu

Skip to content

Commit

Permalink
BaseService refactor, HttpRequestInitializer was moved to a private c…
Browse files Browse the repository at this point in the history
…lass
  • Loading branch information
guillermo-delucio committed Feb 7, 2014
1 parent b59455f commit d8e7a71
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
Expand Up @@ -69,6 +69,14 @@ public void createToken() throws InterruptedException {
this.signal.await();
}

@Test
public void createToken_ExpirationYearError() throws InterruptedException {
Card card = this.getCard();
card.expirationYear(12);
this.openpay.createToken(card, new LocalOperationCallBack());
this.signal.await();
}

private Address getAddres() {
return new Address().city("Mexico").countryCode("MX").line1("Calle de la felicidad").line2("Numero 20")
.line3("Col ensueño").postalCode("76900").state("Mexico");
Expand Down
50 changes: 32 additions & 18 deletions openpay-android/src/mx/openpay/android/services/BaseService.java
Expand Up @@ -40,11 +40,14 @@
*
*/
public abstract class BaseService<V, T> {
public final static JsonFactory JSON_FACTORY = new JacksonFactory();
private final static JsonFactory JSON_FACTORY = new JacksonFactory();
private final static HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport();
private final static String EMPTY_PASSWORD = "";
private final static String API_VERSION = "v1";
private final static String URL_SEPARATOR = "/";
private static final String AGENT = "openpay-android/";

private static final int DEFAULT_CONNECTION_TIMEOUT = 60000;

protected String baseUrl;
protected String merchantId;
Expand All @@ -60,15 +63,7 @@ public BaseService(final String baseUrl, final String merchantId, final String a
}

public HttpRequestFactory getRequestFactory() {
return HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
@Override
public void initialize(final HttpRequest request) {
request.setParser(new JsonObjectParser(JSON_FACTORY));
request.getHeaders().setBasicAuthentication(BaseService.this.apiKey, EMPTY_PASSWORD);
request.getHeaders().setUserAgent("OpenPay-");
request.setUnsuccessfulResponseHandler(new ServiceUnsuccessfulResponseHandler());
}
});
return HTTP_TRANSPORT.createRequestFactory(new OpenpayHttpRequestInitializer());
}

public GenericUrl getGenericUrl(final String resourceUrl) {
Expand Down Expand Up @@ -97,15 +92,34 @@ public T post(final String resourceUrl, final V data) throws OpenpayServiceExcep
}
}

}
private class OpenpayHttpRequestInitializer implements HttpRequestInitializer {
@Override
public void initialize(final HttpRequest request) {
request.setParser(new JsonObjectParser(JSON_FACTORY));
request.getHeaders().setBasicAuthentication(BaseService.this.apiKey, EMPTY_PASSWORD);
String version = this.getClass().getPackage().getImplementationVersion();
if (version == null) {
version = "1.0.1-UNKNOWN";
}
request.setSuppressUserAgentSuffix(true);
request.getHeaders().setUserAgent(AGENT + version);
request.setUnsuccessfulResponseHandler(new ServiceUnsuccessfulResponseHandler());
request.setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT);
request.setReadTimeout(DEFAULT_CONNECTION_TIMEOUT);
}

class ServiceUnsuccessfulResponseHandler implements HttpUnsuccessfulResponseHandler {
@Override
public boolean handleResponse(final HttpRequest request, final HttpResponse response, final boolean arg2)
throws IOException {
OpenpayServiceException exception = new JsonObjectParser(BaseService.JSON_FACTORY).parseAndClose(
response.getContent(), response.getContentCharset(), OpenpayServiceException.class);
throw exception;
private class ServiceUnsuccessfulResponseHandler implements HttpUnsuccessfulResponseHandler {
@Override
public boolean handleResponse(final HttpRequest request, final HttpResponse response, final boolean arg2)
throws IOException {
OpenpayServiceException exception = new JsonObjectParser(BaseService.JSON_FACTORY).parseAndClose(
response.getContent(), response.getContentCharset(), OpenpayServiceException.class);
throw exception;
}

}
}

}


0 comments on commit d8e7a71

Please sign in to comment.