<commit_before>require File.dirname(__FILE__) + '/query'
require 'net/http'
require 'json'
require 'base64'
require 'mime/types'

module Request
  TYPE = {json: 'application/json',
          form: 'application/x-www-form-urlencoded',
          text: 'text/plain',
          html: 'text/html',
          xml: 'application/xml'}

  class << self
    def [](url)
      client = Client.new(url)
      block_given? ? yield(client) : client
    end
    alias_method :create, :[]
  end

  class Client
    attr_accessor :data, :files, :body, :headers
    attr_reader :client, :url

    def initialize(url)
      @data = {}
      @headers = {}
      @files = []
      @body = ''
      self.url = url
      @client = Net::HTTP.new(uri.hostname, uri.port)
    end

    def url= url
      # if not schema is given, default is `http`
      @url = (url['://'] ? '' : 'http://') << url
    end

    def auth user, pass
      # setup basic auth header
      set "Authorization" => "Basic #{Base64.encode64(user + ":" + pass).chop}"
    end

    def get
      update_uri
      client.get uri.request_uri, headers
    end

    def post
      build
      client.post(uri.request_uri, body, headers)
    end

    def put
      build
      client.put(uri.request_uri, body, headers)
    end

    def delete
    end

    def head
    end

    def send name, file=nil, filename=nil
      if file
        upload name, file, filename
      else
        query name
      end
    end

    def upload field, file, filename = nil
      file = File.open(file)
      @files << [field, file, filename || file.path]
      self
    end
    alias_method :attach, :upload

    def query option
      data.merge! option
      self
    end

    def header option
      headers.merge! option
      self
    end
    alias_method :set, :header

    def write body
      @body << body
      self
    end

    def type t
      # Set `Content-Type` header
      tp = t.to_sym
      t = TYPE[tp] if TYPE[tp]
      set "Content-Type" => t
    end

    def multi
      @multi = true
      self
    end

    private

    def build
      if not files.empty? or @multi
        build_multipart
      else
        build_body
      end
    end

    def build_body
      case headers['Content-Type']
      when nil, TYPE[:form] then write data.to_query
      when TYPE[:json] then write data.to_json
      end
    end

    def build_multipart
      m = Multipart.create(files, data)
      write m.body
      header m.header
    end

    def update_uri
      unless data.empty?
        url << "?" unless url["?"]
        url << data.to_query
        @uri = nil
      end
    end

    def uri
      @uri ||= URI(url).tap do |u|
        # If the url is something like this: http://user:password@localhost",
        # we setup the basic authorization header.<commit_msg>support ssl.<commit_after>require File.dirname(__FILE__) + '/query'
require 'net/http'
require 'json'
require 'base64'
require 'mime/types'

module Request
  TYPE = {json: 'application/json',
          form: 'application/x-www-form-urlencoded',
          text: 'text/plain',
          html: 'text/html',
          xml: 'application/xml'}

  class << self
    def [](url)
      client = Client.new(url)
      block_given? ? yield(client) : client
    end
    alias_method :create, :[]
  end

  class Client
    attr_accessor :data, :files, :body, :headers
    attr_reader :client, :url

    def initialize(url)
      @data = {}
      @headers = {}
      @files = []
      @body = ''
      self.url = url
      @client = Net::HTTP.new(uri.hostname, uri.port)
    end

    def use_ssl use=true
      client.use_ssl = use
      self
    end

    def url= url
      # if not schema is given, default is `http`
      @url = (url['://'] ? '' : 'http://') << url
    end

    def auth user, pass
      # setup basic auth header
      set "Authorization" => "Basic #{Base64.encode64(user + ":" + pass).chop}"
    end

    def get
      update_uri
      client.get uri.request_uri, headers
    end

    def post
      build
      client.post(uri.request_uri, body, headers)
    end

    def put
      build
      client.put(uri.request_uri, body, headers)
    end

    def delete
    end

    def head
    end

    def send name, file=nil, filename=nil
      if file
        upload name, file, filename
      else
        query name
      end
    end

    def upload field, file, filename = nil
      file = File.open(file)
      @files << [field, file, filename || file.path]
      self
    end
    alias_method :attach, :upload

    def query option
      data.merge! option
      self
    end

    def header option
      headers.merge! option
      self
    end
    alias_method :set, :header

    def write body
      @body << body
      self
    end

    def type t
      # Set `Content-Type` header
      tp = t.to_sym
      t = TYPE[tp] if TYPE[tp]
      set "Content-Type" => t
    end

    def multi
      @multi = true
      self
    end

    private

    def build
      if not files.empty? or @multi
        build_multipart
      else
        build_body
      end
    end

    def build_header
    end

    def build_body
      case headers['Content-Type']
      when nil, TYPE[:form] then write data.to_query
      when TYPE[:json] then write data.to_json
      end
    end

    def build_multipart
      m = Multipart.create(files, data)
      write m.body
      header m.header
    end

    def update_uri
      unless data.empty?
        url << "?" unless url["?"]
        url << data.to_query
        @uri = nil
      end
    end

    def uri
      @uri ||= URI(url).tap do |u|
        # If the url is something like this: http://user:password@localhost",
        # we setup the basic authorization header.
{'fraction_non_alphanumeric': 0.10092544089401083, 'fraction_numerical': 0.0020953378732320588, 'mean_word_length': 1.8314384577360356, 'pattern_counts': {'":': 2, '<': 27, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, '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': '25471', 'n_tokens_mistral': 1811, 'n_tokens_neox': 1752, 'n_words': 548}