<commit_before>require 'uri'
require 'net/http'
require 'json'

module MrMurano
  module Http
    def token
      return @token unless @token.nil?
      @token = Account.new.token
      raise "Not logged in!" if @token.nil?
      @token
    end

    def json_opts
      return @json_opts unless @json_opts.nil?
      @json_opts = {
        :allow_nan => true,
        :symbolize_names => true,
        :create_additions => false
      }
    end

    def curldebug(request)
      if $cfg['tool.curldebug'] then
        a = []
        a << %{curl -s }
        if request.key?('Authorization') then
          a << %{-H 'Authorization: #{request['Authorization']}'}
        end
        a << %{-H 'User-Agent: #{request['User-Agent']}'}
        a << %{-H 'Content-Type: #{request.content_type}'}
        a << %{-X #{request.method}}
        a << %{'#{request.uri.to_s}'}
        a << %{-d '#{request.body}'} unless request.body.nil?
        puts a.join(' ')
      end
    end

    def http
      uri = URI('https://' + $cfg['net.host'])
      if @http.nil? then
        @http = Net::HTTP.new(uri.host, uri.port)
        @http.use_ssl = true
        @http.start
      end
      @http
    end
    def http_reset
      @http = nil
    end

    def set_def_headers(request)
      request.content_type = 'application/json'
      request['Authorization'] = 'token ' + token
      request['User-Agent'] = "MrMurano/#{MrMurano::VERSION}"
      request
    end

    def workit(request, &block)
      curldebug(request)
      if block_given? then
        return yield request, http()
      else
        response = http().request(request)
        case response
        when Net::HTTPSuccess
          return {} if response.body.nil?
          begin
            return JSON.parse(response.body, json_opts)
          rescue
            return response.body
          end
        else
          say_error "got #{response} from #{request} #{request.uri.to_s}"
          say_error ":: #{response.body}"
          say_error '==='
          raise response
        end
      end
    end

    def get(path='', &block)
      uri = endPoint(path)
      workit(set_def_headers(Net::HTTP::Get.new(uri)), &block)
    end

    def post(path='', body={}, &block)
      uri = endPoint(path)
      req = Net::HTTP::Post.new(uri)
      set_def_headers(req)
      req.body = JSON.generate(body)
      workit(req, &block)
    end

    def postf(path='', form={}, &block)
      uri = endPoint(path)
      req = Net::HTTP::Post.new(uri)
      set_def_headers(req)
      req.content_type = 'application/x-www-form-urlencoded; charset=utf-8'
      req.form_data = form
      workit(req, &block)<commit_msg>Starting on some ideas for better errors.<commit_after>require 'uri'
require 'net/http'
require 'json'

module MrMurano
  module Http
    def token
      return @token unless @token.nil?
      @token = Account.new.token
      raise "Not logged in!" if @token.nil?
      @token
    end

    def json_opts
      return @json_opts unless @json_opts.nil?
      @json_opts = {
        :allow_nan => true,
        :symbolize_names => true,
        :create_additions => false
      }
    end

    def curldebug(request)
      if $cfg['tool.curldebug'] then
        a = []
        a << %{curl -s }
        if request.key?('Authorization') then
          a << %{-H 'Authorization: #{request['Authorization']}'}
        end
        a << %{-H 'User-Agent: #{request['User-Agent']}'}
        a << %{-H 'Content-Type: #{request.content_type}'}
        a << %{-X #{request.method}}
        a << %{'#{request.uri.to_s}'}
        a << %{-d '#{request.body}'} unless request.body.nil?
        puts a.join(' ')
      end
    end

    def http
      uri = URI('https://' + $cfg['net.host'])
      if @http.nil? then
        @http = Net::HTTP.new(uri.host, uri.port)
        @http.use_ssl = true
        @http.start
      end
      @http
    end
    def http_reset
      @http = nil
    end

    def set_def_headers(request)
      request.content_type = 'application/json'
      request['Authorization'] = 'token ' + token
      request['User-Agent'] = "MrMurano/#{MrMurano::VERSION}"
      request
    end

    def tryToPrettyJSON(data)
      begin
        ret = JSON.parse(data, json_opts)
        return JSON.pretty_generate(ret)
      rescue
        return response.body
      end
    end

    def workit(request, &block)
      curldebug(request)
      if block_given? then
        return yield request, http()
      else
        response = http().request(request)
        case response
        when Net::HTTPSuccess
          return {} if response.body.nil?
          begin
            return JSON.parse(response.body, json_opts)
          rescue
            return response.body
          end
        else
          if $cfg['tool.debug'] then
            say_error "From #{request.method} #{request.uri.to_s}"
            request.each_capitalized{|k,v| say_error "  #{k}: #{v}"}
            # ?? body?
            say_error "got #{response.code} #{response.message}"
            response.each_capitalized{|k,v| say_error "  #{k}: #{v}"}
          end
          say_error "Request Failed: #{response.code}: #{tryToPrettyJSON(response.body)}"
          raise response
        end
      end
    end

    def get(path='', &block)
      uri = endPoint(path)
      workit(set_def_headers(Net::HTTP::Get.new(uri)), &block)
    end

    def post(path='', body={}, &block)
      uri = endPoint(path)
      req = Net::HTTP::Post.new(uri)
      set_def_headers(req)
      req.body = JSON.generate(body)
      workit(req, &block)
    end

    def postf(path='', form={}, &block)
      uri = endPoint(path)
      req = Net::HTTP::Post.new(uri)
      set_def_headers(req)
      req.content_type = 'application/x-www-form-urlencoded; charset=utf-8'
      req.form_data = form
      workit(req, &block)
{'fraction_non_alphanumeric': 0.13859859516875106, 'fraction_numerical': 0.00034264176803152307, 'mean_word_length': 2.0374609781477626, 'pattern_counts': {'":': 1, '<': 31, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '6677597', 'n_tokens_mistral': 1866, 'n_tokens_neox': 1851, 'n_words': 423}