Using RAD Server Components

From RAD Studio
Jump to: navigation, search

Go Up to RAD Server (EMS)


Ability to Delegate Processing of a Request to a Custom Class or Component

Add ability to RAD Server custom resources API to delegate request processing to resource module Delphi fields, which are custom endpoint publisher classes / components. For example:

type
 TEmpsProvider = class(TComponent, IEMSEndpointPublisher)
 private
   class var
     FArray: TJSONArray;
 private
   function GetItemByID(ARequest: TEndpointRequest): Integer;
   class constructor Create;
   class destructor Destroy;
 public
   procedure Get(const AContext: TEndpointContext; 
     const ARequest: TEndpointRequest; 
     const AResponse: TEndpointResponse); overload;
   [ResourceSuffix('./{id}')]
   procedure GetItem(const AContext: TEndpointContext; 
     const ARequest: TEndpointRequest;
     const AResponse: TEndpointResponse); overload;
   [ResourceSuffix('./{id}')]
   procedure Put(const AContext: TEndpointContext; 
     const ARequest: TEndpointRequest;
     const AResponse: TEndpointResponse);
   procedure Post(const AContext: TEndpointContext; 
     const ARequest: TEndpointRequest;
     const AResponse: TEndpointResponse);
   [ResourceSuffix('./{id}')]
   procedure Delete(const AContext: TEndpointContext; 
     const ARequest: TEndpointRequest;
     const AResponse: TEndpointResponse);
 end;
 [ResourceName('Test')]
 TTestResource = class(TDataModule)
 public
   Emps: TEmpsProvider;
   constructor Create(AOwner: TComponent); override;
 end;

The following requests will be processed by TEmpsProvider methods of Emps object:

  • GET http://localhost/test/emps - TEmpsProvider.Get method.
  • GET http://localhost/test/emps/1 - TEmpsProvider.GetItem method.

Additionally:

  • Request processing may be delegated to a resource module field of object type. The object type may be any class (including TComponent descendant), implementing IEMSEndpointPublisher interface. A class is called endpoint publisher class.
  • By default, all class endpoints will get resource suffix equal to resource module field name. This may be overridden by specifying attribute for resource module field:
    • ResourceName - to override field name.
    • ResourceSuffix - to override resource suffix.
  • A class endpoint may be marked with ResourceSuffix attribute. When suffix starts with "./", then specified suffix will be appended to default resource suffix of resource module field.
  • Any attribute specified for a resource module endpoint can be specified for a class endpoint. An attribute can be specified at one of the places in order of priority (less priority at beginning of the list, higher priority overrides lower priority attribute):
    • class endpoint
    • resource module field. An attribute first argument may be a class endpoint name.
    • TEMSResourceAttributes specified at resource module registration. A name used for TEMSResourceAttributes calls is a <resource module field name>.<class endpoint name>

Helper Components for JSON Processing

Taking advantage of the new ability to delegate processing to a component, RAD Studio 10.3 introduces new components for simplifying the JSON processing work, particularly when database queries are involved. The components can be added to a class mapped to a RAD Server resource (or a data module), and the HTTP methods can be mapped to them without writing any code.

  • The TEMSFileResource component has properties indicating the path and the default file name.
  • The TEMSDataSetResource has a reference to a DataSet and properties for paging size, and the sorting and paging query params names.

Both components can be configured to accept specific HTTP verbs and connected with the hosting resource, as in this sample code:

  [ResourceName('Notes')]
 TNotesResource1 = class(TDataModule)
   FDConnection1: TFDConnection; // connects to SQLite DB with "notes" table
   FDQuery1: TFDQuery; // SELECT * FROM Notes
   [ResourceSuffix('list', '/')]
   [ResourceSuffix('get', '/{id}')]
   [ResourceSuffix('put', '/{id}')]
   [ResourceSuffix('post', '/')]
   [ResourceSuffix('delete', '/{id}')]
   EMSDBResource1: TEMSDataSetResource;
 end;
      // database resource
   [ResourceSuffix('/')]
   EMSDBResource1: TEMSDataSetResource;
   [ResourceSuffix('/')]
   [EndpointProduce('get', 'image/jpeg, image/png')]
   EMSDBResource1: TEMSDataSetResource;
      // file resource
   [ResourceSuffix('/')]
   [EndpointProduce('get', 'application/pdf')]
   EMSFileResource1: TEMSFileResource;

See Also