Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagepy
titlePython example
linenumberstrue
collapsetrue
def find_setting_uuid(param_name: str, product_code: str) -> str:
    hawk_auth = HawkAuth(id=API_KEY, key=API_SECRET, always_hash_content=False)
    headers = {'accept': 'application/json'}

    # Fetch API root and get link to product & setting lists
    response = requests.get(url=GRAPE_BASE_URL, headers=headers, auth=hawk_auth)
    if response.status_code != 200:
        print(f'Error fetching API root: {response.text}')
        exit(1)

    api_root = response.json()

    setting_list_url = f'{api_root["links"]["settings"]}'

    response = requests.get(url=setting_list_url, headers=headers, auth=hawk_auth)

    if response.status_code != 200:
        print(f'Error fetching setting list: {response.text}')
        exit(1)

    settings = response.json()

    product_url = f'{api_root["links"]["products"]}{product_code}'
    response = requests.get(url=product_url, headers=headers, auth=hawk_auth)

    if response.status_code != 200:
        print(f'Error fetching product: {response.text}')
        exit(1)

    product_detail = response.json()

    for setting in settings:
        if setting['param_name'] != param_name:
            continue
        # Crucial check: ensure the setting's product groups intersect with the product's groups
        if 'product_groups' in setting and 'groups' in product_detail:
            if bool(set(setting['product_groups']).intersection(product_detail['groups'])):
                return setting['uuid']
        # Fallback for settings that might not explicitly list product_groups or have universal applicability
        elif 'product_groups' not in setting and 'groups' not in product_detail:
             return setting['uuid']

    # If no matching UUID is found through the dynamic lookup
    return None

...