Class: PatientHttp::OutgoingRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/patient_http/outgoing_request.rb

Overview

A mutable view of a request as it is about to be sent, after secret references have been resolved and the send-time headers (x-request-id and the default user-agent) have been set.

Preprocessors attached to a request receive this object and can modify the headers or append query parameters before the request goes out -- for example, to sign the request. The HTTP method, URL, and body are read-only; headers can be changed in place and query parameters appended with #add_param.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_method:, url:, headers:, body:) ⇒ OutgoingRequest

Initialize a new OutgoingRequest.

Parameters:

  • http_method (Symbol)

    the HTTP method

  • url (String)

    the resolved request URL

  • headers (HttpHeaders)

    the resolved request headers

  • body (String, nil)

    the request body



33
34
35
36
37
38
# File 'lib/patient_http/outgoing_request.rb', line 33

def initialize(http_method:, url:, headers:, body:)
  @http_method = http_method
  @url = url.to_s
  @headers = headers
  @body = body
end

Instance Attribute Details

#bodyString? (readonly)

Returns the request body.

Returns:

  • (String, nil)

    the request body



22
23
24
# File 'lib/patient_http/outgoing_request.rb', line 22

def body
  @body
end

#headersHttpHeaders (readonly)

Returns mutable, case-insensitive request headers.

Returns:

  • (HttpHeaders)

    mutable, case-insensitive request headers



25
26
27
# File 'lib/patient_http/outgoing_request.rb', line 25

def headers
  @headers
end

#http_methodSymbol (readonly)

Returns HTTP method (:get, :post, :put, :patch, :delete).

Returns:

  • (Symbol)

    HTTP method (:get, :post, :put, :patch, :delete)



16
17
18
# File 'lib/patient_http/outgoing_request.rb', line 16

def http_method
  @http_method
end

#urlString (readonly)

Returns the request URL with any secret query params already resolved.

Returns:

  • (String)

    the request URL with any secret query params already resolved



19
20
21
# File 'lib/patient_http/outgoing_request.rb', line 19

def url
  @url
end

Instance Method Details

#add_param(name, value) ⇒ String

Append a query parameter to the request URL.

Parameters:

  • name (String, Symbol)

    the parameter name

  • value (Object)

    the parameter value

Returns:

  • (String)

    the updated URL



45
46
47
48
49
50
# File 'lib/patient_http/outgoing_request.rb', line 45

def add_param(name, value)
  serialized_param = URI.encode_www_form([[name.to_s, value]])
  uri = URI(@url)
  uri.query = [uri.query, serialized_param].compact.reject(&:empty?).join("&")
  @url = uri.to_s
end

#inspectString

Inspect the outgoing request. Header values, the query string, and the body are not shown since they may contain resolved secrets.

Returns:

  • (String)


56
57
58
# File 'lib/patient_http/outgoing_request.rb', line 56

def inspect
  "#<#{self.class.name} #{http_method.to_s.upcase} #{redacted_url} headers=#{headers.to_h.keys.inspect}>"
end