Skip to content

API prefix#268

Open
rwb27 wants to merge 3 commits intomainfrom
api-prefix
Open

API prefix#268
rwb27 wants to merge 3 commits intomainfrom
api-prefix

Conversation

@rwb27
Copy link
Collaborator

@rwb27 rwb27 commented Feb 26, 2026

This adds a configuration parameter for the API prefix. This will allow us to have things visible at, for example, /api/v3/stage.

The prefix applies to LabThings endpoints too, for example /thing_descriptions/ would become /api/v3/thing_descriptions.

I've added tests for

  • Validation of the prefix
  • LabThings endpoints (checked by inspecting app.routes)
  • Use with a TestClient (by adding a prefix in test_thing_client).

If the config parameter is not used, nothing changes.

In doing this, I've swapped functions where I pass the app around for ones that use APIRouter to pass the routes in a nicer way. I have not tackled Thing.add_to_fastapi because that will be a more involved change.

I added some tests for the /things/ and /thing_descriptions/ endpoints, and in so doing discovered I needed to fix /things/. I've done this by adding an endpoint name for the thing description of each thing: it's now named things.{name}, which means we can get its URL with url_for. It would be nice to do this for all endpoints, but that's a job for another time.

Closes #264

It's now possible to configure an API prefix, which affects all LabThings-generated URLs.

I've also switched a couple of places from passing the app around to creating an APIRouter, which feels much cleaner.

So far, tests pass but I've not tried to set a prefix.
The previous commit laid the groundwork but failed to actually set the API prefix. This is now fixed.

The API prefix is tested in a couple of places: validation is tested in `test_server_config_model`, and the endpoints are checked in `test_server` explicitly, and `test_thing_client` implicitly (because we use a prefix for the thing that's tested).
I'd accidentally modified these endpoints (and deleted `things`) when I changed the function that added them.

I've now added tests for these endpoints, and fixed the URL generation in `things`.
@rwb27 rwb27 requested a review from julianstirling February 26, 2026 00:23
@rwb27 rwb27 added this to the v0.0.16 milestone Feb 26, 2026
@barecheck
Copy link

barecheck bot commented Feb 26, 2026

Barecheck - Code coverage report

Total: 96.55%

Your code coverage diff: 0.05% ▴

Uncovered files and lines
FileLines
src/labthings_fastapi/actions.py512-513, 526, 551-552, 727, 871-872, 875, 878, 926
src/labthings_fastapi/server/__init__.py209
src/labthings_fastapi/thing.py223, 231, 264, 316-318, 350, 438

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a URL prefix to Things

1 participant