Skip to content

Commit

Permalink
Support overriding protocol in ResourceURL
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Collins committed Jul 29, 2020
1 parent e51b2e0 commit baf2c1b
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/labthings/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,21 @@ class ResourceURL(UserString):
Behaves as a Python string.
"""

def __init__(self, path: str, external: bool = True):
def __init__(self, path: str, external: bool = True, protocol: str = ""):
self.path = path
self.external = external
# Strip :// if the user mistakenly included it in the argument
self.protocol = protocol.rstrip("://")
UserString.__init__(self, path)

@property
def data(self):
if self.external and has_request_context():
prefix = request.host_url.rstrip("/")
# Optional protocol override
if self.protocol:
# Strip old protocol and replace with custom protocol
prefix = self.protocol + "://" + prefix.split("://")[1]
else:
prefix = ""
return prefix + self.path
Expand Down

0 comments on commit baf2c1b

Please sign in to comment.