robinhodemorais Posted February 24, 2020 Share Posted February 24, 2020 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? Quote Link to comment Share on other sites More sharing options...
JarekZ Posted February 24, 2020 Share Posted February 24, 2020 unigui + synopse mormot Quote Link to comment Share on other sites More sharing options...
MOGSY Posted February 24, 2020 Share Posted February 24, 2020 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 Quote Link to comment Share on other sites More sharing options...
robinhodemorais Posted February 24, 2020 Author Share Posted February 24, 2020 9 hours ago, JarekZ said: unigui + synopse mormot can you help me with an example? can we use this framework directly on unigui, without having to create another service for the api? Wouldn't these features of the onHttpDocument and onHttpCommand unigui be for that? Quote Link to comment Share on other sites More sharing options...
robinhodemorais Posted February 24, 2020 Author Share Posted February 24, 2020 5 hours ago, MOGSY said: This is very usful for many situations. Regards I really need it too and if the unigui naturally has this feature it will be fantastic, but I didn't find anything helping with these servermodule resources Quote Link to comment Share on other sites More sharing options...
JarekZ Posted February 24, 2020 Share Posted February 24, 2020 What is the API supposed to do? Give a specific example of a function. Quote Link to comment Share on other sites More sharing options...
robinhodemorais Posted February 24, 2020 Author Share Posted February 24, 2020 1 hour ago, JarekZ said: What is the API supposed to do? Give a specific example of a function. i need to set up an api that will receive a post, this post will be a json of some information Quote Link to comment Share on other sites More sharing options...
JarekZ Posted February 25, 2020 Share Posted February 25, 2020 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&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 Quote Link to comment Share on other sites More sharing options...
robinhodemorais Posted February 25, 2020 Author Share Posted February 25, 2020 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&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? Quote Link to comment Share on other sites More sharing options...
eduardosuruagy Posted March 13, 2020 Share Posted March 13, 2020 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.