Class: Parse::Client
- Inherits:
-
Object
- Object
- Parse::Client
- Defined in:
- lib/parse/client.rb
Overview
The client that communicates with the Parse server via REST
Constant Summary collapse
- RETRIED_EXCEPTIONS =
[ 'Faraday::Error::TimeoutError', 'Faraday::Error::ParsingError', 'Faraday::Error::ConnectionFailed', 'Parse::ParseProtocolRetry' ]
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#application_id ⇒ Object
Returns the value of attribute application_id.
-
#backoff_factor ⇒ Object
Returns the value of attribute backoff_factor.
-
#get_method_override ⇒ Object
readonly
Returns the value of attribute get_method_override.
-
#host ⇒ Object
Returns the value of attribute host.
-
#interval ⇒ Object
Returns the value of attribute interval.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#master_key ⇒ Object
Returns the value of attribute master_key.
-
#max_retries ⇒ Object
Returns the value of attribute max_retries.
-
#path ⇒ Object
Returns the value of attribute path.
-
#quiet ⇒ Object
Returns the value of attribute quiet.
-
#retried_exceptions ⇒ Object
Returns the value of attribute retried_exceptions.
-
#session ⇒ Object
Returns the value of attribute session.
-
#session_token ⇒ Object
Returns the value of attribute session_token.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #application_config ⇒ Object
- #batch ⇒ Object
- #cloud_function(function_name) ⇒ Object
- #delete(uri) ⇒ Object
- #file(data) ⇒ Object
- #get(uri) ⇒ Object
-
#initialize(data = {}, &_blk) ⇒ Client
constructor
A new instance of Client.
- #installation(object_id = nil) ⇒ Object
- #object(class_name, data = nil) ⇒ Object
- #post(uri, body) ⇒ Object
- #push(data, channel = '') ⇒ Object
- #put(uri, body) ⇒ Object
- #query(class_name) ⇒ Object
-
#request(uri, method = :get, body = nil, query = nil, content_type = nil) ⇒ Object
Perform an HTTP request for the given uri and method with common basic response handling.
- #user(data) ⇒ Object
Constructor Details
#initialize(data = {}, &_blk) ⇒ Client
Returns a new instance of Client.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/parse/client.rb', line 35 def initialize(data = {}, &_blk) @host = data[:host] @path = data[:path] || Protocol::PATH @application_id = data[:application_id] @master_key = data[:master_key] @api_key = data[:api_key] @session_token = data[:session_token] @max_retries = data[:max_retries] || 3 @logger = data[:logger] || Logger .new(STDERR).tap { |l| l.level = Logger::INFO } @quiet = data[:quiet] || false @timeout = data[:timeout] || 30 # Additional parameters for Faraday Request::Retry @interval = data[:interval] || 0.5 @backoff_factor = data[:backoff_factor] || 2 @retried_exceptions = RETRIED_EXCEPTIONS @retried_exceptions += data[:retried_exceptions] if data[ :retried_exceptions] @get_method_override = data[:get_method_override] = { request: { timeout: @timeout, open_timeout: @timeout } } @session = Faraday.new(host, ) do |c| c.request :json c.use Faraday::GetMethodOverride if @get_method_override c.use Faraday::BetterRetry, max: @max_retries, logger: @logger, interval: @interval, backoff_factor: @backoff_factor, exceptions: @retried_exceptions c.use Faraday::ExtendedParseJson c.response :logger, @logger unless @quiet c.adapter Faraday.default_adapter yield(c) if block_given? end end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
22 23 24 |
# File 'lib/parse/client.rb', line 22 def api_key @api_key end |
#application_id ⇒ Object
Returns the value of attribute application_id.
21 22 23 |
# File 'lib/parse/client.rb', line 21 def application_id @application_id end |
#backoff_factor ⇒ Object
Returns the value of attribute backoff_factor.
31 32 33 |
# File 'lib/parse/client.rb', line 31 def backoff_factor @backoff_factor end |
#get_method_override ⇒ Object (readonly)
Returns the value of attribute get_method_override.
33 34 35 |
# File 'lib/parse/client.rb', line 33 def get_method_override @get_method_override end |
#host ⇒ Object
Returns the value of attribute host.
19 20 21 |
# File 'lib/parse/client.rb', line 19 def host @host end |
#interval ⇒ Object
Returns the value of attribute interval.
30 31 32 |
# File 'lib/parse/client.rb', line 30 def interval @interval end |
#logger ⇒ Object
Returns the value of attribute logger.
27 28 29 |
# File 'lib/parse/client.rb', line 27 def logger @logger end |
#master_key ⇒ Object
Returns the value of attribute master_key.
23 24 25 |
# File 'lib/parse/client.rb', line 23 def master_key @master_key end |
#max_retries ⇒ Object
Returns the value of attribute max_retries.
26 27 28 |
# File 'lib/parse/client.rb', line 26 def max_retries @max_retries end |
#path ⇒ Object
Returns the value of attribute path.
20 21 22 |
# File 'lib/parse/client.rb', line 20 def path @path end |
#quiet ⇒ Object
Returns the value of attribute quiet.
28 29 30 |
# File 'lib/parse/client.rb', line 28 def quiet @quiet end |
#retried_exceptions ⇒ Object
Returns the value of attribute retried_exceptions.
32 33 34 |
# File 'lib/parse/client.rb', line 32 def retried_exceptions @retried_exceptions end |
#session ⇒ Object
Returns the value of attribute session.
25 26 27 |
# File 'lib/parse/client.rb', line 25 def session @session end |
#session_token ⇒ Object
Returns the value of attribute session_token.
24 25 26 |
# File 'lib/parse/client.rb', line 24 def session_token @session_token end |
#timeout ⇒ Object
Returns the value of attribute timeout.
29 30 31 |
# File 'lib/parse/client.rb', line 29 def timeout @timeout end |
Instance Method Details
#application_config ⇒ Object
127 128 129 |
# File 'lib/parse/client.rb', line 127 def application_config Parse::Application.config(self) end |
#batch ⇒ Object
131 132 133 |
# File 'lib/parse/client.rb', line 131 def batch Parse::Batch.new(self) end |
#cloud_function(function_name) ⇒ Object
135 136 137 |
# File 'lib/parse/client.rb', line 135 def cloud_function(function_name) Parse::Cloud::Function.new(function_name, self) end |
#delete(uri) ⇒ Object
123 124 125 |
# File 'lib/parse/client.rb', line 123 def delete(uri) request(uri, :delete) end |
#file(data) ⇒ Object
139 140 141 |
# File 'lib/parse/client.rb', line 139 def file(data) Parse::File.new(data, self) end |
#get(uri) ⇒ Object
111 112 113 |
# File 'lib/parse/client.rb', line 111 def get(uri) request(uri) end |
#installation(object_id = nil) ⇒ Object
151 152 153 |
# File 'lib/parse/client.rb', line 151 def installation(object_id = nil) Parse::Installation.new(object_id, self) end |
#object(class_name, data = nil) ⇒ Object
143 144 145 |
# File 'lib/parse/client.rb', line 143 def object(class_name, data = nil) Parse::Object.new(class_name, data, self) end |
#post(uri, body) ⇒ Object
115 116 117 |
# File 'lib/parse/client.rb', line 115 def post(uri, body) request(uri, :post, body) end |
#push(data, channel = '') ⇒ Object
147 148 149 |
# File 'lib/parse/client.rb', line 147 def push(data, channel = '') Parse::Push.new(data, channel, self) end |
#put(uri, body) ⇒ Object
119 120 121 |
# File 'lib/parse/client.rb', line 119 def put(uri, body) request(uri, :put, body) end |
#query(class_name) ⇒ Object
155 156 157 |
# File 'lib/parse/client.rb', line 155 def query(class_name) Parse::Query.new(class_name, self) end |
#request(uri, method = :get, body = nil, query = nil, content_type = nil) ⇒ Object
Perform an HTTP request for the given uri and method with common basic response handling. Will raise a ParseProtocolError if the response has an error status code, and will return the parsed JSON body on success, if there is one.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/parse/client.rb', line 87 def request(uri, method = :get, body = nil, query = nil, content_type = nil) headers = {} { 'Content-Type' => content_type || 'application/json', 'User-Agent' => "Parse for Ruby, #{VERSION}", Protocol::HEADER_MASTER_KEY => @master_key, Protocol::HEADER_APP_ID => @application_id, Protocol::HEADER_API_KEY => @api_key, Protocol::HEADER_SESSION_TOKEN => @session_token }.each do |key, value| headers[key] = value if value end uri = ::File.join(path, uri) response = @session.send(method, uri, query || body || {}, headers) response.body # NOTE: Don't leak our internal libraries to our clients. # Extend this list of exceptions as needed. rescue Faraday::Error::ClientError => e raise Parse::ConnectionError, e. end |