コード例 #1
0
async def test_get_json(aiohttp_client, loop):
    async def hello(request):
        return web.json_response({'test': 'Hello, world'})

    app = web.Application()
    app.router.add_get('/test', hello)
    client = AiohttpAsyncHttpClient(session=await aiohttp_client(app))
    resp = await client.get('/test')
    assert resp.status_code == 200
    json = await resp.json
    assert 'Hello, world' == json['test']
コード例 #2
0
async def test_put(aiohttp_client, loop):
    async def hello(request):
        return web.Response(text='Hello, world')

    app = web.Application()
    app.router.add_put('/test', hello)

    client = AiohttpAsyncHttpClient(session=await aiohttp_client(app))
    resp = await client.put('/test')
    assert resp.status_code == 200
    text = await resp.text
    assert 'Hello, world' in text
コード例 #3
0
async def test_get_iter(aiohttp_client, loop):
    async def hello(request):
        return web.Response(text='Hello, world')

    app = web.Application()
    app.router.add_get('/test', hello)

    client = AiohttpAsyncHttpClient(session=await aiohttp_client(app))
    resp = await client.get('/test')
    assert resp.status_code == 200

    buffer = ''
    async for data in resp.iter_content(1024):
        buffer += data.decode('utf-8')
    assert 'Hello, world' in buffer
コード例 #4
0
async def main(port=8000):
    session = aiohttp.ClientSession()
    async_http_client = AiohttpAsyncHttpClient(session)
    line_bot_api = AsyncLineBotApi(channel_access_token, async_http_client)
    parser = WebhookParser(channel_secret)

    handler = Handler(line_bot_api, parser)

    app = web.Application()
    app.add_routes([web.post('/callback', handler.echo)])

    runner = web.AppRunner(app)
    await runner.setup()
    site = TCPSite(runner=runner, port=port)
    await site.start()
    while True:
        await asyncio.sleep(3600)  # sleep forever
コード例 #5
0
async def test_async_profile(aiohttp_client, loop):
    expect = {
        'displayName': 'test',
        'userId': 'test',
        'language': 'en',
        'pictureUrl': 'https://obs.line-apps.com/...',
        'statusMessage': 'Hello, LINE!'
    }

    async def profile(request):
        return web.json_response(expect)

    app = web.Application()
    app.router.add_get('//v2/bot/profile/test', profile)

    aiohttp = await aiohttp_client(app,
                                   server_kwargs={"skip_url_asserts": True})
    async_client = AiohttpAsyncHttpClient(session=aiohttp)

    bot = AsyncLineBotApi('TOKENTOKEN', async_client, endpoint='/')
    profile_response = await bot.get_profile('test')
    assert profile_response.user_id == expect['userId']
    assert profile_response.display_name == expect['displayName']
コード例 #6
0
    MessageEvent, TextMessage, TextSendMessage,
)

# get channel_secret and channel_access_token from your environment variable
channel_secret = os.getenv('LINE_CHANNEL_SECRET', None)
channel_access_token = os.getenv('LINE_CHANNEL_ACCESS_TOKEN', None)
if channel_secret is None:
    print('Specify LINE_CHANNEL_SECRET as environment variable.')
    sys.exit(1)
if channel_access_token is None:
    print('Specify LINE_CHANNEL_ACCESS_TOKEN as environment variable.')
    sys.exit(1)

app = FastAPI()
session = aiohttp.ClientSession()
async_http_client = AiohttpAsyncHttpClient(session)
line_bot_api = AsyncLineBotApi(channel_access_token, async_http_client)
parser = WebhookParser(channel_secret)


@app.post("/callback")
async def handle_callback(request: Request):
    signature = request.headers['X-Line-Signature']

    # get request body as text
    body = await request.body()
    body = body.decode()

    try:
        events = parser.parse(body, signature)
    except InvalidSignatureError:
コード例 #7
0
 async def test_async_get_profile_exception(self):
     self.server.skip_url_asserts = True
     async_client = AiohttpAsyncHttpClient(session=self.client)
     bot = AsyncLineBotApi('TOKENTOKEN', async_client, endpoint='/')
     with self.assertRaises(LineBotApiError):
         await bot.get_profile('failed')