api/common.py: Reduce code duplication between API call methods
(imported from commit 4a0f1a8479c21366bf552edf98401b03bad72d9c)
This commit is contained in:
parent
c1cc221cb6
commit
602ec5cfbf
43
common.py
43
common.py
|
@ -121,29 +121,15 @@ class HumbugAPI(object):
|
||||||
return {'msg': res.text, "result": "http-error",
|
return {'msg': res.text, "result": "http-error",
|
||||||
"status_code": res.status_code}
|
"status_code": res.status_code}
|
||||||
|
|
||||||
def send_message(self, request):
|
@classmethod
|
||||||
return self.do_api_query(request, "/api/v1/send_message")
|
def _register(cls, name, url=None, make_request=(lambda request={}: request), **query_kwargs):
|
||||||
|
if url is None:
|
||||||
def get_messages(self, request = {}):
|
url = name
|
||||||
return self.do_api_query(request, "/api/v1/get_messages",
|
def call(self, *args, **kwargs):
|
||||||
longpolling=True)
|
request = make_request(*args, **kwargs)
|
||||||
|
return self.do_api_query(request, '/api/v1/' + url, **query_kwargs)
|
||||||
def get_profile(self, request = {}):
|
call.func_name = name
|
||||||
return self.do_api_query(request, "/api/v1/get_profile")
|
setattr(cls, name, call)
|
||||||
|
|
||||||
def get_public_streams(self, request = {}):
|
|
||||||
return self.do_api_query(request, "/api/v1/get_public_streams")
|
|
||||||
|
|
||||||
def list_subscriptions(self, request = {}):
|
|
||||||
return self.do_api_query(request, "/api/v1/subscriptions/list")
|
|
||||||
|
|
||||||
def add_subscriptions(self, streams):
|
|
||||||
request = {'subscriptions': streams}
|
|
||||||
return self.do_api_query(request, "/api/v1/subscriptions/add")
|
|
||||||
|
|
||||||
def remove_subscriptions(self, streams):
|
|
||||||
request = {'subscriptions': streams}
|
|
||||||
return self.do_api_query(request, "/api/v1/subscriptions/remove")
|
|
||||||
|
|
||||||
def call_on_each_message(self, callback, options = {}):
|
def call_on_each_message(self, callback, options = {}):
|
||||||
max_message_id = None
|
max_message_id = None
|
||||||
|
@ -166,3 +152,14 @@ class HumbugAPI(object):
|
||||||
for message in sorted(res['messages'], key=lambda x: int(x["id"])):
|
for message in sorted(res['messages'], key=lambda x: int(x["id"])):
|
||||||
max_message_id = max(max_message_id, int(message["id"]))
|
max_message_id = max(max_message_id, int(message["id"]))
|
||||||
callback(message)
|
callback(message)
|
||||||
|
|
||||||
|
def _mk_subs(streams):
|
||||||
|
return {'subscriptions': streams}
|
||||||
|
|
||||||
|
HumbugAPI._register('send_message', make_request=(lambda request: request))
|
||||||
|
HumbugAPI._register('get_messages', longpolling=True)
|
||||||
|
HumbugAPI._register('get_profile')
|
||||||
|
HumbugAPI._register('get_public_streams')
|
||||||
|
HumbugAPI._register('list_subscriptions', url='subscriptions/list')
|
||||||
|
HumbugAPI._register('add_subscriptions', url='subscriptions/add', make_request=_mk_subs)
|
||||||
|
HumbugAPI._register('remove_subscriptions', url='subscriptions/remove', make_request=_mk_subs)
|
||||||
|
|
Loading…
Reference in a new issue