Skip to content

Commit 0323312

Browse files
authored
SG-35529 Minor code refactoring: Clarify the use of _build_opener in download_attachment (#343)
Minor code refactoring: move the _build_opener call from set_up_auth_cookie to download_attachment
1 parent 2e1cd50 commit 0323312

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

docs/reference.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ control, these methods are available.
117117
.. automethod:: Shotgun.close
118118
.. automethod:: Shotgun.authenticate_human_user
119119
.. automethod:: Shotgun.get_session_token
120-
.. automethod:: Shotgun.set_up_auth_cookie
120+
.. automethod:: Shotgun.get_auth_cookie_handler
121121
.. automethod:: Shotgun.add_user_agent
122122
.. automethod:: Shotgun.reset_user_agent
123123
.. automethod:: Shotgun.set_session_uuid

shotgun_api3/shotgun.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2702,14 +2702,16 @@ def download_attachment(self, attachment=False, file_path=None, attachment_id=No
27022702
if url is None:
27032703
return None
27042704

2705-
# We only need to set the auth cookie for downloads from Shotgun server
2705+
cookie_handler = None
27062706
if self.config.server in url:
2707-
self.set_up_auth_cookie()
2707+
# We only need to set the auth cookie for downloads from Shotgun server
2708+
cookie_handler = self.get_auth_cookie_handler()
27082709

2710+
opener = self._build_opener(cookie_handler)
27092711
try:
27102712
request = urllib.request.Request(url)
27112713
request.add_header("user-agent", "; ".join(self._user_agents))
2712-
req = urllib.request.urlopen(request)
2714+
req = opener.open(request)
27132715
if file_path:
27142716
shutil.copyfileobj(req, fp)
27152717
else:
@@ -2750,21 +2752,21 @@ def download_attachment(self, attachment=False, file_path=None, attachment_id=No
27502752
else:
27512753
return attachment
27522754

2753-
def set_up_auth_cookie(self):
2755+
def get_auth_cookie_handler(self):
27542756
"""
2755-
Set up urllib2 with a cookie for authentication on the Shotgun instance.
2757+
Return an urllib cookie handler containing a cookie for FPTR
2758+
authentication.
27562759
2757-
Looks up session token and sets that in a cookie in the :mod:`urllib2` handler. This is
2758-
used internally for downloading attachments from the Shotgun server.
2760+
Looks up session token and sets that in a cookie in the :mod:`urllib2`
2761+
handler.
2762+
This is used internally for downloading attachments from FPTR.
27592763
"""
27602764
sid = self.get_session_token()
27612765
cj = http_cookiejar.LWPCookieJar()
27622766
c = http_cookiejar.Cookie("0", "_session_id", sid, None, False, self.config.server, False,
27632767
False, "/", True, False, None, True, None, None, {})
27642768
cj.set_cookie(c)
2765-
cookie_handler = urllib.request.HTTPCookieProcessor(cj)
2766-
opener = self._build_opener(cookie_handler)
2767-
urllib.request.install_opener(opener)
2769+
return urllib.request.HTTPCookieProcessor(cj)
27682770

27692771
def get_attachment_download_url(self, attachment):
27702772
"""

0 commit comments

Comments
 (0)