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

com.eligible.net.APIResource Maven / Gradle / Ivy

There is a newer version: 1.13.13
Show newest version
package com.eligible.net;

import com.eligible.Eligible;
import com.eligible.exception.APIConnectionException;
import com.eligible.exception.APIException;
import com.eligible.exception.AuthenticationException;
import com.eligible.exception.InvalidRequestException;
import com.eligible.json.deserializer.ClaimErrorDeserializer;
import com.eligible.json.deserializer.DatesDeserializer;
import com.eligible.json.deserializer.EligibleObjectTypeAdapterFactory;
import com.eligible.json.deserializer.FinancialFlowListDeserializer;
import com.eligible.model.EligibleObject;
import com.eligible.model.claim.Error;
import com.eligible.model.coverage.Dates;
import com.eligible.model.coverage.FinancialFlow;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import lombok.Setter;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import static com.eligible.util.NetworkUtil.CHARSET;
import static com.eligible.util.NetworkUtil.urlEncode;

/**
 * Base class for all Eligible API Endpoints.
 */
public abstract class APIResource extends EligibleObject {

    /**
     * {@link EligibleResponseGetter} for fetching API responses. It can be overridden to a mock for TESTING.
     *
     * @param eligibleResponseGetter eligible response getter
     */
    @Setter
    private static EligibleResponseGetter eligibleResponseGetter = new LiveEligibleResponseGetter();

    /**
     * {@link Gson} deserializer for API responses.
     */
    public static final Gson GSON = new GsonBuilder()
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            .registerTypeAdapterFactory(new EligibleObjectTypeAdapterFactory())
            .registerTypeAdapter(Dates.class, new DatesDeserializer())
            .registerTypeAdapter(Error.class, new ClaimErrorDeserializer())
            .registerTypeAdapter(
                    new TypeToken>() { }.getType(),
                    new FinancialFlowListDeserializer())
            .create();

    protected static String className(Class clazz) {
        String className = clazz.getSimpleName().toLowerCase();

        switch (className) {
            case "searchoptions":
                return "search_options";
            case "costestimates":
                return "cost_estimates";
            case "paymentreport":
                return "payment_report";
            case "paymentreports":
                return "payment_reports";
            case "paymentstatus":
                return "payment/status";
            case "enrollmentnpi":
                return "enrollment_npi";
            case "receivedpdf":
                return "received_pdf";
            case "originalsignaturepdf":
                return "original_signature_pdf";
            default:
                return className;
        }
    }

    protected static String singleClassURL(Class clazz) {
        return singleClassURL(clazz, Eligible.getApiBase());
    }

    protected static String singleClassURL(Class clazz, String apiBase) {
        return String.format("%s/v1.5/%s", apiBase, className(clazz));
    }

    protected static String classURL(Class clazz) {
        return classURL(clazz, Eligible.getApiBase());
    }

    protected static String classURL(Class clazz, String apiBase) {
        return String.format("%ss", singleClassURL(clazz, apiBase));
    }

    protected static String instanceURL(Class clazz, String id)
            throws InvalidRequestException {
        return instanceURL(clazz, id, Eligible.getApiBase());
    }

    protected static String instanceURL(Class clazz, String id, String apiBase)
            throws InvalidRequestException {
        try {
            return String.format("%s/%s", classURL(clazz, apiBase), urlEncode(id));
        } catch (UnsupportedEncodingException e) {
            throw new InvalidRequestException("Unable to encode parameters to "
                    + CHARSET
                    + ". Please contact [email protected] for assistance.",
                    null, e);
        }
    }

    protected static  T multipartRequest(RequestMethod method,
                                            String url, Map params, Type typeOfT,
                                            RequestOptions options) throws AuthenticationException,
            InvalidRequestException, APIConnectionException,
            APIException {
        return APIResource.eligibleResponseGetter.request(method, url, params, typeOfT,
                RequestType.MULTIPART, options);
    }

    protected static  T request(RequestMethod method,
                                   String url, Map params, Type typeOfT,
                                   RequestOptions options) throws AuthenticationException,
            InvalidRequestException, APIConnectionException,
            APIException {
        return APIResource.eligibleResponseGetter.request(method, url, params, typeOfT,
                RequestType.NORMAL, options);
    }

    // override request method for type checking.
    protected static  T request(RequestMethod method,
                                   String url, Map params, Class typeOfT,
                                   RequestOptions options) throws AuthenticationException,
            InvalidRequestException, APIConnectionException,
            APIException {
        return request(method, url, params, (Type) typeOfT, options);
    }

    protected static EligibleResponse request(RequestMethod method,
                                              String url, Map params,
                                              RequestOptions options) throws AuthenticationException,
            InvalidRequestException, APIConnectionException,
            APIException {
        return APIResource.eligibleResponseGetter.request(method, url, params,
                RequestType.NORMAL, options);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy