mirror of
https://github.com/danieleteti/delphimvcframework.git
synced 2024-11-15 15:55:54 +01:00
00d5a9699a
- Now the JSON-RPC executor provides methods to handle HTTP headers for JSON-RPC requests and notifications. - FIX for [issue #141](https://github.com/danieleteti/delphimvcframework/issues/141) - `TDataSetHolder` is a new render that is able to render a dataset with a set of custom metadata (eg `count`,`page` etc). Check [issue #137](https://github.com/danieleteti/delphimvcframework/issues/137)
182 lines
5.2 KiB
ObjectPascal
182 lines
5.2 KiB
ObjectPascal
unit MVCFramework.JSONRPC.Client;
|
|
|
|
interface
|
|
|
|
uses
|
|
MVCFramework.JSONRPC,
|
|
System.Net.HttpClient,
|
|
System.Net.URLClient,
|
|
System.Generics.Collections;
|
|
|
|
type
|
|
IMVCJSONRPCExecutor = interface
|
|
['{55415094-9D28-4707-AEC5-5FCF925E82BC}']
|
|
function ExecuteRequest(const aJSONRPCRequest: TJSONRPCRequest): TJSONRPCResponse;
|
|
procedure ExecuteNotification(const aJSONRPCNotification: TJSONRPCNotification);
|
|
// Http headers handling
|
|
procedure AddHTTPHeader(const aNetHeader: TNetHeader);
|
|
procedure ClearHTTPHeaders;
|
|
function HTTPHeadersCount: Integer;
|
|
end;
|
|
|
|
TMVCJSONRPCExecutor = class(TInterfacedObject, IMVCJSONRPCExecutor)
|
|
private
|
|
FURL: string;
|
|
FHTTP: THTTPClient;
|
|
FRaiseExceptionOnError: Boolean;
|
|
FHTTPRequestHeaders: TList<TNetHeader>;
|
|
function GetHTTPRequestHeaders: TList<TNetHeader>;
|
|
protected
|
|
function InternalExecute(const aJSONRPCObject: TJSONRPCObject): TJSONRPCResponse;
|
|
public
|
|
constructor Create(const aURL: string; const aRaiseExceptionOnError: Boolean = True); virtual;
|
|
destructor Destroy; override;
|
|
function ExecuteRequest(const aJSONRPCRequest: TJSONRPCRequest): TJSONRPCResponse;
|
|
procedure ExecuteNotification(const aJSONRPCNotification: TJSONRPCNotification);
|
|
// Http headers handling
|
|
procedure AddHTTPHeader(const aNetHeader: TNetHeader);
|
|
procedure ClearHTTPHeaders;
|
|
function HTTPHeadersCount: Integer;
|
|
end;
|
|
|
|
implementation
|
|
|
|
uses
|
|
System.Classes,
|
|
System.SysUtils;
|
|
|
|
procedure JSONRPCExec(const aJSONRPCURL: string; const aJSONRPCRequest: TJSONRPCRequest; out aJSONRPCResponse: TJSONRPCResponse);
|
|
var
|
|
lSS: TStringStream;
|
|
lHttpResp: IHTTPResponse;
|
|
lHTTP: THTTPClient;
|
|
begin
|
|
lSS := TStringStream.Create(aJSONRPCRequest.AsJSONString);
|
|
try
|
|
lSS.Position := 0;
|
|
lHTTP := THTTPClient.Create;
|
|
try
|
|
lHttpResp := lHTTP.Post('http://localhost:8080/jsonrpc', lSS, nil, [TNetHeader.Create('content-type', 'application/json'),
|
|
TNetHeader.Create('accept', 'application/json')]);
|
|
if (lHttpResp.StatusCode <> 204) then
|
|
begin
|
|
aJSONRPCResponse := TJSONRPCResponse.Create;
|
|
try
|
|
aJSONRPCResponse.AsJSONString := lHttpResp.ContentAsString;
|
|
if Assigned(aJSONRPCResponse.Error) then
|
|
raise Exception.CreateFmt('Error [%d]: %s', [aJSONRPCResponse.Error.Code, aJSONRPCResponse.Error.ErrMessage]);
|
|
except
|
|
aJSONRPCResponse.Free;
|
|
raise;
|
|
end;
|
|
end;
|
|
finally
|
|
lHTTP.Free;
|
|
end;
|
|
finally
|
|
lSS.Free;
|
|
end;
|
|
end;
|
|
|
|
{ TMVCJSONRPCExecutor }
|
|
|
|
procedure TMVCJSONRPCExecutor.AddHTTPHeader(const aNetHeader: TNetHeader);
|
|
begin
|
|
GetHTTPRequestHeaders.Add(aNetHeader);
|
|
end;
|
|
|
|
procedure TMVCJSONRPCExecutor.ClearHTTPHeaders;
|
|
begin
|
|
if Assigned(FHTTPRequestHeaders) then
|
|
begin
|
|
FHTTPRequestHeaders.Clear;
|
|
end;
|
|
end;
|
|
|
|
constructor TMVCJSONRPCExecutor.Create(const aURL: string; const aRaiseExceptionOnError: Boolean = True);
|
|
begin
|
|
inherited Create;
|
|
FRaiseExceptionOnError := aRaiseExceptionOnError;
|
|
FURL := aURL;
|
|
FHTTP := THTTPClient.Create;
|
|
FHTTPRequestHeaders := nil;
|
|
end;
|
|
|
|
destructor TMVCJSONRPCExecutor.Destroy;
|
|
begin
|
|
FHTTP.Free;
|
|
FHTTPRequestHeaders.Free;
|
|
inherited;
|
|
end;
|
|
|
|
procedure TMVCJSONRPCExecutor.ExecuteNotification(const aJSONRPCNotification: TJSONRPCNotification);
|
|
begin
|
|
if InternalExecute(aJSONRPCNotification as TJSONRPCObject) <> nil then
|
|
raise EMVCJSONRPCException.Create('A "notification" cannot returns a response. Use ExecuteRequest instead.');
|
|
end;
|
|
|
|
function TMVCJSONRPCExecutor.ExecuteRequest(const aJSONRPCRequest: TJSONRPCRequest): TJSONRPCResponse;
|
|
begin
|
|
Result := InternalExecute(aJSONRPCRequest);
|
|
end;
|
|
|
|
function TMVCJSONRPCExecutor.GetHTTPRequestHeaders: TList<TNetHeader>;
|
|
begin
|
|
if not Assigned(FHTTPRequestHeaders) then
|
|
begin
|
|
FHTTPRequestHeaders := TList<TNetHeader>.Create;
|
|
end;
|
|
Result := FHTTPRequestHeaders;
|
|
end;
|
|
|
|
function TMVCJSONRPCExecutor.HTTPHeadersCount: Integer;
|
|
begin
|
|
if Assigned(FHTTPRequestHeaders) then
|
|
begin
|
|
Result := FHTTPRequestHeaders.Count;
|
|
end
|
|
else
|
|
begin
|
|
Result := 0;
|
|
end;
|
|
end;
|
|
|
|
function TMVCJSONRPCExecutor.InternalExecute(const aJSONRPCObject: TJSONRPCObject): TJSONRPCResponse;
|
|
var
|
|
lSS: TStringStream;
|
|
lHttpResp: IHTTPResponse;
|
|
lJSONRPCResponse: TJSONRPCResponse;
|
|
lCustomHeaders: TNetHeaders;
|
|
begin
|
|
lCustomHeaders := [];
|
|
if Assigned(FHTTPRequestHeaders) then
|
|
begin
|
|
lCustomHeaders := FHTTPRequestHeaders.ToArray;
|
|
end;
|
|
|
|
Result := nil;
|
|
lSS := TStringStream.Create(aJSONRPCObject.AsJSONString);
|
|
try
|
|
lSS.Position := 0;
|
|
lHttpResp := FHTTP.Post(FURL, lSS, nil, [TNetHeader.Create('content-type', 'application/json'),
|
|
TNetHeader.Create('accept', 'application/json')] + lCustomHeaders);
|
|
if (lHttpResp.StatusCode <> 204) then
|
|
begin
|
|
lJSONRPCResponse := TJSONRPCResponse.Create;
|
|
try
|
|
lJSONRPCResponse.AsJSONString := lHttpResp.ContentAsString;
|
|
if Assigned(lJSONRPCResponse.Error) and FRaiseExceptionOnError then
|
|
raise Exception.CreateFmt('Error [%d]: %s', [lJSONRPCResponse.Error.Code, lJSONRPCResponse.Error.ErrMessage]);
|
|
Result := lJSONRPCResponse;
|
|
except
|
|
lJSONRPCResponse.Free;
|
|
raise;
|
|
end;
|
|
end;
|
|
finally
|
|
lSS.Free;
|
|
end;
|
|
end;
|
|
|
|
end.
|