From b67f83fa66b39736a3281b484d84aef8eff890cd Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Fri, 2 Jan 2015 19:29:16 -0500 Subject: Analog of request-all-items for sx-method-call --- sx-method.el | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'sx-method.el') diff --git a/sx-method.el b/sx-method.el index 1078014..8fa30c9 100644 --- a/sx-method.el +++ b/sx-method.el @@ -38,6 +38,9 @@ (filter '(())) auth (url-method "GET") + get-all + (process-function + #'sx-request-response-get-items) site) "Call METHOD with additional keys. @@ -50,6 +53,8 @@ user. authentication. :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 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 +71,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)) @@ -82,7 +99,12 @@ Return the entire response as a complex alist." (prog1 (format "?site=%s" site) (setq site nil))))) - (call #'sx-request-make) + (call (if get-all #'sx-request-all-items #'sx-request-make)) + (get-all-stop-when + (cond + ((eq get-all t) #'sx-request-all-stop-when-no-more) + (get-all get-all) + (t nil))) parameters) (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) @@ -106,11 +128,15 @@ Return the entire response as a complex alist." (cons (cons 'filter (sx-filter-get-var filter)) keywords)) (when site + ;; @TODO: Maybe use `push' instead? (setq parameters (cons (cons 'site site) parameters))) (funcall call full-method parameters - url-method))) + url-method + (if get-all + get-all-stop-when + process-function)))) (provide 'sx-method) ;;; sx-method.el ends here -- cgit v1.2.3 From 37e05282fa3d6c9c85e75d9b1768c67b8e2afbd9 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sat, 3 Jan 2015 22:21:57 -0500 Subject: Remove superfluous local variable --- sx-method.el | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'sx-method.el') diff --git a/sx-method.el b/sx-method.el index 8fa30c9..6288cbb 100644 --- a/sx-method.el +++ b/sx-method.el @@ -104,8 +104,7 @@ Return the entire response as a complex alist." (cond ((eq get-all t) #'sx-request-all-stop-when-no-more) (get-all get-all) - (t nil))) - parameters) + (t nil)))) (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 @@ -124,15 +123,15 @@ 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 + (setq keywords (cons (cons 'filter (sx-filter-get-var filter)) keywords)) (when site ;; @TODO: Maybe use `push' instead? - (setq parameters (cons (cons 'site site) parameters))) + (setq keywords (cons (cons 'site site) keywords))) (funcall call full-method - parameters + keywords url-method (if get-all get-all-stop-when -- cgit v1.2.3 From 14d7ada674f2d1f20c3843aa15a02d717c8f388b Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sat, 3 Jan 2015 22:23:07 -0500 Subject: Simplify form If it's `t', then we still have our desired behavior. If it's not `t', then we just have 'it' -- when 'it' is `nil' (note, also not `t'), we obviously have `nil'. The behavior has not changed through this patch. --- sx-method.el | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'sx-method.el') diff --git a/sx-method.el b/sx-method.el index 6288cbb..063c404 100644 --- a/sx-method.el +++ b/sx-method.el @@ -103,8 +103,7 @@ Return the entire response as a complex alist." (get-all-stop-when (cond ((eq get-all t) #'sx-request-all-stop-when-no-more) - (get-all get-all) - (t nil)))) + (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 -- cgit v1.2.3 From 9ff43f79e579a9da9a74429204ff52b26f53bf4e Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sat, 3 Jan 2015 22:36:58 -0500 Subject: Use `push' instead of `setq' --- sx-method.el | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'sx-method.el') diff --git a/sx-method.el b/sx-method.el index 063c404..8b97849 100644 --- a/sx-method.el +++ b/sx-method.el @@ -122,12 +122,9 @@ 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 keywords - (cons (cons 'filter (sx-filter-get-var filter)) - keywords)) + (push `(filter . ,(sx-filter-get-var filter)) keywords) (when site - ;; @TODO: Maybe use `push' instead? - (setq keywords (cons (cons 'site site) keywords))) + (push `(site . ,site) keywords)) (funcall call full-method keywords -- cgit v1.2.3 From d19917d9f152e2eda6b15687dae62f149a3d5779 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sat, 3 Jan 2015 22:40:57 -0500 Subject: Simplfy logic `get-all' will be re-bound to either a function or nil, so using `or' with it is appropriate. --- sx-method.el | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'sx-method.el') diff --git a/sx-method.el b/sx-method.el index 8b97849..64dbc1f 100644 --- a/sx-method.el +++ b/sx-method.el @@ -100,7 +100,7 @@ Return the entire response as a complex alist." (format "?site=%s" site) (setq site nil))))) (call (if get-all #'sx-request-all-items #'sx-request-make)) - (get-all-stop-when + (get-all (cond ((eq get-all t) #'sx-request-all-stop-when-no-more) (t get-all)))) @@ -129,9 +129,7 @@ Return the entire response as a complex alist." full-method keywords url-method - (if get-all - get-all-stop-when - process-function)))) + (or get-all process-function)))) (provide 'sx-method) ;;; sx-method.el ends here -- cgit v1.2.3 From eeb327e3af94e1118218c129916b8f7165e835ef Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sat, 3 Jan 2015 22:51:45 -0500 Subject: Introduce :page and :pagesize into sx-method-call Default page size is 100 to reduce number of overall requests made. Fixes #190. --- sx-method.el | 6 ++++++ sx-site.el | 2 +- sx-tag.el | 8 +++----- 3 files changed, 10 insertions(+), 6 deletions(-) (limited to 'sx-method.el') diff --git a/sx-method.el b/sx-method.el index 64dbc1f..e0764fb 100644 --- a/sx-method.el +++ b/sx-method.el @@ -35,6 +35,8 @@ (cl-defun sx-method-call (method &key id submethod keywords + page + (pagesize 100) (filter '(())) auth (url-method "GET") @@ -55,6 +57,8 @@ authentication. :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 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 @@ -123,6 +127,8 @@ Return the entire response as a complex alist." (error "This request requires authentication.")))) ;; Concatenate all parameters now that filter is ensured. (push `(filter . ,(sx-filter-get-var filter)) keywords) + (push `(page . ,page) keywords) + (push `(pagesize . ,pagesize) keywords) (when site (push `(site . ,site) keywords)) (funcall call diff --git a/sx-site.el b/sx-site.el index 8bd4fc0..1402bb9 100644 --- a/sx-site.el +++ b/sx-site.el @@ -49,7 +49,7 @@ (sx-cache-get 'site-list '(sx-method-call 'sites - :keywords '((pagesize . 999)) + :pagesize 999 :filter sx-site-browse-filter))) (defcustom sx-site-favorites diff --git a/sx-tag.el b/sx-tag.el index e9d3f4f..07b7a77 100644 --- a/sx-tag.el +++ b/sx-tag.el @@ -39,8 +39,7 @@ (sx-method-call 'tags :get-all t :filter sx-tag-filter - :site site - :keywords '((pagesize . 100))))) + :site site))) (defun sx-tag--get-some-tags-containing (site string) "Return at most 100 tags for SITE containing STRING. @@ -49,7 +48,7 @@ Returns an array." :auth nil :filter sx-tag-filter :site site - :keywords `((page . 1) (pagesize . 100) (inname . ,string)))) + :keywords `((inname . ,string)))) (defun sx-tag--get-some-tag-names-containing (site string) "Return at most 100 tag names for SITE containing STRING. @@ -74,8 +73,7 @@ Return the list of invalid tags in TAGS." :submethod 'info :auth nil :filter sx-tag-filter - :site site - :keywords '((page . 1) (pagesize . 100)))))) + :site site)))) (cl-remove-if (lambda (x) (member x result)) tags))) (provide 'sx-tag) -- cgit v1.2.3 From f5efa010e4c16eda6d3652b325efaf524acd9422 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sun, 4 Jan 2015 12:16:00 -0500 Subject: Add page and pagesize only if not already present --- sx-method.el | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'sx-method.el') diff --git a/sx-method.el b/sx-method.el index e0764fb..e68a4ea 100644 --- a/sx-method.el +++ b/sx-method.el @@ -60,6 +60,9 @@ authentication. :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 to ensure awareness of where auth is needed. @@ -127,8 +130,10 @@ Return the entire response as a complex alist." (error "This request requires authentication.")))) ;; Concatenate all parameters now that filter is ensured. (push `(filter . ,(sx-filter-get-var filter)) keywords) - (push `(page . ,page) keywords) - (push `(pagesize . ,pagesize) keywords) + (unless (assq 'page keywords) + (push `(page . ,page) keywords)) + (unless (assq 'pagesize keywords) + (push `(pagesize . ,pagesize) keywords)) (when site (push `(site . ,site) keywords)) (funcall call -- cgit v1.2.3 From 1b5fd39d84cfc3138c2619631eebe508e7a0c2f9 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sun, 4 Jan 2015 15:21:55 -0500 Subject: Use symbols for url-request-method --- sx-compose.el | 4 ++-- sx-interaction.el | 6 +++--- sx-method.el | 6 +++--- sx-request.el | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) (limited to 'sx-method.el') diff --git a/sx-compose.el b/sx-compose.el index d27d2f3..8a8637b 100644 --- a/sx-compose.el +++ b/sx-compose.el @@ -188,7 +188,7 @@ respectively added locally to `sx-compose-before-send-hook' and (.comment_id 'comments) (t 'answers)) :auth 'warn - :url-method "POST" + :url-method 'post :filter sx-browse-filter :site site :keywords (sx-compose--generate-keywords is-question) @@ -196,7 +196,7 @@ respectively added locally to `sx-compose-before-send-hook' and :submethod 'edit))) (lambda () (sx-method-call 'questions :auth 'warn - :url-method "POST" + :url-method 'post :filter sx-browse-filter :site site :keywords (sx-compose--generate-keywords is-question) diff --git a/sx-interaction.el b/sx-interaction.el index 80c8e5a..a72b14c 100644 --- a/sx-interaction.el +++ b/sx-interaction.el @@ -202,7 +202,7 @@ With the UNDO prefix argument, unfavorite the question instead." :submethod (if undo 'favorite/undo 'favorite) :auth 'warn :site .site_par - :url-method "POST" + :url-method 'post :filter sx-browse-filter))) (defalias 'sx-star #'sx-favorite) @@ -241,7 +241,7 @@ changes." :id (or .comment_id .answer_id .question_id) :submethod (concat type (unless status "/undo")) :auth 'warn - :url-method "POST" + :url-method 'post :filter sx-browse-filter :site .site_par)))) ;; The api returns the new DATA. @@ -282,7 +282,7 @@ TEXT is a string. Interactively, it is read from the minibufer." :id (or .post_id .answer_id .question_id) :submethod "comments/add" :auth 'warn - :url-method "POST" + :url-method 'post :filter sx-browse-filter :site .site_par :keywords `((body . ,text))))) diff --git a/sx-method.el b/sx-method.el index e68a4ea..bff6d30 100644 --- a/sx-method.el +++ b/sx-method.el @@ -39,7 +39,7 @@ (pagesize 100) (filter '(())) auth - (url-method "GET") + (url-method 'get) get-all (process-function #'sx-request-response-get-items) @@ -53,7 +53,7 @@ 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 @@ -102,7 +102,7 @@ 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))))) diff --git a/sx-request.el b/sx-request.el index b2aee5d..bab53ec 100644 --- a/sx-request.el +++ b/sx-request.el @@ -134,7 +134,7 @@ access the response wrapper." (defun sx-request-make (method &optional args request-method process-function) "Make a request to the API, executing METHOD with ARGS. You should almost certainly be using `sx-method-call' instead of -this function. REQUEST-METHOD is one of `GET' (default) or `POST'. +this function. REQUEST-METHOD is one of `get' (default) or `post'. Returns the entire response as processed by PROCESS-FUNCTION. This defaults to `sx-request-response-get-items'. @@ -158,7 +158,7 @@ the main content of the response is returned." (url-inhibit-uncompression t) (url-request-data (sx-request--build-keyword-arguments args nil)) (request-url (concat sx-request-api-root method)) - (url-request-method request-method) + (url-request-method (and request-method (symbol-name request-method))) (url-request-extra-headers '(("Content-Type" . "application/x-www-form-urlencoded"))) (response-buffer (url-retrieve-synchronously request-url))) -- cgit v1.2.3