# Wednesday, July 20, 2011

On a previous post I outlined a procedure for implementing ReSTful Web Services. In this post I will expand that outline with the design/implementation of a Web Api for The Smart Shopping List.

Figure 1 shows a class diagram of the simple domain model for The Smart Shopping List.

SSLDomain

Figure 1. The Smart Shopping List domain model

As you can see we have a Shopper that could have zero to many Stores and also zero to many Shopping Lists.

Each Store could have zero to many Product Categories and also zero to many Products.

Each Shopping List could have zero to many Products along with some other information.

Figure 2 shows a class diagram of the logical resources that will represent our Web Api.

SSLResources

Figure 2.The Smart Shopping List Resource Model

We have one entry point logical resource named Smart Shopping List, that advertises the Store  & the Shopping List logical resources for each Shopper.

The Store logical resource contains two logical sub-resources Product Category & Product  and the Shopping List logical resource has one Detail logical sub-resource.

Those logical resources and sub-resources are named by their Uris as depicted in Table 1.

Logical Resource Uri
SmartShoppingListResource ""
StoreResource "stores"
  "stores/{storeid}/productcategories"
  "stores/{storeid}/products"
ShoppingListResource "shoppinglists"
  "shoppinglists/{shoppinglistid}/detail"

Table 1. Logical Resources Uris

Each logical resource also implement a subset of the uniform interface as Table 2 shows.

Method Uri Description Result
GET "" Request current Status of The Smart Shopping List A representation of The Smart Shopping List  status
" "stores" Request all the Stores of the current Shopper

If success,  return 200 and a list representation. If not, 400. 

" "stores/productcategories" Request all the Product Categories of the current Shopper "
" "stores/{sid}/productcategories" Request all the Product Categories in a Store "
" "stores/products" Request all the Products of the current Shopper "
" "stores/{id}/products" Request all the Products in a Store "
" "shoppinglists" Request all the Shopping Lists of the current Shopper "
" "stores/{id}" Request the current state of a Store If success, return 200 and a single representation with its status. If not, 304, 400 or 404.
" "stores/productcategories/{id}" Request the current state of a Product Category "
" "stores/products/{id}" Request the current state of a Product "
" "shoppinglists/{id}" Request the current state of a Shopping List "
" "shoppinglists/{slid}/details" Request all the Detail in a Shopping List "
" "shoppinglists/{slid}/details/{id}" Request the current state of a Shopping List Detail "
POST "stores" Create a new Store for the current Shopper If success, return 201 a Location header & with the new representation & its status. If not, 204, 400 or 409
" "stores/{id}/productcategories" Creates a new Product Category for a Store "
" "stores/{id}/products" Creates a new Product  for a Store "
" "shoppinglists" Creates a new Shopping List for the current Shopper "
" "shoppinglists/{slid}/details" Creates a new Detail for a Shopping List "
PUT "stores/{id}" Updates a Store If success, return 200 with the representation & its status. If not, 204, 400, 404 or 409
" "stores/{sid}/productcategories/{id}" Updates a Product Category "
" "stores/{sid}/products/{id}" Updates a Product "
" "shoppinglists/{id}" Updates a Shopping List "
" "shoppinglists/{slid}/details/{id}" Updates a Shopping List's Detail "
DELETE "stores/{id}" Removes a Store If success return 200. If not 204, 400, 404 or 409
" "stores/{sid}/productcategories/{id}" Removes a Product Category "
" "stores/{sid}/products/{id}" Removes a Product "
" "shoppinglists/{id}" Removes a Shopping List "
" "shoppinglists/{slid}/details/{id}" Removes a Shopping List's Detail "

Table 2. Logical Resource Uniform Interface subset

Figure 3 shows that each logical resource has two representations one for a single resource and another for its collection along with its status.

SSLResourceRepresentations

Figure 3. The Smart Shopping List Resource Representation Model

Also each resource representation contains links that advertises the logical resource status.

For the implementation I have used preview 4 of the WCF Web Api and I will be adding this code later this week (if everything goes smoothly Smile) to The Smart Shopping List Source Code. You also will be able to "test drive" The Smart Shopping List at https://webapi.smartshoppinglist.net but you have to create a Smart Shopping List account and also use the following headers with your client:

Accept: application/vnd.cyberbizsoft.ssl+xml

Authorization: Basic Your-User-ID:Your-Password (Base64 encoded)

When using POST, PUT & DELETE you must also add:

Content-Type: application/vnd.cyberbizsoft.ssl+xml

Just point your client to https://webapi.smartshoppinglist.net and follow the protocol.

image 

Figure 4. Sample POSTing session using Fiddler.

posted on Wednesday, July 20, 2011 3:07:07 AM UTC  #    Comments [0]