<commit_before>module XeroGateway
  module Http
    OPEN_TIMEOUT = 10 unless defined? OPEN_TIMEOUT
    READ_TIMEOUT = 60 unless defined? READ_TIMEOUT
    ROOT_CA_FILE = File.join(File.dirname(__FILE__), 'ca-certificates.crt') unless defined? ROOT_CA_FILE
    
    def http_get(client, url, extra_params = {})
      http_request(client, :get, url, nil, extra_params)
    end

    def http_post(client, url, body, extra_params = {})
      http_request(client, :post, url, body, extra_params)
    end

    def http_put(client, url, body, extra_params = {})
      http_request(client, :put, url, body, extra_params)
    end
    
    private
    
      def http_request(client, method, url, body, params = {})
        headers = {'Accept-Encoding' => 'gzip, deflate'}

        if method != :get
         headers['Content-Type'] ||= "application/x-www-form-urlencoded"
        end
        
        # HAX.  Xero completely misuse the If-Modified-Since HTTP header.
        headers['If-Modified-Since'] = params.delete(:modified_since).utc.strftime("%Y-%m-%dT%H:%S") if params[:modified_since]

        params = params.map {|key,value| "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"}.join("&")

        uri   = URI.parse(url + "?" + params)

        # # Only setup @cached_http once on first use as loading the CA file is quite expensive computationally.
        # unless @cached_http && @cached_http.address == uri.host && @cached_http.port == uri.port
        #   @cached_http = Net::HTTP.new(uri.host, uri.port) 
        #   @cached_http.open_timeout = OPEN_TIMEOUT
        #   @cached_http.read_timeout = READ_TIMEOUT
        #   @cached_http.use_ssl      = true
        # 
        #   # Need to validate server's certificate against root certificate authority to prevent man-in-the-middle attacks.
        #   @cached_http.ca_file        = ROOT_CA_FILE
        #   # http.verify_mode    = OpenSSL::SSL::VERIFY_NONE
        #   @cached_http.verify_mode    = OpenSSL::SSL::VERIFY_PEER
        #   @cached_http.verify_depth   = 5
        # end
        
        case method
          when :get   then    client.get(uri.request_uri, headers).plain_body<commit_msg>Remove the gzip Accept-Encoding header, as it causes blank responses from Xero<commit_after>module XeroGateway
  module Http
    OPEN_TIMEOUT = 10 unless defined? OPEN_TIMEOUT
    READ_TIMEOUT = 60 unless defined? READ_TIMEOUT
    ROOT_CA_FILE = File.join(File.dirname(__FILE__), 'ca-certificates.crt') unless defined? ROOT_CA_FILE
    
    def http_get(client, url, extra_params = {})
      http_request(client, :get, url, nil, extra_params)
    end

    def http_post(client, url, body, extra_params = {})
      http_request(client, :post, url, body, extra_params)
    end

    def http_put(client, url, body, extra_params = {})
      http_request(client, :put, url, body, extra_params)
    end
    
    private
    
      def http_request(client, method, url, body, params = {})
        # headers = {'Accept-Encoding' => 'gzip, deflate'}

        headers = {}

        if method != :get
         headers['Content-Type'] ||= "application/x-www-form-urlencoded"
        end
        
        # HAX.  Xero completely misuse the If-Modified-Since HTTP header.
        headers['If-Modified-Since'] = params.delete(:modified_since).utc.strftime("%Y-%m-%dT%H:%S") if params[:modified_since]

        params = params.map {|key,value| "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"}.join("&")

        uri   = URI.parse(url + "?" + params)

        # # Only setup @cached_http once on first use as loading the CA file is quite expensive computationally.
        # unless @cached_http && @cached_http.address == uri.host && @cached_http.port == uri.port
        #   @cached_http = Net::HTTP.new(uri.host, uri.port) 
        #   @cached_http.open_timeout = OPEN_TIMEOUT
        #   @cached_http.read_timeout = READ_TIMEOUT
        #   @cached_http.use_ssl      = true
        # 
        #   # Need to validate server's certificate against root certificate authority to prevent man-in-the-middle attacks.
        #   @cached_http.ca_file        = ROOT_CA_FILE
        #   # http.verify_mode    = OpenSSL::SSL::VERIFY_NONE
        #   @cached_http.verify_mode    = OpenSSL::SSL::VERIFY_PEER
        #   @cached_http.verify_depth   = 5
        # end
        
        case method
          when :get   then    client.get(uri.request_uri, headers).plain_body
{'fraction_non_alphanumeric': 0.11748695849398956, 'fraction_numerical': 0.0022680880018144706, 'mean_word_length': 2.675, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '3066905', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1372, 'n_words': 307}