RESTful Web Services
RESTful Web Services
RESTful Web Services
1
Among PowerBuilder 12.5 .NET's new features comes the ability to use WCF to call
RESTful web services. This article, the first in a two-part series, provides a
PowerBuilder 12.5 .NET developer with the foundational knowledge and skills to
rapidly get up and running building PowerBuilder RESTful Web Service clients. Along
the way I'll share with you a few tips and workarounds.
Introduction
PowerBuilder 12.0 .NET provided the ability to call SOAP-based web services using
Microsoft's .NET 3.5 WCF API. I encourage those of you not familiar with WCF and
SOAP to view my PowerBuilder WCF primer and StockTrader reference application
overview hosted on Sybase.com. You can get to all my free tutorials from
http://yakovwerde.ulitzer.com/node/1551687. New with version 12.5, along with an
upgrade to WCF 4.0, is the ability to call RESTful web services, an increasingly popular
mechanism for accessing remote resources in SOA applications. In this article I'll help
you gain some foundational understanding and definitions, then familiarize you with
the mechanics of coding a client to retrieve data from a response-oriented RESTful
service. In Part 2, I'll explore the three types of request-oriented operations.
What Are RESTful Web Services?
In a nutshell, RESTful services are "web natural" and lightweight. They leverage HTPP's
native GET, PUT, POST & DELETE methods to invoke service methods at well-known
URIs. Much has already been written and is freely available on the Internet about the
nature and implementation of RESTful web services and how they compare to
SOAP/WSDL services. Instead of rehashing what has already been thoroughly
described, Listing 1 provides a couple of web references from which you can glean an
understanding of the technology. If you need more information, a quick search on your
favorite search engine will turn up more references. If you're really serious and would
like a full-sized book, you can purchase Jon Flanders' RESTful .NET published by
O'Reilly.
1. A Guide to Designing and Building RESTful Web Services with WCF 3.5
2. Learn REST: A Tutorial
Listing 1: Useful Web References
One pragmatic difference between WSDL / SOAP services and RESTful services from
the client viewpoint is the amount of onus placed on the client developer. SOAP
services are fully described in a WSDL document, including their service methods,
input parameters and return types. A PowerBuilder developer has no responsibility
other than to input the WSDL location into the wizard or project painter and click the
generate proxy button. The project painter does the rest. SOAP calls are also method
based. At runtime the developer instantiates a single proxy and invokes specific
methods as necessary. With RESTful services, however, the developer has more
responsibility in defining the interface. He has to get hold of either a schema (XSD)
document or .NET assembly that fully describes the types in the returned XML or JSON,
OR a sample result set with sufficiently varied data to help the proxy generator
decipher the return structure. He also has to know enough about the structure of the
return type so as to be able to review the wizard's work and make corrections as
necessary (more about this later). Calls to a RESTful service are not method based simply navigating to the service URI with correct parameters appended gets you a
response. This means there is one proxy per service (method), hence more code
objects to maintain.
What Is JSON?
Depending on how they are implemented, RESTful web services can return data in one
of several standard formats. Among the possible formats are XML, CSV, RSS, ATOM
and JSON. PowerBuilder 12.5 allows you to choose either XML or JSON for your
transfer format. What is JSON? The upshot is that JSON is a JavaScript / AJAX browser
friendly data transfer format. Because AJAX is a pervasive web technology, many
services are only available in JSON flavor. Listing 2 provides some useful references
from which you can get more background on JSON.
JSON presents no problem for PowerBuilder! At design time you can provide a sample
data set in JSON or XML format to the project painter. The project painter will parse
the sample data, JSON data and generate the .NET reference objects you'll need to
work with your data. At runtime JSON-based data transfer is parsed and converted to
.NET value objects. The good news is that as a PB programmer you will not have to
write JSON; however, you will have to read it to understand how a result set is
constructed.
1. Quick overview of this JSON
2. JSON, a formal definition
Listing 2: Useful JSON References
Viewing JSON
IE 9 does not have a native facility for viewing JSON data. You'll need to use a tool to
view the output data. One such tool is http://www.jsonviewer.com/. Once on the
page, input the full invocation URL and click the Get JSON and Parse' button. Figure 1
shows what you might see.
their database. Figure 3 shows a partial listing of available services. You can find the
full list of available services at http://www.geonames.org/export/ws-overview.html.
To explore GeoNames on your own, you'll want to create a free user account using the
Login link on the upper right side of their home page. Having an account is important
since you'll need to provide your username as a parameter to your service calls and
access using the "demo" user name is extremely limited.
4. A name for your proxy class. You will interact with this class in your code. I'd
suggest using the P_ prefix as part of your name.
5. A name for the NameSpace you want to put your proxy class in - you can leave
it blank to use the default namespace.
6. The URI for the service including any parameters needed for the call.
7. The service's return data format: XML or JSON.
8. A name for a generated assembly. This assembly will contain value objects used
as parameters and/or return types. It will be added by the wizard to your target
as a reference. This name will also become the namespace for the assembly.
9. A source to help the painter determine the return data type. There are three
options, choose one:
- A schema document (XSD) that defines the structure of the returned dataset.
- A preexisting assembly that formally defines the return type structure
- A sample XML snippet or JSON object whose data is rich and robust enough
that the tool can figure out the data types from its contents.
Sidebar Tip: As shown in Figure 4 you can cause the generator to include input
parameters on the GetMessage( ) method by changing Service URL literal values to
tokens. Identifiers inside { } will become parameter names. If you leave the literal
values in, the generator will emit a parameter-less method call.
needed multipart/form-data with the JSON sent as a form parameter. This is not a
supported format. So e caveat emptor, make sure the service provider has a
traditional' form of RESTful API. Luckily, I believe most of the big boys' with widely
used APIs do.
Oh, Those Project Objects!
Before getting into details, a few words about RESTful project objects and generated
code. RESTful project objects are designed to be like single shot Derringer pistols; fire
one shot, then you have to reload. Each project object is capable of generating one
proxy object for one method, period. That means if you are interacting with multiple
web methods (GET, POST, PUT and DELETE) you're going to have multiple proxy
objects. In addition, each operation, depending on its type, can have up to two
referenced .NET value objects, one for the request and one for the response. One nice
thing I noticed is that if multiple services rely on the same parameter sets, you can
reuse referenced value objects instead of generating superfluous carbon copies. Figure
1 shows how you'd configure the project painter to reference an already-generated
assembly. Each reference object is placed in its own assembly inside an identically
named namespace. Unfortunately it seems like you can't have multiple value objects in
a single assembly or namespace. Figure 2 shows how reference object assemblies
appear in the Solution Explorer. Figure 3 shows you the wizard view when choosing an
existing value object assembly.
Listing 3
Secure Socket Layer Communication
Specifying and handling encrypted communication over a SSL transport is pretty
transparent to the application developer. As you can see in Figure 10, all you need to
do is specify the HTTPS protocol in the Project Painter Service URL. The handshake and
encryption are handled by the underlying infrastructure as shown in Figure 11.