Module: Outhad::Integrations::Core::HttpHelper

Included in:
HttpClient, StreamingHttpClient
Defined in:
lib/outhad/integrations/core/http_helper.rb

Instance Method Summary collapse

Instance Method Details

#build_request(method, uri, payload, headers) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/outhad/integrations/core/http_helper.rb', line 6

def build_request(method, uri, payload, headers)
  request_class = case method.upcase
                  when Constants::HTTP_GET then Net::HTTP::Get
                  when Constants::HTTP_POST then Net::HTTP::Post
                  when Constants::HTTP_PUT then Net::HTTP::Put
                  when Constants::HTTP_PATCH then Net::HTTP::Patch
                  when Constants::HTTP_DELETE then Net::HTTP::Delete
                  else raise ArgumentError, "Unsupported HTTP method: #{method}"
                  end

  request = request_class.new(uri)
  headers.each { |key, value| request[key] = value }
  request.body = payload.to_json if payload && %w[POST PUT PATCH].include?(method.upcase)
  request
end

#configure_http(uri, config) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/outhad/integrations/core/http_helper.rb', line 22

def configure_http(uri, config)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.scheme == "https")

  if config[:timeout]
    timeout_value = config[:timeout].to_f
    http.open_timeout = timeout_value
    http.read_timeout = timeout_value
  end

  http
end