All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jp.gopay.sdk.utils.RequestUtils Maven / Gradle / Ivy

There is a newer version: 0.11.17
Show newest version
package jp.gopay.sdk.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jp.gopay.sdk.converters.BinaryDataConverterFactory;
import jp.gopay.sdk.converters.DomainConverterFactory;
import jp.gopay.sdk.converters.IdempotencyKeyConverterFactory;
import jp.gopay.sdk.converters.VoidConverterFactory;
import jp.gopay.sdk.models.common.Domain;
import jp.gopay.sdk.models.common.auth.AuthStrategy;
import jp.gopay.sdk.models.common.auth.LoginJWTStrategy;
import jp.gopay.sdk.models.request.subscription.RemoveInstallmentsPlan;
import jp.gopay.sdk.models.response.PaymentsPlan;
import jp.gopay.sdk.models.response.transactiontoken.TokenAliasKey;
import jp.gopay.sdk.models.webhook.WebhookEvent;
import jp.gopay.sdk.models.webhook.WebhookEventDeserializer;
import jp.gopay.sdk.settings.AbstractSDKSettings;
import jp.gopay.sdk.types.CardBrand;
import jp.gopay.sdk.types.Country;
import jp.gopay.sdk.types.DayOfMonth;
import jp.gopay.sdk.types.MetadataMap;
import okhttp3.ConnectionPool;
import org.joda.time.LocalDate;
import org.joda.time.Period;
import org.threeten.bp.Duration;
import org.threeten.bp.ZoneId;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import java.util.Date;

import static jp.gopay.sdk.adapters.JsonAdapters.*;

public class RequestUtils {
    // Utility gson that doesn't ignore null values. Useful on some patch methods that use nulls to unset properties.
    private static Gson gsonForNulls = new GsonBuilder().serializeNulls().create();

    private static Gson gson = new GsonBuilder()
            .enableComplexMapKeySerialization()
            .registerTypeAdapter(Date.class, new JsonDateAdapter())
            .registerTypeAdapter(Domain.class, new JsonDomainAdapter())
            .registerTypeAdapter(Period.class, new JsonPeriodAdapter())
            .registerTypeAdapter(Duration.class, new JsonDurationAdapter())
            .registerTypeAdapter(LocalDate.class, new JsonLocalDateAdapter())
            .registerTypeAdapter(LoginJWTStrategy.class, new JsonJWTDeserializer())
            .registerTypeAdapter(WebhookEvent.class, new WebhookEventDeserializer())
            .registerTypeAdapter(RemoveInstallmentsPlan.class, new JsonRemoveInstallmentsPlanAdapter(gsonForNulls))
            .registerTypeAdapter(ZoneId.class, new JsonZoneIdAdapter())
            .registerTypeAdapter(Country.class, new JsonCountryAdapter())
            .registerTypeAdapter(MetadataMap.class, new JsonMetadataMapAdapter())
            .registerTypeAdapter(CardBrand.class, new JsonCardBrandAdapter())
            .registerTypeAdapter(PaymentsPlan.class, new JsonPaymentsPlanAdapter())
            .registerTypeAdapter(DayOfMonth.class, new JsonDayOfMonthAdapter())
            .registerTypeAdapter(TokenAliasKey.class, new JsonTokenAliasKeyAdapter())
            .create();


    public static Retrofit createClient(AuthStrategy authStrategy, AbstractSDKSettings settings, ConnectionPool connectionPool){
        return getBuilderBeforeClient(settings)
                .client(settings.getClient(authStrategy, connectionPool))
                .build();
    }

    public static Retrofit createClient(AuthStrategy authStrategy, AbstractSDKSettings settings){
        return getBuilderBeforeClient(settings)
                .client(settings.getClient(authStrategy))
                .build();
    }

    public static Retrofit.Builder getBuilderBeforeClient(AbstractSDKSettings settings){
        return new Retrofit.Builder()
                .baseUrl(settings.getEndpoint())
                .addConverterFactory(new BinaryDataConverterFactory())
                .addConverterFactory(new VoidConverterFactory())
                .addConverterFactory(new EnumConverter())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addConverterFactory(new IdempotencyKeyConverterFactory())
                .addConverterFactory(new DomainConverterFactory());
    }

    public static Gson getGson() {
        return gson;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy