Python aiohttp.web.Application() Examples

The following are 30 code examples of aiohttp.web.Application(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module aiohttp.web , or try the search function .
Example #1
Source File: auth.py    From backend.ai-manager with GNU Lesser General Public License v3.0 6 votes vote down vote up
def create_app(default_cors_options: CORSOptions) -> Tuple[web.Application, Iterable[WebMiddleware]]:
    app = web.Application()
    app['prefix'] = 'auth'  # slashed to distinguish with "/vN/authorize"
    app['api_versions'] = (1, 2, 3, 4)
    cors = aiohttp_cors.setup(app, defaults=default_cors_options)
    root_resource = cors.add(app.router.add_resource(r''))
    cors.add(root_resource.add_route('GET', test))
    cors.add(root_resource.add_route('POST', test))
    test_resource = cors.add(app.router.add_resource('/test'))
    cors.add(test_resource.add_route('GET', test))
    cors.add(test_resource.add_route('POST', test))
    cors.add(app.router.add_route('POST', '/authorize', authorize))
    cors.add(app.router.add_route('GET', '/role', get_role))
    cors.add(app.router.add_route('POST', '/signup', signup))
    cors.add(app.router.add_route('POST', '/signout', signout))
    cors.add(app.router.add_route('POST', '/update-password', update_password))
    cors.add(app.router.add_route('GET', '/ssh-keypair', get_ssh_keypair))
    cors.add(app.router.add_route('PATCH', '/ssh-keypair', refresh_ssh_keypair))
    return app, [auth_middleware] 
Example #2
Source File: test_aiohttp.py    From gql with MIT License 6 votes vote down vote up
def test_aiohttp_cannot_execute_if_not_connected(event_loop, aiohttp_server):
    async def handler(request):
        return web.Response(text=query1_server_answer, content_type="application/json")

    app = web.Application()
    app.router.add_route("POST", "/", handler)
    server = await aiohttp_server(app)

    url = server.make_url("/")

    sample_transport = AIOHTTPTransport(url=url, timeout=10)

    query = gql(query1_str)

    with pytest.raises(TransportClosed):
        await sample_transport.execute(query) 
Example #3
Source File: test_aiohttp.py    From gql with MIT License 6 votes vote down vote up
def test_aiohttp_subscribe_not_supported(event_loop, aiohttp_server):
    async def handler(request):
        return web.Response(text="does not matter", content_type="application/json")

    app = web.Application()
    app.router.add_route("POST", "/", handler)
    server = await aiohttp_server(app)

    url = server.make_url("/")

    sample_transport = AIOHTTPTransport(url=url)

    async with Client(transport=sample_transport,) as session:

        query = gql(query1_str)

        with pytest.raises(NotImplementedError):
            async for result in session.subscribe(query):
                pass 
Example #4
Source File: test_aiohttp.py    From gql with MIT License 6 votes vote down vote up
def test_aiohttp_error_code(event_loop, aiohttp_server):
    async def handler(request):
        return web.Response(
            text=query1_server_error_answer, content_type="application/json"
        )

    app = web.Application()
    app.router.add_route("POST", "/", handler)
    server = await aiohttp_server(app)

    url = server.make_url("/")

    sample_transport = AIOHTTPTransport(url=url)

    async with Client(transport=sample_transport,) as session:

        query = gql(query1_str)

        with pytest.raises(TransportQueryError):
            await session.execute(query) 
Example #5
Source File: test_aiohttp.py    From gql with MIT License 6 votes vote down vote up
def test_aiohttp_error_code_500(event_loop, aiohttp_server):
    async def handler(request):
        # Will generate http error code 500
        raise Exception("Server error")

    app = web.Application()
    app.router.add_route("POST", "/", handler)
    server = await aiohttp_server(app)

    url = server.make_url("/")

    sample_transport = AIOHTTPTransport(url=url)

    async with Client(transport=sample_transport,) as session:

        query = gql(query1_str)

        with pytest.raises(TransportServerError):
            await session.execute(query) 
Example #6
Source File: test_aiohttp.py    From gql with MIT License 6 votes vote down vote up
def test_aiohttp_query(event_loop, aiohttp_server):
    async def handler(request):
        return web.Response(text=query1_server_answer, content_type="application/json")

    app = web.Application()
    app.router.add_route("POST", "/", handler)
    server = await aiohttp_server(app)

    url = server.make_url("/")

    sample_transport = AIOHTTPTransport(url=url, timeout=10)

    async with Client(transport=sample_transport,) as session:

        query = gql(query1_str)

        # Execute query asynchronously
        result = await session.execute(query)

        continents = result["continents"]

        africa = continents[0]

        assert africa["code"] == "AF" 
Example #7
Source File: test_requests.py    From gql with MIT License 6 votes vote down vote up
def test_requests_cannot_connect_twice(event_loop, aiohttp_server):
    async def handler(request):
        return web.Response(text=query1_server_answer, content_type="application/json")

    app = web.Application()
    app.router.add_route("POST", "/", handler)
    server = await aiohttp_server(app)

    url = server.make_url("/")

    def test_code():
        sample_transport = RequestsHTTPTransport(url=url)

        with Client(transport=sample_transport,) as session:

            with pytest.raises(TransportAlreadyConnected):
                session.transport.connect()

    await run_sync_test(event_loop, server, test_code) 
Example #8
Source File: test_requests.py    From gql with MIT License 6 votes vote down vote up
def test_requests_invalid_protocol(event_loop, aiohttp_server, response):
    async def handler(request):
        return web.Response(text=response, content_type="application/json")

    app = web.Application()
    app.router.add_route("POST", "/", handler)
    server = await aiohttp_server(app)

    url = server.make_url("/")

    def test_code():
        sample_transport = RequestsHTTPTransport(url=url)

        with Client(transport=sample_transport,) as session:

            query = gql(query1_str)

            with pytest.raises(TransportProtocolError):
                session.execute(query)

    await run_sync_test(event_loop, server, test_code) 
Example #9
Source File: test_requests.py    From gql with MIT License 6 votes vote down vote up
def test_requests_error_code(event_loop, aiohttp_server):
    async def handler(request):
        return web.Response(
            text=query1_server_error_answer, content_type="application/json"
        )

    app = web.Application()
    app.router.add_route("POST", "/", handler)
    server = await aiohttp_server(app)

    url = server.make_url("/")

    def test_code():
        sample_transport = RequestsHTTPTransport(url=url)

        with Client(transport=sample_transport,) as session:

            query = gql(query1_str)

            with pytest.raises(TransportQueryError):
                session.execute(query)

    await run_sync_test(event_loop, server, test_code) 
Example #10
Source File: test_aiohttp.py    From gql with MIT License 6 votes vote down vote up
def test_aiohttp_invalid_protocol(event_loop, aiohttp_server, response):
    async def handler(request):
        return web.Response(text=response, content_type="application/json")

    app = web.Application()
    app.router.add_route("POST", "/", handler)
    server = await aiohttp_server(app)

    url = server.make_url("/")

    sample_transport = AIOHTTPTransport(url=url)

    async with Client(transport=sample_transport,) as session:

        query = gql(query1_str)

        with pytest.raises(TransportProtocolError):
            await session.execute(query) 
Example #11
Source File: resource.py    From backend.ai-manager with GNU Lesser General Public License v3.0 6 votes vote down vote up
def create_app(default_cors_options: CORSOptions) -> Tuple[web.Application, Iterable[WebMiddleware]]:
    app = web.Application()
    app['api_versions'] = (4,)
    cors = aiohttp_cors.setup(app, defaults=default_cors_options)
    add_route = app.router.add_route
    cors.add(add_route('GET',  '/presets', list_presets))
    cors.add(add_route('POST', '/check-presets', check_presets))
    cors.add(add_route('POST', '/recalculate-usage', recalculate_usage))
    cors.add(add_route('GET',  '/usage/month', usage_per_month))
    cors.add(add_route('GET',  '/usage/period', usage_per_period))
    cors.add(add_route('GET',  '/stats/user/month', user_month_stats))
    cors.add(add_route('GET',  '/stats/admin/month', admin_month_stats))
    cors.add(add_route('GET',  '/watcher', get_watcher_status))
    cors.add(add_route('POST', '/watcher/agent/start', watcher_agent_start))
    cors.add(add_route('POST', '/watcher/agent/stop', watcher_agent_stop))
    cors.add(add_route('POST', '/watcher/agent/restart', watcher_agent_restart))
    return app, [] 
Example #12
Source File: headnode.py    From hsds with Apache License 2.0 6 votes vote down vote up
def create_app(loop):
    """Create headnode aiohttp application

    :param loop: The asyncio loop to use for the application
    :rtype: aiohttp.web.Application
    """
    app = init()  

    # create a client Session here so that all client requests
    #   will share the same connection pool 
    app["loop"] = loop
    app["last_health_check"] = 0

    app.on_startup.append(start_background_tasks)

    return app 
Example #13
Source File: error_monitor.py    From backend.ai-manager with GNU Lesser General Public License v3.0 6 votes vote down vote up
def handle_agent_error(self, app: web.Application, agent_id: AgentId, event_name: str,
                                 message: str,
                                 traceback: str = None,
                                 user: Any = None,
                                 context_env: Any = None,
                                 severity: LogSeverity = None) -> None:
        if severity is None:
            severity = LogSeverity.ERROR
        async with self.dbpool.acquire() as conn, conn.begin():
            query = error_logs.insert().values({
                'severity': severity,
                'source': 'agent',
                'user': user,
                'message': message,
                'context_lang': 'python',
                'context_env': context_env,
                'traceback': traceback
            })
            await conn.execute(query)
        log.debug('Agent log collected: {}', message) 
Example #14
Source File: test_requests.py    From gql with MIT License 6 votes vote down vote up
def test_requests_error_code_500(event_loop, aiohttp_server):
    async def handler(request):
        # Will generate http error code 500
        raise Exception("Server error")

    app = web.Application()
    app.router.add_route("POST", "/", handler)
    server = await aiohttp_server(app)

    url = server.make_url("/")

    def test_code():
        sample_transport = RequestsHTTPTransport(url=url)

        with Client(transport=sample_transport,) as session:

            query = gql(query1_str)

            with pytest.raises(TransportServerError):
                session.execute(query)

    await run_sync_test(event_loop, server, test_code) 
Example #15
Source File: server.py    From backend.ai-manager with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _init_subapp(pkg_name: str,
                 root_app: web.Application,
                 subapp: web.Application,
                 global_middlewares: Iterable[WebMiddleware]) -> None:
    subapp.on_response_prepare.append(on_prepare)

    async def _copy_public_interface_objs(subapp: web.Application):
        # Allow subapp's access to the root app properties.
        # These are the public APIs exposed to plugins as well.
        for key, obj in public_interface_objs.items():
            subapp[key] = obj

    # We must copy the public interface prior to all user-defined startup signal handlers.
    subapp.on_startup.insert(0, _copy_public_interface_objs)
    prefix = subapp.get('prefix', pkg_name.split('.')[-1].replace('_', '-'))
    aiojobs.aiohttp.setup(subapp, **root_app['scheduler_opts'])
    root_app.add_subapp('/' + prefix, subapp)
    root_app.middlewares.extend(global_middlewares) 
Example #16
Source File: server.py    From backend.ai-manager with GNU Lesser General Public License v3.0 6 votes vote down vote up
def handle_loop_error(
    root_app: web.Application,
    loop: asyncio.AbstractEventLoop,
    context: Mapping[str, Any],
) -> None:
    if isinstance(loop, aiojobs.Scheduler):
        loop = current_loop()
    exception = context.get('exception')
    msg = context.get('message', '(empty message)')
    if exception is not None:
        if sys.exc_info()[0] is not None:
            log.exception('Error inside event loop: {0}', msg)
            if 'error_monitor' in root_app:
                loop.create_task(root_app['error_monitor'].capture_exception())
        else:
            exc_info = (type(exception), exception, exception.__traceback__)
            log.error('Error inside event loop: {0}', msg, exc_info=exc_info)
            if 'error_monitor' in root_app:
                loop.create_task(root_app['error_monitor'].capture_exception(exception)) 
Example #17
Source File: session.py    From backend.ai-manager with GNU Lesser General Public License v3.0 6 votes vote down vote up
def handle_kernel_lifecycle(app: web.Application, agent_id: AgentId, event_name: str,
                                  raw_kernel_id: str,
                                  reason: str = None,
                                  exit_code: int = None) -> None:
    kernel_id = uuid.UUID(raw_kernel_id)
    registry = app['registry']
    if event_name == 'kernel_preparing':
        await registry.set_kernel_status(kernel_id, KernelStatus.PREPARING, reason)
    elif event_name == 'kernel_pulling':
        await registry.set_kernel_status(kernel_id, KernelStatus.PULLING, reason)
    elif event_name == 'kernel_creating':
        await registry.set_kernel_status(kernel_id, KernelStatus.PREPARING, reason)
    elif event_name == 'kernel_started':
        # The create_kernel() RPC caller will set the "RUNNING" status.
        pass
    elif event_name == 'kernel_terminating':
        # The destroy_kernel() API handler will set the "TERMINATING" status.
        pass
    elif event_name == 'kernel_terminated':
        await registry.mark_kernel_terminated(kernel_id, reason, exit_code) 
Example #18
Source File: session.py    From backend.ai-manager with GNU Lesser General Public License v3.0 6 votes vote down vote up
def handle_instance_lifecycle(app: web.Application, agent_id: AgentId, event_name: str,
                                    reason: str = None) -> None:
    if event_name == 'instance_started':
        log.info('instance_lifecycle: ag:{0} joined ({1})', agent_id, reason)
        await app['registry'].update_instance(agent_id, {
            'status': AgentStatus.ALIVE,
        })
    elif event_name == 'instance_terminated':
        if reason == 'agent-lost':
            await app['registry'].mark_agent_terminated(agent_id, AgentStatus.LOST)
        elif reason == 'agent-restart':
            log.info('agent@{0} restarting for maintenance.', agent_id)
            await app['registry'].update_instance(agent_id, {
                'status': AgentStatus.RESTARTING,
            })
        else:
            # On normal instance termination, kernel_terminated events were already
            # triggered by the agent.
            await app['registry'].mark_agent_terminated(agent_id, AgentStatus.TERMINATED) 
Example #19
Source File: session.py    From backend.ai-manager with GNU Lesser General Public License v3.0 6 votes vote down vote up
def init(app: web.Application) -> None:
    event_dispatcher = app['event_dispatcher']
    event_dispatcher.consume('kernel_preparing', app, handle_kernel_lifecycle)
    event_dispatcher.consume('kernel_pulling', app, handle_kernel_lifecycle)
    event_dispatcher.consume('kernel_creating', app, handle_kernel_lifecycle)
    event_dispatcher.consume('kernel_started', app, handle_kernel_lifecycle)
    event_dispatcher.consume('kernel_terminating', app, handle_kernel_lifecycle)
    event_dispatcher.consume('kernel_terminated', app, handle_kernel_lifecycle)
    event_dispatcher.consume('kernel_success', app, handle_batch_result)
    event_dispatcher.consume('kernel_failure', app, handle_batch_result)
    event_dispatcher.consume('kernel_stat_sync', app, handle_kernel_stat_sync)
    event_dispatcher.consume('kernel_log', app, handle_kernel_log)
    event_dispatcher.consume('instance_started', app, handle_instance_lifecycle)
    event_dispatcher.consume('instance_terminated', app, handle_instance_lifecycle)
    event_dispatcher.consume('instance_heartbeat', app, handle_instance_heartbeat)
    event_dispatcher.consume('instance_stats', app, handle_instance_stats)

    app['pending_waits'] = set()

    # Scan ALIVE agents
    app['agent_lost_checker'] = aiotools.create_timer(
        functools.partial(check_agent_lost, app), 1.0)
    app['stats_task'] = asyncio.create_task(stats_report_timer(app)) 
Example #20
Source File: session.py    From backend.ai-manager with GNU Lesser General Public License v3.0 6 votes vote down vote up
def shutdown(app: web.Application) -> None:
    app['agent_lost_checker'].cancel()
    await app['agent_lost_checker']
    app['stats_task'].cancel()
    await app['stats_task']

    checked_tasks = ('kernel_agent_event_collector', 'kernel_ddtimer')
    for tname in checked_tasks:
        t = app.get(tname, None)
        if t and not t.done():
            t.cancel()
            await t

    for task in app['pending_waits']:
        task.cancel()
        try:
            await task
        except asyncio.CancelledError:
            pass 
Example #21
Source File: session.py    From backend.ai-manager with GNU Lesser General Public License v3.0 6 votes vote down vote up
def create_app(default_cors_options: CORSOptions) -> Tuple[web.Application, Iterable[WebMiddleware]]:
    app = web.Application()
    app.on_startup.append(init)
    app.on_shutdown.append(shutdown)
    app['api_versions'] = (1, 2, 3, 4)
    cors = aiohttp_cors.setup(app, defaults=default_cors_options)
    cors.add(app.router.add_route('POST', '/_/create-from-template', create_from_template))
    cors.add(app.router.add_route('GET',  '/_/match', match_sessions))
    cors.add(app.router.add_route('POST', '', create_from_params))
    session_resource = cors.add(app.router.add_resource(r'/{session_name}'))
    cors.add(session_resource.add_route('GET',    get_info))
    cors.add(session_resource.add_route('PATCH',  restart))
    cors.add(session_resource.add_route('DELETE', destroy))
    cors.add(session_resource.add_route('POST',   execute))
    task_log_resource = cors.add(app.router.add_resource(r'/_/logs'))
    cors.add(task_log_resource.add_route('HEAD', get_task_logs))
    cors.add(task_log_resource.add_route('GET',  get_task_logs))
    cors.add(app.router.add_route('GET',  '/{session_name}/logs', get_container_logs))
    cors.add(app.router.add_route('POST', '/{session_name}/interrupt', interrupt))
    cors.add(app.router.add_route('POST', '/{session_name}/complete', complete))
    cors.add(app.router.add_route('POST', '/{session_name}/upload', upload_files))
    cors.add(app.router.add_route('GET',  '/{session_name}/download', download_files))
    cors.add(app.router.add_route('GET',  '/{session_name}/download_single', download_single))
    cors.add(app.router.add_route('GET',  '/{session_name}/files', list_files))
    return app, [] 
Example #22
Source File: server.py    From backend.ai-manager with GNU Lesser General Public License v3.0 6 votes vote down vote up
def config_server_ctx(app: web.Application) -> AsyncIterator[None]:
    # populate public interfaces
    app['config_server'] = ConfigServer(
        app, app['config']['etcd']['addr'],
        app['config']['etcd']['user'], app['config']['etcd']['password'],
        app['config']['etcd']['namespace'])

    app['config'].update(
        await load_shared_config(app['config_server'].etcd)
    )
    app['config']['redis'] = redis_config_iv.check(
        await app['config_server'].etcd.get_prefix('config/redis')
    )
    _update_public_interface_objs(app)
    yield
    await app['config_server'].close() 
Example #23
Source File: server.py    From maubot with GNU Affero General Public License v3.0 6 votes vote down vote up
def __init__(self, management_api: web.Application, config: Config,
                 loop: asyncio.AbstractEventLoop) -> None:
        self.loop = loop or asyncio.get_event_loop()
        self.app = web.Application(loop=self.loop, client_max_size=100 * 1024 * 1024)
        self.config = config

        self.setup_appservice()
        self.app.add_subapp(config["server.base_path"], management_api)
        self.setup_instance_subapps()
        self.setup_management_ui()

        self.runner = web.AppRunner(self.app, access_log_class=AccessLogger) 
Example #24
Source File: logs.py    From backend.ai-manager with GNU Lesser General Public License v3.0 5 votes vote down vote up
def shutdown(app: web.Application) -> None:
    app['log_cleanup_task'].cancel()
    await app['log_cleanup_task'] 
Example #25
Source File: manager.py    From backend.ai-manager with GNU Lesser General Public License v3.0 5 votes vote down vote up
def init(app: web.Application) -> None:
    app['status_watch_task'] = asyncio.create_task(detect_status_update(app)) 
Example #26
Source File: logs.py    From backend.ai-manager with GNU Lesser General Public License v3.0 5 votes vote down vote up
def init(app: web.Application) -> None:
    app['log_cleanup_lock'] = aioredlock.Aioredlock([
        {'host': str(app['config']['redis']['addr'][0]),
         'port': app['config']['redis']['addr'][1],
         'password': app['config']['redis']['password'] if app['config']['redis']['password'] else None,
         'db': REDIS_LIVE_DB},
    ])
    app['log_cleanup_task'] = aiotools.create_timer(
        functools.partial(log_cleanup_task, app), 5.0) 
Example #27
Source File: ratelimit.py    From backend.ai-manager with GNU Lesser General Public License v3.0 5 votes vote down vote up
def create_app(default_cors_options: CORSOptions) -> Tuple[web.Application, Iterable[WebMiddleware]]:
    app = web.Application()
    app['api_versions'] = (1, 2, 3, 4)
    app.on_startup.append(init)
    app.on_shutdown.append(shutdown)
    # middleware must be wrapped by web.middleware at the outermost level.
    return app, [web.middleware(apartial(rlim_middleware, app))] 
Example #28
Source File: logs.py    From backend.ai-manager with GNU Lesser General Public License v3.0 5 votes vote down vote up
def log_cleanup_task(app: web.Application, interval):
    dbpool = app['dbpool']
    etcd = app['config_server'].etcd
    raw_lifetime = await etcd.get('config/logs/error/retention')
    if raw_lifetime is None:
        raw_lifetime = '90d'
    checker = tx.TimeDuration()
    try:
        lifetime = checker.check(raw_lifetime)
    except ValueError:
        lifetime = dt.timedelta(days=90)
        log.info('Retention value specified in etcd not recognized by'
                 'trafaret validator, falling back to 90 days')
    boundary = datetime.now() - lifetime
    try:
        lock = await app['log_cleanup_lock'].lock('gateway.logs')
        async with lock:
            async with dbpool.acquire() as conn, conn.begin():
                query = (sa.select([error_logs.c.id])
                            .select_from(error_logs)
                            .where(error_logs.c.created_at < boundary))
                result = await conn.execute(query)
                log_ids = []
                async for row in result:
                    log_ids.append(row['id'])
                if len(log_ids) > 0:
                    log.info('Cleaning up {} log{}', len(log_ids), 's' if len(log_ids) > 1 else '')
                query = error_logs.delete().where(error_logs.c.id.in_(log_ids))
                result = await conn.execute(query)
                assert result.rowcount == len(log_ids)

    except aioredlock.LockError:
        log.debug('schedule(): temporary locking failure; will be retried.') 
Example #29
Source File: ratelimit.py    From backend.ai-manager with GNU Lesser General Public License v3.0 5 votes vote down vote up
def shutdown(app: web.Application) -> None:
    try:
        await app['redis_rlim'].flushdb()
    except (ConnectionResetError, ConnectionRefusedError):
        pass
    app['redis_rlim'].close()
    await app['redis_rlim'].wait_closed() 
Example #30
Source File: ratelimit.py    From backend.ai-manager with GNU Lesser General Public License v3.0 5 votes vote down vote up
def init(app: web.Application) -> None:
    rr = await redis.connect_with_retries(
        app['config']['redis']['addr'].as_sockaddr(),
        password=(app['config']['redis']['password']
                  if app['config']['redis']['password'] else None),
        timeout=3.0,
        encoding='utf8',
        db=REDIS_RLIM_DB)
    app['redis_rlim'] = rr
    app['redis_rlim_script'] = await rr.script_load(_rlim_script)