* Added generator

* Added request output
This commit is contained in:
Andreas Schneider 2015-10-07 16:22:44 +02:00
parent b260f184f8
commit e576805e84
2 changed files with 57 additions and 5 deletions

View File

@ -113,6 +113,16 @@ Once the first none-empty line is unrecognized (no command found), the parser wi
: Sets authentication for the HTTP proxy.
Same syntax as `BasicAuth`.
`Generate <variablename> <generator>`
: Sets a variable with a generated value.
Possible generators:
* `UUID`
* `unixtime` (a unix timestamp)
* `localtime` (a formatted time)
* `isodatetime` (Date and Time according to ISO8601)
* `isodate` (Date according to ISO8601)
* `isotime` (Time according to ISO8601)
`Call <URL>`
: This prepares the actual call by providing the URL to be called.
Variables in the form of `@<variablename>` are replaced accordingly.

View File

@ -69,6 +69,7 @@ type
procedure CmdFormField(AData: String);
procedure CmdProxy(AData: String);
procedure CmdProxyAuth(AData: String);
procedure CmdGenerate(AData: String);
procedure ProcessCall(AURL: String);
//Helper
procedure WriteHelp;
@ -83,7 +84,7 @@ implementation
uses
strutils, crt, UCRTHelper, fpjson, DOM, XMLRead, XMLWrite, vinfo,
IdCompressorZLib, IdHeaderList, IdGlobalProtocols;
IdCompressorZLib, IdHeaderList, IdGlobalProtocols, xmlxsdparser;
type
TContentType = (ctOther, ctJSON, ctXML);
@ -165,8 +166,8 @@ begin
if FileExists(ParamStr(ParamCount)) then
begin
AssignFile(data, ParamStr(1));
FTemplateName := ExtractFileName(ParamStr(1));
AssignFile(data, ParamStr(ParamCount));
FTemplateName := ExtractFileName(ParamStr(ParamCount));
if AnsiEndsStr('.rest', FTemplateName) then
FTemplateName := Copy(FTemplateName, 1, Length(FTemplateName) - 5);
end else
@ -267,6 +268,11 @@ begin
Result := True;
CmdProxy(Copy(ALine, 10, Length(ALine)));
end else
if AnsiStartsStr('Generate ', ALine) then
begin
Result := True;
CmdGenerate(Copy(ALine, 10, Length(ALine)));
end else
if AnsiStartsStr('Call ', ALine) then
begin
Result := True;
@ -379,6 +385,35 @@ begin
FHttp.ProxyParams.ProxyPassword := Copy(AData, i + 1, Length(AData));
end;
procedure TRestemplateApplication.CmdGenerate(AData: String);
var
i: Integer;
varName, generator: String;
guid: TGuid;
begin
i := 1;
while (i < Length(AData)) and (AData[i] <> ' ') do
Inc(i);
varName := Copy(AData, 1, i - 1);
generator := LowerCase(Copy(AData, i + 1, Length(AData)));
case generator of
'uuid':
begin
CreateGUID(guid);
FParser.Fields.Add(varName, Copy(GUIDToString(guid), 2, 36));
end;
'unixtime': FParser.Fields.Add(varName, IntToStr(DateTimeToUnix(Now)));
'localtime': FParser.Fields.Add(varName, DateTimeToStr(Now));
'isodatetime': FParser.Fields.Add(varName, xsdFormatDateTime(Now, nil));
'isodate': FParser.Fields.Add(varName, xsdFormatDate(Now));
'isotime': FParser.Fields.Add(varName, xsdFormatTime(Now));
else
raise Exception.Create('Unknown generator: ' + generator);
end;
end;
procedure TRestemplateApplication.ProcessCall(AURL: String);
var
s: String;
@ -387,8 +422,6 @@ var
jsonData: TJSONData;
contentType: TContentType;
xmlDoc: TXMLDocument;
//
i: Integer;
begin
FParser.Content := AURL;
FParser.Replace;
@ -409,6 +442,15 @@ begin
FContent.SaveToStream(request);
end;
if HasOption('showrequest') then
begin
//TODO Handle URL-Encoded FormFields here!
writeln;
writeln('Request:');
for s in FContent do
writeln(' ', s);
end;
try
FRequest.Prepare;
FHttp.Request := FRequest;