def request(params, &block)
      # Returns a query hash with non nil values.
      params[:query].reject! { |_, value| value.nil? } if params[:query]

      case params[:method]
      when :post
        query = @connection.options.merge(params[:query] || {})
        @connection[params[:path]].post(query) { |response, _, _|
          @response = response
        }
      when :get
        query = @connection.options[:params].merge(params[:query] || {})
        @connection[params[:path]].get(:params => query) { |response, _, _|
          @response = response
        }
      end

      log.debug("args: #{@response.request.args}") if log

      if @response.headers[:content_disposition]
        filename = params[:output] || @response.headers[:content_disposition][/attachment; filename="(.+?)"/, 1]
        body = @response.body
        file = open(filename, 'wb')
        file.write(body)
        file.close
        return true
      else
        doc = JSON.load(@response.body)
        log.debug("body: #{doc}") if log

        if doc.kind_of?(Hash) && doc['success'] == false
          code    = doc['error']['code']
          message = doc['error']['message']
          error   = Crowdin::API::Errors::Error.new(code, message)
          raise(error)
        else
          return doc
        end
      end

    end
def request(params, &block)
      # Returns a query hash with non nil values.
      params[:query].reject! { |_, value| value.nil? } if params[:query]

      case params[:method]
      when :post
        query = @connection.options.merge(params[:query] || {})
        @connection[params[:path]].post(query) { |response, _, _|
          @response = response
        }
      when :get
        query = @connection.options[:params].merge(params[:query] || {})
        @connection[params[:path]].get(:params => query) { |response, _, _|
          @response = response
        }
      end

      log.debug("args: #{@response.request.args}") if log

      if @response.headers[:content_disposition]
        filename = params[:output] || @response.headers[:content_disposition][/attachment; filename="(.+?)"/, 1]
        body = @response.body
        file = open(filename, 'wb')
        file.write(body)
        file.close
        return true
      else
        doc = JSON.load(@response.body)
        log.debug("body: #{doc}") if log

        if doc.kind_of?(Hash) && doc['success'] == false
          code    = doc['error']['code']
          message = doc['error']['message']
          error   = Crowdin::API::Errors::Error.new(code, message)
          raise(error)
        else
          return doc
        end
      end

    end
Create a new API object using the given parameters.

 @param [String] api_key the authentication API key can be found on the project settings page
 @param [String] project_id the project identifier.
 @param [String] account_key the account API Key
 @param [String] base_url the url of the Crowdin API