Skip to content

Commit 8a5f350

Browse files
committed
added ability to init API with credentials from OAuth flow
1 parent 719b586 commit 8a5f350

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

webexteamssdk/api/__init__.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from webexteamssdk.exceptions import AccessTokenError
3333
from webexteamssdk.models.immutable import immutable_data_factory
3434
from webexteamssdk.restsession import RestSession
35-
from webexteamssdk.utils import check_type
35+
from webexteamssdk.utils import check_type, check_all_not_none
3636
from .access_tokens import AccessTokensAPI
3737
from .events import EventsAPI
3838
from .licenses import LicensesAPI
@@ -47,6 +47,7 @@
4747
from .webhooks import WebhooksAPI
4848

4949

50+
5051
class WebexTeamsAPI(object):
5152
"""Webex Teams API wrapper.
5253
@@ -61,18 +62,25 @@ class WebexTeamsAPI(object):
6162
def __init__(self, access_token=None, base_url=DEFAULT_BASE_URL,
6263
single_request_timeout=DEFAULT_SINGLE_REQUEST_TIMEOUT,
6364
wait_on_rate_limit=DEFAULT_WAIT_ON_RATE_LIMIT,
64-
object_factory=immutable_data_factory):
65+
object_factory=immutable_data_factory,
66+
client_id=None,
67+
client_secret=None,
68+
oauth_code=None,
69+
redirect_uri=None):
6570
"""Create a new WebexTeamsAPI object.
6671
6772
An access token must be used when interacting with the Webex Teams API.
68-
This package supports two methods for you to provide that access token:
73+
This package supports thre methods for you to provide that access token:
6974
7075
1. You may manually specify the access token via the `access_token`
7176
argument, when creating a new WebexTeamsAPI object.
7277
7378
2. If an access_token argument is not supplied, the package checks
7479
for a WEBEX_TEAMS_ACCESS_TOKEN environment variable.
7580
81+
3. Provide the parameters (client_id, client_secret, oauth_code and
82+
oauth_redirect_uri) from your oauth flow.
83+
7684
An AccessTokenError is raised if an access token is not provided
7785
via one of these two methods.
7886
@@ -91,6 +99,14 @@ def __init__(self, access_token=None, base_url=DEFAULT_BASE_URL,
9199
webexteamssdk.config.DEFAULT_WAIT_ON_RATE_LIMIT.
92100
object_factory(callable): The factory function to use to create
93101
Python objects from the returned Webex Teams JSON data objects.
102+
client_id(basestring): The client id of your integration. Provided
103+
upon creation in the portal.
104+
client_secret(basestring): The client secret of your integration.
105+
Provided upon creation in the portal.
106+
oauth_code(basestring): The oauth authorization code provided by the
107+
user oauth process.
108+
oauth_redirect_uri(basestring): The redirect URI used in the user
109+
OAuth process.
94110
95111
Returns:
96112
WebexTeamsAPI: A new WebexTeamsAPI object.
@@ -105,8 +121,26 @@ def __init__(self, access_token=None, base_url=DEFAULT_BASE_URL,
105121
check_type(base_url, basestring)
106122
check_type(single_request_timeout, int)
107123
check_type(wait_on_rate_limit, bool)
124+
check_type(client_id, basestring, may_be_none=True)
125+
check_type(client_secret, basestring, may_be_none=True)
126+
check_type(oauth_code, basestring, may_be_none=True)
127+
check_type(redirect_uri, basestring, may_be_none=True)
108128

109129
access_token = access_token or WEBEX_TEAMS_ACCESS_TOKEN
130+
131+
# Check if the user has provided the required oauth parameters
132+
oauth_param_list = [client_id, client_secret, oauth_code, redirect_uri]
133+
# Init AccessTokensAPI earlier to use for oauth requests
134+
self.access_tokens = AccessTokensAPI(
135+
self.base_url, object_factory,
136+
single_request_timeout=single_request_timeout
137+
)
138+
if not access_token and check_all_not_none(oauth_param_list):
139+
access_token = self.access_tokens.get(client_id=client_id,
140+
client_secret=client_secret,
141+
code=oauth_code,
142+
redirect_uri=redirect_uri
143+
).access_token
110144
if not access_token:
111145
raise AccessTokenError(
112146
"You must provide a Webex Teams access token to interact with "
@@ -138,10 +172,6 @@ def __init__(self, access_token=None, base_url=DEFAULT_BASE_URL,
138172
self.organizations = OrganizationsAPI(self._session, object_factory)
139173
self.licenses = LicensesAPI(self._session, object_factory)
140174
self.roles = RolesAPI(self._session, object_factory)
141-
self.access_tokens = AccessTokensAPI(
142-
self.base_url, object_factory,
143-
single_request_timeout=single_request_timeout
144-
)
145175
self.events = EventsAPI(self._session, object_factory)
146176

147177
@property

webexteamssdk/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,21 @@ def open_local_file(file_path):
127127
file_object=file_object,
128128
content_type=content_type)
129129

130+
def check_all_not_none(l):
131+
"""Checks if all the elements in the list are not none.
130132
133+
Args:
134+
l(list): A list of objects that should be checked
135+
136+
Returns:
137+
boolean: True if all list items are not none, false otherwise
138+
139+
"""
140+
for o in l:
141+
if o is None:
142+
return false
143+
144+
return true
131145
def check_type(o, acceptable_types, may_be_none=True):
132146
"""Object is an instance of one of the acceptable types or None.
133147

0 commit comments

Comments
 (0)