diff options
author | Sean Allred <code@seanallred.com> | 2015-01-02 00:59:59 -0500 |
---|---|---|
committer | Sean Allred <code@seanallred.com> | 2015-01-02 00:59:59 -0500 |
commit | 27eb38cfc4bba9013e8454bbe81ce497bf224474 (patch) | |
tree | ad5d57ad2f04e6d700456a824c7885fe6f793efa /sx-request.el | |
parent | 5babd59dfd51b5dd33cfd411fc2c2754adf63381 (diff) |
Introduce `sx-request-all-items'
This function repeatedly makes API requests until a condition is
satisfied (such as 'no more items').
First and foremost, this will allow us to retrieve all tags for a site.
Diffstat (limited to 'sx-request.el')
-rw-r--r-- | sx-request.el | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/sx-request.el b/sx-request.el index f1d20af..387bde5 100644 --- a/sx-request.el +++ b/sx-request.el @@ -94,6 +94,35 @@ number of requests left every time it finishes a call." ;;; Making Requests +(defun sx-request-all-items (method &optional args request-method + stop-when process-function) + "Call METHOD with ARGS until there are no more items. +STOP-WHEN is a function that takes the entire response and +returns non-nil if the process should stop. + +All other arguments are identical to `sx-request-make', but +PROCESS-FUNCTION is given the default value of `identity' (rather +than `sx-request-response-get-items') to allow STOP-WHEN to +access the response wrapper." + ;; @TODO: Refactor. This is the product of a late-night jam + ;; session... it is not intended to be model code. + (let* ((return-value []) + (current-page 1) + (stop-when (or stop-when #'sx-request-all-stop-when-no-more)) + (process-function (or process-function #'identity)) + (response + (sx-request-make method `((page . ,current-page) ,@args) + request-method process-function))) + (while (not (funcall stop-when response)) + (setq return-value + (vconcat return-value + (cdr (assoc 'items response)))) + (setq current-page (1+ current-page) + response + (sx-request-make method `((page . ,current-page) ,@args) + request-method process-function))) + (vconcat return-value + (cdr (assoc 'items response))))) (defun sx-request-make (method &optional args request-method process-function) "Make a request to the API, executing METHOD with ARGS. @@ -213,6 +242,9 @@ false, use the symbol `false'. Each element is processed with (sx-assoc-let response (sx-encoding-clean-content-deep .items))) +(defun sx-request-all-stop-when-no-more (response) + (or (not response) + (equal :json-false (cdr (assoc 'has_more response))))) (provide 'sx-request) ;;; sx-request.el ends here |