Академический Документы
Профессиональный Документы
Культура Документы
RESTful Services
The Web as Database
REST, at its core, is very simple:
− You can treat the web (or at least your part of
the web) like a database, adding, modifying,
deleting and retrieving documents as if they
were records in a data table.
− RESTful Services extends that concept by
rethinking the notion of documents in a
dynamic environment
RESTful Services: 10,000 ft.
Replace processoriented services with
resourceoriented ones
Treat server as addressable database with
HTTP methods as CRUD operations
Associate multiple presentation & input faces
with each HTTP operations
Bind each face with a pipeline operation.
RESTful Services Maturing
Roy Fieldings REST dissertation in 1999
SOAP/WSDL services rise and fall (20002008)
Blogging pushes syndication, social networks
and streaming data (2003)
AJAX (2004) and Ruby (2005)
Rise of Xquery and conformant XML databases
(2007)
03/06/09 An Introduction to RESTful Services 4
Why RESTful Services?
RESTful Services are becoming important:
− Rise of Rich Data (XBRL, NIEM IEPDs,
Data.gov, S1000D, GML, etc.)
− Cloud computing favors RESTful architectures
− WS APIs cloud analog to vendor lockin
− Clientside componentization favors RESTful
services.
− Data models are trumping process models
03/06/09 An Introduction to RESTful Services 5
Advantages of RESTful Services
RESTful services reduce siloization by
eliminating process coupling
RESTful services work well with pipeline
architectures (Xproc) and queries (XQuery)
RESTful services can also integrate cleanly with
traditional SOA through those pipelines
RESTful services reduces API complexity
Methodologies only now emerging
No formal discovery mechanism for RESTful
Services.
Works best with URL Rewrite capability
RESTful clients such as Xforms are, while
maturing, not yet mature
Few product offerings (as yet)
03/06/09 An Introduction to RESTful Services 7
RESTful Service Implementations
XqueryXProc/REST/XForms (XRX)
Drupal (PHP)
Atom Publishing Protocol (AtomPub)
Ruby on Rails (limited)
Others
There are no “things” no physical objects on
the Internet.
•
There are, however, abstractions of things.
These abstractions are known as resources.
Resources are fundamental to REST and
RESTful services.
Have an explicit or implicit model (schema)
Are made of property values and containers in
that model (instance)
Have an address (addressable)
Are unique (nonfungible)
Have one or more presentation or input faces
(representational)
03/06/09 An Introduction to RESTful Services 10
Representations
A resource is an abstraction. A representation
provides a “face” for the abstraction.
Some faces are visual: HTML tables, lists,
records, pages, edit forms, pdfs, etc.
Some faces are more abstract: raw xml, json, csv
All resources can have one or more
representations
Print
Rendered Video Formatted
Audio
Graphics (PDF)
<employee>
<firstname>
Jane Doe is an Jane
employee who </firstname>
works at ACME <lastname>
Widget Works as
a Senior Design Jane Doe, Doe
Bitmap
</lastname>
Engineer. He’s <title>
been here Engineer Engineer
Images nce 1995 .... </title>
(resource)
XML
Text Description
Static vs. Dynamic
In the “early” web, resources were static: They
did not change significantly over time (Web pages)
Contemporary resources are dynamic: They are
time dependent, or streaming. (Twitter)
Thus, at any given time, a representation is a
“snapshot” of that resource at that time it was
requested – it's the state of the resource.
A RESTful Service is a web service which
− Is resource rather than process oriented
− Creates, reads, updates and/or deletes
resources, collections or views
− Uses a single (base) URL for all of these
operations on any given resource
− Provides one or more faces (representations)
for these operations
A collection is a set of resources
Collections are also resources:
− They can be represented by a URL
− They can have one or more representations
RSS feeds, index pages, HTML tables & lists,
etc.
While a directory is a collection, collections can
be created from arbitrary criteria
03/06/09 An Introduction to RESTful Services 16
Collections & HTTP
The primary HTTP 1.1 “methods” are all
collection operations:
− POST adds a new resource to a collection
− GET retrieves a resource from a collection
− PUT replaces a resource within a collection
− DELETE removes a resource from a collection
HTTP Operations ~ Database Table Operations
Database CRUD HTTP Methods
Create POST
Read GET
Update PUT
Delete Delete
A collection is analogous to a database table
− Each resource is analogous to a record or row
− REST operations are set operations
− While not required, most resources in a
collection share a common data model
− Note that both relational tables and REST
collections do not maintain session state*
* In theory. In practice, most RESTful systems maintain caches.
A collection can be an existing set of resources
A collection can also be the result of a query
that filters the result by multiple criteria
Such collections are views, analogous to
database SQL Views.
Views are also resources, and can have URLs
Changing Sort Order
Changing Keys
Establishing Subset
Virtual resources
Postprocessing
Formal vs. Virtual Resources
A formal resource is one where it's internal
representation matches its external
representation.
A virtual resource is one where the external
representation differs from the internal,
requiring processing.
Formal resources can be managed via virtual
resource processors (important!)
03/06/09 An Introduction to RESTful Services 22
RESTful Services vs. Web Services
Comparison of services
Web Services RESTful Services
Process or API Oriented (Imperative) Resource Oriented (Declarative)
State is managed by session State is contained within resource
Application Data Model is implicit Application Data model is explicit
Mediated by SOAP Mediated by Appropriate Face
Works poorly on the web Works well on the web
Presentation limited part of services Presentation intrinsic to services
Heavy-weight, works well in heavy Light-weight, good 90% solution
enterprise services
Typically each HTTP method and face is
associated with a pipeline
Output pipelines perform queries, sorts views,
paginates and generate faces
Input pipelines perform validation, rectifies
faces, updates content
Error handling updates Fault Collection
Pipelines Reference & Write To Views
If Error
<foo/> Occurs <foo/>
<foo/>
<foo/>
<foo/>
<foo/>
<foo/> parse <foo/>
<foo/>
<err/>
<foo/> validate
<foo/>
<foo/>
<foo/>
<qry/>
render
/foo/query view
Syndication (news feeds) are classical RESTful
applications. Feeds are faces of views or collections.
User subscribes to view showing newest
content for that view
Links in syndication feeds can then open up
individual resources in the appropriate face
Model also works well with social network
apps – Twitter, Facebook, etc.
03/06/09 An Introduction to RESTful Services 28
RESTful Services & Queries
Depending on implementation, queries can be
simple, complex or both
Complex queries are uploaded as resources that
can in turn define new views
Simple queries are passed as a query string
parameter, then expanded in pipeline
Simple queries are synonymous with “Search”
A key is a hashed string that, when resolved,
will uniquely identify a given document in the
context of the collection.
Keys can also resolve to subdocuments, such
that the key identifies both the document and
the subdocument.
Each collection or view will have a different key
mechanism.
03/06/09 An Introduction to RESTful Services 30
Keys & Subdocuments
For instance, in the resource URL:
/docs/airframes//787.doc.html
the key “787” defines which airframe document is referenced for the
collection airframes
However, in /doc/airframe/leftwingassembly//787engassbly.doc.html
the key “787engassbly” will be parsed to retrieve both the airframe model
“787” and the subsection “engassbly” (engine assembly).
Subdocuments make it possible to view, edit and validate relevant sub
portions of a large document without having to send the entire document to
the client.
Subdocuments may also contain additional “processing” metadata before
being sent to the client that will be removed upon submission.
Any RESTful service that exposes a POST
method should include a template document
for each POST (input) face
A template is a “baseline” document that is
used to create new documents of the given
schema.
Such a template should use a string like “new”,
“default” or the like for its primary key.
03/06/09 An Introduction to RESTful Services 32
RESTful Clients
RESTful clients work with resource instances
rendered in the appropriate face
Interactive RESTful clients maintain integrity of
the state instance when sending to server
Faces may be embedded in one another (such
as through XInclude or AJAX)
Examples: XForms, AJAX components
Managing Editor, XMLToday.org
Contributing Editor, O'Reilly Media
Chief Architect, Metaphorical Web, Inc.
Available for Consulting and Hire
(kurt.cagle@gmail.com)