#1 2014-12-04 18:52:38

restneophyte
Member
Registered: 2014-11-18
Posts: 26

Need help accessing a 'mORMot' ReST server hosted externally

On my local machine, I have created a ReST Server and Client (code bewlow). When I run the Server locally, I am able via the Client to retrieve a list of products stored in a mySQL database hosted externally. My end goal is to run (at first as a console) my ReSt Server on a Windows instance hosted by Amazon.

I am able to 'install' and run my 'console' server on the Windows instance. However, for the life of me, I am unable to figure out how to modify the client so that it might access my 'mORMot' server. Any pointers and/or advices would be greatly appreciated.


My ReST Server is defined as follow:

uses
  ...,
  OurReST_Model;

var
  iModel: TSQLModel;
  iHttpServer: TSQLHttpServer;
  iRestServer: TSQLRestServerDB;
  iConnection: TSQLDBConnectionProperties;
begin
  SQLite3Log.Family.Level        := LOG_VERBOSE;
  SQLite3Log.Family.PerThreadLog := ptIdentifiedInOnFile;
  iConnection := TSQLDBZEOSConnectionProperties.Create(TSQLDBZEOSConnectionProperties.URI(dMySQL, c_mySQLDBServer + ':3306'), c_mySQLDB, c_mySQLDBUsername, c_mySQlDBPassword);
  try
    iModel := OurDataModel;
    VirtualTableExternalRegister(iModel, TCustomersProducts, iConnection, 'TableCustomersProducts');
    try
      iRestServer := TSQLRestServerDB.Create(iModel, ':memory:', false);
      try
        iRestServer.CreateMissingTables;
        iHttpServer := TSQLHttpServer.Create(SERVER_PORT, [iRestServer], '+', useHttpApiRegisteringURI);
        try
          writeln('Background server is running.'#10);
          write('Press [Enter] to close the server.');
          readln;
        finally
          iHttpServer.Free;
        end;
      finally
        iRestServer.Free;
      end;
    finally
      iModel.Free;
    end;
  finally
    iConnection.Free;
  end;
end.

'OurReST_Model' is defined as follow:

unit OurReST_Model;

interface

uses
  SynCommons,
  mORMot;

type
  TCustomersProducts = class(TSQLRecord)      // TSQLRecord has a built-in ID: integer primary key
    private
      fProductName: RawUTF8;
      fProductAddedOn: TDateTime;
    published
      /// ORM will create the table columns
      property ProductName: RawUTF8 index 12 read fProductName write fProductName;
      property ProductAddedOn: TDateTime read fProductAddedOn write fProductAddedOn;
  end;

function OurDataModel: TSQLModel;

const
  SERVER_ROOT = 'root';
  SERVER_PORT = '888';

implementation

function OurDataModel: TSQLModel;
begin
  Result := TSQLModel.Create([TCustomersProducts], SERVER_ROOT);
end;

end.

Within my 'client' VCL application, if I want to retrieve a list of products that belongs to a specific customer, I do the following:

function get_UserProducts(aUserID: Integer; var aClientDataSet: TClientDataSet): Boolean;
var
  iModel: TSQLModel;
  iClient: TSQLRestClientHTTP;
  iUserRecord: TCustomersProducts;
begin
  iModel  := GetModel;
  iClient := GetClient('localhost');
  try
    iUserRecord := TCustomersProducts.CreateAndFillPrepare(iClient, '', 'ID = ?', [aUserID]);
    try
      aClientDataSet.Close;
      aClientDataSet.Open;
      while iUserRecord.FillOne do begin
        aClientDataSet.Append;
        aClientDataSet.FieldByName('ProductName').AsString      := iUserRecord.ProductName;
        aClientDataSet.FieldByName('ProductAddedOn').AsDateTime := iUserRecord.ProductAddedOn;
        aClientDataSet.Post;
      end;
      Result := True;
    finally
      iUserRecord.Free;
    end;
  finally
    iClient.Free;
    iModel.Free;
  end;
end;

Offline

#2 2014-12-04 18:59:31

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Need help accessing a 'mORMot' ReST server hosted externally

For a vcl app, just use the same model as the server.
It is not mandatory nor advised to use the cross platform clients here.
Start from one of the supplied samples.

Offline

#3 2014-12-04 21:29:01

restneophyte
Member
Registered: 2014-11-18
Posts: 26

Re: Need help accessing a 'mORMot' ReST server hosted externally

@ab
I am running my 'client' as a VCL application for testing purposes only (it is faster than deploying to my mobile device each time I made a change). At the end, it will be running on a mobile device. That is why I am developing a cross platform client. For my own curiosity, why is not advised to use a cross platform client within a VCL app?

I am not sure if I understand your response in terms of use the same model as the server.

Offline

Board footer

Powered by FluxBB