<jupyter_start><jupyter_text>AUTH API Utils<jupyter_code>import os
from sqlalchemy import desc
import datedelta
import base64
import uuid
ACCOUNT_SVC_AUTH_URL = os.getenv('ACCOUNT_SVC_AUTH_URL')
ACCOUNT_SVC_CLIENT_ID = os.getenv('ACCOUNT_SVC_CLIENT_ID')
ACCOUNT_SVC_CLIENT_SECRET = os.getenv('ACCOUNT_SVC_CLIENT_SECRET')
ACCOUNT_SVC_AFFILIATE_URL = os.getenv('ACCOUNT_SVC_AFFILIATE_URL')
ACCOUNT_SVC_ENTITY_URL = os.getenv('ACCOUNT_SVC_ENTITY_URL')
def get_auth_token(verify_ssl = True):
    payload = "grant_type=client_credentials"
    basic_hash = base64.b64encode(f'{ACCOUNT_SVC_CLIENT_ID}:{ACCOUNT_SVC_CLIENT_SECRET}'.encode()).decode()
    basic_hash
    headers = {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': f'Basic {basic_hash}'
    }

    response = requests.request("POST", ACCOUNT_SVC_AUTH_URL, headers=headers, data=payload, verify=verify_ssl)

    auth_token = response.json()['access_token']
    # auth_token
    assert auth_token
    return auth_token
def affiliate_nr_to_account(auth_token: str, nr_num: str, email: str, account_id: int, verify_ssl = True):
    url = ACCOUNT_SVC_AFFILIATE_URL.format(account_id=account_id)
    url = f'{url}?newBusiness=true'

    headers = {
      'Authorization': f'Bearer {auth_token}'
    }

    payload = {
      "businessIdentifier": nr_num,
      "phone": "",
      "email": email
    }

    r = requests.post(url, headers=headers, json=payload, verify=verify_ssl)
    return r
def affiliate_nr_to_entity(auth_token: str, nr_num: str, nr_name: str, verify_ssl = True):
    url = f'{ACCOUNT_SVC_ENTITY_URL}/{nr_num}'

    headers = {
      'Authorization': f'Bearer {auth_token}'
    }

    payload = {
      "businessIdentifier": nr_num,
      "name": nr_name
    }

    r = requests.patch(url, headers=headers, json=payload, verify=verify_ssl)    
    return r    
def get_identifier_from_affiliations(auth_token: str, account_id: int, nr_name: str, verify_ssl = True):
    url = ACCOUNT_SVC_AFFILIATE_URL.format(account_id=account_id)
    
    headers = {
      'Authorization': f'Bearer {auth_token}'
    }

    r = requests.get(url, headers=headers, verify=verify_ssl)
    r.status_code, r.text
    response_dict = json.loads(r.text)
    entities = response_dict['entities']

    matching_entities = list(filter(lambda x: x['name'] == approved_nr_name, entities))
    # print(f'matching_entities: {matching_entities}, matches: {len(matching_entities)}')
    assert len(matching_entities) == 1
    identifier = matching_entities[0]['businessIdentifier']
    return identifier<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11516314779270634, 'fraction_numerical': 0.003071017274472169, 'mean_word_length': 4.7274725274725276, 'pattern_counts': {'":': 5, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '99731', 'license': 'Apache-2.0', 'n_tokens_mistral': 931, 'n_tokens_neox': 925, 'n_words': 152, 'path': 'tests/data/common/auth_api_utils.ipynb', 'repo_name': 'ozamani9/lear', 'size': '2605'}