Jump to content

How to create api ?


robinhodemorais

Recommended Posts

Hello, in unigui I have how to create an api?
For example, I needed to send a post to an api in unigui in json, when the third party application send unigui will handle that json.

how can i do it, researched it and found it a little choppy about OnHTTPDocument, but nothing concrete, can someone help me?

Link to comment
Share on other sites

9 hours ago, robinhodemorais said:

Hello, in unigui I have how to create an api?
For example, I needed to send a post to an api in unigui in json, when the third party application send unigui will handle that json.

how can i do it, researched it and found it a little choppy about OnHTTPDocument, but nothing concrete, can someone help me?

This is very usful for many situations.

Regards

Link to comment
Share on other sites

 const
     SERVER_PORT='888';
     SERVER_ROOT='api';

 

type
  TSQLRestServerTest = class(TSQLRestServerDB)

  published
    procedure Sum(Ctxt: TSQLRestServerURIContext);
    procedure MaxId(Ctxt: TSQLRestServerURIContext);

end;

....

var
  SerwerXL: TSerwerXL;
  aModel: TSQLModel;
  aProps: TSQLDBConnectionProperties;
  aRestServer:TSQLRestServerTest;
  aHttpServer: TSQLHttpServer;

 

procedure TSQLRestServerTest.Sum(Ctxt: TSQLRestServerURIContext);
var a,b: double;
    res: ISQLDBRows;
begin

  if UrlDecodeNeedParameters(Ctxt.Parameters,'A,B') then begin
    while Ctxt.Parameters<>nil do begin
      UrlDecodeDouble(Ctxt.Parameters,'A=',a);
      UrlDecodeDouble(Ctxt.Parameters,'B=',b,@Ctxt.Parameters);
    end;

    Ctxt.Results([a+b],HTTP_SUCCESS,TEXT_CONTENT_TYPE_HEADER);

end;

 

procedure TSQLRestServerTest.MaxId(Ctxt: TSQLRestServerURIContext);
var a,b: Integer;
    res: ISQLDBRows;
    id:Int64;
begin

  if UrlDecodeNeedParameters(Ctxt.Parameters,'idpos,rodzaj') then
   begin
    a:=Ctxt.InputInt['idpos'];
    b:=Ctxt.InputInt['rodzaj'];
      if (a<>0) and (b<>0) then
       begin
        res:=aProps.ExecuteInlined('Exec NSMaxNumer '+IntToStr(a)+','+IntToStr(b),true);

        Ctxt.Returns(res.FetchAllAsJSON(true),HTTP_SUCCESS,TEXT_CONTENT_TYPE_HEADER);
       end
      else
       Ctxt.Returns('Error',HTTP_SUCCESS,TEXT_CONTENT_TYPE_HEADER);

  end else
   begin
    Ctxt.Returns('Error',HTTP_SUCCESS,TEXT_CONTENT_TYPE_HEADER);

   end;

end;

 

procedure TSerwerXL.ServiceStart(Sender: TService; var Started: Boolean);
begin
 try
   CoInitialize(nil);
   SQLite3Log.Family.Level := LOG_VERBOSE;// STACKTRACE;
   SQLite3Log.Family.PerThreadLog := ptIdentifiedInOnFile;

   aProps := TOleDBMSSQL2012ConnectionProperties.Create(AdresIP

                                                      ,DataBase
                                                      ,DBUser
                                                      ,DBPassword);
   aModel := DataModel(SERVER_ROOT);
   VirtualTableExternalRegisterAll(aModel,aProps);
    try
      // create the main mORMot server
      aRestServer := TSQLRestServerTest.Create(aModel,true);
      // TSQLRestServerDB.Create(aModel,false); // authentication=false
      try
        // create tables or fields if missing
        try
         aRestServer.CreateMissingTables;
        except

        end;

        aRestServer.AcquireExecutionMode[execORMWrite]:=amBackgroundThread;
        aProps.ConnectionTimeOutMinutes:=10;
        aRestServer.DB.Synchronous:=smOff;
        aRestServer.DB.LockingMode:=lmExclusive;

         aHttpServer := TSQLHttpServer.Create(SERVER_PORT,[aRestServer],'+',HTTP_DEFAULT_MODE,32,secSynShaAes);

        try
          aHttpServer.AccessControlAllowOrigin := '*'; // allow cross-site AJAX queries
        finally
        end;

      finally
      end;

    finally
    end;
  finally
  end;
 Started:=true;
