(requests, domain, form_json, openmrs_config, case_trigger_infos)
return RepeaterResponse(200, "OK")
Updates an OpenMRS patient and (optionally) creates visits.

    This involves several requests to the `OpenMRS REST Web Services`_. If any of those requests fail, we want to
    roll back previous changes to avoid inconsistencies in OpenMRS. To do this we define a workflow of tasks we
    want to do. Each workflow task has a rollback task. If a task fails, all previous tasks are rolled back in
    reverse order.

    :return: A response-like object that can be used by Repeater.handle_response(),
             RepeatRecord.handle_success() and RepeatRecord.handle_failure()


    .. _OpenMRS REST Web Services: https://wiki.openmrs.org/display/docs/REST+Web+Services+API+For+Clients
Updates an OpenMRS patient and (optionally) creates visits.
Updates an OpenMRS patient and ( optionally ) creates visits .
def send_openmrs_data(requests, domain, form_json, openmrs_config, case_trigger_infos):
    """
    Updates an OpenMRS patient and (optionally) creates visits.

    This involves several requests to the `OpenMRS REST Web Services`_. If any of those requests fail, we want to
    roll back previous changes to avoid inconsistencies in OpenMRS. To do this we define a workflow of tasks we
    want to do. Each workflow task has a rollback task. If a task fails, all previous tasks are rolled back in
    reverse order.

    :return: A response-like object that can be used by Repeater.handle_response(),
             RepeatRecord.handle_success() and RepeatRecord.handle_failure()


    .. _OpenMRS REST Web Services: https://wiki.openmrs.org/display/docs/REST+Web+Services+API+For+Clients
    """
    warnings = []
    errors = []
    for info in case_trigger_infos:
        assert isinstance(info, CaseTriggerInfo)
        try:
            patient = get_patient(requests, domain, info, openmrs_config)
        except (RequestException, HTTPError) as err:
            errors.append(_(
                "Unable to create an OpenMRS patient for case "
                f"{info.case_id!r}: {err}"
            ))
            continue
        if patient is None:
            warnings.append(
                f"CommCare case '{info.case_id}' was not matched to a "
                f"patient in OpenMRS instance '{requests.base_url}'."
            )
            continue

        # case_trigger_infos are info about all of the cases
        # created/updated by the form. Execute a separate workflow to
        # update each patient.
        workflow = [
            # Update name first. If the current name in OpenMRS fails
            # validation, other API requests will be rejected.
            UpdatePersonNameTask(requests, info, openmrs_config, patient['person']),
            # Update identifiers second. If a current identifier fails
            # validation, other API requests will be rejected.
            SyncPatientIdentifiersTask(requests, info, openmrs_config, patient),
            # Now we should be able to update the rest.
            UpdatePersonPropertiesTask(requests, info, openmrs_config, patient['person']),
            SyncPersonAttributesTask(
                requests, info, openmrs_config, patient['person']['uuid'], patient['person']['attributes']
            ),
        ]
        if patient['person']['preferredAddress']:
            workflow.append(
                UpdatePersonAddressTask(requests, info, openmrs_config, patient['person'])
            )
        else:
            workflow.append(
                CreatePersonAddressTask(requests, info, openmrs_config, patient['person'])
            )
        workflow.append(
            CreateVisitsEncountersObsTask(
                requests, domain, info, form_json, openmrs_config, patient['person']['uuid']
            ),
        )

        errors.extend(
            execute_workflow(workflow)
        )

    if errors:
        requests.notify_error(f'Errors encountered sending OpenMRS data: {errors}')
        # If the form included multiple patients, some workflows may
        # have succeeded, but don't say everything was OK if any
        # workflows failed. (Of course most forms will only involve one
        # case, so one workflow.)
        return RepeaterResponse(400, 'Bad Request', "Errors: " + pformat_json([str(e) for e in errors]))

    if warnings:
        return RepeaterResponse(201, "Accepted", "Warnings: " + pformat_json([str(e) for e in warnings]))

    return RepeaterResponse(200, "OK")
def send_openmrs_data ( requests , domain , form_json , openmrs_config , case_trigger_infos ) : warnings = [ ] errors = [ ] for info in case_trigger_infos : assert isinstance ( info , CaseTriggerInfo ) try : patient = get_patient ( requests , domain , info , openmrs_config ) except ( RequestException , HTTPError ) as err : errors . append ( _ ( "Unable to create an OpenMRS patient for case " f"{info.case_id!r}: {err}" ) ) continue if patient is None : warnings . append ( f"CommCare case '{info.case_id}' was not matched to a " f"patient in OpenMRS instance '{requests.base_url}'." ) continue # case_trigger_infos are info about all of the cases # created/updated by the form. Execute a separate workflow to # update each patient. workflow = [ # Update name first. If the current name in OpenMRS fails # validation, other API requests will be rejected. UpdatePersonNameTask ( requests , info , openmrs_config , patient [ 'person' ] ) , # Update identifiers second. If a current identifier fails # validation, other API requests will be rejected. SyncPatientIdentifiersTask ( requests , info , openmrs_config , patient ) , # Now we should be able to update the rest. UpdatePersonPropertiesTask ( requests , info , openmrs_config , patient [ 'person' ] ) , SyncPersonAttributesTask ( requests , info , openmrs_config , patient [ 'person' ] [ 'uuid' ] , patient [ 'person' ] [ 'attributes' ] ) , ] if patient [ 'person' ] [ 'preferredAddress' ] : workflow . append ( UpdatePersonAddressTask ( requests , info , openmrs_config , patient [ 'person' ] ) ) else : workflow . append ( CreatePersonAddressTask ( requests , info , openmrs_config , patient [ 'person' ] ) ) workflow . append ( CreateVisitsEncountersObsTask ( requests , domain , info , form_json , openmrs_config , patient [ 'person' ] [ 'uuid' ] ) , ) errors . extend ( execute_workflow ( workflow ) ) if errors : requests . notify_error ( f'Errors encountered sending OpenMRS data: {errors}' ) # If the form included multiple patients, some workflows may # have succeeded, but don't say everything was OK if any # workflows failed. (Of course most forms will only involve one # case, so one workflow.) return RepeaterResponse ( 400 , 'Bad Request' , "Errors: " + pformat_json ( [ str ( e ) for e in errors ] ) ) if warnings : return RepeaterResponse ( 201 , "Accepted" , "Warnings: " + pformat_json ( [ str ( e ) for e in warnings ] ) ) return RepeaterResponse ( 200 , "OK" )