Skip to content

Commit 6fc2cc3

Browse files
authored
Merge pull request #76 and #80 from CiscoDevNet/pr/76
Pr/76
2 parents 4dc7e5f + 0be8463 commit 6fc2cc3

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

webexteamssdk/api/__init__.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from webexteamssdk.utils import check_type
3636
from .access_tokens import AccessTokensAPI
3737
from .events import EventsAPI
38+
from .guest_issuer import GuestIssuerAPI
3839
from .licenses import LicensesAPI
3940
from .memberships import MembershipsAPI
4041
from .messages import MessagesAPI
@@ -45,7 +46,6 @@
4546
from .team_memberships import TeamMembershipsAPI
4647
from .teams import TeamsAPI
4748
from .webhooks import WebhooksAPI
48-
from .guest_issuer import GuestIssuerAPI
4949

5050

5151
class WebexTeamsAPI(object):
@@ -62,18 +62,26 @@ class WebexTeamsAPI(object):
6262
def __init__(self, access_token=None, base_url=DEFAULT_BASE_URL,
6363
single_request_timeout=DEFAULT_SINGLE_REQUEST_TIMEOUT,
6464
wait_on_rate_limit=DEFAULT_WAIT_ON_RATE_LIMIT,
65-
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):
6670
"""Create a new WebexTeamsAPI object.
6771
6872
An access token must be used when interacting with the Webex Teams API.
69-
This package supports two methods for you to provide that access token:
73+
This package supports three methods for you to provide that access
74+
token:
7075
7176
1. You may manually specify the access token via the `access_token`
7277
argument, when creating a new WebexTeamsAPI object.
7378
7479
2. If an access_token argument is not supplied, the package checks
7580
for a WEBEX_TEAMS_ACCESS_TOKEN environment variable.
7681
82+
3. Provide the parameters (client_id, client_secret, oauth_code and
83+
oauth_redirect_uri) from your oauth flow.
84+
7785
An AccessTokenError is raised if an access token is not provided
7886
via one of these two methods.
7987
@@ -92,6 +100,14 @@ def __init__(self, access_token=None, base_url=DEFAULT_BASE_URL,
92100
webexteamssdk.config.DEFAULT_WAIT_ON_RATE_LIMIT.
93101
object_factory(callable): The factory function to use to create
94102
Python objects from the returned Webex Teams JSON data objects.
103+
client_id(basestring): The client id of your integration. Provided
104+
upon creation in the portal.
105+
client_secret(basestring): The client secret of your integration.
106+
Provided upon creation in the portal.
107+
oauth_code(basestring): The oauth authorization code provided by
108+
the user oauth process.
109+
oauth_redirect_uri(basestring): The redirect URI used in the user
110+
OAuth process.
95111
96112
Returns:
97113
WebexTeamsAPI: A new WebexTeamsAPI object.
@@ -106,8 +122,31 @@ def __init__(self, access_token=None, base_url=DEFAULT_BASE_URL,
106122
check_type(base_url, basestring)
107123
check_type(single_request_timeout, int)
108124
check_type(wait_on_rate_limit, bool)
125+
check_type(client_id, basestring, may_be_none=True)
126+
check_type(client_secret, basestring, may_be_none=True)
127+
check_type(oauth_code, basestring, may_be_none=True)
128+
check_type(redirect_uri, basestring, may_be_none=True)
109129

110130
access_token = access_token or WEBEX_TEAMS_ACCESS_TOKEN
131+
132+
# Init AccessTokensAPI wrapper early to use for oauth requests
133+
self.access_tokens = AccessTokensAPI(
134+
base_url, object_factory,
135+
single_request_timeout=single_request_timeout,
136+
)
137+
138+
# Check if the user has provided the required oauth parameters
139+
oauth_param_list = [client_id, client_secret, oauth_code, redirect_uri]
140+
if not access_token and all(oauth_param_list):
141+
access_token = self.access_tokens.get(
142+
client_id=client_id,
143+
client_secret=client_secret,
144+
code=oauth_code,
145+
redirect_uri=redirect_uri
146+
).access_token
147+
148+
# If an access token hasn't been provided as a parameter, environment
149+
# variable, or obtained via an OAuth exchange raise an error.
111150
if not access_token:
112151
raise AccessTokenError(
113152
"You must provide a Webex Teams access token to interact with "
@@ -139,10 +178,6 @@ def __init__(self, access_token=None, base_url=DEFAULT_BASE_URL,
139178
self.organizations = OrganizationsAPI(self._session, object_factory)
140179
self.licenses = LicensesAPI(self._session, object_factory)
141180
self.roles = RolesAPI(self._session, object_factory)
142-
self.access_tokens = AccessTokensAPI(
143-
self.base_url, object_factory,
144-
single_request_timeout=single_request_timeout
145-
)
146181
self.events = EventsAPI(self._session, object_factory)
147182
self.guest_issuer = GuestIssuerAPI(self._session, object_factory)
148183

0 commit comments

Comments
 (0)