class GdsApi::JsonClient

def do_json_request(method, url, params = nil, additional_headers = {}, &create_response)

from the Net::HTTPResponse
create_response: optional block to instantiate a custom response object
additional_headers: headers to set on the request (in addition to the default ones)
params: the data to send (JSON-serialised) in the request body
url: the request URL
method: the symbolic name of the method to use, e.g. :get, :post
def do_json_request(method, url, params = nil, additional_headers = {}, &create_response)
  begin
    response = do_request_with_cache(method, url, (params.to_json if params), additional_headers)
  rescue RestClient::ResourceNotFound => e
    raise GdsApi::HTTPNotFound.new(e.http_code)
  rescue RestClient::Gone => e
    raise GdsApi::HTTPGone.new(e.http_code)
  rescue RestClient::Exception => e
    # Attempt to parse the body as JSON if possible
    error_details = begin
      e.http_body ? JSON.parse(e.http_body) : nil
    rescue JSON::ParserError
      nil
    end
    raise GdsApi::HTTPErrorResponse.new(e.http_code, error_details), e.http_body
  end
  # If no custom response is given, just instantiate Response
  create_response ||= Proc.new { |r| Response.new(r) }
  create_response.call(response)
end