Class: Enconvert::Internal::Transport

Inherits:
Object
  • Object
show all
Defined in:
lib/enconvert/internal.rb

Overview

Authenticated, timeout-wrapped Net::HTTP wrapper bound to the client's base URL. Always sends the X-API-Key header (the presigned-URL download path in Client deliberately bypasses this class).

Constant Summary collapse

REQUEST_CLASSES =
{
  get: Net::HTTP::Get,
  post: Net::HTTP::Post,
  patch: Net::HTTP::Patch,
  delete: Net::HTTP::Delete
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url:, timeout:) ⇒ Transport

Returns a new instance of Transport.



29
30
31
32
33
# File 'lib/enconvert/internal.rb', line 29

def initialize(api_key:, base_url:, timeout:)
  @api_key = api_key
  @base_url = base_url
  @timeout = timeout
end

Instance Method Details

#request(method, path, headers: {}, body: nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/enconvert/internal.rb', line 35

def request(method, path, headers: {}, body: nil)
  uri = URI("#{@base_url}#{path}")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  http.open_timeout = @timeout
  http.read_timeout = @timeout

  request_class = REQUEST_CLASSES.fetch(method) do
    raise ArgumentError, "Unsupported HTTP method: #{method}"
  end
  req = request_class.new(uri)
  req["X-API-Key"] = @api_key
  headers.each { |key, value| req[key] = value }
  req.body = body unless body.nil?

  http.request(req)
end