35
REST 11.07.2009 Understanding and Using RESTful APIs

Unerstanding and Using RESTful APIs

Embed Size (px)

DESCRIPTION

KeyLimeTie CTO Peter Morano provides an overview of using REST to architect applications. Complete with examples and resources.

Citation preview

Page 1: Unerstanding and Using RESTful APIs

REST 11.07.2009Understanding and Using RESTful APIs

Page 2: Unerstanding and Using RESTful APIs

About Me

•Developing software for 15 years

•Bulk of experience on enterprise-scale applications

•Microsoft platform (mostly)

•CTO at KeyLimeTie

Page 3: Unerstanding and Using RESTful APIs

Overview

Although REST is 10 years old, APIs based on the REST model continue to become an integral part of the technology landscape.

ProgrammableWeb.com lists 1051 RESTful APIs, 4422 Mashups.

We’ll look at

• Basic REST Concepts• Using REST• WADL

Page 4: Unerstanding and Using RESTful APIs

REST OVERVIEW

Page 5: Unerstanding and Using RESTful APIs

REST Overview

APIs based on the Representational State Transfer (REST) architecture dominate web 2.0 mash-up and social media development.

There are a number of frameworks available that support the development of RESTful APIs (WCF, JAX-RS, Wicket, Zend)

We are still missing an established mechanism for documentation and discovery and client-side tool support for rapid implementation.

Page 6: Unerstanding and Using RESTful APIs

REST Overview

REST-style architectures consist of clients and servers.

Requests and responses are built around the transfer of "representations" of "resources".

A resource is any entity that can be addressed in a URI (an account, an employee, a physical file)

A representation of a resource is any information that captures the current or intended state of a resource. All information necessary to process and complete a request is contained within it.

Page 7: Unerstanding and Using RESTful APIs

Example

Resource (Twitter Public Timeline)http://twitter.com/statuses/public_timeline.xml

Resource (Twitter User) http://twitter.com/users/petermorano.xml

Page 8: Unerstanding and Using RESTful APIs

REST Overview

REST-style architectures consist of clients and servers.

Requests and responses are built around the transfer of "representations" of "resources".

A resource is any entity that can be addressed in a URI (an account, an employee, a physical file)

A representation of a resource is any information that captures the current or intended state of a resource. All information necessary to process and complete a request is contained within it.

Page 9: Unerstanding and Using RESTful APIs

Wait a Minute

If this sounds a lot like the Web, it’s because the Web is a REST

implementation

Roy Fielding, who created REST, was also one of the principal authors of HTTP

Page 10: Unerstanding and Using RESTful APIs

REST Basics

The REST architecture was designed around a few key principles:

• Use HTTP Methods (and Response Codes)

• Be Stateless and Cacheable

• Use Addressable Resources

• Support the transfer of Representations

Page 11: Unerstanding and Using RESTful APIs

REST Basics

The REST architecture was designed around a few key principles:

• Use HTTP Methods (and Response Codes)

• Be Stateless and Cacheable

• Use Addressable Resources

• Support the transfer of Representations

Page 12: Unerstanding and Using RESTful APIs

HTTP Methods

Action

Create

Read

Update

Delete

SQL

Insert

Select

Update

Delete

HTTP Method

Post

Get

Put

Delete

Map to CRUD Operations

Page 13: Unerstanding and Using RESTful APIs

URI Examples

GET

GET

POST

PUT

http://www.example.com/v1/employeeshttp://www.example.com/v1/employees/1824http://www.example.com/v1/employeeshttp://www.example.com/v1/employees/1513

DELETEhttp://www.example.com/v1/employees/1222

Page 14: Unerstanding and Using RESTful APIs

REST Basics

The REST architecture was designed around a few key principles:

• Use HTTP Methods (and Response Codes)

• Be Stateless and Cacheable

• Use Addressable Resources

• Support the transfer of Representations

Page 15: Unerstanding and Using RESTful APIs

Stateless and Cacheable

• No State Stored on the Server

• Every HTTP request executes in complete isolation on the server

• Easier to scale because of GET method

Page 16: Unerstanding and Using RESTful APIs

REST Basics

The REST architecture was designed around a few key principles:

• Use HTTP Methods (and Response Codes)

