class UserEvents(Api): """ User related application events definition """ class Meta: """ User events meta definition """ name = 'user' user_created = Event(parameters=( Parameter('uaa_id', int), Parameter('username', str), )) user_deleted = Event(parameters=(Parameter('uaa_id', int), ))
class ProductsApi(Api): created = Event(arguments=['name', 'uuid']) updated = Event(arguments=['name', 'uuid']) deleted = Event(arguments=['uuid']) class Meta: # The name below defines how this api will be accessed, # eg: bus.products.all() name = 'products' def all(self): """Get all products""" # The database returns OrderdDict objects, convert them into # regular dicts so they can be easily serialised. return [dict(product) for product in db['products'].all()]
class AuthApi(Api): user_registered = Event(parameters=[Parameter("username", str)]) class Meta: name = "auth" def check_password(self, username: str, password: str): return username == "admin" and password == "secret"
class AuthApi(Api): user_registered = Event(arguments=[NewType('username', str)]) class Meta: name = 'my_company.auth' def check_password(self, username: str, password: str) -> bool: return username == 'admin' and password == 'secret'
class TestApi(Api): my_event = Event([Parameter("field", bool)]) class Meta: name = "my.test_api" def my_proc(self, field: bool = True) -> str: pass
class AuthApi(Api): user_registered = Event(arguments=['username']) class Meta: name = 'my_company.auth' def check_password(self, username, password): return username == 'admin' and password == 'secret'
class DummyApi(Api): my_event = Event([Parameter("field", str)]) class Meta: name = "my.dummy" def my_proc(self, field) -> str: return "value: {}".format(field) def sudden_death(self, n): raise SuddenDeathException() def random_death(self, n, death_probability=0.5): if random() < float(death_probability): raise SuddenDeathException() return n def general_error(self): raise RuntimeError("Oh no, there was some kind of error")
class TestApi(Api): _my_event = Event(["field"]) class Meta: name = "my.test_api"
class TestApi2(Api): my_event_a = Event(["field"]) class Meta: name = "my.test_api2"
class UserApi(Api): my_event = Event([Parameter("user", User)]) class Meta: name = "my.user_api"
class TestApi(Api): my_event = Event([Parameter("field", bool)]) class Meta: name = "my.test_api"
def test_pass_string_as_event_params(): # Check we cannot accidentally pass a string to Event in the # case that we omit a ',' when specifying a parameters tuple with pytest.raises(InvalidApiEventConfiguration): Event(parameters=("foo"))