forked from GetStream/stream-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollections.py
More file actions
81 lines (63 loc) · 2.7 KB
/
Copy pathcollections.py
File metadata and controls
81 lines (63 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class Collections(object):
def __init__(self, client, token):
"""
Used to manipulate data at the 'meta' endpoint
:param client: the api client
:param token: the token
"""
self.client = client
self.token = token
def upsert(self, collection_name, data):
"""
"Insert new or update existing data.
:param collection_name: Collection Name i.e 'user'
:param data: list of dictionaries
:return: http response, 201 if successful along with data posted.
**Example**::
client.collection.upsert('user', [{"id": '1', "name": "Juniper", "hobbies": ["Playing", "Sleeping", "Eating"]},
{"id": '2', "name": "Ruby", "interests": ["Sunbeams", "Surprise Attacks"]}])
"""
if type(data) != list:
data = [data]
data_json = {collection_name: data}
response = self.client.post('meta', personal='meta',
signature=self.token, data={'data': data_json})
return response
def select(self, collection_name, ids):
"""
Retrieve data from meta endpoint, can include data you've uploaded or personalization/analytic data
created by the stream team.
:param collection_name: Collection Name i.e 'user'
:param ids: list of ids of feed group i.e [123,456]
:return: meta data as json blob
**Example**::
client.collection.select('user', 1)
client.collection.select('user', [1,2,3])
"""
if type(ids) != list:
ids = [ids]
ids = [str(i) for i in ids]
foreign_ids = []
for i in range(len(ids)):
foreign_ids.append('%s:%s' % (collection_name, ids[i]))
foreign_ids = ','.join(foreign_ids)
response = self.client.get('meta', personal='meta', params={'foreign_ids': foreign_ids},
signature=self.token)
return response
def delete(self, collection_name, ids):
"""
Delete data from meta.
:param collection_name: Collection Name i.e 'user'
:param ids: list of ids to delete i.e [123,456]
:return: data that was deleted if if successful or not.
**Example**::
client.collections.delete('user', '1')
collections.delete('user', ['1','2','3'])
"""
if type(ids) != list:
ids = [ids]
ids = [str(i) for i in ids]
data = {'collection_name': collection_name, 'ids': ids}
response = self.client.delete('meta', personal='meta', data=data,
signature=self.token)
return response