-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaioclient.py
More file actions
39 lines (30 loc) · 1.44 KB
/
aioclient.py
File metadata and controls
39 lines (30 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import io
from xml.etree import ElementTree
from aiohttp import ClientSession
async def request(method, *args, **kargs):
async with ClientSession() as session:
async with session.request(method, *args, **kargs) as response:
mimetype = response.content_type
if mimetype.endswith('json'):
return response.status, response.headers, await response.json()
elif mimetype.endswith('xml'):
with io.StringIO(await response.text()) as stream:
return response.status, response.headers, ElementTree.parse(stream)
elif mimetype.startswith('text') or mimetype.endswith('javascript') or mimetype.endswith('svg'):
return response.status, response.headers, await response.text()
else:
return response.status, response.headers, await response.read()
async def options(*args, **kargs):
return await request('OPTIONS', *args, **kargs)
async def head(*args, **kargs):
return await request('HEAD', *args, **kargs)
async def get(*args, **kargs):
return await request('GET', *args, **kargs)
async def post(*args, **kargs):
return await request('POST', *args, **kargs)
async def put(*args, **kargs):
return await request('PUT', *args, **kargs)
async def patch(*args, **kargs):
return await request('PATCH', *args, **kargs)
async def delete(*args, **kargs):
return await request('DELETE', *args, **kargs)