Skip to content

Conversation

@rwb27
Copy link
Collaborator

@rwb27 rwb27 commented Jan 8, 2026

There are a number of places in LabThings where we must generate URLs pointing to the LabThings server. This is done in several ways, none of which are very nice. This PR lays the groundwork for improving URL generation, especially:

  • In Thing Descriptions
  • When serialising Blobs
  • When describing Invocations

Once it's in, it should make it relatively straightforward to eliminate things like BlobManagerContextDep and the need to pass paths around the application as strings that may or may not be consistent with the actual endpoint URLs.

This PR adds:

  • a middleware function that pushes the url_for method of the Request object to a context variable.
  • a pydantic-compatible class that calls url_for to generate URLs at serialisation time.
  • a test harness that allows this to be used outside of an HTTP request handler.

The middleware is currently tested on its own and in the context of a FastAPI app, but isolated from the rest of LabThings.

The middleware isn't currently added to the app created by the ThingServer. I might describe that in a separate PR that also uses it to tidy up e.g. blob serialisation: it may simplify review to split the two, though probably it is easier to review this PR once I've made the next one that actually uses it.

@barecheck
Copy link

barecheck bot commented Jan 8, 2026

Barecheck - Code coverage report

Total: 96.33%

Your code coverage diff: 0.07% ▴

Uncovered files and lines
FileLines
src/labthings_fastapi/testing.py214

Copy link
Contributor

@julianstirling julianstirling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with the code changes in isolation. I don't full understand the context of when url_for is called though

Copy link
Contributor

@julianstirling julianstirling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One final question on why the Pydantic Style class exists. I assume it is made before the context where it is used is set, but I don't understand where that would be.

Are you able to add a bit more context to the top of the class docstring before merging?

rwb27 added 5 commits January 12, 2026 14:17
This adds:

* a middleware function that pushes the `url_for` method of the `Request` object to a context variable.
* a `pydantic`-compatible class that calls `url_for` to generate URLs at serialisation time.
* a test harness that allows this to be used outside of an HTTP request handler.

The middleware is tested on its own and in the context of a FastAPI app, but isolated from the rest of LabThings.
Verify that both `URLFor` serialisation and `url_for` are OK in request handlers, but the latter fails in a thread. This should help make it clear where you can (and can't) use the two ways of getting URLs.
Make it clearer why URLFor exists and when it's appropriate to call `url_for`.
@rwb27 rwb27 force-pushed the url-for-middleware branch from 4741776 to 3ec2b8b Compare January 12, 2026 14:18
@rwb27 rwb27 merged commit 620abee into main Jan 12, 2026
14 checks passed
@rwb27 rwb27 deleted the url-for-middleware branch January 12, 2026 14:22
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.

3 participants