Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI utilities to Kinto.py #34

Closed
wants to merge 12 commits into from
20 changes: 14 additions & 6 deletions kinto_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


from kinto_client import utils
from kinto_client.batch import Batch
from kinto_client import batch
from kinto_client.exceptions import BucketNotFound, KintoException


Expand Down Expand Up @@ -145,18 +145,20 @@ def batch(self, **kwargs):
self._server_settings = resp['settings']

batch_max_requests = self._server_settings['batch_max_requests']
batch = Batch(self, batch_max_requests=batch_max_requests)
yield self.clone(session=batch, **kwargs)
for (resp, headers) in batch.send():
batch_session = batch.Session(
self,
batch_max_requests=batch_max_requests)
yield self.clone(session=batch_session, **kwargs)
for (resp, headers) in batch_session.send():
for i, response in enumerate(resp['responses']):
status_code = response['status']
if not (200 <= status_code < 400):
message = '{0} - {1}'.format(status_code, response['body'])
exception = KintoException(message)
exception.request = batch.requests[i]
exception.request = batch_session.requests[i]
exception.response = response
raise exception
batch.reset()
batch_session.reset()

def _get_endpoint(self, name, bucket=None, collection=None, id=None):
kwargs = {
Expand Down Expand Up @@ -232,6 +234,8 @@ def delete_bucket(self, bucket=None, safe=True, last_modified=None):
endpoint = self._get_endpoint('bucket', bucket)
headers = self._get_cache_headers(safe, last_modified=last_modified)
resp, _ = self.session.request('delete', endpoint, headers=headers)
if resp == batch.RESPONSE_BODY:
return
return resp['data']

# Collections
Expand Down Expand Up @@ -273,6 +277,8 @@ def delete_collection(self, collection=None, bucket=None,
endpoint = self._get_endpoint('collection', bucket, collection)
headers = self._get_cache_headers(safe, last_modified=last_modified)
resp, _ = self.session.request('delete', endpoint, headers=headers)
if resp == batch.RESPONSE_BODY:
return
return resp['data']

# Records
Expand Down Expand Up @@ -323,6 +329,8 @@ def delete_record(self, id, collection=None, bucket=None,
resp, _ = self.session.request(
'delete', endpoint,
headers=self._get_cache_headers(safe, last_modified=last_modified))
if resp == batch.RESPONSE_BODY:
return
return resp['data']

def delete_records(self, records):
Expand Down
7 changes: 5 additions & 2 deletions kinto_client/batch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from . import utils

RESPONSE_HEADERS = 'BATCH_RESPONSE_HEADERS'
RESPONSE_BODY = 'BATCH_RESPONSE_BODY'

class Batch(object):

class Session(object):

def __init__(self, client, batch_max_requests=0):
self.session = client.session
Expand All @@ -15,7 +18,7 @@ def request(self, method, endpoint, data=None, permissions=None,
# is called.
self.requests.append((method, endpoint, data, permissions, headers))
# This is the signature of the session request.
return None, None
return RESPONSE_BODY, RESPONSE_HEADERS

def reset(self):
# Reinitialize the batch.
Expand Down
Loading