Skip to content

Commit

Permalink
Support raw requests WIP #17
Browse files Browse the repository at this point in the history
  • Loading branch information
ir4y committed Nov 17, 2019
1 parent 241033f commit 65bc248
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions fhirpy/base/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from abc import ABC, abstractmethod
from collections import defaultdict
from .utils import (
encode_params, convert_values, get_by_path, parse_path, chunks
AttrDict, encode_params, convert_values, get_by_path, parse_path, chunks
)
from .exceptions import (ResourceNotFound, OperationOutcome, InvalidResponse)

Expand Down Expand Up @@ -117,7 +117,8 @@ async def _do_request(self, method, path, data=None, params=None):
method, url, json=data, headers=headers
) as r:
if 200 <= r.status < 300:
return await r.json()
data = await r.text()
return json.loads(data, object_hook=AttrDict)

if r.status == 404 or r.status == 410:
raise ResourceNotFound(await r.text())
Expand All @@ -142,7 +143,7 @@ def _do_request(self, method, path, data=None, params=None):
r = requests.request(method, url, json=data, headers=headers)

if 200 <= r.status_code < 300:
return json.loads(r.content.decode()) if r.content else None
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())
Expand Down Expand Up @@ -176,6 +177,10 @@ def _perform_resource(self, data, skip_caching):
def fetch(self, *, skip_caching=False):
pass

@abstractmethod
def fetch_raw(self, *, skip_caching=False):
pass

@abstractmethod
def fetch_all(self, *, skip_caching=False):
pass
Expand Down Expand Up @@ -308,6 +313,19 @@ def fetch(self, *, skip_caching=False):

return resources

def fetch_raw(self, *, skip_caching=False):
data = self.client._fetch_resource(
self.resource_type, self.params
)
data_resource_type = data.get('resourceType', None)

if data_resource_type == 'Bundle':
for item in data['entry']:
item.resource = self._perform_resource(
item.resource, skip_caching)

return data

def fetch_all(self, *, skip_caching=False):
page = 1
resources = []
Expand Down Expand Up @@ -388,6 +406,19 @@ async def fetch(self, *, skip_caching=False):

return resources

async def fetch_raw(self, *, skip_caching=False):
data = await self.client._fetch_resource(
self.resource_type, self.params
)
data_resource_type = data.get('resourceType', None)

if data_resource_type == 'Bundle':
for item in data['entry']:
item.resource = self._perform_resource(
item.resource, skip_caching)

return data

async def fetch_all(self, *, skip_caching=False):
page = 1
resources = []
Expand Down

0 comments on commit 65bc248

Please sign in to comment.