Skip to content

SG-35529 Minor code refactoring: Clarify the use of _build_opener in download_attachment #343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 12 additions & 10 deletions shotgun_api3/shotgun.py
Original file line number Diff line number Diff line change
Expand Up @@ -2718,14 +2718,16 @@ def download_attachment(self, attachment=False, file_path=None, attachment_id=No
if url is None:
return None

# We only need to set the auth cookie for downloads from Shotgun server
cookie_handler = None
if self.config.server in url:
self.set_up_auth_cookie()
# 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:
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:
Expand Down Expand Up @@ -2766,21 +2768,21 @@ 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.
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()
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):
"""
Expand Down
Loading