Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works

Random project

REST4Delphi

REST4Delphi is a powerful framework based on DMVCFramework for REST web solution in Delphi

View on GitHub

Anyone may press Interesting, signed in or not. It helps others find this project.

Member rating

No ratings yet.

Sign in to rate this project.

README

REST For Delphi

The REST4Delphi is an API to facilitate the use of the project Delphi MVC Framework.

With this API you can create servers that provide REST services and consume REST services written in any programming language.

The REST4Delphi API works with Delphi XE3 or higher and the client works on all platforms.

External Dependencies

The REST4Delphi is coupled to DMVCFramework. Therefore this dependence is included in the project within the "dependencies" folder. You must put in the Path of his application or IDE the font directories of this dependence.

Samples

Within the project there are a few examples of API usage. In addition there are also some unit tests that can aid in the use of REST4Delphi.

Using REST4Delphi API

Using this API will is very simple, you simply add the Search Path of your IDE or your project the following directories:

  • REST4Delphi\src\

Then you must add to your project the WebModule, so that you can make to your heritage:

  • REST4D.WebModule.pas

REST4Delphi in 5 Minutes

Here is a step by step how to create a Delphi server application using the REST4D:

First, you must create your Controller that will provide the REST resources:

pascal
uses
  REST4D,
  REST4D.Server,
  System.Generics.Collections,
  System.SysUtils;
pascal
type
pascal
[Path('/')]
TUserController = class(TRESTController)
public
   [Path('/hello')]
   [HTTPMethod([THTTPMethod.httpGET])]
   procedure HelloWorld(ctx: TRESTWebContext);
pascal
[Path('/user')]
[HTTPMethod([THTTPMethod.httpGET])]
procedure GetUser(ctx: TRESTWebContext);
pascal
[Path('/user/save')]
[HTTPMethod([THTTPMethod.httpPOST])]
procedure PostUser(ctx: TRESTWebContext);
pascal
[Path('/users')]
[HTTPMethod([THTTPMethod.httpGET])]
procedure GetUsers(ctx: TRESTWebContext);
pascal
[Path('/users/save')]
[HTTPMethod([THTTPMethod.httpPOST])]
procedure PostUsers(ctx: TRESTWebContext);
pascal
end;

Now, you must inherit the REST4D.WebModule and create your own WebModule, implement the abstract method Initialize(const pRESTEngine: TRESTEngine) and SecurityLayer(out pRESTSecurity: IRESTSecurity) and add a public variable of type TComponentClass on your WebModule:

pascal
uses
  REST4D,
  REST4D.Server,
  REST4D.WebModule;
pascal
type
pascal
TBasicDemoWebModule = class(TRESTWebModule)
private
   { Private declarations }
strict protected
   procedure ConfigureRESTEngine(const pRESTEngine: TRESTEngine); override;
   function GetRESTSecurity(): IRESTSecurity; override;
public
   { Public declarations }  
end;
pascal
var
   BasicDemoWebModuleClass: TComponentClass = TBasicDemoWebModule;
pascal
implementation
pascal
uses
  User.Controller;
pascal
{$R *.dfm}
pascal
{ TAppWebModule }
pascal
procedure TAppWebModule.ConfigureRESTEngine(const pRESTEngine: TRESTEngine);
begin
  inherited;
  pRESTEngine.AddController(TAppController);
end;
pascal
function TAppWebModule.GetRESTSecurity: IRESTSecurity;
begin
  Result := RESTServerDefault.Container
     .FindServerByName('ServerBasicDemo').Info.Security;
end;

Now create your server using the container:

pascal
uses
  REST4D, REST4D.Server,
  BasicDemo.AppWebModule;
pascal
procedure CreateServer;
var
  vServerInfo: IRESTServerInfo;
  vOnAuthentication: TRESTAuthenticationDelegate;
begin
  vServerInfo := TRESTServerInfoFactory.Build;
  vServerInfo.ServerName := 'ServerBasicDemo';
  vServerInfo.Port := 3000;
  vServerInfo.MaxConnections := 1024;
  vServerInfo.WebModuleClass := BasicDemoWebModuleClass;
pascal
vOnAuthentication := procedure(const pUserName, pPassword: string;
    pUserRoles: TList<string>; var pIsValid: Boolean)
  begin
     pIsValid := pUserName.Equals('ezequiel') and pPassword.Equals('123');
  end;
pascal
vServerInfo.Security := TRESTDefaultSecurity.Create(vOnAuthentication, nil);
pascal
RESTServer.Container.CreateServer(vServerInfo);
RESTServer.Container.StartServers;
pascal
end;

Using the TRESTfulClient

It's very simple:

pascal
uses
   REST4D.Client, REST4D;
pascal
var
   vRESTfulClient: TRESTfulClient;
   vResp: TRESTfulResponse;
begin
   vRESTfulClient := TRESTfulClient.Create('localhost', 3000);
   try
     vRESTfulClient.Resource('/user').Params([]);
     vRESTfulClient.Authorization('ezequiel', '123');    
     vResp := vRESTfulClient.GET;   
     ShowMessage(vResp.AsString);
   finally
     FreeAndNil(vRESTfulClient);
   end;
end;

Using the TRESTAdapter

The Adapter is a way to create a mapped interface with your REST resources that exist on the server. Thus, the Client you direct calls the methods of the interface.

First, create and map the resource to be consumed:

pascal
uses
  User,
  System.Generics.Collections,
  REST4D.Mapping,      
  REST4D,
  REST4D.Adapter;
pascal
type
pascal
IUserResource = interface(IInvokable)
  ['{BF864FFF-7EB6-4A07-812E-EAC80F7AE0C9}']
  [RESTResource(THTTPMethod.httpGET, '/hello')]
  function HelloWorld(): string;
pascal
[RESTResource(THTTPMethod.httpGET, '/user')]
function GetUser(): TUser;
pascal
[RESTResource(THTTPMethod.httpPOST, '/user/save')]
procedure PostUser([Body] pBody: TUser);
pascal
[RESTResource(THTTPMethod.httpGET, '/users')]
[MapperListOf(TUser)]
function GetUsers(): TObjectList<TUser>;
pascal
[RESTResource(THTTPMethod.httpPOST, '/users/save')]
procedure PostUsers([Body] pBody: TObjectList<TUser>);
pascal
end;

Now, initialize the resource and use it wherever you need:

pascal
uses
   User.Resource;
pascal
//In class or Form ...
private
   FUserResource: IUserResource;
pascal
procedure Initialize;
var
   vRESTfulClient: TRESTfulClient;
   vRESTAdapter: TRESTAdapter<IUserResource>;
begin
   vRESTfulClient := TRESTfulClient.Create('localhost', 3000);
   vRESTfulClient.Authorization('ezequiel', '123');
   vRESTAdapter := TRESTAdapter<IUserResource>.Create;
   vRESTAdapter.Build(vRESTfulClient, True);
   FUserResource := vRESTAdapter.ResourcesService;
end;
pascal
procedure ShowUsers;
var
   vUsers: TObjectList<TUser>;
   I: Integer;
begin
   vUsers := FUserResource.GetUsers;
   try
     vUsers.OwnsObjects := True;
     for I := 0 to vUsers.Count - 1 do
       ShowMessage(vUsers.Items[I].Name);
   finally
     FreeAndNil(vUsers);
   end;
end;