Jump to content

Andrea Magni

  • Content Count

  • Joined

  • Last visited

Community Reputation

29 Excellent


About Andrea Magni

  • Birthday 05/24/1982

Technical Information

  • Delphi-Version
    Delphi 10.3 Rio

Recent Profile Visitors

319 profile views
  1. Andrea Magni

    Looking for beta testers of TFrameStand and TFormStand

    Unfortunately not... The TFrameStand functionalities should be the same, there are some changes due to refactoring (i.e. if you had event handlers for TFrameStand events, some argument changed type like TFrameInfo<TFrame> now being TSubjecTInfo, so you may need to adjust them on your side). TFormStand is the same concept but supporting TForm instead of TFrame. If you already have projects using TFrameStand, it would be helpful if you could confirm I broke nothing. Sincerely, Andrea
  2. Andrea Magni

    Image from Form has no value

    Hi, yes the HTML form should be a multipart/form-data one... Glad you solved 😉 Sincerely, Andrea
  3. Andrea Magni

    Looking for beta testers of TFrameStand and TFormStand

    Great, please check out the new version from https://github.com/andrea-magni/TFrameStand/tree/TSubjectStand (you can use Git or download a zip file if you are not confident with Git). Just beware to uninstall any previous version of TFrameStand before proceeding. Some changes in your code may be needed especially if you used TFrameStand events. For example where you had TFrameInfo<TFrame> as event handler argument, now you'll find TSubjectInfo (that is a new superclass of TFrameInfo<T>). If you encounter issues, just drop me a line. Thanks. Sincerely, Andrea
  4. Andrea Magni

    Looking for beta testers of TFrameStand and TFormStand

    Thanks, I relocated the blog some time ago and I set up a rewrite rule that for some reason is not working well. I changed the links and updated also the demo list. That document will change again soon however, I'll add section for TFormStand soon. Sincerely, Andrea
  5. I'm about to release TFormStand as a form-oriented twin of TFrameStand. To add support for TForm I had to refactor quite heavily TFrameStand code (now there is a common ancestor that is TSubjectStand and two inherited components TFrameStand and TFormStand). I am looking for: - people willing to test TFrameStand functionalities (regression tests) are still functioning; - people willing to test TFormStand Please reply to this message or contact me directly if you like to be involved. The new version is available in the TSubjectStand branch of TFrameStand GitHub repository: https://github.com/andrea-magni/TFrameStand/tree/TSubjectStand (Beware: uninstall any previous version of TFrameStand before installing the new one) Thanks Sincerely, Andrea
  6. Andrea Magni

    Shortening the URL

    Hi @Yaron, you can definitely get rid of the "/rest" prefix by setting the Engine BasePath property to empty string (by default the engine has 'DefaultEngine' as Name and '/rest/ as BasePath): class constructor TServerEngine.CreateEngine; begin FEngine := TMARSEngine.Create; try // Engine configuration FEngine.BasePath := ''; // <============== FEngine.Parameters.LoadFromIniFile; // Application configuration FEngine.AddApplication('DefaultApp', '/default', [ 'Server.Resources.*']); If you want to push this thing futher, you can even think about inheriting your own TMARSActivation class and shortcut request matching by overriding FindMethodToInvoke method. HTH Sincerely, Andrea
  7. Andrea Magni

    Get Exception message on client

    Sorry, it's my fault: MARS supports two http layer implementor on the client side: Indy and TNetHttpClient. Handling errors has some differences across the two, so it depends if you are using a TMARSClient (Indy) or TMARSNetClient (TNetHttpClient). Either ways, you can define an OnError handler for your TMARSApplication: uses FMX.Dialogs , MARS.Core.Utils, MARS.Client.Utils , IdHttp; var LException: EMARSClientHttpException; LIdException: EIdHTTPProtocolException; begin if AException is EMARSClientHttpException then begin LException := EMARSClientHttpException(AException); ShowMEssage('Error' + LException.Message + ' Details: ' + StreamToString(LException.Content)); AHandled := True; end; if AException is EIdHttpProtocolException then begin LIdException := EIdHttpProtocolException(AException); ShowMEssage('Error' + LIdException.Message); AHandled := True; end; end; IIRC I was not able to find a way to collect error details (that are sent back from the server to the client using the body of the response) when using Indy. I am open to discuss how to improve the overall mechanism, please express your thoughts (everybody!). I'll try to update the old demos (such as HelloWorld) to include error handling at some point. Sincerely, Andrea
  8. Andrea Magni

    Datasnap -> MARS conversion

    Change TDataSet to TFDDataSet and you'll be able to produce both TMediaType.APPLICATION_JSON and TMediaType.APPLICATION_JSON_FireDAC (MARS will match what client prefer, looking for the Accept header's value): [POST] function GetData([BodyParam] AStatement: TStatement): TFDDataSet; function THelloWorldResource.GetData(AStatement: TStatement): TFDDataSet; begin Result := FD.SetName<TFDDataSet>(FD.Query(AStatement.SQL), AStatement.Name); end; Beware to remove the explicit Produces attribute you had. You may want to use a REST debugger (I like Postman) to make a call using Accept: application/json-firedac or Accept: application/json to see the two serializations. Now you can add a TMARSClientResourceJSON to your client application, note the SpecificAccept header where I am instructing to set Accept header to application/json-firedac media type: object MARSClientResourceJSON1: TMARSClientResourceJSON Application = MARSApplication SpecificAccept = 'application/json-firedac' SpecificContentType = 'application/json' Resource = 'helloworld' end Then use the following code (TMainDataModule is a datamodule containing the REST components, TMARSClient, TMARSApplication, TMARSClientResourceJSON): uses MARS.Core.JSON, MARS.Core.Utils, MARS.Data.FireDAC.Utils; procedure TMainDataModule.RetrieveData(const ASQL, AName: string; const AOnCompleted: TProc<TFDMemTable>; const AOnError: TProc<Exception> = nil); begin MARSClientResourceJSON1.POSTAsync( procedure (ABody: TMemoryStream) var LJSON: TJSONObject; begin LJSON := TJSONObject.Create; try LJSON.WriteStringValue('SQL', ASQL); LJSON.WriteStringValue('name', AName); JSONValueToStream(LJSON, ABody); finally LJSON.Free; end; end , procedure (ARes: TMARSClientCustomResource) var LData: TArray<TFDMemTable>; begin LData := TFDDataSets.FromJSON((ARes as TMARSClientResourceJSON).Response as TJSONObject); try if (Length(LData)>0) and Assigned(AOnCompleted) then AOnCompleted(LData[0]); finally TFDDataSets.FreeAll(LData); end; end , AOnError ); end; Then you can have a call like: MainDataModule.RetrieveData('select * from EMPLOYEE', 'Employee' , procedure (ADataSet: TFDMemTable) begin FDMemTable1.Data := ADataSet.Data; // cloning dataset end , procedure (AError: Exception) begin ShowMessage(AError.ToString); end ); FDMemTable1 is a TFDMemTable to store data on the client side. Please consider: 1) code could have been more simple than this but I wanted to include the fact this call is actually made asynchronously (so if you need to transfer a large dataset or connection is not very fast, you are not going to hang your client application); 2) this is a good example how to retrieve a dataset and then have a single (synchronized) point where the resulting dataset is then cloned to a local TFDMemTable that can be easily involved in databinding (LiveBindings) without being affected by the async call. Please let me know if this helps. Sincerely, Andrea
  9. Andrea Magni

    Datasnap -> MARS conversion

    TMARSFDResource is tightly coupled with a server side resource that has this interface: [GET, IsReference] function Retrieve: TArray<TFDDataSet>; [POST] function Update([BodyParam] const ADeltas: TArray<TFDMemTable>): TArray<TMARSFDApplyUpdatesRes>; One example of such resource is TMARSFDDataModuleResource (MARS.Data.FireDAC.DataModule.pas) that is the base class of the datamodule in FireDAC Basic demo. The logic of this resource does not allow the client to push statements to server (the Retrieve method is marked with GET verb so you don't have body available to send statements to be executed to sever side). Also, the Update method needs to know the structure (statements) of dataset in order to proprerly apply deltas to them. You can build a different resource on the same idea but adding statements as input to both methods. The Retrieve part we already covered in the previous message (using POST verb). You may want to have a PUT verb to apply updates. On the client side, you can use a TMARSClientResourceJSON and the TFDDataSets utility class (MARS.Data.FireDAC.Utils unit) to manage persistence of an array of datasets and/or an array of deltas (have a look at MARS.Client.FireDAC unit for some examples). As said, it is a bit unusual to send statements to be executed from the client to the server so there's no specific integration in MARS for this. It should not be hard to implement and all building blocks are already there. However if you find difficulties just write on this forum and I'll be happy to help. Sincerely, Andrea
  10. Andrea Magni

    Get Exception message on client

    Hi @Michal S., for security reasons, I decided to avoid error messaging to automatically flow to the client. Error messages may contain sensitive data and by default MARS reply with an 'Internal Server Error' message if an Exception is raised in the server code. However: 1) for debug purposes, you can switch the Build Configuration from Release to Debug and have the error message appended after the Internal Server Error string. 2) in real code, if you want the message to flow to the client use a EMARSHttpException object: function THelloWorldResource.MyMethodWithException: TJSONObject; begin Result := TJSONObject.Create; try Result.WriteStringValue('msg', 'This method will raise an exception'); // raise Exception.Create('This is my custom error message'); // this will be seen as 'Internal server error' raise EMARSHttpException.Create('This is my custom error message'); // this message will flow to the client except Result.Free; raise; end; end; HTH. Sincerely, Andrea
  11. Hi @Yaron, if you are using MARS develop branch, you can do as the following example: uses MARS.Core.RequestAndResponse.Interfaces THelloWorldResource = class //... protected [Context] Response: IMARSResponse; public [GET, Path('cookietest')] function MyMethod(): TJSONObject; end; function THelloWorldResource.MyMethod: TJSONObject; begin Result := TJSONObject.Create; Result.WriteStringValue('msg', 'This method will also set MyCookie value to current time'); Response.SetCookie('MyCookie', TimeToStr(Now), 'localhost', '/', Now+1, True); end; If you are using MARS master branch, it should be very similar but you need to use TMARSResponse instead of IMARSResponse (and the MARS.Core.RequestAndResponse.Interfaces unit is not available). But the SetCookie method call should be the same. This example sets the MyCookie value to the current time (string) for 'localhost' domain (you may want to read the actual host from the incoming request) and for every path matching '/'. Expiration is set 1 day after the request and the Secure flag is set to True. Sincerely, Andrea
  12. Andrea Magni

    Datasnap -> MARS conversion

    @Arnaud Bouchez great to see you chiming in here :-) @ertank I agree with Arnaud that having the client sending the SQL statements to be executed on the server is basically breaking the separation between layers in the multi-tier system and this has consequences (changes in client and/or db structure are probably deeply tied). It is easy though to implement what you are asking for: 1) find a way to send SQL statements to the server (I would probably use a JSON structure in the body of a POST request) 2) grab the list (or the single) of SQL statements from the body 3) build an array of corresponding datasets 4) return that array to the client Extra: if you need to modify data and you are building a Delphi-To-Delphi system (Delphi client, Delphi server), you can even consider then posting back delta changes to the server and have these applied as in a C/S scenario (MARS has FireDAC integration for this task, just ask if it's your case). Simple example: 0) bootstrap a new MARS server application (using MARSCmd) or use an existing one 1) include these units: Data.DB, MARS.Data.FireDAC, FireDAC.Phys.FB (or whatever physical driver is needed for your DB) 2) define data structures: TStatement = record SQL: string; name: string; end; TStatements = TArray<TStatement>; 3) define a POST method having TStatements as input and an array of datasets as output: [Path('helloworld')] THelloWorldResource = class protected [Context] FD: TMARSFireDAC; public [POST, Path('data'), Produces(TMediaType.APPLICATION_JSON)] function GetData([BodyParam] AStatements: TStatements): TArray<TDataSet>; end; implement it: function THelloWorldResource.GetData(AStatements: TStatements): TArray<TDataSet>; var LStatement: TStatement; begin Result := []; for LStatement in AStatements do Result := Result + [ FD.SetName<TDataSet>(FD.Query(LStatement.SQL), LStatement.name) ]; end; 4) make the POST call from the client side (using MARS Client or whatever other http library available): POST /rest/default/helloworld/data HTTP/1.1 Host: localhost:8080 Accept: application/json Content-Type: application/json Host: localhost:8080 [ { "SQL": "select * from EMPLOYEE order by EMP_NO", "name": "Employees" } , { "SQL": "select * from COUNTRY where COUNTRY like 'I%'", "name": "Country" } ] 5) this is what the server response will look like: { "Employees": [ { "EMP_NO": 2, "FIRST_NAME": "Robert", "LAST_NAME": "Nelson", "PHONE_EXT": "250", "HIRE_DATE": "1988-12-28T00:00:00.000+01:00", "DEPT_NO": "600", "JOB_CODE": "VP", "JOB_GRADE": 2, "JOB_COUNTRY": "USA", "SALARY": 105900, "FULL_NAME": "Nelson, Robert" }, { "EMP_NO": 4, "FIRST_NAME": "Robert", "LAST_NAME": "Vecchio", "PHONE_EXT": "233", "HIRE_DATE": "1988-12-28T00:00:00.000+01:00", "DEPT_NO": "621", "JOB_CODE": "Eng", "JOB_GRADE": 2, "JOB_COUNTRY": "USA", "SALARY": 97500, "FULL_NAME": "Vecchio, Robert" } ], "Country": [ { "COUNTRY": "Italy", "CURRENCY": "Lira" } ] } To materialize back data into datasets on the client side you can use several strategies (including the Delphi REST Client library TRESTResponseDataSetAdapter, either using REST Client library components or MARS Client library components to perform the request). If you are using FireDAC on the client side and you can afford relying on having the same FireDAC version on client and server, consider also using the TMARSFDResource component (see MARS' FireDAC Basic demo). Sincerely, Andrea
  13. Hi @ertank, latest stable version is 1.3.2 (that currently is even with master branch). The develop branch is where development occurs and is a hundred commits ahead of the master branch currently. I would suggest you to use 1.3.2 in production and eventually check this page: https://github.com/andrea-magni/MARS/releases to see if it is still the latest from time to time. You also have there a kind-of what's changed information. If you are looking for latest features, then develop branch is there for you but beware things may change unexpectedly and bugs may be there. Sincerely, Andrea
  14. Hi @Yaron, web applications may authenticate following several different schemas. One of them is handling authentication and authorization through a token (Authentication Bearer schema) that for convenience may also be embedded into requests through the use of a cookie. This kind of authorization/authentication schema is built-in with MARS and relies on JWT (https://jwt.io/) technology. This means I've made some work to integrate JWT in MARS and it is provided as an example of authentication/authorization mechanism but it does not mean it is mandatory to use it, you can consider it as a working example. It is also production-ready BTW. The typical use case: 1) the client (JS or whatever) should get a token from the server (make a POST request to the included TTokenResource class) 2) store the token somewhere on the client-side (or rely on the cookie that the server instructs to keep when successful authentication is done) 3) endorse the token within each client request through the Authentication header ("Bearer <TOKEN>") or rely on browser sending the cookie back to the server On the server side, you may ask MARS to provide you an instance of TMARSToken through the Dependency Injection mechanism built-in with MARS. Just add a "[Context] TheToken: TMARSToken;" field to your resource class and you'll get a valid instance (check TheToken.Verified, TheToken.UserName and other properties). Also check the Authentication demo and try to open a browser at the following URL: http://localhost:8080/rest/default/token/html You can use Chrome Developer Tools to see requests and cookies. Let me know if this helps, otherwise I may add a demo of a simple webapp using Bootstrap and performing authentication. Sincerely, Andrea
  15. Andrea Magni


    Hi @Rafael Mattos, MARS server applications can be deployed in several ways. You can go for the ones relying on Webbroker (standalone indy server) and configure SSL there. Otherwise you can go for apache module/iis ISAPI and let the web server handle encryption. Last, if you are brave enough, you may try new Delphi Cross Socket integration (develop branch) and have SSL implemented that way. I tend to leave the REST server http only and ads a reverse-proxy apache instance in front of it, implementing https there (easy, well documented, reliable). Sincerely, Andrea