Class: Api2Convert::Http::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/api2convert/http/request.rb

Overview

A transport-agnostic HTTP request. The Transport builds these and hands them to an HttpSender; a retry rebuilds a fresh one so a seekable body is replayed from the start.

Exactly one of #body (a String) or #body_stream (an IO whose byte length is #content_length) is set on a request with a payload.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method:, url:, headers: {}, body: nil, body_stream: nil, content_length: nil, follow_redirects: false, response_sink: nil) ⇒ Request

Returns a new instance of Request.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/api2convert/http/request.rb', line 18

def initialize(method:, url:, headers: {}, body: nil, body_stream: nil,
               content_length: nil, follow_redirects: false, response_sink: nil)
  @method = method.to_s.upcase
  @url = url
  @headers = headers
  @body = body
  @body_stream = body_stream
  @content_length = content_length
  @follow_redirects = follow_redirects
  # An IO-like object (responds to `write`) to stream a successful download
  # body into, instead of buffering it whole. nil for a normal request.
  @response_sink = response_sink
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



12
13
14
# File 'lib/api2convert/http/request.rb', line 12

def body
  @body
end

#body_streamObject (readonly)

Returns the value of attribute body_stream.



12
13
14
# File 'lib/api2convert/http/request.rb', line 12

def body_stream
  @body_stream
end

#content_lengthObject (readonly)

Returns the value of attribute content_length.



12
13
14
# File 'lib/api2convert/http/request.rb', line 12

def content_length
  @content_length
end

#follow_redirectsObject

Whether the sender may follow a 3xx redirect. Set by the transport per request: authenticated requests carry a secret in a custom X-Oc-* header, so they must NOT follow redirects (the header could leak to another host).



16
17
18
# File 'lib/api2convert/http/request.rb', line 16

def follow_redirects
  @follow_redirects
end

#headersObject (readonly)

Returns the value of attribute headers.



12
13
14
# File 'lib/api2convert/http/request.rb', line 12

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



12
13
14
# File 'lib/api2convert/http/request.rb', line 12

def method
  @method
end

#response_sinkObject (readonly)

Returns the value of attribute response_sink.



12
13
14
# File 'lib/api2convert/http/request.rb', line 12

def response_sink
  @response_sink
end

#urlObject (readonly)

Returns the value of attribute url.



12
13
14
# File 'lib/api2convert/http/request.rb', line 12

def url
  @url
end

Instance Method Details

#inspectObject

Redacted representation — #headers carries the raw X-Oc-Api-Key / X-Oc-Download-Password secrets, so the default #inspect would print them in cleartext in a log line or a backtrace. Mask every X-Oc-* value.



35
36
37
38
39
40
# File 'lib/api2convert/http/request.rb', line 35

def inspect
  safe = @headers.to_h do |key, value|
    [key, key.to_s.downcase.start_with?("x-oc-") ? Support::Secret.mask(value) : value]
  end
  "#<#{self.class.name} method=#{@method.inspect} url=#{@url.inspect} headers=#{safe.inspect}>"
end

#to_sObject



42
43
44
# File 'lib/api2convert/http/request.rb', line 42

def to_s
  inspect
end