aboutsummaryrefslogtreecommitdiff
path: root/sx-method.el
diff options
context:
space:
mode:
Diffstat (limited to 'sx-method.el')
-rw-r--r--sx-method.el52
1 files changed, 41 insertions, 11 deletions
diff --git a/sx-method.el b/sx-method.el
index 1078014..bff6d30 100644
--- a/sx-method.el
+++ b/sx-method.el
@@ -35,9 +35,14 @@
(cl-defun sx-method-call (method &key id
submethod
keywords
+ page
+ (pagesize 100)
(filter '(()))
auth
- (url-method "GET")
+ (url-method 'get)
+ get-all
+ (process-function
+ #'sx-request-response-get-items)
site)
"Call METHOD with additional keys.
@@ -48,8 +53,15 @@ user.
:FILTER is the set of filters to control the returned information
:AUTH defines how to act if the method or filters require
authentication.
-:URL-METHOD is either \"POST\" or \"GET\"
+:URL-METHOD is either `post' or `get'
:SITE is the api parameter specifying the site.
+:GET-ALL is nil or non-nil
+:PROCESS-FUNCTION is a response-processing function
+:PAGE is the page number which will be requested
+:PAGESIZE is the number of items to retrieve per request, default 100
+
+Any conflicting information in :KEYWORDS overrides the :PAGE
+and :PAGESIZE settings.
When AUTH is nil, it is assumed that no auth-requiring filters or
methods will be used. If they are an error will be signaled. This is
@@ -66,6 +78,18 @@ for interactive commands that absolutely require authentication
\(submitting questions/answers, reading inbox, etc). Filters will
treat 'warn as equivalent to t.
+If GET-ALL is nil, this method will only return the first (or
+specified) page available from this method call. If t, all pages
+will be retrieved (`sx-request-all-stop-when-no-more') .
+Otherwise, it is a function STOP-WHEN for `sx-request-all-items'.
+
+If PROCESS-FUNCTION is nil, only the items of the response will
+be returned (`sx-request-response-get-items'). Otherwise, it is
+a function that processes the entire response (as returned by
+`json-read').
+
+See `sx-request-make' and `sx-request-all-items'.
+
Return the entire response as a complex alist."
(declare (indent 1))
(let ((access-token (sx-cache-get 'auth))
@@ -78,12 +102,15 @@ Return the entire response as a complex alist."
(format "/%s" submethod))
;; On GET methods site is buggy, so we
;; need to provide it as a url argument.
- (when (and site (string= url-method "GET"))
+ (when (and site (eq url-method 'get))
(prog1
(format "?site=%s" site)
(setq site nil)))))
- (call #'sx-request-make)
- parameters)
+ (call (if get-all #'sx-request-all-items #'sx-request-make))
+ (get-all
+ (cond
+ ((eq get-all t) #'sx-request-all-stop-when-no-more)
+ (t get-all))))
(lwarn "sx-call-method" :debug "A: %S T: %S. M: %S,%s. F: %S" (equal 'warn auth)
access-token method-auth full-method filter-auth)
(unless access-token
@@ -102,15 +129,18 @@ Return the entire response as a complex alist."
((and (or filter-auth method-auth) (not auth))
(error "This request requires authentication."))))
;; Concatenate all parameters now that filter is ensured.
- (setq parameters
- (cons (cons 'filter (sx-filter-get-var filter))
- keywords))
+ (push `(filter . ,(sx-filter-get-var filter)) keywords)
+ (unless (assq 'page keywords)
+ (push `(page . ,page) keywords))
+ (unless (assq 'pagesize keywords)
+ (push `(pagesize . ,pagesize) keywords))
(when site
- (setq parameters (cons (cons 'site site) parameters)))
+ (push `(site . ,site) keywords))
(funcall call
full-method
- parameters
- url-method)))
+ keywords
+ url-method
+ (or get-all process-function))))
(provide 'sx-method)
;;; sx-method.el ends here