Class: PatientHttp::Request

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

Overview

Represents an async HTTP request that will be processed by the async processor.

Examples:

Creating a request

request = PatientHttp::Request.new(:get, "https://api.example.com/users/123")

Creating a POST request with JSON body

request = PatientHttp::Request.new(
  :post,
  "https://api.example.com/users",
  json: {name: "John", email: "john@example.com"}
)

Constant Summary collapse

VALID_METHODS =

Valid HTTP methods

%i[get post put patch delete].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_method, url, headers: {}, body: nil, json: nil, params: nil, timeout: nil, max_redirects: nil, preprocessors: nil) ⇒ Request

Initializes a new Request.

Parameters:

  • http_method (Symbol, String)

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

  • url (String, URI::Generic)

    The request URL.

  • headers (Hash, HttpHeaders) (defaults to: {})

    Request headers.

  • body (String, nil) (defaults to: nil)

    Request body.

  • json (Object, nil) (defaults to: nil)

    JSON body to be serialized (alternative to body).

  • params (Hash, nil) (defaults to: nil)

    Query parameters to append to the URL.

  • timeout (Numeric, nil) (defaults to: nil)

    Overall timeout in seconds.

  • max_redirects (Integer, nil) (defaults to: nil)

    Maximum redirects to follow (nil uses config, 0 disables).

  • preprocessors (String, Symbol, Array<String, Symbol>, nil) (defaults to: nil)

    Names of preprocessors registered on the configuration to apply to the request when it is sent.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/patient_http/request.rb', line 94

def initialize(
  http_method,
  url,
  headers: {},
  body: nil,
  json: nil,
  params: nil,
  timeout: nil,
  max_redirects: nil,
  preprocessors: nil
)
  @http_method = http_method.is_a?(String) ? http_method.downcase.to_sym : http_method

  unless url.nil? || url.is_a?(String) || url.is_a?(URI::Generic)
    raise ArgumentError.new("url must be a String or URI, got: #{url.class}")
  end

  @secret_params = {}
  @url = normalized_url(url, params)
  # Copy the headers so the request does not share mutable state with the
  # caller (or with another request when following redirects).
  @headers = headers.is_a?(HttpHeaders) ? headers.dup : HttpHeaders.new(headers)
  @body = (body == "") ? nil : body
  @timeout = timeout
  @max_redirects = max_redirects
  @preprocessors = normalized_preprocessors(preprocessors)

  if json
    raise ArgumentError.new("Cannot provide both body and json") if @body

    @body = JSON.generate(json)
    @headers["content-type"] ||= "application/json; charset=utf-8"
  end

  validate!

  encoding, encoded_body, charset = Payload.encode(@body, @headers["content-type"])
  @payload = Payload.new(encoding, encoded_body, charset) unless @body.nil?
  @body = UNDEFINED
end

Instance Attribute Details

#headersHttpHeaders (readonly)

Returns Request headers.

Returns:



29
30
31
# File 'lib/patient_http/request.rb', line 29

def headers
  @headers
end

#http_methodSymbol (readonly)

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

Returns:

  • (Symbol)

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



23
24
25
# File 'lib/patient_http/request.rb', line 23

def http_method
  @http_method
end

#max_redirectsInteger? (readonly)

Returns Maximum number of redirects to follow (nil uses config default, 0 disables).

Returns:

  • (Integer, nil)

    Maximum number of redirects to follow (nil uses config default, 0 disables)



35
36
37
# File 'lib/patient_http/request.rb', line 35

def max_redirects
  @max_redirects
end

#preprocessorsArray<String> (readonly)

Returns Names of preprocessors registered on the configuration to apply to the request when it is sent.

Returns:

  • (Array<String>)

    Names of preprocessors registered on the configuration to apply to the request when it is sent



43
44
45
# File 'lib/patient_http/request.rb', line 43

def preprocessors
  @preprocessors
end

#secret_paramsHash{String, Symbol => SecretReference} (readonly)

Returns Query parameters whose values are secret references, kept out of the serialized URL and resolved at send time.

Returns:

  • (Hash{String, Symbol => SecretReference})

    Query parameters whose values are secret references, kept out of the serialized URL and resolved at send time



39
40
41
# File 'lib/patient_http/request.rb', line 39

def secret_params
  @secret_params
end

#timeoutNumeric? (readonly)

Returns Overall timeout in seconds.

Returns:

  • (Numeric, nil)

    Overall timeout in seconds



32
33
34
# File 'lib/patient_http/request.rb', line 32

def timeout
  @timeout
end

#urlString (readonly)

Returns The request URL.

Returns:

  • (String)

    The request URL



26
27
28
# File 'lib/patient_http/request.rb', line 26

def url
  @url
end

Class Method Details

.load(hash) ⇒ Request

Reconstruct a Request from a hash

Parameters:

  • hash (Hash)

    hash representation

Returns:

  • (Request)

    reconstructed request



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/patient_http/request.rb', line 50

def load(hash)
  new(
    hash["http_method"].to_sym,
    hash["url"],
    headers: load_headers(hash["headers"]),
    body: Payload.load(hash["body"])&.value,
    params: load_secret_params(hash["secret_params"]),
    timeout: hash["timeout"],
    max_redirects: hash["max_redirects"],
    preprocessors: hash["preprocessors"]
  )
end

Instance Method Details

#as_jsonHash

Serialize to JSON hash.

Returns:

  • (Hash)


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/patient_http/request.rb', line 146

def as_json
  hash = {
    "http_method" => @http_method.to_s,
    "url" => @url.to_s,
    "headers" => serialized_headers,
    "body" => @payload&.as_json,
    "timeout" => @timeout,
    "max_redirects" => @max_redirects
  }

  if @secret_params.any?
    hash["secret_params"] = @secret_params.transform_values(&:as_json)
  end

  hash["preprocessors"] = @preprocessors if @preprocessors.any?

  hash
end

#bodyString?

Returns the request body, decoding it from the payload if necessary.

Returns:

  • (String, nil)

    The decoded request body or nil if there was no body.



138
139
140
141
# File 'lib/patient_http/request.rb', line 138

def body
  @body = @payload&.value if @body.equal?(UNDEFINED)
  @body
end