Allow ThingServer to accept ThingServerConfig instances directly#320
Open
Allow ThingServer to accept ThingServerConfig instances directly#320
ThingServer to accept ThingServerConfig instances directly#320Conversation
This avoids some argument duplication and avoids revalidation of the config model. This should allow subclassing of the config model, which ought to come in handy when customising the server downstream. I've also added a property for `debug` to allow downstream code to see it.
Barecheck - Code coverage reportTotal: 96.68%Your code coverage diff: 0.04% ▴ |
…erver`. These methods don't do anything new, but might tidy up downstream code slightly.
This makes the initialiser of `ThingServer` much simpler, and makes it clearer what should go where. It's technically a breaking change, but I think it's easy enough to fix that Julian and I are comfortable it's the right call here.
Passing a dictionary of things is common in test/example code, and
needs to be updated to use the new form. This commit is more or less
a search and replace to do that.
`ThingServer({}, **kwargs)` becomes `ThingServer.from_things({}, **kwargs)`
`ThingServer.from_config(config)` becomes `ThingServer(config)`
This seems tidier than `TestClient(server.app)`.
This: * replaces the call to `uvicorn.run` with a call to `ThingServer.serve` in `serve_from_cli`. * removes the `dry_run` argument in favour of using a mock for `uvicorn.run` * always either returns a server or fails to return in `serve_from_cli` * tests the server's debug flag directly in the debug-related tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Currently,
ThingServeris initialised by supplying a dictionary ofThingsubclasses and other parameters. This means the constructor ofThingServerduplicates all the arguments to the constructor ofThingServerConfig, which causes a few wrinkles.I propose we allow
ThingServerto accept a config model directly. This avoids some argument duplication and avoids revalidation of the config model.It is, however, the case that it's more convenient in test or example code to use
ThingServerin the current way, i.e. to supply the dictionary ofThingsubclasses as the first argument, and other config parameters as subsequent arguments. It is also desirable to be backwards-compatible, which requires keeping this signature as an option.I propose we allow
ThingServerto accept a config model, providing backwards compatibility with the existing way of doing things. This can be done unambiguously if we makethingsa reserved name, i.e. it's not allowed to have aThingon your server with the namethings. In the future, we might split this out of the initialiser, such that you can either initialise a server with:or
Avoiding revalidation should allow easier subclassing of the config model, which ought to come in handy when customising the server downstream.
I've also added:
debugto allow downstream code to see it.settings_folderfrom the config modelservemethod to run the server withuvicorn.runtest_clientmethod to supply a test client.I've removed the
dry_runargument fromserve_from_cli(in favour of testing it properly usingmocker) and changed it to use the newservemethod of the server. This also meant I could improve a couple of the tests by checking it produced a server in the correct state, and not just that they logged the right thing.It is unfortunate that the number of touched files is so large. Mostly this is a result of a couple of search-and-replace operations to change
ThingServer(things)toThingServer.from_things(things)and get rid ofThingServer.from_config.Closes #295
Closes #298
Closes #311