1 © Donald F. Ferguson, 2015. All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns Some.

Slides:



Advertisements
Similar presentations
Pierre-Johan CHARTRE Java EE - JAX-RS - Pierre-Johan CHARTRE
Advertisements

Semantic Web Introduction
B.Sc. Multimedia ComputingMedia Technologies Database Technologies.
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
1 The World Wide Web. 2  Web Fundamentals  Pages are defined by the Hypertext Markup Language (HTML) and contain text, graphics, audio, video and software.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Computer Science 101 Web Access to Databases Overview of Web Access to Databases.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Chapter 9 Collecting Data with Forms. A form on a web page consists of form objects such as text boxes or radio buttons into which users type information.
Lecture 3 – Data Storage with XML+AJAX and MySQL+socket.io
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
Databases From A to Boyce Codd. What is a database? It depends on your point of view. For Manovich, a database is a means of structuring information in.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
INTERNET APPLICATION DEVELOPMENT For More visit:
Introduction –All information systems create, read, update and delete data. This data is stored in files and databases. Files are collections of similar.
Systems analysis and design, 6th edition Dennis, wixom, and roth
Server-side Scripting Powering the webs favourite services.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
REST.  REST is an acronym standing for Representational State Transfer  A software architecture style for building scalable web services  Typically,
1 Overview of Databases. 2 Content Databases Example: Access Structure Query language (SQL)
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Prepared By : Monika Darji Web Services using REST & JAX-WS.
HNDComputing – DeMontfort University  DeMontfort University 2011 Database Fundamentals wk2 Database Design ConceptsDatabase Design Concepts Database Design.
Open Data Protocol * Han Wang 11/30/2012 *
Lecturer: Prof. Piero Fraternali, Teaching Assistant: Alessandro Bozzon, Advanced Web Technologies: Struts–
SQL 101 for Web Developers 14 November What is a database and why have one? Tables, relationships, normalization SQL – What SQL is and isn’t – CRUD:
JSTL, XML and XSLT An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout.
 2004 Prentice Hall, Inc. All rights reserved. 1 Segment – 6 Web Server & database.
Chapter 10: The Data Tier We discuss back-end data storage for Web applications, relational data, and using the MySQL database server for back-end storage.
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
JDeveloper 10g and Oracle ADF Business Components Getting the Most Out of Your Data Avrom Roy-Faderman Senior Programmer November, 2005.
1 Seminar on Service Oriented Architecture Principles of REST.
A FIRST TOUCH ON NOSQL SERVERS: COUCHDB GENOVEVA VARGAS SOLAR, JAVIER ESPINOSA CNRS, LIG-LAFMIA, FRANCE
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
Database Basics BCIS 3680 Enterprise Programming.
© Donald F. Ferguson, All rights reserved. Topics in Computer Science: Modern Internet Service Oriented Application Development Lecture 8: IPaaS.
1 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns Some.
Representational State Transfer (REST). What is REST? Network Architectural style Overview: –Resources are defined and addressed –Transmits domain-specific.
The relational model A data model (in general) : Integrated collection of concepts for describing data (data requirements). Relational model was introduced.
ICM – API Server & Forms Gary Ratcliffe.
IST 220 – Intro to Databases Lecture 2 Touring Microsoft Access.
RESTful Web Services What is RESTful?
Session 1 Module 1: Introduction to Data Integrity
Web Technologies Lecture 10 Web services. From W3C – A software system designed to support interoperable machine-to-machine interaction over a network.
Web Technologies Lecture 11 Implementing RESTful services.
Object storage and object interoperability
© Donald F. Ferguson, All rights reserved. Topics in Computer Science: Modern Internet Service Oriented Application Development Dr. Donald F. Ferguson.
© Donald F. Ferguson, All rights reserved. Topics in Computer Science: COMS E Micro-service Application and API Development Lecture.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
MYSQL AND MYSQL WORKBENCH MIS2502 Data Analytics.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
© 2010 IBM Corporation RESTFul Service Modelling in Rational Software Architect April, 2011.
By: Eliav Menachi.  On Android, all application data (including files) are private to that application  Android provides a standard way for an application.
REST API Design. Application API API = Application Programming Interface APIs expose functionality of an application or service that exists independently.
Introduction to Database Programming with Python Gary Stewart
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
IST 220 – Intro to Databases
API (Application Program Interface)
REST: Web Services Abel Sanchez.
Node.js Express Web Services
mysql and mysql workbench
WEB API.
Structured Query Language (SQL) William Klingelsmith
Structured Query Language
Introduction of Week 11 Return assignment 9-1 Collect assignment 10-1
…and web frameworks in general
CS4961 Software Design Laboratory Understand Aquila Backend
Week 05 Node.js Week 05
Chengyu Sun California State University, Los Angeles
Presentation transcript:

1 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns Some Design Patterns Or Best Practices

2 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns Resource Basics

