Callbacks and code generation
See original GitHub issueI’ve had this contribution in the pipeline for a while. A large part of it is addressed in callbacks. However, I think there is value in setting a standard to expose the API definition a-la OpenID Connect.
Proposal
The sample definitions are below.
- The emitter service (
MyCodeRepo
) uses acanEmit
property in its methods. For example, in my definition I declare that endpoints that make aPOST
request to/repositories/{repoId}/events
should be able to receive theNewCommitEvent
type. - The
NewCommitEvent
is defined in theeventDefintions
section of the Swagger file. - The request section defines the shape of the request from the event emitter (http verb + body/parameters)
- The response section defines a contract for the event consumer to respect – for example, the emitter here says that 429 responses will cause a retry
- The consumer service (
MyPipeline
), defines a new events property in one of its paths (in this case /workflows) - The events property contains a list of events that reference the emitters’ Swagger definition
- The dependency relies on a convention similar to OIDC’s .well-known endpoint where the Swagger is exposed
Outcome
- The emitter will be able to automatically generate a library that creates events – the mechanism to store the subscribers is up to the emitter
- The consumer will be able to auto-generate the server-side APIs to receive the events with the required paths, models and response codes.
What’s missing
- A more formal way for consumers to communicate their endpoint to the emitter – not sure if this is the job of the specs.
Samples - Event emitted (MyCodeRepo
)
---
swagger: "2.0"
info:
version: "1.0"
title: "MyCodeRepo"
host: "myservice.com"
basePath: "/Prod"
schemes:
- "https"
paths:
/repositories/{repoId}/events:
post:
responses: {}
security:
- OAuth: []
canEmit:
- !Ref #/eventDefinitions/NewCommitEvent
securityDefinitions:
OAuth:
type: "apiKey"
name: "Authorization"
in: "header"
eventDefinitions:
NewCommitEvent:
request:
method: post
produces:
- application/json
parameters:
- in: body
name: body
required: false
schema: !Ref #/definitions/Commit
response:
consumes:
- application/json
responses:
200:
description: Event received successfully
schema: !Ref #/definitions/ResponseBody
###### if we expect a response from the client
429:
description: Event could not be processed, retry
###### If the client returns this error, the emitter will retry
400:
description: Could not process event
definitions:
Commit:
type: object,
required:
- CommitId
- Username
properties:
CommitId:
type: string
Username:
type: string
description: User profile information
Samples - Event consumer (MyPipeline
)
---
swagger: "2.0"
info:
version: "1.0"
title: "MyPipeline"
host: "mypipeline.com"
basePath: "/Prod"
schemes:
- "https"
paths:
/workflows:
events:
- !Ref 'myservice.com/NewCommitEvent'
###### relies on a well known path like myservice.com/.swagger/definition.yml
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Define Callbacks for Protected Models - MATLAB & Simulink
When you create a protected model, you can customize its behavior by defining callbacks. Callbacks specify code that executes when you view, simulate,...
Read more >Looking for A Safe Callback Before Script Compile - Unity Forum
I want to auto-generate some code files for my project. These code files would need to be destroyed and regenerated every time the...
Read more >Code Generation is slow, callbacks call in Simulink take's ...
Code Generation is slow, callbacks call in Simulink take's more than 3 seconds, specially for the ADC Blocks. For example, I create a...
Read more >A Study on Solving Callbacks with JavaScript Generators
Generator functions can suspend execution with the yield keyword, ... What does the above code have to do with node's callback hell?
Read more >Using model callback with Simulink Coder - Stack Overflow
You cannot generate code for model callbacks. If you do not want to regenerate code every time you change your input you can...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sorry for the delay on this–I just did a presentation on callbacks and I believe the intent should handle this use case. You can see that presentation here: https://t.co/1XUELjz5G5
Thanks @fehguy works for me.