Skip to content

Commit

Permalink
Merge pull request #152 from koenvo/bugfix/retrieve-in-browser
Browse files Browse the repository at this point in the history
Retrieve data in browser as arraybuffer
  • Loading branch information
koenvo authored Aug 26, 2022
2 parents b017c84 + 16d2784 commit 09da211
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions kloppy/infra/io/adapters/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,32 @@ def read_to_stream(self, url: str, output: BinaryIO):
_RUNS_IN_BROWSER = False

if _RUNS_IN_BROWSER:
request = XMLHttpRequest.new()
xhr = XMLHttpRequest.new()
xhr.responseType = "arraybuffer"
if basic_authentication:
authentication = base64.b64encode(
basic_authentication.join(":")
)
request.setRequestHeader(
xhr.setRequestHeader(
"Authorization",
f"Basic {authentication}",
)

request.open("GET", url, False)
request.send(None)
output.write(request.responseText)
xhr.open("GET", url, False)
xhr.send(None)

# Borrowed from 'raise_for_status'
http_error_msg = ""
if 400 <= xhr.status < 500:
http_error_msg = f"{xhr.status} Client Error: url: {url}"

elif 500 <= xhr.status < 600:
http_error_msg = f"{xhr.status} Server Error: url: {url}"

if http_error_msg:
raise AdapterError(http_error_msg)

output.write(xhr.response.to_py().tobytes())
else:
auth = None
if basic_authentication:
Expand Down

0 comments on commit 09da211

Please sign in to comment.