There is REST and then there is “REST”...Server JavaScript in browser Mobile application...

43
There is REST and then there is “REST” Radovan Semančík November 2017

Transcript of There is REST and then there is “REST”...Server JavaScript in browser Mobile application...

Page 1: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

There is REST and then there is “REST”

Radovan SemančíkNovember 2017

Page 2: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Who Am I?

Ing. Radovan Semančík, PhD.

Software Architect at Evolveum

Architect of Evolveum midPoint

Apache Foundation committer

Contributor to ConnId and Apache Directory API

Page 3: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

What is REST?

Server

JavaScript in browserMobile applicationEnterprise applicationCLI toolDesktop client…

Client HTTP

JSONrequest

Web applicationEnterprise application“Cloud” applicationWhatever as a Service“Serverless” server...

JSONresponse

Page 4: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

What is REST?

Server

JavaScript in browserMobile applicationEnterprise applicationCLI toolDesktop client…

Client HTTP

JSONrequest

JSONresponse

Web applicationEnterprise application“Cloud” applicationWhatever as a Service“Serverless” server...

This is simple, easy to understand … and wrong

Page 5: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Who Are You?

I knoweverythingaboutRESTalready

Whatever.I do not needto know REST

This may be interesting

Page 6: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Back to 1990s ...

● 1990: WWW invented

• Tim Berners-Lee

● 1996: HTTP 1.0

● 1997: HTTP 1.1

● 2000: Representational State Transfer

• Roy Fielding

● 2002: WWW Architecture

Page 7: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

REpresentational State Transfer

Page 8: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

REST (according to Fielding)

● Architectural style

• Not “architecture”, not “protocol”, not “API”

● Retro-architecture (HTTP 1.1)

● Transfer of resource representations• HTML page is a representation, HTTP is transfer protocol

● Architectural constraints

• Client-server, Stateless, Cache, Uniform interface, Layered

system, Code on demand

Page 9: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Representational State Transfer

Source: Fielding R.: Architectural Styles and the Design of Network-based Software Architectures, 2000

Page 10: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

So, what exactly is wrong?

ServerClient HTTP

JSONrequest

JSONresponse

Page 11: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

JSON? Not really ...

ServerClient HTTP

request

response

Page 12: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Request-response … kind of ...

ServerClient HTTP

URLresource

representationOperations (verbs):● GET● PUT● DELETE● POST● ...

Page 13: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

REST is not RPC

● Objects (resources) addressed by URL

● Fixed operation set (verbs)• GET, HEAD, PUT, DELETE, POST, OPTIONS, TRACE, CONNECT

● Stateless• Sessions are evil

● Hypertext• Thou shalt not construct your URLs

Page 14: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Theory and Practice

● Theory: REST is architectural style for

hypertext applications

● Practice: we really need RCP

● REST is not good for RPC

… but REST is popular, we want it …

● Result: RESTful APIs

Page 15: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

RESTful APIs

● Not really REST• Not hypertext

• Mostly resource-based, but still some RPC

● Not really API• Application Programming Interface

• Problematic interface definition (Swagger/OpenAPI?)

• Security? Reliability? Transactions?

● Read: “HTTP-based service”

Page 16: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Reinvented Wheel … again

● 1976: RFC 707

● 1981: Xerox Courier

● 1991: CORBA

● 1993: DCE/RPC → DCOM

● 1995: SunRPC

● 1998: SOAP

● 200x: “RESTful” API

Page 17: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Pure REST

● It is possible to implement pure REST service

… but it is going to be real pain (very likely)• Careful design and implementation

• Unnecessary complexity

• Too many network round-trips

• Expect poor performance

● Hypertext is not suitable for everything

Page 18: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

What do we do?

● Keep the parts that fit• Resources, URLs, representations

• GET, DELETE, POST, …

● Ditch the parts that do not fit• Hypertext

● Compromises• Statelessness, caching, security, consistency, ...

● Interface definition• Swagger/OpenAPI

Page 19: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Practical “REST”

● Try to stick to resources and representations• URL represents object (resource)

● GET as safe operation

● Use POST for RPC when needed• But do not abuse

● Define the interface• URL formats and meaning, data types, inputs/outputs, ...

Page 20: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Practical case study: midPoint

● MidPoint: comprehensive identity

management and governance system• Identity management, provisioning, role-based access control,

audit, workflow, entitlement management, password management,

policy management, segregation of duties, …

● 100% open source

● Started in 2011

● 700k lines of (mostly) Java code

Page 21: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

(things are a bitcomplex in this part)

MidPoint Services

midPoint

Self-serviceapplication

IDE

User

Developer

Customclient

Enterpriseintegration

Mobileapplication

Page 22: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

MidPoint “REST” service

● Development started in 2013

