首页 > 解决方案 > Delphi REST - 我如何知道何时检索到所有数据?

问题描述

在 Delphi Tokyo 中,我将一系列REST组件(与 Delphi 一起提供的组件:RESTClientRESTRequestRESTResponseRESTAdapater)捆绑在一起以检索REST数据。

REST正如服务器上定义的那样,调用将分页设置为某个值。
因此,在我的 Delphi 应用程序中,我必须反复更新RESTRequest.

ResourceSuffix 添加 '?page=' 然后是页码。
由于各种 REST 服务可能有不同的分页,或者会有不同的结果行数。
我怎么知道我何时检索了所有数据?

当然,有一些更优雅的东西会一直尝试,直到检索到的行 = 0/一些错误。

标签: restdelphi

解决方案


我找到了一个适合我的解决方案……我的数据来自 Oracle RDBMS,通过 ORDS 和 APEX。REST 内容(特别是 RAW 数据)中有一个用于下一个分页集的 URL。对于第一组数据,此 URL 引用位于 REST 数据流的末尾。对于每个后续数据集,URL 引用位于原始数据流的开头,因此您必须检查这两个位置。这是我正在使用的代码...

function IsMoreRESTDataAvailable(Content: String) : Boolean;
var
Test600 : String;
begin
  // This routine takes the RESTResponse.Content, aka the raw REST data, and checks to see if there is a NEXT: string
  // in either the end of the data (only available for the FIRST DATA set
  // or the beginning of the data

      result := False;

      Test600 := RightStr(Content, 600);
      If AnsiPos('"next":{"$ref":"https://<YOUR_SERVER_HERE>', Test600) <> 0 then
      begin
        result:= True;
        Exit;
      end;

       // If we didn't find it at the end of the REST content, then check at the beginning
       Test600 := LeftStr(Content, 600);
      If AnsiPos('"next":{"$ref":"https://<YOUR_SERVER_HERE>', Test600) <> 0 then
      begin
        result:= True;
        Exit;
      end;

end;

推荐阅读