diff options
author | Jonathan Leech-Pepin <jonathan.leechpepin@gmail.com> | 2014-11-14 15:33:59 -0500 |
---|---|---|
committer | Jonathan Leech-Pepin <jonathan.leechpepin@gmail.com> | 2014-11-14 15:33:59 -0500 |
commit | a1a5ccb76329d835adafdf117883199c92de44cb (patch) | |
tree | c57977750aee4b7942479f563f43e40a0ca37fa7 /sx-request.el | |
parent | 2d8c61963146ee8fc32684c47b3a3761320e671a (diff) |
Updated `sx-request-make` to use a consistent format for both `POST` and `GET` methods.
(sx-request-make): Added optional arguments for using "POST" and AUTH
when performing requests
(sx-request--build-keyword-arguments): Add option of using AUTH and
including in query when required.
(sx-request-build): Removed
(sx-request--request): New function to perform query with all
variables let bound.
sx-auth.el: Updated `sx-auth-root` to be full auth URL rather than
lack method.
(sx-auth-authenticate): Remove dependency on `sx-request-build` and
perform construction inline.
Diffstat (limited to 'sx-request.el')
-rw-r--r-- | sx-request.el | 44 |
1 files changed, 24 insertions, 20 deletions
diff --git a/sx-request.el b/sx-request.el index 6dc54e7..da8e71e 100644 --- a/sx-request.el +++ b/sx-request.el @@ -71,19 +71,19 @@ number of requests left every time it finishes a call.") ;;; Making Requests (defun sx-request-make - (method &optional args silent) + (method &optional args use-auth use-post silent) (let ((url-automatic-caching sx-request-cache-p) (url-inhibit-uncompression t) (silent (or silent sx-request-silent-p)) - (call (sx-request-build - method - (cons (cons 'key sx-request-api-key) - args)))) + (request-method (if use-post "POST" "GET")) + (request-args + (sx-request--build-keyword-arguments args nil use-auth)) + (request-url (concat sx-request-api-root method))) (unless silent (sx-message "Request: %S" call)) - (let ((response-buffer (cond - ((equal '(24 . 4) (cons emacs-major-version emacs-minor-version)) - (url-retrieve-synchronously call silent)) - (t (url-retrieve-synchronously call))))) + (let ((response-buffer (sx-request--request request-url + request-args + request-method + silent))) (if (not response-buffer) (error "Something went wrong in `url-retrieve-synchronously'") (with-current-buffer response-buffer @@ -120,22 +120,26 @@ number of requests left every time it finishes a call.") ;;; Support Functions -(defun sx-request-build (method keyword-arguments &optional kv-value-sep root) - "Build the request string that will be used to process REQUEST -with the given KEYWORD-ARGUMENTS." - (let ((base (concat (or root sx-request-api-root) method)) - (args (sx-request--build-keyword-arguments - keyword-arguments kv-value-sep))) - (if (string-equal "" args) - base - (concat base "?" args)))) - -(defun sx-request--build-keyword-arguments (alist &optional kv-value-sep) +(defun sx-request--request (url args method silent) + (let ((url-request-method method) + (url-request-extra-headers + '(("Content-Type" . "application/x-www-form-urlencoded"))) + (url-request-data args)) + (cond + ((equal '(24 . 4) (cons emacs-major-version emacs-minor-version)) + (url-retrieve-synchronously url silent)) + (t (url-retrieve-synchronously url))))) + +(defun sx-request--build-keyword-arguments (alist &optional + kv-value-sep use-auth) "Build a \"key=value&key=value&...\"-style string with the elements of ALIST. If any value in the alist is `nil', that pair will not be included in the return. If you wish to pass a notion of false, use the symbol `false'. Each element is processed with `sx--thing-as-string'." + (when use-auth + (add-to-list 'alist (cons "key" sx-request-api-key)) + (add-to-list 'alist (car (sx-cache-get 'auth)))) (mapconcat (lambda (pair) (concat |