From e576805e845f83d8a25ec3ccc9c0410bdf1d2edd Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Wed, 7 Oct 2015 16:22:44 +0200 Subject: [PATCH] * Added generator * Added request output --- README.md | 10 +++++++++ URestemplateApp.pas | 52 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 872f829..df5367a 100644 --- a/README.md +++ b/README.md @@ -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 ` +: 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 ` : This prepares the actual call by providing the URL to be called. Variables in the form of `@` are replaced accordingly. diff --git a/URestemplateApp.pas b/URestemplateApp.pas index 01556ab..966269a 100644 --- a/URestemplateApp.pas +++ b/URestemplateApp.pas @@ -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;