<commit_before>require 'fluent/output'
require 'fluent/mixin'

require 'uri'
require 'httpclient'

module Fluent
  class FileBufferChunk < BufferChunk
    # to pass file object to httpclient for multipart upload
    def to_io
      @file
    end
  end

  class HttpFileUploadOutput < BufferedOutput
    Plugin.register_output('http_file_output', self)

    include Fluent::SetTimeKeyMixin

    config_set_default :buffer_type, "file"

    config_param :uri, :string, desc: "Full URI for http upload endpoint for POST requests"
    config_param :param_name, :string, default: "file", desc: "Parameter name which contains uploaded file content"
    config_param :user_agent, :string, default: "fluent-plugin-http_file_upload", desc: "User-Agent header content"
    config_param :headers,    :hash, default: {}, desc: "Additional header fields for requests"
    config_param :parameters, :hash, default: {}, desc: "Additional form parameters (key-value pairs) for requests"

    desc "Filename in upload requests, formats for strftime available"
    config_param :filename, :string, default: "data.%Y-%m-%d-%M-%H-%S"

    config_param :format,   :string, default: "json", desc: "How to format records in uploaded files"

    # TODO: support compression
    # TODO: support gzipped transferring

    def configure(conf)
      super

      @formatter = Plugin.new_formatter(@format)
      @formatter.configure(conf)
      @client = HTTPClient.new(agent_name: @user_agent)
      # @client.debug_dev = $stderr
    end

    def format(tag, time, record)
      @formatter.format(tag, time, record)
    end

    def write(chunk)
      filename = Time.now.strftime(@filename)
      chunk.open do |io|
        io.singleton_class.class_eval{ define_method(:path){ filename } }
        postdata = { @param_name => io }
        unless @parameters.empty?
          postdata = @parameters.merge(postdata)
        end
        @client.post(@uri, postdata, @headers)
      end
    end
  end
end<commit_msg>fix to set headers as client instance default<commit_after>require 'fluent/output'
require 'fluent/mixin'

require 'uri'
require 'httpclient'

module Fluent
  class FileBufferChunk < BufferChunk
    # to pass file object to httpclient for multipart upload
    def to_io
      @file
    end
  end

  class HttpFileUploadOutput < BufferedOutput
    Plugin.register_output('http_file_output', self)

    include Fluent::SetTimeKeyMixin

    config_set_default :buffer_type, "file"

    config_param :uri, :string, desc: "Full URI for http upload endpoint for POST requests"
    config_param :param_name, :string, default: "file", desc: "Parameter name which contains uploaded file content"
    config_param :user_agent, :string, default: "fluent-plugin-http_file_upload", desc: "User-Agent header content"
    config_param :headers,    :hash, default: {}, desc: "Additional header fields for requests"
    config_param :parameters, :hash, default: {}, desc: "Additional form parameters (key-value pairs) for requests"

    desc "Filename in upload requests, formats for strftime available"
    config_param :filename, :string, default: "data.%Y-%m-%d-%M-%H-%S"

    config_param :format,   :string, default: "json", desc: "How to format records in uploaded files"

    # TODO: support compression
    # TODO: support gzipped transferring

    def configure(conf)
      super

      @formatter = Plugin.new_formatter(@format)
      @formatter.configure(conf)
      @client = HTTPClient.new(agent_name: @user_agent, default_header: @headers)
      # @client.debug_dev = $stderr
    end

    def format(tag, time, record)
      @formatter.format(tag, time, record)
    end

    def write(chunk)
      filename = Time.now.strftime(@filename)
      chunk.open do |io|
        io.singleton_class.class_eval{ define_method(:path){ filename } }
        postdata = { @param_name => io }
        unless @parameters.empty?
          postdata = @parameters.merge(postdata)
        end
        @client.post(@uri, postdata)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09925373134328358, 'fraction_numerical': 0.0, 'mean_word_length': 3.51290684624018, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '7577014', 'n_tokens_mistral': 1204, 'n_tokens_neox': 1151, 'n_words': 353}