After too many days on the road, I've started publishing the source code of the demos I've shown at recent conferences. The first is an example of the use of the REST client library.
This is a demo I used for my CodeRage 8 session on the REST client library (the video is available) and uses the REST API behind the web site DiscoGS. The program is a very simple client that searches for albums with a given text in their title. Honestly, that's not the important part of the project. The application uses many different Delphi technologies, shortly covered in the following sections.
REST Client Library
The REST Client component has a BaseURL = 'http://api.discogs.com' while the REST Request component has an URI and a few parameters:
object RESTRequest1: TRESTRequest Client = RESTClient1 Params = < item name = 'title' Value = 'Thriller' end item name = 'per_page' Value = '100' end item name = 'type' Value = 'master' end> Resource = 'database/search' Response = RESTResponse1 end
From JSON To FDMemTable
The program automatically maps the resulting JSON to a FireDAC in-memory table, whose metadata gets automatically filled:
object RESTResponseDataSetAdapter1: TRESTResponseDataSetAdapter Dataset = FDMemTable1 FieldDefs = <> Response = RESTResponse1 end
Live Bindings (and DataModules)
The data is displayed in a ListView of a mobile Delphi application (I tested it on Android) using Visual Live Bindings. For better separation of data access and application logic and user interface, all of the non-visual components are in a data module (or "the model"), safe for those defining the live bindings, which are related to the user interface definition (or "the viewmodel" mapping). All of the code in the data module could be used in a VCL application or any other project.
Again notice how you can use the Live Binding Designer to connect the user interface controls with components on a data module (the system uses different colors, the brown boxes represents components in the data module, while the designer is open on the form):
The Final Application
The final application has the following output, here shown at design time (the ability to preview the actual user interface populated with data is a very relevant feature of Delphi):
Where is the Code?
You mean the Object Pascal source code? There is very little, as almost everything is configured visually. Here is all of it:
procedure THeaderFooterForm.btnGetClick(Sender: TObject); begin DataModuleMusic.RESTRequest1.Params.ParameterByName('title'). Value := edTitle.Text; DataModuleMusic.RESTRequest1.Execute; DataModuleMusic.FDMemTable1.Active := True; end;
Getting the Code
After this long description, here is finally what I meant to write, which is how to get this code. I created a new public repository (read-only) based on Subversion. You can get the latest version of the code (which might be udpated over time) or just browse it online. The project will host many different examples, this is only the first one. The location of the entire project is:
http://code.marcocantu.com/trac/marcocantu_delphi_sessions_demos
While the Subversion URL is:
code.marcocantu.com/svn_public/marcocantu_delphi_sessions_demos (updated, was wrong in the original post)
To see the source code of this specific demo online you can refer to:
code.marcocantu.com/trac/marcocantu_delphi_sessions_demos/browser/restclient/MobileMusicClient
More Demos to Come
Hope to have time to add all of my recent demos to this repository in the coming weeks.