44
55import requests
66import tableauserverclient as TSC
7+ import urllib3
78from urllib3 .exceptions import InsecureRequestWarning
89
910from tabcmd .commands .constants import Errors
@@ -74,7 +75,26 @@ def _update_session_data(self, args):
7475 self .no_certcheck = args .no_certcheck or self .no_certcheck
7576 self .no_proxy = args .no_proxy or self .no_proxy
7677 self .proxy = args .proxy or self .proxy
77- self .timeout = args .timeout or self .timeout
78+ self .timeout = self .timeout_as_integer (self .logger , args .timeout , self .timeout )
79+
80+ @staticmethod
81+ def timeout_as_integer (logger , option_1 , option_2 ):
82+ result = None
83+ if option_1 :
84+ try :
85+ result = int (option_1 )
86+ except Exception as anyE :
87+ result = 0
88+ if option_2 and (not result or result <= 0 ):
89+ try :
90+ result = int (option_2 )
91+ except Exception as anyE :
92+ result = 0
93+ if not option_1 and not option_2 :
94+ logger .debug (_ ("setsetting.status" ).format ("timeout" , "None" ))
95+ elif not result or result <= 0 :
96+ logger .warning (_ ("sessionoptions.errors.bad_timeout" ).format ("--timeout" , result ))
97+ return result or 0
7898
7999 @staticmethod
80100 def _read_password_from_file (filename ):
@@ -108,7 +128,7 @@ def _create_new_credential(self, password, credential_type):
108128 credentials = self ._create_new_token_credential ()
109129 return credentials
110130 else :
111- Errors .exit_with_error (self .logger , "Couldn't find credentials" )
131+ Errors .exit_with_error (self .logger , _ ( "session.errors.missing_arguments" ). format ( "" ) )
112132
113133 def _create_new_token_credential (self ):
114134 if self .token_value :
@@ -127,31 +147,60 @@ def _create_new_token_credential(self):
127147 else :
128148 Errors .exit_with_error (self .logger , _ ("session.errors.missing_arguments" ).format ("token name" ))
129149
130- def _set_connection_options (self ):
150+ def _set_connection_options (self ) -> TSC .Server :
151+ self .logger .debug ("Setting up request options" )
131152 # args still to be handled here:
132153 # proxy, --no-proxy,
133154 # cert
134- # timeout
135155 http_options = {}
136156 if self .no_certcheck :
137- http_options = {"verify" : False }
138- requests .packages .urllib3 .disable_warnings (category = InsecureRequestWarning )
157+ http_options ["verify" ] = False
158+ urllib3 .disable_warnings (category = InsecureRequestWarning )
159+ if self .proxy :
160+ # do we catch this error? "sessionoptions.errors.bad_proxy_format"
161+ self .logger .debug ("Setting proxy: " , self .proxy )
162+ if self .timeout :
163+ http_options ["timeout" ] = self .timeout
139164 try :
140- tableau_server = TSC .Server (self .server_url , use_server_version = True , http_options = http_options )
165+ self .logger .debug (http_options )
166+ tableau_server = TSC .Server (self .server_url , http_options = http_options )
167+
141168 except Exception as e :
169+ self .logger .debug (
170+ "Connection args: server {}, site {}, proxy {}, cert {}" .format (
171+ self .server_url , self .site_name , self .proxy , self .certificate
172+ )
173+ )
142174 Errors .exit_with_error (self .logger , "Failed to connect to server" , e )
143175
176+ self .logger .debug ("Finished setting up connection" )
144177 return tableau_server
145178
146- def _create_new_connection (self ):
179+ def _verify_server_connection_unauthed (self ):
180+ try :
181+ self .tableau_server .use_server_version ()
182+ except requests .exceptions .ReadTimeout as timeout_error :
183+ Errors .exit_with_error (
184+ self .logger ,
185+ message = "Timed out after {} seconds attempting to connect to server" .format (self .timeout ),
186+ exception = timeout_error ,
187+ )
188+ except requests .exceptions .RequestException as requests_error :
189+ Errors .exit_with_error (
190+ self .logger , message = "Error attempting to connect to the server" , exception = requests_error
191+ )
192+ except Exception as e :
193+ Errors .exit_with_error (self .logger , exception = e )
194+
195+ def _create_new_connection (self ) -> TSC .Server :
147196 self .logger .info (_ ("session.new_session" ))
148- self .tableau_server = self ._set_connection_options ()
149197 self ._print_server_info ()
150198 self .logger .info (_ ("session.connecting" ))
151199 try :
152- self .tableau_server . use_server_version () # this will attempt to contact the server
200+ self .tableau_server = self . _set_connection_options ()
153201 except Exception as e :
154202 Errors .exit_with_error (self .logger , "Failed to connect to server" , e )
203+ return self .tableau_server
155204
156205 def _read_existing_state (self ):
157206 if self ._check_json ():
@@ -168,7 +217,7 @@ def _print_server_info(self):
168217
169218 def _validate_existing_signin (self ):
170219 self .logger .info (_ ("session.continuing_session" ))
171- self . tableau_server = self ._set_connection_options ( )
220+ # when do these two messages show up? self.logger.info(_("session.auto_site_login") )
172221 try :
173222 if self .tableau_server and self .tableau_server .is_signed_in ():
174223 response = self .tableau_server .users .get_by_id (self .user_id )
@@ -181,7 +230,8 @@ def _validate_existing_signin(self):
181230 self .logger .info (_ ("errors.internal_error.request.message" ), e )
182231 return None
183232
184- def _sign_in (self , tableau_auth ):
233+ # server connection created, not yet logged in
234+ def _sign_in (self , tableau_auth ) -> TSC .Server :
185235 self .logger .debug (_ ("session.login" ) + self .server_url )
186236 self .logger .debug (_ ("listsites.output" ).format ("" , self .username or self .token_name , self .site_name ))
187237 try :
@@ -245,7 +295,8 @@ def create_session(self, args):
245295
246296 if credentials and not signed_in_object :
247297 # logging in, not using an existing session
248- self ._create_new_connection ()
298+ self .tableau_server = self ._create_new_connection ()
299+ self ._verify_server_connection_unauthed ()
249300 signed_in_object = self ._sign_in (credentials )
250301
251302 if not signed_in_object :
0 commit comments