N342 RESTful Services

Modified

Disclosure   

REST overview from http://en.wikipedia.org/wiki/Representational_State_Transfer

 

Overview

REST is not a formal standard but a philosophy, one that supplies a client with light-weight data, often in the form of XML.

REST is designed to be simple and light-weight in resource requirements.

Alternatives to REST include Web-services using SOAP.

Proponents of REST argue that the Web's scalability and growth are a direct result of a few key design principles:

  • Application state and functionality are abstracted into resources
  • Every resource is uniquely addressable using a universal syntax for use in hypermedia links
  • All resources share a uniform interface for the transfer of state between client and resource, consisting of
  • A protocol which is:
    The following table associates several common HTTP verbs with similar database operations, however the meaning of the HTTP verbs do not correspond directly with a single database operation. For example, an HTTP PUT is used to set the value of a resource and may result in either a creation or replacement as needed.
     
    HTTP CRUD
    POST Create
    GET Read
    PUT Update, Create
    DELETE Delete

    When used RESTfully, HTTP is stateless. Each message contains all the information necessary to understand the request when combined with state at the resource. As a result, neither the client nor the server needs to remember any communication state between messages. Any state retained by the server must be modeled as a resource.

 

Example: Implementing a RESTful service

A RESTful Portfolio service might return the SYMBOL and SHARES of a stock owned by a given trader. The URL to access the RESTful Portfolio service could be:

http://iu-uits-eiwp1.ads.iu.edu/username/n342/project/RESTfulPortfolio.asp?trader=Ray

The service would return XML for the Portfolio table entries at right of:

<?xml version="1.0">
<Portfolio>
      <SYMBOL>IBM</SYMBOL>
      <SHARES>10</SHARES>
      <SYMBOL>MSFT</SYMBOL>
      <SHARES>100</SHARES>
</Portfolio>

The RESTful service could be implemented by simply writing all records as XML from the Portfolio table that match the trader ID:

<%@ Language=JScript%>
<%
   conn = Server.CreateObject("ADODB.Connection");
   conn.Open ("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + Server.MapPath("Project.mdb"));
   rs = conn.Execute("SELECT SYMBOL, SHARES FROM PORTFOLIO WHERE ID='"+Request("trader")+"';");

   Response.Write('<?xml version="1.0"?>');
   Response.Write("<Portfolio>");

   while( !rs.EOF ) {
      Response.Write("<SYMBOL>"+rs("SYMBOL")+"</SYMBOL>");
      Response.Write("<SHARES>"+rs("SHARES")+"</SHARES>");
      rs.MoveNext();
   }
   Response.Write("</Portfolio>");
   conn.Close();
%>

 

Example: Accessing a RESTful service

The RESTful Portfolio service returns the SYMBOL and SHARES in XML form which can be automatically parsed into a DOM tree.

http://iu-uits-eiwp1.ads.iu.edu/username/n342/project/RESTfulPortfolio.asp?trader=Ray

The service would return XML for the Portfolio table entries at right of:

<?xml version="1.0">
<Portfolio>
      <SYMBOL>IBM</SYMBOL>
      <SHARES>10</SHARES>
      <SYMBOL>MSFT</SYMBOL>
      <SHARES>100</SHARES>
</Portfolio>

The service is accessed by the usual URL:

 xmlDoc.load("http://iu-uits-eiwp1.ads.iu.edu/username/N342/project/RESTfulPortfolio.asp?trader=Ray");

which parses the XML into a DOM tree.

As an example, to display all the SYMBOL elements in the DOM tree:

  1. xmlDoc.getElementsByTagName("SYMBOL").length -the number of SYMBOL elements, there are 2
     
  2. xmlDoc.getElementsByTagName("SYMBOL")[i].text -text of each SYMBOL element
<%@ Language=JScript%>
<%
   var xmlDoc = Server.CreateObject("Msxml2.DOMDocument");
   xmlDoc.async = false;
   xmlDoc.setProperty("ServerHTTPRequest") = true;

   xmlDoc.load("http://iu-uits-eiwp1.ads.iu.edu/username/N342/project/RESTfulPortfolio.asp?trader=Ray");

   for(i=0; i < xmlDoc.getElementsByTagName("SYMBOL").length; i++)
       Response.Write( xmlDoc.getElementsByTagName("SYMBOL")[i].text + "<br/>");
%>
 

Output:

IBM
MSFT

Note:

Be sure to change usename to a real one.

 

Example: a RESTful Stock Quote service

A RESTful stock quote service returns XML of stock data given a stock symbol.

For example the Google service request: http://www.google.com/ig/api?stock=AAPL

returns:

  <?xml version="1.0" ?>
- <xml_api_reply version="1">
- <finance module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
  <symbol data="AAPL" />
  <pretty_symbol data="AAPL" />
  <symbol_lookup_url data="/finance?client=ig&q=AAPL" />
  <company data="Apple Inc." />
  <exchange data="Nasdaq" />
  <exchange_timezone data="ET" />
  <exchange_utc_offset data="+05:00" />
  <exchange_closing data="960" />
  <divisor data="2" />
  <currency data="USD" />
  <last data="335.22" />
  <high data="340.95" />
  <low data="335.02" />
  <volume data="12078244" />
  <avg_volume data="14549" />
  <market_cap data="308830.81" />
  <open data="339.56" />
  <y_close data="340.53" />
  <change data="-5.31" />
  <perc_change data="-1.56" />
  <delay data="0" />
  <trade_timestamp data="May 20, 2011" />
  <trade_date_utc data="20110520" />
  <trade_time_utc data="200018" />
  <current_date_utc data="20110522" />
  <current_time_utc data="024936" />
  <symbol_url data="/finance?client=ig&q=AAPL" />
  <chart_url data="/finance/chart?q=NASDAQ:AAPL&tlf=12" />
  <disclaimer_url data="/help/stock_disclaimer.html" />
  <ecn_url data="" />
  <isld_last data="" />
  <isld_trade_date_utc data="" />
  <isld_trade_time_utc data="" />
  <brut_last data="" />
  <brut_trade_date_utc data="" />
  <brut_trade_time_utc data="" />
  <daylight_savings data="true" />
  </finance>
  </xml_api_reply>

Note the last quoted stock value:

  <last data="335.22" />

 

Example of Accessing a RESTful service from ASP

The XML generated by the RESTful stock quote service can be accessed by an ASP script.

To display the Last price for the AAPL stock symbol, we need to:

  1. xmldoc.load("http://www.google.com/ig/api?stock=AAPL"); - load the returned XML into a DOM tree.
     
  2. xmldoc.getElementsByTagName("last")[0].attributes.getNamedItem("data").nodeValue - access the data attribute of element with the tag name last.

 

<%@ LANGUAGE = JScript %>
<%                 
    var xmldoc = Server.CreateObject("MSXML2.DOMDocument");
    xmldoc.async = false;
    xmldoc.setProperty("ServerHTTPRequest") = true;

    xmldoc.load("http://www.google.com/ig/api?stock=AAPL");

    Response.Write( xmldoc.getElementsByTagName("last")[0].attributes.getNamedItem("data").nodeValue );
%>

Output:

335.22