def _set_access_token(username: str, access_token: str, connect_string: str) -> Optional[str]: """ Sets the access_token for the connect request by doing the following: 1. retrieves the token configured for the connect_string from the .arkouda/tokens.txt file, if any 2. if access_token is None, returns the retrieved token 3. if access_token is not None, replaces retrieved token with the access_token to account for situations where the token can change for a url (for example, the arkouda_server is restarted and a corresponding new token is generated). :param str username: the username retrieved from the user's home directory :param str access_token: the access_token supplied by the user, defaults to None :param str connect_string: the arkouda_server host:port connect string :return: the retrieved or supplied access_token, defaults to None """ path = '{}/tokens.txt'.format(security.get_arkouda_client_directory()) tokens = io_util.delimited_file_to_dict(path) if access_token and access_token not in {'', 'None'}: saved_token = tokens.get(connect_string) if saved_token is None or saved_token != access_token: tokens[connect_string] = access_token io_util.dict_to_delimited_file(values=tokens, path=path, delimiter=',') return access_token else: tokens = io_util.delimited_file_to_dict(path) return tokens.get(connect_string)
def _set_access_token(access_token: Optional[str], connect_string: str = 'localhost:5555') -> Optional[str]: """ Sets the access_token for the connect request by doing the following: 1. retrieves the token configured for the connect_string from the .arkouda/tokens.txt file, if any 2. if access_token is None, returns the retrieved token 3. if access_token is not None, replaces retrieved token with the access_token to account for situations where the token can change for a url (for example, the arkouda_server is restarted and a corresponding new token is generated). Parameters ---------- username : str The username retrieved from the user's home directory access_token : str The access_token supplied by the user, which is required if authentication is enabled, defaults to None connect_string : str The arkouda_server host:port connect string, defaults to localhost:5555 Returns ------- str The access token configured for the host:port, None if there is no token configured for the host:port Raises ------ IOError If there's an error writing host:port -> access_token mapping to the user's tokens.txt file or retrieving the user's tokens """ path = '{}/tokens.txt'.format(security.get_arkouda_client_directory()) try: tokens = io_util.delimited_file_to_dict(path) except Exception as e: raise IOError(e) if cast(str, access_token) and cast(str, access_token) not in {'', 'None'}: saved_token = tokens.get(connect_string) if saved_token is None or saved_token != access_token: tokens[connect_string] = cast(str, access_token) try: io_util.dict_to_delimited_file(values=tokens, path=path, delimiter=',') except Exception as e: raise IOError(e) return access_token else: try: tokens = io_util.delimited_file_to_dict(path) except Exception as e: raise IOError(e) return tokens.get(connect_string)
def testDelimitedFileToDict(self): io_util.write_line_to_file(path='{}/testfile.txt'.format( IOUtilTest.io_test_dir), line='localhost:5555,9ty4h6olr4') io_util.write_line_to_file(path='{}/testfile.txt'.format( IOUtilTest.io_test_dir), line='127.0.0.1:5556,6ky3i91l17') values = io_util.delimited_file_to_dict(path='{}/testfile.txt'.format( IOUtilTest.io_test_dir), delimiter=',') self.assertTrue(values) self.assertEqual('9ty4h6olr4', values['localhost:5555']) self.assertEqual('6ky3i91l17', values['127.0.0.1:5556']) Path.unlink(Path('{}/testfile.txt'.format(IOUtilTest.io_test_dir)))