Top Banner
Building Self Documenting HTTP APIs with CQRS Derek Comartin @codeopinion codeopinion.com
62

Building Self Documenting HTTP APIs with CQRS

Jan 22, 2018

Download

Technology

Derek Comartin
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Building Self Documenting HTTP APIs with CQRS

Building Self Documenting HTTP APIs with CQRS

Derek Comartin@codeopinion

codeopinion.com

Page 2: Building Self Documenting HTTP APIs with CQRS

Context

Page 3: Building Self Documenting HTTP APIs with CQRS
Page 4: Building Self Documenting HTTP APIs with CQRS

TechSPA + HTTP API

Page 5: Building Self Documenting HTTP APIs with CQRS

HTTP APIASP.NET WebAPI

Page 6: Building Self Documenting HTTP APIs with CQRS

HTTP APIASP.NET WebAPI+ Entity Framework+ OData

Page 7: Building Self Documenting HTTP APIs with CQRS

ClientAngularJS

Page 8: Building Self Documenting HTTP APIs with CQRS

3 Major Pain PointsBreaking Changes & Versioning

Unable to change internals data structuresModels still evolving

Unable to change endpoint routesBoundaries still evolving

Documenting Endpoints & Payloads

Page 9: Building Self Documenting HTTP APIs with CQRS

3 Major Pain PointsBreaking Changes & Versioning

Unable to change internals data structuresModels still evolving

Unable to change endpoint routesBoundaries still evolving

Documenting Endpoints & Payloads

Page 10: Building Self Documenting HTTP APIs with CQRS

OARObject as Resource

Alex Moorehttp://mooreniemi.github.io/rest/apis/2016/11/08/oar-is-not-rest.html

1. A resource maps 1:1(-ish) to an object in your server’s application logic.

2.URIs and HTTP methods are given conventions mapping to CRUD operations.

Page 11: Building Self Documenting HTTP APIs with CQRS

OARObject as Resource

HTTP VERB SQL STATEMENT

GET SELECT

POST INSERT

PUT UPDATE

PATCH UPDATE

DELETE DELETE

Page 12: Building Self Documenting HTTP APIs with CQRS

Breaking ChangesUnable to Change Internals

Page 13: Building Self Documenting HTTP APIs with CQRS

Breaking ChangesUnable to Change Internals

Page 14: Building Self Documenting HTTP APIs with CQRS

Breaking ChangesUnable to Change Internals

Page 15: Building Self Documenting HTTP APIs with CQRS

Breaking ChangesResource Model != Data Model

Page 16: Building Self Documenting HTTP APIs with CQRS

3 Major Pain PointsBreaking Changes & Versioning

Unable to change internals data structuresModels still evolving

Unable to change endpoint routesBoundaries still evolving

Documenting Endpoints & Payloads

Page 17: Building Self Documenting HTTP APIs with CQRS

Breaking ChangesUnable to change endpoint routes

Page 18: Building Self Documenting HTTP APIs with CQRS

Breaking ChangesUnable to change endpoint routes

Page 19: Building Self Documenting HTTP APIs with CQRS

3 Major Pain PointsBreaking Changes & Versioning

Unable to change internals data structuresModels still evolving

Unable to change endpoint routesBoundaries still evolving

Documenting Endpoints & Payloads

Page 20: Building Self Documenting HTTP APIs with CQRS

Ability to create an evolving Web API

Page 21: Building Self Documenting HTTP APIs with CQRS

EvolutionNot versioning…

Page 22: Building Self Documenting HTTP APIs with CQRS

3 Major Pain PointsBreaking Changes & Versioning

Unable to change internals data structuresModels still evolving

Unable to change endpoint routesBoundaries still evolving

Documenting Endpoints & Payloads

Page 23: Building Self Documenting HTTP APIs with CQRS

ContractsData Transfer Objects

DB Entity JSON

Page 24: Building Self Documenting HTTP APIs with CQRS

ContractsData Transfer Objects

DB

Entity

Entity

Resource JSON

Page 25: Building Self Documenting HTTP APIs with CQRS

ContractsData Transfer Objects

DB

Entity

Entity

Resource Representation

Page 26: Building Self Documenting HTTP APIs with CQRS

3 Major Pain PointsBreaking Changes & Versioning

Unable to change internals data structuresModels still evolving

Unable to change endpoint routesBoundaries still evolving

Documenting Endpoints & Payloads

Page 27: Building Self Documenting HTTP APIs with CQRS

RESTWat?

Page 28: Building Self Documenting HTTP APIs with CQRS

HypertextREST APIs must be hypertext-driven

When I say hypertext, I mean the simultaneous presentation of information and controls such that the information becomes the affordance through which the user (or automaton) obtains choices and selects actions.

