From 22139683834b8323d92ac0c4824814566aab6fa0 Mon Sep 17 00:00:00 2001 From: Julien Langlois Date: Fri, 31 May 2024 14:14:21 -0700 Subject: [PATCH 1/2] Minor code refactoring: move the _build_opener call from set_up_auth_cookie to download_attachment --- shotgun_api3/shotgun.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index 3adacb92f..64beaa5ca 100644 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -2718,14 +2718,16 @@ def download_attachment(self, attachment=False, file_path=None, attachment_id=No if url is None: return None + cookie_handler = None # We only need to set the auth cookie for downloads from Shotgun server if self.config.server in url: - self.set_up_auth_cookie() + cookie_handler = self.get_auth_cookie_handler() try: + opener = self._build_opener(cookie_handler) request = urllib.request.Request(url) request.add_header("user-agent", "; ".join(self._user_agents)) - req = urllib.request.urlopen(request) + req = opener.open(request) if file_path: shutil.copyfileobj(req, fp) else: @@ -2766,7 +2768,7 @@ def download_attachment(self, attachment=False, file_path=None, attachment_id=No else: return attachment - def set_up_auth_cookie(self): + def get_auth_cookie_handler(self): """ Set up urllib2 with a cookie for authentication on the Shotgun instance. @@ -2778,9 +2780,7 @@ def set_up_auth_cookie(self): c = http_cookiejar.Cookie("0", "_session_id", sid, None, False, self.config.server, False, False, "/", True, False, None, True, None, None, {}) cj.set_cookie(c) - cookie_handler = urllib.request.HTTPCookieProcessor(cj) - opener = self._build_opener(cookie_handler) - urllib.request.install_opener(opener) + return urllib.request.HTTPCookieProcessor(cj) def get_attachment_download_url(self, attachment): """ From 43a006442eed95b6dd1d9250a2a751da53bb503f Mon Sep 17 00:00:00 2001 From: Julien Langlois Date: Mon, 3 Jun 2024 13:46:01 -0700 Subject: [PATCH 2/2] fixup! Minor code refactoring: move the _build_opener call from set_up_auth_cookie to download_attachment --- docs/reference.rst | 2 +- shotgun_api3/shotgun.py | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/docs/reference.rst b/docs/reference.rst index 8c9e6af6f..da171ea85 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -117,7 +117,7 @@ control, these methods are available. .. automethod:: Shotgun.close .. automethod:: Shotgun.authenticate_human_user .. automethod:: Shotgun.get_session_token -.. automethod:: Shotgun.set_up_auth_cookie +.. automethod:: Shotgun.get_auth_cookie_handler .. automethod:: Shotgun.add_user_agent .. automethod:: Shotgun.reset_user_agent .. automethod:: Shotgun.set_session_uuid diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index 64beaa5ca..d59ceafbc 100644 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -2719,12 +2719,12 @@ def download_attachment(self, attachment=False, file_path=None, attachment_id=No return None cookie_handler = None - # We only need to set the auth cookie for downloads from Shotgun server if self.config.server in url: + # We only need to set the auth cookie for downloads from Shotgun server cookie_handler = self.get_auth_cookie_handler() + opener = self._build_opener(cookie_handler) try: - opener = self._build_opener(cookie_handler) request = urllib.request.Request(url) request.add_header("user-agent", "; ".join(self._user_agents)) req = opener.open(request) @@ -2770,10 +2770,12 @@ def download_attachment(self, attachment=False, file_path=None, attachment_id=No def get_auth_cookie_handler(self): """ - Set up urllib2 with a cookie for authentication on the Shotgun instance. + Return an urllib cookie handler containing a cookie for FPTR + authentication. - Looks up session token and sets that in a cookie in the :mod:`urllib2` handler. This is - used internally for downloading attachments from the Shotgun server. + Looks up session token and sets that in a cookie in the :mod:`urllib2` + handler. + This is used internally for downloading attachments from FPTR. """ sid = self.get_session_token() cj = http_cookiejar.LWPCookieJar()