Use async model in api
This commit is contained in:
parent
1b97d6d7ca
commit
c05b023bdb
|
@ -1,18 +1,23 @@
|
||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
import logging
|
||||||
|
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
|
|
||||||
from phi.logging import get_logger
|
from phi.logging import get_logger
|
||||||
from phi.ldap.client import Client
|
from phi.async_ldap.client import AsyncClient
|
||||||
|
from phi.async_ldap.model import Hackers, Robots, Congregations
|
||||||
from phi.api.routes import api_routes
|
from phi.api.routes import api_routes
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
alog = logging.getLogger("asyncio")
|
||||||
|
|
||||||
|
|
||||||
def api_startup(app):
|
def api_startup(app):
|
||||||
app['ldap_client'].open()
|
app["ldap_client"].open()
|
||||||
|
|
||||||
|
|
||||||
def api_shutdown(app):
|
def api_shutdown(app):
|
||||||
app['ldap_client'].close()
|
app["ldap_client"].close()
|
||||||
|
|
||||||
|
|
||||||
def api_app(config):
|
def api_app(config):
|
||||||
|
@ -20,8 +25,12 @@ def api_app(config):
|
||||||
|
|
||||||
app = web.Application()
|
app = web.Application()
|
||||||
|
|
||||||
ldap_client = Client(**config.get('ldap', {}))
|
ldap_client = AsyncClient(**config.get("ldap", {}))
|
||||||
app['ldap_client'] = ldap_client
|
app["ldap_client"] = ldap_client
|
||||||
|
app["users"] = Hackers(ldap_client)
|
||||||
|
app["services"] = Robots(ldap_client)
|
||||||
|
app["groups"] = Congregations(ldap_client)
|
||||||
|
app["alog"] = alog
|
||||||
|
|
||||||
app.on_startup.append(api_startup)
|
app.on_startup.append(api_startup)
|
||||||
app.on_shutdown.append(api_shutdown)
|
app.on_shutdown.append(api_shutdown)
|
||||||
|
|
|
@ -2,23 +2,26 @@ from aiohttp.web import json_response, View
|
||||||
from aiohttp.web import HTTPNotFound, HTTPUnprocessableEntity
|
from aiohttp.web import HTTPNotFound, HTTPUnprocessableEntity
|
||||||
|
|
||||||
from phi.logging import get_logger
|
from phi.logging import get_logger
|
||||||
from phi.ldap.user import get_user_by_uid
|
|
||||||
from phi.api.utils import serialize
|
from phi.api.utils import serialize
|
||||||
|
from phi.async_ldap.model import Hackers
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class User(View):
|
class UserView(View):
|
||||||
async def get(self):
|
async def get(self):
|
||||||
uid = self.request.match_info.get('uid', None)
|
uid = self.request.match_info.get("uid", None)
|
||||||
|
|
||||||
if uid is None:
|
if uid is None:
|
||||||
return HTTPUnprocessableEntity()
|
return HTTPUnprocessableEntity()
|
||||||
|
|
||||||
client = self.request.app['ldap_client']
|
user = await self.request.app["users"].get_by_uid(uid)
|
||||||
user = get_user_by_uid(client, uid)
|
self.request.app["alog"].info("Found user %s", user)
|
||||||
|
|
||||||
if not user:
|
if not user:
|
||||||
return HTTPNotFound()
|
return HTTPNotFound()
|
||||||
|
|
||||||
return json_response(serialize(user))
|
result = await user.describe()
|
||||||
|
self.request.app["alog"].debug("Returning result %s", result)
|
||||||
|
|
||||||
|
return json_response(result)
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
from aiohttp.web import route
|
from aiohttp.web import route
|
||||||
|
|
||||||
from phi.api.rest import User
|
from phi.api.rest import UserView
|
||||||
|
|
||||||
|
|
||||||
api_routes = [
|
api_routes = [
|
||||||
route('*', '/user', User),
|
route("*", "/user", UserView),
|
||||||
route('*', '/user/', User),
|
route("*", "/user/", UserView),
|
||||||
route('*', '/user/{uid}', User)
|
route("*", "/user/{uid}", UserView),
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user