Collecting Multiple Pages of REST Results in a Single MemTable

I’ve run into this a few times, where a REST Service returns results in pages, so you have to make multiple passes to get all the data. One thing I love about the RAD REST Client Library is the ability to store all the data in the FDMemTable where you can work on it. Unfortunately when you can only get part of the results that limits the functionality. Here is a simple solution to get all the pages in a single FDMemTable.

What you need is a second FDMemTable. My first one is called MembersTempTable, and it is attached to the Response Adapter. The second one is called MembersMemTable and it will hold the complete set of data.

<br />
  // Drop any existing data<br />
  MembersMemTable.Close; </p>
<p>  // Get the first page<br />
  MembersRequest.Params.ParameterByName('limit').Value := '50';<br />
  MembersRequest.Params.ParameterByName('offset').Value := '0';<br />
  MembersRequest.Execute;</p>
<p>  // This clones the FDMemTable<br />
  MembersMemTable.CopyDataSet(MembersTempTable,<br />
    [coStructure, coRestart, coAppend]);</p>
<p>  // Now we add the other tables<br />
  MembersMemTable.BeginBatch();<br />
  try<br />
    // Repeat until we don't get a full page<br />
    while MembersTempTable.RecordCount = 50 do<br />
    begin<br />
      // Start with the next page<br />
      MembersRequest.Params.ParameterByName('offset').Value :=<br />
        (MembersMemTable.RecordCount - 1).ToString;<br />
      MembersRequest.Execute;<br />
      // Append those records into our FDMemTable<br />
      MembersMemTable.CopyDataSet(MembersTempTable, [coAppend]);<br />
    end;<br />
    MembersMemTable.IndexFieldNames := 'id';<br />
  finally<br />
    MembersMemTable.EndBatch;<br />
  end;<br />

This entry was posted in REST. Bookmark the permalink.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.