end;

 

in uDataModel.pas

type TSQLSTORNO=class(TSQLRecord)
   private
    fID_KASJERA   :INTEGER;
    fID_TOWARU    :Int64;
    fILOSC        :real;
    fCENA         :real;
    fRODZAJ       :RawUtf8;
    fID_PARAGONU  :Int64;
    fID_POZYCJI   :Int64;
    fDATA         :TDatetime;
    fWYSLANE      :INTEGER;
    fID_POS       :Integer;
   published
    property ID_KASJERA   :INTEGER read fID_KASJERA write fID_KASJERA;
    property ID_TOWARU    :Int64 read fID_TOWARU write fID_TOWARU;
    property ILOSC        :real read fILOSC write fILOSC;
    property CENA         :real read fCENA write fCENA;
    property RODZAJ       :RawUtf8 read fRODZAJ write fRODZAJ;
    property ID_PARAGONU  :Int64 read fID_PARAGONU write fID_PARAGONU;
    property ID_POZYCJI   :Int64 read fID_POZYCJI write fID_POZYCJI;
    property DATA         :TDatetime read fDATA write fDATA;
    property WYSLANE      :INTEGER read fWYSLANE write fWYSLANE;
    property ID_POS       :Integer read fID_POS write fID_POS;
end;

function DataModel(const RootURI: RawUTF8): TSQLModel;

 

function DataModel(const RootURI: RawUTF8): TSQLModel;
begin
 result := TSQLModel.Create(TSQLSTORNO[ ],RootURI);
end;

 

 

http://localhost:888/api/sum?a=1&amp;b=2.44

or

http://localhost:888/api/storno

 

this is how it looks in general. Launched as a widows service together with unigui.
This will not work with hyperserver

 

 

Link to comment
Share on other sites

9 hours ago, JarekZ said:

 const
     SERVER_PORT='888';
     SERVER_ROOT='api';

 

type
  TSQLRestServerTest = class(TSQLRestServerDB)

  published
    procedure Sum(Ctxt: TSQLRestServerURIContext);
    procedure MaxId(Ctxt: TSQLRestServerURIContext);

end;

....

var
  SerwerXL: TSerwerXL;
  aModel: TSQLModel;
  aProps: TSQLDBConnectionProperties;
  aRestServer:TSQLRestServerTest;
  aHttpServer: TSQLHttpServer;

 

procedure TSQLRestServerTest.Sum(Ctxt: TSQLRestServerURIContext);
var a,b: double;
    res: ISQLDBRows;
begin

  if UrlDecodeNeedParameters(Ctxt.Parameters,'A,B') then begin
    while Ctxt.Parameters<>nil do begin
      UrlDecodeDouble(Ctxt.Parameters,'A=',a);
      UrlDecodeDouble(Ctxt.Parameters,'B=',b,@Ctxt.Parameters);
    end;

    Ctxt.Results([a+b],HTTP_SUCCESS,TEXT_CONTENT_TYPE_HEADER);

end;

 

procedure TSQLRestServerTest.MaxId(Ctxt: TSQLRestServerURIContext);
var a,b: Integer;
    res: ISQLDBRows;
    id:Int64;
begin

  if UrlDecodeNeedParameters(Ctxt.Parameters,'idpos,rodzaj') then
   begin
    a:=Ctxt.InputInt['idpos'];
    b:=Ctxt.InputInt['rodzaj'];
      if (a<>0) and (b<>0) then
       begin
        res:=aProps.ExecuteInlined('Exec NSMaxNumer '+IntToStr(a)+','+IntToStr(b),true);

        Ctxt.Returns(res.FetchAllAsJSON(true),HTTP_SUCCESS,TEXT_CONTENT_TYPE_HEADER);
       end
      else
       Ctxt.Returns('Error',HTTP_SUCCESS,TEXT_CONTENT_TYPE_HEADER);

  end else
   begin
    Ctxt.Returns('Error',HTTP_SUCCESS,TEXT_CONTENT_TYPE_HEADER);

   end;

