REST is awesome. There I said it. And, like many of you, I’ve lived through several generations of remoting technologies over the years.
What I like most about REST is its simplicity and inherent flexibility AND the fact that everyone is exposing their services this way AND it’s relatively painless to integrate regardless of the language used. Basically, REST is everywhere and there countless services exposed that will add value to your apps.
In RAD Studio XE5, we’ve introduced components for VCL and FireMonkey to easily integrate REST services and although there have been a few other blogs about it, I wanted to share a simple example that illustrates how REST works really well. For this example, I will wire together a VCL app to gather surf forecast information.
REST is made up of a couple of key concepts: the server, the request, and the response. Additionally, it would nice if the data exchange format, called JSON (which is basically a tuple of strings) could be turned into an compatible dataset to integrate into our UI controls.
So, as I mentioned, the basic REST components are RESTClient, RESTRequest, RESTResponse, and RESTResponseDataSetAdapter. There are of course other components provided in the framework to deal with authentication, with OAuth, for example, but these are the basics.
First up is the API server. Most services will use a convention like api.domain.com. In this example, the service is provided by spitcast.com and they provide the API server api.spitcast.com. So the first thing I need is a TRESTClient component and to set its base URL.
Now, that we are pointing to the API server, let’s setup our first request. REST requests are formatted like an URL off of the BaseURL, using standard HTTP requests, like GET, PUT, DELETE, etc. So, since a big swell is coming in this weekend I would like to see what the top spots are using the resource /api/top/spots.
We are ready to get a response now. REST responses usually come back as a string based data packet called JSON. First, let’s take a look at the JSON through the web browser.
Great, now let’s add a Tesponse component to grab this JSON. And now, let’s execute the request within the IDE and populate the Resonse component.
The request is good.
Now, we should see that JSON packet in our component Content property.
Now, we could parse this simple format but there is a VCL/FM component that can turn this into a compatible dataset that we could easily integrate with LiveBindings. For that we will use a TRESTResponseDataSetAdapter.
After we make them both Active we can add fields and bind the data visually using the Live Bindings Designer to get them into a ListBox.
Ok, everything is setup now and I can see my results live in the designers. Like I said, Awesome!
Now, look at that 3 out 5 top spots are here in Santa Cruz. Choices, choices.
So, now that you have a flexible REST client components set for VCL and FM in XE5, go forth and integrate.
And don’t forget to take advantage of our current promotions before December 31st to get REST with RAD Studio XE5. Current offers include “Coding in Delphi” book by Nick Hodges and the InfoPower XE5 VCL Grid and Components in addition the the InfoPower for FireMonkey. And a 45% off upgrade offer for previous owners of any version of RAD Studio, Delphi, or C++Builder.
Share This | Email this page to a friend