#1 2012-12-04 19:04:29

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 12,637
Website

BREAKING CHANGE in TSQLRestServerCallBackParams

BREAKING CHANGE in TSQLRestServerCallBackParams use: all method-based services should now be a procedure, and use aParams.Results()/Error() methods to return the content - new definition of aParams features now full access to incoming/outgoing context and parameters.

See http://synopse.info/fossil/info/ace12b0996

In short:

function TServer.Method(var aParams: TSQLRestServerCallBackParams): integer;

shall be changed into

procedure TServer.Method(var Ctxt: TSQLRestServerCallBackParams);

(here Ctxt is used as parameter name since it was found to be more meaningful)

And instead of setting the HTTP status in result variable and aParams.Resp for returned body content or aParams.ErrorMsg^ in case of error, you have to use Ctxt.Returns() or Ctxt.Results() or Ctxt.Error() new methods.

For instance:

function TSQLRestServerTest.Sum(var aParams: TSQLRestServerCallBackParams): Integer;
var a,b: Extended;
begin
  if not UrlDecodeNeedParameters(aParams.Parameters,'A,B') then
  begin
    result := 404; // invalid Request
    aParams.ErrorMsg^ := 'Missing Parameter';
    exit;
  end;
  while aParameters<>nil do
  begin
    UrlDecodeExtended(aParams.Parameters,'A=',a);
    UrlDecodeExtended(aParams.Parameters,'B=',b,@aParams.Parameters);
  end;
  aParams.Resp := JSONEncodeResult([a+b]);
  // same as : aResp := JSONEncode(['result',a+b],TempMemoryStream);
  result := 200; // success
end;

shall be changed into the clearer syntax:

procedure TSQLRestServerTest.Sum(var Ctxt: TSQLRestServerCallBackParams);
var a,b: Extended;
  if UrlDecodeNeedParameters(Ctxt.Parameters,'A,B') then begin
    while Ctxt.Parameters<>nil do begin
      UrlDecodeExtended(Ctxt.Parameters,'A=',a);
      UrlDecodeExtended(Ctxt.Parameters,'B=',b,@Ctxt.Parameters);
    end;
    Ctxt.Results([a+b]);
  end else
    Ctxt.Error('Missing Parameter');
end;

Or even the easier to read/code/understand:

procedure TSQLRestServerTest.Sum(var Ctxt: TSQLRestServerCallBackParams);
begin
  with Ctxt do
    Results([InputDouble['a']+InputDouble['b']]);
end;

See http://blog.synopse.info/post/2010/07/1 … phi-7-2010 rewritten article.

Offline

#2 2013-10-15 18:16:16

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 12,637
Website

Re: BREAKING CHANGE in TSQLRestServerCallBackParams

WARNING!

New breaking change:

procedure TServer.Method(var Ctxt: TSQLRestServerCallBackParams);

should now be written as:

procedure TServer.Method(Ctxt: TSQLRestServerURIContext);

See http://synopse.info/fossil/info/c28e9e05c2

In fact, TSQLRestServerURIContext is now a true Delphi class, with virtual methods able to change the whole routing and execution scheme of any remote request.
In the close future the basic fixed TServiceRoutingMode = (rmREST, rmJSON_RPC) will certainly be replaced by dedicated classes.
No speed penalty has been observed (on the contrary), and TSQLRestServer.URI() code is now much easier to enhance/maintain.

The main blog article about method-based services and the associated documentation have been updated to reflect this change.

Offline

#3 2013-10-16 17:14:33

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,213
Website

Re: BREAKING CHANGE in TSQLRestServerCallBackParams

Very good changes. Previous URI method  implementation was pretty scary. Now everything much clear IMHO. Thanks!

Offline

#4 2013-10-18 20:53:26

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 12,637
Website

Re: BREAKING CHANGE in TSQLRestServerCallBackParams

That's it...
URI routing for interface-based service is now specified by the two TSQLRestRoutingREST and TSQLRestRoutingJSON_RPC classes (inheriting from the abstract TSQLRestServerURIContext class) instead of rmJSON and rmJSON_RPC enums - it allows any custom URI routing by inheritance .

See http://synopse.info/fossil/info/4359ace86e

Offline

Board footer

Powered by FluxBB