From 534b1ab1f70dbc9029cd76d4ac627c1792e69ed9 Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Thu, 20 Nov 2014 13:06:01 -0500 Subject: sx-method-call rewritten and associated improvements in sx-request. Now requires `'sx-auth` to account for authentication. (sx-method-call) is now defined using `cl-defun` and uses keys rather than positional arguments. Authentication logic is performed in `sx-method-call` to ensure commands are only passed on once they are needed. When access token is available: - Filters will be used as provided - Methods will be called When access token is unavailable: - Filters will downgrade to only request object that do not require auth. - Methods will instead use `sx-request-fallback` and return nil. If auth is not specified but is required, `sx-method-call` will throw an error. If auth is specified as `'warn`, `user-error` will be called. (sx-request--build-keyword-arguments) has been simplified. It will always pass an available access_token (api indicates doing so will use a user/key quota rather than simply key quota). (sx-request-make) now performs url retrieval directly. Query construction is all performed by `sx-method-call`. --- sx-method.el | 74 ++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 20 deletions(-) (limited to 'sx-method.el') diff --git a/sx-method.el b/sx-method.el index e9c4f60..feebd84 100644 --- a/sx-method.el +++ b/sx-method.el @@ -25,32 +25,66 @@ (require 'json) (require 'url) (require 'sx) +(require 'sx-auth) (require 'sx-request) (require 'sx-filter) -(defun sx-method-call - (method &optional keyword-arguments filter need-auth use-post silent) - "Call METHOD with KEYWORD-ARGUMENTS using FILTER. +(cl-defun sx-method-call (method &key id + submethod + keywords + (filter 'none) + auth + (url-method "GET") + (site sx-site-default)) + "Call METHOD with additional keys. -If NEED-AUTH is non-nil, an auth-token is required. If 'WARN, -warn the user `(user-error ...)' if they do not have an AUTH -token set. - -If USE-POST is non-nil, use `POST' rather than `GET' for passing -arguments. - -If SILENT is non-nil, no messages will be printed. +:ID is the id associated with a question, answer, comment, post or +user. +:SUBMETHOD is the additional segments of the method. +:KEYWORDS are the api parameters. +: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\" +:SITE is the api parameter specifying the site. Return the entire response as a complex alist." - (sx-request-make - method - (cons (cons 'filter - (sx-filter-get-var - (cond (filter filter) - ((boundp 'stack-filter) stack-filter)))) - keyword-arguments) - need-auth - use-post)) + (let ((access-token (sx-cache-get 'auth)) + (method-auth (sx-auth--method-p method submethod)) + (filter-auth (sx-auth--filter-p filter)) + (full-method (concat (format "%s" method) + (when id + (format "/%s" id)) + (when submethod + (format "/%s" submethod)))) + (call 'sx-request-make)) + (lwarn "sx-call-method" :warning "A: %S T: %S. M: %S,%s. F: %S" (equal 'warn auth) + access-token method-auth full-method filter-auth) + (unless access-token + (cond + ;; 1. Need auth and warn user (interactive use) + ((and method-auth (equal 'warn auth)) + (user-error + "This request requires authentication. Please run `M-x sx-auth-authenticate' and try again.")) + ;; 2. Need auth to populate UI, cannot provide subset + ((and method-auth auth) + (setq call 'sx-request-fallback)) + ;; 3. Need auth for type. Use auth-less filter. + ((and filter-auth auth) + (setq filter filter-auth)) + ;; 4. Requires auth but no value set for auth + ((and (or filter-auth method-auth) (not auth)) + (error "This request requires authentication.")))) + ;; Concatenate all parameters now that filter is ensured. + (setq parameters + (cons `(site . ,site) + (cons (cons 'filter + (sx-filter-get-var filter)) + keywords))) + (funcall call + full-method + parameters + url-method))) (provide 'sx-method) ;;; sx-method.el ends here -- cgit v1.2.3 From 6b94d5f8c2c5b187a458c1256d9b496a0378770f Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Thu, 20 Nov 2014 13:17:48 -0500 Subject: Remove default site specification. Defaults to nil if not specified (for network-wide searches). Remove :warning, drop back to :debug --- sx-method.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'sx-method.el') diff --git a/sx-method.el b/sx-method.el index feebd84..5499fb1 100644 --- a/sx-method.el +++ b/sx-method.el @@ -35,7 +35,7 @@ (filter 'none) auth (url-method "GET") - (site sx-site-default)) + site) "Call METHOD with additional keys. :ID is the id associated with a question, answer, comment, post or @@ -58,7 +58,7 @@ Return the entire response as a complex alist." (when submethod (format "/%s" submethod)))) (call 'sx-request-make)) - (lwarn "sx-call-method" :warning "A: %S T: %S. M: %S,%s. F: %S" (equal 'warn auth) + (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 (cond -- cgit v1.2.3 From 91069a3b2fe9b800436c603f94186c2e34a212ac Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Fri, 21 Nov 2014 08:45:54 -0500 Subject: Update docstring to reflect uses of `AUTH`. --- sx-method.el | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'sx-method.el') diff --git a/sx-method.el b/sx-method.el index c5764cd..ee1f0b2 100644 --- a/sx-method.el +++ b/sx-method.el @@ -51,6 +51,21 @@ authentication. :URL-METHOD is either \"POST\" or \"GET\" :SITE is the api parameter specifying the site. +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. + +When AUTH Is t, filters will automatically use a non-auth subset if +no `access_token' is available. Methods requiring auth will instead +use `sx-request-fallback' rather than have a failed API response. +This is meant to allow for UI pages where portions may require AUTH +but could still be used without authenticating (i.e a launch/home page). + +When AUTH is 'warn, methods will signal a `user-error'. This is meant +for interactive commands that absolutely require authentication +(submitting questions/answers, reading inbox, etc). Filters will +treat 'warn as equivalent to t. + Return the entire response as a complex alist." (let ((access-token (sx-cache-get 'auth)) (method-auth (sx-auth--method-p method submethod)) -- cgit v1.2.3 From 3c772002f24db89410b14d7a938ff12b23ac5580 Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Fri, 21 Nov 2014 10:29:27 -0500 Subject: Whitespace/indent fixup. --- sx-method.el | 76 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 38 insertions(+), 38 deletions(-) (limited to 'sx-method.el') diff --git a/sx-method.el b/sx-method.el index ee1f0b2..a1a2901 100644 --- a/sx-method.el +++ b/sx-method.el @@ -39,7 +39,7 @@ auth (url-method "GET") site) - "Call METHOD with additional keys. + "Call METHOD with additional keys. :ID is the id associated with a question, answer, comment, post or user. @@ -63,46 +63,46 @@ but could still be used without authenticating (i.e a launch/home page). When AUTH is 'warn, methods will signal a `user-error'. This is meant for interactive commands that absolutely require authentication -(submitting questions/answers, reading inbox, etc). Filters will + (submitting questions/answers, reading inbox, etc). Filters will treat 'warn as equivalent to t. Return the entire response as a complex alist." - (let ((access-token (sx-cache-get 'auth)) - (method-auth (sx-auth--method-p method submethod)) - (filter-auth (sx-auth--filter-p filter)) - (full-method (concat (format "%s" method) - (when id - (format "/%s" id)) - (when submethod - (format "/%s" submethod)))) - (call 'sx-request-make)) - (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 - (cond - ;; 1. Need auth and warn user (interactive use) - ((and method-auth (equal 'warn auth)) - (user-error - "This request requires authentication. Please run `M-x sx-auth-authenticate' and try again.")) - ;; 2. Need auth to populate UI, cannot provide subset - ((and method-auth auth) - (setq call 'sx-request-fallback)) - ;; 3. Need auth for type. Use auth-less filter. - ((and filter-auth auth) - (setq filter filter-auth)) - ;; 4. Requires auth but no value set for auth - ((and (or filter-auth method-auth) (not auth)) - (error "This request requires authentication.")))) - ;; Concatenate all parameters now that filter is ensured. - (setq parameters - (cons `(site . ,site) - (cons (cons 'filter - (sx-filter-get-var filter)) - keywords))) - (funcall call - full-method - parameters - url-method))) + (let ((access-token (sx-cache-get 'auth)) + (method-auth (sx-auth--method-p method submethod)) + (filter-auth (sx-auth--filter-p filter)) + (full-method (concat (format "%s" method) + (when id + (format "/%s" id)) + (when submethod + (format "/%s" submethod)))) + (call 'sx-request-make)) + (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 + (cond + ;; 1. Need auth and warn user (interactive use) + ((and method-auth (equal 'warn auth)) + (user-error + "This request requires authentication. Please run `M-x sx-auth-authenticate' and try again.")) + ;; 2. Need auth to populate UI, cannot provide subset + ((and method-auth auth) + (setq call 'sx-request-fallback)) + ;; 3. Need auth for type. Use auth-less filter. + ((and filter-auth auth) + (setq filter filter-auth)) + ;; 4. Requires auth but no value set for auth + ((and (or filter-auth method-auth) (not auth)) + (error "This request requires authentication.")))) + ;; Concatenate all parameters now that filter is ensured. + (setq parameters + (cons `(site . ,site) + (cons (cons 'filter + (sx-filter-get-var filter)) + keywords))) + (funcall call + full-method + parameters + url-method))) (provide 'sx-method) ;;; sx-method.el ends here -- cgit v1.2.3 From a62483526c336219424c9f7d03abbe2ba6516c53 Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Fri, 21 Nov 2014 10:29:49 -0500 Subject: Use `(declare (indent 1)) in `sx-method-call`. --- sx-method.el | 1 + 1 file changed, 1 insertion(+) (limited to 'sx-method.el') diff --git a/sx-method.el b/sx-method.el index a1a2901..ed32b3d 100644 --- a/sx-method.el +++ b/sx-method.el @@ -67,6 +67,7 @@ for interactive commands that absolutely require authentication treat 'warn as equivalent to t. Return the entire response as a complex alist." + (declare (indent 1)) (let ((access-token (sx-cache-get 'auth)) (method-auth (sx-auth--method-p method submethod)) (filter-auth (sx-auth--filter-p filter)) -- cgit v1.2.3 From dc17d78972536c746be0c1ed66989a1509f5ad70 Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Fri, 21 Nov 2014 10:31:49 -0500 Subject: Use `\(` for ( at beginning of line in docstring. --- sx-method.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sx-method.el') diff --git a/sx-method.el b/sx-method.el index ed32b3d..3eea5d7 100644 --- a/sx-method.el +++ b/sx-method.el @@ -63,7 +63,7 @@ but could still be used without authenticating (i.e a launch/home page). When AUTH is 'warn, methods will signal a `user-error'. This is meant for interactive commands that absolutely require authentication - (submitting questions/answers, reading inbox, etc). Filters will +\(submitting questions/answers, reading inbox, etc). Filters will treat 'warn as equivalent to t. Return the entire response as a complex alist." -- cgit v1.2.3 From 06cd4215de8d2e8680de74546984d9bdbc47b1d3 Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Fri, 21 Nov 2014 12:11:11 -0500 Subject: Whitespace fixup (spaces not tabs). --- sx-method.el | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'sx-method.el') diff --git a/sx-method.el b/sx-method.el index 3eea5d7..04d973d 100644 --- a/sx-method.el +++ b/sx-method.el @@ -69,41 +69,41 @@ treat 'warn as equivalent to t. Return the entire response as a complex alist." (declare (indent 1)) (let ((access-token (sx-cache-get 'auth)) - (method-auth (sx-auth--method-p method submethod)) - (filter-auth (sx-auth--filter-p filter)) - (full-method (concat (format "%s" method) - (when id - (format "/%s" id)) - (when submethod - (format "/%s" submethod)))) - (call 'sx-request-make)) + (method-auth (sx-auth--method-p method submethod)) + (filter-auth (sx-auth--filter-p filter)) + (full-method (concat (format "%s" method) + (when id + (format "/%s" id)) + (when submethod + (format "/%s" submethod)))) + (call 'sx-request-make)) (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) + access-token method-auth full-method filter-auth) (unless access-token (cond ;; 1. Need auth and warn user (interactive use) ((and method-auth (equal 'warn auth)) - (user-error - "This request requires authentication. Please run `M-x sx-auth-authenticate' and try again.")) + (user-error + "This request requires authentication. Please run `M-x sx-auth-authenticate' and try again.")) ;; 2. Need auth to populate UI, cannot provide subset ((and method-auth auth) - (setq call 'sx-request-fallback)) + (setq call 'sx-request-fallback)) ;; 3. Need auth for type. Use auth-less filter. ((and filter-auth auth) - (setq filter filter-auth)) + (setq filter filter-auth)) ;; 4. Requires auth but no value set for auth ((and (or filter-auth method-auth) (not auth)) - (error "This request requires authentication.")))) + (error "This request requires authentication.")))) ;; Concatenate all parameters now that filter is ensured. (setq parameters - (cons `(site . ,site) - (cons (cons 'filter - (sx-filter-get-var filter)) - keywords))) + (cons `(site . ,site) + (cons (cons 'filter + (sx-filter-get-var filter)) + keywords))) (funcall call - full-method - parameters - url-method))) + full-method + parameters + url-method))) (provide 'sx-method) ;;; sx-method.el ends here -- cgit v1.2.3