surik00

InfluxDB Create DB aiohttp request

Feb 2nd, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. import json
  2. import logging
  3. import asyncio
  4. import aiohttp
  5. from http import HTTPStatus
  6.  
  7.  
  8. logging.basicConfig(format=u'%(levelname)-8s [%(asctime)s] %(message)s', level=logging.INFO)
  9.  
  10. BASE_URL = 'http://localhost:8086/{}'
  11. # BASE_URL = 'https://httpbin.org/anything/{}'
  12. QUERY = 'query'
  13. CREATE_DB = 'CREATE DATABASE {}'
  14.  
  15.  
  16. async def createDB(name):
  17.     async with aiohttp.ClientSession() as session:
  18.         payload = {'q': CREATE_DB.format(name)}
  19.         async with await session.post(BASE_URL.format(QUERY), data=json.dumps(payload)) as response:
  20.             if response.status != HTTPStatus.OK:
  21.                 logging.error(f'Unexpected response code: {response.status}')
  22.             text = await response.text()
  23.             logging.info(f'Response text:\n{text}')
  24.  
  25.  
  26. loop = asyncio.get_event_loop()
  27. loop.run_until_complete(createDB('mydb'))
Advertisement
Add Comment
Please, Sign In to add comment