id.unum.facade.rest.Client Maven / Gradle / Ivy
The newest version!
package id.unum.facade.rest;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import id.unum.facade.rest.serialization.*;
import id.unum.protos.credential.v1.CredentialStatusInfo;
import id.unum.protos.credential.v1.IssueCredentialRequest;
import id.unum.protos.crypto.v1.PublicKeyInfo;
import id.unum.protos.didDocument.v1.DidDocument;
import id.unum.protos.externalMessage.v1.ExternalMessage;
import id.unum.protos.issuer.v1.Issuer;
import id.unum.protos.presentationRequest.v1.PresentationRequest;
import id.unum.protos.presentationRequestEnriched.v1.PresentationRequestEnriched;
import id.unum.protos.presentationRequestEnriched.v1.PresentationRequestRepoDto;
import id.unum.protos.receipt.v1.PresentationVerifiedReceiptOptions;
import id.unum.protos.verifier.v1.Verifier;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import java.io.IOException;
// TODO make singleton
public class Client {
private Retrofit retrofit;
public Client(String baseUrl) {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder().addHeader("version", "3.0.0").build();
return chain.proceed(request);
}
});
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
// set your desired log level
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
// add logging as last interceptor
httpClient.addInterceptor(logging); // <-- this is the important line!
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(buildGsonConverter())
// .addConverterFactory(GsonConverterFactory.create())
// .addConverterFactory(ProtoConverterFactory.create())
// .addConverterFactory(JacksonConverterFactory.create())
.client(httpClient.build())
.build();
}
public Retrofit getRetrofit() {
return retrofit;
}
private static GsonConverterFactory buildGsonConverter() {
GsonBuilder gsonBuilder = new GsonBuilder();
// Adding custom deserializers
gsonBuilder.registerTypeAdapter(PresentationRequestEnriched.class,
new PresentationRequestEnrichedDeserializer());
gsonBuilder.registerTypeAdapter(DidDocument.class,
new DidDocumentDeserializer());
gsonBuilder.registerTypeAdapter(PublicKeyInfo.class,
new PublicKeyInfoDeserializer());
gsonBuilder.registerTypeAdapter(Issuer.class,
new IssuerDeserializer());
gsonBuilder.registerTypeAdapter(Verifier.class,
new VerifierDeserializer());
gsonBuilder.registerTypeAdapter(CredentialStatusInfo.class,
new CredentialStatusInfoDeserializer());
gsonBuilder.registerTypeAdapter(PresentationRequestRepoDto.class,
new PresentationRequestRepoDtoDeserializer());
// Adding custom serializers
gsonBuilder.registerTypeAdapter(ExternalMessage.class,
new ExternalMessageSerializer());
gsonBuilder.registerTypeAdapter(PresentationRequest.class,
new PresentationRequestSerializer());
gsonBuilder.registerTypeAdapter(IssueCredentialRequest.class,
new IssueCredentialRequestSerializer());
gsonBuilder.registerTypeAdapter(PresentationVerifiedReceiptOptions.class,
new PresentationVerifiedReceiptOptionsSerializer());
Gson myGson = gsonBuilder.create();
return GsonConverterFactory.create(myGson);
}
}