@@ -49,6 +49,20 @@ def get_config_dir(config_dir_name="gspread", os_is_windows=os.name == "nt"):
49
49
DEFAULT_SERVICE_ACCOUNT_FILENAME = DEFAULT_CONFIG_DIR / "service_account.json"
50
50
51
51
52
+ def authorize (credentials , client_factory = Client ):
53
+ """Login to Google API using OAuth2 credentials.
54
+ This is a shortcut/helper function which
55
+ instantiates a client using `client_factory`.
56
+ By default :class:`gspread.Client` is used (but could also use
57
+ :class:`gspread.BackoffClient` to avoid rate limiting).
58
+
59
+ :returns: An instance of the class produced by `client_factory`.
60
+ :rtype: :class:`gspread.client.Client`
61
+ """
62
+
63
+ return client_factory (auth = credentials )
64
+
65
+
52
66
def local_server_flow (client_config , scopes , port = 0 ):
53
67
"""Run an OAuth flow using a local server strategy.
54
68
@@ -105,6 +119,7 @@ def oauth(
105
119
flow = local_server_flow ,
106
120
credentials_filename = DEFAULT_CREDENTIALS_FILENAME ,
107
121
authorized_user_filename = DEFAULT_AUTHORIZED_USER_FILENAME ,
122
+ client_factory = Client ,
108
123
):
109
124
r"""Authenticate with OAuth Client ID.
110
125
@@ -162,8 +177,12 @@ def oauth(
162
177
163
178
* `%APPDATA%\gspread\authorized_user.json` on Windows
164
179
* `~/.config/gspread/authorized_user.json` everywhere else
180
+ :type client_factory: :class:`gspread.ClientFactory`
181
+ :param client_factory: A factory function that returns a client class.
182
+ Defaults to :class:`gspread.Client` (but could also use
183
+ :class:`gspread.BackoffClient` to avoid rate limiting)
165
184
166
- :rtype: :class:`gspread.Client`
185
+ :rtype: :class:`gspread.client. Client`
167
186
"""
168
187
authorized_user_filename = Path (authorized_user_filename )
169
188
creds = load_credentials (filename = authorized_user_filename )
@@ -174,14 +193,15 @@ def oauth(
174
193
creds = flow (client_config = client_config , scopes = scopes )
175
194
store_credentials (creds , filename = authorized_user_filename )
176
195
177
- return Client (auth = creds )
196
+ return client_factory (auth = creds )
178
197
179
198
180
199
def oauth_from_dict (
181
200
credentials = None ,
182
201
authorized_user_info = None ,
183
202
scopes = DEFAULT_SCOPES ,
184
203
flow = local_server_flow ,
204
+ client_factory = Client ,
185
205
):
186
206
r"""Authenticate with OAuth Client ID.
187
207
@@ -237,8 +257,12 @@ def oauth_from_dict(
237
257
:param list scopes: The scopes used to obtain authorization.
238
258
:param function flow: OAuth flow to use for authentication.
239
259
Defaults to :meth:`~gspread.auth.local_server_flow`
260
+ :type client_factory: :class:`gspread.ClientFactory`
261
+ :param client_factory: A factory function that returns a client class.
262
+ Defaults to :class:`gspread.Client` (but could also use
263
+ :class:`gspread.BackoffClient` to avoid rate limiting)
240
264
241
- :rtype: tuple(:class: `gspread.Client` , str)
265
+ :rtype: ( `gspread.client. Client`, str)
242
266
"""
243
267
244
268
creds = None
@@ -248,15 +272,19 @@ def oauth_from_dict(
248
272
if not creds :
249
273
creds = flow (client_config = credentials , scopes = scopes )
250
274
251
- client = Client (auth = creds )
275
+ client = client_factory (auth = creds )
252
276
253
277
# must return the creds to the user
254
278
# must strip the token an use the dedicated method from Credentials
255
279
# to return a dict "safe to store".
256
280
return (client , creds .to_json ("token" ))
257
281
258
282
259
- def service_account (filename = DEFAULT_SERVICE_ACCOUNT_FILENAME , scopes = DEFAULT_SCOPES ):
283
+ def service_account (
284
+ filename = DEFAULT_SERVICE_ACCOUNT_FILENAME ,
285
+ scopes = DEFAULT_SCOPES ,
286
+ client_factory = Client ,
287
+ ):
260
288
"""Authenticate using a service account.
261
289
262
290
``scopes`` parameter defaults to read/write scope available in
@@ -274,14 +302,18 @@ def service_account(filename=DEFAULT_SERVICE_ACCOUNT_FILENAME, scopes=DEFAULT_SC
274
302
275
303
:param str filename: The path to the service account json file.
276
304
:param list scopes: The scopes used to obtain authorization.
305
+ :type client_factory: :class:`gspread.ClientFactory`
306
+ :param client_factory: A factory function that returns a client class.
307
+ Defaults to :class:`gspread.Client` (but could also use
308
+ :class:`gspread.BackoffClient` to avoid rate limiting)
277
309
278
- :rtype: :class:`gspread.Client`
310
+ :rtype: :class:`gspread.client. Client`
279
311
"""
280
312
creds = ServiceAccountCredentials .from_service_account_file (filename , scopes = scopes )
281
- return Client (auth = creds )
313
+ return client_factory (auth = creds )
282
314
283
315
284
- def service_account_from_dict (info , scopes = DEFAULT_SCOPES ):
316
+ def service_account_from_dict (info , scopes = DEFAULT_SCOPES , client_factory = Client ):
285
317
"""Authenticate using a service account (json).
286
318
287
319
``scopes`` parameter defaults to read/write scope available in
@@ -299,11 +331,15 @@ def service_account_from_dict(info, scopes=DEFAULT_SCOPES):
299
331
300
332
:param info (Mapping[str, str]): The service account info in Google format
301
333
:param list scopes: The scopes used to obtain authorization.
334
+ :type client_factory: :class:`gspread.ClientFactory`
335
+ :param client_factory: A factory function that returns a client class.
336
+ Defaults to :class:`gspread.Client` (but could also use
337
+ :class:`gspread.BackoffClient` to avoid rate limiting)
302
338
303
- :rtype: :class:`gspread.Client`
339
+ :rtype: :class:`gspread.client. Client`
304
340
"""
305
341
creds = ServiceAccountCredentials .from_service_account_info (
306
342
info = info ,
307
343
scopes = scopes ,
308
344
)
309
- return Client (auth = creds )
345
+ return client_factory (auth = creds )
0 commit comments