#9 | initial api auth implementation

This commit is contained in:
Iurii Tatishchev 2021-02-11 00:35:35 -08:00
parent 7a5c7faf91
commit 20bac4eedf
Signed by: CaZzzer
GPG Key ID: 926BE949E29DCD03

37
main/apiauth.py Normal file
View File

@ -0,0 +1,37 @@
import os
from zenpy import Zenpy
def api_auth():
credentials = {
'subdomain': 'ngenix1612197338'
}
email = os.getenv('ACCESS_CONTROLLER_API_EMAIL')
token = os.getenv('ACCESS_CONTROLLER_API_TOKEN')
password = os.getenv('ACCESS_CONTROLLER_API_PASSWORD')
if email is None:
raise ValueError('access_controller email not in env')
credentials['email'] = os.getenv('ACCESS_CONTROLLER_API_EMAIL')
# prefer token, use password if token not provided
if token is not None:
credentials['token'] = token
else:
if password is None:
raise ValueError('access_controller token or password not in env')
credentials['password'] = password
zenpy_client = Zenpy(**credentials)
zenpy_user = zenpy_client.users.search(email).values[0]
user = {
'id': zenpy_user.id,
'name': zenpy_user.name, # Zendesk doesn't have separate first and last name fields
'email': zenpy_user.email,
'role': zenpy_user.role, # str like 'admin' or 'agent', not id
'photo': zenpy_user.photo['content_url'] if zenpy_user.photo is not None else None,
}
return user