Class: Parse::Request
- Inherits:
-
Object
- Object
- Parse::Request
- Defined in:
- lib/parse/client/request.rb
Overview
This class represents a Parse request.
Class Attribute Summary collapse
-
.assume_server_idempotency ⇒ Boolean
Operator assertion that the Parse Server is configured with
idempotencyOptionscovering the write paths the SDK targets. -
.enable_request_id ⇒ Boolean
Whether to automatically generate request IDs for idempotency.
-
.idempotent_methods ⇒ Array<Symbol>
HTTP methods that should include request IDs.
-
.request_id_header ⇒ String
The header name to use for request IDs.
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#cache ⇒ Boolean
Unused by 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]. -
#headers ⇒ Object
Returns the value of attribute headers.
-
#method ⇒ Object
Returns the value of attribute method.
-
#opts ⇒ Hash
Per-request options consumed by Client#request when it builds the HTTP headers for this request.
-
#path ⇒ Object
Returns the value of attribute path.
-
#request_id ⇒ String
Unique identifier for this request to enable idempotency.
Class Method Summary collapse
-
.configure_idempotency(enabled: true, methods: [:post, :put, :patch], header: "X-Parse-Request-Id", assume_server_dedup: false) ⇒ Object
Configures idempotency settings.
-
.disable_idempotency! ⇒ Object
Disables request ID generation globally.
-
.enable_idempotency!(methods: [:post, :put, :patch], header: "X-Parse-Request-Id", assume_server_dedup: nil) ⇒ Object
Enables request ID generation globally.
Instance Method Summary collapse
- #==(r) ⇒ Boolean
-
#as_json ⇒ Hash
JSON encoded hash.
-
#idempotent? ⇒ Boolean
Checks if this request has idempotency enabled.
-
#initialize(method, uri, body: nil, headers: nil, opts: {}) ⇒ Request
constructor
Creates a new request.
-
#query ⇒ Hash
The parameters of this request if the HTTP method is GET.
-
#signature ⇒ Hash
Signature provies a way for us to compare different requests objects.
- #to_s ⇒ String
-
#with_idempotency(custom_id = nil) ⇒ self
Enables idempotency for this specific request.
-
#without_idempotency ⇒ self
Disables idempotency for this specific request.
Constructor Details
#initialize(method, uri, body: nil, headers: nil, opts: {}) ⇒ Request
Creates a new request
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/parse/client/request.rb', line 102 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 self.headers = headers || {} self.opts = opts || {} # 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).
83 84 85 |
# File 'lib/parse/client/request.rb', line 83 def assume_server_idempotency @assume_server_idempotency end |
.enable_request_id ⇒ Boolean
Returns whether to automatically generate request IDs for idempotency.
52 53 54 |
# File 'lib/parse/client/request.rb', line 52 def enable_request_id @enable_request_id end |
.idempotent_methods ⇒ Array<Symbol>
Returns HTTP methods that should include request IDs.
60 61 62 |
# File 'lib/parse/client/request.rb', line 60 def idempotent_methods @idempotent_methods end |
.request_id_header ⇒ String
Returns the header name to use for request IDs.
56 57 58 |
# File 'lib/parse/client/request.rb', line 56 def request_id_header @request_id_header end |
Instance Attribute Details
#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].
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—falsesendsCache-Control: no-cache;:write_onlyskips the cache read but still writes the response; aNumericoverrides the cache expiration (seconds) for this request only.:use_master_key—falseforces 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 |
#request_id ⇒ String
Returns unique identifier for this request to enable idempotency.
46 47 48 |
# File 'lib/parse/client/request.rb', line 46 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
289 290 291 292 293 294 |
# File 'lib/parse/client/request.rb', line 289 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.
277 278 279 280 |
# File 'lib/parse/client/request.rb', line 277 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
267 268 269 270 271 272 |
# File 'lib/parse/client/request.rb', line 267 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
131 132 133 134 |
# File 'lib/parse/client/request.rb', line 131 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.
126 127 128 |
# File 'lib/parse/client/request.rb', line 126 def as_json signature.as_json end |
#idempotent? ⇒ Boolean
Checks if this request has idempotency enabled
253 254 255 |
# File 'lib/parse/client/request.rb', line 253 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.
121 122 123 |
# File 'lib/parse/client/request.rb', line 121 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.
139 140 141 |
# File 'lib/parse/client/request.rb', line 139 def signature { method: @method.upcase, path: @path, body: @body } end |
#to_s ⇒ String
149 150 151 |
# File 'lib/parse/client/request.rb', line 149 def to_s "#{@method.to_s.upcase} #{@path}" end |
#with_idempotency(custom_id = nil) ⇒ self
Enables idempotency for this specific request
235 236 237 238 239 240 |
# File 'lib/parse/client/request.rb', line 235 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
244 245 246 247 248 249 |
# File 'lib/parse/client/request.rb', line 244 def without_idempotency @opts[:idempotent] = false @request_id = nil @headers.delete(self.class.request_id_header) self end |