Class: PatientHttp::OutgoingRequest
- Inherits:
-
Object
- Object
- PatientHttp::OutgoingRequest
- 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
-
#body ⇒ String?
readonly
The request body.
-
#headers ⇒ HttpHeaders
readonly
Mutable, case-insensitive request headers.
-
#http_method ⇒ Symbol
readonly
HTTP method (:get, :post, :put, :patch, :delete).
-
#url ⇒ String
readonly
The request URL with any secret query params already resolved.
Instance Method Summary collapse
-
#add_param(name, value) ⇒ String
Append a query parameter to the request URL.
-
#initialize(http_method:, url:, headers:, body:) ⇒ OutgoingRequest
constructor
Initialize a new OutgoingRequest.
-
#inspect ⇒ String
Inspect the outgoing request.
Constructor Details
#initialize(http_method:, url:, headers:, body:) ⇒ OutgoingRequest
Initialize a new OutgoingRequest.
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
#body ⇒ String? (readonly)
Returns the request body.
22 23 24 |
# File 'lib/patient_http/outgoing_request.rb', line 22 def body @body end |
#headers ⇒ HttpHeaders (readonly)
Returns mutable, case-insensitive request headers.
25 26 27 |
# File 'lib/patient_http/outgoing_request.rb', line 25 def headers @headers end |
#http_method ⇒ Symbol (readonly)
Returns 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 |
#url ⇒ String (readonly)
Returns 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.
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 |
#inspect ⇒ String
Inspect the outgoing request. Header values, the query string, and the body are not shown since they may contain resolved secrets.
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 |