Class: Parse::Request

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

Overview

This class represents a Parse request.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, uri, body: nil, headers: nil, opts: {}) ⇒ Request

Creates a new request

Parameters:

  • method (String) —

    the HTTP method

  • uri (String) —

    the API path of the request (without the host)

  • body (Hash) (defaults to: nil) —

    the body (or parameters) of this request.

  • headers (Hash) (defaults to: nil) —

    additional headers to send in this request.

  • opts (Hash) (defaults to: {}) —

    additional optional parameters.

Options Hash (opts:):

  • :request_id (String) —

    custom request ID for idempotency

  • :idempotent (Boolean) —

    force enable/disable idempotency for this request



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/parse/client/request.rb', line 161

def initialize(method, uri, body: nil, headers: nil, opts: {})
  @tag = 0
  method = method.downcase.to_sym
  unless method == :get || method == :put || method == :post || method == :delete
    raise ArgumentError, "Invalid method #{method} for request : '#{uri}'"
  end

  self.method = method
  self.path = uri
  self.body = body
  # Copy the caller's hashes: the request id header is written into
  # `headers` below, and writing it into a hash the caller reuses would
  # send the same id on the caller's next request, which Parse Server's
  # idempotency layer rejects as a duplicate.
  self.headers = headers ? headers.dup : {}
  self.opts = opts ? opts.dup : {}

  # Handle request ID for idempotency
  setup_request_id
end

Class Attribute Details

.assume_server_idempotency ⇒ Boolean

Returns operator assertion that the Parse Server is configured with idempotencyOptions covering the write paths the SDK targets. When true, a request that carries a stable X-Parse-Request-Id header becomes safe for Client to transparently RETRY on an ambiguous failure (500/503/dropped connection) even when it is a POST or an atomic-op write — Parse Server deduplicates the replay server-side, so the write applies AT MOST ONCE.

The replay does NOT transparently return the original response, though: Parse Server rejects the duplicate with error 159, which the SDK raises as Error::DuplicateRequestError. A caller relying on this retry must rescue that error (the original write already landed) and re-fetch by its own key if it needs the result.

Default false. Sending the X-Parse-Request-Id header is harmless on its own, but ASSUMING the server deduplicates when it does not would double-apply the write on retry. Only set this true when Parse Server's idempotencyOptions is actually configured to cover those paths (it is OFF by default on Parse Server).

Returns:

  • (Boolean) —

    operator assertion that the Parse Server is configured with idempotencyOptions covering the write paths the SDK targets. When true, a request that carries a stable X-Parse-Request-Id header becomes safe for Client to transparently RETRY on an ambiguous failure (500/503/dropped connection) even when it is a POST or an atomic-op write — Parse Server deduplicates the replay server-side, so the write applies AT MOST ONCE.

    The replay does NOT transparently return the original response, though: Parse Server rejects the duplicate with error 159, which the SDK raises as Error::DuplicateRequestError. A caller relying on this retry must rescue that error (the original write already landed) and re-fetch by its own key if it needs the result.

    Default false. Sending the X-Parse-Request-Id header is harmless on its own, but ASSUMING the server deduplicates when it does not would double-apply the write on retry. Only set this true when Parse Server's idempotencyOptions is actually configured to cover those paths (it is OFF by default on Parse Server).



142
143
144
# File 'lib/parse/client/request.rb', line 142

def assume_server_idempotency
  @assume_server_idempotency
end

.enable_request_id ⇒ Boolean

Returns whether to automatically generate request IDs for idempotency.

Returns:

  • (Boolean) —

    whether to automatically generate request IDs for idempotency



111
112
113
# File 'lib/parse/client/request.rb', line 111

def enable_request_id
  @enable_request_id
end

.idempotent_methods ⇒ Array<Symbol>

Returns HTTP methods that should include request IDs.

Returns:

  • (Array<Symbol>) —

    HTTP methods that should include request IDs



119
120
121
# File 'lib/parse/client/request.rb', line 119

def idempotent_methods
  @idempotent_methods
end

.request_id_header ⇒ String

Returns the header name to use for request IDs.

Returns:

  • (String) —

    the header name to use for request IDs



115
116
117
# File 'lib/parse/client/request.rb', line 115

def request_id_header
  @request_id_header
end

Instance Attribute Details

#body ⇒ Object

Returns the value of attribute body.



# File 'lib/parse/client/request.rb', line 17

#cache ⇒ Boolean

Returns unused by Parse::Request itself; retained as a plain accessor for callers that stash a cache handle or flag directly on the request object rather than through opts[:cache].

Returns:

  • (Boolean) —

    unused by Parse::Request itself; retained as a plain accessor for callers that stash a cache handle or flag directly on the request object rather than through opts[:cache].



38
# File 'lib/parse/client/request.rb', line 38

attr_accessor :method, :path, :body, :headers, :opts, :cache

#headers ⇒ Object

Returns the value of attribute headers.



38
39
40
# File 'lib/parse/client/request.rb', line 38

def headers
  @headers
end

#method ⇒ Object

Returns the value of attribute method.



# File 'lib/parse/client/request.rb', line 11

#opts ⇒ Hash

Returns per-request options consumed by Client#request when it builds the HTTP headers for this request. Recognized keys:

  • :cache — false sends Cache-Control: no-cache; :write_only skips the cache read but still writes the response; a Numeric overrides the cache expiration (seconds) for this request only.
  • :use_master_key — false forces the master key off for this request even if the client has one configured.
  • :session_token — a session token to authenticate this request as a specific user, bypassing the client's default auth context.
  • :idempotent — explicitly enables/disables idempotency-header generation for this request, overriding the class-level defaults.
  • :request_id — a caller-supplied idempotency key; see enable_idempotency!.

Returns:

  • (Hash) —

    per-request options consumed by Client#request when it builds the HTTP headers for this request. Recognized keys:

    • :cache — false sends Cache-Control: no-cache; :write_only skips the cache read but still writes the response; a Numeric overrides the cache expiration (seconds) for this request only.
    • :use_master_key — false forces the master key off for this request even if the client has one configured.
    • :session_token — a session token to authenticate this request as a specific user, bypassing the client's default auth context.
    • :idempotent — explicitly enables/disables idempotency-header generation for this request, overriding the class-level defaults.
    • :request_id — a caller-supplied idempotency key; see enable_idempotency!.


38
# File 'lib/parse/client/request.rb', line 38

attr_accessor :method, :path, :body, :headers, :opts, :cache

#path ⇒ Object

Returns the value of attribute path.



# File 'lib/parse/client/request.rb', line 14

#request_id ⇒ String

Returns unique identifier for this request to enable idempotency.

Returns:

  • (String) —

    unique identifier for this request to enable idempotency



105
106
107
# File 'lib/parse/client/request.rb', line 105

def request_id
  @request_id
end

Class Method Details

.configure_idempotency(enabled: true, methods: [:post, :put, :patch], header: "X-Parse-Request-Id", assume_server_dedup: false) ⇒ Object

Configures idempotency settings

Parameters:

  • enabled (Boolean) (defaults to: true) —

    whether to enable idempotency

  • methods (Array<Symbol>) (defaults to: [:post, :put, :patch]) —

    HTTP methods to apply idempotency to

  • header (String) (defaults to: "X-Parse-Request-Id") —

    header name to use for request IDs

  • assume_server_dedup (Boolean) (defaults to: false) —

    sets assume_server_idempotency (default false). Pass true ONLY when Parse Server idempotencyOptions is configured for the targeted paths.



360
361
362
363
364
365
# File 'lib/parse/client/request.rb', line 360

def self.configure_idempotency(enabled: true, methods: [:post, :put, :patch], header: "X-Parse-Request-Id", assume_server_dedup: false)
  self.enable_request_id = enabled
  self.idempotent_methods = methods
  self.request_id_header = header
  self.assume_server_idempotency = assume_server_dedup
