Skip to content

Commit

Permalink
feat: remove direct dependency on requests
Browse files Browse the repository at this point in the history
  • Loading branch information
judahrand committed Aug 15, 2023
1 parent 0f0f210 commit 45c29c8
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 201 deletions.
2 changes: 1 addition & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Expand Up @@ -55,7 +55,6 @@ dependencies = [
"python-dateutil",
"python-multipart",
"python-json-logger",
"requests",
"rich>=11.2.0",
"schema",
"simple-di>=0.1.4",
Expand Down
89 changes: 46 additions & 43 deletions src/bentoml/_internal/cloud/bentocloud.py
Expand Up @@ -11,7 +11,7 @@
from tempfile import NamedTemporaryFile

import fs
import requests
import httpx
from rich.live import Live
from simple_di import Provide
from simple_di import inject
Expand Down Expand Up @@ -265,7 +265,7 @@ def io_cb(x: int):
)
try:
if presigned_upload_url is not None:
resp = requests.put(presigned_upload_url, data=wrapped_file)
resp = httpx.put(presigned_upload_url, data=wrapped_file)
if resp.status_code != 200:
finish_req = FinishUploadBentoSchema(
status=BentoUploadStatus.FAILED,
Expand Down Expand Up @@ -324,7 +324,7 @@ def chunk_upload(
io_cb, chunk_io, "read"
)

resp = requests.put(
resp = httpx.put(
remote_bento.presigned_upload_url, data=wrapped_file
)
if resp.status_code != 200:
Expand Down Expand Up @@ -513,27 +513,28 @@ def pull_model(model_tag: Tag):
name, version
)
presigned_download_url = remote_bento.presigned_download_url
response = requests.get(presigned_download_url, stream=True)

if response.status_code != 200:
raise BentoMLException(
f'Failed to download bento "{_tag}": {response.text}'
)
total_size_in_bytes = int(response.headers.get("content-length", 0))
block_size = 1024 # 1 Kibibyte
with NamedTemporaryFile() as tar_file:
self.transmission_progress.update(
download_task_id,
completed=0,
total=total_size_in_bytes,
visible=True,
)
self.transmission_progress.start_task(download_task_id)
for data in response.iter_content(block_size):
self.transmission_progress.update(
download_task_id, advance=len(data)
)
tar_file.write(data)
with httpx.stream("GET", presigned_download_url) as response:
if response.status_code != 200:
raise BentoMLException(
f'Failed to download bento "{_tag}": {response.text}'
)
total_size_in_bytes = int(response.headers.get("content-length", 0))
block_size = 1024 # 1 Kibibyte
with NamedTemporaryFile() as tar_file:
self.transmission_progress.update(
download_task_id,
completed=0,
total=total_size_in_bytes,
visible=True,
)
self.transmission_progress.start_task(download_task_id)
for data in response.iter_bytes(block_size):
self.transmission_progress.update(
download_task_id, advance=len(data)
)
tar_file.write(data)

self.log_progress.add_task(
f'[bold green]Finished downloading all bento "{_tag}" files'
)
Expand Down Expand Up @@ -706,7 +707,7 @@ def io_cb(x: int):
)
try:
if presigned_upload_url is not None:
resp = requests.put(presigned_upload_url, data=wrapped_file)
resp = httpx.put(presigned_upload_url, data=wrapped_file)
if resp.status_code != 200:
finish_req = FinishUploadModelSchema(
status=ModelUploadStatus.FAILED,
Expand Down Expand Up @@ -768,7 +769,7 @@ def chunk_upload(
"read",
)

resp = requests.put(
resp = httpx.put(
remote_model.presigned_upload_url, data=wrapped_file
)
if resp.status_code != 200:
Expand Down Expand Up @@ -959,25 +960,27 @@ def _do_pull_model(
)
presigned_download_url = remote_model.presigned_download_url

response = requests.get(presigned_download_url, stream=True)
if response.status_code != 200:
raise BentoMLException(
f'Failed to download model "{_tag}": {response.text}'
)
with httpx.stream("GET", presigned_download_url) as response:
if response.status_code != 200:
raise BentoMLException(
f'Failed to download model "{_tag}": {response.text}'
)
total_size_in_bytes = int(response.headers.get("content-length", 0))
block_size = 1024 # 1 Kibibyte
with NamedTemporaryFile() as tar_file:
self.transmission_progress.update(
download_task_id,
description=f'Downloading model "{_tag}"',
total=total_size_in_bytes,
visible=True,
)
self.transmission_progress.start_task(download_task_id)
for data in response.iter_bytes(block_size):
self.transmission_progress.update(
download_task_id, advance=len(data)
)
tar_file.write(data)

total_size_in_bytes = int(response.headers.get("content-length", 0))
block_size = 1024 # 1 Kibibyte
with NamedTemporaryFile() as tar_file:
self.transmission_progress.update(
download_task_id,
description=f'Downloading model "{_tag}"',
total=total_size_in_bytes,
visible=True,
)
self.transmission_progress.start_task(download_task_id)
for data in response.iter_content(block_size):
self.transmission_progress.update(download_task_id, advance=len(data))
tar_file.write(data)
self.log_progress.add_task(
f'[bold green]Finished downloading model "{_tag}" files'
)
Expand Down
24 changes: 12 additions & 12 deletions src/bentoml/_internal/cloud/client.py
Expand Up @@ -4,7 +4,7 @@
import logging
from urllib.parse import urljoin

import requests
import httpx

from ...exceptions import CloudRESTApiClientError
from ..configuration import BENTOML_VERSION
Expand Down Expand Up @@ -41,7 +41,7 @@
class RestApiClient:
def __init__(self, endpoint: str, api_token: str) -> None:
self.endpoint = endpoint
self.session = requests.Session()
self.session = httpx.Client()
self.session.headers.update(
{
"X-YATAI-API-TOKEN": api_token,
Expand All @@ -50,15 +50,15 @@ def __init__(self, endpoint: str, api_token: str) -> None:
}
)

def _is_not_found(self, resp: requests.Response) -> bool:
def _is_not_found(self, resp: httpx.Response) -> bool:
# We used to return 400 for record not found, handle both cases
return (
resp.status_code == 404
or resp.status_code == 400
and "record not found" in resp.text
)

def _check_resp(self, resp: requests.Response) -> None:
def _check_resp(self, resp: httpx.Response) -> None:
if resp.status_code != 200:
raise CloudRESTApiClientError(
f"request failed with status code {resp.status_code}: {resp.text}"
Expand Down Expand Up @@ -234,14 +234,14 @@ def upload_bento(

def download_bento(
self, bento_repository_name: str, version: str
) -> requests.Response:
) -> httpx.Response:
url = urljoin(
self.endpoint,
f"/api/v1/bento_repositories/{bento_repository_name}/bentos/{version}/download",
)
resp = self.session.get(url, stream=True)
self._check_resp(resp)
return resp
with self.session.stream("GET", url) as resp:
self._check_resp(resp)
return resp

def get_model_repository(
self, model_repository_name: str
Expand Down Expand Up @@ -386,14 +386,14 @@ def upload_model(

def download_model(
self, model_repository_name: str, version: str
) -> requests.Response:
) -> httpx.Response:
url = urljoin(
self.endpoint,
f"/api/v1/model_repositories/{model_repository_name}/models/{version}/download",
)
resp = self.session.get(url, stream=True)
self._check_resp(resp)
return resp
with self.session.stream("GET", url) as resp:
self._check_resp(resp)
return resp

def get_bento_repositories_list(
self, bento_repository_name: str
Expand Down
90 changes: 45 additions & 45 deletions src/bentoml/_internal/cloud/yatai.py
Expand Up @@ -11,7 +11,7 @@
from tempfile import NamedTemporaryFile

import fs
import requests
import httpx
from rich.live import Live
from simple_di import Provide
from simple_di import inject
Expand Down Expand Up @@ -257,7 +257,7 @@ def io_cb(x: int):
)
try:
if presigned_upload_url is not None:
resp = requests.put(presigned_upload_url, data=wrapped_file)
resp = httpx.put(presigned_upload_url, data=wrapped_file)
if resp.status_code != 200:
finish_req = FinishUploadBentoSchema(
status=BentoUploadStatus.FAILED,
Expand Down Expand Up @@ -316,7 +316,7 @@ def chunk_upload(
io_cb, chunk_io, "read"
)

resp = requests.put(
resp = httpx.put(
remote_bento.presigned_upload_url, data=wrapped_file
)
if resp.status_code != 200:
Expand Down Expand Up @@ -505,27 +505,26 @@ def pull_model(model_tag: Tag):
name, version
)
presigned_download_url = remote_bento.presigned_download_url
response = requests.get(presigned_download_url, stream=True)

if response.status_code != 200:
raise BentoMLException(
f'Failed to download bento "{_tag}": {response.text}'
)
total_size_in_bytes = int(response.headers.get("content-length", 0))
block_size = 1024 # 1 Kibibyte
with NamedTemporaryFile() as tar_file:
self.transmission_progress.update(
download_task_id,
completed=0,
total=total_size_in_bytes,
visible=True,
)
self.transmission_progress.start_task(download_task_id)
for data in response.iter_content(block_size):
self.transmission_progress.update(
download_task_id, advance=len(data)
)
tar_file.write(data)
with httpx.stream("GET", presigned_download_url) as response:
if response.status_code != 200:
raise BentoMLException(
f'Failed to download bento "{_tag}": {response.text}'
)
total_size_in_bytes = int(response.headers.get("content-length", 0))
block_size = 1024 # 1 Kibibyte
with NamedTemporaryFile() as tar_file:
self.transmission_progress.update(
download_task_id,
completed=0,
total=total_size_in_bytes,
visible=True,
)
self.transmission_progress.start_task(download_task_id)
for data in response.iter_bytes(block_size):
self.transmission_progress.update(
download_task_id, advance=len(data)
)
tar_file.write(data)
self.log_progress.add_task(
f'[bold green]Finished downloading all bento "{_tag}" files'
)
Expand Down Expand Up @@ -698,7 +697,7 @@ def io_cb(x: int):
)
try:
if presigned_upload_url is not None:
resp = requests.put(presigned_upload_url, data=wrapped_file)
resp = httpx.put(presigned_upload_url, data=wrapped_file)
if resp.status_code != 200:
finish_req = FinishUploadModelSchema(
status=ModelUploadStatus.FAILED,
Expand Down Expand Up @@ -760,7 +759,7 @@ def chunk_upload(
"read",
)

resp = requests.put(
resp = httpx.put(
remote_model.presigned_upload_url, data=wrapped_file
)
if resp.status_code != 200:
Expand Down Expand Up @@ -951,25 +950,26 @@ def _do_pull_model(
)
presigned_download_url = remote_model.presigned_download_url

response = requests.get(presigned_download_url, stream=True)
if response.status_code != 200:
raise BentoMLException(
f'Failed to download model "{_tag}": {response.text}'
)

total_size_in_bytes = int(response.headers.get("content-length", 0))
block_size = 1024 # 1 Kibibyte
with NamedTemporaryFile() as tar_file:
self.transmission_progress.update(
download_task_id,
description=f'Downloading model "{_tag}"',
total=total_size_in_bytes,
visible=True,
)
self.transmission_progress.start_task(download_task_id)
for data in response.iter_content(block_size):
self.transmission_progress.update(download_task_id, advance=len(data))
tar_file.write(data)
with httpx.stream("GET", presigned_download_url) as response:
if response.status_code != 200:
raise BentoMLException(
f'Failed to download model "{_tag}": {response.text}'
)
total_size_in_bytes = int(response.headers.get("content-length", 0))
block_size = 1024 # 1 Kibibyte
with NamedTemporaryFile() as tar_file:
self.transmission_progress.update(
download_task_id,
description=f'Downloading model "{_tag}"',
total=total_size_in_bytes,
visible=True,
)
self.transmission_progress.start_task(download_task_id)
for data in response.iter_bytes(block_size):
self.transmission_progress.update(
download_task_id, advance=len(data)
)
tar_file.write(data)
self.log_progress.add_task(
f'[bold green]Finished downloading model "{_tag}" files'
)
Expand Down
4 changes: 3 additions & 1 deletion src/bentoml/_internal/ray/__init__.py
Expand Up @@ -3,7 +3,6 @@
import typing as t
from functools import partial

import requests
from ray.serve._private.http_util import ASGIHTTPSender

import bentoml
Expand All @@ -23,6 +22,9 @@
"""'ray[serve]' is required in order to use module 'bentoml.ray', install with 'pip install -U "ray[serve]"'. See https://docs.ray.io/ for more information.""",
)

if t.TYPE_CHECKING:
import requests


def _get_runner_deployment(
svc: bentoml.Service,
Expand Down

0 comments on commit 45c29c8

Please sign in to comment.