Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow urls to end in slash #78

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions fhirpy/base/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import aiohttp
import requests
import urllib

from yarl import URL
from fhirpy.base.searchset import AbstractSearchSet
Expand Down Expand Up @@ -78,7 +79,13 @@ def _build_request_headers(self):

def _build_request_url(self, path, params):
if URL(path).is_absolute():
if self.url in path:
if urllib.parse.urlparse(self.url).port:
parsed = urllib.parse.urlparse(path)
if parsed.port is None and parsed.scheme == "https":
path = f'{parsed.scheme}://{parsed.netloc}:443{parsed.path}?{parsed.query}'
if parsed.fragment != "":
path += f'#{parsed.fragment}'
if self.url.rstrip("/") in path.rstrip("/"):
return path
else:
raise ValueError(
Expand All @@ -87,7 +94,7 @@ def _build_request_url(self, path, params):
)

params = params or {}
return f'{self.url}/{path.lstrip("/")}?{encode_params(params)}'
return f'{self.url.rstrip("/")}/{path.lstrip("/")}?{encode_params(params)}'


class AsyncClient(AbstractClient, ABC):
Expand All @@ -98,7 +105,7 @@ 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
method, url, json=data, headers=headers
) as r:
if 200 <= r.status < 300:
data = await r.text()
Expand Down Expand Up @@ -323,7 +330,7 @@ def is_valid(self, raise_exception=False):
data=self.serialize()
)
if any(
issue['severity'] in ['fatal', 'error'] for issue in data['issue']
issue['severity'] in ['fatal', 'error'] for issue in data['issue']
):
if raise_exception:
raise OperationOutcome(data)
Expand Down Expand Up @@ -381,7 +388,7 @@ async def is_valid(self, raise_exception=False):
data=self.serialize()
)
if any(
issue['severity'] in ['fatal', 'error'] for issue in data['issue']
issue['severity'] in ['fatal', 'error'] for issue in data['issue']
):
if raise_exception:
raise OperationOutcome(data)
Expand All @@ -403,7 +410,7 @@ def to_resource(self):
if not self.is_local:
raise ResourceNotFound('Can not resolve not local resource')
return self.client.resources(self.resource_type).search(_id=self.id
).get()
).get()

def execute(self, operation, method='post', **kwargs):
if not self.is_local:
Expand All @@ -424,7 +431,7 @@ async def to_resource(self):
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()
).search(_id=self.id).get()

async def execute(self, operation, method='post', **kwargs):
if not self.is_local:
Expand Down