A Look at OData

20
A LOOK AT ODATA Chris Woodruff

Transcript of A Look at OData

A LOOK AT ODATAChris Woodruff

Chris Woodruff

[email protected]

SKYPEcwoodruff

TWITTER@cwoodruff

PHONE616.724.6885

Open Data Protocol (OData)

What is OData?

•OData is an Web Standard and Protocol that defines the best practice for building and consuming RESTful APIs.

•OData helps you focus on your business logic while building RESTful APIs without having to worry about the approaches to define your APIs.

•OData RESTful APIs are easy to consume.

What is a Protocol in the Internet World?

Some Basics about HTTP

Resources These are the URLs you use to get to pages on the web

Request HeadersThese are additional instructions that are sent with the request. These might define what type of response is required or authorization details.

Request VerbsThese describe what you want to do with the resource. A browser typically issues a GET verb to instruct the endpoint it wants to get data, however there are many other verbs available including things like POST, PUT and DELETE.

Request BodyData that is sent with the request. For example a POST (creation of a new item) will required some data which is typically sent as the request body in the format of JSON or XML.

Response BodyThis is the main body of the response. If the request was to a web server, this might be a full HTML page.

Response Status codesThese codes are issues with the response and give the client details on the status of the request.

Some Basics about HTTP - Verbs

GETRequests a representation of the specified resource. Requests using GET should only retrieve data and should have no other effect.

POSTRequests that the server accept the entity enclosed in the request as a new subordinate of the web resource identified by the URI.

PUTRequests that the enclosed entity be stored under the supplied URI.

DELETEDeletes the specified resource.

Some Basics about HTTP – Response Codes1XXRequest received, continuing process.

This class of status code indicates a provisional response, consisting only of the Status-Line and optional headers, and is terminated by an empty line.

2XXThis class of status codes indicates the action requested by the client was received, understood, accepted and processed successfully.

3XXThis class of status code indicates the client must take additional action to complete the request.

4XXThe 4xx class of status code is intended for cases in which the client seems to have errored.

5XXThe server failed to fulfill an apparently valid request.

Response status codes beginning with the digit "5" indicate cases in which the server is aware that it has encountered an error or is otherwise incapable of performing the request.

What is REST?

•REST stands for Representational State Transfer.

•An architectural style for building loosely coupled systems

•REST is not a "standard". There will never be a W3C recommendataion for REST

• It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used.

What is REST?

RESOURCES

VERBS

URL

http://foo.com/doc

GET

How does OData use REST?

•OData APIs are just Collections of Typed Entities (Resources)

•OData services expose all these Resources via URIs

•OData services use HTTP Verbs to intereact with Resources

•OData services can also expose Actions and Function for custom work

How OData use HTTP Verbs?

HTTP Command (Verb) SQL Command

GET SELECT

PUT UPDATE

POST INSERT

DELETE DELETE

OData in 6 Simple Steps

1. Requesting Resources

2. Requesting an Individual Resource

3. Queries

4. Creating a New Resource

5. Relating Resources

6. Invoking a Function

OData – Requesting Resources

GET http://services.odata.org/TripPinServiceRW/People HTTP/1.1

OData-Version: 4.0

OData-MaxVersion: 4.0

OData – Requesting an Individual Resource

GET http://services.odata.org/TripPinServiceRW/People('russellwhyte') HTTP/1.1

OData-Version: 4.0

OData-MaxVersion: 4.0

OData –Queries

GET http://services.odata.org/TripPinServiceRW/People?$select=FirstName,LastName&$orderby=LastName&$top=10 HTTP/1.1

OData-Version: 4.0

OData-MaxVersion: 4.0

Query Options

Sorting /Bookmarks?$orderby=Name

Filtering /Bookmarks?$filter=year(Created) gt 2007

Paging /Bookmarks?$top=10&$skip=30

Inline expansion /Bookmarks?$expand=Tags

Query Projection /Bookmarks?$select=Name,Description

OData –Creating a New ResourcePOST http://services.odata.org/TripPinServiceRW/People HTTP/1.1

OData-Version: 4.0

OData-MaxVersion: 4.0

Content-Type: application/json

{

'UserName':'lewisblack',

'FirstName':'Lewis',

'LastName':'Black',

'Emails':[

'[email protected]'

]

}

OData – Relating Resources

POST http://services.odata.org/TripPinServiceRW/People('lewisblack')/Trips/$ref HTTP/1.1

OData-Version: 4.0

OData-MaxVersion: 4.0

Content-Type: application/json

{ '@odata.id':'http://services.odata.org/V4/(S(34wtn2c0hkuk5ekg0pjr513b))/TripPinServiceRW/People('russellwhyte')/Trips(0)'

}

OData – Invoking an Action

GET http://services.odata.org/TripPinServiceRW/People('russellwhyte')/Trips(0)/Microsoft.OData.SampleService.Models.TripPin.GetInvolvedPeople()

HTTP/1.1

OData-Version: 4.0

OData-MaxVersion: 4.0

Wrap Up

[email protected]

SKYPEcwoodruff

TWITTER@cwoodruff

PHONE616.724.6885