-
-
Notifications
You must be signed in to change notification settings - Fork 25
Add route operation callbacks #167
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
387 changes: 387 additions & 0 deletions
387
...k/co/compendiumdev/thingifier/adapter/http/apihandlers/RouteOperationCallbackApplier.java
Large diffs are not rendered by default.
Oops, something went wrong.
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
26 changes: 26 additions & 0 deletions
26
...ier/src/main/java/uk/co/compendiumdev/thingifier/api/callbacks/CallbackFailurePolicy.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package uk.co.compendiumdev.thingifier.api.callbacks; | ||
|
|
||
| /** | ||
| * Controls how Thingifier reacts when a route operation callback throws. | ||
| * | ||
| * <p>Callbacks are trusted application code running after Thingifier has decided an operation | ||
| * result. Applications can choose whether a side-effect failure should fail the visible API request | ||
| * or be logged while preserving the original response. | ||
| */ | ||
| public enum CallbackFailurePolicy { | ||
| /** | ||
| * Convert the callback exception into a 500 API response. | ||
| * | ||
| * <p>This is the default because silently skipping application side effects can leave | ||
| * application-owned state inconsistent with Thingifier-managed data. | ||
| */ | ||
| FAIL_REQUEST, | ||
|
|
||
| /** | ||
| * Log the callback exception and preserve the original operation response. | ||
| * | ||
| * <p>Use this when the callback is observational, such as diagnostics or best-effort metrics, | ||
| * and the API operation should not fail because the callback failed. | ||
| */ | ||
| LOG_AND_CONTINUE | ||
| } |
21 changes: 21 additions & 0 deletions
21
...ain/java/uk/co/compendiumdev/thingifier/api/callbacks/ThingifierApiOperationCallback.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package uk.co.compendiumdev.thingifier.api.callbacks; | ||
|
|
||
| /** | ||
| * Trusted application callback invoked after a route operation has produced an API result. | ||
| * | ||
| * <p>Use route operation callbacks for application side effects such as audit logging, projections, | ||
| * cache invalidation, or synchronising app-owned state. Response shaping should stay in route | ||
| * response policies or response hooks so callbacks can remain focused on observing the completed | ||
| * operation. | ||
| */ | ||
| @FunctionalInterface | ||
| public interface ThingifierApiOperationCallback { | ||
|
|
||
| /** | ||
| * Runs the application callback for one completed route operation. | ||
| * | ||
| * @param context immutable route, request, auth, and data-scope information | ||
| * @param result immutable operation outcome details | ||
| */ | ||
| void run(ThingifierApiOperationContext context, ThingifierApiOperationResult result); | ||
| } |
156 changes: 156 additions & 0 deletions
156
...k/co/compendiumdev/thingifier/api/callbacks/ThingifierApiOperationCallbackDefinition.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| package uk.co.compendiumdev.thingifier.api.callbacks; | ||
|
|
||
| import uk.co.compendiumdev.thingifier.api.spec.ThingifierApiRouteRule; | ||
|
|
||
| /** | ||
| * Runtime-only registration for one route operation callback. | ||
| * | ||
| * <p>The definition is code-only by design. Java callbacks cannot safely round-trip through YAML or | ||
| * OpenAPI, so Thingifier stores them only in the in-memory API contract and uses the name for | ||
| * diagnostics. | ||
| */ | ||
| public final class ThingifierApiOperationCallbackDefinition { | ||
|
|
||
| /** Outcome selector used when deciding whether a callback should run. */ | ||
| public enum Outcome { | ||
| /** Run for any completed outcome. */ | ||
| ANY, | ||
|
|
||
| /** Run only for 2xx/3xx operation responses. */ | ||
| SUCCESS, | ||
|
|
||
| /** Run only for non-success operation responses. */ | ||
| FAILURE, | ||
|
|
||
| /** Run only when the final status code matches {@link #statusCode()}. */ | ||
| STATUS | ||
| } | ||
|
|
||
| private final ThingifierApiRouteRule routeRule; | ||
| private final String name; | ||
| private final Outcome outcome; | ||
| private final Integer statusCode; | ||
| private final ThingifierApiOperationCallback callback; | ||
| private CallbackFailurePolicy failurePolicy; | ||
|
|
||
| /** | ||
| * Creates a callback registration. | ||
| * | ||
| * @param routeRule route that owns the callback | ||
| * @param name stable diagnostic name | ||
| * @param outcome outcome selector | ||
| * @param statusCode status code for {@link Outcome#STATUS}, otherwise null | ||
| * @param callback trusted application callback | ||
| */ | ||
| public ThingifierApiOperationCallbackDefinition( | ||
| final ThingifierApiRouteRule routeRule, | ||
| final String name, | ||
| final Outcome outcome, | ||
| final Integer statusCode, | ||
| final ThingifierApiOperationCallback callback) { | ||
| if (routeRule == null) { | ||
| throw new IllegalArgumentException("route rule is required"); | ||
| } | ||
| if (name == null || name.trim().isEmpty()) { | ||
| throw new IllegalArgumentException("callback name is required"); | ||
| } | ||
| if (outcome == null) { | ||
| throw new IllegalArgumentException("callback outcome is required"); | ||
| } | ||
| if (outcome == Outcome.STATUS && statusCode == null) { | ||
| throw new IllegalArgumentException("status callback requires a status code"); | ||
| } | ||
| if (callback == null) { | ||
| throw new IllegalArgumentException("callback is required"); | ||
| } | ||
| this.routeRule = routeRule; | ||
| this.name = name.trim(); | ||
| this.outcome = outcome; | ||
| this.statusCode = statusCode; | ||
| this.callback = callback; | ||
| this.failurePolicy = CallbackFailurePolicy.FAIL_REQUEST; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the failure policy for this callback. | ||
| * | ||
| * @param policy callback exception handling policy | ||
| * @return owning route rule so route configuration can continue fluently | ||
| */ | ||
| public ThingifierApiRouteRule onCallbackFailure(final CallbackFailurePolicy policy) { | ||
| if (policy == null) { | ||
| throw new IllegalArgumentException("callback failure policy is required"); | ||
| } | ||
| this.failurePolicy = policy; | ||
| return routeRule; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the stable diagnostic callback name. | ||
| * | ||
| * @return callback name | ||
| */ | ||
| public String name() { | ||
| return name; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the outcome selector for this callback. | ||
| * | ||
| * @return configured outcome selector | ||
| */ | ||
| public Outcome outcome() { | ||
| return outcome; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the status code matched by status-specific callbacks. | ||
| * | ||
| * @return status code, or null for non-status callbacks | ||
| */ | ||
| public Integer statusCode() { | ||
| return statusCode; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the application callback. | ||
| * | ||
| * @return trusted callback | ||
| */ | ||
| public ThingifierApiOperationCallback callback() { | ||
| return callback; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the configured callback failure policy. | ||
| * | ||
| * @return failure policy | ||
| */ | ||
| public CallbackFailurePolicy failurePolicy() { | ||
| return failurePolicy; | ||
| } | ||
|
|
||
| /** | ||
| * Reports whether this callback should run for the supplied result. | ||
| * | ||
| * @param result operation result | ||
| * @return true when the outcome selector matches | ||
| */ | ||
| public boolean matches(final ThingifierApiOperationResult result) { | ||
| if (result == null) { | ||
| return false; | ||
| } | ||
| switch (outcome) { | ||
| case ANY: | ||
| return true; | ||
| case SUCCESS: | ||
| return result.successful(); | ||
| case FAILURE: | ||
| return result.failed(); | ||
| case STATUS: | ||
| return statusCode != null && statusCode == result.statusCode(); | ||
| default: | ||
| return false; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
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.
For HTTP-backed requests, this invokes callbacks after one response-policy application, but
ThingifierHttpApi.httpResponseForappliesRouteApiResponsePolicyApplieragain afterward. If a success policy changes 200 to an error status, for example, the callback observes that intermediate status while the second pass can select the matching error policy and expose a different status/body to the client, soafterStatusandresult.statusCode()do not describe the promised final route-shaped result. Ensure the HTTP path applies response policy only once or move callback execution after its last application.Useful? React with 👍 / 👎.