<commit_before>module Amiando
  class Request < Typhoeus::Request
    attr_reader :object

    def initialize(object, verb, path, params)
      @object = object

      if verb == :post
        path = build_url(path, default_params)
      else
        path = build_url(path)
        params = default_params.merge(params || {})
      end

      super(path, :method => verb, :params => params, :verbose => Amiando.verbose)
    end

    def log_request
      if Amiando.logger && Amiando.logger.debug?
        Amiando.logger.debug "REST request #{filter_log(url)} with body #{filter_log(self.params.inspect)}"
      end
    end

    def log_response
      if Amiando.logger
        Amiando.logger.info "REST request #{filter_log(url)} returned #{response.code} and took #{response.time} seconds"
        if Amiando.logger.debug? && response.body
          Amiando.logger.debug "REST response body: #{filter_log(response.body.inspect)}"
        end
      end
    end

    def filter_log(text)
      text.gsub(/password=([^&]+)/, "password=[FILTERED]").gsub(/password: .*/,'password: [FILTERED]')
    end

    private

    def default_params
      default = {
        :format  => :json,
        :version => 1
      }
      default.merge!(:apikey => Amiando.api_key) if Amiando.api_key
      default
    end

    def build_url(url, merge_params = {})
      url = URI.join(Amiando.base_url, url).to_s<commit_msg>Log the verb of the request<commit_after>module Amiando
  class Request < Typhoeus::Request
    attr_reader :object

    def initialize(object, verb, path, params)
      @object = object

      if verb == :post
        path = build_url(path, default_params)
      else
        path = build_url(path)
        params = default_params.merge(params || {})
      end

      super(path, :method => verb, :params => params, :verbose => Amiando.verbose)
    end

    def log_request
      if Amiando.logger && Amiando.logger.debug?
        Amiando.logger.debug "#{self.method.to_s.upcase} request #{filter_log(url)} with body #{filter_log(self.params.inspect)}"
      end
    end

    def log_response
      if Amiando.logger
        Amiando.logger.info "#{self.method.to_s.upcase} request #{filter_log(url)} returned #{response.code} and took #{response.time} seconds"
        if Amiando.logger.debug? && response.body
          Amiando.logger.debug "#{self.method.to_s.upcase} response body: #{filter_log(response.body.inspect)}"
        end
      end
    end

    def filter_log(text)
      text.gsub(/password=([^&]+)/, "password=[FILTERED]").gsub(/password: .*/,'password: [FILTERED]')
    end

    private

    def default_params
      default = {
        :format  => :json,
        :version => 1
      }
      default.merge!(:apikey => Amiando.api_key) if Amiando.api_key
      default
    end

    def build_url(url, merge_params = {})
      url = URI.join(Amiando.base_url, url).to_s
{'fraction_non_alphanumeric': 0.1260387811634349, 'fraction_numerical': 0.0006925207756232687, 'mean_word_length': 2.6248431618569636, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '7423705', 'n_tokens_mistral': 936, 'n_tokens_neox': 914, 'n_words': 204}