• Be Stateless and Cacheable

• Use Addressable Resources

• Support the transfer of Representations

Page 17: Unerstanding and Using RESTful APIs

Resources

• Any THING – person, concept, artifact

• Anything you can point to

• Explicit Request and Response – No State

Page 18: Unerstanding and Using RESTful APIs

REST Basics

The REST architecture was designed around a few key principles:

• Use HTTP Methods (and Response Codes)

• Be Stateless and Cacheable

• Addressable Resources

• Support the transfer of Representations

Page 19: Unerstanding and Using RESTful APIs

Representations

• A serializable description of a Resource

• Resources are modifiable through Representations

• XML

• JSON

• Binary Formats (jpg, gif, mpeg)

Page 20: Unerstanding and Using RESTful APIs

REST Pros

Cacheable

Scalable

Different Representations

Human Readable Results

Lightweight

Page 21: Unerstanding and Using RESTful APIs

REST Cons

Must be HTTP

No Atomic Transactions

No Standards for security except HTTPS

No Standardized Discovery

Page 22: Unerstanding and Using RESTful APIs

REST vs. SOAP

Unlike SOAP-based web services, there is no "official" standard for RESTful web service.

Though not a standard, a RESTful implementation uses standards like HTTP, URL, XML, GIF, etc.

Page 23: Unerstanding and Using RESTful APIs

REST vs. SOAP

SOAP

•Protocol

•Verbose Payload• Envelope

•Not easily readable

•Requires Development Tools

•Uses POST Method

•Supports Transactions

•Standardized Discovery• WSDL

REST

•Architecture

•Lightweight Payload• Postcard

•Human Readable Results

•Easy to work with, Brower-testable

•GET Method supports caching

•No Transactions

•Discovery• WADL?

Page 24: Unerstanding and Using RESTful APIs

USING REST

Page 25: Unerstanding and Using RESTful APIs

USING REST

Think of a REST API as another data source

Details depend on the language, but there are common steps:

• Serialize the representation (if PUT or POST)

• Format the URI (parameters, variables)

• Encode Authentication into the Header

• Make Call

• Deserialize Response

Page 26: Unerstanding and Using RESTful APIs

USING REST

Three basic options for consuming RESTful APIs

• Use the service’s toolkit (not always available)

• Community-built library/wrapper• code.google.com• www.codeplex.com

• Code your own implementation

Page 27: Unerstanding and Using RESTful APIs

Community BuiltUpside

• Ready to use

Downside

• It’s not your code

• You may be waiting for the library to become available.

• Enterprise clients may have concerns

Page 28: Unerstanding and Using RESTful APIs

Writing Your OwnUpside

• You know the code

Downside

• Manual Discovery Process

• Implementation can be tedious

Page 29: Unerstanding and Using RESTful APIs

Tools

Tools for testing RESTful APIs

• Curl

• Eclipse Http4e

• Fiddler

• Your Browser (limited)

Page 30: Unerstanding and Using RESTful APIs

WADL

Page 31: Unerstanding and Using RESTful APIs

WADL

Web Application Discovery Language (WADL)

Created in 2005 by Marc Hadley of Sun Microsystems

Submitted to W3C on 8/31/2009

REST’s Answer to WSDL

Specification: https://wadl.dev.java.net/

Page 32: Unerstanding and Using RESTful APIs

What WADL Gets You

Reduced Errors in Discovery Opportunities for Code Generation

Spend more time building your application and less time implementing APIs

Page 33: Unerstanding and Using RESTful APIs

So Where Is It?

Currently in Submission to W3C API Providers are slow to adopt, so tool providers are slow to implement

There are projects underway to generate code from WADL, and at least one tool that helps in creating WADL

Page 34: Unerstanding and Using RESTful APIs

WADL DemoProject on code.google.com

Online Version: http://tomayac.de/rest-describe/latest/RestDescribe.html

Page 35: Unerstanding and Using RESTful APIs

Referenceshttp://en.wikipedia.org/wiki/Representational_State_Transfer

http://weblogs.java.net/blog/mhadley/archive/2005/05/introducing_wad.html

http://blog.tomayac.de/index.php?date=2007-05-23

RESTful Web Services – Richardson, Ruby – O’Reilly Press