dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

request-deferred.el (2307B)


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