9.2. RPC ServiceDelegate, Facade, Procedure, Proxy, Remote, Remoting, RPC, SOAP, WSDL Figure 9-2. RPC Service9.2.1. Developer StoryDave's finished coding the backend services for some blogging software. There's an ArticleRepository object, for example, with functions to create, remove, update, and delete articles. Using a new product, he's able to effortlessly expose ArticleRepository as a web service API, with a different URL for each operation. Now he can code up the browser script for his own application to talk to the API, and there's a ready-made API available to third parties too. 9.2.2. ProblemHow do you expose services on the Web? 9.2.3. ForcesRefer to the section "Forces" in RESTful Service. 9.2.4. SolutionExpose web services as Remote Procedural Calls (RPCs). Like REST, RPC is a broad term. It's somewhat ambiguous too, meaning different things to different people. This pattern uses a fairly general definitiona Remote Procedural Call (RPC) is a form of communication where the client invokes a remote procedure on the server. The definition implies:
Here are some concrete forms of RPC:
Can RPC be RESTful? Some argue it can. RESTful Service is one of the largest patterns in this language, precisely because REST is a broad idea consisting of many principles and conventions. Given that RPC URLs represent actions rather than resources, it's a little over the top to say that RPC can be made truly RESTful. However, many of the REST principles do make sense in an RPC context, and it's advisable to follow them. Remember that many entities on the Internet, such as caches, are based on certain RESTful assumptions, and you'll be affected by those no matter how you design your interface. A few guidelines:
9.2.5. Real-World Examples9.2.5.1. KikoKiko (http://kiko.com) is an online calendar application with a slew of Ajax features. The browser communicates to an interface using RPC-style URLs. For example, the following call retrieves my contacts: http://www.kiko.com/kiko/kikoservlet?function=SelectContactsByUser , false , undefined , undefined) A change is made in the same way. Here's what the browser called when I added an appointment: http://www.kiko.com/kiko/kikoservlet?function=CreateAppt&starttime=2005-9-12 18:0: 00&endtime=2005-9-12 s18:30:00 , false , undefined , undefined) Kiko invokes these URLs using POSTs, where the body is just an authentication key. Note that the service could be more REST-like, while retaining the RPC-style URLs, if Kiko wants to open the API to the public and ensure scaleability. To make it more REST-like, queries like SelectContactsByUser would be issued as GETs, with cookies used for authentication in place of the authentication key in the body. This would not only improve consistency across APIs, but also have the direct practical benefit of supporting caching, since GET responses can easily be cached. Also, POSTs could include all arguments inside the body, while retaining RPC-style URLs like /?function=CreateAppt. 9.2.5.2. Flickr APIThe Flickr API (http://www.flickr.com/services/api/), available to external developers, is a good example of an API with RPC-style URLs that also respects the basic REST conventions of GET strictly for reads and POST strictly for writes. Most calls include an API key, which the developer must apply for. A query for photo details such as owner and description is a GET call to a URL like this: http://www.flickr.com/services/rest/?method=flickr.photos.getInfo&api_ key=ap1000&photo_id=2000 You can also effect changes with the API. For example, you can tag an element by POSTing to a URL like this: http://www.flickr.com/services/rest/?method=flickr.photos.addTags containing a body like this: api_key=ap1000&photo_id=2000&tags=Screenshot 9.2.6. Code Example: AjaxPatterns RPC Shop DemoThe Basic Ajax Shop Demo (http://ajaxify.com/run/shop) provides a web service to expose and manipulate store items and user shopping carts. In this demo, the cart and product services are refactored to provide a cohesive RPC service. You can contrast this to the Refactoring Illustration in RESTful Service. A single service acts as a Facade (Gamma et al., 1995) to all server-side functionality. It accepts command-like URLs and routes them to the appropriate function. Read-only queries are of the following form, with the argument being optional: services.phtml?command=queryType&someArg=someValue State-affecting commands instead require all arguments, including the command, to be posted to a simple URL: services.phtml Because the URL is generic, the command must qualify what object it applies to; e.g., addToCart as opposed to add. 9.2.6.1. Reading the Categories listTo read the Categories, you GET http://ajaxify.com/shop/rpc/services.phtml?command=get-Categories. 9.2.6.2. Reading an individual categoryTo drill down to an individual category, say "Movies," you GET http://ajaxify.com/shop/rpc/services.phtml?categoryName=Movies. 9.2.6.3. Reading cart contentsTo read cart contents, you GET http://ajaxify.com/shop/rpc/services.phtml?command=getCart. Note that cart access is based on the session, as with the Basic Shop Demo (and unlike the RESTful Service demo). It doesn't have to be like this, but doing so allowed for a minimal change. 9.2.6.4. Changing cart contentsTo clear the cart, we POST a body of command=clearCart to services.phtml. To add an item, we POST the command and item in the body, such as command=addToCart&item=Accidental Empires to services.phtml. 9.2.7. Alternatives9.2.7.1. RESTful ServiceRESTful Service (see earlier in this chapter) is an alternative to RPC Service, but many tenets of REST can, and should, still be followed in RPC. See the RESTful Service "Solution" for a comparison. 9.2.8. Related Patterns9.2.8.1. Ajax StubAjax Stub (see later in this chapter) automates the production of RPC Services. 9.2.8.2. Plain-Text Message, XML MessageRPC is often used to design an application-independent web service API, or at least one that doesn't know anything about the user interface. That being the case, responses tend to be of a raw, semantic nature such as a Plain-Text Message (see later in this chapter) or an XML Message (see later). 9.2.9. MetaphorTask-oriented user interfaces, such as simple menu systems and the new MS Office Task Ribbon idea, are similar to RPC. They explicitly offer the typical tasks you wish to achieve. |