-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkitten.py
54 lines (41 loc) · 1.8 KB
/
kitten.py
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
import asyncio
from kettlingar import RPCKitten
class MyKitten(RPCKitten):
"""mykitten - A sample kettlingar microservice
This microservice knows how to meow and how to purr. Purring is
private and requires authentication, and may go on for a while.
"""
class Configuration(RPCKitten.Configuration):
APP_NAME = 'mykitten'
WORKER_NAME = 'Kitty'
async def public_api_meow(self, request_info):
"""/meow
This endpoint requires no authentication!
This endpoint returns `text/plain` content, which will either be
rendered directly or embedded in a {"mimetype":..., "data": ...}
object when invoked as an RPC.
"""
return (
'text/plain', # Fixed MIME type of `text/plain`
'Meow world, meow!\n') # Meow!
async def api_purr(self, request_info, count=1, purr='purr'):
"""/purr [--count=<N>] [--purr=<sound>]
Authenticated endpoint taking a single argument. The response
will be encoded as JSON or using msgpack, depending on what the
caller requested.
The generated client convenience function, MyKitten.purr() will
be an async generator yielding results as they are received.
"""
_format = self.config.worker_name + ' says %(purr)s'
count = int(count) # CLI users send strings
for i in range(count):
result = {
'purr': purr * (i + 1), # Purring!
'_format': _format} # Formatting rule for CLI interface
yield (
None, # No MIME-type, let framework choose
result) # Result object
await asyncio.sleep(1)
if __name__ == '__main__':
import sys
MyKitten.Main(sys.argv[1:])