-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from koenvo/feature/accept-pathlib
Refactor open_as_file to also accept Path inputs
- Loading branch information
Showing
5 changed files
with
84 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,55 @@ | ||
from typing import BinaryIO | ||
import base64 | ||
|
||
from kloppy.config import get_config | ||
from kloppy.exceptions import AdapterError, InputNotFoundError | ||
from .adapter import Adapter | ||
|
||
try: | ||
from js import XMLHttpRequest | ||
|
||
class HTTPAdapter(Adapter): | ||
def supports(self, url: str) -> bool: | ||
return url.startswith("http://") or url.startswith("https://") | ||
RUNS_IN_BROWSER = True | ||
except ImportError: | ||
RUNS_IN_BROWSER = False | ||
|
||
def read_to_stream(self, url: str, output: BinaryIO): | ||
basic_authentication = get_config("adapters.http.basic_authentication") | ||
|
||
def check_requests_patch(): | ||
if RUNS_IN_BROWSER: | ||
try: | ||
from js import XMLHttpRequest | ||
|
||
_RUNS_IN_BROWSER = True | ||
import pyodide_http | ||
except ImportError: | ||
try: | ||
import requests | ||
except ImportError: | ||
raise AdapterError( | ||
"Seems like you don't have requests installed. Please" | ||
" install it using: pip install requests" | ||
) | ||
|
||
_RUNS_IN_BROWSER = False | ||
|
||
if _RUNS_IN_BROWSER: | ||
xhr = XMLHttpRequest.new() | ||
xhr.responseType = "arraybuffer" | ||
if basic_authentication: | ||
authentication = base64.b64encode( | ||
basic_authentication.join(":") | ||
) | ||
xhr.setRequestHeader( | ||
"Authorization", | ||
f"Basic {authentication}", | ||
) | ||
raise AdapterError( | ||
"Seems like you don't have `pyodide-http` installed, which is required to make http requests " | ||
"work in the browser. Please install it using: pip install pyodide-http" | ||
) | ||
|
||
xhr.open("GET", url, False) | ||
xhr.send(None) | ||
pyodide_http.patch_all() | ||
|
||
# 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) | ||
class HTTPAdapter(Adapter): | ||
def supports(self, url: str) -> bool: | ||
return url.startswith("http://") or url.startswith("https://") | ||
|
||
output.write(xhr.response.to_py().tobytes()) | ||
else: | ||
auth = None | ||
if basic_authentication: | ||
auth = requests.auth.HTTPBasicAuth(*basic_authentication) | ||
def read_to_stream(self, url: str, output: BinaryIO): | ||
check_requests_patch() | ||
|
||
with requests.get(url, stream=True, auth=auth) as r: | ||
if r.status_code == 404: | ||
raise InputNotFoundError(f"Could not find {url}") | ||
basic_authentication = get_config("adapters.http.basic_authentication") | ||
|
||
r.raise_for_status() | ||
for chunk in r.iter_content(chunk_size=8192): | ||
output.write(chunk) | ||
try: | ||
import requests | ||
except ImportError: | ||
raise AdapterError( | ||
"Seems like you don't have `requests` installed. Please" | ||
" install it using: pip install requests" | ||
) | ||
|
||
auth = None | ||
if basic_authentication: | ||
auth = requests.auth.HTTPBasicAuth(*basic_authentication) | ||
|
||
with requests.get(url, stream=True, auth=auth) as r: | ||
if r.status_code == 404: | ||
raise InputNotFoundError(f"Could not find {url}") | ||
|
||
r.raise_for_status() | ||
for chunk in r.iter_content(chunk_size=8192): | ||
output.write(chunk) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters