async def get_permalink(self, channel_id: str, message_ts: str) -> Result[str, str]: """ Get permalink for given message in given channel :param channel_id: channel ID where message is located :param message_ts: timestamp (unixtime) of the message :return: permalink or None if not found or any error """ try: answer = await self.retry_policy.execute( lambda: self.bot_web_client.chat_getPermalink( channel=channel_id, message_ts=message_ts)) link = answer.get("permalink", "") return Result.Ok(link) except (asyncio.TimeoutError, RetryAfterError): self.logger.debug( "Too much permalinks requested, will try next time.") except errors.SlackApiError as e: if e.response.get("error", "") == "message_not_found": return Result.Err("Sorry, the message was deleted :(") if e.response.get("error", "") == "channel_not_found": return Result.Err("Sorry, the channel was deleted :(") self.logger.exception(e) except Exception as e: self.logger.exception(e) return Result.Err("")
def check_qna_answer(answer: Any) -> Result[List[QnAAnswer], str]: try: if not (isinstance(answer, list)): raise ValueError("Base type is not list.") return Result.Ok([QnAAnswer.parse_obj(x) for x in answer]) except (ValidationError, ValueError) as e: container.logger.exception(e) return Result.Err(str(e))
async def qna_interaction_fastpath(data: dict) -> Result[dict, str]: channel_id = data.get("channel", {}).get("id", "") user_id = data.get('user', {}).get('id', '') # receive all messages for last 5 minutes and find last from the user message = await container.slacker.get_im_latest_user_message_text( channel_id=channel_id, oldest=datetime.now() - timedelta(minutes=5), user_id=user_id) if not message: return Result.Err( "Didn't find your message. Is it more than 5 minutes old?") return Result.Ok({"query": message})
from result import Result, Ok, Err, UnwrapError @pytest.mark.parametrize('instance', [ Ok(1), Result.Ok(1), ]) def test_ok_factories(instance): assert instance._value == 1 assert instance.is_ok() is True @pytest.mark.parametrize('instance', [ Err(2), Result.Err(2), ]) def test_err_factories(instance): assert instance._value == 2 assert instance.is_err() is True def test_eq(): assert Ok(1) == Ok(1) assert Err(1) == Err(1) assert Ok(1) != Err(1) assert Ok(1) != Ok(2) assert not (Ok(1) != Ok(1)) assert Ok(1) != "abc" assert Ok("0") != Ok(0)