Creating Your Own Basic Thriftly APIs¶
Now that you’ve examined our example application and APIs, we’ll walk you through creating your own simple application that includes two Thriftly APIs. These APIs, built from Delphi services, will return strings, boolean values, and lists. One service will operate on primitive data types, and the other will operate on arrays.
To get started, we’ll create a new Delphi application.
Creating Your Application¶
If you don’t already have it open, launch Delphi RAD Studio.
From the RAD Studio toolbar, select File > New > Other.
The New Items window appears. Select Delphi Projects > Console Application. Then, select OK to create a new console application. Your new project appears in RAD Studio.
Now, you must add the Thriftly.Server library to your project, so your application can call the Thriftly Server and include the [PublishedAttribute]. To do so:
From the RAD Studio toolbar, select Project > Options. The Project Options window appears.
With Delphi Compiler selected within the left sidebar, select Search path. Then, select the … button that appears to open the Search path window.
Within the Search path window, select the button. Then, browse to
<User>\Documents\Thriftly Developer\Delphi\Property Tracker\Delphi.Thriftly
and select (but do not open) the Debug folder. Then, select the Select Folder button.When you return to the Search path window, select Add. Then, select OK.
The new search path appears in the Project Options window. Select OK again.
In the Project Manager, the Debug reference now appears within the Build Configurations section.
Now that you have your application set up, you must modify it to:
Reference the ThriftlyServer library.
Create a Thriftly Server object.
Call the services we’ll create and define in the next set of steps.
Send the StartServer command to the ThriftlyServer object.
Basically, you need to modify your project’s .exe file so it reads like the code below:
Be sure to examine this code as you integrate it into your project, rather than just mindlessly copying it over. This code shows you how to connect an app to the Thriftly Server (which lets you access your APIs) and how to call the services that define your APIs. You’ll create these services in the next set of setup steps.
Creating the Services That Define Your APIs¶
You’ll now create the two services that define your APIs. As you create these services, note where and how you include the uses Attributes directive and [PublishedAttribute] attribute. These two pieces of code define what services and functions are published to the Thriftly Server as APIs.
The first service you create will operate on primitive data types and include functions that return a string and a boolean value. To create this service:
From the RAD Studio toolbar, select File > New > Unit - Delphi.
Your new service appears in RAD Studio. Modify the default code to read as shown below. Again, note the placement of the uses Attributes directive and the [PublishedAttribute]:
Save (File > Save As) the service as PrimitiveService.pas.
The second service you create will operate on arrays and include functions that return a list and a boolean value. To create this service:
From the RAD Studio toolbar, select File > New > Unit - Delphi.
Your new service appears in RAD Studio. Modify the default code to read as shown below. Again, note the uses Attributes directive and the [PublishedAttribute]:
Save (File > Save As) the service as ArraysService.pas.
Running Your New Application and Testing Your APIs¶
Play around with your new application a bit to see how the APIs you created work.
Return to your project’s .exe file.
From the RAD Studio toolbar, select the Run button.
When you run your application, the Thriftly Developer window appears. This window allows you to start configuring and testing your APIs. To take the APIs you just created out for a test drive, refer to the Testing Your Thriftly API section.