Skip to content

Commit

Permalink
Merge 6bf417f into 60a494a
Browse files Browse the repository at this point in the history
  • Loading branch information
mansimarkaur committed Sep 13, 2016
2 parents 60a494a + 6bf417f commit f8747d5
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 40 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This document describes changes between each past release.
6.3.0 (unreleased)
==================

- Nothing changed yet.
- Added CRUD methods for the endpoint group. (#95)


6.2.1 (2016-09-08)
Expand Down
32 changes: 25 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Kinto python client
:target: https://coveralls.io/r/Kinto/kinto-http.py


Kinto is a service that allows to store and synchronize arbitrary data,
Kinto is a service that allows users to store and synchronize arbitrary data,
attached to a user account. Its primary interface is HTTP.

*kinto-http* is a Python library that eases the interactions with
Expand All @@ -33,9 +33,9 @@ Usage
.. note::

Operations are always performed directly on the server, and no
synchronisation features are implemented yet.
synchronisation features have been implemented yet.

- The first version of this API doesn't cache any access nor provide any
- The first version of this API doesn't cache any access nor provides any
refresh mechanism. If you want to be sure you have the latest data available,
issue another call.

Expand Down Expand Up @@ -117,11 +117,29 @@ If no specific bucket name is provided, the "default" bucket is used.
client.update_bucket('payments', permissions={})
# Or delete a bucket and everything under.
client.delete_bucket('payment')
client.delete_bucket('payment',if_exists=True)
# Or even every writable buckets.
client.delete_buckets()
Groups
------

A group associates a name to a list of principals. It is useful in order to handle permissions.

.. code-block:: python
client.create_group('receipts', bucket='payments')
# Or get an existing one.
client.get_group('receipts', bucket='payments')
# To delete an existing group.
client.delete_group('receipts', bucket='payments', if_exists=True)
# Or all groups in a bucket.
client.delete_groups(bucket='payments')
Collections
-----------
Expand All @@ -136,7 +154,7 @@ A collection is where records are stored.
client.get_collection('receipts', bucket='payments')
# To delete an existing collection.
client.delete_collection('receipts', bucket='payments')
client.delete_collection('receipts', bucket='payments', if_exists=True)
# Or every collections in a bucket.
client.delete_collections(bucket='payments')
Expand Down Expand Up @@ -172,7 +190,7 @@ A record is a dict with the "permissions" and "data" keys.
# It is also possible to delete a record.
client.delete_record(id='89881454-e4e9-4ef0-99a9-404d95900352',
collection='todos')
collection='todos', if_exists=True)
# Or every records of a collection.
client.delete_records(collection='todos')
Expand Down Expand Up @@ -211,7 +229,7 @@ For instance to give access to "leplatrem" to a specific record, you would do:
Get or create
-------------

In some cases, you might want to create a bucket, collection or record only if
In some cases, you might want to create a bucket, collection, group or record only if
it doesn't exist already. To do so, you can pass the ``if_not_exists=True``
to the ``create_*`` methods::

Expand Down
113 changes: 91 additions & 22 deletions kinto_http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Endpoints(object):
'batch': '{root}/batch',
'buckets': '{root}/buckets',
'bucket': '{root}/buckets/{bucket}',
'groups': '{root}/buckets/{bucket}/groups',
'group': '{root}/buckets/{bucket}/groups/{group}',
'collections': '{root}/buckets/{bucket}/collections',
'collection': '{root}/buckets/{bucket}/collections/{collection}',
'records': '{root}/buckets/{bucket}/collections/{collection}/records', # NOQA
Expand Down Expand Up @@ -97,7 +99,7 @@ def batch(self, **kwargs):
batch_session.send()
batch_session.reset()

