<commit_before>require 'json'

require 'faraday'
require 'mustache'

require_relative 'endpoint'

class Chaplin
  ApiEndpoint = Struct.new(:http_method, :path, :params, :headers) do

    def initialize(http_method, path, params, headers = {})
      super(http_method, path, params || {}, headers)
    end

    def self.configure(api_url, default_headers, basic_auth)
      @@client = Faraday.new(url: api_url) do |client|
        client.adapter Faraday.default_adapter
        if basic_auth
          client.basic_auth(basic_auth['user'], basic_auth['password'])
        end
      end
      @@default_headers = default_headers || {}
    end

    def render(request_params)
      response_body = api_response(request_params).body
      return nil if response_body == 'null'
      JSON.parse(response_body)
    end

    private

    def api_response(request_params)
      @@client.send(
        http_method,
        parsed_path(request_params),
        api_request_params(request_params),
        @@default_headers
      )
    end

    def api_request_params(chaplin_request_params)
      if json_request? && [:post, :put, :patch].include?(http_method)
        Mustache.render(params.to_json, chaplin_request_params)
      else
        rendered_params(chaplin_request_params)
      end
    end

    def json_request?
      request_headers['Content-Type'] == 'application/json'
    end<commit_msg>Return nil for empty response body<commit_after>require 'json'

require 'faraday'
require 'mustache'

require_relative 'endpoint'

class Chaplin
  ApiEndpoint = Struct.new(:http_method, :path, :params, :headers) do

    def initialize(http_method, path, params, headers = {})
      super(http_method, path, params || {}, headers)
    end

    def self.configure(api_url, default_headers, basic_auth)
      @@client = Faraday.new(url: api_url) do |client|
        client.adapter Faraday.default_adapter
        if basic_auth
          client.basic_auth(basic_auth['user'], basic_auth['password'])
        end
      end
      @@default_headers = default_headers || {}
    end

    def render(request_params)
      response_body = api_response(request_params).body
      return nil if (response_body == 'null' or response_body == '')
      JSON.parse(response_body)
    end

    private

    def api_response(request_params)
      @@client.send(
        http_method,
        parsed_path(request_params),
        api_request_params(request_params),
        @@default_headers
      )
    end

    def api_request_params(chaplin_request_params)
      if json_request? && [:post, :put, :patch].include?(http_method)
        Mustache.render(params.to_json, chaplin_request_params)
      else
        rendered_params(chaplin_request_params)
      end
    end

    def json_request?
      request_headers['Content-Type'] == 'application/json'
    end
{'fraction_non_alphanumeric': 0.09880028228652082, 'fraction_numerical': 0.0, 'mean_word_length': 3.1691176470588234, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6059137', 'n_tokens_mistral': 861, 'n_tokens_neox': 846, 'n_words': 174}