Provider adapters: replacing an external service without rewriting the editorial workflow
A shared contract for different providers: capabilities, accepted work, confirmed delivery and the origin of unfinished operations.
An editor approves a short announcement and selects a channel. The application passes the text to an external service, receives a response and displays the result. With just one provider, it is easy to treat that provider's vocabulary as part of the editorial workflow: a successful HTTP request means publication, a response identifier means a message, and an error means an opportunity to retry.
Replacing the service tests those assumptions. The new provider might only accept jobs into a queue. Its identifier represents work, not a completed message. A different length limit requires a text change, while a retry after a lost response might create a second publication. If those differences hide behind send(): bool, the editorial workflow gets a simple interface and an inaccurate view of the world.
Consider an original teaching example with two fictional providers: synchronous Aster and asynchronous Boreal. The aim is to define an honest shared contract, examine capability differences and preserve the origin of an unfinished operation. The example uses local fake clients; its limits and fields are not the specification of a real service.
Start with the editorial action
The editor cares about the approved text, its destination and an understandable result. HTTP field names, authentication mechanisms and internal queue identifiers belong to the integration. The input Envelope therefore contains an operation identifier, a logical destination, the exact text and its format. It does not accept an arbitrary external API address.
Approval, permissions, draft storage and protection against repeated dispatch remain application responsibilities. The adapter receives an already authorized operation and translates it into a particular protocol. That boundary does not make editorial decisions less important: changing text after approval must go through the appropriate process before the adapter is called.
In PHP, an interface defines methods and compatible implementation signatures. That is enough to check the shape of a call, but our contract determines what its result means. The PHP interface documentation describes the mechanism; semantic promises need a separate definition.
interface ProviderPort
{
public function binding(): string;
public function send(Envelope $envelope): Observation;
public function inspect(Envelope $envelope, Observation $previous): Observation;
}
send() makes one attempt. inspect() determines the state of previously accepted work if the provider supports that capability. A shared interface does not promise that every service has every operation: this teaching protocol gives Aster no lookup by queue receipt. Calling that unsupported inspection is a contract usage error.
Name what is actually known
The result should describe an observation that lets the workflow make its next decision. The example has six states:
| State | What has been established | What the application can do |
|---|---|---|
delivered |
The provider confirmed a created message and identified it | Record delivery |
pending |
Work was accepted, but there is no final result yet | Schedule reconciliation |
temporary_refusal |
The provider explicitly refused before acceptance | Consider a bounded retry |
final_refusal |
The same request has a definite permanent refusal | Indicate that a correction is needed |
unknown |
Evidence is insufficient to determine the effect | Preserve uncertainty and reconcile |
unsupported |
A local check found an unsupported capability | Change the plan before an external call |
Here, delivered means confirmed message creation at the provider. It does not prove that a person read the announcement or saw a notification. That conclusion needs different evidence and a separate product agreement.
HTTP 202 reports acceptance for processing, not completed processing, as RFC 9110, section 15.3.3 explicitly defines. Boreal therefore returns pending with receipt job-8. Only a later consistent lookup response containing a message identifier changes the observation to delivered.
In our fictional specification, Aster instead returns 201, state created and a receipt for the created message. The adapter checks these together. A success code alone is insufficient: a missing identifier or contradictory response leaves the outcome unknown. For comparison with a real API, Telegram sendMessage returns a Message object on success. That format cannot be assumed for every delivery service.
Compare capabilities before switching
Both teaching providers accept plain text. Aster allows up to 120 Unicode code points and Boreal up to 80. These deliberately small, illustrative values make incompatibility easy to see. The limit counts code points, not bytes or visible graphemes.
| Property in the example | Aster | Boreal |
|---|---|---|
| Plain UTF-8 text | Up to 120 code points | Up to 80 code points |
| Markdown | Unsupported | Unsupported |
| First positive response | Created message | Accepted work |
| Later inspection by receipt | Unavailable | Supported |
| Automatic provider substitution | Unavailable | Unavailable |
A hundred-code-point announcement fits Aster but not Boreal. The adapter must not silently remove the last twenty or strip formatting. Doing so would change approved material. A local unsupported response returns the decision to the editorial workflow before an external call.
The same applies to the destination. The logical channel news maps to a particular external destination in the binding configuration. An unknown destination is rejected locally. The fake client counts calls, and the check requires zero requests for an unsupported format, excessive length or unknown channel.
An unfinished operation keeps its provider
The least obvious part of replacement is the old queue. Suppose Boreal accepts an announcement and issues job-8. Aster then becomes the primary service. Inspection of the earlier job must contact Boreal, although new sends now go to Aster.
To support this, Observation stores the binding key, operation identifier, logical destination and content fingerprint. PublicationGateway::inspect() selects an adapter from the earlier observation. The current default provider participates only in a new send.
A binding key identifies an immutable configuration version: adapter, account and destination mappings. Replacing a token or channel under an old key breaks that guarantee. In the example, the registry is immutable within the process; the application must provide production storage for configuration versions. A binding field does not create that storage by itself.
If the old binding has already been removed, inspection returns unknown with a binding-unavailable reason. There is no call to the new provider. The operator sees unfinished work: restore access to the old reconciliation channel or make a separate delivery decision.
The fingerprint is not a signature, access permission or idempotency key either. It checks that a receipt is being matched to the same operation, destination and exact text. The example rejects inspecting an old receipt with an altered announcement. Authenticity of the stored observation itself remains the trusted application's responsibility.
Retry depends on evidence of refusal
A lost response raises a difficult question: did the provider accept the request? A second service cannot answer merely because it is available. A hidden fallback call might create two publications in different channels or two copies within one logical stream.
In the fictional protocol, temporary refusal requires consistent 429, refused and accepted=false values. A valid waiting hint is additionally bounded to 1–300 seconds. A hint of another type or outside that range is discarded; the explicit refusal remains established. The application decides the retry count, time budget and continuing validity of the approval.
This is a local agreement for the teaching providers. In general, automatically retrying a non-idempotent operation requires known idempotency or evidence that the original request was not applied; RFC 9110, section 9.2.2 states the relevant rule. A retry word in JSON does not supply that evidence.
Real services can have materially different conditions. For example, the Stripe documentation describes key behavior separately for API v1 and v2; for v1, execution start, saved results and key retention matter. A generic operation in our object therefore does not prove that an external API supports safe retries.
A queue lookup error is another trap. A 404 response when looking for job-8 might indicate an unavailable or already removed record. It does not prove that the original announcement was never created. The teaching adapter retains the receipt and returns unknown, rather than turning a failed read into permission to resend.
Check shared meaning and specific differences
The run executed 44 native PHP cases: 26 literal responses and 18 additional scenarios in one PHPUnit test with 51 assertions. They were included in a global run of 723 tests and 4,489 assertions. The checks covered response classification, switching providers, preserving a known binding, Unicode limits and the absence of a hidden fallback call. unknown does not mean refusal, and safeRetry applies only to temporary refusal.
The fake client only returns scripted responses and records calls. It implements no hidden deduplication, creates no real messages and proves nothing about external network behavior. Calculating an expected result with the same mapper would merely repeat its error, so the decisions in the fixture set are specified independently of the mapper.
The PHP example used only scripted Aster and Boreal responses, without a real network, providers, durable ledger or fallback delivery. It also does not cover durable queues, concurrent workers, recovery after restart or real provider authentication. The integration stage must examine these boundaries in the application and compare fake responses with the selected service's current specification.
Replacement as a controlled transition
A practical transition starts with a capability matrix and the meaning of results. The new binding then receives only new authorized sends, while older operations are reconciled through the previous binding when it is available. The editor's status indicator must distinguish accepted work from confirmed message creation so that a queue does not look like a successful publication.
An adapter is useful when the editorial workflow retains its decisions and service differences remain explicit. Switching configuration is just one step. Afterwards, the system still needs to know which text was sent, where it belongs, what the provider actually confirmed and who can finish the unresolved operations.