futureproofing rest apis

89
FUTUREPROOF REST APIS Mark Stafford Microsoft

Upload: markdstafford

Post on 28-Jul-2015

352 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Futureproofing REST APIs

FUTUREPROOFREST APIS

Mark StaffordMicrosoft

Page 2: Futureproofing REST APIs
Page 3: Futureproofing REST APIs
Page 4: Futureproofing REST APIs
Page 5: Futureproofing REST APIs
Page 6: Futureproofing REST APIs
Page 7: Futureproofing REST APIs

Fast forward

• Three decades

• A few degrees

• A wife

• Four! kids

Page 8: Futureproofing REST APIs

LESS DREAMING,

MORE THINKING

Page 9: Futureproofing REST APIs

HTTP APIS

Page 10: Futureproofing REST APIs

Standards are good

Page 11: Futureproofing REST APIs

API franca • HTTP APIs can learn from lingua francas

• HTTP APIs should be interoperable

• Not making enough of an effort

• Most HTTP APIs are “bespoke” – built-to-fit

• There are no bonus points for originality

Page 12: Futureproofing REST APIs
Page 13: Futureproofing REST APIs

THIS TALK IS ABOUT FUTUREPROOFING

Page 14: Futureproofing REST APIs

Disclaimers

• These are my opinions

• The opinions are conversation starters

• Focus on the future, not the past

• Not exhaustive

• Feedback appreciated

Page 15: Futureproofing REST APIs

1.Policies2. Versioning

3. Headers

4. Pagination

5. Separate data & metadata

6. Don’t mint mime types

7. Wrap arrays & primitives

8. Responses should be self-describing

9. Use a consistent query syntax

10. Be consistent elsewhere

Page 16: Futureproofing REST APIs
Page 17: Futureproofing REST APIs

Important policies

• Terms of service

• Privacy

• Deprecation

• Breaking change

• SLAs

• Rate limits

• Licenses

• Support

Page 18: Futureproofing REST APIs
Page 19: Futureproofing REST APIs

Before & after

• Nobody is doing this right

• Twitter gets honorable mention

Page 20: Futureproofing REST APIs

What should it look like?

• All policies in one place

• Somewhere developers will “stumble” over them

• Refresh developers on policies

• Consider versioning the API for policy changes

Page 21: Futureproofing REST APIs

1. Policies

2.Versioning3. Headers

4. Pagination

5. Separate data & metadata

6. Don’t mint mime types

7. Wrap arrays & primitives

8. Responses should be self-describing

9. Use a consistent query syntax

10. Be consistent elsewhere

Page 22: Futureproofing REST APIs

3 ways • Path, e.g., http://api/v1/

• Headers, e.g., x-version: 1.0

• Query string, e.g., http://api?version=1.0

Page 23: Futureproofing REST APIs

PATH

Page 24: Futureproofing REST APIs

Versioning principles

• Version from the beginning

• Version both public and private APIs

Page 25: Futureproofing REST APIs

Additional consideration

s

• How long to keep a version around

• How to deprecate a version

• Whether to require clients to request version

• How to handle breaking changes if version is not required

• Provide a –pre (or similar) suffix

Page 26: Futureproofing REST APIs

Before & after

Page 27: Futureproofing REST APIs

1. Policies

2. Versioning

3.Headers4. Pagination

5. Separate data & metadata

6. Don’t mint mime types

7. Wrap arrays & primitives

8. Responses should be self-describing

9. Use a consistent query syntax

10. Be consistent elsewhere

Page 28: Futureproofing REST APIs

HEADERS ARE ONLY FOR DATA CONCEPTUALLY

SCOPED TO THE REQUEST OR RESPONSE

Rule of thumb

Page 29: Futureproofing REST APIs

Pagination example

Page 30: Futureproofing REST APIs

WHAT HAPPENS WHEN YOU WANT TO RETURN

MULTIPLE COLLECTIONS?

Page 31: Futureproofing REST APIs

Options • Cram links header with many links

• Force SELECT (N+1) requests

Page 32: Futureproofing REST APIs

Etag example

Page 33: Futureproofing REST APIs

Collection Questions

• Can you guarantee an etag for the collection?

• Where do etags for the embedded resources go?

Page 34: Futureproofing REST APIs

CONSIDER HEADER USAGE ON CASE-BY-

CASE BASIS

Page 35: Futureproofing REST APIs

Before & after

Page 36: Futureproofing REST APIs

1. Policies

2. Versioning

3. Headers

4.Pagination5. Separate data & metadata

6. Don’t mint mime types

7. Wrap arrays & primitives

8. Responses should be self-describing

9. Use a consistent query syntax

10. Be consistent elsewhere

Page 37: Futureproofing REST APIs

2 TYPES

Page 38: Futureproofing REST APIs

Client-driven

pagination

• Client-driven: skip, take, top, etc

• Client-initiation keeps this from being a breaking change

Page 39: Futureproofing REST APIs

Server-driven paging

• Data has a tendency to grow

• Server should force pagination to prevent DoS

• Clients should always be prepared for paginated collections

Page 40: Futureproofing REST APIs

Server-driven paging

guidance

• Tell clients to expect that any collection may be paginated

• Decide what a continuation token is (opaque string? URL?)

• Put continuation tokens in response body

Page 41: Futureproofing REST APIs

Server-driven paging

examples

HAL

Siren

Page 42: Futureproofing REST APIs

Server-driven paging

examples

OData

Page 43: Futureproofing REST APIs

Before & after

Page 44: Futureproofing REST APIs

1. Policies

2. Versioning

3. Headers

4. Pagination

