2016-06-22 17:49:16 +02:00
// ***************************************************************************
//
// Delphi MVC Framework
//
2020-01-06 16:49:18 +01:00
// Copyright (c) 2010-2020 Daniele Teti and the DMVCFramework Team
2016-06-22 17:49:16 +02:00
//
// https://github.com/danieleteti/delphimvcframework
//
// ***************************************************************************
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// *************************************************************************** }
2015-01-14 11:39:44 +01:00
unit MVCFramework. RESTAdapter;
2017-01-18 21:53:53 +01:00
{$I dmvcframework.inc}
2015-01-14 11:39:44 +01:00
interface
uses
2020-09-22 00:31:26 +02:00
System. Rtti,
System. TypInfo,
MVCFramework. RESTClient. Intf,
MVCFramework. Commons,
IdIOHandler,
System. Classes,
System. SysUtils;
2015-01-14 11:39:44 +01:00
2015-01-17 17:19:09 +01:00
const
URL_SEPARATOR = '/' ;
2015-01-14 11:39:44 +01:00
type
2017-01-18 21:53:53 +01:00
RESTResourceAttribute = class( TCustomAttribute)
2015-01-14 11:39:44 +01:00
private
2020-09-22 00:31:26 +02:00
fURL: string ;
fHTTPMethodType: TMVCHTTPMethodType;
2015-01-14 11:39:44 +01:00
public
2020-09-22 00:31:26 +02:00
constructor Create( aMVCHTTPMethod: TMVCHTTPMethodType; aURL: string ) ;
property URL: string read fURL;
property HTTPMethodType: TMVCHTTPMethodType read fHTTPMethodType;
2015-01-14 11:39:44 +01:00
end ;
BodyAttribute = class( TCustomAttribute)
private
2020-09-22 00:31:26 +02:00
fOwnsObject: Boolean ;
2015-01-14 11:39:44 +01:00
public
2020-09-22 00:31:26 +02:00
constructor Create( aOwnsObject: Boolean = True ) ;
property OwnsObject: Boolean read fOwnsObject;
2015-01-14 11:39:44 +01:00
end ;
ParamAttribute = class( TCustomAttribute)
private
2020-09-22 00:31:26 +02:00
fParamType: string ;
fCustomFormat: string ;
fParamMatch: string ;
2015-01-14 11:39:44 +01:00
public
2020-09-22 00:31:26 +02:00
constructor Create( aParamMatch: string ; aParamType: string = '' ; aCustomFormat: string = '' ) ;
property ParamMatch: string read fParamMatch;
property ParamType: string read fParamType;
property CustomFormat: string read fCustomFormat;
2015-01-17 17:19:09 +01:00
function FmtParamMatch: string ;
2015-01-14 11:39:44 +01:00
end ;
HeadersAttribute = class( TCustomAttribute)
private
2020-09-22 00:31:26 +02:00
fKey: string ;
fValue: string ;
2015-01-14 11:39:44 +01:00
public
2020-09-22 00:31:26 +02:00
constructor Create( aKey: string ; aValue: string ) ;
property Key: string read fKey;
property Value: string read fValue;
2015-01-14 11:39:44 +01:00
end ;
2015-02-17 09:40:55 +01:00
MappingAttribute = class( TCustomAttribute)
private
2020-09-22 00:31:26 +02:00
fClass: TClass;
2015-02-17 09:40:55 +01:00
public
2020-09-22 00:31:26 +02:00
constructor Create( aClass: TClass) ;
2015-02-17 09:40:55 +01:00
function GetType: TRttiType;
end ;
2015-01-14 11:39:44 +01:00
IRESTAdapter< T> = interface
[ '{AAA41F40-69DB-419B-9922-F59F990CBDB5}' ]
function ResourcesService: T;
2020-09-22 00:31:26 +02:00
procedure AddRequestHeaders( aObj: TRttiObject) ;
procedure AddRequestHeader( aKey: string ; aValue: string ) ;
procedure MapResult( aResp: IMVCRESTResponse; aMethod: TRttiMethod; aRttiType: TRttiType; out aResult: TValue) ;
2015-01-14 11:39:44 +01:00
end ;
2015-06-16 15:13:59 +02:00
TRESTAdapter< T: IInvokable> = class( TVirtualInterface, IRESTAdapter< T> )
2015-01-14 11:39:44 +01:00
private
2020-09-22 00:31:26 +02:00
fRESTClient: IMVCRESTClient;
2015-01-14 11:39:44 +01:00
protected
2020-09-22 00:31:26 +02:00
procedure DoInvoke( aMethod: TRttiMethod; const aArgs: TArray< TValue> ; out aResult: TValue) ;
procedure AddRequestHeaders( aObj: TRttiObject) ;
procedure AddRequestHeader( aKey: string ; aValue: string ) ;
procedure MapResult( aResp: IMVCRESTResponse; aMethod: TRttiMethod; aRttiType: TRttiType; out aResult: TValue) ;
function GetURL( aMethod: TRttiMethod; const aArgs: TArray< TValue> ) : string ;
function GetBodyAsString( aMethod: TRttiMethod; const aArgs: TArray< TValue> ) : string ;
2015-01-14 11:39:44 +01:00
public
constructor Create;
destructor Destroy; override ;
2020-09-22 00:31:26 +02:00
function Build( aRESTClient: IMVCRESTClient) : T; overload ;
function Build( const aServerName: string ; const aServerPort: Word = 8 0 ) : T; overload ;
2015-01-14 11:39:44 +01:00
function ResourcesService: T;
2020-09-22 00:31:26 +02:00
property RESTClient: IMVCRESTClient read fRESTClient write fRESTClient;
2015-01-14 11:39:44 +01:00
end ;
2015-02-17 09:40:55 +01:00
IAsynchRequest = interface
[ '{3E720356-F2B7-4C32-8051-B7723263740F}' ]
2020-09-22 00:31:26 +02:00
procedure SetErrorProc( const aValue: TProc< Exception> ) ;
procedure SetSuccessProc( const aValue: TProc< TValue> ) ;
procedure SetSynchronized( const aValue: Boolean ) ;
2015-02-17 09:40:55 +01:00
function GetErrorProc: TProc< Exception> ;
function GetSuccessProc: TProc< TValue> ;
2020-09-22 00:31:26 +02:00
function GetSynchronized: Boolean ;
2015-02-17 09:40:55 +01:00
2020-09-22 00:31:26 +02:00
property SuccessProc: TProc< TValue> read GetSuccessProc write SetSuccessProc;
2015-02-17 09:40:55 +01:00
property ErrorProc: TProc< Exception> read GetErrorProc write SetErrorProc;
2020-09-22 00:31:26 +02:00
property Synchronized: Boolean read GetSynchronized write SetSynchronized;
2015-02-17 09:40:55 +01:00
end ;
TAsynchRequest = class( TInterfacedObject, IAsynchRequest)
2015-01-19 15:28:04 +01:00
private
2020-09-22 00:31:26 +02:00
fSynchronized: Boolean ;
fSuccessProc: TProc< TValue> ;
fErrorProc: TProc< Exception> ;
procedure SetErrorProc( const aValue: TProc< Exception> ) ;
procedure SetSuccessProc( const aValue: TProc< TValue> ) ;
procedure SetSynchronized( const aValue: Boolean ) ;
2015-02-17 09:40:55 +01:00
function GetErrorProc: TProc< Exception> ;
function GetSuccessProc: TProc< TValue> ;
2020-09-22 00:31:26 +02:00
function GetSynchronized: Boolean ;
2015-01-19 15:28:04 +01:00
public
2020-09-22 00:31:26 +02:00
constructor Create( aSuccProc: TProc< TValue> = nil ; aProcErr: TProc< Exception> = nil ;
aSynchronized: Boolean = False ) ;
property SuccessProc: TProc< TValue> read GetSuccessProc write SetSuccessProc;
2015-02-17 09:40:55 +01:00
property ErrorProc: TProc< Exception> read GetErrorProc write SetErrorProc;
2020-09-22 00:31:26 +02:00
property Synchronized: Boolean read GetSynchronized write SetSynchronized;
2015-01-19 15:28:04 +01:00
end ;
2015-01-14 11:39:44 +01:00
implementation
uses
2017-04-26 14:39:18 +02:00
// ObjectsMappers,
MVCFramework. Serializer. Commons,
MVCFramework. Serializer. Defaults,
2020-09-02 16:13:29 +02:00
{$IFDEF SYSTEMJSON}
2017-01-18 21:53:53 +01:00
System. JSON,
2017-04-26 14:39:18 +02:00
2020-09-02 16:13:29 +02:00
{$ELSE}
2015-01-14 11:39:44 +01:00
Data. DBXJSON,
Data. SqlExpr,
DBXCommon,
2017-04-26 14:39:18 +02:00
2020-09-02 16:13:29 +02:00
{$ENDIF}
2017-03-20 19:08:01 +01:00
MVCFramework. Rtti. Utils,
2017-01-18 21:53:53 +01:00
MVCFramework. DuckTyping,
2020-09-22 00:31:26 +02:00
Generics. Collections,
MVCFramework. RESTClient,
System. NetConsts;
2015-01-14 11:39:44 +01:00
{ TRESTAdapter }
2020-09-22 00:31:26 +02:00
function TRESTAdapter< T> . Build( aRESTClient: IMVCRESTClient) : T;
2015-01-14 11:39:44 +01:00
begin
2020-09-22 00:31:26 +02:00
fRESTClient : = aRESTClient;
2015-01-14 11:39:44 +01:00
Result : = ResourcesService;
end ;
2020-09-22 00:31:26 +02:00
procedure TRESTAdapter< T> . AddRequestHeader( aKey, aValue: string ) ;
2015-01-14 11:39:44 +01:00
begin
2020-09-22 00:31:26 +02:00
if CompareText( aKey, sAccept) = 0 then
fRESTClient. Accept( aValue)
else if CompareText( aKey, sAcceptCharset) = 0 then
fRESTClient. AcceptCharset( aValue)
else if CompareText( aKey, sAcceptEncoding) = 0 then
fRESTClient. AcceptEncoding( aValue)
2015-01-14 11:39:44 +01:00
else
2020-09-22 00:31:26 +02:00
fRESTClient. AddHeader( aKey, aValue) ;
2015-01-14 11:39:44 +01:00
end ;
2020-09-22 00:31:26 +02:00
procedure TRESTAdapter< T> . AddRequestHeaders( aObj: TRttiObject) ;
2015-01-14 11:39:44 +01:00
var
2020-09-22 00:31:26 +02:00
lAttr: TCustomAttribute;
2015-01-14 11:39:44 +01:00
begin
2020-09-22 00:31:26 +02:00
for lAttr in aObj. GetAttributes do
begin
if lAttr is HeadersAttribute then
AddRequestHeader( HeadersAttribute( lAttr) . Key, HeadersAttribute( lAttr) . Value) ;
end ;
2015-01-14 11:39:44 +01:00
end ;
2020-09-22 00:31:26 +02:00
function TRESTAdapter< T> . Build( const aServerName: string ; const aServerPort: Word ) : T;
2015-01-14 11:39:44 +01:00
begin
2020-09-22 00:31:26 +02:00
Result : = Build( TMVCRESTClient. New. BaseURL( aServerName, aServerPort) ) ;
2015-01-14 11:39:44 +01:00
end ;
constructor TRESTAdapter< T> . Create;
begin
2015-06-16 15:13:59 +02:00
inherited Create( TypeInfo( T) , DoInvoke) ;
2015-01-14 11:39:44 +01:00
end ;
destructor TRESTAdapter< T> . Destroy;
begin
2016-06-22 17:49:16 +02:00
// Ezequiel J. M<> ller (If it is created outside, it must be destroyed out)
// d.spinetti added RESTClientOwner to manage desctruction of RESTClient and free its associated memory
2020-09-22 00:31:26 +02:00
// if RESTClientOwner and Assigned(fRESTClient) then
// fRESTClient.Free;
2015-01-14 11:39:44 +01:00
inherited ;
end ;
2020-09-22 00:31:26 +02:00
procedure TRESTAdapter< T> . DoInvoke( aMethod: TRttiMethod; const aArgs: TArray< TValue> ; out aResult: TValue) ;
2015-01-14 11:39:44 +01:00
var
2020-09-22 00:31:26 +02:00
lResp: IMVCRESTResponse;
lRestResourceAttr: RESTResourceAttribute;
lURL: string ;
lBody: string ;
lAsyncClass: IAsynchRequest;
lMappingAttr: MappingAttribute;
2015-01-14 11:39:44 +01:00
begin
// Implementation of RESTClient DoGet DoPut ecc...
2020-09-22 00:31:26 +02:00
if not TRttiUtils. HasAttribute< RESTResourceAttribute> ( aMethod, lRestResourceAttr) then
raise Exception. CreateFmt( 'No REST Resource specified in method %s' , [ aMethod. Name ] ) ;
2015-01-14 11:39:44 +01:00
// headers can be more than one
2017-03-01 21:40:57 +01:00
AddRequestHeaders( TRttiUtils. GlContext. GetType( TypeInfo( T) ) ) ;
2020-09-22 00:31:26 +02:00
// aMethod
AddRequestHeaders( aMethod) ;
2015-01-14 11:39:44 +01:00
2020-09-22 00:31:26 +02:00
// lURL and lBody
lURL : = GetURL( aMethod, aArgs) ;
lBody : = GetBodyAsString( aMethod, aArgs) ;
2015-01-17 17:19:09 +01:00
2015-01-19 15:28:04 +01:00
// Asynch way to do
2020-09-22 00:31:26 +02:00
if aArgs[ Length( aArgs) - 1 ] . TryAsType< IAsynchRequest> ( lAsyncClass) then
2015-02-17 09:40:55 +01:00
begin
2020-09-22 00:31:26 +02:00
fRESTClient. Async(
procedure( ARESTResponse: IMVCRESTResponse)
2015-01-19 15:28:04 +01:00
var
2020-09-22 00:31:26 +02:00
lResValue: TValue;
2015-01-19 15:28:04 +01:00
begin
2020-09-22 00:31:26 +02:00
if TRttiUtils. HasAttribute< MappingAttribute> ( aMethod, lMappingAttr) then
MapResult( ARESTResponse, aMethod, lMappingAttr. GetType, lResValue)
2015-02-17 09:40:55 +01:00
else
2020-09-22 00:31:26 +02:00
lResValue : = TValue. From( ARESTResponse) ;
if Assigned( lAsyncClass. SuccessProc) then
lAsyncClass. SuccessProc( lResValue) ;
end ,
lAsyncClass. ErrorProc,
lAsyncClass. Synchronized) ;
2015-02-17 09:40:55 +01:00
end ;
2015-01-19 15:28:04 +01:00
2020-09-22 00:31:26 +02:00
case lRestResourceAttr. HTTPMethodType of
2015-01-14 11:39:44 +01:00
httpGET:
2020-09-22 00:31:26 +02:00
lResp : = fRESTClient. Get( lURL) ;
2015-01-14 11:39:44 +01:00
httpPUT:
2020-09-22 00:31:26 +02:00
lResp : = fRESTClient. Put( lURL, lBody) ;
2015-01-14 11:39:44 +01:00
httpPOST:
2020-09-22 00:31:26 +02:00
lResp : = fRESTClient. Post( lURL, lBody) ;
2017-11-16 22:49:38 +01:00
httpDELETE:
2020-09-22 00:31:26 +02:00
lResp : = fRESTClient. Delete( lURL) ;
2015-01-14 11:39:44 +01:00
end ;
// if is a procedure no need a return type
2020-09-22 00:31:26 +02:00
if Assigned( aMethod. ReturnType) then
MapResult( lResp, aMethod, aMethod. ReturnType, aResult) ;
2015-01-14 11:39:44 +01:00
end ;
2020-09-22 00:31:26 +02:00
function TRESTAdapter< T> . GetBodyAsString( aMethod: TRttiMethod; const aArgs: TArray< TValue> ) : string ;
2015-01-17 17:19:09 +01:00
var
2020-09-22 00:31:26 +02:00
lParameters: TArray< TRttiParameter> ;
2015-01-17 17:19:09 +01:00
I: Integer ;
2020-09-22 00:31:26 +02:00
lParameter: TRttiParameter;
lParam: BodyAttribute;
lAttrListOf: MVCListOfAttribute;
lArg: TValue;
2015-01-14 11:39:44 +01:00
begin
2020-09-22 00:31:26 +02:00
lParameters : = aMethod. GetParameters;
for I : = 0 to Length( lParameters) - 1 do
2015-01-17 17:19:09 +01:00
begin
2020-09-22 00:31:26 +02:00
lParameter : = lParameters[ I] ;
// lArg := aArgs[I+1] because
// aArgs RTTI for the arguments of the interface method that has been called. The first argument (located at index 0) represents the interface instance itself.
lArg : = aArgs[ I + 1 ] ;
if TRttiUtils. HasAttribute< BodyAttribute> ( lParameter, lParam) then
2015-01-19 15:28:04 +01:00
try
2020-09-22 00:31:26 +02:00
if lArg. IsObject then
2015-12-22 12:15:43 +01:00
begin
2017-04-26 14:39:18 +02:00
2020-09-22 00:31:26 +02:00
if TRttiUtils. HasAttribute< MVCListOfAttribute> ( aMethod, lAttrListOf) then
2017-04-26 14:39:18 +02:00
Exit(
2020-09-22 00:31:26 +02:00
GetDefaultSerializer. SerializeCollection( lArg. AsObject)
{ Mapper.ObjectListToJSONArrayString(WrapAsList(lArg.AsObject), True) }
2017-04-26 14:39:18 +02:00
)
2015-12-22 12:15:43 +01:00
else
2017-04-26 14:39:18 +02:00
Exit(
2020-09-22 00:31:26 +02:00
GetDefaultSerializer. SerializeObject( lArg. AsObject)
{ Mapper.ObjectToJSONObjectString(lArg.AsObject) }
2017-04-26 14:39:18 +02:00
) ;
2015-12-22 12:15:43 +01:00
end
2015-01-19 15:28:04 +01:00
else
2020-09-22 00:31:26 +02:00
Exit( TRttiUtils. TValueAsString( lArg, '' , '' ) ) ;
2015-01-19 15:28:04 +01:00
finally
2020-09-22 00:31:26 +02:00
if lParam. OwnsObject and lArg. IsObject then
2017-01-18 21:53:53 +01:00
begin
2017-04-26 14:39:18 +02:00
2020-09-02 16:13:29 +02:00
{$HINTS OFF}
2020-09-22 00:31:26 +02:00
lArg. AsObject. Free;
2017-04-26 14:39:18 +02:00
2020-09-02 16:13:29 +02:00
{$HINTS ON}
2017-01-18 21:53:53 +01:00
end ;
2015-01-19 15:28:04 +01:00
end ;
2015-01-17 17:19:09 +01:00
end ;
2015-01-14 11:39:44 +01:00
end ;
2020-09-22 00:31:26 +02:00
function TRESTAdapter< T> . GetURL( aMethod: TRttiMethod; const aArgs: TArray< TValue> ) : string ;
2015-01-14 11:39:44 +01:00
var
2020-09-22 00:31:26 +02:00
lRestResourceAttr: RESTResourceAttribute;
lURL: string ;
lSplitUrl: TArray< string > ;
lURLDict: TDictionary< string , string > ;
lSplit: string ;
lParameters: TArray< TRttiParameter> ;
2015-01-14 11:39:44 +01:00
I: Integer ;
2020-09-22 00:31:26 +02:00
lParameter: TRttiParameter;
lParam: ParamAttribute;
lArg: TValue;
begin
lRestResourceAttr : = TRttiUtils. GetAttribute< RESTResourceAttribute> ( aMethod) ;
lURL : = lRestResourceAttr. URL;
lSplitUrl : = lURL. Split( [ URL_SEPARATOR] ) ;
lURLDict : = TDictionary< string , string > . Create;
2015-01-17 17:19:09 +01:00
try
2020-09-22 00:31:26 +02:00
for lSplit in lSplitUrl do
if not lSplit. IsEmpty then
lURLDict. Add( lSplit, lSplit) ;
lParameters : = aMethod. GetParameters;
// lArg := aArgs[I+1] because
// aArgs RTTI for the arguments of the interface method that has been called. The first argument (located at index 0) represents the interface instance itself.
for I : = 0 to Length( lParameters) - 1 do
2015-01-17 17:19:09 +01:00
begin
2020-09-22 00:31:26 +02:00
lParameter : = lParameters[ I] ;
lArg : = aArgs[ I + 1 ] ;
if TRttiUtils. HasAttribute< ParamAttribute> ( lParameter, lParam) then
lURLDict[ lParam. FmtParamMatch] : = TRttiUtils. TValueAsString( lArg,
lParam. ParamType, lParam. CustomFormat) ;
2015-01-17 17:19:09 +01:00
end ;
2020-09-22 00:31:26 +02:00
for lSplit in lSplitUrl do
if not lSplit. IsEmpty then
Result : = Result + URL_SEPARATOR + lURLDict[ lSplit] ;
2015-01-17 17:19:09 +01:00
2020-09-22 00:31:26 +02:00
if lURL. EndsWith( URL_SEPARATOR) and not ( Result . EndsWith( URL_SEPARATOR) ) then
2015-01-17 17:19:09 +01:00
Result : = Result + URL_SEPARATOR;
finally
2020-09-22 00:31:26 +02:00
lURLDict. Free;
2015-01-14 11:39:44 +01:00
end ;
end ;
2020-09-22 00:31:26 +02:00
procedure TRESTAdapter< T> . MapResult( aResp: IMVCRESTResponse; aMethod: TRttiMethod; aRttiType: TRttiType;
out aResult: TValue) ;
2015-01-14 11:39:44 +01:00
var
2020-09-22 00:31:26 +02:00
lAttrListOf: MVCListOfAttribute;
2015-01-14 11:39:44 +01:00
begin
2020-09-22 00:31:26 +02:00
if aRttiType. TypeKind = tkClass then
2015-01-14 11:39:44 +01:00
begin
2015-01-17 17:19:09 +01:00
// ListOf
2020-09-22 00:31:26 +02:00
if TRttiUtils. HasAttribute< MVCListOfAttribute> ( aMethod, lAttrListOf) then
2015-01-17 17:19:09 +01:00
begin
2020-09-22 00:31:26 +02:00
aResult : = TRttiUtils. CreateObject( aRttiType. QualifiedName) ;
GetDefaultSerializer. DeserializeCollection( aResp. Content, aResult. AsObject, lAttrListOf. Value) ;
2015-01-17 17:19:09 +01:00
end
// JSONValue
2020-09-22 00:31:26 +02:00
else if aRttiType. AsInstance. MetaclassType. InheritsFrom( TJSONValue) then
2017-04-26 14:39:18 +02:00
begin
2020-09-22 00:31:26 +02:00
aResult : = TJSONObject. ParseJSONValue( aResp. Content) ;
2015-01-17 17:19:09 +01:00
// Object
2017-04-26 14:39:18 +02:00
end
2015-01-17 17:19:09 +01:00
else
2017-04-26 14:39:18 +02:00
begin
2020-09-22 00:31:26 +02:00
aResult : = TRttiUtils. CreateObject( aRttiType. QualifiedName) ;
GetDefaultSerializer. DeserializeObject( aResp. Content, aResult. AsObject) ;
2017-04-26 14:39:18 +02:00
end ;
2015-01-14 11:39:44 +01:00
end
2020-09-22 00:31:26 +02:00
// IRESTResponse
else if aRttiType. QualifiedName = TRttiUtils. GlContext. GetType( TypeInfo( IMVCRESTResponse) ) . QualifiedName then
begin
aResult : = aResult. From( aResp) ;
end
else // else a simple Content
begin
aResult : = aResp. Content;
end ;
2015-01-14 11:39:44 +01:00
end ;
function TRESTAdapter< T> . ResourcesService: T;
var
2020-09-22 00:31:26 +02:00
lTypeInfo: PTypeInfo;
2015-01-14 11:39:44 +01:00
begin
2020-09-22 00:31:26 +02:00
lTypeInfo : = TypeInfo( T) ;
if QueryInterface( GetTypeData( lTypeInfo) . Guid, Result ) < > 0 then
2015-01-14 11:39:44 +01:00
begin
2017-01-18 21:53:53 +01:00
raise Exception. Create( 'RESTAdapter is unable to cast to its interface' ) ;
2015-01-14 11:39:44 +01:00
end ;
end ;
{ RESTResourceAttribute }
2020-09-22 00:31:26 +02:00
constructor RESTResourceAttribute. Create( aMVCHTTPMethod: TMVCHTTPMethodType; aURL: string ) ;
2015-01-14 11:39:44 +01:00
begin
2020-09-22 00:31:26 +02:00
fURL : = aURL;
fHTTPMethodType : = aMVCHTTPMethod;
2015-01-14 11:39:44 +01:00
end ;
{ BodyAttribute }
2020-09-22 00:31:26 +02:00
constructor BodyAttribute. Create( aOwnsObject: Boolean ) ;
2015-01-14 11:39:44 +01:00
begin
2015-01-17 17:19:09 +01:00
inherited Create;
2020-09-22 00:31:26 +02:00
fOwnsObject : = aOwnsObject;
2015-01-14 11:39:44 +01:00
end ;
{ ParamAttribute }
2020-09-22 00:31:26 +02:00
constructor ParamAttribute. Create( aParamMatch, aParamType, aCustomFormat: string ) ;
2015-01-14 11:39:44 +01:00
begin
2015-01-17 17:19:09 +01:00
inherited Create;
2020-09-22 00:31:26 +02:00
fParamMatch : = aParamMatch;
fParamType : = aParamType;
fCustomFormat : = aCustomFormat;
2015-01-14 11:39:44 +01:00
end ;
2015-01-17 17:19:09 +01:00
function ParamAttribute. FmtParamMatch: string ;
begin
Result : = '{' + ParamMatch + '}' ;
end ;
2015-01-14 11:39:44 +01:00
{ HeadersAttribute }
2020-09-22 00:31:26 +02:00
constructor HeadersAttribute. Create( aKey: string ; aValue: string ) ;
2015-01-14 11:39:44 +01:00
begin
2020-09-22 00:31:26 +02:00
fKey : = aKey;
fValue : = aValue;
2015-01-14 11:39:44 +01:00
end ;
2015-01-19 15:28:04 +01:00
{ TAsynchRequest }
2020-09-22 00:31:26 +02:00
constructor TAsynchRequest. Create( aSuccProc: TProc< TValue> = nil ; aProcErr: TProc< Exception> = nil ;
aSynchronized: Boolean = False ) ;
2015-01-19 15:28:04 +01:00
begin
inherited Create;
2020-09-22 00:31:26 +02:00
fSuccessProc : = aSuccProc;
fErrorProc : = aProcErr;
fSynchronized : = aSynchronized;
2015-02-17 09:40:55 +01:00
end ;
function TAsynchRequest. GetErrorProc: TProc< Exception> ;
begin
2020-09-22 00:31:26 +02:00
Result : = fErrorProc;
2015-02-17 09:40:55 +01:00
end ;
function TAsynchRequest. GetSuccessProc: TProc< TValue> ;
begin
2020-09-22 00:31:26 +02:00
Result : = fSuccessProc;
2015-02-17 09:40:55 +01:00
end ;
2020-09-22 00:31:26 +02:00
function TAsynchRequest. GetSynchronized: Boolean ;
2015-01-19 15:28:04 +01:00
begin
2020-09-22 00:31:26 +02:00
Result : = fSynchronized;
2015-01-19 15:28:04 +01:00
end ;
2020-09-22 00:31:26 +02:00
procedure TAsynchRequest. SetErrorProc( const aValue: TProc< Exception> ) ;
2015-01-19 15:28:04 +01:00
begin
2020-09-22 00:31:26 +02:00
fErrorProc : = aValue;
2015-01-19 15:28:04 +01:00
end ;
2020-09-22 00:31:26 +02:00
procedure TAsynchRequest. SetSuccessProc( const aValue: TProc< TValue> ) ;
2015-01-19 15:28:04 +01:00
begin
2020-09-22 00:31:26 +02:00
fSuccessProc : = aValue;
2015-01-19 15:28:04 +01:00
end ;
2020-09-22 00:31:26 +02:00
procedure TAsynchRequest. SetSynchronized( const aValue: Boolean ) ;
2015-01-19 15:28:04 +01:00
begin
2020-09-22 00:31:26 +02:00
fSynchronized : = aValue;
2015-01-19 15:28:04 +01:00
end ;
2015-02-17 09:40:55 +01:00
{ MappingAttribute }
2020-09-22 00:31:26 +02:00
constructor MappingAttribute. Create( aClass: TClass) ;
2015-02-17 09:40:55 +01:00
begin
2020-09-22 00:31:26 +02:00
fClass : = aClass;
2015-02-17 09:40:55 +01:00
end ;
function MappingAttribute. GetType: TRttiType;
begin
2020-09-22 00:31:26 +02:00
Result : = TRttiUtils. GlContext. GetType( fClass) ;
2015-02-17 09:40:55 +01:00
end ;
2015-01-14 11:39:44 +01:00
end .