Skip to content

Commit

Permalink
On client side, use client library for HTTP status code constants.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielballan committed Apr 10, 2024
1 parent 9a90328 commit 95f5cad
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 40 deletions.
5 changes: 2 additions & 3 deletions tiled/client/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from pathlib import Path

import httpx
from starlette.status import HTTP_401_UNAUTHORIZED

from .utils import SerializableLock, handle_error

Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down
17 changes: 5 additions & 12 deletions tiled/client/cache_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions tiled/client/constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
):
Expand Down
4 changes: 2 additions & 2 deletions tiled/client/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions tiled/client/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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."
)
Expand Down
4 changes: 2 additions & 2 deletions tiled/client/dataframe.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"]
Expand Down
20 changes: 6 additions & 14 deletions tiled/client/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
):
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions tiled/client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import httpx
import msgpack
from starlette.status import HTTP_500_INTERNAL_SERVER_ERROR

from ..utils import path_from_uri

Expand Down Expand Up @@ -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", "")
Expand Down

0 comments on commit 95f5cad

Please sign in to comment.