"""Look flow. Provides functions to sync over looks from main looker instance to client looker instance. As a part of this process, original looks query should be synced before posting the look to the client instance. Refer to query_tools.py module. """ import requests import config import utils def get_look_json(look_id): """Get json of a look from main looker instance. Args: look_id (int): id of a look from main looker instance. Found in looksURL after .com/looks/ Returns: dict: json representation of a look. Raises: HTTPError. """ response = requests.get( f'{config.LOOKER_MAIN_INSTANCE_URL}/looks/{look_id}', headers=config.LOOKER_MAIN_VALIDATION ) return utils.process_response(response) def adjust_look_data( look_json, client_instance_query_id, space_id=config.CLIENT_LOOKER_SPACE_ID, folder_id=config.CLIENT_LOOKER_FOLDER_ID): """Set proper look data for client looker instance. Query id, space id and folder id for a look are different between instances, therefore should be adjusted. Args: look_json (dict): json representation of a look. Should come as a return value of get_look function. client_instance_query_id (int): id of an underlying query on the CLIENT instance. Should come as a return value of post_query_to_client_instance function from query_tools.py module. space_id (int): id of a space where the look should appear. folder_id (int): id of a folder where the look should be placed. Returns: dict: a json representation of a look, ready to be posted to client instance. """ look_json['query_id'] = client_instance_query_id look_json['space_id'] = space_id look_json['folder_id'] = folder_id return look_json def post_look_to_client_instance(json): """Create a look on client looker instance. Args: json (dict): json representation of a look. Should come as a return value of adjust_look_data function. Returns: int: id of a newly created look on client instance. Raises: HTTPError. """ response = requests.post( f'{config.LOOKER_CLIENT_INSTANCE_URL}/looks', headers=config.LOOKER_CLIENT_VALIDATION, json=json ) return utils.process_response(response)['id'] def get_all_looks(instance=config.LOOKER_CLIENT_INSTANCE_URL): """Get all looks from given instance (client by default). This function is only needed in the case of updating already existing look, since there is quite little in common between looks on different instances. Most likely, they will only share a title. So in order to find a look to update you should find a title of original look, then get the whole list of looks on client instance and find the one with the same title. This way we'll know which look to update. Args: instance (str): URL of an instance. It's client by default. Returns: list: contains dicts - json representations of all looks on client instance. Raises: HTTPError. """ validation = config.get_validation(instance) response = requests.get( f'{instance}/looks', headers=validation ) return utils.process_response(response) def get_id_of_a_look_on_client_instance(look_title, list_of_looks): """Get id of a look with a given title if found in a given list. Args: look_title (str): title of a look. Comes as a return value of get_look_title function. list_of_looks (list): list of dicts which are json representations of looks. Comes as a return value of get_all_looks function. Returns: int: id of a look with a given title, if matches. bool: False if no look with such title was found. """ for look in list_of_looks: if look['title'] == look_title: return look['id'] return False def update_look(client_look_id, look_json): """Update look on a client looker instance. Args: client_look_id (int): id a of look to update. Comes as a return value of get_id_of_a_look_on_client_instance look_json (dict): json representation of a look. Comes as a return value of adjust_look_data function. Returns: dict: json representation of a look. Raises: HTTPError. """ response = requests.patch( f'{config.LOOKER_CLIENT_INSTANCE_URL}/looks/{client_look_id}', json=look_json, headers=config.LOOKER_CLIENT_VALIDATION ) return utils.process_response(response) def check_look_on_client_instance(main_instance_look_json): """Check if a look exists on client instance. Args: main_instance_look_json (dict): json representation of a look. Returns: int: id of a look with a given title, if matches. bool: False if no look with such title was found. """ all_looks_on_client_instance = get_all_looks() look_title = main_instance_look_json.get('title') return get_id_of_a_look_on_client_instance( look_title, all_looks_on_client_instance)