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 DataFlex 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 DataFlex application.
Creating Your Application¶
If you don’t already have it open, launch DataFlex Studio.
From the DataFlex Studio toolbar, select File > New Workspace.
The New Workspace Wizard appears. Select Next, then enter a name, such as ThriftlyDataFlexExampleAPIs, in the Name of the New Workspace: field. Continue through the New Workspace Wizard prompts and select Finish to create your workspace.
Now, you must add the Thriftly library to your project, so your application can call the Thriftly Server and include the {Published = True} attribute. To do so:
First, if you are using a version of DataFlex Studio other than 18.2, you must adjust the Thriftly library so it works with your version of DataFlex Studio:
From the DataFlex Studio toolbar, select Open File(s).
Browse to
<User>\Documents\Thriftly Developer\Dataflex\Library
. Then, select and open ThriftlyLib.When ThriftlyLib.sws opens in DataFlex Studio, update the Version= string to reference your version of DataFlex Studio.
From the DataFlex Studio toolbar, select Tools > Maintain Libraries. The Library Maintenance window appears.
Select Add Library, then browse to
<User>\Documents\Thriftly Developer\Dataflex\Library
.Select ThriftlyLib, then select Open. A dialog will ask whether you want to add the library using relative pathing. Select Yes to do so.
When you return to the Library Maintenance window, select OK.
With the Thriftly library imported, you’re ready to create your project. Select File > New > Project to open the Create New window. Select Thriftly API Project. Then, select OK.
When the Create a New Thriftly API Project window appears, name your project (e.g. ExampleThriftlyAPIs) and select OK. Your project opens in the main DataFlex Studio workspace.
Now that your application’s set up, let’s look at a few of the key Thriftly-related directives that it automatically includes:
The Use Thriftly.pkg directive allows your application to include a variety of Thriftly-related directives and metadata.
oThriftly is a cThriftlyServer creates a ThriftlyServer object. This object will hold the Thriftly services we’ll create in the next set of setup steps.
Send StartServer of oThriftly starts the Thriftly Server when you run your application.
Before you move on to create the services that you’ll make available as APIs, you need to update your app’s .src file to reference those services. When you’re done, your .src file should read as below.
Be sure to examine this code and take particular note of the directives we pointed out above, so you understand both how to connect an app to the Thriftly Server and how to call the services you want to publish as 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 Thriftly-related directives and attributes. These pieces of code define what services and functions are published to the Thriftly Server as APIs.
The first service will operate on primitive data types and include functions that return a string and a boolean value. To create this service:
From the DataFlex Studio toolbar, select File > New > Class. The Create New window appears.
Select Thriftly API Service, then select OK. When the Create a New Thriftly API Service window appears, enter PrimitivesService in the File Name: field. Then, select OK again.
Modify the default code that appears in DataFlex Studio to read as shown below.
Note how the code:
Uses the Use cThriftlyService.pkg and Object oMyService Is a cThriftlyService directives to specify that this class is a Thriftly service.
Uses the {Published = True} attribute to specify which functions are accessible from your API.
Save (File > Save) your service.
The second service will operate on arrays and include functions that return a list and a boolean value. To create this service:
From the DataFlex Studio toolbar, select File > New > Class. The Create New window appears.
Select Thriftly API Service, then select OK. When the Create a New Thriftly API Service window appears, enter ArraysService in the File Name: field. Then, select OK again.
Modify the default code to read as shown below. Again, note how the code uses ThriftlyService directives and the {Published = True} attribute:
Save (File > Save) the service.
Running Your New Application and Testing Your APIs¶
Play around with your new application a bit to see how the APIs you created work.
From the DataFlex Studio toolbar, select the Run button to run your application.
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.