● Existing data model and operations• Object-based: User, Role, Organizational unit, Account, Task,

Security policy, ...

• CRUD … but there are exceptions

● “RESTful” part and RPC part

● Kept as close to REST ideas as was practical

Page 23: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

RESTful Operations: object

http://…./rest/users/02c15378­c48b­11e7­b010­1ff8606bae23

prefix type object identifier (OID)

Verbs:

● GET: read

● POST: modify

● DELETE: delete

● PUT: create (not recommended)

Page 24: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

How does object look like?

<user oid="02c15378­c48b­11e7­b010­1ff8606bae23">    <name>jack</name>    <description>Where's the rum?</description>    <fullName>Jack Sparrow</fullName>    <givenName>Jack</givenName>    <familyName>Sparrow</familyName>    <emailAddress>[email protected]</emailAddress>    <locality>Caribbean</locality>    <activation>     <administrativeStatus>enabled</administrativeStatus>    </activation></user>

{    "name" : "jack",    "fullName" : "Jack Sparrow",    "givenName" : "Jack",     "familyName" : "Sparrow",    …

XML

JSON

Page 25: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

RESTful Operations: collection

http://…./rest/users

prefix type

Verbs:

● GET: list objects, search (theoretically)

● POST: create new object

● DELETE: not supported

● PUT: not supported

Page 26: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Search

● Query language<q:equal>

    <q:path>name</q:path>

    <q:value>jack</q:value>

</q:equal>

● Search with POST: convenience

● Deviation from REST• Dedicated “search” resource for POST instead of GET

• Should return list of URLs, but returns objects (to save round trips)

http://…./rest/users/search

Page 27: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

HTTP verbs

● GET is safe

● POST used for many things

● DELETE deletes

● PUT is not very useful

● PATCH for modification• But POST works as well

● Typical “REST” API

Page 28: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Error Codes

● 1xx: Information. Stay tuned.

● 2xx: Success. All done.

● 3xx: Redirection or “in progress” (we will get to that)

● 4xx: Client error (e.g. bad request)

● 5xx: Server error (e.g. bug in server code)

Page 29: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Trivial, isn’t it? Now let’s have a look at the interesting stuff.

Page 30: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Asynchronous Operations

● Operations that take a looooong time (days)

● Cannot return success (2xx)

● Solution: redirects (3xx)

Page 31: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Asynchronous Operations

midPointclientModify user

POST …/users/12345

Redirect

302: …/tasks/87654

Start operation: create new task

Task87654

Check taskGET …/tasks/87654

200: <task>...</task>

Page 32: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

RPC Operations: object-related

http://…/rest/tasks/c68d7770­c493­11e7­bce6­9bec1fc3b57c/suspend

prefix type object identifier (OID)

Verbs:

● GET: not applicable

● POST: execute the operation

● DELETE: not applicable

● PUT: not applicable

op

Page 33: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Object-related RPC operations

● Deviation from pure REST• Should be modeled as resource changes or separate resources

… but that is a pain

● Error handling

Page 34: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

RPC Operations: global

http://…/rest/notifyChange

prefix

Verbs:

● GET: not applicable

● POST: execute the operation

● DELETE: not applicable

● PUT: not applicable

operation

Page 35: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Global RPC operations

● Complex input and output data structures

● Huge deviation from pure REST

● It was necessary to keep the interface simple

and efficient

Page 36: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Next: really hard problems ...

Page 37: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Security

● REST is stateless• Cookie-based sessions are out

● HTTP Basic authentication• Oh, really?

● OAuth2 / OpenID Connect• “best” practice for REST APIs

● JSON Web Tokens (JWT)• SAML tokens reinvented

Page 38: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Interface Definition

● Swagger / OpenAPI• WSDL reinvented (CORBA IDL reinvented)

• JSON-oriented

• Still quite limited, but at least something

● REST purists hate it• Because this goes directly against hypertext principles

Page 39: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Stub and Skeleton

Clientapp

t1.suspend()

Interface definition

stub

Server

suspend() {  …}

skeletonHTTPPOST

generatecode

generatecode

Does not really works in “RESTful” APIs (yet)

Page 40: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Transactions and Consistency

● Transactions (ACID)• Forget it

● Consistency• Cannot forget it, but it is tricky

• Optimistic locking / MVCC is best bet

• ETag (RFC7232) is a standard mechanism

• But a lot of proprietary mechanisms is used instead

Page 41: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Conclusion

● REST is no good for RPC

… but we are going to use it anyway

● And it is practical

… with some tweaks and compromises

● Future?

Page 42: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Questions and Answers

Page 43: There is REST and then there is “REST”...Server JavaScript in browser Mobile application Enterprise application CLI tool Desktop client … Client HTTP JSON request JSON response

Radovan Semančík

www.evolveum.com

Thank You