Delphi DA REST server versioning with code-first server

Hi,

if you dislike this workaround:

procedure bar; override;  // will call inherited

I can suggest other one. check this snippet: Using OnCustomResponseEvent in a ROSDK Server.

How it should work:

  • you have ROHttpApiDispatcher1. it handles v1 path.
  • you have ROHttpApiDispatcher2. it handles v2 path but his initialization is non-standard:
  Server.Active := True;

  // these lines are needed!
  // as a result, ROHttpApiDispatcher2 can handle `/v2` 
  // but `Server` will think that `/v2` is unhandled path
  ROHttpApiDispatcher2.Server := Server; 
  ROHttpApiDispatcher2.Activate;
  ROHttpApiDispatcher2.Server := nil;
  • you have OnCustomResponseEvent like
procedure TServerMainForm.ROServerCustomResponseEvent(
  const aTransport: IROHTTPTransport; const aRequestStream,
  aResponseStream: TStream; const aResponse: IROHTTPResponse;
  var aHandled: Boolean);

  // your method for detection of redirects
  // it can be hardcoded like my one
  // or you can read from global variable, etc
  function _CheckForRedirect(aValue: string): string;
  begin
    if aValue = '/v1/bar' then 
      Result := '/v2/bar' 
    else 
      Result := aValue;
  end;
var
   l_path: string;
   l_HttpApiDispatcher: TROHttpApiDispatcher;
begin
  // handle all requests started with '/v2'
  if (Pos('/v2/',aTransport.PathInfo) = 1) or (aTransport.PathInfo = '/v2') then begin    
    l_path := _CheckForRedirect(aTransport.PathInfo); 
    aTransport.PathInfo := l_path;
    if (Pos('/v2/', l_path) = 1) or (l_path = '/v2') then
      l_HttpApiDispatcher := ROHttpApiDispatcher2
    else if (Pos('/v1/', l_path) = 1) or (l_path = '/v1') then
      l_HttpApiDispatcher := ROHttpApiDispatcher1
    else 
      Exit;
    l_HttpApiDispatcher.Process(aTransport, aTransport as IROHTTPRequest, aResponse, aRequestStream, aResponseStream);
    aHandled := True;    
  end;
end;