Service interfaces
Table of contents
Introduction
When your line has several features that do the same job, such as two ways of paying, a parent feature declares the interface they implement and each of them registers as a provider. The parent owns the wiring, the configuration and the choice. A product swaps one provider for another without a refinement.
# splent_feature_pdf_export/__init__.py
from splent_io.splent_feature_reports.exports import register_export_provider
from splent_io.splent_feature_pdf_export.provider import PdfExportProvider
def init_feature(app):
register_export_provider(app, PdfExportProvider)
The provider imports its parent, never the other way round.
When to use one
Use an interface when the line’s model groups interchangeable features under one parent.
| Group | Parent | Interface | A product installs |
|---|---|---|---|
payments (alternative) |
shop |
PaymentService |
One provider |
exports (or) |
reports |
ExportProvider |
One or more, chosen per report |
report_review (optional) |
reports |
ReportReviewer |
None, or one when a setting needs it |
Without an interface, each child wires itself, the copies drift, and changing one behaviour ends in a refinement.
Declaring the interface in the parent
Write an abstract class in the parent’s package. Mark what every provider must implement with abstractmethod, and give the rest a default.
# splent_feature_reports/exports.py
from abc import ABC, abstractmethod
from splent_framework.services.service_locator import register_service
class ExportProvider(ABC):
name: str = "" # what a report records, "pdf"
media_type: str = "" # what it produces, "application/pdf"
@abstractmethod
def render(self, report) -> bytes: ...
def filename(self, report):
return None
def register_export_provider(app, provider):
accept_provider(app, provider) # the parent's own checks
_registry(app)[provider.name] = provider.__name__
register_service(app, provider.__name__, provider)
Next to it, write the parent’s own accept_provider. Raise TypeError for a class that does not subclass the interface, leaves an abstract method out, or has no valid name. Raise RuntimeError for a second provider where the group allows one, or a second class under a taken name. Accept the same class twice, since a rebuilt app registers its features again.
Registering the provider in the service locator too keeps it replaceable with refine_service.
Requiring the parent
The provider’s contract requires the parent (requires.features = ["reports>=0.7.0"]), and the model says so (pdf_export => reports), which also makes it load after the parent.
A provider never builds a Flask response and never sees the request. It raises the errors the interface declares, and the parent’s routes turn them into statuses and messages.
Letting the parent choose
For an alternative, the provider is the one registered. For an or-group, the parent keeps a registry by name and picks with a rule your product configures. reports takes the provider a report is bound to, else the one the request names, else REPORTS_EXPORT_PROVIDER, else the only one installed.
Requiring a provider for a setting
Some interfaces need a provider only for one value of a parameter. reports sends a report to review only when REPORTS_SHARE_POLICY is reviewed, and the reviewer is a ReportReviewer provider.
# splent_feature_report_review/__init__.py
def init_feature(app):
register_report_reviewer(app, BoardReviewer)
When the setting cannot work without its provider, stop the start with a message that says what to install. The parent loads first, so it reads what the product declares (get_features_from_pyproject) rather than waiting for the registration. Say the same in the model, so product:validate catches it before any start.
reports_share_policy {param 'REPORTS_SHARE_POLICY'}
alternative
share_open {value 'open'}
share_reviewed {value 'reviewed'}
share_reviewed => report_review
See Letting a value select features.
Testing providers
The parent’s tests register a fake provider of their own and restore the registry afterwards, so they pass with any provider installed, or none. Pin the settings a product may change underneath them, with a fixture.
def test_a_failure_before_the_deliver_step_discards_the_file(report, provider):
provider.fail["render"] = ProviderUnavailable("the render broke")
...
assert provider.names() == ["render", "discard"]
A provider’s tests cannot import the parent’s, since the tests package is left out of a feature’s wheel. Bring your own fakes, mock the third party one layer below (such as requests.request), and check that each answer becomes the right error of the interface.
See also
- Service locator, where providers are registered
- Refine, extend or compose, a provider composes with its parent