Beautiful REST + JSON APIs

Slides:



Advertisements
Similar presentations
Give it a REST already Arnon Rotem-Gal-Oz VP R&D xsights
Advertisements

REST Vs. SOAP.
Introduction to Web Services
REST &.NET James Crisp.NET Practice Lead ThoughtWorks Australia.
Server Access The REST of the Story David Cleary
REST (Representational State Transfer)
Hypertext Transfer PROTOCOL ----HTTP Sen Wang CSE5232 Network Programming.
Building RESTful Interfaces
REST support for B2B access to your AppServer PUG Challenge Americas Michael Jacobs : Senior Software Architect Edsel Garcia : Principal Software.
Hypertext Transport Protocol CS Dick Steflik.
 What is it ? What is it ?  URI,URN,URL URI,URN,URL  HTTP – methods HTTP – methods  HTTP Request Packets HTTP Request Packets  HTTP Request Headers.
OAuth-as-a-service using ASP.NET Web API and Windows Azure Access Control Maarten
Designing and Implementing Web Data Services in Perl
Web Architecture & Services (2) Representational State Transfer (REST)
REST.  REST is an acronym standing for Representational State Transfer  A software architecture style for building scalable web services  Typically,
CollectionSpace Service REST-based APIs June 2009 Face-to-face Aron Roberts U.C. Berkeley IST/Data Services.
MINT Working Group Jan 9-10 at Harris FBC Melbourne, FL.
REST - Introduction Based on material from InfoQ.com (Stefan Tilkov) And slides from MindTouch.com (Steve Bjorg) 1.
Introduction to the SharePoint 2013 REST API. 2 About Me SharePoint Solutions Architect at Sparkhound in Baton Rouge
A Limited Definition of Web Services Paul Kopacz CIS* Service Oriented Architecture Instructor: Qusay H. Mahmoud, Ph.D. February.
1 Seminar on Service Oriented Architecture Principles of REST.
Google Data Protocol Guy Mark Lifshitz. Motivation Google’s Mission: – Organize the world’s information – Make information universally accessible – Provide.
API Crash Course CWU Startup Club. OUTLINE What is an API? Why are API’s useful? What is HTTP? JSON? XML? What is a RESTful API? How do we consume an.
ICM – API Server & Forms Gary Ratcliffe.
RESTful Web Services What is RESTful?
REST By: Vishwanath Vineet.
Web Technologies Lecture 10 Web services. From W3C – A software system designed to support interoperable machine-to-machine interaction over a network.
Keith Telle Lead Software Engineer Bit Wizards REST, WebAPI 2, and Best Practices.
Web Protocols: HTTP COMP6017 Topics on Web Services Dr Nicholas Gibbins –
Representational State Transfer COMP6017 Topics on Web Services Dr Nicholas Gibbins –
REST API Design. Application API API = Application Programming Interface APIs expose functionality of an application or service that exists independently.
National College of Science & Information Technology.
Hypertext Transfer Protocol (HTTP) COMP6218 Web Architecture Dr Nicholas Gibbins –
Gridpp37 – 31/08/2016 George Ryall David Meredith
Hypertext Transfer Protocol
How HTTP Works Made by Manish Kushwaha.
Azure Identity Premier Fast Start
IoT Integration Patterns, REST, and CoAP
API (Application Program Interface)
Hypertext Transfer Protocol
Content from Python Docs.
Better RESTFul API – Best Practices
OpenInsight as a REST API Engine
Node.js Express Web Applications
HTTP request message: general format
API Security Auditing Be Aware,Be Safe
Data Virtualization Tutorial… CORS and CIS
Node.js Express Web Services
C++ Tango REST API implementation
An introduction to REST for SharePoint 2013
Advanced Web-based Systems | Misbhauddin
Hypertext Transfer Protocol
Hypertext Transport Protocol
What is REST API ? A REST (Representational State Transfer) Server simply provides access to resources and the REST client accesses and presents the.
Client / Session Identification Cookies
Representational State Transfer
Addressing the Beast: Single Sign-On II
Ashish Pandit IT Architect, Middleware & Integration Services
Testing REST IPA using POSTMAN
Ben Burbridge, Rebecca Jones, Hilary Newman Product Development
WEB API.
Windows Azure Keenan Newton 3-021
$, $$, $$$ API testing Edition
Hypertext Transfer Protocol
WebDAV Design Overview
Building production-ready APIs with ASP.NET Core 2.2
Web-Services and RESTful APIs
Chengyu Sun California State University, Los Angeles
REST API Design Borrowed heavily from:
Restful APIs 101 Laura
Presentation transcript:

Beautiful REST + JSON APIs Les Hazlewood @lhazlewood CTO, Stormpath stormpath.com

.com Identity Management and Access Control API Security for your applications User security workflows Security best practices Developer tools, SDKs, libraries

Outline APIs, REST & JSON REST Fundamentals Design Base URL Errors Versioning Resource Format Return Values Content Negotiation References (Linking) Pagination Query Parameters Associations Errors IDs Method Overloading Resource Expansion Partial Responses Caching & Etags Security Multi Tenancy Maintenance

APIs Applications Developers Pragmatism over Ideology Adoption Scale Learn more at Stormpath.com

Why REST? Scalability Generality Independence Latency (Caching) Security Encapsulation Learn more at Stormpath.com

Why JSON? Ubiquity Simplicity Readability Scalability Flexibility Learn more at Stormpath.com

HATEOAS Hypermedia As The Engine Of Application State Further restriction on REST architectures. Learn more at Stormpath.com

REST Is Easy Learn more at Stormpath.com

REST Is *&@#$! Hard (for providers) Learn more at Stormpath.com

REST can be easy (if you follow some guidelines) Learn more at Stormpath.com

Example Domain: Stormpath Applications Directories Accounts Groups Associations Workflows

Fundamentals Learn more at Stormpath.com

Resources Nouns, not Verbs Coarse Grained, not Fine Grained Architectural style for use-case scalability Learn more at Stormpath.com

What If? /getAccount /createDirectory /updateGroup /verifyAccountEmailAddress Learn more at Stormpath.com

What If? /getAccount /getAllAccounts /searchAccounts /createDirectory /createLdapDirectory /updateGroup /updateGroupName /findGroupsByDirectory /searchGroupsByName /verifyAccountEmailAddress /verifyAccountEmailAddressByToken … Smells like bad RPC. DON’T DO THIS. Learn more at Stormpath.com

Keep It Simple Learn more at Stormpath.com

The Answer Fundamentally two types of resources: Collection Resource Instance Resource Learn more at Stormpath.com

Collection Resource /applications Learn more at Stormpath.com

Instance Resource /applications/a1b2c3 Learn more at Stormpath.com

Behavior GET PUT POST DELETE HEAD Learn more at Stormpath.com

Create, Read, Update, Delete Behavior POST, GET, PUT, DELETE ≠ 1:1 Create, Read, Update, Delete Learn more at Stormpath.com

Behavior As you would expect: GET = Read DELETE = Delete HEAD = Headers, no Body Learn more at Stormpath.com

Behavior Not so obvious: PUT and POST can both be used for Create and Update Learn more at Stormpath.com

PUT for Create Identifier is known by the client: PUT /applications/clientSpecifiedId { … } Learn more at Stormpath.com

PUT for Update Full Replacement PUT /applications/existingId { “name”: “Best App Ever”, “description”: “Awesomeness” } Learn more at Stormpath.com

PUT Idempotent Learn more at Stormpath.com

POST as Create On a parent resource POST /applications { “name”: “Best App Ever” } Response: 201 Created Location: https://api.stormpath.com/applications/a1b2c3 Learn more at Stormpath.com

POST as Update On instance resource POST /applications/a1b2c3 { “name”: “Best App Ever. Srsly.” } Response: 200 OK Learn more at Stormpath.com

POST NOT Idempotent Learn more at Stormpath.com

Media Types Format Specification + Parsing Rules Request: Accept header Response: Content-Type header application/json application/foo+json application/foo+json;application … Learn more at Stormpath.com

Design Time! Learn more at Stormpath.com

Base URL Learn more at Stormpath.com

http(s)://api.foo.com vs http://www.foo.com/dev/service/api/rest Learn more at Stormpath.com

http(s)://api.foo.com Rest Client vs Browser Learn more at Stormpath.com

Versioning Learn more at Stormpath.com

URL https://api. stormpath. com/v1 vs URL https://api.stormpath.com/v1 vs. Media-Type application/foo+json;application&v=1 Learn more at Stormpath.com

