TMS RADical WEB, Easy REST Server integration with TMS XData Client

Bookmarks: 

Monday, February 26, 2018

One very cool feature of TMS RADical WEB (if not the main one) is that it allows Delphi developers to build Web applications that do not need a server connection. It's just HTML and JS files that run in the browser.

But we all know that there are some applications that need to retrieve data from somewhere, and also eventually persist data. We do not need a server to "run" the application, but we eventually might need communicate to a server to ask for data, perform centralized operations, authentication, etc.

While you can use any API server you want from the Web application, when using TMS XData to build the server your life will be way easier:

  • First, because with TMS XData you can write your backend server using Delphi and Pascal.
  • Also, because TMS XData is simply way too productive - it's very easy and fast to publish entities directly from database and to create service operations in a snap.
  • And, finally, because TMS XData provides first class support to TMS WEB Core, providing components that make it very easy and straightforward to connect to a XData server

The following screenshot shows the TMS XData Web Client demo in action. It's a sample that connects to a public XData REST server that holds information about music artists, albums, genres and tracks. With this demo you can retrieve and modify data from this server.



Note that this demo is dynamic. It means you can simply change the URL of server and it will work with any TMS XData Server you might have, regardless of the entities it publishes. The server provides the metadata for the whole API, and the client loads it at runtime allowing you to access the API in a very high level.

Also design-time components are available for you to simply assign properties and set handlers for the events that will fire when the server request is finished.



In case you're curious about how it's accomplished, here is the full source code for the implementation section of the single unit of the demo. The interface part was removed just because it's a bunch of declarations of the visual control fields.
procedure TForm1.btDeleteClick(Sender: TObject);
begin
  XDataWebClient1.Delete(SelectedEntitySet, TJSObject(TJSJSON.parse(DeleteMemo.Lines.Text)));
end;

procedure TForm1.btGetClick(Sender: TObject);
begin
  XDataWebClient1.Get(SelectedEntitySet, SelectedId);
end;

procedure TForm1.btListClick(Sender: TObject);
begin
  XDataWebClient1.List(SelectedEntitySet, edQuery.Text);
end;

function TForm1.SelectedEntitySet: string;
begin
  Result := cbEntitySets.Items[cbEntitySets.ItemIndex];
end;

function TForm1.SelectedId: Integer;
begin
  Result := StrToInt(edId.Text);
end;

procedure TForm1.ShowInfo(const Msg: string; Error: Boolean);
begin
  InfoLabel.Caption := Msg;
  if Error then
    InfoLabel.Font.Color := clRed
  else
    InfoLabel.Font.Color := clWindowText;
  InfoLabel.Visible := True;
end;

procedure TForm1.btPostClick(Sender: TObject);
begin
  XDataWebClient1.Post(SelectedEntitySet, TJSObject(TJSJSON.parse(PostMemo.Lines.Text)));
end;

procedure TForm1.btPutClick(Sender: TObject);
begin
  XDataWebClient1.Put(SelectedEntitySet, TJSObject(TJSJSON.parse(PutMemo.Lines.Text)));
end;

procedure TForm1.WebFormCreate(Sender: TObject);
begin
  ServerLabel.Caption := Format('XData Server URL: %s', [XDataConnection1.URL]);
  XDataConnection1.LoadModel;
end;

procedure TForm1.XDataConnection1ModelLoad(Sender: TObject);
var
  Container: TXDataEntityContainer;
  I: Integer;
begin
  Container  := XDataConnection1.Model.DefaultEntityContainer;

  cbEntitySets.Items.Clear;
  for I := 0 to Container.EntitySets.Count - 1 do
    cbEntitySets.Items.Add(Container.EntitySets[I].Name);
  if cbEntitySets.Items.Count > 0 then
    cbEntitySets.ItemIndex := 0;
end;

procedure TForm1.XDataWebClient1Error(Error: TXDataClientError);
begin
  ShowInfo(
    Format('%s. RequestId: %s. Code: %s. Request Url: %s',
      [Error.ErrorMessage, Error.RequestId, Error.ErrorCode, Error.RequestUrl]),
    True
  );
end;

procedure TForm1.XDataWebClient1Load(Response: TXDataClientResponse);
begin
  InfoLabel.Visible := False;
  if Response.RequestId = 'get' then
    GetMemo.Lines.Text := TJSJson.stringify(Response.Result)
  else
  if Response.RequestId = 'list' then
    ListMemo.Lines.Text := TJSJson.stringify(Response.Result)
  else
  if Response.RequestId = 'post' then
    ShowInfo(Format('New entity created in %s collection.', [SelectedEntitySet]))
  else
  if Response.RequestId = 'put' then
    ShowInfo(Format('Entity updated in %s collection.', [SelectedEntitySet]))
  else
  if Response.RequestId = 'delete' then
    ShowInfo(Format('Entity removed from %s collection.', [SelectedEntitySet]));
end;

Get started today: Technical previews of TMS WEB Core, TMS FNC UI web-enabled controls, web-enabled TMS XData, the first parts under the TMS RADical WEB umbrella are exclusively available now for all active TMS-ALL-ACCESS customers.

Wagner R. Landgraf


Bookmarks: 

This blog post has not received any comments yet.



Add a new comment:
Author:
Email:
  You will receive a confirmation mail with a link to validate your comment, so please use a valid email address.
Comment:
 
Change Image
Fill in the characters from the image above:
 

All fields are required.
 




Previous  |  Next  |  Index