* 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. : Sets authentication for the HTTP proxy.
Same syntax as `BasicAuth`. 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>` `Call <URL>`
: This prepares the actual call by providing the URL to be called. : This prepares the actual call by providing the URL to be called.
Variables in the form of `@<variablename>` are replaced accordingly. Variables in the form of `@<variablename>` are replaced accordingly.

View File

@ -69,6 +69,7 @@ type
procedure CmdFormField(AData: String); procedure CmdFormField(AData: String);
procedure CmdProxy(AData: String); procedure CmdProxy(AData: String);
procedure CmdProxyAuth(AData: String); procedure CmdProxyAuth(AData: String);
procedure CmdGenerate(AData: String);
procedure ProcessCall(AURL: String); procedure ProcessCall(AURL: String);
//Helper //Helper
procedure WriteHelp; procedure WriteHelp;
@ -83,7 +84,7 @@ implementation
uses uses
strutils, crt, UCRTHelper, fpjson, DOM, XMLRead, XMLWrite, vinfo, strutils, crt, UCRTHelper, fpjson, DOM, XMLRead, XMLWrite, vinfo,
IdCompressorZLib, IdHeaderList, IdGlobalProtocols; IdCompressorZLib, IdHeaderList, IdGlobalProtocols, xmlxsdparser;
type type
TContentType = (ctOther, ctJSON, ctXML); TContentType = (ctOther, ctJSON, ctXML);
@ -165,8 +166,8 @@ begin
if FileExists(ParamStr(ParamCount)) then if FileExists(ParamStr(ParamCount)) then
begin begin
AssignFile(data, ParamStr(1)); AssignFile(data, ParamStr(ParamCount));
FTemplateName := ExtractFileName(ParamStr(1)); FTemplateName := ExtractFileName(ParamStr(ParamCount));
if AnsiEndsStr('.rest', FTemplateName) then if AnsiEndsStr('.rest', FTemplateName) then
FTemplateName := Copy(FTemplateName, 1, Length(FTemplateName) - 5); FTemplateName := Copy(FTemplateName, 1, Length(FTemplateName) - 5);
end else end else
@ -267,6 +268,11 @@ begin
Result := True; Result := True;
CmdProxy(Copy(ALine, 10, Length(ALine))); CmdProxy(Copy(ALine, 10, Length(ALine)));
end else end else
if AnsiStartsStr('Generate ', ALine) then
begin
Result := True;
CmdGenerate(Copy(ALine, 10, Length(ALine)));
end else
if AnsiStartsStr('Call ', ALine) then if AnsiStartsStr('Call ', ALine) then
begin begin
Result := True; Result := True;
@ -379,6 +385,35 @@ begin
FHttp.ProxyParams.ProxyPassword := Copy(AData, i + 1, Length(AData)); FHttp.ProxyParams.ProxyPassword := Copy(AData, i + 1, Length(AData));
end; 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); procedure TRestemplateApplication.ProcessCall(AURL: String);
var var
s: String; s: String;
@ -387,8 +422,6 @@ var
jsonData: TJSONData; jsonData: TJSONData;
contentType: TContentType; contentType: TContentType;
xmlDoc: TXMLDocument; xmlDoc: TXMLDocument;
//
i: Integer;
begin begin
FParser.Content := AURL; FParser.Content := AURL;
FParser.Replace; FParser.Replace;
@ -409,6 +442,15 @@ begin
FContent.SaveToStream(request); FContent.SaveToStream(request);
end; end;
if HasOption('showrequest') then
begin
//TODO Handle URL-Encoded FormFields here!
writeln;
writeln('Request:');
for s in FContent do
writeln(' ', s);
end;
try try
FRequest.Prepare; FRequest.Prepare;
FHttp.Request := FRequest; FHttp.Request := FRequest;