-
Notifications
You must be signed in to change notification settings - Fork 3
URLFor class to generate URLs at serialisation time
#242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Barecheck - Code coverage reportTotal: 96.33%Your code coverage diff: 0.07% ▴ Uncovered files and lines
|
julianstirling
left a comment
There was a problem hiding this 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
julianstirling
left a comment
There was a problem hiding this 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?
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`.
4741776 to
3ec2b8b
Compare
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:
BlobsInvocationsOnce it's in, it should make it relatively straightforward to eliminate things like
BlobManagerContextDepand 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:
url_formethod of theRequestobject to a context variable.pydantic-compatible class that callsurl_forto generate URLs at serialisation time.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.