5.Separate data & metadata6. Don’t mint mime types

7. Wrap arrays & primitives

8. Responses should be self-describing

9. Use a consistent query syntax

10. Be consistent elsewhere

Page 45: Futureproofing REST APIs

Ambiguity is bad

• Forcing consumers to the docs is bad

• Ambiguity limits extensibility

Page 46: Futureproofing REST APIs

Metadata examples

HAL

Siren

Page 47: Futureproofing REST APIs

Metadata examples

JSON-LD

OData

Page 48: Futureproofing REST APIs

OData annotation

s

Target parent

Target sibling

Page 49: Futureproofing REST APIs

Before & after

Page 50: Futureproofing REST APIs

1. Policies

2. Versioning

3. Headers

4. Pagination

5. Separate data & metadata

6.Don’t mint mime types7. Wrap arrays & primitives

8. Responses should be self-describing

9. Use a consistent query syntax

10. Be consistent elsewhere

Page 51: Futureproofing REST APIs

TO MINT OR NOT TO MINT?

Page 52: Futureproofing REST APIs

NEW MIME TYPES OFTEN LEAD TO

BREAKING CHANGES

Page 53: Futureproofing REST APIs

Introducing new

resource types

• HAL: doesn’t care about type

• Siren: doesn’t care about type

• OData: describes type when necessary

• JSON-LD: always describes type

Page 54: Futureproofing REST APIs

Best of both

worlds

• Unambiguous typing

• No media type minting

Page 55: Futureproofing REST APIs

Before & after

• Still looking for a real-world example of minting

Page 56: Futureproofing REST APIs

1. Policies

2. Versioning

3. Headers

4. Pagination

5. Separate data & metadata

6. Don’t mint mime types

7.Wrap arrays & primitives8. Responses should be self-describing

9. Use a consistent query syntax

10. Be consistent elsewhere

Page 57: Futureproofing REST APIs

JSON ALLOWS ROOT-LEVEL

ARRAYS, BUT…

Page 58: Futureproofing REST APIs

WHERE DOES METADATA GO?

Page 59: Futureproofing REST APIs

Array metadata

• Pagination links

• Count

• Self-link

• Type information

Page 60: Futureproofing REST APIs

Also wrap primitives

Page 61: Futureproofing REST APIs

Before & after

Page 62: Futureproofing REST APIs

1. Policies

2. Versioning

3. Headers

4. Pagination

5. Separate data & metadata

6. Don’t mint mime types

7. Wrap arrays & primitives

8.Responses should be self-describing

9. Use a consistent query syntax

10. Be consistent elsewhere

Page 63: Futureproofing REST APIs

Self-description is a REST

constraint

Page 64: Futureproofing REST APIs

CLIENT ONLY NEEDS THE RESPONSE TO

INTERPRET THE RESPONSE

Page 65: Futureproofing REST APIs

Means of self-

describing

JSON-LD

OData

Page 66: Futureproofing REST APIs

Github Webhooks

Page 67: Futureproofing REST APIs

Scenarios • Webhooks

• Push notifications

• Asynchronous clients

• Intermediary processing

Page 68: Futureproofing REST APIs

Before & after

Page 69: Futureproofing REST APIs

1. Policies

2. Versioning

3. Headers

4. Pagination

5. Separate data & metadata

6. Don’t mint mime types

7. Wrap arrays & primitives

8. Responses should be self-describing

9.Use a consistent query syntax10. Be consistent elsewhere

Page 70: Futureproofing REST APIs

Twitter

Page 71: Futureproofing REST APIs

Facebook

Page 72: Futureproofing REST APIs

Appcelerator

Page 73: Futureproofing REST APIs

VERY LITTLE CONSISTENCY

ACROSS REST APIS

Page 74: Futureproofing REST APIs

WORSE, VERY LITTLE CONSISTENCY WITHIN

REST APIS

Page 75: Futureproofing REST APIs

Query needs

• Consistent

• Robust

• NOT original

Page 76: Futureproofing REST APIs

Two major options

Lucene

OData

Page 77: Futureproofing REST APIs

NO BONUS POINTS FOR ORIGINALITY

Page 78: Futureproofing REST APIs

1. Policies

2. Versioning

3. Headers

4. Pagination

5. Separate data & metadata

6. Don’t mint mime types

7. Wrap arrays & primitives

8. Responses should be self-describing

9. Use a consistent query syntax

10.Be consistent elsewhere

Page 79: Futureproofing REST APIs

CONSISTENCY DOESN’T JUST APPLY

TO QUERY SYNTAX

Page 80: Futureproofing REST APIs

Areas of consistenc

y

• Resource paths

• Serialization

• Deterministic responses

• Error codes

Page 81: Futureproofing REST APIs

First look: Model-first HTTP APIs

• Use Swagger API description format

• Use Swagger tooling

• Introduce new YAML syntax

• Resource model only

Page 82: Futureproofing REST APIs

DEMO

Page 83: Futureproofing REST APIs

3 takeaways

• Does not require HTTP expertise

• Builds consistent HTTP APIs

• Your input necessary

Page 84: Futureproofing REST APIs
Page 85: Futureproofing REST APIs

ODATA: WORST BRAND NAME

EVER!

Page 86: Futureproofing REST APIs

OData • OData can help any HTTP API

• Public standard (OASIS, ISO)

Page 87: Futureproofing REST APIs

Who?

Page 88: Futureproofing REST APIs

Enjoy the REST of

the conference

(har, har)

Page 89: Futureproofing REST APIs

Contact info

• Mark Stafford

• Microsoft

[email protected]

• http://www.odata.org