def fetch_data(self, request, resource_type, *args, **kwargs):
        resource_router = get_resourcerouter(request.crosswalk)
        target_url = self.build_url(resource_router, resource_type, *args,
                                    **kwargs)

        logger.debug('FHIR URL with key:%s' % target_url)

        try:
            get_parameters = {
                **self.filter_parameters(request),
                **self.build_parameters(request)
            }
        except voluptuous.error.Invalid as e:
            raise exceptions.ParseError(detail=e.msg)

        logger.debug('Here is the URL to send, %s now add '
                     'GET parameters %s' % (target_url, get_parameters))

        # Now make the call to the backend API
        req = Request('GET',
                      target_url,
                      data=get_parameters,
                      params=get_parameters,
                      headers=backend_connection.headers(request,
                                                         url=target_url))
        s = Session()
        prepped = s.prepare_request(req)
        # Send signal
        pre_fetch.send_robust(self.__class__, request=req)
        r = s.send(prepped,
                   cert=backend_connection.certs(crosswalk=request.crosswalk),
                   timeout=resource_router.wait_time,
                   verify=FhirServerVerify(crosswalk=request.crosswalk))
        # Send signal
        post_fetch.send_robust(self.__class__, request=prepped, response=r)
        response = build_fhir_response(request._request,
                                       target_url,
                                       request.crosswalk,
                                       r=r,
                                       e=None)

        if response.status_code == 404:
            raise exceptions.NotFound(
                detail='The requested resource does not exist')

        # TODO: This should be more specific
        if response.status_code >= 300:
            raise UpstreamServerException(
                detail='An error occurred contacting the upstream server')

        self.validate_response(response)

        out_data = r.json()

        self.check_object_permissions(request, out_data)

        return out_data
Esempio n. 2
0
def dataserver():
    resource_router = get_resourcerouter()
    target_url = resource_router.fhir_url + "metadata"
    r = requests.get(target_url,
                     params={"_format": "json"},
                     cert=backend_connection.certs(),
                     verify=False)
    try:
        r.raise_for_status()
    except Exception:
        logger.exception("Failed to ping backend")
        return False
    return r.json()
Esempio n. 3
0
def bfd_fhir_dataserver(v2=False):
    resource_router = get_resourcerouter()
    target_url = "{}{}".format(resource_router.fhir_url, "/v2/fhir/metadata" if v2 else "/v1/fhir/metadata")
    r = requests.get(target_url,
                     params={"_format": "json"},
                     cert=backend_connection.certs(),
                     verify=False,
                     timeout=5)
    try:
        r.raise_for_status()
    except Exception:
        logger.exception("Failed to ping backend")
        return False
    return r.json()
Esempio n. 4
0
    def fetch_data(self, request, resource_type, *args, **kwargs):
        resource_router = get_resourcerouter(request.crosswalk)
        target_url = self.build_url(resource_router,
                                    resource_type,
                                    *args,
                                    **kwargs)

        logger.debug('FHIR URL with key:%s' % target_url)

        try:
            get_parameters = {**self.filter_parameters(request), **self.build_parameters(request)}
        except voluptuous.error.Invalid as e:
            raise exceptions.ParseError(detail=e.msg)

        logger.debug('Here is the URL to send, %s now add '
                     'GET parameters %s' % (target_url, get_parameters))

        # Now make the call to the backend API
        req = Request('GET',
                      target_url,
                      data=get_parameters,
                      params=get_parameters,
                      headers=backend_connection.headers(request, url=target_url))
        s = Session()
        prepped = s.prepare_request(req)
        # Send signal
        pre_fetch.send_robust(FhirDataView, request=req)
        r = s.send(
            prepped,
            cert=backend_connection.certs(crosswalk=request.crosswalk),
            timeout=resource_router.wait_time,
            verify=FhirServerVerify(crosswalk=request.crosswalk))
        # Send signal
        post_fetch.send_robust(FhirDataView, request=prepped, response=r)
        response = build_fhir_response(request._request, target_url, request.crosswalk, r=r, e=None)

        # BB2-128
        error = process_error_response(response)

        if error is not None:
            raise error

        self.validate_response(response)

        out_data = r.json()

        self.check_object_permissions(request, out_data)

        return out_data
Esempio n. 5
0
    def fetch_data(self, request, resource_type, *args, **kwargs):
        resource_router = get_resourcerouter(request.crosswalk)
        target_url = self.build_url(resource_router, resource_type, *args,
                                    **kwargs)

        logger.debug('FHIR URL with key:%s' % target_url)

        get_parameters = self.build_parameters(request)

        logger.debug('Here is the URL to send, %s now add '
                     'GET parameters %s' % (target_url, get_parameters))

        # Now make the call to the backend API
        r = requests.get(
            target_url,
            params=get_parameters,
            cert=backend_connection.certs(crosswalk=request.crosswalk),
            headers=backend_connection.headers(request, url=target_url),
            timeout=resource_router.wait_time,
            verify=FhirServerVerify(crosswalk=request.crosswalk))
        response = build_fhir_response(request._request,
                                       target_url,
                                       request.crosswalk,
                                       r=r,
                                       e=None)

        if response.status_code == 404:
            raise exceptions.NotFound(
                detail='The requested resource does not exist')

        # TODO: This should be more specific
        if response.status_code >= 300:
            raise UpstreamServerException(
                detail='An error occurred contacting the upstream server')

        self.validate_response(response)

        out_data = localize(request=request,
                            response=response,
                            crosswalk=request.crosswalk,
                            resource_type=resource_type)

        self.check_object_permissions(request, out_data)

        return out_data