-
Notifications
You must be signed in to change notification settings - Fork 2
/
microservice.py
52 lines (38 loc) · 1.12 KB
/
microservice.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
import logging
from json import (
JSONDecodeError,
)
from aiohttp import (
web,
)
from ..domain import (
Microservice,
)
from .router import (
routes,
)
logger = logging.getLogger(__name__)
@routes.view("/microservices/{name}")
class MicroserviceView(web.View):
async def post(self):
body = await self.get_body()
redis_client = self.request.app["db_client"]
try:
microservice = Microservice(**body)
except TypeError as exc:
raise web.HTTPBadRequest(text=exc.args[0])
await microservice.save(redis_client)
return web.HTTPCreated()
async def delete(self):
microservice_name = self.request.match_info["name"]
redis_client = self.request.app["db_client"]
await Microservice.delete(microservice_name, redis_client)
return web.HTTPOk()
async def get_body(self):
try:
body = await self.request.json()
except JSONDecodeError:
raise web.HTTPBadRequest(text="Empty or wrong body")
else:
body["name"] = self.request.match_info["name"]
return body