From 963097e4b79a65b22dbf58196ebb639bc8d909bd Mon Sep 17 00:00:00 2001 From: Alexandre Lavigne Date: Sun, 28 Jan 2024 23:38:56 +0100 Subject: [PATCH] Allow client to use external Session object Allow new `gspread.client.Client` to be initialized using an external `requests.Session` object. closes #1383 Signed-off-by: Alexandre Lavigne --- gspread/auth.py | 15 +++++++++++++-- gspread/client.py | 9 ++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/gspread/auth.py b/gspread/auth.py index 1964cfad2..49a70e571 100644 --- a/gspread/auth.py +++ b/gspread/auth.py @@ -15,6 +15,7 @@ from google.oauth2.credentials import Credentials as OAuthCredentials from google.oauth2.service_account import Credentials as SACredentials from google_auth_oauthlib.flow import InstalledAppFlow +from requests import Session from .client import Client from .http_client import HTTPClient, HTTPClientType @@ -54,7 +55,9 @@ def get_config_dir( def authorize( - credentials: Credentials, http_client: HTTPClientType = HTTPClient + credentials: Credentials, + http_client: HTTPClientType = HTTPClient, + session: Optional[Session] = None, ) -> Client: """Login to Google API using OAuth2 credentials. This is a shortcut/helper function which @@ -62,11 +65,19 @@ def authorize( By default :class:`gspread.HTTPClient` is used (but could also use :class:`gspread.BackOffHTTPClient` to avoid rate limiting). + It can take an additional `requests.Session` object in order to provide + you own session object. + + .. note:: + + When providing your own `requests.Session` object, + use the value `None` as `credentials`. + :returns: An instance of the class produced by `http_client`. :rtype: :class:`gspread.client.Client` """ - return Client(auth=credentials, http_client=http_client) + return Client(auth=credentials, http_client=http_client, session=session) def local_server_flow( diff --git a/gspread/client.py b/gspread/client.py index 0e9d50a4f..34666b0de 100644 --- a/gspread/client.py +++ b/gspread/client.py @@ -10,7 +10,7 @@ from typing import Any, Dict, List, Optional, Tuple, Union from google.auth.credentials import Credentials -from requests import Response +from requests import Response, Session from .exceptions import APIError, SpreadsheetNotFound, UnSupportedExportFormat from .http_client import HTTPClient, HTTPClientType, ParamsType @@ -36,9 +36,12 @@ class Client: """ def __init__( - self, auth: Credentials, http_client: HTTPClientType = HTTPClient + self, + auth: Credentials, + http_client: HTTPClientType = HTTPClient, + session: Optional[Session] = None, ) -> None: - self.http_client = http_client(auth) + self.http_client = http_client(auth, session) def get_file_drive_metadata(self, id: str) -> Any: """Get the metadata from the Drive API for a specific file