Ejemplo n.º 1
0
def main():
    loop = asyncio.get_event_loop()

    server = aiorest.RESTServer(hostname='127.0.0.1', loop=loop)
    server.add_url('GET', '/hello-world', handler)
    server.add_url('GET', '/hello/{name}', say_hello)

    srv = loop.run_until_complete(
        loop.create_server(server.make_handler, '127.0.0.1', 8080))

    @asyncio.coroutine
    def query():
        resp = yield from aiohttp.request('GET',
                                          'http://127.0.0.1:8080/hello-world',
                                          loop=loop)
        json_data = yield from resp.json()
        print(json_data)

        name = os.environ.get('USER', 'John')
        resp = yield from aiohttp.request(
            'GET', 'http://127.0.0.1:8080/hello/{}'.format(name))

        json_data = yield from resp.json()
        print(json_data)

    loop.run_until_complete(query())
    srv.close()
    loop.run_until_complete(srv.wait_closed())
    loop.close()
Ejemplo n.º 2
0
def main():
    loop = asyncio.get_event_loop()

    identity_policy = CookieIdentityPolicy()
    auth_policy = DictionaryAuthorizationPolicy({'chris': ('read',)})

    server = aiorest.RESTServer(
        hostname='127.0.0.1', loop=loop,
        identity_policy=identity_policy,
        auth_policy=auth_policy
    )
    server.add_url('GET', '/auth/{permission}', handler)

    srv = loop.run_until_complete(loop.create_server(
        server.make_handler, '127.0.0.1', 8080))

    @asyncio.coroutine
    def query():
        resp = yield from aiohttp.request(
            'GET', 'http://127.0.0.1:8080/auth/read',
            cookies={},
            loop=loop)
        json_data = yield from resp.json()
        assert json_data == {'error': 'Identity not found'}

        resp = yield from aiohttp.request(
            'GET', 'http://127.0.0.1:8080/auth/read',
            cookies={'user_id': 'john'},
            loop=loop)
        json_data = yield from resp.json()
        assert json_data == {'error': 'User not found'}

        # correct user, must have read permission
        resp = yield from aiohttp.request(
            'GET', 'http://127.0.0.1:8080/auth/read',
            cookies={'user_id': 'chris'},
            loop=loop)
        json_data = yield from resp.json()
        assert json_data == {'allowed': True}

        # correct user, don't have write permission
        resp = yield from aiohttp.request(
            'GET', 'http://127.0.0.1:8080/auth/write',
            cookies={'user_id': 'chris'},
            loop=loop)
        json_data = yield from resp.json()
        assert json_data == {'allowed': False}

        print('Success')

    loop.run_until_complete(query())
    srv.close()
    loop.run_until_complete(srv.wait_closed())
    loop.close()
Ejemplo n.º 3
0
def main():
    loop = asyncio.get_event_loop()

    handler = Handler()

    redis = loop.run_until_complete(
        Connection.create(db=0, encoder=BytesEncoder(), loop=loop))

    session_factory = RedisSessionFactory(redis,
                                          secret_key=b'secret',
                                          cookie_name='test_cookie',
                                          loop=loop)

    server = aiorest.RESTServer(hostname='127.0.0.1',
                                keep_alive=75,
                                session_factory=session_factory,
                                loop=loop)

    server.add_url('GET', '/count', handler.counter)

    srv = loop.run_until_complete(
        loop.create_server(server.make_handler, '127.0.0.1', 8080))

    @asyncio.coroutine
    def query():
        connector = aiohttp.TCPConnector(share_cookies=True, loop=loop)

        for _ in range(6):
            resp = yield from aiohttp.request('GET',
                                              'http://127.0.0.1:8080/count',
                                              connector=connector,
                                              loop=loop)
            data = yield from resp.json()
            print('Count is', data)

    loop.run_until_complete(query())

    srv.close()
    loop.run_until_complete(srv.wait_closed())
    loop.close()
Ejemplo n.º 4
0
#!var/env/bin/python
import asyncio
import aiorest

import cfg
import lib.logging
import prepare_static
import urls


lib.logging.setup(cfg.LOG_FILE)
prepare_static.client_config(urls=urls.CLIENT_URLS)
prepare_static.render_html('index.html')


server = aiorest.RESTServer(hostname='127.0.0.1')
for url in urls.SERVER_URLS:
    server.add_url(*url)


loop = asyncio.get_event_loop()
loop.run_until_complete(loop.create_server(
    server.make_handler, '127.0.0.1', cfg.PORT))
try:
    loop.run_forever()
except KeyboardInterrupt:
    pass