flock: Change returned type of get_recipient_id to tuple (userId, error).

This commit is contained in:
novokrest 2018-06-09 01:57:48 +03:00 committed by showell
parent c2a3d4c517
commit efdc96929f
2 changed files with 16 additions and 9 deletions

View file

@ -1,6 +1,6 @@
import logging import logging
import requests import requests
from typing import Any, Dict from typing import Any, Dict, Tuple, Optional
from requests.exceptions import ConnectionError from requests.exceptions import ConnectionError
USERS_LIST_URL = 'https://api.flock.co/v1/roster.listContacts' USERS_LIST_URL = 'https://api.flock.co/v1/roster.listContacts'
@ -18,8 +18,10 @@ def find_recipient_id(res: str, recipient_name: str) -> str:
if recipient_name == obj['firstName']: if recipient_name == obj['firstName']:
return obj['id'] return obj['id']
# Returns User's ID, if not found, returns error message. # Returns two-element tuple whose left-hand value contains recipient
def get_recipient_id(recipient_name: str, config: Dict[str, str]) -> str: # user's ID (or None if it was not found) and right-hand value contains
# an error message (or None if recipient user's ID was found)
def get_recipient_id(recipient_name: str, config: Dict[str, str]) -> Tuple[Optional[str], Optional[str]]:
token = config['token'] token = config['token']
payload = { payload = {
'token': token 'token': token
@ -29,15 +31,17 @@ def get_recipient_id(recipient_name: str, config: Dict[str, str]) -> str:
res = requests.get(USERS_LIST_URL, params=payload) res = requests.get(USERS_LIST_URL, params=payload)
except ConnectionError as e: except ConnectionError as e:
logging.exception(str(e)) logging.exception(str(e))
return "Uh-Oh, couldn't process the request \ error = "Uh-Oh, couldn't process the request \
right now.\nPlease try again later" right now.\nPlease try again later"
return (None, error)
res = res.json() res = res.json()
recipient_id = find_recipient_id(res, recipient_name) recipient_id = find_recipient_id(res, recipient_name)
if recipient_id is None: if recipient_id is None:
return "No user found. Make sure you typed it correctly." error = "No user found. Make sure you typed it correctly."
return (None, error)
else: else:
return recipient_id return (recipient_id, None)
# This handles the message sending work. # This handles the message sending work.
def get_flock_response(content: str, config: Dict[str, str]) -> str: def get_flock_response(content: str, config: Dict[str, str]) -> str:
@ -46,7 +50,10 @@ def get_flock_response(content: str, config: Dict[str, str]) -> str:
recipient_name = content_pieces[0].strip() recipient_name = content_pieces[0].strip()
message = content_pieces[1].strip() message = content_pieces[1].strip()
recipient_id = get_recipient_id(recipient_name, config) recipient_id, error = get_recipient_id(recipient_name, config)
if recipient_id is None:
return error
if len(str(recipient_id)) > 30: if len(str(recipient_id)) > 30:
return recipient_id return recipient_id

View file

@ -48,7 +48,7 @@ right now.\nPlease try again later")
@patch('zulip_bots.bots.flock.flock.get_recipient_id') @patch('zulip_bots.bots.flock.flock.get_recipient_id')
def test_message_send_success(self, get_recipient_id: str) -> None: def test_message_send_success(self, get_recipient_id: str) -> None:
bot_response = "Message sent." bot_response = "Message sent."
get_recipient_id.return_value = "u:userid" get_recipient_id.return_value = ["u:userid", None]
with self.mock_config_info(self.normal_config), \ with self.mock_config_info(self.normal_config), \
self.mock_http_conversation('test_message_send_success'): self.mock_http_conversation('test_message_send_success'):
self.verify_reply('Rishabh: hi there', bot_response) self.verify_reply('Rishabh: hi there', bot_response)
@ -56,7 +56,7 @@ right now.\nPlease try again later")
@patch('zulip_bots.bots.flock.flock.get_recipient_id') @patch('zulip_bots.bots.flock.flock.get_recipient_id')
def test_message_send_failed(self, get_recipient_id: str) -> None: def test_message_send_failed(self, get_recipient_id: str) -> None:
bot_response = "Message sending failed :slightly_frowning_face:. Please try again." bot_response = "Message sending failed :slightly_frowning_face:. Please try again."
get_recipient_id.return_value = "u:invalid" get_recipient_id.return_value = ["u:invalid", None]
with self.mock_config_info(self.normal_config), \ with self.mock_config_info(self.normal_config), \
self.mock_http_conversation('test_message_send_failed'): self.mock_http_conversation('test_message_send_failed'):
self.verify_reply('Rishabh: hi there', bot_response) self.verify_reply('Rishabh: hi there', bot_response)