"""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 from looker_updater import config def get_look_json(look_id): """Gets json of a look from main looker instance. Args: look_id (int): id of a look from main looker instance. Found in looks URL after .com/looks/ Returns: dict: json representation of a look. bool: False if something went wrong and http response was other than 200. """ response = requests.get( f'{config.LOOKER_MAIN_INSTANCE_URL}/looks/{look_id}', headers=config.LOOKER_MAIN_VALIDATION ) if response.status_code == 200: return response.json() return False def adjust_look_data(look_json, client_instance_query_id, space_id=37, folder_id=37): """Sets 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 in 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. Currently set 37 as default for rolkhovskiy@theorchard.com user base folder. Should later be changed to a dedicated space. folder_id (int): id of a folder where the look should be placed. Currently set 37 as default for rolkhovskiy@theorchard.com user base folder. Should later be changed to a dedicated space. 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): """Creates 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. bool: False if something went wrong and http response was other than 200. """ response = requests.post( f'{config.LOOKER_CLIENT_INSTANCE_URL}/looks', headers=config.LOOKER_CLIENT_VALIDATION, json=json ) if response.status_code == 200: return response.json()['id'] return False def get_all_looks(instance=config.LOOKER_CLIENT_INSTANCE_URL): """Gets 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. bool: False if response was other than 200. """ validation = config.LOOKER_CLIENT_VALIDATION if instance == config.LOOKER_MAIN_INSTANCE_URL: validation = config.LOOKER_MAIN_VALIDATION response = requests.get( f'{instance}/looks', headers=validation ) if response.status_code == 200: return response.json() return False def get_look_title(look_json): """Gets the title of a given look. Args: look_json (dict): json representation of a look. Comes as a return value of get_look function. Returns: str: title of a given look. """ if look_json: return look_json.get('title', '') return False def get_id_of_a_look_on_client_instance(look_title, list_of_looks): """Gets 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): """Updates 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 function. look_json (dict): json representation of a look. Comes as a return value of adjust_look_data function. Returns: bool: True if update was successful, False otherwise. """ response = requests.patch( f'{config.LOOKER_CLIENT_INSTANCE_URL}/looks/{client_look_id}', data=look_json ) if response.status_code == 200: return True return False def check_look_on_client_instance(main_instance_look_json): """Checks if a look exists on client instance. Args: main_instance_look_json (dict): json representation of a look. Returns: calls get_id_of_a_look_on_client_instance function. """ all_looks_on_client_instance = get_all_looks() look_title = get_look_title(main_instance_look_json) return get_id_of_a_look_on_client_instance(look_title, all_looks_on_client_instance)