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

com.global.api.gateways.RestGateway Maven / Gradle / Ivy

There is a newer version: 14.2.3
Show newest version
package com.global.api.gateways;

import com.global.api.entities.exceptions.GatewayException;
import com.global.api.utils.JsonDoc;

import java.util.HashMap;

public class RestGateway extends Gateway {
    public RestGateway() {
        super("application/json");
    }

    public String doTransaction(String verb, String endpoint) throws GatewayException {
        return doTransaction(verb, endpoint, null, null);
    }

    public String doTransaction(String verb, String endpoint, String data) throws GatewayException {
        return doTransaction(verb, endpoint, data, null);
    }

    public String doTransaction(String verb, String endpoint, String data, HashMap queryStringParams) throws GatewayException {
        try {
            GatewayResponse response = sendRequest(verb, endpoint, data, queryStringParams);
            return handleResponse(response);
        } catch (GatewayException exc) {
            JsonDoc parsed = JsonDoc.parse(exc.getResponseText());
            if (parsed.has("error_code")) {     // has the expected JSON GP API error format
                String errorCode = parsed.getString("error_code");
                String detailedErrorCode = parsed.getString("detailed_error_code");
                String detailedErrorDescription = parsed.getString("detailed_error_description");

                throw new GatewayException(
                        String.format("Status Code: %s - %s", exc.getResponseCode(), detailedErrorDescription),
                        errorCode,
                        detailedErrorCode
                );
            } else if (parsed.has("error")) {
                throw new GatewayException("Error occurred while communicating with gateway.", exc, exc.getResponseCode(), parsed.getString("message"));
            }
            throw new GatewayException("Error occurred while communicating with gateway.", exc, exc.getResponseCode(), exc.getResponseText());
        }
    }

    protected String handleResponse(GatewayResponse response) throws GatewayException {
        if(response.getStatusCode() != 200 && response.getStatusCode() != 204 && response.getStatusCode() != 201) {
            JsonDoc parsed = JsonDoc.parse(response.getRawResponse());
            if(parsed.has("error")) {
                JsonDoc error = parsed.get("error");
                throw new GatewayException(String.format("Status code: %s - %s", response.getStatusCode(), error.getString("message")));
            }
            throw new GatewayException(String.format("Status code: %s - %s", response.getStatusCode(), parsed.getString("message")));
        }
        return response.getRawResponse();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy