forked from M-Mueller/mattermost-poll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mattermost_api.py
45 lines (34 loc) · 1.21 KB
/
mattermost_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# -*- coding: utf-8 -*-
import json
import logging
import requests
import settings
logger = logging.getLogger('flask.app')
def user_locale(user_id):
"""Returns the locale of the user with the given user_id."""
if not settings.MATTERMOST_PA_TOKEN:
return "en"
try:
header = {'Authorization': 'Bearer ' + settings.MATTERMOST_PA_TOKEN}
url = settings.MATTERMOST_URL + '/api/v4/users/' + user_id
r = requests.get(url, headers=header)
if r.ok:
locale = json.loads(r.text)['locale']
if locale:
return locale
except KeyError as e:
logger.error(e)
return "en"
def resolve_usernames(user_ids):
"""Resolve the list of user ids to list of user names."""
if len(user_ids) == 0:
return []
try:
header = {'Authorization': 'Bearer ' + settings.MATTERMOST_PA_TOKEN}
url = settings.MATTERMOST_URL + '/api/v4/users/ids'
r = requests.post(url, headers=header, json=user_ids)
if r.ok:
return [user["username"] for user in json.loads(r.text)]
except Exception as e:
logger.error('Username query failed: %s', str(e))
return ['<Failed to resolve usernames>']