32
32
from webexteamssdk .exceptions import AccessTokenError
33
33
from webexteamssdk .models .immutable import immutable_data_factory
34
34
from webexteamssdk .restsession import RestSession
35
- from webexteamssdk .utils import check_type
35
+ from webexteamssdk .utils import check_type , check_all_not_none
36
36
from .access_tokens import AccessTokensAPI
37
37
from .events import EventsAPI
38
38
from .licenses import LicensesAPI
47
47
from .webhooks import WebhooksAPI
48
48
49
49
50
+
50
51
class WebexTeamsAPI (object ):
51
52
"""Webex Teams API wrapper.
52
53
@@ -61,18 +62,25 @@ class WebexTeamsAPI(object):
61
62
def __init__ (self , access_token = None , base_url = DEFAULT_BASE_URL ,
62
63
single_request_timeout = DEFAULT_SINGLE_REQUEST_TIMEOUT ,
63
64
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 ):
65
70
"""Create a new WebexTeamsAPI object.
66
71
67
72
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:
69
74
70
75
1. You may manually specify the access token via the `access_token`
71
76
argument, when creating a new WebexTeamsAPI object.
72
77
73
78
2. If an access_token argument is not supplied, the package checks
74
79
for a WEBEX_TEAMS_ACCESS_TOKEN environment variable.
75
80
81
+ 3. Provide the parameters (client_id, client_secret, oauth_code and
82
+ oauth_redirect_uri) from your oauth flow.
83
+
76
84
An AccessTokenError is raised if an access token is not provided
77
85
via one of these two methods.
78
86
@@ -91,6 +99,14 @@ def __init__(self, access_token=None, base_url=DEFAULT_BASE_URL,
91
99
webexteamssdk.config.DEFAULT_WAIT_ON_RATE_LIMIT.
92
100
object_factory(callable): The factory function to use to create
93
101
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.
94
110
95
111
Returns:
96
112
WebexTeamsAPI: A new WebexTeamsAPI object.
@@ -105,8 +121,26 @@ def __init__(self, access_token=None, base_url=DEFAULT_BASE_URL,
105
121
check_type (base_url , basestring )
106
122
check_type (single_request_timeout , int )
107
123
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 )
108
128
109
129
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
110
144
if not access_token :
111
145
raise AccessTokenError (
112
146
"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,
138
172
self .organizations = OrganizationsAPI (self ._session , object_factory )
139
173
self .licenses = LicensesAPI (self ._session , object_factory )
140
174
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
- )
145
175
self .events = EventsAPI (self ._session , object_factory )
146
176
147
177
@property
0 commit comments