Skip to content

Commit

Permalink
Refactor code for api helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
samj1912 committed Jul 8, 2017
1 parent ee97c4e commit ad96473
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions picard/webservice/api_helpers.py
Expand Up @@ -35,7 +35,7 @@ def _wrap_xml_metadata(data):
'<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#">%s</metadata>' % data)


class APIHelper():
class APIHelper(object):

def __init__(self, host, port, api_path, webservice):
self.host = host
Expand Down Expand Up @@ -73,10 +73,10 @@ def delete(self, path_list, handler, priority=True, important=False,
queryargs=queryargs)


class MBAPIHelper():
class MBAPIHelper(APIHelper):

def __init__(self, webservice):
self.api_helper = APIHelper(config.setting['server_host'],config.setting['server_port'],
super().__init__(config.setting['server_host'], config.setting['server_port'],
"/ws/2/", webservice)

def _get_by_id(self, entitytype, entityid, handler, inc=None, queryargs=None,
Expand All @@ -86,7 +86,7 @@ def _get_by_id(self, entitytype, entityid, handler, inc=None, queryargs=None,
queryargs = {}
if inc:
queryargs["inc"] = "+".join(inc)
return self.api_helper.get(path_list, handler,
return self.get(path_list, handler,
priority=priority, important=important, mblogin=mblogin,
refresh=refresh, queryargs=queryargs)

Expand Down Expand Up @@ -138,7 +138,7 @@ def _find(self, entitytype, handler, **kwargs):
value = QUrl.toPercentEncoding(string_(value))
queryargs[string_(name)] = value
path_list = [entitytype]
return self.api_helper.get(path_list, handler, queryargs=queryargs,
return self.get(path_list, handler, queryargs=queryargs,
priority=True, important=True, mblogin=False,
refresh=False)

Expand All @@ -156,7 +156,7 @@ def _browse(self, entitytype, handler, inc=None, **kwargs):
queryargs = kwargs
if inc:
queryargs["inc"] = "+".join(inc)
return self.api_helper.get(path_list, handler, queryargs=queryargs,
return self.get(path_list, handler, queryargs=queryargs,
priority=True, important=True, mblogin=False,
refresh=False)

Expand All @@ -171,7 +171,7 @@ def submit_ratings(self, ratings, handler):
(i[1], j*20) for i, j in ratings.items() if i[0] == 'recording']))

data = _wrap_xml_metadata('<recording-list>%s</recording-list>' % recordings)
return self.api_helper.post(path_list, data, handler, priority=True, queryargs=params)
return self.post(path_list, data, handler, priority=True, queryargs=params)

def get_collection(self, collection_id, handler, limit=100, offset=0):
path_list = ["collection"]
Expand All @@ -183,7 +183,7 @@ def get_collection(self, collection_id, handler, limit=100, offset=0):
queryargs["inc"] = "+".join(inc)
queryargs["limit"] = limit
queryargs["offset"] = offset
return self.api_helper.get(path_list, handler, priority=True, important=True,
return self.get(path_list, handler, priority=True, important=True,
mblogin=True, queryargs=queryargs)

def get_collection_list(self, handler):
Expand All @@ -200,19 +200,19 @@ def _get_client_queryarg(self):

def put_to_collection(self, collection_id, releases, handler):
for path_list in self._collection_request(collection_id, releases):
self.api_helper.put(path_list, "", handler,
self.put(path_list, "", handler,
queryargs=self._get_client_queryarg())

def delete_from_collection(self, collection_id, releases, handler):
for path_list in self._collection_request(collection_id, releases):
self.api_helper.delete(path_list, handler,
self.delete(path_list, handler,
queryargs=self._get_client_queryarg())


class AcoustIdAPIHelper():
class AcoustIdAPIHelper(APIHelper):

def __init__(self, webservice):
self.api_helper = APIHelper(ACOUSTID_HOST, ACOUSTID_PORT,
super().__init__(ACOUSTID_HOST, ACOUSTID_PORT,
'/v2/', webservice)

def _encode_acoustid_args(self, args, format_='xml'):
Expand All @@ -228,7 +228,7 @@ def _encode_acoustid_args(self, args, format_='xml'):
def query_acoustid(self, handler, **args):
path_list = ['lookup']
body = self._encode_acoustid_args(args)
return self.api_helper.post(path_list, body, handler, priority=False, important=False, mblogin=False)
return self.post(path_list, body, handler, priority=False, important=False, mblogin=False)

def submit_acoustid_fingerprints(self, submissions, handler):
path_list = ['submit']
Expand All @@ -240,4 +240,4 @@ def submit_acoustid_fingerprints(self, submissions, handler):
if submission.puid:
args['puid.%d' % i] = string_(submission.puid)
body = self._encode_acoustid_args(args, format_='json')
return self.api_helper.post(path_list, body, handler, priority=True, important=False, mblogin=False)
return self.post(path_list, body, handler, priority=True, important=False, mblogin=False)

0 comments on commit ad96473

Please sign in to comment.