async def execute(self, operation, method="post", **kwargs): if not self.is_local: raise ResourceNotFound("Can not execute on not local resource") return await self.client._do_request( method, "{0}/{1}/{2}".format(self.resource_type, self.id, operation), **kwargs, )
async def to_resource(self): """ Returns Resource instance for this reference from fhir server otherwise. """ if not self.is_local: raise ResourceNotFound('Can not resolve not local resource') return await self.client.resources(self.resource_type ).search(id=self.id).get()
def to_reference(self, **kwargs): """ Returns Reference instance for this resource """ if not self.reference: raise ResourceNotFound( 'Can not get reference to unsaved resource without id') return self.client.reference(reference=self.reference, **kwargs)
async def _do_request(self, method, path, data=None, params=None): headers = self._build_request_headers() url = self._build_request_url(path, params) async with aiohttp.request(method, url, json=data, headers=headers) as r: if 200 <= r.status < 300: data = await r.text() return json.loads(data, object_hook=AttrDict) if r.status == 404 or r.status == 410: raise ResourceNotFound(await r.text()) raise OperationOutcome(await r.text())
def _do_request(self, method, path, data=None, params=None): headers = self._build_request_headers() url = self._build_request_url(path, params) r = requests.request(method, url, json=data, headers=headers) if 200 <= r.status_code < 300: return json.loads(r.content.decode(), object_hook=AttrDict) if r.content else None if r.status_code == 404 or r.status_code == 410: raise ResourceNotFound(r.content.decode()) raise OperationOutcome(r.content.decode())
async def get(self, id=None): searchset = self.limit(2) if id: warnings.warn( "parameter 'id' of method get() is deprecated " "and will be removed in future versions. " "Please use 'search(id='...').get()'", DeprecationWarning, stacklevel=2) searchset = searchset.search(_id=id) res_data = await searchset.fetch() if len(res_data) == 0: raise ResourceNotFound('No resources found') elif len(res_data) > 1: raise MultipleResourcesFound('More than one resource found') resource = res_data[0] return self._perform_resource(resource)
def _do_request(self, method, path, data=None, params=None): headers = self._build_request_headers() url = self._build_request_url(path, params) r = requests.request(method, url, json=data, headers=headers) if 200 <= r.status_code < 300: return (json.loads(r.content.decode(), object_hook=AttrDict) if r.content else None) if r.status_code == 404 or r.status_code == 410: raise ResourceNotFound(r.content.decode()) data = r.content.decode() try: parsed_data = json.loads(data) if parsed_data["resourceType"] == "OperationOutcome": raise OperationOutcome(resource=parsed_data) raise OperationOutcome(reason=data) except (KeyError, JSONDecodeError): raise OperationOutcome(reason=data)
async def _do_request(self, method, path, data=None, params=None): headers = self._build_request_headers() url = self._build_request_url(path, params) async with aiohttp.request(method, url, json=data, headers=headers) as r: if 200 <= r.status < 300: data = await r.text() return json.loads(data, object_hook=AttrDict) if r.status == 404 or r.status == 410: raise ResourceNotFound(await r.text()) data = await r.text() try: parsed_data = json.loads(data) if parsed_data["resourceType"] == "OperationOutcome": raise OperationOutcome(resource=parsed_data) raise OperationOutcome(reason=data) except (KeyError, JSONDecodeError): raise OperationOutcome(reason=data)
def _do_request(self, method, path, data=None, params=None): headers = self._build_request_headers() url = self._build_request_url(path, params) r = requests.request(method, url, json=data, headers=headers) if 200 <= r.status_code < 300: if r.content: return json.loads(r.content.decode(), object_hook=AttrDict) else: if data is None: return None location = r.headers.get('Location', '') paths = location.split( '{}/'.format(data.get('resourceType', 'Unk')), 1) id = 'unknown' version = 1 if len(paths) > 1: array = paths[1].split('/') id = array[0] version = array[-1] last_updated = r.headers.get('Last-Modified') if last_updated is not None: last_updated = datetime.strptime( last_updated, "%a, %d %b %Y %H:%M:%S %Z").strftime( '%Y-%m-%d %H:%M:%S.%fZ') return { 'id': id, 'meta': { 'version': version, 'lastUpdated': last_updated } } if r.status_code == 404 or r.status_code == 410: raise ResourceNotFound(r.content.decode()) raise OperationOutcome(r.content.decode())
def execute(self, operation, method='post', **kwargs): if not self.is_local: raise ResourceNotFound('Can not execute on not local resource') return self.client._do_request( method, '{0}/{1}/{2}'.format(self.resource_type, self.id, operation), **kwargs)