(requests, identifier_type)
return identifier
Calls the idgen module's generateIdentifier endpoint

    Identifier source ID is determined from `identifier_type`. If
    `identifier_type` doesn't have an identifier source, return None.
    If the identifier source doesn't return an identifier, return None.
    If anything goes wrong ... return None.

    Partners in Health have written basic auth support for the idgen
    module, but it is not yet widely used. Until then, requests must use
    a session that has been authenticated with the HTML login page.
Calls the idgen module's generateIdentifier endpoint
Calls the idgen module s generateIdentifier endpoint
def generate_identifier(requests, identifier_type):
    """
    Calls the idgen module's generateIdentifier endpoint

    Identifier source ID is determined from `identifier_type`. If
    `identifier_type` doesn't have an identifier source, return None.
    If the identifier source doesn't return an identifier, return None.
    If anything goes wrong ... return None.

    Partners in Health have written basic auth support for the idgen
    module, but it is not yet widely used. Until then, requests must use
    a session that has been authenticated with the HTML login page.
    """
    identifier = None
    source_id = None

    # Create a new Requests session to log in using an HTML login page.
    # See `authenticate_session()` for details.
    with Requests(
        domain_name=requests.domain_name,
        base_url=requests.base_url,
        verify=requests.verify,
        auth_manager=requests.auth_manager,
        notify_addresses=requests.notify_addresses,
        payload_id=requests.payload_id,
        logger=requests.logger,
    ) as requests_session:
        authenticate_session(requests_session)
        try:
            source_id = get_identifier_source_id(requests_session, identifier_type)
        except OpenmrsHtmlUiChanged as err:
            requests.notify_exception('Unexpected OpenMRS HTML UI', details=str(err))
        if source_id:
            # Example request: http://www.example.com/openmrs/module/idgen/generateIdentifier.form?source=1
            response = requests_session.get('/module/idgen/generateIdentifier.form', params={'source': source_id})
            try:
                if not (200 <= response.status_code < 300 and response.content):
                    raise OpenmrsException()
                try:
                    # Example response: {"identifiers": ["CHR203007"]}
                    identifier = response.json()['identifiers'][0]
                except (ValueError, IndexError, KeyError):
                    raise OpenmrsException()
            except OpenmrsException:
                requests.notify_exception(
                    'OpenMRS idgen module returned an unexpected response',
                    details=(
                        f'OpenMRS idgen module at "{response.url}" '
                        f'returned an unexpected response {response.status_code}: \r\n'
                        f'{response.content}'
                    )
                )
    return identifier
def generate_identifier ( requests , identifier_type ) : identifier = None source_id = None # Create a new Requests session to log in using an HTML login page. # See `authenticate_session()` for details. with Requests ( domain_name = requests . domain_name , base_url = requests . base_url , verify = requests . verify , auth_manager = requests . auth_manager , notify_addresses = requests . notify_addresses , payload_id = requests . payload_id , logger = requests . logger , ) as requests_session : authenticate_session ( requests_session ) try : source_id = get_identifier_source_id ( requests_session , identifier_type ) except OpenmrsHtmlUiChanged as err : requests . notify_exception ( 'Unexpected OpenMRS HTML UI' , details = str ( err ) ) if source_id : # Example request: http://www.example.com/openmrs/module/idgen/generateIdentifier.form?source=1 response = requests_session . get ( '/module/idgen/generateIdentifier.form' , params = { 'source' : source_id } ) try : if not ( 200 <= response . status_code < 300 and response . content ) : raise OpenmrsException ( ) try : # Example response: {"identifiers": ["CHR203007"]} identifier = response . json ( ) [ 'identifiers' ] [ 0 ] except ( ValueError , IndexError , KeyError ) : raise OpenmrsException ( ) except OpenmrsException : requests . notify_exception ( 'OpenMRS idgen module returned an unexpected response' , details = ( f'OpenMRS idgen module at "{response.url}" ' f'returned an unexpected response {response.status_code}: \r\n' f'{response.content}' ) ) return identifier