예제 #1
0
def make_fhir_bundle_entry(
    resource_type_url: str,
    identifier: Identifier,
    resource: Dict,
    identifier_is_list: bool = True,
) -> Dict:
    """
    Builds a FHIR BundleEntry, as a JSON dict.

    This also takes care of the identifier, by ensuring (a) that the resource
    is labelled with the identifier, and (b) that the BundleEntryRequest has
    an ifNoneExist condition referring to that identifier.
    """
    if Fc.IDENTIFIER in resource:
        log.warning(f"Duplication: {Fc.IDENTIFIER!r} specified in resource "
                    f"but would be auto-added by make_fhir_bundle_entry()")
    if identifier_is_list:
        # Some, like Observation, Patient, and Questionnaire, need lists here.
        resource[Fc.IDENTIFIER] = [identifier.as_json()]
    else:
        # Others, like QuestionnaireResponse, don't.
        resource[Fc.IDENTIFIER] = identifier.as_json()
    bundle_request = BundleEntryRequest(
        jsondict={
            Fc.METHOD: HttpMethod.POST,
            Fc.URL: resource_type_url,
            Fc.IF_NONE_EXIST: fhir_reference_from_identifier(identifier),
            # "If this resource doesn't exist, as determined by this
            # identifier, then create it:"
            # https://www.hl7.org/fhir/http.html#ccreate
        })
    return BundleEntry(jsondict={
        Fc.REQUEST: bundle_request.as_json(),
        Fc.RESOURCE: resource
    }).as_json()
예제 #2
0
    def delete(self, request, questionnaire_id, format=None):
        """
        Deletes A FHIR Questionnaire for the passed ID
        """
        # Use the FHIR client lib to validate our resource.
        questionnaire_request = BundleEntryRequest(
            {
                "url": f"Questionnaire/{questionnaire_id}",
                "method": "DELETE",
            }
        )
        questionnaire_entry = BundleEntry()
        questionnaire_entry.request = questionnaire_request

        # Validate it.
        bundle = Bundle()
        bundle.entry = [questionnaire_entry]
        bundle.type = "transaction"

        try:
            # Create the organization
            response = requests.post(PPM.fhir_url(), json=bundle.as_json())
            logger.debug("Response: {}".format(response.status_code))
            response.raise_for_status()

            return response.json()

        except Exception as e:
            logger.exception(
                "API/Questionnaire: Delete Questionnaire error: {}".format(e),
                exc_info=True,
                extra={
                    "request": request,
                },
            )
예제 #3
0
    def questionnaire_transaction(cls, questionnaire, questionnaire_id=None):
        """
        Accepts a Questionnaire object and builds the transaction to be used
        to perform the needed operation in FHIR. Operations can be POST or PUT,
        depending on if an ID is passed. If the object does not need to be created
        or updated, the operation will return as a success with an empty response
        object.

        :param questionnaire: The Questionnaire object to be persisted
        :type questionnaire: dict
        :param questionnaire_id: The ID to use for new Questionnaire, defaults to None
        :type questionnaire_id: str, optional
        :return: The response if the resource was created, None if no operation needed
        :rtype: dict
        """
        # Check for a version matching the created one
        version = questionnaire["version"]
        query = {"identifier": f"{FHIR.qualtrics_survey_version_identifier_system}|{version}"}
        if questionnaire_id:
            query["_id"] = questionnaire_id

        questionnaires = FHIR._query_resources("Questionnaire", query)
        if questionnaires:

            # No need to recreate it
            logger.debug(f"PPM/Qualtrics: Questionnaire already exists for survey version {version}")
            return None

        # Use the FHIR client lib to validate our resource.
        questionnaire = Questionnaire(questionnaire)
        questionnaire_request = BundleEntryRequest(
            {
                "url": f"Questionnaire/{questionnaire_id}" if questionnaire_id else "Questionnaire",
                "method": "PUT" if questionnaire_id else "POST",
            }
        )
        questionnaire_entry = BundleEntry()
        questionnaire_entry.resource = questionnaire
        questionnaire_entry.request = questionnaire_request

        # Validate it.
        bundle = Bundle()
        bundle.entry = [questionnaire_entry]
        bundle.type = "transaction"

        # Create the organization
        response = requests.post(PPM.fhir_url(), json=bundle.as_json())
        logger.debug("PPM/Qualtrics: FHIR Response: {}".format(response.status_code))
        response.raise_for_status()

        return response.json()
예제 #4
0
    def _bundle(resources):

        # Build the bundle
        bundle = Bundle()
        bundle.type = 'transaction'
        bundle.entry = []

        for resource in resources:

            # Build the entry request
            bundle_entry_request = BundleEntryRequest()
            bundle_entry_request.method = 'POST'
            bundle_entry_request.url = resource.resource_type

            # Add it to the entry
            bundle_entry = BundleEntry()
            bundle_entry.resource = resource
            bundle_entry.fullUrl = resource.id
            bundle_entry.request = bundle_entry_request

            # Add it
            bundle.entry.append(bundle_entry)

        return bundle
예제 #5
0
    def patch(request, questionnaire_id, format=None):
        """
        Uses FHIR+json patch to update a resource
        """

        # Base 64 encode the patch
        parameters = Parameters(request.data)
        questionnaire_request = BundleEntryRequest(
            {
                "url": f"Questionnaire/{questionnaire_id}",
                "method": "PATCH",
            }
        )
        questionnaire_entry = BundleEntry()
        questionnaire_entry.resource = parameters
        questionnaire_entry.request = questionnaire_request

        # Validate it.
        bundle = Bundle()
        bundle.entry = [questionnaire_entry]
        bundle.type = "transaction"

        try:
            # Create the organization
            response = requests.post(PPM.fhir_url(), json=bundle.as_json())
            logger.debug("Response: {}".format(response.status_code))
            response.raise_for_status()

            return response.json()

        except Exception as e:
            logger.exception(
                "API/Questionnaire: Delete Questionnaire error: {}".format(e),
                exc_info=True,
                extra={
                    "request": request,
                },
            )
def make_bundle(value: str, sp_unique: bool = False) -> Bundle:
    system = "https://some_system"
    jd = {
        "type":
        "transaction",
        "entry": [
            BundleEntry(
                jsondict={
                    "request":
                    BundleEntryRequest(
                        jsondict={
                            "method": "POST",
                            "url": "Questionnaire",
                            "ifNoneExist": f"identifier={system}|{value}",
                        }).as_json(),
                    "resource":
                    Questionnaire(
                        jsondict={
                            "name":
                            "some_questionnaire_name",
                            "status":
                            "active",
                            "identifier": [
                                Identifier(jsondict={
                                    "system": system,
                                    "value": value
                                }).as_json()
                            ],
                        }).as_json(),
                }).as_json()
        ],
    }
    # Note: the .as_json() conversions are necessary.
    if sp_unique:
        raise NotImplementedError(
            "sp_unique method not implemented; see "
            "https://github.com/hapifhir/hapi-fhir/issues/3141")
    return Bundle(jsondict=jd)