Skip to content

Instantly share code, notes, and snippets.

@brianneisler
Last active June 23, 2017 04:15
Show Gist options
  • Save brianneisler/d37997cfb96d8d77b69bd20079f3cfb3 to your computer and use it in GitHub Desktop.
Save brianneisler/d37997cfb96d8d77b69bd20079f3cfb3 to your computer and use it in GitHub Desktop.

Examples

REST API

Platform

service:
  name: my-service
  provider: aws   # default for all functions in the service
  
providers:
  aws:
   type: aws
   region: us-east-1
  google:
   type: google 
   region: tokyo

functions:
  functionOne:
    events:
      - http:
          path: functionOne
          method: GET
  functionTwo:
    events:
      - http:
          path: functionOne
          method: GET
    provider: google

Using external stream/gateway

service:
  name: my-service
  stream: nordstrom

streams:
  nordstrom:
    id: gateway.nordstrom.io/s/some/stream
    token: ${env:streamToken}
  serverless:
    id: gateway.serverless.io/s/some/stream
    token: ${env:streamToken}

functions:
  functionOne:
    events:  # these events use the service stream
      - http:
          path: functionOne
          method: GET
          stream: serverless
  functionTwo:
    events:  # these events use the service stream
      - http:
          path: functionTwo
          method: PUT
          stream: nordstrom

Multi-cloud REST API

service:
  name: my-service

providers:
  aws:
   type: aws
   region: us-east-1
  google:
   type: google 
   region: tokyo
  azure:
   type: azure 
   region: eastus

functions:
  functionOne:
    provider: aws
    events:
      - http:
          path: functionOne
          method: GET
  functionTwo:
    provider: google
    events:
      - http:
          path: functionTwo
          method: PUT
  functionThree:
    provider: azure
    events:
      - azure.http:  # azure specific http event
          path: functionThree
          method: POST

Multi-service REST API

service:
  name: my-service
service:
  name: my-service-users
  stream: my-service
  
streams:
  my-service:
    id: ${service:my-service.stream}
    token: ${env:streamToken}

functions:
  createUser:
    events:
      - http:
          path: createUser
          method: POST
  getUser:
    events:
      - http:
          path: getUser
          method: GET
service:
  name: my-service-posts
  stream: my-service
  
streams:
  my-service:
    id: ${service:my-service.stream}
    token: ${env:streamToken}

functions:
  createPost:
    events:
      - http:
          path: createPost
          method: POST
  getPost:
    events:
      - http:
          path: getPost
          method: GET

Publishing and Subscribing to Custom Events

Basic emit through emit function

var sdk = require('sdk')

sdk.emit(
  {
    stream: 'gateway.io/s/ac360/my-service/${env:stage}/comments',
    streamToken: '89HA0JAFSLJF09J1A'
  }, // config
  {
    event: 'myBlog.comment.created',
    data: {
      comment: 'hi'
    }
  } // event
)

Specify a stream for convenience

const sdk = require('sdk')

const stream = sdk.stream({
  stream: 'gateway.io/s/ac360/my-service/${env:stage}/comments',
  streamToken: '89HA0JAFSLJF09J1A'
})

stream.emit({
  event: 'myBlog.comment.created',
  data: {
    comment: 'hi'
  }
})

stream.emit({
  event: 'myBlog.comment.update',
  data: {
    comment: 'hello'
  }
})

Questions

  • querying/filtering? How do i specify that i only want to subscribe to a subset of events from a stream that match certain criteria?
  • security around streams? How do i prevent someone from sending an event type on a stream that they're not allowed to send?
  • validation? how do i validate that the publisher is behaving correctly and not sending malformed data?
  • why do we need stream tokens?
  • is async/sync needed?

Alternative to tokens

auth through distributed public keys

  • create service
  • generates public/private key pair for service (similar to oauth)
  • connect and authorize a service (publisher) to access a stream through the platform

Querying/Filtering

var sdk = require('sdk')

sdk.createSubscription({
  streamToken: 'PA019ASFJALSFJA',
  event: sdk.query('myapp.payment.received').where('amount').gt(25),
  function: {
    name: 'checkForFraud'
  }
})

This event is handled

stream.emit({
  event: 'myapp.payment.received',
  data: {
    amount: 50
  }
})

This event is not

stream.emit({
  event: 'myapp.payment.received',
  data: {
    amount: 10
  }
})

config data gets converted to a query

functions:
  createUser:
    events:
      - http:
          path: createUser
          method: POST

is simply...

var sdk = require('sdk')

sdk.createSubscription({
  streamToken: 'PA019ASFJALSFJA',
  event: sdk.query('myapp.http.post').where('path').equals('createUser'),
  function: {
    name: 'createUser'
  }
})

other query examples

.where('name.last').equals('Ghost')
.where('age').gt(17).lt(66)
.where('likes').in(['vaporizing', 'talking'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment