<commit_before>require "rack/passthrough/version"
require "httparty"

module Rack
  module Passthrough

    class Endpoint
      def initialize( endpoint )
        raise "Required api endpoint path." if endpoint.blank?
        @endpoint = endpoint
      end

      def call(env)
        request = Rack::Request.new(env)
        full_path = URI::join( @endpoint, request.path )
        # Set Query String
        full_path.query = request.query_string
        data = request.body.read
        body_data = JSON.parse( data ) unless data.blank?
        conn = HTTParty.method( request.request_method.downcase.to_sym )

        # Request to endpoint
        response = conn.call( full_path.to_s, { body: body_data } )

        [ response.code , response.headers , [response.body] ]
      end
    end
    
  end
end<commit_msg>fix headers response<commit_after>require "rack/passthrough/version"
require "httparty"

module Rack
  module Passthrough

    class Endpoint
      def initialize( endpoint )
        raise "Required api endpoint path." if endpoint.blank?
        @endpoint = endpoint
      end

      def to_normal_string(data)
        if data.class == Array
          data = data.join
        else
          data
        end

        data
      end

      def call(env)
        request = Rack::Request.new(env)
        full_path = URI::join( @endpoint, request.path )
        # Set Query String
        full_path.query = request.query_string
        data = request.body.read
        # body_data = JSON.parse( data ) unless data.blank?
        # body_data = Rack::Utils.parse_query( data ) unless data.blank?
        conn = HTTParty.method( request.request_method.downcase.to_sym )

        # Request to endpoint
        response = conn.call( full_path.to_s, { body: env["action_dispatch.request.request_parameters"] } )
        headers = {}

        response.headers.each do |key,value|
          headers.merge!({ key => to_normal_string(value) })

        end

        [ response.code , headers , [response.body] ]
      end
    end
    
  end
end
{'fraction_non_alphanumeric': 0.09055501460564752, 'fraction_numerical': 0.0, 'mean_word_length': 2.190993788819876, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '1169380', 'n_tokens_mistral': 602, 'n_tokens_neox': 587, 'n_words': 148}