diff --git a/slowapi/extension.py b/slowapi/extension.py index 78bad1b..3214624 100644 --- a/slowapi/extension.py +++ b/slowapi/extension.py @@ -237,7 +237,7 @@ class Limiter: C.HEADERS_ENABLED, False ) self._storage_options.update(self.get_app_config(C.STORAGE_OPTIONS, {})) - self._storage: Union[Storage, AsyncStorage] = storage_from_string( + self._storage = storage_from_string( self._storage_uri or self.get_app_config(C.STORAGE_URL, "memory://"), **self._storage_options, ) @@ -334,7 +334,11 @@ class Limiter: """ Place holder until we find a better way to load config from app """ - return self.app_config(key, default=default_value, cast=type(default_value)) + return ( + self.app_config(key, default=default_value, cast=type(default_value)) + if default_value + else self.app_config(key, default=default_value) + ) def __should_check_backend(self) -> bool: if self.__check_backend_count > MAX_BACKEND_CHECKS: diff --git a/slowapi/util.py b/slowapi/util.py index be6064b..8a5bdfc 100644 --- a/slowapi/util.py +++ b/slowapi/util.py @@ -1,3 +1,5 @@ +from typing import Optional + from starlette.requests import Request @@ -11,11 +13,17 @@ def get_ipaddr(request: Request) -> str: if "X_FORWARDED_FOR" in request.headers: return request.headers["X_FORWARDED_FOR"] else: - return request.client.host or "127.0.0.1" + if not request.client or not request.client.host: + return "127.0.0.1" + + return request.client.host def get_remote_address(request: Request) -> str: """ Returns the ip address for the current request (or 127.0.0.1 if none found) """ - return request.client.host or "127.0.0.1" + if not request.client or not request.client.host: + return "127.0.0.1" + + return request.client.host