<commit_before>require 'httparty'
class Communicator::Client
  class ServerError < StandardError; end;
  class AuthError < StandardError; end;
  class InvalidStartingId < StandardError; end;

  include HTTParty
  default_timeout 10
  
  class << self
    attr_writer :username, :password
    # Return configured username for http auth basic or raise an error message if not configured
    def username
      @username || raise(Communicator::MissingCredentials.new("No Username specified for HTTP AUTH. Please configure using Communicator::Client.username='xyz'"))
    end

    # Return configured password for http auth basic or raise an error message if not configured    
    def password
      @password || raise(Communicator::MissingCredentials.new("No Password specified for HTTP AUTH. Please configure using Communicator::Client.password='<PASSWORD>'"))
    end
    
    # Helper for basic auth in httparty-expected format for request options
    def credentials
      {:username => username, :password => password}
    end
    
    def pull
      request = get('/messages.json', :query => {:from_id => Communicator::InboundMessage.last_id+1}, :basic_auth => credentials)
      verify_response request.response
      Communicator::InboundMessage.create_from_json_collection!(JSON.parse(request.parsed_response))
      request
      
    # Customize error message on HTTP Timeout
    rescue Timeout::Error => err
      err.message << " - Failed to PULL from #{base_uri}"
      raise err
    end
  
    def push(from_id=nil)
      messages = Communicator::OutboundMessage.delivery_collection(from_id)
      request = post("/messages.json", :body => messages.map(&:payload).to_json, :basic_auth => credentials)
      verify_response request.response
      # Everything went fine? Mark the messages as delivered
      messages.each {|m| m.delivered! }
      request
    
    # Retry when server sent a from_id expectation
    rescue Communicator::Client::InvalidStartingId => err
      unless from_id
        push(request.response.body)
      else
        raise "Could not agree upon from_id with server!"
      end
      
    # Customize error message on HTTP Timeout
    rescue Timeout::Error => err
      err.message << " - Failed to PUSH to #{base_uri}"
      raise err
    end
    
    def verify_response(response)
      if response.kind_of?(Net::HTTPSuccess)
        return true
      elsif response.kind_of?(Net::HTTPUnauthorized) or response.kind_of?(Net::HTTPForbidden)
        raise Communicator::Client::AuthError.new("Failed to authenticate!")
      elsif response.kind_of?(Net::HTTPConflict)
        raise Communicator::Client::InvalidStartingId.new("Expected from_id to begin with #{response.body.strip.chomp}")
      elsif response.kind_of?(Net::HTTPServerError)
        raise Communicator::Client::ServerError.new("Request failed with #{response.class}")
      end
    end
  end

end
<commit_msg>DAMN YOU, frozen exception message...<commit_after>require 'httparty'
class Communicator::Client
  class ServerError < StandardError; end;
  class AuthError < StandardError; end;
  class InvalidStartingId < StandardError; end;

  include HTTParty
  default_timeout 10
  
  class << self
    attr_writer :username, :password
    # Return configured username for http auth basic or raise an error message if not configured
    def username
      @username || raise(Communicator::MissingCredentials.new("No Username specified for HTTP AUTH. Please configure using Communicator::Client.username='xyz'"))
    end

    # Return configured password for http auth basic or raise an error message if not configured    
    def password
      @password || raise(Communicator::MissingCredentials.new("No Password specified for HTTP AUTH. Please configure using Communicator::Client.password='<PASSWORD>'"))
    end
    
    # Helper for basic auth in httparty-expected format for request options
    def credentials
      {:username => username, :password => password}
    end
    
    def pull
      request = get('/messages.json', :query => {:from_id => Communicator::InboundMessage.last_id+1}, :basic_auth => credentials)
      verify_response request.response
      Communicator::InboundMessage.create_from_json_collection!(JSON.parse(request.parsed_response))
      request
      
    # Customize error message on HTTP Timeout
    rescue Timeout::Error => err
      err = err.dup
      err.message << " - Failed to PULL from #{base_uri}"
      raise err
    end
  
    def push(from_id=nil)
      messages = Communicator::OutboundMessage.delivery_collection(from_id)
      request = post("/messages.json", :body => messages.map(&:payload).to_json, :basic_auth => credentials)
      verify_response request.response
      # Everything went fine? Mark the messages as delivered
      messages.each {|m| m.delivered! }
      request
    
    # Retry when server sent a from_id expectation
    rescue Communicator::Client::InvalidStartingId => err
      unless from_id
        push(request.response.body)
      else
        raise "Could not agree upon from_id with server!"
      end
      
    # Customize error message on HTTP Timeout
    rescue Timeout::Error => err
      err = err.dup
      err.message << " - Failed to PUSH to #{base_uri}"
      raise err
    end
    
    def verify_response(response)
      if response.kind_of?(Net::HTTPSuccess)
        return true
      elsif response.kind_of?(Net::HTTPUnauthorized) or response.kind_of?(Net::HTTPForbidden)
        raise Communicator::Client::AuthError.new("Failed to authenticate!")
      elsif response.kind_of?(Net::HTTPConflict)
        raise Communicator::Client::InvalidStartingId.new("Expected from_id to begin with #{response.body.strip.chomp}")
      elsif response.kind_of?(Net::HTTPServerError)
        raise Communicator::Client::ServerError.new("Request failed with #{response.class}")
      end
    end
  end

end

{'fraction_non_alphanumeric': 0.08992195453003055, 'fraction_numerical': 0.0010179843909060061, 'mean_word_length': 3.4390060240963853, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, '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': '2729978', 'n_tokens_mistral': 1577, 'n_tokens_neox': 1576, 'n_words': 468}