diff --git a/tiled/client/auth.py b/tiled/client/auth.py index bc39bc3d4..d61c0baa8 100644 --- a/tiled/client/auth.py +++ b/tiled/client/auth.py @@ -2,7 +2,6 @@ from pathlib import Path import httpx -from starlette.status import HTTP_401_UNAUTHORIZED from .utils import SerializableLock, handle_error @@ -92,7 +91,7 @@ def sync_auth_flow(self, request, attempt=0): if access_token is not None: request.headers["Authorization"] = f"Bearer {access_token}" response = yield request - if (access_token is None) or (response.status_code == HTTP_401_UNAUTHORIZED): + if (access_token is None) or (response.status_code == httpx.codes.UNAUTHORIZED): # Maybe the token cached in memory is stale. maybe_new_access_token = self.sync_get_token( "access_token", reload_from_disk=True @@ -114,7 +113,7 @@ def sync_auth_flow(self, request, attempt=0): self.refresh_url, refresh_token, self.csrf_token ) token_response = yield token_request - if token_response.status_code == HTTP_401_UNAUTHORIZED: + if token_response.status_code == httpx.codes.UNAUTHORIZED: # Refreshing the token failed. # Discard the expired (or otherwise invalid) refresh_token. self.sync_clear_token("refresh_token") diff --git a/tiled/client/cache_control.py b/tiled/client/cache_control.py index ab1ffb816..c08f4fdde 100644 --- a/tiled/client/cache_control.py +++ b/tiled/client/cache_control.py @@ -9,13 +9,6 @@ import attr import httpx -from starlette.status import ( - HTTP_200_OK, - HTTP_203_NON_AUTHORITATIVE_INFORMATION, - HTTP_300_MULTIPLE_CHOICES, - HTTP_301_MOVED_PERMANENTLY, - HTTP_308_PERMANENT_REDIRECT, -) logger = logging.getLogger(__name__) @@ -121,11 +114,11 @@ def __init__( *, cacheable_methods: tp.Tuple[str, ...] = ("GET",), cacheable_status_codes: tp.Tuple[int, ...] = ( - HTTP_200_OK, - HTTP_203_NON_AUTHORITATIVE_INFORMATION, - HTTP_300_MULTIPLE_CHOICES, - HTTP_301_MOVED_PERMANENTLY, - HTTP_308_PERMANENT_REDIRECT, + httpx.codes.OK, + httpx.codes.NON_AUTHORITATIVE_INFORMATION, + httpx.codes.MULTIPLE_CHOICES, + httpx.codes.MOVED_PERMANENTLY, + httpx.codes.PERMANENT_REDIRECT, ), always_cache: bool = False, ) -> None: diff --git a/tiled/client/constructors.py b/tiled/client/constructors.py index 71eb0ee24..8b2bc0104 100644 --- a/tiled/client/constructors.py +++ b/tiled/client/constructors.py @@ -2,7 +2,6 @@ import collections.abc import httpx -from starlette.status import HTTP_401_UNAUTHORIZED from ..utils import import_object, prepend_to_sys_path from .container import DEFAULT_STRUCTURE_CLIENT_DISPATCH, Container @@ -146,7 +145,7 @@ def from_context( ).json() except ClientError as err: if ( - (err.response.status_code == HTTP_401_UNAUTHORIZED) + (err.response.status_code == httpx.codes.UNAUTHORIZED) and (context.api_key is None) and (context.http_client.auth is None) ): diff --git a/tiled/client/container.py b/tiled/client/container.py index ff077caf7..2a42b36e3 100644 --- a/tiled/client/container.py +++ b/tiled/client/container.py @@ -7,7 +7,7 @@ from dataclasses import asdict import entrypoints -from starlette.status import HTTP_404_NOT_FOUND +import httpx from ..adapters.utils import IndexersMixin from ..iterviews import ItemsView, KeysView, ValuesView @@ -319,7 +319,7 @@ def __getitem__(self, keys, _ignore_inlined_contents=False): ) ).json() except ClientError as err: - if err.response.status_code == HTTP_404_NOT_FOUND: + if err.response.status_code == httpx.codes.NOT_FOUND: # If this is a scalar lookup, raise KeyError("X") not KeyError(("X",)). err_arg = keys[i:] if len(err_arg) == 1: diff --git a/tiled/client/context.py b/tiled/client/context.py index 68c9b6422..e907efee0 100644 --- a/tiled/client/context.py +++ b/tiled/client/context.py @@ -10,7 +10,6 @@ import appdirs import httpx -from starlette.status import HTTP_400_BAD_REQUEST, HTTP_401_UNAUTHORIZED from .._version import __version__ as tiled_version from ..utils import UNSET, DictView @@ -554,7 +553,7 @@ def authenticate( }, auth=None, ) - if (access_response.status_code == HTTP_400_BAD_REQUEST) and ( + if (access_response.status_code == httpx.codes.BAD_REQUEST) and ( access_response.json()["detail"]["error"] == "authorization_pending" ): print(".", end="", flush=True) @@ -641,7 +640,7 @@ def force_auth_refresh(self): csrf_token, ) token_response = self.http_client.send(refresh_request, auth=None) - if token_response.status_code == HTTP_401_UNAUTHORIZED: + if token_response.status_code == httpx.codes.UNAUTHORIZED: raise CannotRefreshAuthentication( "Session cannot be refreshed. Log in again." ) diff --git a/tiled/client/dataframe.py b/tiled/client/dataframe.py index cc689ee1b..c3fc1bc76 100644 --- a/tiled/client/dataframe.py +++ b/tiled/client/dataframe.py @@ -1,6 +1,6 @@ import dask import dask.dataframe.core -from starlette.status import HTTP_404_NOT_FOUND +import httpx from ..serialization.table import deserialize_arrow, serialize_arrow from ..utils import APACHE_ARROW_FILE_MIME_TYPE, UNCHANGED @@ -192,7 +192,7 @@ def __getitem__(self, column): ) ).json() except ClientError as err: - if err.response.status_code == HTTP_404_NOT_FOUND: + if err.response.status_code == httpx.codes.NOT_FOUND: raise KeyError(column) raise item = content["data"] diff --git a/tiled/client/transport.py b/tiled/client/transport.py index 48fc3ea3f..07f2405b6 100644 --- a/tiled/client/transport.py +++ b/tiled/client/transport.py @@ -5,14 +5,6 @@ import typing as tp import httpx -from starlette.status import ( - HTTP_200_OK, - HTTP_203_NON_AUTHORITATIVE_INFORMATION, - HTTP_300_MULTIPLE_CHOICES, - HTTP_301_MOVED_PERMANENTLY, - HTTP_304_NOT_MODIFIED, - HTTP_308_PERMANENT_REDIRECT, -) from .cache import Cache from .cache_control import ByteStreamWrapper, CacheControl @@ -40,11 +32,11 @@ def __init__( cache: tp.Optional[Cache] = None, cacheable_methods: tp.Tuple[str, ...] = ("GET",), cacheable_status_codes: tp.Tuple[int, ...] = ( - HTTP_200_OK, - HTTP_203_NON_AUTHORITATIVE_INFORMATION, - HTTP_300_MULTIPLE_CHOICES, - HTTP_301_MOVED_PERMANENTLY, - HTTP_308_PERMANENT_REDIRECT, + httpx.codes.OK, + httpx.codes.NON_AUTHORITATIVE_INFORMATION, + httpx.codes.MULTIPLE_CHOICES, + httpx.codes.MOVED_PERMANENTLY, + httpx.codes.PERMANENT_REDIRECT, ), always_cache: bool = False, ): @@ -105,7 +97,7 @@ def handle_request(self, request: httpx.Request) -> httpx.Response: # But, below _collect_ the response with the content in it. if self.cache is not None: - if response.status_code == HTTP_304_NOT_MODIFIED: + if response.status_code == httpx.codes.NOT_MODIFIED: if __debug__: logger.debug( "Server validated as fresh cached entry for: %s", request diff --git a/tiled/client/utils.py b/tiled/client/utils.py index 2557ac6cf..8b280a3cc 100644 --- a/tiled/client/utils.py +++ b/tiled/client/utils.py @@ -7,7 +7,6 @@ import httpx import msgpack -from starlette.status import HTTP_500_INTERNAL_SERVER_ERROR from ..utils import path_from_uri @@ -70,7 +69,7 @@ def handle_error(response): except httpx.RequestError: raise # Nothing to add in this case; just raise it. except httpx.HTTPStatusError as exc: - if response.status_code < HTTP_500_INTERNAL_SERVER_ERROR: + if response.status_code < httpx.codes.INTERNAL_SERVER_ERROR: # Include more detail that httpx does by default. if response.headers["Content-Type"] == "application/json": detail = response.json().get("detail", "")