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

  1. If you don’t already have it open, launch Delphi RAD Studio.

  2. From the RAD Studio toolbar, select File > New > Other.

  3. 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.

  4. 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:

    1. From the RAD Studio toolbar, select Project > Options. The Project Options window appears.

    2. With Delphi Compiler selected within the left sidebar, select Search path. Then, select the button that appears to open the Search path window.

    The Search path button in RAD Studio's Project Options window
    1. Within the Search path window, select the folder_button 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.

    2. When you return to the Search path window, select Add. Then, select OK.

    3. 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.

The Debug Reference in the Project Manager

Now that you have your application set up, you must modify it to:

  • Reference the ThriftlyServer library.

The uses ThriftlyServer directive
  • Create a Thriftly Server object.

Creating a ThriftlyServer object
  • Call the services we’ll create and define in the next set of steps.

Creating and calling Thriftly services
  • Send the StartServer command to the ThriftlyServer object.

The StartServer command

Basically, you need to modify your project’s .exe file so it reads like the code below:

The example Delphi Project.exe file

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:

  1. From the RAD Studio toolbar, select File > New > Unit - Delphi.

  2. 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]:

The Delphi example PrimitivesService
  1. 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:

  1. From the RAD Studio toolbar, select File > New > Unit - Delphi.

  2. 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]:

The Delphi example ArraysService
  1. 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.

  1. Return to your project’s .exe file.

  2. 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.