<commit_before>module CursedConsole

  class WebServiceClient

    attr_reader :host, :ssl, :api_token, :trace

    def initialize(host, ssl, api_token, trace=false)
      @host = host
      @ssl = ssl
      @api_token = api_token
      @trace = trace
    end

    def get(uri, is_json=true)
      invoke_rest(is_json) do
        result = RestClient.get(build_url(uri),
                                authentication_headers)
      end
    end

    def delete(uri, is_json=true)
      invoke_rest(is_json) do
        result = RestClient.delete(build_url(uri),
                                  authentication_headers)
      end
    end

    def post(uri, parameters, is_json=true)
      invoke_rest(is_json) do
        result = RestClient.post(build_url(uri),
                                parameters,
                                authentication_headers)
      end
    end

    def put(uri, parameters, is_json=true)
      invoke_rest(is_json) do
        result = RestClient.put(build_url(uri),
                                parameters,
                                authentication_headers)
      end
    end

    private

    def invoke_rest(is_json=true)
      begin
        response = yield
        begin
          is_json ? JSON.parse(response) : response
        rescue Exception => ex
          puts "Error parsing response: #{ex.message[0..100]}"
          if trace 
            puts "Full Error message: #{ex.message}"
            puts "\t#{ex.backtrace.join("\n\t")}"
          end
          exit 1
        end
      rescue RestClient::Exception => ex
        puts "Response: #{ex.response.code} - #{ex.response.description}"
        if [ 403, 404 ].include?(ex.response.code)
          if is_json
            begin
              hash = JSON.parse(ex.response.http_body)
              if hash.has_key?('error')
                puts(hash['error'])
              elsif hash.has_key?('validation_error')
                puts "Validation errors:"
                hash['validation_error'].each_pair do | key, value |
                  puts "  #{key}"
                  puts "    #{value.join("\n    ")}"
                end
              else
                puts(hash.inspect)
              end
            rescue Exception => json_ex
              puts "Unable to parse body for error: #{ex.response.http_body[0..50]}"
            end
          end
        end
        exit 1
      rescue SystemExit
        raise
      rescue Exception => ex
        # General unknown exception
        puts "Error invoking RestClient: #{ex.message}"
        puts "\t#{ex.backtrace.join("\n\t")}" if trace
        exit 1<commit_msg>Raising exceptions rather than quitting.<commit_after>module CursedConsole

  class WebServiceClient

    attr_reader :host, :ssl, :api_token, :trace

    def initialize(host, ssl, api_token, trace=false)
      @host = host
      @ssl = ssl
      @api_token = api_token
      @trace = trace
    end

    def get(uri, is_json=true)
      invoke_rest(is_json) do
        result = RestClient.get(build_url(uri),
                                authentication_headers)
      end
    end

    def delete(uri, is_json=true)
      invoke_rest(is_json) do
        result = RestClient.delete(build_url(uri),
                                  authentication_headers)
      end
    end

    def post(uri, parameters, is_json=true)
      invoke_rest(is_json) do
        result = RestClient.post(build_url(uri),
                                parameters,
                                authentication_headers)
      end
    end

    def put(uri, parameters, is_json=true)
      invoke_rest(is_json) do
        result = RestClient.put(build_url(uri),
                                parameters,
                                authentication_headers)
      end
    end

    private

    def invoke_rest(is_json=true)
      begin
        response = yield
        begin
          is_json ? JSON.parse(response) : response
        rescue Exception => ex
          puts "Error parsing response: #{ex.message[0..100]}"
          if trace 
            puts "Full Error message: #{ex.message}"
            puts "\t#{ex.backtrace.join("\n\t")}"
          end
          # exit 1
          raise "Error parsing response: #{ex.message[0..100]}"
        end
      rescue RestClient::Exception => ex
        puts "Response: #{ex.response.code} - #{ex.response.description}"
        if [ 403, 404 ].include?(ex.response.code)
          if is_json
            begin
              hash = JSON.parse(ex.response.http_body)
              if hash.has_key?('error')
                puts(hash['error'])
              elsif hash.has_key?('validation_error')
                puts "Validation errors:"
                hash['validation_error'].each_pair do | key, value |
                  puts "  #{key}"
                  puts "    #{value.join("\n    ")}"
                end
              else
                puts(hash.inspect)
              end
            rescue Exception => json_ex
              raise "Unable to parse body for error: #{ex.response.http_body[0..50]}"
            end
          end
        end
        # exit 1
      rescue SystemExit
        raise
      rescue Exception => ex
        # General unknown exception
        raise "Error invoking RestClient: #{ex.message}"
        # puts "\t#{ex.backtrace.join("\n\t")}" if trace
        # exit 1
{'fraction_non_alphanumeric': 0.09323140033563304, 'fraction_numerical': 0.006712660824165579, 'mean_word_length': 1.4948837209302326, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5382575', 'n_tokens_mistral': 1497, 'n_tokens_neox': 1463, 'n_words': 348}