Skip to content

Commit cfb81fa

Browse files
committed
fix: Too much data for declared Content-Length error when endpoint caches enabled (#92)
* Fix exception with cached responses * Lint / format
1 parent 32a89af commit cfb81fa

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

src/environments.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from functools import lru_cache
55

66
import httpx
7-
from fastapi.responses import ORJSONResponse
87
from flag_engine.engine import (
98
get_environment_feature_state,
109
get_environment_feature_states,
@@ -15,7 +14,7 @@
1514
from orjson import orjson
1615

1716
from src.cache import BaseEnvironmentsCache, LocalMemEnvironmentsCache
18-
from src.exceptions import FlagsmithUnknownKeyError
17+
from src.exceptions import FeatureNotFoundError, FlagsmithUnknownKeyError
1918
from src.feature_utils import filter_out_server_key_only_feature_states
2019
from src.mappers import (
2120
map_feature_state_to_response_data,
@@ -73,7 +72,7 @@ async def refresh_environment_caches(self):
7372

7473
def get_flags_response_data(
7574
self, environment_key: str, feature: str = None
76-
) -> ORJSONResponse:
75+
) -> dict[str, typing.Any]:
7776
environment_document = self.get_environment(environment_key)
7877
environment = build_environment_model(environment_document)
7978

@@ -84,13 +83,7 @@ def get_flags_response_data(
8483
feature_states=[feature_state],
8584
environment=environment,
8685
):
87-
return ORJSONResponse(
88-
status_code=404,
89-
content={
90-
"status": "not_found",
91-
"message": f"feature '{feature}' not found",
92-
},
93-
)
86+
raise FeatureNotFoundError()
9487

9588
data = map_feature_state_to_response_data(feature_state)
9689

@@ -101,11 +94,11 @@ def get_flags_response_data(
10194
)
10295
data = map_feature_states_to_response_data(feature_states)
10396

104-
return ORJSONResponse(data)
97+
return data
10598

10699
def get_identity_response_data(
107100
self, input_data: IdentityWithTraits, environment_key: str
108-
) -> ORJSONResponse:
101+
) -> dict[str, typing.Any]:
109102
environment_document = self.get_environment(environment_key)
110103
environment = build_environment_model(environment_document)
111104
identity = IdentityModel(
@@ -127,7 +120,7 @@ def get_identity_response_data(
127120
identity_hash_key=identity.composite_key,
128121
),
129122
}
130-
return ORJSONResponse(data)
123+
return data
131124

132125
def get_environment(self, client_side_key: str) -> dict[str, typing.Any]:
133126
if environment_document := self.cache.get_environment(client_side_key):

src/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
class FlagsmithUnknownKeyError(KeyError):
22
pass
3+
4+
5+
class FeatureNotFoundError(Exception):
6+
pass

src/main.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from .cache import LocalMemEnvironmentsCache
1414
from .environments import EnvironmentService
15-
from .exceptions import FlagsmithUnknownKeyError
15+
from .exceptions import FeatureNotFoundError, FlagsmithUnknownKeyError
1616
from .models import IdentityWithTraits
1717
from .settings import Settings
1818
from .sse import router as sse_router
@@ -51,15 +51,27 @@ async def health_check():
5151

5252
@app.get("/api/v1/flags/", response_class=ORJSONResponse)
5353
async def flags(feature: str = None, x_environment_key: str = Header(None)):
54-
return environment_service.get_flags_response_data(x_environment_key, feature)
54+
try:
55+
data = environment_service.get_flags_response_data(x_environment_key, feature)
56+
except FeatureNotFoundError:
57+
return ORJSONResponse(
58+
status_code=404,
59+
content={
60+
"status": "not_found",
61+
"message": f"feature '{feature}' not found",
62+
},
63+
)
64+
65+
return ORJSONResponse(data)
5566

5667

5768
@app.post("/api/v1/identities/", response_class=ORJSONResponse)
5869
async def identity(
5970
input_data: IdentityWithTraits,
6071
x_environment_key: str = Header(None),
6172
):
62-
return environment_service.get_identity_response_data(input_data, x_environment_key)
73+
data = environment_service.get_identity_response_data(input_data, x_environment_key)
74+
return ORJSONResponse(data)
6375

6476

6577
@app.on_event("startup")

0 commit comments

Comments
 (0)