Roy T. Fieldinghttp://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven

Page 29: Building Self Documenting HTTP APIs with CQRS

HypertextWhat can I do with a resource?

Hypertext is a text which contains links to other texts. The term was invented by Ted Nelson around 1965.

Page 30: Building Self Documenting HTTP APIs with CQRS

HypermediaLinks

Page 31: Building Self Documenting HTTP APIs with CQRS

HypermediaForms

Page 32: Building Self Documenting HTTP APIs with CQRS

ResourcesState + Actions

DB

Entity

Entity

DTO

RepresentationState

Links

Forms

Page 33: Building Self Documenting HTTP APIs with CQRS

HypermediaLinks

Page 34: Building Self Documenting HTTP APIs with CQRS

HypermediaLinks

Page 35: Building Self Documenting HTTP APIs with CQRS

HypermediaLinks

Page 36: Building Self Documenting HTTP APIs with CQRS

HypermediaForms

Page 37: Building Self Documenting HTTP APIs with CQRS

HypermediaForms

POST: /books/addContent-Type: application/x-www-form-urlencoded

book_title=The+Goal%3A+A+Process+of+Ongoing+Improvement&book_price=1.45

Page 38: Building Self Documenting HTTP APIs with CQRS

HypermediaForms

Page 39: Building Self Documenting HTTP APIs with CQRS

HypermediaEmbedded Resources

Page 40: Building Self Documenting HTTP APIs with CQRS

HypermediaEmbedded Resources

Page 41: Building Self Documenting HTTP APIs with CQRS

HypermediaEmbedded Resources

Page 42: Building Self Documenting HTTP APIs with CQRS

Media TypesHypermedia

• HAL• Collection+JSON• Siren• JSON API• Hydra and JSON-LD

Page 43: Building Self Documenting HTTP APIs with CQRS

CQRS is simply the creation of twoobjects where there was previously onlyone.The separation occurs based uponwhether the methods are a command ora query (the same definition that is usedby Meyer in Command and QuerySeparation, a command is any methodthat mutates state and a query is anymethod that returns a value).

– Greg Young

CQRS

Page 44: Building Self Documenting HTTP APIs with CQRS

http://www.codeproject.com/Articles/555855/Introduction-to-CQRS

Not this…

Page 45: Building Self Documenting HTTP APIs with CQRS

Controller

Command

Query

Database

CQRS

Page 46: Building Self Documenting HTTP APIs with CQRS

CQRS

Command = ResourceQuery = Resource

Page 47: Building Self Documenting HTTP APIs with CQRS

Controller

Command

PUTPOST

DELETE

Query

GETOPTIONS

HEAD

Database

ResourcesCQRS

Page 48: Building Self Documenting HTTP APIs with CQRS

MVCViewModel = State + Queries + Commands

DB

Entity

Entity

Razor

HTMLViewModel

Links

Forms

Page 49: Building Self Documenting HTTP APIs with CQRS

Query ResourcesReturn State + Actions

DB

Entity

Entity

Query

RepresentationState

Queries

Commands

Page 50: Building Self Documenting HTTP APIs with CQRS

Command ResourcesMutate State

DB

Entity

Entity

Command(Resource)

Page 51: Building Self Documenting HTTP APIs with CQRS

ResourcesGet + Post + Redirect Pattern

Get Books The Goal Add To Cart Get Cart The Goal

Page 52: Building Self Documenting HTTP APIs with CQRS

GET /api/books

Page 53: Building Self Documenting HTTP APIs with CQRS

GET /api/books/243

Page 54: Building Self Documenting HTTP APIs with CQRS

Request:POST /api/books/243/addToCart

Response:HTTP 1.1 303 See OtherLocation: /api/cart

Page 55: Building Self Documenting HTTP APIs with CQRS

GET /api/cart

Page 56: Building Self Documenting HTTP APIs with CQRS

GET /api/books/243

Page 57: Building Self Documenting HTTP APIs with CQRS

Opaque URILook for the “Rel”

Page 58: Building Self Documenting HTTP APIs with CQRS

VersioningEvolve your API

Page 59: Building Self Documenting HTTP APIs with CQRS

VersioningEvolve your API

Page 60: Building Self Documenting HTTP APIs with CQRS

Hide/show functionality based on payload

“Dumb” Client responsible for view/display

Easy to Navigate API

Success!

Page 61: Building Self Documenting HTTP APIs with CQRS

Trade-offsNotes

State Transition/Flow

Analytics before resource removal301 Moved Permanently

No client magic dust

SPA Deep Linking

Page 62: Building Self Documenting HTTP APIs with CQRS

That’s it…Thanks!

Derek Comartin@codeopinion

codeopinion.com