Creating the EMS Pets Client
Go Up to Tutorial: Using an EMS Client to Access a Custom EMS Resource
You can create an EMS Client application to connect to the EMS Server through the REST API that the RAD Server Engine (EMS Server) exposes.
To access the EMS Server Resources and information from the EMS database, the EMS Pets Client application of this tutorial uses the developed custom EMS Pets resource.
In this tutorial, you need the following components in your EMS Client application:
- TheTEMSProvider component is used to set up the connection with the RAD Server Engine (EMS Server).
- The TBackendEndpoint component is used to access a particular EMS Resource Endpoint method from your EMS Client application (one of the EMS Server REST API methods). An EMS Client application calls the TBackendEndpoint to GET or POST JSON from/to the EMS Server.
- Note: Each TBackendEndpoint component is configured to access an EMS Resource Endpoint. Add as many TBackendEndpoint as EMS Resource Endpoint methods are accessed.
To make a call to an EMS Resource Endpoint, you need to select the proper method to the Method property of the TBackendEndpoint component.
Contents
Creating an EMS Client Application
- Create a new Multi-Device Application:
- For Delphi: File > New > Multi-Device Application - Delphi
- In the Tool Palette, search for a TEMSProvider component and drop it to the TForm.
- In the Object Inspector, configure the EMS server parameters according to the EMS server configuration:
- URLHost:
localhost
- URLPort:
8080
- URLProtocol:
http
- URLHost:
- Click the Test Connection button. If the connection is set up successfully, you get a message with the current version of the EMS Server.
Calling a Resource Endpoint Method
You can call the EMS Server resource endpoints to retrieve or set data by making REST API calls from your EMS Client application.
In this tutorial, you are using the custom resource endpoints that extend the EMS Server functionality from Creating the EMS Pets Resource. The EMS Resource Endpoint Methods are defined in EMS Pets Resource.
- In your EMS Client application, drop a TBackendEndpoint component to the TForm.
- In the Object Inspector, rename the TBackendEndpoint component to BackendEndpointGetPets.
- In the Object Inspector, set the following parameters of the TBackendEndpoint to use the EMS Server resource:
- Provider: In the drop-down list, select
EMSProvider1
. - Resource:
Pets
. - Resource Suffix: Leave it blank for the tutorial.
- Method:
rmGET
.
- Provider: In the drop-down list, select
- If the parameters are properly set, you will get a successful response from the EMS Server while testing the configuration of the TBackendEndpoint component. You can either:
- In the Object Inspector, click Execute.
- Right-click the TBackendEndpoint component | Execute. If the response from the endpoint is sucessful, you get the following message:
Response: 200 - OK
- In the RAD Server Engine Window, the log pane shows the Version request.
- Note: You need to add as many TBackendEndpoint as EMS Resource Endpoint methods are defined in EMS Pets Resource.
Name Provider Resource Resource Suffix Method BackendEndpointGetPet
EMSProvider1
Pets
{item}
rmGET
BackendEndpointPostPets
EMSProvider1
Pets
(leave it blank)
rmPOST
BackendEndpointPutPet
EMSProvider1
Pets
{item}
rmPUT
BackendEndpointDeletePet
EMSProvider1
Pets
{item}
rmDELETE
- Drop a TMemo component to the TForm.
- Rename the TMemo to ServerResponse.
- In the Object Inspector, for the TMemo component set the property ReadOnly to
True
. - For this tutorial, drop 5 TButton components to your TForm.
- In the Object Inspector, set the Name and the Text property of each button as follows:
Button 1 Button 2 Button 3 Button 4 Button 5 Name
ButtonAddPet
ButtonDelete
ButtonGetPet
ButtonGetPets
ButtonUpdate
Text
Add Pet
Delete Pet
Get Pet
Get Pets
Update Pet
- Drop four TEdit components to your form. Rename them to
EId
,EName
,EKind
, andEPetId
. - Drop four TLabel components to your form. In the Object Inspector, set the Text property of each one to
Id
,Name
,Kind
, andPet Id
.
Adding the Pet Class to Your EMS Client
The EMS Client uses the same user-defined object that has been created for the EMS Pet Resource.
Add the Pet type unit in your EMS Client application:
- In the Projects Window | right-click in the project | Add New > Unit.
- Rename the unit to PetType.
- Add the PetType Unit code from Creating the Pet Type to your unit.
Creating the EMS Pets Resource Endpoint Methods
You need to create the request to the EMS Pets Resource Endpoint methods for each of the buttons of your application. You can also visually bind the response data (from the EMS Server) to a component in your EMS Pets Client application.
- Create a private function in your class to retrieve the data from the EMS Server.
- For Delphi:
// To Get all pets procedure TForm1.GetAllPets; begin BackendEndpointGetPets.Execute; ServerResponse.Text := BackendEndpointGetPets.Response.JSONText; end;
- Create the onClick event of each TButton component (double-click it) dropped in the TForm.
- Add the following code to the event handler implementation to the events:
- For Delphi:
// To Add a Pet procedure TForm1.ButtonAddPetClick(Sender: TObject); var LJSONObject: TJSONObject; LPet: TPet; begin LJSONObject := TJSONObject.Create; LPet.Create(EId.Text.ToInteger, EName.Text, EKind.Text); try TPetJSON.PetToJSON(LPet, LJSONObject); BackendEndpointPostPets.ClearBody; BackendEndpointPostPets.AddBody(LJSONObject); BackendEndpointPostPets.Execute; GetAllPets; finally LJSONObject.Free; end; end; // To Get all pets procedure TForm1.ButtonGetPetsClick(Sender: TObject); begin GetAllPets end; // To Get a Pet procedure TForm1.ButtonGetPetClick(Sender: TObject); var LJSON: TJSONValue; APet: TPet; begin BackendEndpointGetPet.Params.Items[0].Value := EPetId.Text; BackendEndpointGetPet.Execute; LJSON := BackendEndpointGetPet.Response.JSONValue; if LJSON <> nil then begin APet := TPetJSON.JSONToPet(LJSON); ServerResponse.Text := BackendEndpointGetPet.Response.JSONText; EName.Text := APet.Name; EKind.Text := APet.Kind; EId.Text := APet.Id.ToString; end; end; procedure TForm1.ButtonDeleteClick(Sender: TObject); begin BackendEndpointDeletePet.Params.Items[0].Value := EPetId.Text; BackendEndpointDeletePet.Execute; GetAllPets; end; // To update a pet procedure TForm1.ButtonUpdateClick(Sender: TObject); var LJSONObject: TJSONObject; LPet: TPet; begin LJSONObject := TJSONObject.Create; LPet.Create(0, EName.Text, EKind.Text); try BackendEndpointPutPet.Params.Items[0].Value := EId.Text; TPetJSON.PetToJSON(LPet, LJSONObject); BackendEndpointPutPet.ClearBody; BackendEndpointPutPet.AddBody(LJSONObject); BackendEndpointPutPet.Execute; GetAllPets; finally LJSONObject.Free; end; end;
- Your EMS Pets Client class declaration:
- For Delphi:
TForm1 = class(TForm) EMSProvider1: TEMSProvider; BackendEndpointGetPets: TBackendEndpoint; BackendEndpointGetPet: TBackendEndpoint; BackendEndpointPostPets: TBackendEndpoint; BackendEndpointPutPet: TBackendEndpoint; BackendEndpointDeletePet: TBackendEndpoint; ButtonAddPet: TButton; ButtonGetPets: TButton; ButtonUpdate: TButton; ButtonGetPet: TButton; ButtonDelete: TButton; EId: TEdit; EName: TEdit; EKind: TEdit; LId: TLabel; LName: TLabel; LKind: TLabel; ServerResponse: TMemo; EPetId: TEdit; LPetId: TLabel; procedure ButtonAddPetClick(Sender: TObject); procedure ButtonGetPetsClick(Sender: TObject); procedure ButtonUpdateClick(Sender: TObject); procedure ButtonGetPetClick(Sender: TObject); procedure ButtonDeleteClick(Sender: TObject); private procedure GetAllPets; public { Public declarations } end;
- Build and run your application. Press F9 or Run >Run.
- Note: The EMS Server should be running and the EMS Pets Resource has been previously loaded to it.