Skip to content

Commit

Permalink
allow specifiying session_token to init, defaulting of partial init a…
Browse files Browse the repository at this point in the history
…rguments, specifying PARSE_MASTER_API_KEY environment variable, and setting headers that are variable on Client to current values each request
  • Loading branch information
ericcj authored and tikhon committed Oct 8, 2012
1 parent e5324ad commit 681e30a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
26 changes: 19 additions & 7 deletions lib/parse/client.rb
Expand Up @@ -11,13 +11,16 @@ class Client
attr_accessor :host
attr_accessor :application_id
attr_accessor :api_key
attr_accessor :master_key
attr_accessor :session_token
attr_accessor :session

def initialize(data = {})
@host = data[:host] || Protocol::HOST
@application_id = data[:application_id]
@api_key = data[:api_key]
@master_key = data[:master_key]
@session_token = data[:session_token]
@session = Patron::Session.new
@session.timeout = 30
@session.connect_timeout = 30
Expand All @@ -26,16 +29,18 @@ def initialize(data = {})
@session.headers["Content-Type"] = "application/json"
@session.headers["Accept"] = "application/json"
@session.headers["User-Agent"] = "Parse for Ruby, 0.0"
@session.headers[Protocol::HEADER_MASTER_KEY] = @master_key
@session.headers[Protocol::HEADER_API_KEY] = @api_key
@session.headers[Protocol::HEADER_APP_ID] = @application_id
end

# 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.
def request(uri, method = :get, body = nil, query = nil, max_retries = 2)
@session.headers[Protocol::HEADER_MASTER_KEY] = @master_key
@session.headers[Protocol::HEADER_API_KEY] = @api_key
@session.headers[Protocol::HEADER_APP_ID] = @application_id
@session.headers[Protocol::HEADER_SESSION_TOKEN] = @session_token

options = {}
if body
options[:data] = body
Expand All @@ -50,7 +55,7 @@ def request(uri, method = :get, body = nil, query = nil, max_retries = 2)
rescue Patron::TimeoutError
num_tries += 1
if num_tries <= max_retries
retry
retry
else
raise Patron::TimeoutError
end
Expand Down Expand Up @@ -94,10 +99,17 @@ def delete(uri)
# Initialize the singleton instance of Client which is used
# by all API methods. Parse.init must be called before saving
# or retrieving any objects.
def Parse.init(data = {:application_id => ENV["PARSE_APPLICATION_ID"], :api_key => ENV["PARSE_REST_API_KEY"]})
@@client = Client.new(data)
def Parse.init(data = {})
defaulted = {:application_id => ENV["PARSE_APPLICATION_ID"],
:api_key => ENV["PARSE_REST_API_KEY"]}
defaulted.merge!(data)

# use less permissive key if both are specified
defaulted[:master_key] = ENV["PARSE_MASTER_API_KEY"] unless data[:master_key] || defaulted[:api_key]

@@client = Client.new(defaulted)
end

# Used mostly for testing. Lets you delete the api key global vars.
def Parse.destroy
@@client = nil
Expand Down
3 changes: 3 additions & 0 deletions lib/parse/protocol.rb
Expand Up @@ -26,6 +26,9 @@ module Protocol
# Parse API.
HEADER_MASTER_KEY = "X-Parse-Master-Key"

# The HTTP header used for passing your authenticated session
HEADER_SESSION_TOKEN = "X-Parse-Session-Token"

# JSON Keys
# ----------------------------------------

Expand Down

0 comments on commit 681e30a

Please sign in to comment.