Skip to content

Commit c350111

Browse files
committed
Apply suggestions from code review.
Thanks @bprobert97 - a couple of nice improvements to tests and docstrings.
1 parent a2d8cdc commit c350111

3 files changed

Lines changed: 24 additions & 18 deletions

File tree

src/labthings_fastapi/server/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def __init__(
8484
arguments, and any connections to other `.Thing`\ s.
8585
:param settings_folder: the location on disk where `.Thing`
8686
settings will be saved.
87-
:param api_prefix: An optional prefix for all API routes. This must either
87+
:param api_prefix: A prefix for all API routes. This must either
8888
be empty, or start with a slash and not end with a slash.
8989
:param application_config: A mapping containing custom configuration for the
9090
application. This is not processed by LabThings. Each `.Thing` can access
@@ -176,10 +176,10 @@ def application_config(self) -> Mapping[str, Any] | None:
176176

177177
@property
178178
def _api_prefix(self) -> str:
179-
"""A string that prefixes all URLs in the application.
179+
r"""A string that prefixes all URLs in the application.
180180
181-
This must either be empty, or start with a slash and not
182-
end with a slash.
181+
This will either be empty, or start with a slash and not
182+
end with a slash. Validation is performed in `.ThingServerConfig`\ .
183183
"""
184184
return self._config.api_prefix
185185

tests/test_server.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,27 +73,33 @@ def test_server_thing_descriptions():
7373
assert prop["forms"][0]["href"] == expected_href
7474

7575

76-
def test_api_prefix():
76+
@pytest.mark.parametrize("api_prefix", ["/api/v3", "/v1", "/custom/prefix"])
77+
def test_api_prefix(api_prefix):
7778
"""Check we can add a prefix to the URLs on a server."""
7879

7980
class Example(lt.Thing):
8081
"""An example Thing"""
8182

82-
server = lt.ThingServer(things={"example": Example}, api_prefix="/api/v3")
83+
server = lt.ThingServer(things={"example": Example}, api_prefix=api_prefix)
8384
paths = [route.path for route in server.app.routes if isinstance(route, Route)]
84-
for expected_path in [
85-
"/api/v3/action_invocations",
86-
"/api/v3/action_invocations/{id}",
87-
"/api/v3/action_invocations/{id}/output",
88-
"/api/v3/action_invocations/{id}",
89-
"/api/v3/blob/{blob_id}",
90-
"/api/v3/thing_descriptions/",
91-
"/api/v3/things/",
92-
"/api/v3/example/",
93-
]:
85+
86+
# Dynamically generate expected paths based on the parametrized prefix
87+
expected_paths = [
88+
f"{api_prefix}/action_invocations",
89+
f"{api_prefix}/action_invocations/{{id}}",
90+
f"{api_prefix}/action_invocations/{{id}}/output",
91+
f"{api_prefix}/blob/{{blob_id}}",
92+
f"{api_prefix}/thing_descriptions/",
93+
f"{api_prefix}/things/",
94+
f"{api_prefix}/example/",
95+
]
96+
97+
for expected_path in expected_paths:
9498
assert expected_path in paths
9599

96-
unprefixed_paths = {p for p in paths if not p.startswith("/api/v3/")}
100+
prefix_with_slash = f"{api_prefix}/"
101+
unprefixed_paths = {p for p in paths if not p.startswith(prefix_with_slash)}
102+
97103
assert unprefixed_paths == {
98104
"/openapi.json",
99105
"/docs",

tests/test_server_config_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def test_ThingServerConfig():
106106
assert config.api_prefix == prefix
107107

108108
# Check some bad prefixes
109-
for prefix in ["api", "/api/", "api/v2", "/badchars!"]:
109+
for prefix in ["api", "/api/", "api/", "api/v2", "/badchars!"]:
110110
with pytest.raises(ValidationError):
111111
ThingServerConfig(things={}, api_prefix=prefix)
112112

0 commit comments

Comments
 (0)