<commit_before>require 'net/http'
require 'net/https'
require 'uri'
require 'rack/utils'
require 'json'

module Appsignal
  class Transmitter
    CONTENT_TYPE = 'application/json; charset=UTF-8'.freeze
    CONTENT_ENCODING = 'gzip'.freeze
    CA_FILE_PATH = File.expand_path(File.join(__FILE__, '../../../resources/cacert.pem'))

    HTTP_ERRORS = [
      EOFError,
      Errno::ECONNREFUSED,
      Errno::ECONNRESET,
      Errno::EINVAL,
      Net::HTTPBadResponse,
      Net::HTTPHeaderSyntaxError,
      Net::ProtocolError,
      Timeout::Error,
      OpenSSL::SSL::SSLError
    ]

    attr_reader :config, :action

    def initialize(action, config=Appsignal.config)
      @action = action
      @config = config
    end

    def uri
      @uri ||= URI("#{config[:endpoint]}/#{action}").tap do |uri|
        uri.query = ::Rack::Utils.build_query({
          :api_key => config[:push_api_key],
          :name => config[:name],
          :environment => config.env,
          :hostname => Socket.gethostname,
          :gem_version => Appsignal::VERSION
        })
      end
    end

    # Accepts a string or `Appsignal::ZippedPayload`
    # If no `Appsignal::ZippedPayload` is given, it will convert it to one.
    def transmit(payload)
      unless payload.is_a?(Appsignal::ZippedPayload)
        payload = Appsignal::ZippedPayload.new(payload)
      end
      Appsignal.logger.debug "Transmitting payload to #{uri}"
      http_client.request(http_post(payload)).code
    end

    protected

    def http_post(payload)
      Net::HTTP::Post.new(uri.request_uri).tap do |request|
        request['Content-Type'] = CONTENT_TYPE
        request['Content-Encoding'] = CONTENT_ENCODING
        request.body = payload.body
      end
    end

    def http_client
      Net::HTTP.new(uri.host, uri.port, proxy_addr, proxy_port).tap do |http|
        if uri.scheme == 'https'
          http.use_ssl     = true<commit_msg>Refactor proxy support<commit_after>require 'net/http'
require 'net/https'
require 'uri'
require 'rack/utils'
require 'json'

module Appsignal
  class Transmitter
    CONTENT_TYPE = 'application/json; charset=UTF-8'.freeze
    CONTENT_ENCODING = 'gzip'.freeze
    CA_FILE_PATH = File.expand_path(File.join(__FILE__, '../../../resources/cacert.pem'))

    HTTP_ERRORS = [
      EOFError,
      Errno::ECONNREFUSED,
      Errno::ECONNRESET,
      Errno::EINVAL,
      Net::HTTPBadResponse,
      Net::HTTPHeaderSyntaxError,
      Net::ProtocolError,
      Timeout::Error,
      OpenSSL::SSL::SSLError
    ]

    attr_reader :config, :action

    def initialize(action, config=Appsignal.config)
      @action = action
      @config = config
    end

    def uri
      @uri ||= URI("#{config[:endpoint]}/#{action}").tap do |uri|
        uri.query = ::Rack::Utils.build_query({
          :api_key => config[:push_api_key],
          :name => config[:name],
          :environment => config.env,
          :hostname => Socket.gethostname,
          :gem_version => Appsignal::VERSION
        })
      end
    end

    # Accepts a string or `Appsignal::ZippedPayload`
    # If no `Appsignal::ZippedPayload` is given, it will convert it to one.
    def transmit(payload)
      unless payload.is_a?(Appsignal::ZippedPayload)
        payload = Appsignal::ZippedPayload.new(payload)
      end
      Appsignal.logger.debug "Transmitting payload to #{uri}"
      http_client.request(http_post(payload)).code
    end

    protected

    def http_post(payload)
      Net::HTTP::Post.new(uri.request_uri).tap do |request|
        request['Content-Type'] = CONTENT_TYPE
        request['Content-Encoding'] = CONTENT_ENCODING
        request.body = payload.body
      end
    end

    def http_client
      client = if config[:http_proxy]
        Net::HTTP.new(uri.host, uri.port, proxy_addr, proxy_port)
      else
        Net::HTTP.new(uri.host, uri.port)
      end

      client.tap do |http|
        if uri.scheme == 'https'
          http.use_ssl     = true
{'fraction_non_alphanumeric': 0.12317011610297829, 'fraction_numerical': 0.0005047955577990914, 'mean_word_length': 2.683085501858736, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '1049306', 'n_tokens_mistral': 1268, 'n_tokens_neox': 1222, 'n_words': 249}