Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PyOhio 2016 - Fake It Before You Make It: Mocking Your Way to Better HTTP APIs

PyOhio 2016 - Fake It Before You Make It: Mocking Your Way to Better HTTP APIs

Dave Forgac

July 31, 2016
Tweet

More Decks by Dave Forgac

Other Decks in Technology

Transcript

  1. JSON Schema { "title": "Example Schema", "type": "object", "properties": {

    "firstName": { "type": "string" }, "lastName": { "type": "string" }, "age": { "description": "Age in years", "type": "integer", "minimum": 0 } }, "required": ["firstName", "lastName"] }
  2. The Difference We provided They expected * simplified example {

    "valid": false, "errors": { "field_1": "too short" "field_2": "required", } } { "valid": false, "errors": [ { "field": "field_1" "error": "too short", }, { "error": "required" "field": "field_2", } ] }
  3. Swagger Example swagger: '2.0' info: version: '1.0.0' title: Fake Better

    API description: > This is a basic example API for our talk at http://betterapis.com. termsOfService: http://betterapis.com/terms/ contact: name: API Team email: [email protected] url: http://betterapis.com/contact/ license: name: Apache 2.0 url: http://www.apache.org/licenses/LICENSE-2.0.html host: api.betterapis.com basePath: / securityDefinitions: {} schemes: - http consumes: - application/json produces: ...
  4. Swagger Example Contd. paths: /foo: get: operationId: listFoo produces: -

    application/json parameters: - name: limit in: query type: integer description: maximum number of foo to return responses: 200: description: List all foo schema: type: array items: $ref: '#/definitions/Foo' ... definitions: Foo: title: Foo description: An example type of object type: object properties:
  5. RAML Example #%RAML 0.8 title: Fake Better API version: 1.0.0

    documentation: - title: Fake Better API content: Basic example API for our talk at http://betterapis.com. baseUri: http://api.betterapis.com/ schemas: - Foo-array: | { "type": "array", "items": { "id": "Foo", "title": "Foo", "description": "An example type of object", "type": "object", "properties": { "name": { "type": "string" }, "score": { "type": "integer", "format": "int32" } ...
  6. RAML Example Contd. /foo: /{fooId}: uriParameters: fooId: description: ID of

    the foo type: string required: true displayName: fooId get: displayName: getFooByID responses: 200: description: Foo with the specified ID 404: description: Foo with specified ID not found get: displayName: listFoo responses: 200: description: List all foo body: application/json: schema: Foo-array ... `
  7. API Blueprint Example FORMAT: 1A HOST: http://api.betterapis.com/ # Fake Better

    API This is a basic example API for our talk at http://betterapis.com. # Foo [/foo{?limit}] ## listFoo [GET] + Parameters + limit (, optional) - maximum number of foo to return + Default: 10 + Response 200 (application/json) List all foo + Attributes (array[Foo]) ...
  8. API Blueprint Example Contd. # Foo [/foo] ## putFoo [PUT]

    + Request (application/json) + Attributes (Foo) + Response 200 (application/json) Updates a foo + Attributes (Dynamic) + Response 400 Invalid foo data ## postFoo [POST] + Request (application/json) + Attributes (Foo) + Response 200 (application/json) ...