REST easy with kbmMW #21 – Delphi client stubs

In previous blog post, I mentioned that the new stub generator framework had the features needed to also generate Delphi client stubs, to make it very easy to develop Delphi smart clients with full support for compile time type checking and IDE class/property assistance.

I did not expect to include it in the upcoming release of kbmMW, but I couldn’t help myself…. it had to be added to make a fairly complete initial stub generator release.

The stub generator will generate plain Delphi code, that require kbmMW smart client features to compile. But the code is complete, and contains all unit references needed. If you are using custom units containing objects, you will have to make those units available to the Delphi client developer along with the generated stub code.

The SimpleInvocation server will, in addition to OpenAPI support also support returning Delphi stub code directly via the REST interface.

Lets call it: http://localhost:888/myserver/delphi

This will return give you a dialog similar to this:

Saving it, the unit received will look like this:

As you may notice, all the methods in the SmartInvocation service that have been marked with the kbmMW_Method attribute has been made available in the stub code.

The stub generator automatically detected various other units which are required to compile, like kbmMemTable, and uObjects and added to the uses clause.

So how did we return the stub code from the REST interface?

In the unit containing the REST service, we simply added a function returning a string and made it accessible from REST.

The function implementation is pretty simple

Obviously you can call GenerateDelphi from any unit you wish, and you can reference any kbmMW Smartservice. The first argument to GenerateDelphi is an optional unit naming prefix. When nothing has been specified, the unitnames will start with ‘u’. In this case we will receive a file named uSMARTDEMO.pas since its the SMARTDEMO service that is being described in a Delphi client stub.

Lets try to use the generated code in a client. We create a standard VCL based client (kbmMW works fine with Firemonkey too for cross platform use). We add a client transport (in this case TkbmMWTCPIPIndyClientTransport), a client connection pool and some buttons that will activate the calls. In addition there is an edit box to type an IP address of the server we will want to connect to, if it is not running on the local host.

Then make sure to add uSMARTDEMO.pas to the uses clause of the unit. Now we have access to the stub code which makes it easy to call the Smart services hosted by the kbmMW application server.

The eventhandler for StorePerson 1 could look like this:

The sample here instantiates a TSMARTDEMO instance with the transport we would like to use. We could have provided the connection pool or a Smart Client instead if we wanted to. The stub code supports all 3 variations.

Further this button shows how to send an interfaced object to the server.

The eventhandler for StorePerson 2 could look like this:

The slight difference is that we provide an object for the call. After the call we free the object. All completely normal Delphi code.

The eventhandler for Simple Calls could look like this:

It shows various types being sent and received.

The eventhandler for GetPerson could look like this:

Again… just showing how to receive various types of objects (regular ones without use of kbmMWNullable, objects with use of kbmMWNullable and lists of objects).

Finally the eventhandler GetMemTable could look like this:

This sample show how a memory table instance containing 5 rows produced on the server, can be returned from the server to the client.

Can it be easier to make advanced application servers in Delphi, which supports all sorts of clients via REST or native Delphi code? I don’t think so 🙂

So… if you like what you see, please share the word. Reshare blog posts and let others know about the product!

Essentially help us to help you 🙂

258 total views, 7 views today

Author: kimbomadsen

Leave a Reply

Your email address will not be published. Required fields are marked *

1 − 1 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.