Files
superset2/superset/utils/slack.py
2024-04-16 08:40:27 -07:00

54 lines
1.8 KiB
Python

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from flask import current_app
from slack_sdk import WebClient
class SlackClientError(Exception):
pass
def get_slack_client() -> WebClient:
token: str = current_app.config["SLACK_API_TOKEN"]
if callable(token):
token = token()
return WebClient(token=token, proxy=current_app.config["SLACK_PROXY"])
def get_user_avatar(email: str, client: WebClient = None) -> str:
client = client or get_slack_client()
try:
response = client.users_lookupByEmail(email=email)
except Exception as ex:
raise SlackClientError(f"Failed to lookup user by email: {email}") from ex
user = response.data.get("user")
if user is None:
raise SlackClientError("No user found with that email.")
profile = user.get("profile")
if profile is None:
raise SlackClientError("User found but no profile available.")
avatar_url = profile.get("image_192")
if avatar_url is None:
raise SlackClientError("Profile image is not available.")
return avatar_url