<commit_before>require 'addressable/uri'
require 'http'
require 'http/form_data'
require 'json'

module Caplinked
  class Request
    attr_accessor :client, :request_method, :path, :headers, :params, :uri

    def initialize(client, request_method, path, options = {})
      @client = client
      @request_method = request_method
      @uri = Addressable::URI.parse("#{client.api_scheme}://#{client.api_host}#{path}")
      @path = @uri.path
      @options = options
    end

    def perform
      headers = (@options.delete(:headers) || {}).merge('X-Token' => @client.api_key)
      @uri.query_values = @options.delete(:params)
      response = HTTP.headers(headers).request(@request_method, @uri.to_s, @options)
      response_body = response.body.empty? ? '' : response.parse.symbolize_keys!
      response_headers = response.headers
      fail_or_return_response_body(response.code, response_body, response_headers)
    end

  private
    def fail_or_return_response_body(code, body, headers)
      return(Caplinked::Error.from_response(body, headers)) if body[:error].present?
      body
    end
  end
end<commit_msg>change return back to raise<commit_after>require 'addressable/uri'
require 'http'
require 'http/form_data'
require 'json'

module Caplinked
  class Request
    attr_accessor :client, :request_method, :path, :headers, :params, :uri

    def initialize(client, request_method, path, options = {})
      @client = client
      @request_method = request_method
      @uri = Addressable::URI.parse("#{client.api_scheme}://#{client.api_host}#{path}")
      @path = @uri.path
      @options = options
    end

    def perform
      headers = (@options.delete(:headers) || {}).merge('X-Token' => @client.api_key)
      @uri.query_values = @options.delete(:params)
      response = HTTP.headers(headers).request(@request_method, @uri.to_s, @options)
      response_body = response.body.empty? ? '' : response.parse.symbolize_keys!
      response_headers = response.headers
      fail_or_return_response_body(response.code, response_body, response_headers)
    end

  private
    def fail_or_return_response_body(code, body, headers)
      raise(Caplinked::Error.from_response(body, headers)) if body[:error].present?
      body
    end
  end
end
{'fraction_non_alphanumeric': 0.12660469234174412, 'fraction_numerical': 0.0, 'mean_word_length': 3.9452954048140043, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '205116', 'n_tokens_mistral': 689, 'n_tokens_neox': 698, 'n_words': 141}