end;

 

procedure TSerwerXL.ServiceStart(Sender: TService; var Started: Boolean);
begin
 try
   CoInitialize(nil);
   SQLite3Log.Family.Level := LOG_VERBOSE;// STACKTRACE;
   SQLite3Log.Family.PerThreadLog := ptIdentifiedInOnFile;

   aProps := TOleDBMSSQL2012ConnectionProperties.Create(AdresIP

                                                      ,DataBase
                                                      ,DBUser
                                                      ,DBPassword);
   aModel := DataModel(SERVER_ROOT);
   VirtualTableExternalRegisterAll(aModel,aProps);
    try
      // create the main mORMot server
      aRestServer := TSQLRestServerTest.Create(aModel,true);
      // TSQLRestServerDB.Create(aModel,false); // authentication=false
      try
        // create tables or fields if missing
        try
         aRestServer.CreateMissingTables;
        except

        end;

        aRestServer.AcquireExecutionMode[execORMWrite]:=amBackgroundThread;
        aProps.ConnectionTimeOutMinutes:=10;
        aRestServer.DB.Synchronous:=smOff;
        aRestServer.DB.LockingMode:=lmExclusive;

         aHttpServer := TSQLHttpServer.Create(SERVER_PORT,[aRestServer],'+',HTTP_DEFAULT_MODE,32,secSynShaAes);

        try
          aHttpServer.AccessControlAllowOrigin := '*'; // allow cross-site AJAX queries
        finally
        end;

      finally
      end;

    finally
    end;
  finally
  end;
 Started:=true;
end;

 

in uDataModel.pas

type TSQLSTORNO=class(TSQLRecord)
   private
    fID_KASJERA   :INTEGER;
    fID_TOWARU    :Int64;
    fILOSC        :real;
    fCENA         :real;
    fRODZAJ       :RawUtf8;
    fID_PARAGONU  :Int64;
    fID_POZYCJI   :Int64;
    fDATA         :TDatetime;
    fWYSLANE      :INTEGER;
    fID_POS       :Integer;
   published
    property ID_KASJERA   :INTEGER read fID_KASJERA write fID_KASJERA;
    property ID_TOWARU    :Int64 read fID_TOWARU write fID_TOWARU;
    property ILOSC        :real read fILOSC write fILOSC;
    property CENA         :real read fCENA write fCENA;
    property RODZAJ       :RawUtf8 read fRODZAJ write fRODZAJ;
    property ID_PARAGONU  :Int64 read fID_PARAGONU write fID_PARAGONU;
    property ID_POZYCJI   :Int64 read fID_POZYCJI write fID_POZYCJI;
    property DATA         :TDatetime read fDATA write fDATA;
    property WYSLANE      :INTEGER read fWYSLANE write fWYSLANE;
    property ID_POS       :Integer read fID_POS write fID_POS;
end;

function DataModel(const RootURI: RawUTF8): TSQLModel;

 

function DataModel(const RootURI: RawUTF8): TSQLModel;
begin
 result := TSQLModel.Create(TSQLSTORNO[ ],RootURI);
end;

 

 

http://localhost:888/api/sum?a=1&amp;b=2.44

or

http://localhost:888/api/storno

 

this is how it looks in general. Launched as a widows service together with unigui.
This will not work with hyperserver

 

 

Thanks for the help, you will do some tests, but it will not work with Hypper Server, you will not be able to put it into production, but in any case it will be used for studies .....

A doubt, do you know the features of the onHttpDocument and onHttpCommand events?

Link to comment
Share on other sites

  • 3 weeks later...
On 25/02/2020 at 13:48, robinhodemorais said:

Obrigado pela ajuda, você fará alguns testes, mas ele não funcionará com o Hypper Server, você não poderá colocá-lo em produção, mas, em qualquer caso, será usado para estudos ...

Uma dúvida, você conhece os recursos dos eventos onHttpDocument e onHttpCommand?

Robinho vc conseguiu alguma coisa sobre esse tópico aqui?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...