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

Move token from the query strings to the session #210

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 16 additions & 16 deletions slacker.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ def __init__(self, slack_name, token, init=True):
assert self.token, "Token should not be blank"
self.url = self.api_url()
self.session = requests.Session()
self.session.headers.update({"Authorization": "Bearer " + token})
if init:
self.get_users()
self.get_channels()

def get_emojis(self):
url = self.url + "emoji.list?token={}".format(self.token)
url = self.url + "emoji.list"
return self.get_with_retry_to_json(url)

def get_users(self):
Expand Down Expand Up @@ -82,7 +83,7 @@ def get_messages_in_time_range(self, oldest, cid, latest=None):
messages = []
done = False
while not done:
murl = self.url + "conversations.history?oldest={}&token={}&channel={}".format(oldest, self.token, cid)
murl = self.url + "conversations.history?oldest={}&channel={}".format(oldest, cid)
if latest:
murl += "&latest={}".format(latest)
else:
Expand Down Expand Up @@ -183,8 +184,8 @@ def get_channel_members_ids(self, channel_name):
if not member_count:
return members # should be an empty set

url_template = self.url + "conversations.members?token={}&channel={}"
url = url_template.format(self.token, cid)
url_template = self.url + "conversations.members?channel={}"
url = url_template.format(cid)

while True:
ret = self.get_with_retry_to_json(url)
Expand All @@ -198,8 +199,8 @@ def get_channel_members_ids(self, channel_name):

# once through the loop once, update the url to call to include the cursor
if ret['response_metadata']['next_cursor']:
url_template = self.url + "conversations.members?token={}&channel={}&cursor={}"
url = url_template.format(self.token, cid, ret['response_metadata']['next_cursor'])
url_template = self.url + "conversations.members?channel={}&cursor={}"
url = url_template.format(cid, ret['response_metadata']['next_cursor'])
# no more members to iterate over
else:
break
Expand Down Expand Up @@ -231,10 +232,10 @@ def get_channel_info(self, channel_name):
returns JSON with channel information. Adds 'age' in seconds to JSON
"""
# ensure include_num_members is available for get_channel_member_count()
url_template = self.url + "conversations.info?token={}&channel={}&include_num_members=true"
url_template = self.url + "conversations.info?channel={}&include_num_members=true"
cid = self.get_channelid(channel_name)
now = int(time.time())
url = url_template.format(self.token, cid)
url = url_template.format(cid)
ret = self.get_with_retry_to_json(url)
if ret['ok'] is not True:
m = "Attempted get_channel_info() for {}, but return was {}"
Expand All @@ -259,8 +260,8 @@ def get_all_channel_objects(self, exclude_archived=True):
else:
exclude_archived = 0

url_template = self.url + "conversations.list?exclude_archived={}&token={}"
url = url_template.format(exclude_archived, self.token)
url_template = self.url + "conversations.list?exclude_archived={}"
url = url_template.format(exclude_archived)

while True:
ret = self.get_with_retry_to_json(url)
Expand All @@ -274,8 +275,8 @@ def get_all_channel_objects(self, exclude_archived=True):
# after going through the loop once, update the url to call to
# include the pagination cursor
if ret['response_metadata']['next_cursor']:
url_template = self.url + "conversations.list?exclude_archived={}&token={}&cursor={}"
url = url_template.format(exclude_archived, self.token, ret['response_metadata']['next_cursor'])
url_template = self.url + "conversations.list?exclude_archived={}&cursor={}"
url = url_template.format(exclude_archived, ret['response_metadata']['next_cursor'])

# no more channels to iterate over
else:
Expand All @@ -284,7 +285,7 @@ def get_all_channel_objects(self, exclude_archived=True):
return channels

def get_all_user_objects(self):
url = self.url + "users.list?token=" + self.token
url = self.url + "users.list"
response = self.get_with_retry_to_json(url)
try:
return response['members']
Expand All @@ -293,9 +294,9 @@ def get_all_user_objects(self):
raise e

def archive(self, channel_name):
url_template = self.url + "conversations.archive?token={}&channel={}"
url_template = self.url + "conversations.archive?channel={}"
cid = self.get_channelid(channel_name)
url = url_template.format(self.token, cid)
url = url_template.format(cid)
request = self.session.post(url)
payload = request.json()
return payload
Expand All @@ -312,7 +313,6 @@ def post_message(self, channel, message, message_type=None):
channel = channel[1:]

post_data = {
'token': self.token,
'channel': channel,
'text': message.encode('utf-8')
}
Expand Down