Skip to content

Commit b267f54

Browse files
committed
add user endpoints
1 parent b303ca0 commit b267f54

File tree

3 files changed

+79
-16
lines changed

3 files changed

+79
-16
lines changed

stream/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from stream import exceptions, serializer
1111
from stream.signing import sign
12+
from stream.users import Users
1213
from stream.utils import validate_feed_slug, validate_user_id, validate_foreign_id_time
1314
from stream.httpsig.requests_auth import HTTPSignatureAuth
1415
from requests import Request
@@ -99,6 +100,9 @@ def __init__(
99100
token = self.create_jwt_token("reactions", "*", feed_id="*")
100101
self.reactions = Reactions(self, token)
101102

103+
token = self.create_jwt_token("users", "*", feed_id="*")
104+
self.users = Users(self, token)
105+
102106
def feed(self, feed_slug, user_id):
103107
"""
104108
Returns a Feed object

stream/tests/test_client.py

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from dateutil.tz import tzlocal
22
import stream
33
import time
4-
from stream.exceptions import ApiKeyException, InputException
4+
from stream.exceptions import ApiKeyException, InputException, StreamApiException
55
import random
66
import jwt
77

@@ -1210,7 +1210,9 @@ def test_reaction_get(self):
12101210
self.assertEqual(reaction["data"], {})
12111211
self.assertEqual(reaction["latest_children"], {})
12121212
self.assertEqual(reaction["children_counts"], {})
1213-
self.assertEqual(reaction["activity_id"], "54a60c1e-4ee3-494b-a1e3-50c06acb5ed4")
1213+
self.assertEqual(
1214+
reaction["activity_id"], "54a60c1e-4ee3-494b-a1e3-50c06acb5ed4"
1215+
)
12141216
self.assertEqual(reaction["kind"], "like")
12151217
self.assertIn("created_at", reaction)
12161218
self.assertIn("updated_at", reaction)
@@ -1249,31 +1251,55 @@ def test_reaction_filter_random(self):
12491251

12501252
def _first_result_should_be(self, response, element):
12511253
el = element.copy()
1252-
el.pop('duration')
1254+
el.pop("duration")
12531255
self.assertEqual(len(response["results"]), 1)
12541256
self.assertEqual(response["results"][0], el)
12551257

12561258
def test_reaction_filter(self):
12571259
activity_id = str(uuid1())
12581260
user = str(uuid1())
12591261

1260-
response = self.c.reactions.add(
1261-
"like", activity_id, user
1262-
)
1263-
child = self.c.reactions.add_child(
1264-
"like", response["id"], user
1265-
)
1262+
response = self.c.reactions.add("like", activity_id, user)
1263+
child = self.c.reactions.add_child("like", response["id"], user)
12661264
reaction = self.c.reactions.get(response["id"])
1267-
r = self.c.reactions.filter(
1268-
reaction_id=reaction["id"],
1269-
)
1265+
r = self.c.reactions.filter(reaction_id=reaction["id"])
12701266
self._first_result_should_be(r, child)
12711267

1272-
r = self.c.reactions.filter(
1273-
activity_id=activity_id,
1274-
id_lte=reaction["id"],
1275-
)
1268+
r = self.c.reactions.filter(activity_id=activity_id, id_lte=reaction["id"])
12761269
self._first_result_should_be(r, reaction)
12771270

12781271
r = self.c.reactions.filter(user_id=user, id_lte=reaction["id"])
12791272
self._first_result_should_be(r, reaction)
1273+
1274+
def test_user_add(self):
1275+
self.c.users.add(str(uuid1()))
1276+
1277+
def test_user_add_twice(self):
1278+
user_id = str(uuid1())
1279+
self.c.users.add(user_id)
1280+
with self.assertRaises(StreamApiException):
1281+
self.c.users.add(user_id)
1282+
1283+
def test_user_add_get_or_create(self):
1284+
user_id = str(uuid1())
1285+
r1 = self.c.users.add(user_id)
1286+
r2 = self.c.users.add(user_id, get_or_create=True)
1287+
self.assertEqual(r1["id"], r2["id"])
1288+
self.assertEqual(r1["created_at"], r2["created_at"])
1289+
self.assertEqual(r1["updated_at"], r2["updated_at"])
1290+
1291+
def test_user_get(self):
1292+
response = self.c.users.add(str(uuid1()))
1293+
user = self.c.users.get(response["id"])
1294+
self.assertEqual(user["data"], {})
1295+
self.assertIn("created_at", user)
1296+
self.assertIn("updated_at", user)
1297+
self.assertIn("id", user)
1298+
1299+
def test_user_update(self):
1300+
response = self.c.users.add(str(uuid1()))
1301+
self.c.users.update(response["id"], {"changed": True})
1302+
1303+
def test_user_delete(self):
1304+
response = self.c.users.add(str(uuid1()))
1305+
self.c.users.delete(response["id"])

stream/users.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Users(object):
2+
def __init__(self, client, token):
3+
self.client = client
4+
self.token = token
5+
6+
def add(self, user_id, data=None, get_or_create=False):
7+
payload = dict(id=user_id, data=data)
8+
return self.client.post(
9+
"user/",
10+
service_name="api",
11+
signature=self.token,
12+
data=payload,
13+
params={"get_or_create": get_or_create},
14+
)
15+
16+
def get(self, user_id):
17+
return self.client.get(
18+
"user/%s" % user_id, service_name="api", signature=self.token
19+
)
20+
21+
def update(self, user_id, data=None):
22+
payload = dict(data=data)
23+
return self.client.put(
24+
"user/%s" % user_id,
25+
service_name="api",
26+
signature=self.token,
27+
data=payload,
28+
)
29+
30+
def delete(self, user_id):
31+
return self.client.delete(
32+
"user/%s" % user_id, service_name="api", signature=self.token
33+
)

0 commit comments

Comments
 (0)