From 20bac4eedfa725105aded44746e19d09d6f13950 Mon Sep 17 00:00:00 2001 From: Iurii Tatishchev Date: Thu, 11 Feb 2021 00:35:35 -0800 Subject: [PATCH 1/3] #9 | initial api auth implementation --- main/apiauth.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 main/apiauth.py diff --git a/main/apiauth.py b/main/apiauth.py new file mode 100644 index 0000000..96da5a0 --- /dev/null +++ b/main/apiauth.py @@ -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 From ac446c7746e312203f6b6cc37aedff91df85af8d Mon Sep 17 00:00:00 2001 From: Iurii Tatishchev Date: Thu, 11 Feb 2021 08:24:47 -0800 Subject: [PATCH 2/3] add type hint to ZenpyUser --- main/apiauth.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main/apiauth.py b/main/apiauth.py index 96da5a0..db9bf14 100644 --- a/main/apiauth.py +++ b/main/apiauth.py @@ -1,6 +1,7 @@ import os from zenpy import Zenpy +from zenpy.lib.api_objects import User as ZenpyUser def api_auth(): @@ -24,7 +25,7 @@ def api_auth(): credentials['password'] = password zenpy_client = Zenpy(**credentials) - zenpy_user = zenpy_client.users.search(email).values[0] + zenpy_user: ZenpyUser = zenpy_client.users.search(email).values[0] user = { 'id': zenpy_user.id, From 24551c7d416ee8bde5a8424661f6dfc967a90cb8 Mon Sep 17 00:00:00 2001 From: Andrew Smirnov Date: Thu, 11 Feb 2021 20:30:35 +0300 Subject: [PATCH 3/3] Small code style improvements --- main/apiauth.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main/apiauth.py b/main/apiauth.py index db9bf14..4e3f761 100644 --- a/main/apiauth.py +++ b/main/apiauth.py @@ -17,12 +17,12 @@ def api_auth(): credentials['email'] = os.getenv('ACCESS_CONTROLLER_API_EMAIL') # prefer token, use password if token not provided - if token is not None: + if token: credentials['token'] = token - else: - if password is None: - raise ValueError('access_controller token or password not in env') + elif password: credentials['password'] = password + else: + raise ValueError('access_controller token or password not in env') zenpy_client = Zenpy(**credentials) zenpy_user: ZenpyUser = zenpy_client.users.search(email).values[0]