forked from tkf/emacs-request
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request-deferred.el
69 lines (51 loc) · 2.33 KB
/
request-deferred.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
;;; request-deferred.el --- Wrap request.el by deferred -*- lexical-binding: t; -*-
;; Copyright (C) 2012 Takafumi Arakaki
;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
;; URL: https://github.com/tkf/emacs-request
;; Package-Requires: ((emacs "24.1") (deferred "0.3.1") (request "0.3"))
;; Version: 0.2.0
;; This file is NOT part of GNU Emacs.
;; request-deferred.el is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; request-deferred.el is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with request-deferred.el.
;; If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Trivial wrapper to request library returing kiwanami deferred object.
;;; Code:
(require 'request)
(require 'deferred)
(defun request-deferred (url &rest args)
"Return deferred object associated requesting URL with ARGS.
Following deferred callback takes a response object regardless of
the response result. To make sure no error occurs during the
request, check `request-response-error-thrown'.
Arguments are the same as `request', but COMPLETE callback cannot
be used as it is used for starting deferred callback chain.
Example::
(require 'request-deferred)
(deferred:$
(request-deferred \"https://httpbin.org/get\" :parser 'json-read)
(deferred:nextc it
(lambda (response)
(message \"Got: %S\" (request-response-data response)))))"
(let* ((d (deferred:new #'identity))
(callback-post (apply-partially
(lambda (d &rest args)
(deferred:callback-post
d (plist-get args :response)))
d)))
;; As `deferred:errorback-post' requires an error object to be
;; posted, use `deferred:callback-post' for success and error
;; cases.
(setq args (plist-put args :complete callback-post))
(apply #'request url args)
d))
(provide 'request-deferred)
;;; request-deferred.el ends here