/*
 *                       ######
 *                       ######
 * ############    ####( ######  #####. ######  ############   ############
 * #############  #####( ######  #####. ######  #############  #############
 *        ######  #####( ######  #####. ######  #####  ######  #####  ######
 * ###### ######  #####( ######  #####. ######  #####  #####   #####  ######
 * ###### ######  #####( ######  #####. ######  #####          #####  ######
 * #############  #############  #############  #############  #####  ######
 *  ############   ############  #############   ############  #####  ######
 *                                      ######
 *                               #############
 *                               ############
 *
 * Adyen Java API Library
 *
 * Copyright (c) 2018 Adyen B.V.
 * This file is open source and available under the MIT license.
 * See the LICENSE file for more info.
 */
package com.adyen.service;

import com.adyen.Config;
import com.adyen.Service;
import com.adyen.httpclient.ClientInterface;
import com.adyen.httpclient.HTTPClientException;
import com.adyen.model.ApiError;
import com.adyen.service.exception.ApiException;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;

public class Resource {

    protected static final Gson GSON = new Gson();
    protected String endpoint;
    protected List<String> requiredFields;
    private Service service;

    public Resource(Service service, String endpoint, List<String> requiredFields) {
        this.service = service;
        this.endpoint = endpoint;
        this.requiredFields = requiredFields;
    }

    /**
     * Request using json String
     *
     * @param json
     * @return
     * @throws ApiException
     * @throws IOException
     */
    public String request(String json) throws ApiException, IOException {
        ClientInterface clientInterface = (ClientInterface) this.service.getClient().getHttpClient();
        Config config = this.service.getClient().getConfig();
        String result = null;
        try {
            result = clientInterface.request(this.endpoint, json, config, this.service.isApiKeyRequired());
        } catch (HTTPClientException e) {
            String responseBody = e.getResponseBody();
            ApiError apiError = GSON.fromJson(responseBody, new TypeToken<ApiError>() {
            }.getType());
            ApiException apiException = new ApiException(e.getMessage(), e.getCode());
            apiException.setError(apiError);
            throw apiException;
        }
        return result;
    }
}

@@ -939,16 +939,67 @@
 rvice;%0A%0A
+import java.io.IOException;%0Aimport java.util.List;%0A
 import c
@@ -1273,77 +1273,69 @@
 son.
-reflect.TypeToken;%0A%0Aimport java.io.IOException;%0Aimport java.util.List
+JsonSyntaxException;%0Aimport com.google.gson.reflect.TypeToken
 ;%0A%0Ap
@@ -1762,104 +1762,8 @@
 ing%0A
-     *%0A     * @param json%0A     * @return%0A     * @throws ApiException%0A     * @throws IOException%0A
     
@@ -2026,19 +2026,54 @@
  res
-ult = null;
+ponseBody;%0A        ApiException apiException;%0A
 %0A   
@@ -2101,14 +2101,12 @@
   re
-sult =
+turn
  cli
@@ -2235,39 +2235,32 @@
 ) %7B%0A            
-String 
 responseBody = e
@@ -2275,24 +2275,188 @@
 onseBody();%0A
+            apiException = new ApiException(e.getMessage(), e.getCode());%0A        %7D%0A%0A        // Enhance ApiException with more info from JSON payload%0A        try %7B%0A
             
@@ -2561,33 +2561,33 @@
 ));%0A            
-A
+a
 piException apiE
@@ -2585,146 +2585,197 @@
 tion
- apiException = new ApiException(e.getMessage(), e.getCode());%0A            apiException.setError(apiError);%0A            throw apiException
+.setError(apiError);%0A        %7D catch (JsonSyntaxException ignored) %7B%0A            throw new ApiException(%22Invalid respose or an invalid X-API-Key key was used%22, apiException.getStatusCode())
 ;%0A  
@@ -2782,24 +2782,25 @@
       %7D%0A
+%0A
         
 return r
@@ -2795,21 +2795,26 @@
     
-return result
+throw apiException
 ;%0A  

Add handling for non-json API responses (403)