This commit is contained in:
FoxMaSk
2017-08-18 13:36:53 +02:00
parent 9fbd7cc28d
commit aea9df0978

View File

@@ -27,6 +27,7 @@ class Wallabag(object):
format = '' format = ''
username = '' username = ''
password = '' password = ''
aio_session = None
def __init__(self, def __init__(self,
host='', host='',
@@ -35,7 +36,8 @@ class Wallabag(object):
client_secret='', client_secret='',
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):
""" """
init variable init variable
:param host: string url to the official API Wallabag :param host: string url to the official API Wallabag
@@ -44,6 +46,7 @@ class Wallabag(object):
:param client_secret client secret :param client_secret client secret
:param extension: xml|json|txt|csv|pdf|epub|mobi|html :param extension: xml|json|txt|csv|pdf|epub|mobi|html
:param user_agent :param user_agent
:param aio_session aiohttp session
""" """
self.host = host self.host = host
self.client_id = client_id self.client_id = client_id
@@ -51,6 +54,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
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))
@@ -64,6 +68,7 @@ class Wallabag(object):
async def query(self, path, method='get', **params): async def query(self, path, method='get', **params):
""" """
Do a query to the System API Do a query to the System API
:param path: url to the API :param path: url to the API
:param method: the kind of query to do :param method: the kind of query to do
:param params: a dict with all the :param params: a dict with all the
@@ -72,45 +77,46 @@ 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
async with aiohttp.ClientSession() as session: if method == 'get':
if method == 'get': print(full_path)
async with session.get(full_path, params=params) as resp: print(params)
print(resp.status) async with self.aio_session.get(full_path, params=params) as resp:
print(await resp.text()) print(resp.status)
elif method == 'post': print(await resp.text())
async with session.post(full_path, data=params) as resp: elif method == 'post':
print(resp.status) async with self.aio_session.post(full_path, data=params) as resp:
print(await resp.text()) print(resp.status)
elif method == 'patch': print(await resp.text())
async with session.patch(full_path, data=params) as resp: elif method == 'patch':
print(resp.status) async with self.aio_session.patch(full_path, data=params) as resp:
print(await resp.text()) print(resp.status)
elif method == 'delete': print(await resp.text())
async with session.delete(full_path, headers=params) as resp: elif method == 'delete':
print(resp.status) async with self.aio_session.delete(full_path, headers=params) as resp:
print(await resp.text()) print(resp.status)
elif method == 'put': print(await resp.text())
async with session.delete(full_path, data=params) as resp: elif method == 'put':
print(resp.status) async with self.aio_session.delete(full_path, data=params) as resp:
print(await resp.text()) print(resp.status)
print(await resp.text())
# 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.headers['Content-Type'].startswith('application/pdf') or\
resp.headers['Content-Type'].startswith('application/epub'): resp.headers['Content-Type'].startswith('application/epub'):
return await resp.read() return await resp.read()
else: else:
return self.handle_json_response(resp) return await self.handle_json_response(resp)
else: else:
raise ValueError('method expected: get, post, patch, delete, put') raise ValueError('method expected: get, post, patch, delete, put')
@staticmethod @staticmethod
def handle_json_response(responses): async def handle_json_response(responses):
""" """
get the json data response get the json data response
:param responses: the json response :param responses: the json response
: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=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()
@@ -122,7 +128,7 @@ class Wallabag(object):
for error in json_data['errors']: for error in json_data['errors']:
error_json = json_data['errors'][error]['content'] error_json = json_data['errors'][error]['content']
logging.error("Wallabag: {error}".format(error=error_json)) logging.error("Wallabag: {error}".format(error=error_json))
return json_data return await json_data
@staticmethod @staticmethod
def __get_attr(what, type_attr, value_attr, **kwargs): def __get_attr(what, type_attr, value_attr, **kwargs):
@@ -134,7 +140,7 @@ class Wallabag(object):
:param kwargs: :param kwargs:
:return: :return:
""" """
value = int(kwargs[what]) if type_attr == 'int' else kwargs.get(what) value = int(kwargs[what]) if type_attr == 'int' else kwargs[what]
if what in kwargs and value in value_attr: if what in kwargs and value in value_attr:
return value return value
@@ -168,32 +174,32 @@ class Wallabag(object):
'order': 'desc', 'order': 'desc',
'page': 1, 'page': 1,
'perPage': 30, 'perPage': 30,
'tags': [], 'tags': '',
'since': 0}) 'since': 0})
params['archive'] = self.__get_attr(what='archive', params['archive'] = self.__get_attr(what='archive',
type_attr=int, type_attr=int,
value_attr=(0, 1), value_attr=(0, 1),
**kwargs) **params)
params['star'] = self.__get_attr(what='star', params['star'] = self.__get_attr(what='star',
type_attr=int, type_attr=int,
value_attr=(0, 1), value_attr=(0, 1),
**kwargs) **params)
params['delete'] = self.__get_attr(what='delete', params['delete'] = self.__get_attr(what='delete',
type_attr=int, type_attr=int,
value_attr=(0, 1), value_attr=(0, 1),
**kwargs) **params)
params['order'] = self.__get_attr(what='order', params['order'] = self.__get_attr(what='order',
type_attr=str, type_attr=str,
value_attr=('asc', 'desc'), value_attr=('asc', 'desc'),
**kwargs) **params)
if 'page' in kwargs and isinstance(kwargs['page'], int): if 'page' in kwargs and isinstance(kwargs['page'], int):
params['page'] = kwargs['page'] params['page'] = kwargs['page']
if 'perPage' in kwargs and isinstance(kwargs['perPage'], int): if 'perPage' in kwargs and isinstance(kwargs['perPage'], int):
params['perPage'] = kwargs['perPage'] params['perPage'] = kwargs['perPage']
if 'tags' in kwargs and isinstance(kwargs['tags'], list): if 'tags' in kwargs and isinstance(kwargs['tags'], list):
params['tags'] = kwargs['tags'] params['tags'] = ', '.join(kwargs['tags'])
if 'since' in kwargs and isinstance(kwargs['since'], int): if 'since' in kwargs and isinstance(kwargs['since'], int):
params['since'] = kwargs['since'] params['since'] = kwargs['since']
@@ -405,7 +411,7 @@ class Wallabag(object):
entry=entry, tag=tag, ext=self.format) entry=entry, tag=tag, ext=self.format)
return await self.query(url, "delete", **params) return await self.query(url, "delete", **params)
async def get_tags(self): async def get_tags(self, session):
""" """
GET /api/tags.{_format} GET /api/tags.{_format}
@@ -558,8 +564,9 @@ class Wallabag(object):
return await self.query(url, "get", **params) return await self.query(url, "get", **params)
@classmethod @classmethod
async def get_token(cls, session, host, **params): async def get_token(cls, host, **params):
""" """
:param host: host of the service :param host: host of the service
:param params: will contain : :param params: will contain :
@@ -571,10 +578,10 @@ class Wallabag(object):
:return: access token :return: access token
""" """
params['grant_type'] = "client_credentials" params['grant_type'] = "password"
path = "/oauth/v2/token" path = "/oauth/v2/token"
async with aiohttp.ClientSession(loop=session.loop) as session: async with aiohttp.ClientSession() as sess:
async with session.post(host + path, params=params) as resp: async with sess.post(host + path, data=params) as resp:
print(await resp.text()) data = await cls.handle_json_response(resp)
return await cls.handle_json_response(resp)['access_token'] return data.get("access_token")
# return await cls.handle_json_response(resp)