<commit_before>require 'yaml'
require 'typhoeus'
require 'service_jynx'
require 'json'
require 'restfull_client_configuration'
require 'restfull_client_logger'
require 'restfull_client_uri'

module RestfullClient
  extend self

  class RestError < StandardError; end

  attr_accessor :configuration, :logger, :env
  @@timeout_occured_count = 0

  def self.configure
    self.configuration ||= RestfullClientConfiguration.new
    yield(configuration)
    self.configuration.run!
    self.logger = RestfullClientLogger.logger
  end


  def get(caller, path, params = {}, &on_error_block)
    url = RestfullClientUri.uri_join(callerr_config(caller)["url"], path)
    request = Typhoeus::Request.new(url, headers: { "Accept" => "text/json" }, method: 'GET', timeout: timeout, params: params)
    run_safe_request(caller, request, true, &on_error_block)
  end

  def post(caller, path, payload, &on_error_block)
    url = RestfullClientUri.uri_join(callerr_config(caller)["url"], path)
    headers = {}
    if payload.is_a?(String)
      payload_as_str = payload
    elsif payload.is_a?(Hash)
      payload_as_str = payload.to_json(root: false)
      headers.merge!({ "Content-Type" => "application/json" })
    else
      payload_as_str
    end
    request = Typhoeus::Request.new(url, headers: headers, method: 'POST', body: payload_as_str, timeout: timeout)
    run_safe_request(caller, request, false, &on_error_block)
  end

  def delete(caller, path, &on_error_block)
    url = RestfullClientUri.uri_join(callerr_config(caller)["url"], path)
    request = Typhoeus::Request.new(url, method: 'DELETE', timeout: timeout)
    run_safe_request(caller, request, true, &on_error_block)
  end

  def put(caller, path, payload, &on_error_block)
    url = RestfullClientUri.uri_join(callerr_config(caller)["url"], path)
    headers = {}
    if payload.is_a?(String)
      payload_as_str = payload
    elsif payload.is_a?(Hash)
      payload_as_str = payload.to_json(root: false)
      headers.merge!({ "Content-Type" => "application/json" })
    else
      payload_as_str
    end
    request = Typhoeus::Request.new(url, headers: headers, method: 'PUT', body: payload_as_str, timeout: timeout)
    run_safe_request(caller, request, false, &on_error_block)
  end
<commit_msg>[FIX] Added a post_raw API, to keep request unchanged when needed<commit_after>require 'yaml'
require 'typhoeus'
require 'service_jynx'
require 'json'
require 'restfull_client_configuration'
require 'restfull_client_logger'
require 'restfull_client_uri'

module RestfullClient
  extend self

  class RestError < StandardError; end

  attr_accessor :configuration, :logger, :env
  @@timeout_occured_count = 0

  def self.configure
    self.configuration ||= RestfullClientConfiguration.new
    yield(configuration)
    self.configuration.run!
    self.logger = RestfullClientLogger.logger
  end


  def get(caller, path, params = {}, &on_error_block)
    url = RestfullClientUri.uri_join(callerr_config(caller)["url"], path)
    request = Typhoeus::Request.new(url, headers: { "Accept" => "text/json" }, method: 'GET', timeout: timeout, params: params)
    run_safe_request(caller, request, true, &on_error_block)
  end

  def post(caller, path, payload, &on_error_block)
    url = RestfullClientUri.uri_join(callerr_config(caller)["url"], path)
    headers = {}
    if payload.is_a?(Hash)
      payload_as_str = payload.to_json(root: false)
      headers.merge!({ "Content-Type" => "application/json" })
    else
      payload_as_str = payload
    end
    request = Typhoeus::Request.new(url, headers: headers, method: 'POST', body: payload_as_str, timeout: timeout)
    run_safe_request(caller, request, false, &on_error_block)
  end

  def post_raw(caller, path, payload, &on_error_block)
    url = RestfullClientUri.uri_join(callerr_config(caller)["url"], path)
    request = Typhoeus::Request.new(url, headers: headers, method: 'POST', body: payload, timeout: timeout)
    run_safe_request(caller, request, false, &on_error_block)
  end

  def delete(caller, path, &on_error_block)
    url = RestfullClientUri.uri_join(callerr_config(caller)["url"], path)
    request = Typhoeus::Request.new(url, method: 'DELETE', timeout: timeout)
    run_safe_request(caller, request, true, &on_error_block)
  end

  def put(caller, path, payload, &on_error_block)
    url = RestfullClientUri.uri_join(callerr_config(caller)["url"], path)
    headers = {}
    if payload.is_a?(Hash)
      payload_as_str = payload.to_json(root: false)
      headers.merge!({ "Content-Type" => "application/json" })
    else
      payload_as_str = payload
    end
    request = Typhoeus::Request.new(url, headers: headers, method: 'PUT', body: payload_as_str, timeout: timeout)
    run_safe_request(caller, request, false, &on_error_block)
  end

{'fraction_non_alphanumeric': 0.11831103678929766, 'fraction_numerical': 0.0004180602006688963, 'mean_word_length': 4.744297719087635, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '5073009', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1582, 'n_words': 341}