Resource Format Learn more at Stormpath.com

Media Type Content-Type: application/json When time allows: application/foo+json application/foo+json;bar=baz&v=1 … Learn more at Stormpath.com

camelCase ‘JS’ in ‘JSON’ = JavaScript myArray.forEach Not myArray.for_each account.givenName Not account.given_name Underscores for property/function names are unconventional for JS. Stay consistent. Learn more at Stormpath.com

Date/Time/Timestamp There’s already a standard. Use it: ISO 8601 Example: { …, “createdTimestamp”: “2012-07-10T18:02:24.343Z” } Use UTC! Learn more at Stormpath.com

Response Body Learn more at Stormpath.com

Return the representation in the response when feasible. GET obvious What about POST? Return the representation in the response when feasible. Add override (?_body=false) for control Learn more at Stormpath.com

Content Negotiation Learn more at Stormpath.com

Header Accept header Header values comma delimited in order of preference GET /applications/a1b2c3 Accept: application/json, text/plain Learn more at Stormpath.com

Resource Extension /applications/a1b2c3.json /applications/a1b2c3.csv … Conventionally overrides Accept header Learn more at Stormpath.com

HREF Distributed Hypermedia is paramount! Every accessible Resource has a canonical unique URL Replaces IDs (IDs exist, but are opaque). Critical for linking, as we’ll soon see Learn more at Stormpath.com

