<commit_before>require "net/http"
require "uri"
require "ostruct"
require "json"

class ApiClient
   ENDPOINT = "https://api.replicated.com/vendor/v1"
   VERB_MAP = {
      :get    => Net::HTTP::Get,
      :post   => Net::HTTP::Post,
      :put    => Net::HTTP::Put,
      :delete => Net::HTTP::Delete
   }

   def initialize(token = nil, endpoint = ENDPOINT)
      uri = URI.parse(endpoint)
      @http = Net::HTTP.new(uri.host, uri.port)
   end

   def request_json(method, path, params)
      response = request(method, path, params)
      body = JSON.parse(response.body)

      OpenStruct.new(:code => response.code, :body => body)
   rescue JSON::ParserError
      response
   end

   def request(method, path, params)
      case method
      when :get
         full_path = encode_path_params(path, params)
         request = VERB_MAP[method.to_sym].new(full_path)
      else
         request = VERB_MAP[method.to_sym].new(path)
         request.set_form_data(params)
      end

      @http.request(request)
   end
      
   def encode_path_params(path, params)
      encoded = URI.encode_www_form(params)
      [path, encoded].join("?")
   end
end
<commit_msg>Add token use to client<commit_after>require "net/http"
require "uri"
require "ostruct"
require "json"

class ApiClient
   ENDPOINT = "https://api.replicated.com/vendor/v1"
   VERB_MAP = {
      :get    => Net::HTTP::Get,
      :post   => Net::HTTP::Post,
      :put    => Net::HTTP::Put,
      :delete => Net::HTTP::Delete
   }

   def initialize(endpoint = ENDPOINT)
      uri = URI.parse(endpoint)
      @http = Net::HTTP.new(uri.host, uri.port)
   end

   def set_token(api_token)
      @api_token = api_token
   end

   def request_json(method, path, params)
      response = request(method, path, params)
      body = JSON.parse(response.body)

      OpenStruct.new(:code => response.code, :body => body)
   rescue JSON::ParserError
      response
   end

   def request(method, path, params)
      case method
      when :get
         full_path = encode_path_params(path, params)
         request = VERB_MAP[method.to_sym].new(full_path)
      else
         request = VERB_MAP[method.to_sym].new(path)
         request.set_form_data(params)
      end

      if @api_token
         request['Authorization'] = @api_token
      end

      @http.request(request)
   end
      
   def encode_path_params(path, params)
      encoded = URI.encode_www_form(params)
      [path, encoded].join("?")
   end
end

{'fraction_non_alphanumeric': 0.1232821341956346, 'fraction_numerical': 0.0008084074373484236, 'mean_word_length': 2.623718887262079, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '3058082', 'n_tokens_mistral': 814, 'n_tokens_neox': 806, 'n_words': 170}