Skip to content

Commit

Permalink
Merge pull request #48 from farin/master
Browse files Browse the repository at this point in the history
Python3, tests for new features, small fix for __len__ and query copying
  • Loading branch information
David Robinson committed Jul 19, 2014
2 parents cd9ef16 + 25acbfb commit ffacfc4
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 295 deletions.
5 changes: 2 additions & 3 deletions README.mkd
Expand Up @@ -52,10 +52,9 @@ in the app and may accidentally replace or change existing objects.

* install the [Parse CloudCode tool](https://www.parse.com/docs/cloud_code_guide)

You can then test the installation by running the following in a Python prompt:
You can then test the installation by running the following command:

from parse_rest import tests
tests.run_tests()
python -m 'parse_rest.tests'


Usage
Expand Down
30 changes: 15 additions & 15 deletions parse_rest/connection.py
Expand Up @@ -11,18 +11,13 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

try:
from urllib2 import Request, urlopen, HTTPError
from urllib import urlencode
except ImportError:
# is Python3
from urllib.request import Request, urlopen
from urllib.error import HTTPError
from urllib.parse import urlencode
from six.moves.urllib.request import Request, urlopen
from six.moves.urllib.error import HTTPError
from six.moves.urllib.parse import urlencode

import json

import core
from parse_rest import core

API_ROOT = 'https://api.parse.com/1'
ACCESS_KEYS = {}
Expand Down Expand Up @@ -60,8 +55,7 @@ def execute(cls, uri, http_verb, extra_headers=None, batch=False, **kw):
command.
"""
if batch:
ret = {"method": http_verb,
"path": uri.split("parse.com")[1]}
ret = {"method": http_verb, "path": uri.split("parse.com", 1)[1]}
if kw:
ret["body"] = kw
return ret
Expand All @@ -79,6 +73,8 @@ def execute(cls, uri, http_verb, extra_headers=None, batch=False, **kw):
if http_verb == 'GET' and data:
url += '?%s' % urlencode(kw)
data = None
else:
data = data.encode('utf-8')

request = Request(url, data, headers)
request.add_header('Content-type', 'application/json')
Expand All @@ -101,7 +97,7 @@ def execute(cls, uri, http_verb, extra_headers=None, batch=False, **kw):
}.get(e.code, core.ParseError)
raise exc(e.read())

return json.loads(response.read())
return json.loads(response.read().decode('utf-8'))

@classmethod
def GET(cls, uri, **kw):
Expand Down Expand Up @@ -129,7 +125,11 @@ def batch(self, methods):
Given a list of create, update or delete methods to call, call all
of them in a single batch operation.
"""
queries, callbacks = zip(*[m(batch=True) for m in methods])
methods = list(methods) # methods can be iterator
if not methods:
#accepts also empty list (or generator) - it allows call batch directly with query result (eventually empty)
return
queries, callbacks = list(zip(*[m(batch=True) for m in methods]))
# perform all the operations in one batch
responses = self.execute("", "POST", requests=queries)
# perform the callbacks with the response data (updating the existing
Expand All @@ -139,8 +139,8 @@ def batch(self, methods):

def batch_save(self, objects):
"""save a list of objects in one operation"""
self.batch([o.save for o in objects])
self.batch(o.save for o in objects)

def batch_delete(self, objects):
"""delete a list of objects in one operation"""
self.batch([o.delete for o in objects])
self.batch(o.delete for o in objects)

0 comments on commit ffacfc4

Please sign in to comment.