<commit_before>require 'java'
require File.dirname(__FILE__) + '/commons-codec-1.3.jar'
require File.dirname(__FILE__) + '/commons-httpclient-3.1.jar'

require 'cgi'

module JRestClient  
  include_package 'org.apache.commons.httpclient'
  include_package 'org.apache.commons.httpclient.methods'
  include_package 'org.apache.commons.httpclient.params'    
  include_class 'org.apache.commons.httpclient.params.HttpMethodParams'  

  class << self
    attr_accessor :timeout
  end

  def self.get(url, headers = {})
    client = get_client
    method = GetMethod.new(url)
    set_headers(method, headers)    
    make_request(client, method)
  end
  
  def self.post(url, payload, headers = {})
    client = get_client
    method = PostMethod.new(url)
    set_headers(method, headers)
    
    unless payload.is_a? String
      payload = payload.collect { |key, value| "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"}.join("&")
    end
    
    method.setRequestBody(payload);    
    make_request(client, method)
  end
  
  protected
  
  def self.get_client
    client = HttpClient.new
    
    # Blah blah blah this is deprecated blah blah blah
    client.timeout = timeout if timeout
    
    client
  end
  
  def self.make_request(client, method)
    status = client.executeMethod(method)
    response = method.responseBody
    method.releaseConnection

    java.lang.String.new(response).to_s
  end
  
  def self.set_headers(method, headers)
    headers.each { |key, value| method.setRequestHeader(key.to_s, value.to_s) }
  end
  
end




<commit_msg>Throwing an exception if we time out<commit_after>require 'java'
require File.dirname(__FILE__) + '/commons-codec-1.3.jar'
require File.dirname(__FILE__) + '/commons-httpclient-3.1.jar'

require 'cgi'

module JRestClient  
  class RequestTimeout < Exception; end
	
  include_package 'org.apache.commons.httpclient'
  include_package 'org.apache.commons.httpclient.methods'
  include_package 'org.apache.commons.httpclient.params'    
  include_class 'org.apache.commons.httpclient.params.HttpMethodParams'  

  class << self
    attr_accessor :timeout
  end

  def self.get(url, headers = {})
    client = get_client
    method = GetMethod.new(url)
    set_headers(method, headers)    
    make_request(client, method)
  end
  
  def self.post(url, payload, headers = {})
    client = get_client
    method = PostMethod.new(url)
    set_headers(method, headers)
    
    unless payload.is_a? String
      payload = payload.collect { |key, value| "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"}.join("&")
    end
    
    method.setRequestBody(payload);    
    make_request(client, method)
  end
  
  protected
  
  def self.get_client
    client = HttpClient.new
    
    # Blah blah blah this is deprecated blah blah blah
    client.timeout = timeout if timeout
    
    client
  end
  
  def self.make_request(client, method)
    begin
      status = client.executeMethod(method)
      response = method.responseBody
    rescue java.net.SocketTimeoutException => e
      raise RequestTimeout.new(e)
    ensure
      method.releaseConnection
    end
    java.lang.String.new(response).to_s
  end
  
  def self.set_headers(method, headers)
    headers.each { |key, value| method.setRequestHeader(key.to_s, value.to_s) }
  end
  
end





{'fraction_non_alphanumeric': 0.1043872919818457, 'fraction_numerical': 0.002420574886535552, 'mean_word_length': 3.23303457106274, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 4, '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': '5352931', 'n_tokens_mistral': 1085, 'n_tokens_neox': 1059, 'n_words': 218}