Class: Parse::Response
- Inherits:
-
Object
- Object
- Parse::Response
- Includes:
- Enumerable
- Defined in:
- lib/parse/client/response.rb
Overview
Represents a response from Parse server. A response can also be a set of responses (from a Batch response).
Constant Summary collapse
- ERROR_INTERNAL =
Code for an unknown error.
1- ERROR_SERVICE_UNAVAILABLE =
Code when the server returns a 500 or is non-responsive.
2- ERROR_TIMEOUT =
Code when the request times out.
124- ERROR_EXCEEDED_BURST_LIMIT =
Code when the requests per second limit as been exceeded.
155- ERROR_DUPLICATE_REQUEST =
Code when a request carrying a previously-seen
X-Parse-Request-Idis rejected by Parse Server's idempotency layer (the request was already applied). Surfaced as Error::DuplicateRequestError. 159- ERROR_OBJECT_NOT_FOUND =
Code when a requested record is not found.
101- ERROR_USERNAME_MISSING =
Code when the username is missing in request.
200- ERROR_PASSWORD_MISSING =
Code when the password is missing in request.
201- ERROR_USERNAME_TAKEN =
Code when the username is already in the system.
202- ERROR_EMAIL_TAKEN =
Code when the email is already in the system.
203- ERROR_EMAIL_NOT_FOUND =
Code when the email is not found
205- ERROR_EMAIL_INVALID =
Code when the email is invalid
125- ERROR_INVALID_SESSION_TOKEN =
Code returned for invalid or expired session tokens (Parse Server).
209- ERROR_OPERATION_FORBIDDEN =
Code returned for operations that are not permitted under the caller's ACL / CLP / authentication scope. Parse Server uses this code for both "you are not authenticated for this" and "you are authenticated, but not authorized" — see #permission_denied? for the SDK-friendly predicate.
119- ERROR =
The field name for the error.
"error".freeze
- SUCCESS =
The field name for the success.
"success".freeze
- CODE =
The field name for the error code.
"code".freeze
- RESULTS =
The field name for the results of the request.
"results".freeze
- COUNT =
The field name for the count result in a count response.
"count".freeze
- RETRY_AFTER =
The Retry-After header name.
"Retry-After".freeze
Instance Attribute Summary collapse
-
#code ⇒ Integer
The error code.
-
#count ⇒ Integer
readonly
You can query Parse for counting objects, which may not actually have results.
-
#error ⇒ Integer
The error message.
-
#headers ⇒ Hash
The HTTP response headers.
-
#http_status ⇒ Integer
The HTTP status code from the response.
-
#parse_class ⇒ String
The Parse class for this request.
-
#request ⇒ Integer
The Parse::Request that generated this response.
-
#result ⇒ Hash
The body of the response result.
Class Method Summary collapse
-
.error_response(code, message, http_status: 0) ⇒ Parse::Response
Build a failed response with the given code and message.
-
.success_entry(result) ⇒ Parse::Response
Build a successful response from a result hash.
Instance Method Summary collapse
-
#batch? ⇒ Boolean
true if this was a batch response.
-
#batch_responses ⇒ Array<Parse::Response>
If it is a batch response, we'll create an array of Response objects for each of the ones in the batch.
-
#each {|a| ... } ⇒ Object
Iterate through each result item.
-
#error? ⇒ Boolean
true if the response has an error code.
-
#first ⇒ Object
The first thing in the result array.
-
#initialize(res = {}) ⇒ Response
constructor
Create an instance with a Parse response JSON hash.
-
#object_not_found? ⇒ Boolean
true if the response has an error code of 'object not found'.
-
#parse_result!(h) ⇒ Object
(also: #parse_results!)
This method takes the result hash and determines if it is a regular parse query result, object result or a count result.
-
#permission_denied? ⇒ Boolean
true if the response indicates the caller is not authorized to perform the requested operation.
-
#results ⇒ Array
The result data from the response.
-
#retry_after ⇒ Integer?
Get the Retry-After header value as seconds.
-
#success? ⇒ Boolean
true if the response is successful.
-
#to_s ⇒ String
JSON encoded object, or an error string.
Constructor Details
#initialize(res = {}) ⇒ Response
Create an instance with a Parse response JSON hash.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/parse/client/response.rb', line 110 def initialize(res = {}) @http_status = 0 @count = 0 @batch_response = false # by default, not a batch response @result = nil # If a string is used for initializing, treat it as JSON # check for string to not be 'OK' since that is the health check API response res = JSON.parse(res) if res.is_a?(String) && res != "OK".freeze # If it is a hash (or parsed JSON), then parse the result. parse_result!(res) if res.is_a?(Hash) # if the result is an Array, then most likely it is a set of responses # from using a Batch API. if res.is_a?(Array) @batch_response = true @result = res || [] @count = @result.count end #if none match, set pure result @result = res if @result.nil? end |
Instance Attribute Details
#code ⇒ Integer
Returns the error code.
78 79 |
# File 'lib/parse/client/response.rb', line 78 attr_accessor :parse_class, :code, :error, :result, :http_status, :request, :headers |
#count ⇒ Integer (readonly)
You can query Parse for counting objects, which may not actually have results.
83 84 85 |
# File 'lib/parse/client/response.rb', line 83 def count @count end |
#error ⇒ Integer
Returns the error message.
78 79 |
# File 'lib/parse/client/response.rb', line 78 attr_accessor :parse_class, :code, :error, :result, :http_status, :request, :headers |
#headers ⇒ Hash
Returns the HTTP response headers.
78 79 |
# File 'lib/parse/client/response.rb', line 78 attr_accessor :parse_class, :code, :error, :result, :http_status, :request, :headers |
#http_status ⇒ Integer
Returns the HTTP status code from the response.
78 79 |
# File 'lib/parse/client/response.rb', line 78 attr_accessor :parse_class, :code, :error, :result, :http_status, :request, :headers |
#parse_class ⇒ String
Returns the Parse class for this request.
78 79 80 |
# File 'lib/parse/client/response.rb', line 78 def parse_class @parse_class end |
#request ⇒ Integer
Returns the Parse::Request that generated this response.
78 79 |
# File 'lib/parse/client/response.rb', line 78 attr_accessor :parse_class, :code, :error, :result, :http_status, :request, :headers |
#result ⇒ Hash
Returns the body of the response result.
78 79 |
# File 'lib/parse/client/response.rb', line 78 attr_accessor :parse_class, :code, :error, :result, :http_status, :request, :headers |
Class Method Details
.error_response(code, message, http_status: 0) ⇒ Parse::Response
Build a failed response with the given code and message.
184 185 186 187 188 189 190 |
# File 'lib/parse/client/response.rb', line 184 def self.error_response(code, , http_status: 0) r = new r.code = code r.error = r.http_status = http_status r end |
.success_entry(result) ⇒ Parse::Response
Build a successful response from a result hash. Unlike #initialize,
a code or error key in the hash is kept as data and never turns the
response into a failure.
171 172 173 174 175 176 177 |
# File 'lib/parse/client/response.rb', line 171 def self.success_entry(result) r = new(result.is_a?(Hash) ? result : {}) r.code = nil r.error = nil r.result = result unless result.is_a?(Hash) r end |
Instance Method Details
#batch? ⇒ Boolean
true if this was a batch response.
132 133 134 |
# File 'lib/parse/client/response.rb', line 132 def batch? @batch_response end |
#batch_responses ⇒ Array<Parse::Response>
If it is a batch response, we'll create an array of Response objects for each of the ones in the batch.
Each entry is classified by its envelope key, not by its contents. A
success entry is always a successful response, even when the saved
object carries a column named code or error (for example one set by a
beforeSave trigger). An error entry is always a failed response. An
entry with neither key, or one that is not a Hash, is malformed and is
reported as a failed response so the caller never treats it as a write
that landed.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/parse/client/response.rb', line 147 def batch_responses return [self] unless @batch_response # if batch response, generate array based on the response hash. @result.map do |r| if r.is_a?(Hash) && r.key?(SUCCESS) Parse::Response.success_entry(r[SUCCESS]) elsif r.is_a?(Hash) && r[ERROR].is_a?(Hash) Parse::Response.error_response( r[ERROR][CODE] || ERROR_INTERNAL, r[ERROR][ERROR] || "Unknown batch error", ) elsif r.is_a?(Hash) && r.key?(ERROR) Parse::Response.error_response(ERROR_INTERNAL, r[ERROR].to_s) else Parse::Response.error_response(ERROR_INTERNAL, "Malformed batch response entry") end end end |
#each {|a| ... } ⇒ Object
Iterate through each result item.
261 262 263 264 265 |
# File 'lib/parse/client/response.rb', line 261 def each(&block) return enum_for(:each) unless block_given? results.each(&block) self end |
#error? ⇒ Boolean
true if the response has an error code.
219 220 221 |
# File 'lib/parse/client/response.rb', line 219 def error? !success? end |
#first ⇒ Object
Returns the first thing in the result array.
255 256 257 |
# File 'lib/parse/client/response.rb', line 255 def first @result.is_a?(Array) ? @result.first : @result end |
#object_not_found? ⇒ Boolean
true if the response has an error code of 'object not found'
225 226 227 |
# File 'lib/parse/client/response.rb', line 225 def object_not_found? @code == ERROR_OBJECT_NOT_FOUND end |
#parse_result!(h) ⇒ Object Also known as: parse_results!
This method takes the result hash and determines if it is a regular parse query result, object result or a count result. The response should be a hash either containing the result data or the error.
195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/parse/client/response.rb', line 195 def parse_result!(h) @result = {} return unless h.is_a?(Hash) @code = h[CODE] @error = h[ERROR] if h[RESULTS].is_a?(Array) @result = h[RESULTS] @count = h[COUNT] || @result.count else @result = h @count = 1 end end |
#permission_denied? ⇒ Boolean
true if the response indicates the caller is not authorized to perform the requested operation. Parse Server signals authorization failure in two shapes that no-master-key clients commonly hit:
- HTTP 403 with no body (sometimes 401) — recorded as
http_statusonly, with no error code in the JSON. - HTTP 400 + error code 119 (
OPERATION_FORBIDDEN) — typical for CLP andprotectedFieldsdenials. - HTTP 400 + error code 209 (
INVALID_SESSION_TOKEN) — session token missing, revoked, or expired.
This predicate normalizes those into a single check so client code doesn't have to remember both the HTTP-status and code-only paths.
243 244 245 246 |
# File 'lib/parse/client/response.rb', line 243 def return true if @http_status == 401 || @http_status == 403 @code == ERROR_OPERATION_FORBIDDEN || @code == ERROR_INVALID_SESSION_TOKEN end |
#results ⇒ Array
Returns the result data from the response.
249 250 251 252 |
# File 'lib/parse/client/response.rb', line 249 def results return [] if @result.nil? @result.is_a?(Array) ? @result : [@result] end |
#retry_after ⇒ Integer?
Get the Retry-After header value as seconds. The Retry-After header can be either a number of seconds or an HTTP-date.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/parse/client/response.rb', line 88 def retry_after return nil unless @headers.is_a?(Hash) value = @headers[RETRY_AFTER] || @headers["retry-after"] return nil if value.nil? # Try parsing as integer (seconds) if value.to_s =~ /\A\d+\z/ value.to_i else # Try parsing as HTTP-date begin date = Time.httpdate(value.to_s) delay = (date - Time.now).ceil delay > 0 ? delay : 1 rescue ArgumentError nil end end end |
#success? ⇒ Boolean
true if the response is successful.
213 214 215 |
# File 'lib/parse/client/response.rb', line 213 def success? @code.nil? && @error.nil? end |
#to_s ⇒ String
Returns JSON encoded object, or an error string. Credential
fields (sessionToken, password, authData, MFA recovery codes
and secrets, keys) are replaced with a placeholder, and credential
text inside string values (password=x, X-Parse-Master-Key: mk)
is filtered too, so printing the response with to_s or inspect
does not leak a live session token. The error form has its text redacted and control characters
escaped. #result, #to_json, and #as_json return the raw values
by design; do not log those.
288 289 290 291 292 293 294 295 296 297 |
# File 'lib/parse/client/response.rb', line 288 def to_s if error? return "[E-#{@code}] #{safe_request_text} : #{safe_error_text} (#{@http_status})" end result = redacted_result return Parse::Middleware::BodyBuilder.redact_patterns(result) if result.is_a?(String) # Text patterns run on string values before encoding, never on the # encoded JSON, so escaped quotes cannot defeat them or corrupt it. Parse::Middleware::BodyBuilder.redact_string_values!(result).to_json end |