zimki | documentation | search
 
Zimki Documentation - CookBook.WebService

One of the major developments in Internet technology is the use of XML web services. These enable simple communication between machines in a readable format. When combined with client side JavaScript they allow a developer to create dynamic web pages that can retrieve discrete sets of information without needing to refresh the whole page.

To begin with, we are going to create a simple XML web service that will retrieve quotes for a particular item. Create a new JavaScript instance and copy the following code into it:

function getQuotes() {
  var quotes = Quote.search({ order_by: '-creation_date' });
  return {stock:quotes};
}

//publish and describe format
zimki.publishService( 'myQuotes', getQuotes,
  <list>
    <stock>
      <name>first item</name>
      <price>123.4</price>
      <quantity>12</quantity>
    </stock>
    <stock />
  </list>
);

Notice that we are searching for instances of the Quote class to return. Let's create a few such instances. Go to the JavaScript Console and type in the following code, then hit run:

// Let's create a 'Quote' class and populate it
// with some example data.
zimki.persist.create('Quote');

var things = [
  { name:"chips", price: 1.23, quantity:4 },
  { name:"beans", price: 1.15, quantity:3 },
  { name:"peas", price: 0.43, quantity:2 }
];

// create an instance for each of the example data entries
for (var i = 0; i < things.length; i++) {
  Quote.create(things[i]);
}

Now open a new browser window or tab, and open the URL

http://service.zimki.com/zimki?api_key=quote-service&method=myQuote

(assuming you have an API key with the alias quote-service in your realm; you won't, of course, because I've taken it). You should get the following result back:

<?xml version="1.0" encoding="utf-8"?>
<rsp stat="ok">
  <list>
    <stock>
      <name>peas</name>
      <price>0.43</price>
      <quantity>2</quantity>
    </stock>
    <stock>
      <name>beans</name>
      <price>1.15</price>
      <quantity>3</quantity>
    </stock>
    <stock>
      <name>chips</name>
      <price>1.23</price>
      <quantity>4</quantity>
    </stock>
  </list>
</rsp>

Though how this will be represented by your browser depends on which browser you use (Firefox will show you the syntax-highlighted XML, which is quite handy).

Next step

  1. Try to create an addQuote service call to add new quotes to the quote database.
  2. If adding an item that already exists, just update that item rather than adding a duplicate
  3. Make the quantity and price parameters of addQuote optional, and have sensible defaults if explicit values are not given

See also

Parent

CookBook