def get_endpoint(self, name, bucket=None, collection=None, id=None):
def get_endpoint(self, name, bucket=None, group=None, collection=None, id=None):
"""Return the endpoint with named parameters.
Please always use the method as if it was defined like this:
Expand All @@ -112,6 +114,7 @@ def get_endpoint(self, name, bucket=None, collection=None, id=None):
kwargs = {
'bucket': bucket or self._bucket_name,
'collection': collection or self._collection_name,
'group': group,
'id': id
}
return self.endpoints.get(name, **kwargs)
Expand Down Expand Up @@ -160,13 +163,15 @@ def _create_if_not_exists(self, resource, **kwargs):
# The exception contains the existing record in details.existing
# but it's not enough as we also need to return the permissions.
get_kwargs = {}
if resource in('bucket', 'collection', 'record'):
if resource in('bucket', 'group', 'collection', 'record'):
get_kwargs['bucket'] = kwargs['bucket']
if resource in ('collection', 'record'):
if resource == 'group':
get_kwargs['group'] = kwargs['group']
elif resource in ('collection', 'record'):
get_kwargs['collection'] = kwargs['collection']
if resource == 'record':
_id = kwargs.get('id') or kwargs['data']['id']
get_kwargs['id'] = _id
if resource == 'record':
_id = kwargs.get('id') or kwargs['data']['id']
get_kwargs['id'] = _id

get_method = getattr(self, 'get_%s' % resource)
return get_method(**get_kwargs)
Expand Down Expand Up @@ -249,12 +254,87 @@ def delete_bucket(self, bucket=None, safe=True, if_match=None, if_exists=False):
resp, _ = self.session.request('delete', endpoint, headers=headers)
return resp['data']

def delete_buckets(self, safe=True, if_match=None, if_exists=False):
def delete_buckets(self, safe=True, if_match=None):
endpoint = self.get_endpoint('buckets')
headers = self._get_cache_headers(safe, if_match=if_match)
resp, _ = self.session.request('delete', endpoint, headers=headers)
return resp['data']

# Groups

def get_groups(self, bucket=None):
endpoint = self.get_endpoint('groups', bucket=bucket)
return self._paginated(endpoint)

def create_group(self, group, bucket=None,
data=None, permissions=None,
safe=True, if_not_exists=False):
if if_not_exists:
return self._create_if_not_exists('group',
group=group,
bucket=bucket,
data=data,
permissions=permissions,
safe=safe)
headers = DO_NOT_OVERWRITE if safe else None
endpoint = self.get_endpoint('group',
bucket=bucket,
group=group)
try:
resp, _ = self.session.request('put', endpoint, data=data,
permissions=permissions,
headers=headers)
except KintoException as e:
if e.response.status_code == 403:
msg = ("Unauthorized. Please check that the bucket exists and "
"that you have the permission to create or write on "
"this group.")
e = KintoException(msg, e)
raise e

return resp

def update_group(self, group, data=None, bucket=None,
permissions=None, method='put',
safe=True, if_match=None):
endpoint = self.get_endpoint('group',
bucket=bucket,
group=group)
headers = self._get_cache_headers(safe, data, if_match)
resp, _ = self.session.request(method, endpoint, data=data,
permissions=permissions,
headers=headers)
return resp

def patch_group(self, *args, **kwargs):
kwargs['method'] = 'patch'
return self.update_group(*args, **kwargs)

def get_group(self, group, bucket=None):
endpoint = self.get_endpoint('group',
bucket=bucket,
group=group)
resp, _ = self.session.request('get', endpoint)
return resp

def delete_group(self, group, bucket=None,
safe=True, if_match=None,
if_exists=False):
if if_exists:
return self._delete_if_exists('buckets',
return self._delete_if_exists('group',
group=group,
bucket=bucket,
safe=safe,
if_match=if_match)
endpoint = self.get_endpoint('buckets')
endpoint = self.get_endpoint('group',
bucket=bucket,
group=group)
headers = self._get_cache_headers(safe, if_match=if_match)
resp, _ = self.session.request('delete', endpoint, headers=headers)
return resp['data']

def delete_groups(self, bucket=None, safe=True, if_match=None):
endpoint = self.get_endpoint('groups', bucket=bucket)
headers = self._get_cache_headers(safe, if_match=if_match)
resp, _ = self.session.request('delete', endpoint, headers=headers)
return resp['data']
Expand Down Expand Up @@ -331,12 +411,7 @@ def delete_collection(self, collection=None, bucket=None,
resp, _ = self.session.request('delete', endpoint, headers=headers)
return resp['data']

def delete_collections(self, bucket=None, safe=True, if_match=None, if_exists=False):
if if_exists:
return self._delete_if_exists('collections',
bucket=bucket,
safe=safe,
if_match=if_match)
def delete_collections(self, bucket=None, safe=True, if_match=None):
endpoint = self.get_endpoint('collections', bucket=bucket)
headers = self._get_cache_headers(safe, if_match=if_match)
resp, _ = self.session.request('delete', endpoint, headers=headers)
Expand Down Expand Up @@ -439,13 +514,7 @@ def delete_record(self, id, collection=None, bucket=None,
return resp['data']

def delete_records(self, collection=None, bucket=None,
safe=True, if_match=None, if_exists=False):
if if_exists:
return self._delete_if_exists('records',
collection=collection,
bucket=bucket,
safe=safe,
if_match=if_match)
safe=True, if_match=None):
endpoint = self.get_endpoint('records',
bucket=bucket,
collection=collection)
Expand Down
Loading

0 comments on commit f8747d5

Please sign in to comment.