Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulTSmith/3338421a1fcd7c42f8612a43a65a1173 to your computer and use it in GitHub Desktop.
Save PaulTSmith/3338421a1fcd7c42f8612a43a65a1173 to your computer and use it in GitHub Desktop.
Delphi login and JSON conversion to dataset for Dreamfactory
private
{ Private declarations }
JSONLogin: TJSONObject;
JSONResponse1, JSONResponse2: TJSONObject;
sCleanedSessionID, sCleanedSessionToken: string;
procedure login(sEmail, sPassword: string);
procedure GetDataFromTable(sTableName: string);
public
{ Public declarations }
end;
const
//Get these from DreamFactory
BASEURL = 'http://localhost:82/api/v2';
APIKEY = 'd52c15307c1e4bd7270cdd53fa416bee3d201972231b737ea69eec888889a8b2';
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Login(edit1.Text, edit2.Text); //For the user name and password
end;
procedure TForm1.GetDataFromTable(sTableName: string);
begin
//The DreamFactory Service is MSSQL; the table is Tasks
with RESTRequest2 do
begin
Accept := 'application/json';
Resource := 'MSSQL/_table/tasks';
Params.AddItem('X-DreamFactory-API-Key', APIKEY, pkHTTPHEADER);
Params.AddItem('X-DreamFactory-Session-Token', sCleanedSessionToken, pkHTTPHEADER);
Execute;
end;
end;
procedure TForm1.JSONToDatasetTasks(JSON: TJSONObject);
var
aTasks: TJSONArray;
oTask: TJSONObject;
vItem: TJSONValue;
i: integer;
begin
try
//Put the JSON object into an array
aTasks := JSON.Get('resource').JsonValue as TJSONArray;
if aTasks = nil then
ShowMessage('Empty task list') else
begin
dxMemdata1.active := true; //dxMemdata is an in-memory table from Developer Express. I previously added the columns
//Loop through the individual records
for i := 0 to aTasks.size -1 do
begin
oTask := TJSONObject(aTasks.Get(i));
vItem := oTask.Get('Description').JSONValue;
With dxMemdata1 do
begin
insert;
Fieldvalues['Description'] := vItem.Value;
end;
//Do something similar for the remaining columns
end;
end;
finally
JSON.Free;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
GetDataFromTable('tasks');
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
JSONToDatasetTasks(JSONResponse2);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
RestClient1.BaseURL := BASEURL;
end;
procedure TForm1.RESTRequest1AfterExecute(Sender: TCustomRESTRequest);
var
jvSessionID, jvSessionToken: TJSONValue;
sSessionID, sSessionToken: string;
begin
JSONLogin.Free;
//RESTResponse1 gets the response from RestRequest1; it is linked in the Response property of the request
JSONResponse1 := nil;
//Convert the response into a JSON object for parsing
JSONResponse1 := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(RESTResponse1.JSONText), 0) as TJSONObject;
//Get the session id and token as JSONValues (they appear to be identical)
jvSessionID := JSONResponse1.GetValue('session_id');
jvSessionToken := JSONResponse1.GetValue('session_token');
//Convert it to a string
sSessionID := jvSessionID.ToString;
//Remove the opening and closing double quotes
sCleanedSessionID := StringReplace(sSessionID,'"','',[rfReplaceAll]);
sSessionToken := jvSessionToken.ToString;
sCleanedSessionToken := StringReplace(sSessionToken,'"','',[rfReplaceAll]);
JSONResponse1.Free;
end;
procedure TForm1.RESTRequest2AfterExecute(Sender: TCustomRESTRequest);
begin
Label6.caption := RESTResponse2.JSONText;
JSONResponse2 := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(RESTResponse2.JSONText), 0) as TJSONObject;
//JSONResponse2.Free; //This is done by JSONToDatasetTasks
end;
procedure TForm1.login(sEmail, sPassword: string);
begin
with RestRequest1 do
begin
Method := rmPost;
Resource := 'user/session';
Accept := 'application/json'; //Important: the default values in the Delphi RestRequest component don't work
end;
JSONLogin := TJSONObject.Create;
with JSONLogin do
begin
AddPair('email', sEmail);
AddPair('password', sPassWord);
end;
with RestRequest1 do
begin
AddBody(JSONLogin);
Execute;
end;
end;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment