aiohttp integrated

This commit is contained in:
FoxMaSk
2017-08-18 18:50:56 +02:00
parent aea9df0978
commit c92e55208a
2 changed files with 23 additions and 33 deletions

View File

@@ -110,7 +110,7 @@ this will give you something like this :
client_id='myid', client_id='myid',
token=token, token=token,
extension='pdf') extension='pdf')
article = wall.get_entry(entry=1) article = wall.get_entry_export(entry=1)
with open("my_file.pdf", "wb") as f: with open("my_file.pdf", "wb") as f:
f.write(article) f.write(article)

View File

@@ -2,6 +2,7 @@
import logging import logging
import aiohttp import aiohttp
from aiohttp.http_exceptions import HttpProcessingError from aiohttp.http_exceptions import HttpProcessingError
from aiohttp.client_exceptions import ClientResponseError
# from aiohttp import HTTPError # from aiohttp import HTTPError
@@ -27,7 +28,7 @@ class Wallabag(object):
format = '' format = ''
username = '' username = ''
password = '' password = ''
aio_session = None aio_sess = None
def __init__(self, def __init__(self,
host='', host='',
@@ -37,7 +38,7 @@ class Wallabag(object):
extension='json', extension='json',
user_agent="WallabagPython/1.3 " user_agent="WallabagPython/1.3 "
"+https://github.com/foxmask/wallabag-api", "+https://github.com/foxmask/wallabag-api",
aio_session=None): aio_sess=None):
""" """
init variable init variable
:param host: string url to the official API Wallabag :param host: string url to the official API Wallabag
@@ -54,7 +55,7 @@ class Wallabag(object):
self.token = token self.token = token
self.format = extension self.format = extension
self.user_agent = user_agent self.user_agent = user_agent
self.aio_session = aio_session self.aio_sess = aio_sess
if self.format not in self.EXTENTIONS: if self.format not in self.EXTENTIONS:
raise ValueError("format invalid {0} should be one of {1}".format(self.format, self.EXTENTIONS)) raise ValueError("format invalid {0} should be one of {1}".format(self.format, self.EXTENTIONS))
@@ -78,30 +79,20 @@ class Wallabag(object):
if method in ('get', 'post', 'patch', 'delete', 'put'): if method in ('get', 'post', 'patch', 'delete', 'put'):
full_path = self.host + path full_path = self.host + path
if method == 'get': if method == 'get':
print(full_path) resp = await self.aio_sess.get(full_path, params=params)
print(params)
async with self.aio_session.get(full_path, params=params) as resp:
print(resp.status)
print(await resp.text())
elif method == 'post': elif method == 'post':
async with self.aio_session.post(full_path, data=params) as resp: resp = await self.aio_sess.post(full_path, data=params)
print(resp.status)
print(await resp.text())
elif method == 'patch': elif method == 'patch':
async with self.aio_session.patch(full_path, data=params) as resp: resp = await self.aio_sess.patch(full_path, data=params)
print(resp.status)
print(await resp.text())
elif method == 'delete': elif method == 'delete':
async with self.aio_session.delete(full_path, headers=params) as resp: resp = await self.aio_sess.delete(full_path, headers=params)
print(resp.status)
print(await resp.text())
elif method == 'put': elif method == 'put':
async with self.aio_session.delete(full_path, data=params) as resp: resp = await self.aio_sess.put(full_path, data=params)
print(resp.status)
print(await resp.text()) async with resp:
# return the content if its a binary one # return the content if its a binary one
if resp.headers['Content-Type'].startswith('application/pdf') or\ if resp.content_type.startswith('application/pdf') or \
resp.headers['Content-Type'].startswith('application/epub'): resp.content_type.startswith('application/epub'):
return await resp.read() return await resp.read()
else: else:
return await self.handle_json_response(resp) return await self.handle_json_response(resp)
@@ -116,18 +107,17 @@ class Wallabag(object):
:return the json data without 'root' node :return the json data without 'root' node
""" """
if responses.status != 200: if responses.status != 200:
raise HttpProcessingError(code=responses.status, message=await responses.json()) raise HttpProcessingError(code=responses.status,
message=await responses.json())
json_data = {} json_data = {}
try: try:
json_data = responses.json() json_data = responses.json()
except: except ClientResponseError as e:
# sometimes json_data does not return any json() without # sometimes json_data does not return any json() without
# any error. This is due to the grabbing URL which "rejects" # any error. This is due to the grabbing URL which "rejects"
# the URL # the URL
if 'errors' in json_data: logging.error("Wallabag: aiohttp error {code} {message}"
for error in json_data['errors']: .format(code=e.code, message=e.message))
error_json = json_data['errors'][error]['content']
logging.error("Wallabag: {error}".format(error=error_json))
return await json_data return await json_data
@staticmethod @staticmethod