Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add api base url #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions bearychat/rtm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ class RTMClient(object):
channel(RTMChannel): service of current channel
"""

def __init__(self, token, api_base="https://rtm.bearychat.com"):
def __init__(self, token,
rtm_base="https://rtm.bearychat.com",
api_base="https://api.bearychat.com/v1"):
"""
Args:
token(str): rtm token
rtm_base(str): rtm url base
api_base(str): api url base
"""
self._token = token
self._rtm_base = rtm_base
self._api_base = api_base
self.current_team = RTMCurrentTeam(self)
self.user = RTMUser(self)
Expand All @@ -56,7 +60,7 @@ def start(self):
None if request failed,
else a dict containing "user"(User) and "ws_host"
"""
resp = self.post('start')
resp = self.post('start',base_url=self._rtm_base)

if resp.is_fail():
return None
Expand All @@ -72,6 +76,7 @@ def start(self):
def do(self,
resource,
method,
base_url=None,
params=None,
data=None,
json=None,
Expand All @@ -81,6 +86,7 @@ def do(self,
Args:
resource(str): resource uri(relative path)
method(str): HTTP method
base_url(str): HTTP base url
params(dict): uri queries
data(dict): HTTP body(form)
json(dict): HTTP body(json)
Expand All @@ -89,11 +95,15 @@ def do(self,
Returns:
RTMResponse
"""
uri = "{0}/{1}".format(self._api_base, resource)

if not base_url:
base_url = self._api_base

uri = "{0}/{1}".format(base_url, resource)
if not params:
params = {}
params.update({'token': self._token})

req = Request(
method=method,
url=uri,
Expand All @@ -107,18 +117,18 @@ def do(self,

return RTMResponse(resp)

def get(self, resource, params=None, headers=None):
def get(self, resource, base_url=None, params=None, headers=None):
"""Sends a GET request

Returns:
RTMResponse
"""
return self.do(resource, 'GET', params=params, headers=headers)
return self.do(resource, 'GET', base_url=base_url, params=params, headers=headers)

def post(self, resource, data=None, json=None):
def post(self, resource, base_url=None, data=None, json=None):
"""Sends a POST request

Returns:
RTMResponse
"""
return self.do(resource, 'POST', data=data, json=json)
return self.do(resource, 'POST', base_url=base_url, data=data, json=json)
3 changes: 2 additions & 1 deletion examples/rtm.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def main():
try:
print("rtm loop received {0} from {1}".format(message["text"],
message["uid"]))
except Exception:
except Exception as e:
print(e)
continue

if message.is_from(user):
Expand Down