浏览代码

* Client side can now handle stream content

Michaël Van Canneyt 1 月之前
父节点
当前提交
e497019fbc
共有 1 个文件被更改,包括 48 次插入14 次删除
  1. 48 14
      packages/fcl-openapi/src/fpopenapi.generators.pp

+ 48 - 14
packages/fcl-openapi/src/fpopenapi.generators.pp

@@ -410,7 +410,9 @@ begin
   if Assigned(aMethod.RequestBodyDataType) then
   if Assigned(aMethod.RequestBodyDataType) then
     lBodyType:=aMethod.RequestBodyDataType.GetTypeName(ntPascal);
     lBodyType:=aMethod.RequestBodyDataType.GetTypeName(ntPascal);
   if lBodyType<>'' then
   if lBodyType<>'' then
-    AddTo(lParams, 'aBody : '+lBodyType);
+    AddTo(lParams, 'aRequest : '+lBodyType);
+  if aMethod.ResultDataType.BinaryData then
+    AddTo(lParams, 'aResponseStream : TStream');
   if AsyncService then
   if AsyncService then
     AddTo(lParams, 'aCallback : '+MethodResultCallbackName(aMethod));
     AddTo(lParams, 'aCallback : '+MethodResultCallbackName(aMethod));
   // Optional
   // Optional
@@ -615,7 +617,7 @@ begin
     Addln('');
     Addln('');
     Addln('uses');
     Addln('uses');
     indent;
     indent;
-    Addln(' fpopenapiclient, %s;', [DtoUnit]);
+    Addln(' classes, fpopenapiclient, %s;', [DtoUnit]);
     undent;
     undent;
     Addln('');
     Addln('');
     EnsureSection(csType);
     EnsureSection(csType);
@@ -771,7 +773,8 @@ var
   lDecl: string;
   lDecl: string;
   lHTTPMethod: string;
   lHTTPMethod: string;
   lBodyArg: string;
   lBodyArg: string;
-  lName: string;
+  S,lName: string;
+  lResultType : TAPITypeData;
 
 
 begin
 begin
   lName:=aService.ServiceProxyImplementationClassName;
   lName:=aService.ServiceProxyImplementationClassName;
@@ -796,21 +799,51 @@ begin
   Addln('Result:=Default(%s);', [GetMethodResultType(aMethod)]);
   Addln('Result:=Default(%s);', [GetMethodResultType(aMethod)]);
   GenerateURLConstruction(aService, aMethod);
   GenerateURLConstruction(aService, aMethod);
   lHTTPMethod:=aMethod.Operation.PathComponent;
   lHTTPMethod:=aMethod.Operation.PathComponent;
-  if aMethod.RequestBodyDataType<>nil then
-    lBodyArg:='aBody.Serialize'
+  if (aMethod.RequestBodyDataType=nil) then
+    lBodyArg:=''''''
+  else if (not aMethod.RequestBodyDataType.BinaryData) then
+    lBodyArg:='aRequest.Serialize'
+  else
+    lBodyArg:='aRequest';
+  if aMethod.ResultDataType.BinaryData then
+    Addln('lResponse:=ExecuteRequest(''%s'',lURL,%s,aResponseStream);', [lHTTPMethod, lBodyArg])
   else
   else
-    lBodyArg:='''''';
-  Addln('lResponse:=ExecuteRequest(''%s'',lURL,%s);', [lHTTPMethod, lBodyArg]);
+    Addln('lResponse:=ExecuteRequest(''%s'',lURL,%s);', [lHTTPMethod, lBodyArg]);
   AddLn('Result:=%s.Create(lResponse);', [GetMethodResultType(aMethod)]);
   AddLn('Result:=%s.Create(lResponse);', [GetMethodResultType(aMethod)]);
-  if aMethod.ResultDataType<>nil then
+  lResultType:=aMethod.ResultDataType;
+  if (aMethod.ResultDataType=nil) then
+    Addln('Result.Value:=Result.Success;')
+  else if lResultType.BinaryData then
+    Addln('Result.Value:=aResponseStream;')
+  else
     begin
     begin
     Addln('if Result.Success then');
     Addln('if Result.Success then');
     indent;
     indent;
-    Addln('Result.Value:=%s.Deserialize(lResponse.Content);', [aMethod.ResultDtoType]);
+    S:=lResultType.PascalName;
+    Case lResultType.Pascaltype of
+    ptSchemaStruct,ptAnonStruct,ptArray:
+      Addln('Result.Value:=%s.Deserialize(lResponse.Content);', [S]);
+    ptString,
+    ptJSON:
+      Addln('Result.Value:=lResponse.Content;');
+    ptBoolean:
+      Addln('Result.Value:=StrToBoolDef(lResponse.Content,False);');
+    ptInteger:
+      Addln('Result.Value:=StrToIntDef(lResponse.Content,0);');
+    ptInt64:
+      Addln('Result.Value:=StrToInt64Def(lResponse.Content,0);');
+    ptDateTime:
+      Addln('Result.Value:=ISO8601ToDateDef(lResponse.Content,0);');
+    ptFloat32,
+    ptFloat64:
+      Addln('Result.Value:=StrToFloatDef(lResponse.Content,0.0);');
+    ptEnum:
+      begin
+      Raise EOpenAPi.Create('Enum result not supported');
+      end;
+    end;
     Undent;
     Undent;
-    end
-  else
-    Addln('Result.Value:=Result.Success;');
+    end;
   undent;
   undent;
   Addln('end;');
   Addln('end;');
   Addln('');
   Addln('');
@@ -850,7 +883,8 @@ begin
   Addln('');
   Addln('');
   Addln('uses');
   Addln('uses');
   indent;
   indent;
-  AddLn('fpopenapiclient');
+
+  AddLn('classes, fpopenapiclient');
   if ServiceInterfaceUnit<>'' then
   if ServiceInterfaceUnit<>'' then
     Addln(', %s                     // Service definition ', [ServiceInterfaceUnit]);
     Addln(', %s                     // Service definition ', [ServiceInterfaceUnit]);
   if (ServiceParentUnit<>'') and not SameText(ServiceParentUnit, 'fpopenapiclient') then
   if (ServiceParentUnit<>'') and not SameText(ServiceParentUnit, 'fpopenapiclient') then
@@ -946,7 +980,7 @@ begin
     begin
     begin
     if Result<>'' then
     if Result<>'' then
       Result:=Result+'; ';
       Result:=Result+'; ';
-    Result:=Result+'aBody : '+aMethod.RequestBodyDataType.PascalName;
+    Result:=Result+'aRequest : '+aMethod.RequestBodyDataType.PascalName;
     end;
     end;
 end;
 end;