delphimvcframework/samples/jsonwebtoken_roleauth/vclclient/MainClientFormU.pas

184 lines
4.0 KiB
ObjectPascal
Raw Normal View History

unit MainClientFormU;
interface
uses
Winapi.Windows,
Winapi.Messages,
System.SysUtils,
System.Variants,
System.Classes,
MVCFramework.Middleware.JWT,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls,
Vcl.ExtCtrls;
type
TForm5 = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
Panel1: TPanel;
btnGet: TButton;
btnLOGIN: TButton;
Splitter1: TSplitter;
Memo3: TMemo;
Splitter2: TSplitter;
btnLoginWithException: TButton;
btnLoginJsonObject: TButton;
procedure btnGetClick(Sender: TObject);
procedure btnLOGINClick(Sender: TObject);
procedure btnLoginWithExceptionClick(Sender: TObject);
procedure btnLoginJsonObjectClick(Sender: TObject);
private
FJWT: string;
procedure SetJWT(const Value: string);
property JWT: string read FJWT write SetJWT;
public
{ Public declarations }
end;
var
Form5: TForm5;
implementation
{$R *.dfm}
uses
MVCFramework.RESTClient,
MVCFramework.SystemJSONUtils,
2020-08-26 22:13:18 +02:00
System.JSON,
MVCFramework.RESTClient.Intf;
procedure TForm5.btnGetClick(Sender: TObject);
var
2020-08-26 22:13:18 +02:00
lClient: IMVCRESTClient;
lResp: IMVCRESTResponse;
begin
{ Getting JSON response }
2020-08-26 22:13:18 +02:00
lClient := TMVCRESTClient.New
.BaseURL('localhost', 8080)
.ReadTimeOut(0);
if not FJWT.IsEmpty then
begin
lClient.SetBearerAuthorization(FJWT);
end;
2020-08-26 22:13:18 +02:00
lResp := lClient
.AddQueryStringParam('firstname', 'Daniele')
.AddQueryStringParam('lastname', 'Teti')
.Get('/admin/role1');
if not lResp.Success then
ShowMessage(lResp.Content);
Memo2.Lines.Text := lResp.Content;
lResp := lClient
.AddQueryStringParam('firstname', 'Daniele')
.AddQueryStringParam('lastname', 'Teti')
.Accept('text/html')
.Get('/admin/role1');
if not lResp.Success then
ShowMessage(lResp.Content);
Memo3.Lines.Text := lResp.Content;
end;
procedure TForm5.btnLOGINClick(Sender: TObject);
var
2020-08-26 22:13:18 +02:00
lClient: IMVCRESTClient;
lRest: IMVCRESTResponse;
lJSON: TJSONObject;
begin
2020-08-26 22:13:18 +02:00
lClient := TMVCRESTClient.New
.BaseURL('localhost', 8080)
.ReadTimeOut(0)
.SetBasicAuthorization('user1', 'user1');
lRest := lClient.Post('/login');
if not lRest.Success then
begin
ShowMessage(
'HTTP ERROR: ' + lRest.StatusCode.ToString + sLineBreak +
'HTTP MESSAGE: ' + lRest.StatusText + sLineBreak +
'CONTENT MESSAGE: ' + lRest.Content);
Exit;
end;
lJSON := TSystemJSON.StringAsJSONObject(lRest.Content);
try
2020-08-26 22:13:18 +02:00
JWT := lJSON.GetValue('token').Value;
finally
2020-08-26 22:13:18 +02:00
lJSON.Free;
end;
end;
procedure TForm5.btnLoginJsonObjectClick(Sender: TObject);
var
2020-08-26 22:13:18 +02:00
lClient: IMVCRESTClient;
lRest: IMVCRESTResponse;
lJSON: TJSONObject;
begin
2020-08-26 22:13:18 +02:00
lClient := TMVCRESTClient.New
.BaseURL('localhost', 8080)
.ReadTimeOut(0);
lRest := lClient.Post('/login', '{"jwtusername":"user1","jwtpassword":"user1"}');
if not lRest.Success then
begin
ShowMessage(
'HTTP ERROR: ' + lRest.StatusCode.ToString + sLineBreak +
'HTTP MESSAGE: ' + lRest.StatusText + sLineBreak +
'CONTENT MESSAGE: ' + lRest.Content);
Exit;
end;
lJSON := TSystemJSON.StringAsJSONObject(lRest.Content);
try
2020-08-26 22:13:18 +02:00
JWT := lJSON.GetValue('token').Value;
finally
2020-08-26 22:13:18 +02:00
lJSON.Free;
end;
end;
procedure TForm5.btnLoginWithExceptionClick(Sender: TObject);
var
2020-08-26 22:13:18 +02:00
lClient: IMVCRESTClient;
lRest: IMVCRESTResponse;
lJSON: TJSONObject;
begin
2020-08-26 22:13:18 +02:00
lClient := TMVCRESTClient.New
.BaseURL('localhost', 8080)
.ReadTimeOut(0)
.SetBasicAuthorization('user_raise_exception', 'user_raise_exception');
lRest := lClient.Post('/login');
if not lRest.Success then
begin
ShowMessage(
'HTTP ERROR: ' + lRest.StatusCode.ToString + sLineBreak +
'HTTP MESSAGE: ' + lRest.StatusText + sLineBreak +
'CONTENT MESSAGE: ' + lRest.Content);
Exit;
end;
lJSON := TSystemJSON.StringAsJSONObject(lRest.Content);
try
2020-08-26 22:13:18 +02:00
JWT := lJSON.GetValue('token').Value;
finally
2020-08-26 22:13:18 +02:00
lJSON.Free;
end;
end;
procedure TForm5.SetJWT(const Value: string);
begin
FJWT := Value;
Memo1.Lines.Text := Value;
end;
end.