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 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
|
||||
|
||||
log = get_logger(__name__)
|
||||
alog = logging.getLogger("asyncio")
|
||||
|
||||
|
||||
def api_startup(app):
|
||||
app['ldap_client'].open()
|
||||
app["ldap_client"].open()
|
||||
|
||||
|
||||
def api_shutdown(app):
|
||||
app['ldap_client'].close()
|
||||
app["ldap_client"].close()
|
||||
|
||||
|
||||
def api_app(config):
|
||||
|
@ -20,8 +25,12 @@ def api_app(config):
|
|||
|
||||
app = web.Application()
|
||||
|
||||
ldap_client = Client(**config.get('ldap', {}))
|
||||
app['ldap_client'] = ldap_client
|
||||
ldap_client = AsyncClient(**config.get("ldap", {}))
|
||||
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_shutdown.append(api_shutdown)
|
||||
|
|
|
@ -2,23 +2,26 @@ from aiohttp.web import json_response, View
|
|||
from aiohttp.web import HTTPNotFound, HTTPUnprocessableEntity
|
||||
|
||||
from phi.logging import get_logger
|
||||
from phi.ldap.user import get_user_by_uid
|
||||
from phi.api.utils import serialize
|
||||
from phi.async_ldap.model import Hackers
|
||||
|
||||
log = get_logger(__name__)
|
||||
|
||||
|
||||
class User(View):
|
||||
class UserView(View):
|
||||
async def get(self):
|
||||
uid = self.request.match_info.get('uid', None)
|
||||
uid = self.request.match_info.get("uid", None)
|
||||
|
||||
if uid is None:
|
||||
return HTTPUnprocessableEntity()
|
||||
|
||||
client = self.request.app['ldap_client']
|
||||
user = get_user_by_uid(client, uid)
|
||||
user = await self.request.app["users"].get_by_uid(uid)
|
||||
self.request.app["alog"].info("Found user %s", user)
|
||||
|
||||
if not user:
|
||||
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 phi.api.rest import User
|
||||
|
||||
from phi.api.rest import UserView
|
||||
|
||||
api_routes = [
|
||||
route('*', '/user', User),
|
||||
route('*', '/user/', User),
|
||||
route('*', '/user/{uid}', User)
|
||||
route("*", "/user", UserView),
|
||||
route("*", "/user/", UserView),
|
||||
route("*", "/user/{uid}", UserView),
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue
Block a user