From c54b8e76e1094b53befbd54585249c76dbfa8971 Mon Sep 17 00:00:00 2001 From: Julien Langlois Date: Wed, 13 Sep 2023 09:55:05 -0700 Subject: [PATCH 1/3] Revert SG-32830 and use required name argument --- .../authentication/app_session_launcher.py | 23 +++------ .../authentication/console_authentication.py | 2 +- python/tank/authentication/login_dialog.py | 2 +- .../test_app_session_launcher.py | 49 ++++++++++--------- 4 files changed, 37 insertions(+), 39 deletions(-) diff --git a/python/tank/authentication/app_session_launcher.py b/python/tank/authentication/app_session_launcher.py index 3176564c4c..6ebd25562e 100644 --- a/python/tank/authentication/app_session_launcher.py +++ b/python/tank/authentication/app_session_launcher.py @@ -16,7 +16,6 @@ import time import tank -from tank_vendor import six from tank_vendor.six.moves import http_client, urllib from . import errors @@ -62,9 +61,10 @@ def format(self): def process( sg_url, - browser_open_callback, + *, http_proxy=None, product=None, + browser_open_callback, keep_waiting_callback=lambda: True, ): sg_url = connection.sanitize_url(sg_url) @@ -103,7 +103,7 @@ def process( request = urllib.request.Request( urllib.parse.urljoin(sg_url, "/internal_api/app_session_request"), - # method="POST", # see below + method="POST", data=urllib.parse.urlencode( { "appName": product, @@ -115,9 +115,6 @@ def process( }, ) - # Hook for Python 2 - request.get_method = lambda: "POST" - response = http_request(url_opener, request) logger.debug( "Initial network request returned HTTP code {code}".format( @@ -201,6 +198,10 @@ def process( "/internal_api/app_session_request/{session_id}".format( session_id=session_id, ), + method="PUT", + headers={ + "User-Agent": user_agent, + }, ) approved = False @@ -355,14 +356,6 @@ def get_product_name(): return PRODUCT_DEFAULT -def _get_content_type(headers): - if six.PY2: - value = headers.get("content-type", "text/plain") - return value.split(";", 1)[0].lower() - else: - return headers.get_content_type() - - def http_request(opener, req, max_attempts=4): attempt = 0 backoff = 0.75 # Seconds to wait before retry, times the attempt number @@ -422,7 +415,7 @@ def http_request(opener, req, max_attempts=4): parent_exception=exc, ) - if _get_content_type(response.headers) == "application/json": + if response.headers.get_content_type() == "application/json": try: response.json = json.load(response) except json.JSONDecodeError as exc: diff --git a/python/tank/authentication/console_authentication.py b/python/tank/authentication/console_authentication.py index 679a6aa46a..e4ca3af6cf 100644 --- a/python/tank/authentication/console_authentication.py +++ b/python/tank/authentication/console_authentication.py @@ -181,7 +181,7 @@ def _authenticate_app_session_launcher(self, hostname, login, http_proxy): print() session_info = app_session_launcher.process( hostname, - webbrowser.open, # browser_open_callback + browser_open_callback=webbrowser.open, http_proxy=http_proxy, ) diff --git a/python/tank/authentication/login_dialog.py b/python/tank/authentication/login_dialog.py index 23eabec683..d41d07dec4 100644 --- a/python/tank/authentication/login_dialog.py +++ b/python/tank/authentication/login_dialog.py @@ -975,7 +975,7 @@ def run(self): try: self.session_info = app_session_launcher.process( self._sg_url, - lambda u: QtGui.QDesktopServices.openUrl(u), # browser_open_callback + browser_open_callback=lambda u: QtGui.QDesktopServices.openUrl(u), http_proxy=self._http_proxy, product=self._product, keep_waiting_callback=self.should_continue, diff --git a/tests/authentication_tests/test_app_session_launcher.py b/tests/authentication_tests/test_app_session_launcher.py index dcbbbac543..9c06e967ec 100644 --- a/tests/authentication_tests/test_app_session_launcher.py +++ b/tests/authentication_tests/test_app_session_launcher.py @@ -40,19 +40,24 @@ def test_process_parameters(self): with self.assertRaises(AssertionError): app_session_launcher.process( "https://test.shotgunstudio.com", - None, # browser_open_callback ) with self.assertRaises(AssertionError): app_session_launcher.process( "https://test.shotgunstudio.com", - "Test", # browser_open_callback + browser_open_callback=None, ) with self.assertRaises(AssertionError): app_session_launcher.process( "https://test.shotgunstudio.com", - lambda: True, # browser_open_callback + browser_open_callback="Test", + ) + + with self.assertRaises(AssertionError): + unified_login_flow2.process( + "https://test.shotgunstudio.com", + browser_open_callback=lambda: True, keep_waiting_callback=None, ) @@ -165,7 +170,7 @@ def url_opener(url): self.assertEqual( app_session_launcher.process( self.api_url, - url_opener, # browser_open_callback + browser_open_callback=url_opener, http_proxy="{fqdn}:{port}".format( # For code coverage fqdn=self.httpd.server_address[0], port=self.httpd.server_address[1], @@ -188,7 +193,7 @@ def test_not_reachable(self, *mocks): with self.assertRaises(app_session_launcher.AuthenticationError) as cm: app_session_launcher.process( self.api_url, - lambda url: True, # browser_open_callback + browser_open_callback=lambda url: True, ) self.assertIsInstance( @@ -251,7 +256,7 @@ def api_put_handler(request): self.assertEqual( app_session_launcher.process( self.api_url, - lambda url: True, # browser_open_callback + browser_open_callback=lambda url: True, ), (self.api_url, "john", "to123", None), ) @@ -262,7 +267,7 @@ def test_post_request(self, *mocks): with self.assertRaises(app_session_launcher.AuthenticationError) as cm: app_session_launcher.process( self.api_url, - lambda url: True, # browser_open_callback + browser_open_callback=lambda url: True, ) self.assertEqual( @@ -276,7 +281,7 @@ def test_post_request(self, *mocks): with self.assertRaises(app_session_launcher.AuthenticationError) as cm: app_session_launcher.process( self.api_url, - lambda url: True, # browser_open_callback + browser_open_callback=lambda url: True, ) self.assertEqual( @@ -292,7 +297,7 @@ def api_handler1(request): with self.assertRaises(app_session_launcher.AuthenticationError) as cm: app_session_launcher.process( self.api_url, - lambda url: True, # browser_open_callback + browser_open_callback=lambda url: True, ) self.assertIsInstance( @@ -311,7 +316,7 @@ def api_handler1(request): with self.assertRaises(app_session_launcher.AuthenticationError) as cm: app_session_launcher.process( self.api_url, - lambda url: True, # browser_open_callback + browser_open_callback=lambda url: True, ) self.assertEqual( @@ -325,7 +330,7 @@ def api_handler1(request): with self.assertRaises(app_session_launcher.AuthenticationError) as cm: app_session_launcher.process( self.api_url, - lambda url: True, # browser_open_callback + browser_open_callback=lambda url: True, ) self.assertEqual( @@ -340,7 +345,7 @@ def api_handler1(request): with self.assertRaises(app_session_launcher.AuthenticationError) as cm: app_session_launcher.process( self.api_url, - lambda url: True, # browser_open_callback + browser_open_callback=lambda url: True, ) self.assertEqual( @@ -356,7 +361,7 @@ def api_handler1(request): with self.assertRaises(app_session_launcher.AuthenticationError) as cm: app_session_launcher.process( self.api_url, - lambda url: True, # browser_open_callback + browser_open_callback=lambda url: True, ) self.assertEqual( @@ -372,7 +377,7 @@ def api_handler1(request): with self.assertRaises(app_session_launcher.AuthenticationError) as cm: app_session_launcher.process( self.api_url, - lambda url: True, # browser_open_callback + browser_open_callback=lambda url: True, ) self.assertEqual( @@ -388,7 +393,7 @@ def api_handler1(request): with self.assertRaises(app_session_launcher.AuthenticationError) as cm: app_session_launcher.process( self.api_url, - lambda url: True, # browser_open_callback + browser_open_callback=lambda url: True, ) self.assertEqual( @@ -406,7 +411,7 @@ def api_handler1(request): with self.assertRaises(app_session_launcher.AuthenticationError) as cm: app_session_launcher.process( self.api_url, - lambda url: True, # browser_open_callback + browser_open_callback=lambda url: True, ) self.assertEqual( @@ -426,7 +431,7 @@ def api_handler1(request): with self.assertRaises(app_session_launcher.AuthenticationError) as cm: app_session_launcher.process( self.api_url, - lambda url: True, # browser_open_callback + browser_open_callback=lambda url: True, ) self.assertEqual( @@ -445,7 +450,7 @@ def test_browser_error(self): with self.assertRaises(app_session_launcher.AuthenticationError) as cm: app_session_launcher.process( self.api_url, - lambda url: False, # browser_open_callback + browser_open_callback=lambda url: False, ) self.assertEqual( @@ -470,7 +475,7 @@ def api_handler1(request): with self.assertRaises(app_session_launcher.AuthenticationError) as cm: app_session_launcher.process( self.api_url, - lambda url: True, # browser_open_callback + browser_open_callback=lambda url: True, product="app_2a37c59", keep_waiting_callback=lambda: False, ) @@ -506,7 +511,7 @@ def api_handler1(request): with self.assertRaises(app_session_launcher.AuthenticationError) as cm: app_session_launcher.process( self.api_url, - url_opener, # browser_open_callback + browser_open_callback=url_opener, ) self.assertIsInstance( @@ -531,7 +536,7 @@ def api_handler1(request): with self.assertRaises(app_session_launcher.AuthenticationError) as cm: app_session_launcher.process( self.api_url, - lambda url: True, # browser_open_callback + browser_open_callback=lambda url: True, keep_waiting_callback=lambda: False, # Avoid 5 minute timeout ) @@ -544,7 +549,7 @@ def api_handler1(request): with self.assertRaises(app_session_launcher.AuthenticationError) as cm: app_session_launcher.process( self.api_url, - lambda url: True, # browser_open_callback + browser_open_callback=lambda url: True, keep_waiting_callback=lambda: False, # Avoid 5 minute timeout ) From 5235288de1b8c11f20b9676e76f752bd36684e2b Mon Sep 17 00:00:00 2001 From: Julien Langlois Date: Tue, 25 Feb 2025 14:49:30 -0800 Subject: [PATCH 2/3] Remove six.move from ASL --- python/tank/authentication/app_session_launcher.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/python/tank/authentication/app_session_launcher.py b/python/tank/authentication/app_session_launcher.py index 6ebd25562e..cf0b20efef 100644 --- a/python/tank/authentication/app_session_launcher.py +++ b/python/tank/authentication/app_session_launcher.py @@ -9,14 +9,15 @@ # not expressly granted therein are reserved by Shotgun Software Inc. import json +import http.client import os import platform import random import sys import time +import urllib import tank -from tank_vendor.six.moves import http_client, urllib from . import errors from .. import platform as sgtk_platform @@ -131,7 +132,7 @@ def process( ) elif response_code_major == 4: - if response.code == http_client.FORBIDDEN and hasattr(response, "json"): + if response.code == http.client.FORBIDDEN and hasattr(response, "json"): logger.debug( "HTTP response Forbidden: {data}".format(data=response.json), exc_info=getattr(response, "exception", None), @@ -148,7 +149,7 @@ def process( parent_exception=response.exception, ) - elif response.code != http_client.OK: + elif response.code != http.client.OK: raise AuthenticationError( "Unexpected response from the Flow Production Tracking site", payload=getattr(response, "json", response), @@ -265,7 +266,7 @@ def process( exc_info=getattr(response, "exception", None), ) - if response.code == http_client.NOT_FOUND and hasattr(response, "json"): + if response.code == http.client.NOT_FOUND and hasattr(response, "json"): raise AuthenticationError( "The request has been rejected or has expired." ) @@ -275,7 +276,7 @@ def process( parent_exception=getattr(response, "exception", None), ) - elif response.code != http_client.OK: + elif response.code != http.client.OK: logger.debug("Request denied: http code is: {code}".format( code=response.code, )) From e695a3e936d74a0c007ea18971105d43011a90e1 Mon Sep 17 00:00:00 2001 From: Julien Langlois Date: Tue, 25 Feb 2025 14:52:51 -0800 Subject: [PATCH 3/3] fixup! Revert SG-32830 and use required name argument --- tests/authentication_tests/test_app_session_launcher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/authentication_tests/test_app_session_launcher.py b/tests/authentication_tests/test_app_session_launcher.py index 9c06e967ec..05b1ffb093 100644 --- a/tests/authentication_tests/test_app_session_launcher.py +++ b/tests/authentication_tests/test_app_session_launcher.py @@ -55,7 +55,7 @@ def test_process_parameters(self): ) with self.assertRaises(AssertionError): - unified_login_flow2.process( + app_session_launcher.process( "https://test.shotgunstudio.com", browser_open_callback=lambda: True, keep_waiting_callback=None,