Instance w/ HREF (v1) GET /accounts/x7y8z9 Learn more at Stormpath.com 200 OK { “href”: “https://api.stormpath.com/v1/accounts/x7y8z9”, “givenName”: “Tony”, “surname”: “Stark”, ... } Learn more at Stormpath.com

Resource References aka ‘Linking’ (v1) Learn more at Stormpath.com

Hypermedia is paramount. Linking is fundamental to scalability. Tricky in JSON XML has it (XLink), JSON doesn’t How do we do it? Learn more at Stormpath.com

Instance Reference (v1) GET /accounts/x7y8z9 200 OK { “href”: “https://api.stormpath.com/v1/accounts/x7y8z9”, “givenName”: “Tony”, “surname”: “Stark”, …, “directory”: ???? } Learn more at Stormpath.com

Instance Reference (v1) GET /accounts/x7y8z9 200 OK { “href”: “https://api.stormpath.com/v1/accounts/x7y8z9”, “givenName”: “Tony”, “surname”: “Stark”, …, “directory”: { “href”: “https://api.stormpath.com/v1/directories/g4h5i6” } Learn more at Stormpath.com

Collection Reference (v1) GET /accounts/x7y8z9 200 OK { “href”: “https://api.stormpath.com/v1/accounts/x7y8z9”, “givenName”: “Tony”, “surname”: “Stark”, …, “groups”: { “href”: “https://api.stormpath.com/v1/accounts/x7y8z9/groups” } Learn more at Stormpath.com

Linking v2 (recommended) Learn more at Stormpath.com

Instance HREF (v2) GET /accounts/x7y8z9 Learn more at Stormpath.com 200 OK { “meta”: { “href”: “https://api.stormpath.com/v1/accounts/x7y8z9”, “mediaType”: “application/ion+json;version=2&schema=...” }, “givenName”: “Tony”, “surname”: “Stark”, … } Learn more at Stormpath.com

Instance Reference (v2) GET /accounts/x7y8z9 200 OK { “meta”: { ... }, “givenName”: “Tony”, “surname”: “Stark”, …, “directory”: { “meta”: { “href”: “https://api.stormpath.com/v1/directories/g4h5i6” “mediaType”: “application/ion+json;version=2&schema=...” } Learn more at Stormpath.com

Collection Reference (v2) GET /accounts/x7y8z9 200 OK { “meta”: { ... }, “givenName”: “Tony”, “surname”: “Stark”, …, “groups”: { “meta”: { “href”: “https://api.stormpath.com/v1/accounts/x7y8z9/groups”, “mediaType”: “application/ioncoll+json;version=2&schema=...” } Learn more at Stormpath.com

Reference Expansion (aka Entity Expansion, Link Expansion) Learn more at Stormpath.com

Account and its Directory? Learn more at Stormpath.com

GET /accounts/x7y8z9?expand=directory 200 OK { “meta”: {...}, “givenName”: “Tony”, “surname”: “Stark”, …, “directory”: { “meta”: { ... }, “name”: “Avengers”, “description”: “Hollywood’s hope for more $”, “creationDate”: “2012-07-01T14:22:18.029Z”, … } Learn more at Stormpath.com

Partial Representations Learn more at Stormpath.com

GET /accounts/x7y8z9?fields=givenName,surname,directory(name) Learn more at Stormpath.com

Pagination Learn more at Stormpath.com

Collection Resource supports query params: Offset Limit …/applications?offset=50&limit=25 Learn more at Stormpath.com

GET /accounts/x7y8z9/groups 200 OK { “meta”: { ... }, “offset”: 0, “limit”: 25, “first”: { “meta”:{“href”: “…/accounts/x7y8z9/groups?offset=0”}}, “previous”: null, “next”: { “meta”:{“href”: “…/accounts/x7y8z9/groups?offset=25”}}, “last”: { “meta”:{“href”: “…”}}, “items”: [ “meta”: { “href”: “…”, ...} }, … ] } Learn more at Stormpath.com

Many To Many Learn more at Stormpath.com

Group to Account A group can have many accounts An account can be in many groups Each mapping is a resource: GroupMembership Learn more at Stormpath.com

GET /groupMemberships/23lk3j2j3 200 OK { “meta”:{“href”: “…/groupMemberships/23lk3j2j3”}, “account”: { “meta”:{“href”: “…”} }, “group”: { “meta”{“href”: “…”} … } Learn more at Stormpath.com

GET /accounts/x7y8z9 Learn more at Stormpath.com 200 OK { “meta”:{“href”: “…/accounts/x7y8z9”}, “givenName”: “Tony”, “surname”: “Stark”, …, “groups”: { “meta”:{“href”: “…/accounts/x7y8z9/groups”} }, “groupMemberships”: { “meta”:{“href”: “…/groupMemberships?accountId=x7y8z9”} } Learn more at Stormpath.com

Errors Learn more at Stormpath.com

As descriptive as possible As much information as possible Developers are your customers Learn more at Stormpath.com

POST /directories 409 Conflict { “status”: 409, “code”: 40924, “property”: “name”, “message”: “A Directory named ‘Avengers’ already exists.”, “developerMessage”: “A directory named ‘Avengers’ already exists. If you have a stale local cache, please expire it now.”, “moreInfo”: “https://www.stormpath.com/docs/api/errors/40924” } Learn more at Stormpath.com

Security Learn more at Stormpath.com

Learn more at Stormpath.com Avoid sessions when possible Authenticate every request if necessary Stateless Authorize based on resource content, NOT URL! Use Existing Protocol: Oauth 1.0a, Oauth2, Basic over SSL only Custom Authentication Scheme: Only if you provide client code / SDK Only if you really, really know what you’re doing Use API Keys instead of Username/Passwords Learn more at Stormpath.com

401 vs 403 401 “Unauthorized” really means Unauthenticated “You need valid credentials for me to respond to this request” 403 “Forbidden” really means Unauthorized “I understood your credentials, but so sorry, you’re not allowed!” Learn more at Stormpath.com

HTTP Authentication Schemes Server response to issue challenge: WWW-Authenticate: <scheme name> realm=“Application Name” Client request to submit credentials: Authorization: <scheme name> <data> Learn more at Stormpath.com

API Keys Entropy Password Reset Independence Speed Limited Exposure Traceability Learn more at Stormpath.com

IDs Learn more at Stormpath.com

Should be globally unique IDs should be opaque Should be globally unique Avoid sequential numbers (contention, fusking) Good candidates: UUIDs, ‘Url64’ Learn more at Stormpath.com

HTTP Method Overrides Learn more at Stormpath.com

POST /accounts/x7y8z9?_method=DELETE Learn more at Stormpath.com

Caching & Concurrency Control Learn more at Stormpath.com

Server (initial response): Server (initial response): ETag: "686897696a7c876b7e” Client (later request): If-None-Match: "686897696a7c876b7e” Server (later response): 304 Not Modified Learn more at Stormpath.com

Maintenance Learn more at Stormpath.com

Use HTTP Redirects Create abstraction layer / endpoints when migrating Use well defined custom Media Types Learn more at Stormpath.com

Sign Up Now: Stormpath.com Free for developers Eliminate months of development Automatic security best practices Sign Up Now: Stormpath.com