ChainFetchAdapter

This interface allows your Adapter to indefinitely chain fetchRecord() calls, each in its own execution context.

This is an extension interface to SourceAdapterForPull. It complements the IMMEDIATE FetchStrategy by allowing you to repeatedly call your fetchRecords() method, each time in a new execution context with a new scope.

This interface will be tremendously useful to you if you are fetching records from an external system that cannot predict in advance how many records it will be giving you. Some APIs simply say “I have more records to give you, please query again with this token/page/url”, and you have to keep calling them until you exhaust the records. This interface helps in that circumstance.

If you implement this interface, right after fetchRecords() is called—and in the same execution context—getNextScope() will be called. Store whatever information you need in the scope and it will be passed to the next invocation of fetchRecords().

Note

Chaining will continue until you return a null from getNextScope().

Warning

The very first time fetchRecords() is called, a null scope will be passed to your adapter. This is because getNextScope() has never yet been called. You are expected to initialize appropriate scope state in your planFetch().

So, to be clear, the order of execution will be:

  1. planFetch()

  2. fetchRecords(null scope)

  3. getNextScope()

  4. –NEW EXECUTION CONTEXT–

  5. fetchRecords(scope returned by #3)

  6. getNextScope()

  7. –NEW EXECUTION CONTEXT–

  8. fetchRecords(scope returned by #6)

  9. getNextScope()

  10. …and so on, indefinitely, until you return a null value from getNextScope()

Definition

/**
 * Implement this interface if you are fetching data from an external API that can't tell you in advance how
 * many records will be returned. Implementing this Adapter allows Valence to alternate asking your Adapter
 * for records with requests for the next scope to use.
 */
global interface ChainFetchAdapter extends SourceAdapterForPull {

        /**
         * @return A scope object that will be passed back to you on the next call to FetchRecords.
         */
        Object getNextScope();
}