Commit a39d44df by Daniel Robinson

Merge pull request #138 from milesrichardson/master

work with self-hosted parse server
parents bfb76788 8dddd840
about the fork
=========
- Forked from https://github.com/dgrtwo/ParsePy on 3/28 by @milesrichardson
- upstream seems to be no longer maintained
- major fixes in fork:
- (improvement) Work with self-hosted parse-server
- (improvement) Work with self-hosted parse-server and batch_save
- (bugfix) batch_save calls response callback on ALL responses, even if some contain errors, and raises ParseBatchError([error1, error2, error3]) with list of all errors encountered during batch operation
(See https://github.com/dgrtwo/ParsePy/pull/138 for PR)
parse_rest parse_rest
========== ==========
...@@ -560,7 +572,7 @@ Assuming the CollectedItem 'Sword' is read-protected from the public by an ACL a ...@@ -560,7 +572,7 @@ Assuming the CollectedItem 'Sword' is read-protected from the public by an ACL a
Elevating Access to Master Elevating Access to Master
-------------------------- --------------------------
Sometimes it is useful to only allow privileged use of the master key for specific uses. Sometimes it is useful to only allow privileged use of the master key for specific uses.
~~~~~ {python} ~~~~~ {python}
from parse_rest.connection import MasterKey from parse_rest.connection import MasterKey
......
...@@ -13,13 +13,16 @@ ...@@ -13,13 +13,16 @@
from six.moves.urllib.request import Request, urlopen from six.moves.urllib.request import Request, urlopen
from six.moves.urllib.error import HTTPError from six.moves.urllib.error import HTTPError
from six.moves.urllib.parse import urlencode from six.moves.urllib.parse import urlencode, urlparse
import json import json
from parse_rest import core from parse_rest import core
API_ROOT = 'https://api.parse.com/1' import os
API_ROOT = os.environ.get('PARSE_API_ROOT') or 'https://api.parse.com/1'
ACCESS_KEYS = {} ACCESS_KEYS = {}
...@@ -87,7 +90,8 @@ class ParseBase(object): ...@@ -87,7 +90,8 @@ class ParseBase(object):
command. command.
""" """
if batch: if batch:
ret = {"method": http_verb, "path": uri.split("parse.com", 1)[1]} urlsplitter = urlparse(API_ROOT).netloc
ret = {"method": http_verb, "path": uri.split(urlsplitter, 1)[1]}
if kw: if kw:
ret["body"] = kw ret["body"] = kw
return ret return ret
...@@ -118,7 +122,7 @@ class ParseBase(object): ...@@ -118,7 +122,7 @@ class ParseBase(object):
headers.update(extra_headers or {}) headers.update(extra_headers or {})
request = Request(url, data, headers) request = Request(url, data, headers)
if ACCESS_KEYS.get('session_token'): if ACCESS_KEYS.get('session_token'):
request.add_header('X-Parse-Session-Token', ACCESS_KEYS.get('session_token')) request.add_header('X-Parse-Session-Token', ACCESS_KEYS.get('session_token'))
elif master_key: elif master_key:
...@@ -179,11 +183,16 @@ class ParseBatcher(ParseBase): ...@@ -179,11 +183,16 @@ class ParseBatcher(ParseBase):
responses = self.execute("", "POST", requests=queries) responses = self.execute("", "POST", requests=queries)
# perform the callbacks with the response data (updating the existing # perform the callbacks with the response data (updating the existing
# objets, etc) # objets, etc)
batched_errors = []
for callback, response in zip(callbacks, responses): for callback, response in zip(callbacks, responses):
if "success" in response: if "success" in response:
callback(response["success"]) callback(response["success"])
else: else:
raise core.ParseError(response["error"]) batched_errors.append(response["error"])
if batched_errors:
raise core.ParseBatchError(batched_errors)
def batch_save(self, objects): def batch_save(self, objects):
"""save a list of objects in one operation""" """save a list of objects in one operation"""
......
...@@ -16,6 +16,9 @@ class ParseError(Exception): ...@@ -16,6 +16,9 @@ class ParseError(Exception):
'''Base exceptions from requests made to Parse''' '''Base exceptions from requests made to Parse'''
pass pass
class ParseBatchError(Exception):
''' Error in batching operation... should take a list. '''
pass
class ResourceRequestBadRequest(ParseError): class ResourceRequestBadRequest(ParseError):
'''Request returns a 400''' '''Request returns a 400'''
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment