diff options
author | Jonathan Leech-Pepin <jonathan.leechpepin@gmail.com> | 2014-11-20 13:06:01 -0500 |
---|---|---|
committer | Jonathan Leech-Pepin <jonathan.leechpepin@gmail.com> | 2014-11-20 13:06:01 -0500 |
commit | 534b1ab1f70dbc9029cd76d4ac627c1792e69ed9 (patch) | |
tree | 2b3fa09ff1057282dbb1efae6bbd7286a89c96df /sx-method.el | |
parent | 2e2619d7b1ec9a5d7338c5ec224ac0ba920e017c (diff) |
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`.
Diffstat (limited to 'sx-method.el')
-rw-r--r-- | sx-method.el | 74 |
1 files changed, 54 insertions, 20 deletions
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 |