question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Callbacks and code generation

See original GitHub issue

I’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 a canEmit property in its methods. For example, in my definition I declare that endpoints that make a POST request to /repositories/{repoId}/events should be able to receive the NewCommitEvent type.
  • The NewCommitEvent is defined in the eventDefintions 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:open
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
fehguycommented, May 19, 2017

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

0reactions
sapessicommented, May 19, 2017

Thanks @fehguy works for me.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found