3 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns Some URLs Obvious URLs Collections/Extents –…/staff Returns all staff –…/storesReturn all stores –…/paymentsReturn all payments –…/rentalsReturn all rentals Instances –…/staff/21Return employee 21 –…/stores/11Return store 11 –…/payments/9You get the picture –…/rentals/6You get the picture

4 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns Resource URL and Representation GET …/stores/21 maps to a row in a table with the following columns –Store_id –Manager_staff_id –Address_id –Last_update A straightforward JSON representation is {store_id : 21, manager_staff_id : 11, address_id : 9, last_update : :03:20} There are several issues with this format. Three significant ones are: –How to find the “manager” with id=11 is unclear. I need to know side information not present in the response. –SQL columns names are often “unclear.” I am not exactly sure what “manager_staff” means. –Last_update is a SQL datatype. Understanding how to convert to a corresponding data type in my client application requires knowing the underlying implementation is SQL.

5 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns A Better Representation I would return something like {store: {“data” : {id : 21}, “link” : {“rel” : “self”, “href” : “…/stores/21”}}, {manager : {“data” : {“id” : 11, “name” : {“lastName” : “Ferguson”, “firstName : “Donald”}, {“link” : {“rel” : “storeManager”, “href” : “…/staff/11}}} {address : {“link” : {“rel” : “storeAddress”, “href” : “…/address/9}}, {“timestamp” : “30-Jan-2015”}} Observations –If I got the {…{}…} right in that, I will eat a JSON manual. –I converted the timestamp into something more human/string friendly. –Returning the value of a foreign key columns is wrong. I returned the value and an explicit link to navigate. –I can optionally return some of the data in reference for convenience (columns from the JOIN). –I make the column names a little friendlier.

6 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns Some Additional Observations …/staff/21 maps to a row in a table of the form –Staff_id –First_name –Last_name –Address_id –Picture –… … Some suggestions –Return a name object, not individual fields, i.e. { … …, “name” : {“last” : “Ferguson”, “first” : “Donald”}, … …} –Image is a “BLOB” = “Binary Large Object.” I would return {… …, “Picture” : {“rel”: “picture”, “href” : “…/staff/21/picture”}, … …}

7 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns The BLOB! A GET on –…/staff/21/picture should return –The same kind of thing that you get when you click on – Content-Type: image/jpeg In the Express framework for node.js ( you can set this with the call res.type(‘image/jpeg’) Or just use Amazon S3

8 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns Let ’ s Take a Minute Remember when I talked about REST  “Uniform Interface?” This actually means Resource based –URIs identify (locate) individual resources. –The resources are representations and not the underlying DB schema, file data, … … Manipulation through representations –If I have a representation (may include headers) –I have enough information to reread, modify, delete (subject to security permission) the resource –And do not need “side information,” e.g. knowing that {“manager_id” : 21} needs to become …/staff/21. Self-descriptive: Each “message” contains enough information to process it, e.g. MIME type

9 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns Let ’ s Take a Minute The “pointer” to a resource is the URI The common operations in any data model are (CRUD): –Create = HTTP POST –Retrieve = HTTP GET –Update = HTTP PUT –Delete= HTTP DELETE Datamodels commonly have containment and reference relationships –Containment: Represent logically contained resources as resources, e.g. –Do not return {… …, “lastname” : “Ferguson”, “firstname” : “Donald”, …} –Just because the database represents it that way –When you really mean {“name” : {“last” : “Ferguson”, “first” : “Donald”}} –Reference: Return a typed link to the resource –Relationships are navigable, e.g. –…/store/21/manager returns the resource representation of the correct manager from the “Staff” table –…/staff/32/name/last returns “Ferguson” Datamodels have types, which you represent using MIME types –Application/json  JSON –Application/XML  XML –Image/jpeg  JPG image

10 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns For example, Node.js + Express –Create the URL base at localhost:1234 –Specify the “routes” –app.get(‘URL sub-path’, module.function) –app.put(‘URL sub-path’, module.function) –app.post(‘URL sub-path’, module.function) –app.delete(‘URL sub-path’, module.function) –The JavaScript modules and functions –Perform CRUD on the underlying implementation of the representation, e.g. RDB, MongoDB –And any associated logical like validating data, convert foreign keys to links, etc. There are many others, e.g. Sails, JAX-RS, … This may be Obvious but … How do you implement a service? Use a framework.

11 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns For example, jQuery in a browser And there are many others, e.g. JAX-RS Client API This may be Obvious but … How do you call a service? Use a library.

12 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns Collections

13 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns Collections All datamodels have some form of collections, e.g. –Relational database tables –File system directories –Java.util.Collections.List These map naturally into URIs –…/staff –…/stores –…/addresses But there are a couple of challenges –Locating things in a collection –Paging or iterating through a collection

14 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns Locating Things in a Collection The collection typically has a unique, primary key –With a natural representation in URL/REST, –.../staff/21 –…/stored/36 –Which returns a resource of the form data and links Some collections have non-unique, secondary keys with –A less natural representation in URL/REST, e.g. –.../staff?name.last=“Ferguson”&name.first=“Donald” that –Maps to “SELECT * FROM staff WHERE last_name=“Ferguson” AND first_name=“Donald”; –That is itself also a collection. Some collections support ad hoc, client specified queries –With quasi-natural representation in URL/REST, e.g. –…/addresses?q=“zipcode=12345||state=“California” –Maps to “SELECT * FROM addresses WHERE zipcode=‘12345’ OR state=‘California’ –That is itself also a collection

15 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns Some Observations Why surface secondary keys and not just ad hoc query? –Some databases do not support arbitrary, ad hoc query –DBAs may –Preclude ad hoc queries for various reasons –And only surface predefined, parameterized, compiled template queries –SELECT * FROM CUSTOMERS WHERE zipcode =  …/customers?zipcode=12345 You should –Define your query language in your model –And not just surface the underlying query language, e.g. SQL, MongoDB –Because the underlying language is the implementation, not a representation –And also invites things like SQL injection attacks –Parse, validate and map the query into –Actual implementation language –Actual underlying datamodel, e.g. RDB, MongoDB –And I use q=“….” as the representation of the query string to draw a distinction between –The ability to process ad hoc queries on a collection –Versus a predefined, fixed set of combinations –…/customers?zipcode=12345 is correct –…/customers?zipcode<=12345 is not correct –…/customers?zipcode=12345&type=Gold is not correct

16 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns Iteration/Pagination – The Concept …/earth/animals/humans will return about 6 billion resources And I doubt very much that –The client really wants all 6 billion in one call –Or that the server really wants to return all six billion Which is where iterators and pagination occur –Relational database –DECLARE human_cursor FOR SELECT * FROM humans; –OPEN human_cursor; –FETCH human_cursor INTO …; –Java collections –java.util.Collections.List allHumans; –java.util.Collections.Iterator I = allHumans.getIterator(); –Human h = i.next(); –And other collections/databases have similar concepts

17 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns How do you Represent this in REST? Well, There are a Few Ways Limit and Offset –GET …/earth/humans?offset=0&limit=10 –GET …/earth/humans?offset=10&limit=10 –And this has a natural rendering in some languages, e.g. RDB SELECT * FROM humans LIMIT 10 OFFSET 0; The collection is ordered by a key –GET …/earth/humans?zipcode<=0&limit=10 –Look at the 10 th entry in the result set, which has zipcode=“20135” –And the next 10 are GET …/earth/humans?zipcode>”20135”&limit=10 And you can combine all of this in freaky ways –GET …/earth/humans?q=“hairColor=red&iq>100”&s=“weight,shoesize”&limit=10&offset=10 –Maps to a freaky SQL statement of the form SQL SELECT * FROM humans WHERE hair_color=red AND iq>100 ORDERBY weight, shoe_size OFFSET 10 LIMIT 10

18 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns This Pushes a lot on the Client A well-defined REST API hides this complexity using –RFC 5988 Link headers –The GET returns an HTTP header of the form –Links: ; rel=“next”, ; rel=“prev”, ; rel=“first”, … –rel=“self” is the URL to the page you are on. –rel=“next” is the URL to the “next” page –rel=“prev” is the URL to the “previous” page –rel=“first” is the URL to the “first” page –rel=“last” is the URL to the “last” page –GET on the URL with rel=“next” GETs the next page with a new set of next, prev, first, … related URLs This enables –The client to specify a limit or the server to specifyc a limit –And enable the client to iterate without know how the server is implementing iteration, e.g. –LIMIT and OFFSET –ORDER BY and values –… …

19 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns If You Do Not Like Headers GET …/staff?status=active&limit=2 returns something like { –“data” : –[ –{“id” : 21, “manager” : {“link” : { “rel” : “manager”, “href” : “…/managers/9”}}}, –{“id” : 11, “manager” : {“link” : { “rel” : “manager”, “href” : “…/managers/101”}}}} –] – “links” : –[ –{“rel” : “next”, “href” : “…/staff?status=active&offset=2&limit=2}, –{“rel” : “first”, “href” : “…/staff?status=active&offset=0&limit=2}, –{“rel” : “last”, “href” : “…/staff?status=active&offset=210934&limit=2} –] } Why did I highlight this?

20 © Donald F. Ferguson, All rights reserved.Modern Internet Service Oriented Application Development – Lecture 2: REST Details and Patterns A subtlety A GET on –…/humans?iq=“100” –…/humans?q=“id=100&&hairColor=red” Implicitly –Creates/computes a new collection resource “just for you” –That is the result of running the query or using the keys But, –REST is stateless and the server is not going to “keep the result” for “just for you” –So you have to tell the server how to re-compute the result But, –You may not be the only one CRUD-ing humans –So freaky stuff can happen. For example, following the “self” link might not return the same JSON object that you just got. The freaky stuff can happen in any database. This is why RDBs have isolation levels for cursors. We cannot use isolation levels –Not really compliant with REST –Does not suit “Web scale” and “loosely coupled” applications –Which means we have to handle in other ways