com.payu.sdk.api.RawApi Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of api-client Show documentation
Show all versions of api-client Show documentation
A fresh implementation of the PayU API Client for Android
The newest version!
package com.payu.sdk.api;
import com.payu.sdk.api.handler.ApiErrorHandler;
import com.payu.sdk.api.http.ApiAuthenticator;
import com.payu.sdk.api.http.OkHttpClientHelper;
import com.payu.sdk.api.http.interceptor.HeaderInterceptor;
import com.payu.sdk.api.http.interceptor.LoggingInterceptor;
import com.payu.sdk.api.log.ApiLog;
import com.payu.sdk.api.log.Logger;
import com.payu.sdk.api.services.MobileServices;
import com.payu.sdk.api.services.PaymentServices;
import com.payu.sdk.api.services.ReportServices;
import com.squareup.okhttp.OkHttpClient;
import retrofit.Endpoint;
import retrofit.Endpoints;
import retrofit.RestAdapter;
import retrofit.client.OkClient;
import static com.payu.sdk.base.Preconditions.checkArg;
import static com.payu.sdk.base.Preconditions.checkStringArg;
import static com.payu.sdk.utils.TextUtils.isNullOrEmpty;
final class RawApi {
private static final String CUSTOM_NAME_PAYMENTS = "custom_payments";
private static final String CUSTOM_NAME_REPORTS = "custom_reports";
private static volatile OkClient okClient = null;
private final ApiErrorHandler apiErrorHandler = new ApiErrorHandler();
private final ApiLog log = Logger.getLog();
private final HeaderInterceptor headerInterceptor = new HeaderInterceptor();
private RestAdapter restAdapter;
private Endpoint paymentsEndpoint =
Endpoints.newFixedEndpoint(ApiEndpoints.PAYMENTS_PRODUCTION.url,
ApiEndpoints.PAYMENTS_PRODUCTION.name);
private Endpoint reportsEndPoint = Endpoints.newFixedEndpoint(ApiEndpoints.REPORTS_PRODUCTION.url,
ApiEndpoints.REPORTS_PRODUCTION.name);
private Endpoint actualEndpoint;
private boolean enableLog;
private String apiKey;
private String apiLogin;
private String fingerprint;
RawApi(String apiLogin, String apiKey) {
setApiLogin(apiLogin);
setApiKey(apiKey);
}
private static void validateUrl(String url, String urlToValidate) {
checkStringArg(url, String.format("The url %s be not null", urlToValidate));
}
private static void validateEndpoint(Endpoint endpoint, String endPointToValidate) {
checkArg(endpoint, String.format("The endpoint %s be not null", endPointToValidate));
}
private static void validateEndpointName(Endpoint endpointA, Endpoint endpointB) {
if (endpointA.getName().contentEquals(endpointB.getName()) || endpointA.getUrl()
.contentEquals(endpointB.getUrl())) {
throw new IllegalArgumentException("Endpoint name not be equals");
}
}
private OkClient buildOkClient() {
if (okClient == null) {
synchronized (RawApi.class) {
if (okClient == null) {
OkHttpClient okHttpClient = OkHttpClientHelper.getCustomOkHttpClient();
if (hasCredentials()) {
okHttpClient.setAuthenticator(new ApiAuthenticator(apiLogin, apiKey, fingerprint));
}
if (isEnableLog()) {
okHttpClient.networkInterceptors().add(new LoggingInterceptor());
}
okClient = new OkClient(okHttpClient);
}
}
}
return okClient;
}
public RawApi isDebug(boolean isDebug) {
this.enableLog = isDebug;
this.restAdapter = null;
okClient = null;
return this;
}
public RawApi setUrls(String paymentsUrl, String reportsUrl) {
validateUrl(paymentsUrl, CUSTOM_NAME_PAYMENTS);
validateUrl(reportsUrl, CUSTOM_NAME_REPORTS);
this.paymentsEndpoint = Endpoints.newFixedEndpoint(paymentsUrl, CUSTOM_NAME_PAYMENTS);
this.reportsEndPoint = Endpoints.newFixedEndpoint(reportsUrl, CUSTOM_NAME_REPORTS);
this.restAdapter = null;
okClient = null;
return this;
}
public RawApi setEndpoints(Endpoint paymentsEndpoint, Endpoint reportsEndPoint) {
validateEndpoint(paymentsEndpoint, CUSTOM_NAME_PAYMENTS);
validateEndpoint(reportsEndPoint, CUSTOM_NAME_REPORTS);
validateEndpointName(paymentsEndpoint, reportsEndPoint);
this.paymentsEndpoint = paymentsEndpoint;
this.reportsEndPoint = reportsEndPoint;
this.restAdapter = null;
okClient = null;
return this;
}
public RawApi setReportsEndpoint(Endpoint reportsEndPoint) {
validateEndpoint(reportsEndPoint, CUSTOM_NAME_REPORTS);
validateEndpointName(reportsEndPoint, this.paymentsEndpoint);
this.reportsEndPoint = reportsEndPoint;
this.restAdapter = null;
okClient = null;
return this;
}
public Endpoint getPaymentsEndpoint() {
return this.paymentsEndpoint;
}
public RawApi setPaymentsEndpoint(Endpoint paymentsEndpoint) {
validateEndpoint(paymentsEndpoint, CUSTOM_NAME_PAYMENTS);
validateEndpointName(paymentsEndpoint, this.reportsEndPoint);
this.paymentsEndpoint = paymentsEndpoint;
this.restAdapter = null;
okClient = null;
return this;
}
public Endpoint getReportsEndPoint() {
return this.reportsEndPoint;
}
private RestAdapter buildRestAdapter(Endpoint endpoint) {
if ((restAdapter == null) || (actualEndpoint != endpoint)) {
actualEndpoint = endpoint;
restAdapter = new RestAdapter.Builder().setRequestInterceptor(headerInterceptor)
.setEndpoint(endpoint)
.setLogLevel(logLevel())
.setErrorHandler(apiErrorHandler)
.setClient(buildOkClient())
.setLog(new RestAdapter.Log() {
@Override public void log(String message) {
if (isEnableLog()) {
log.log(message);
}
}
})
.build();
}
return restAdapter;
}
public boolean hasCredentials() {
return !isNullOrEmpty(apiLogin) && !isNullOrEmpty(apiKey) && !isNullOrEmpty(fingerprint);
}
public RestAdapter.LogLevel logLevel() {
return !enableLog ? RestAdapter.LogLevel.NONE : RestAdapter.LogLevel.FULL;
}
public String getPaymentsUrl() {
return this.paymentsEndpoint.getUrl();
}
public RawApi setPaymentsUrl(String paymentsUrl) {
validateUrl(paymentsUrl, CUSTOM_NAME_PAYMENTS);
this.paymentsEndpoint = Endpoints.newFixedEndpoint(paymentsUrl, CUSTOM_NAME_PAYMENTS);
this.restAdapter = null;
return this;
}
public String getReportsUrl() {
return this.reportsEndPoint.getUrl();
}
public RawApi setReportsUrl(String reportsUrl) {
validateUrl(reportsUrl, CUSTOM_NAME_REPORTS);
this.reportsEndPoint = Endpoints.newFixedEndpoint(reportsUrl, CUSTOM_NAME_REPORTS);
this.restAdapter = null;
okClient = null;
return this;
}
public boolean isEnableLog() {
return this.enableLog;
}
private RawApi setApiKey(String apiKey) {
checkStringArg(apiKey, "apiKey can not be null");
this.apiKey = apiKey;
this.restAdapter = null;
okClient = null;
return this;
}
private RawApi setApiLogin(String apiLogin) {
checkStringArg(apiLogin, "apiLogin can not be null");
this.apiLogin = apiLogin;
this.restAdapter = null;
okClient = null;
return this;
}
public RawApi setFingerprint(String fingerprint) {
checkStringArg(fingerprint, "fingerprint can not be null");
this.fingerprint = fingerprint;
this.restAdapter = null;
okClient = null;
return this;
}
public PaymentServices paymentServices() {
return buildRestAdapter(paymentsEndpoint).create(PaymentServices.class);
}
public ReportServices reportServices() {
return buildRestAdapter(reportsEndPoint).create(ReportServices.class);
}
public MobileServices mobileServices() {
return buildRestAdapter(paymentsEndpoint).create(MobileServices.class);
}
}