Show: Object Pascal C++
Display Preferences

Creating the EMS Pets Client

From Appmethod Topics
Jump to: navigation, search

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

Creating an EMS Client Application

  1. Create a new Multi-Device Application:
    • For Object Pascal: File > New > Multi-Device Application - Object Pascal
  2. In the Tool Palette, search for a TEMSProvider component and drop it to the TForm.
  3. In the Object Inspector, configure the EMS server parameters according to the EMS server configuration:
    • URLHost: localhost
    • URLPort: 8080
    • URLProtocol: http
    EMSServerConnection.png
  4. 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.

  1. In your EMS Client application, drop a TBackendEndpoint component to the TForm.
  2. In the Object Inspector, rename the TBackendEndpoint component to BackendEndpointGetPets.
  3. 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 .
    TBackendEndpointGetPets1.png
  4. 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
  5. In the EMS Server Window, the log pane shows the Version request.
    UsersGetEMSServerLogGetPets.png
    Note: You need to add as many TBackendEndpoint as EMS Resource Endpoint methods are defined in EMS Pets Resource.
    Repeat the previous steps (1-4) until you configure the TBackendEndpoint components as shown in the following table:
    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


    EMSClientBackendEndpoints.png
  6. Drop a TMemo component to the TForm.
  7. Rename the TMemo to ServerResponse.
  8. In the Object Inspector, for the TMemo component set the property ReadOnly to True.
  9. For this tutorial, drop 5 TButton components to your TForm.
  10. 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

  11. Drop four TEdit components to your form. Rename them to EId, EName, EKind, and EPetId.
  12. Drop four TLabel components to your form. In the Object Inspector, set the Text property of each one to Id, Name, Kind, and Pet Id.
EMSPetsClientDesignTab.png

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:

  1. In the Project Manager | right-click in the project | Add New > Unit.
  2. Rename the unit to PetType.
  3. 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.

  1. Create a private function in your class to retrieve the data from the EMS Server.
    • For Object Pascal:
    // To Get all pets
    procedure TForm1.GetAllPets;
    begin
      BackendEndpointGetPets.Execute;
      ServerResponse.Text := BackendEndpointGetPets.Response.JSONText;
    end;
  2. Create the onClick event of each TButton component (double-click it) dropped in the TForm.
  3. Add the following code to the event handler implementation to the events:
    • For Object Pascal:
    // 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;
  4. Your EMS Pets Client class declaration:
    • For Object Pascal:
    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;
  5. Build and run your application. Press F9 or Run >Run.
    EMSPetsClientRunning.png
Note: The EMS Server should be running and the EMS Pets Resource has been previously loaded to it.

See Also

Personal tools
In other languages