end

.disable_idempotency! ⇒ Object

Disables request ID generation globally. Also clears assume_server_idempotency so writes are never treated as retry-safe once the header is no longer sent.



348
349
350
351
# File 'lib/parse/client/request.rb', line 348

def self.disable_idempotency!
  self.enable_request_id = false
  self.assume_server_idempotency = false
end

.enable_idempotency!(methods: [:post, :put, :patch], header: "X-Parse-Request-Id", assume_server_dedup: nil) ⇒ Object

Enables request ID generation globally

Parameters:

  • methods (Array<Symbol>) (defaults to: [:post, :put, :patch]) —

    HTTP methods to apply idempotency to

  • header (String) (defaults to: "X-Parse-Request-Id") —

    header name to use for request IDs

  • assume_server_dedup (Boolean, nil) (defaults to: nil) —

    when non-nil, also sets assume_server_idempotency — pass true ONLY when Parse Server's idempotencyOptions is configured, to additionally make writes retry-safe. Leave nil (default) to send the header without changing the retry posture.



338
339
340
341
342
343
# File 'lib/parse/client/request.rb', line 338

def self.enable_idempotency!(methods: [:post, :put, :patch], header: "X-Parse-Request-Id", assume_server_dedup: nil)
  self.enable_request_id = true
  self.idempotent_methods = methods
  self.request_id_header = header
  self.assume_server_idempotency = assume_server_dedup unless assume_server_dedup.nil?
end

Instance Method Details

#==(r) ⇒ Boolean

Returns:

  • (Boolean)


194
195
196
197
# File 'lib/parse/client/request.rb', line 194

def ==(r)
  return false unless r.is_a?(Request)
  @method == r.method && @path == r.path && @body == r.body && @headers == r.headers
end

#as_json ⇒ Hash

Returns JSON encoded hash.

Returns:

  • (Hash) —

    JSON encoded hash



189
190
191
# File 'lib/parse/client/request.rb', line 189

def as_json
  signature.as_json
end

#idempotent? ⇒ Boolean

Checks if this request has idempotency enabled

Returns:

  • (Boolean)


324
325
326
# File 'lib/parse/client/request.rb', line 324

def idempotent?
  @request_id.present? && @headers[self.class.request_id_header].present?
end

#query ⇒ Hash

The parameters of this request if the HTTP method is GET.

Returns:



184
185
186
# File 'lib/parse/client/request.rb', line 184

def query
  body if @method == :get
end

#signature ⇒ Hash

Signature provies a way for us to compare different requests objects. Two requests objects are the same if they have the same signature.

Returns:

  • (Hash) —

    A hash representing this request.



202
203
204
# File 'lib/parse/client/request.rb', line 202

def signature
  { method: @method.upcase, path: @path, body: @body }
end

#to_s ⇒ String

Returns:



212
213
214
# File 'lib/parse/client/request.rb', line 212

def to_s
  "#{@method.to_s.upcase} #{@path}"
end

#with_idempotency(custom_id = nil) ⇒ self

Enables idempotency for this specific request

Parameters:

  • custom_id (String) (defaults to: nil) —

    optional custom request ID to use

Returns:

  • (self) —

    for method chaining



306
307
308
309
310
311
# File 'lib/parse/client/request.rb', line 306

def with_idempotency(custom_id = nil)
  @opts[:idempotent] = true
  @opts[:request_id] = custom_id if custom_id
  setup_request_id
  self
end

#without_idempotency ⇒ self

Disables idempotency for this specific request

Returns:

  • (self) —

    for method chaining



315
316
317
318
319
320
# File 'lib/parse/client/request.rb', line 315

def without_idempotency
  @opts[:idempotent] = false
  @request_id = nil
  @headers.delete(self.class.request_id_header)
  self
end