TComplexType in HttpAPI

I’ve the below Complex type Declaration and function call:

  TAutoReturnInfo = class(TROComplexType)
  private
   fCallStatus : String;
    fMsgInfo : String;
    fDataBinary : Binary;
  published
    [ROSerializeAsAnsiString]
    property CallStatus: String read fCallStatus write fCallStatus;
    property MsgInfo: String read fMsgInfo write fMsgInfo;
    property DataBinary: Binary read fDataBinary write fDataBinary;
  end;

function TAllTableService.getuser(const ENTITY_ID_PK, USER_ID_PK: UnicodeString): TAutoReturnInfo;
var
  srcData,umData: IDADataset;
  sjoson:TDAJSONDataStreamer;
  wb:TDAWhereBuilder;
begin
  Result := TAutoReturnInfo.Create;
  Result.MsgInfo := 'No Message';
  Result.DataBinary := TROHttpApiResult.Create(HTTP_200_code, id_ContentType_application_json_charset_utf8,'',false);
  wb:=TDAWhereBuilder.Create;


     wb.Expression:=wb.NewBinaryExpressionList(
     [
      //wb.NewBinaryExpression(wb.NewField('', 'USER_ENTITY_ID_PK'), wb.NewConstant(ENTITY_ID_PK), dboEqual),
      wb.NewBinaryExpression(wb.NewField('', 'USER_ID_PK'), wb.NewConstant(USER_ID_PK), dboEqual)
     ]
    ,dboAnd);

  srcData := AllTableDASchema.NewDataset(Connection,'USER_TBL',[],wb.Xml);
  umData := AllTableDASchema.NewDataset(Connection,'UM_TBL');
  sjoson:=TDAJSONDataStreamer.Create(Self);
  sjoson.Initialize(Result.DataBinary,aiwrite);
  try
    sjoson.WriteDataset(srcData, [ woRows], -1);
    sjoson.WriteDataset(umData, [ woRows], -1);

  finally
    sjoson.Finalize;
  end;
  FreeAndNil(wb);
  FreeAndNil(sjoson);
  srcData := Nil;
  umData := Nil;

end;

However, I only get the dataset stream data only . The MsgInfo , CallStatus does not return.

please advise how to achieve that case.

joe

Hi,

this is as expected.
TROHttpApiResult is a special type that can be used only as a Result.
when server detects this type, actual method’s result will be replaced with this type.

by other words, it will works as

function TAllTableService.getuser(const ENTITY_ID_PK, USER_ID_PK: UnicodeString):Binary;
begin
  Result :=  TROHttpApiResult.Create(HTTP_200_code, id_ContentType_application_json_charset_utf8,'',false);
....
end;

Yes This is my first version but I need return together with status and some additional info.

I will use the binary type to return dataset list and the additional msginfo if one of dataset get exception, I can get the exception info.

If I really need return binary type together with additional raw data member like the TAutoReturnInfo.
How can I achieve it?

joe

Hi,

You can declare your own type with any set of properties.
Just to not use special TROHttpApiResult type.

Hi,

Result.DataBinary := Binary.Create;

If I change like this , I can get the correct data member. However, The result dataformat for databinary, it is not json format, it is bin format as below picture

How can I Achieve it as the json format as create by TROHttpApiResult as It is consumed by other rest client?

Please advise

Hi,

you can try to add your extra data like CallStatus and MsgInfo to Json struct that is returned by your stream.
what I mean:

function TAllTableService.getuser(const ENTITY_ID_PK, USER_ID_PK: UnicodeString):Binary;
begin
  Result :=  TROHttpApiResult.Create(HTTP_200_code, id_ContentType_application_json_charset_utf8,'',false);
....
  sjoson.Initialize(Result.DataBinary,aiwrite);
  try
    sjoson.WriteDataset(srcData, [ woRows], -1);
    sjoson.WriteDataset(umData, [ woRows], -1);

  finally
    sjoson.Finalize;
  end;
....
// read data back from stream with `JSON_ParseStream` and add `CallStatus` and `MsgInfo` as a properties.
// you may need to put existing dataset to a level below 
// after adding `CallStatus` and `MsgInfo`, you need to write TROJSONValue back to stream.
end;

I hope, it will work as you want.