<commit_before>require 'ticket_sharing/error'
require 'ticket_sharing/request'

module TicketSharing
  class Client
    def initialize(base_url, credentials=nil)
      @base_url    = base_url
      @credentials = credentials
    end

    def post(path, body, options={})
      send_request(:post, path, body, options)
    end

    def put(path, body, options={})
      send_request(:put, path, body, options)
    end

    def delete(path, options={})
      send_request(:delete, path, '', options)
    end

    def success?
      @success
    end

    private

    def send_request(method, path, body, options)
      headers = {'X-Ticket-Sharing-Token' => @credentials} if @credentials
      options = options.merge(:body => body, :headers => headers)
      response = TicketSharing::Request.new.request(method, @base_url + path, options)

      handle_response(response)
    end

    def handle_response(response)
      @success = case response.code.to_i
      when (200..299)
        true
      when 403, 404, 405, 408, 410, 422, 500..599
         false
      else
        raise TicketSharing::Error.new(%Q{#{response.code} "#{response.message}"\n\n#{response.body}})
      end
      response
    end
  end
end<commit_msg>treat 401 like a normal failure<commit_after>require 'ticket_sharing/error'
require 'ticket_sharing/request'

module TicketSharing
  class Client
    def initialize(base_url, credentials=nil)
      @base_url    = base_url
      @credentials = credentials
    end

    def post(path, body, options={})
      send_request(:post, path, body, options)
    end

    def put(path, body, options={})
      send_request(:put, path, body, options)
    end

    def delete(path, options={})
      send_request(:delete, path, '', options)
    end

    def success?
      @success
    end

    private

    def send_request(method, path, body, options)
      headers = {'X-Ticket-Sharing-Token' => @credentials} if @credentials
      options = options.merge(:body => body, :headers => headers)
      response = TicketSharing::Request.new.request(method, @base_url + path, options)

      handle_response(response)
    end

    def handle_response(response)
      @success = case response.code.to_i
      when (200..299)
        true
      when 401, 403, 404, 405, 408, 410, 422, 500..599
         false
      else
        raise TicketSharing::Error.new(%Q{#{response.code} "#{response.message}"\n\n#{response.body}})
      end
      response
    end
  end
end
{'fraction_non_alphanumeric': 0.11533791987049777, 'fraction_numerical': 0.026709834075273168, 'mean_word_length': 2.8089368258859784, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '5497665', 'n_tokens_mistral': 827, 'n_tokens_neox': 745, 'n_words': 193}