Skip to content
Browse files

Revert "make use of library request.el"

This reverts commit a9457ae.

There are some more kinks to be worked out for proper
integration with request.el.

Fixes #25
  • Loading branch information...
1 parent a9457ae commit 08d830ad212dbf9fc45dcf2720f4143339f55066 Mitchel Humpherys committed Aug 7, 2013
Showing with 97 additions and 61 deletions.
  1. +1 −1 .travis.yml
  2. +27 −3 gh-api.el
  3. +1 −1 gh-pkg.el
  4. +68 −56 gh-url.el
View
2 .travis.yml
@@ -20,7 +20,7 @@ before_install:
fi
- pip install --use-mirrors virtualenv-emacs
- virtualenv_install_emacs --with-emacs=`which "$EMACS"` --marmalade
- - elpa-get eieio pcache logito mocker request
+ - elpa-get eieio pcache logito mocker
- emacs --version
script:
make test
View
30 gh-api.el
@@ -123,19 +123,43 @@
(defclass gh-api-response (gh-url-response)
())
+(defun gh-api-json-decode (repr)
+ (if (or (null repr) (string= repr ""))
+ 'empty
+ (let ((json-array-type 'list))
+ (json-read-from-string repr))))
+
(defun gh-api-json-encode (json)
(json-encode-list json))
-(defun gh-url-form-encode (form)
- (mapconcat (lambda (x) (format "%s=%s" (car x) (cdr x)))
- form "&"))
+(defmethod gh-url-response-set-data ((resp gh-api-response) data)
+ (call-next-method resp (gh-api-json-decode data)))
(defclass gh-api-paged-request (gh-api-request)
((default-response-cls :allocation :class :initform gh-api-paged-response)))
(defclass gh-api-paged-response (gh-api-response)
())
+(defmethod gh-api-paging-links ((resp gh-api-paged-response))
+ (let ((links-header (cdr (assoc "Link" (oref resp :headers)))))
+ (when links-header
+ (loop for item in (split-string links-header ", ")
+ when (string-match "^<\\(.*\\)>; rel=\"\\(.*\\)\"" item)
+ collect (cons (match-string 2 item)
+ (match-string 1 item))))))
+
+(defmethod gh-url-response-set-data ((resp gh-api-paged-response) data)
+ (let ((previous-data (oref resp :data))
+ (next (cdr (assoc "next" (gh-api-paging-links resp)))))
+ (call-next-method)
+ (oset resp :data (append previous-data (oref resp :data)))
+ (when next
+ (let ((req (oref resp :-req)))
+ (oset resp :data-received nil)
+ (oset req :url next)
+ (gh-url-run-request req resp)))))
+
(defmethod gh-api-authenticated-request
((api gh-api) transformer method resource &optional data params)
(let* ((fmt (oref api :data-format))
View
2 gh-pkg.el
@@ -1,2 +1,2 @@
(define-package "gh" "%VERSION%" "A GitHub library for Emacs"
- '((eieio "1.3") (pcache "0.2.3") (logito "0.1") (request "0.2.0")))
+ '((eieio "1.3") (pcache "0.2.3") (logito "0.1")))
View
124 gh-url.el
@@ -32,7 +32,7 @@
;;;###autoload
(require 'eieio)
-(require 'request)
+(require 'url-http)
(defclass gh-url-request ()
((method :initarg :method :type string)
@@ -54,6 +54,14 @@
(transform :initarg :transform :initform nil)
(-req :initarg :-req :initform nil)))
+(defmethod gh-url-response-set-data ((resp gh-url-response) data)
+ (let ((transform (oref resp :transform)))
+ (oset resp :data
+ (if transform
+ (funcall transform data)
+ data))
+ (oset resp :data-received t)))
+
(defmethod gh-url-response-run-callbacks ((resp gh-url-response))
(let ((copy-list (lambda (list)
(if (consp list)
@@ -72,7 +80,7 @@
(defmethod gh-url-add-response-callback ((resp gh-url-response) callback)
(object-add-to-list resp :callbacks callback t)
(if (oref resp :data-received)
- (gh-url-response-run-callbacks resp)
+ (gh-url-response-run-callbacks resp)
resp))
;;; code borrowed from nicferrier's web.el
@@ -98,63 +106,67 @@
(push (cons name value) headers)))
headers))
-(defun gh-url-next-page (headers)
- (let ((links-header (cdr (assoc "Link" headers))))
- (when links-header
- (loop for item in (split-string links-header ", ")
- when (string-match "^<\\(.*\\)>; rel=\"next\"" item)
- return (match-string 1 item)))))
-
-(defmethod gh-url-run-request ((req gh-url-request) &optional resp)
- (unless resp
- (setq resp (make-instance (oref req default-response-cls))))
- (oset resp :-req req)
- (request (oref req :url)
- :sync (not (oref req :async))
- :type (oref req :method)
- :headers (oref req :headers)
- :params (oref req :query)
- :data (oref req :data)
- :parser (apply-partially 'gh-request-parse-response resp)
- :success (apply-partially 'gh-request-handle-success resp)
- :error (apply-partially 'gh-request-handle-error resp))
+(defmethod gh-url-response-finalize ((resp gh-url-response))
+ (when (oref resp :data-received)
+ (gh-url-response-run-callbacks resp)))
+
+(defmethod gh-url-response-init ((resp gh-url-response)
+ buffer)
+ (declare (special url-http-end-of-headers))
+ (unwind-protect
+ (with-current-buffer buffer
+ (let ((headers (gh-url-parse-headers
+ (buffer-substring
+ (point-min) (1+ url-http-end-of-headers)))))
+ (oset resp :headers headers)
+ (oset resp :http-status (read (cdr (assoc 'status-code headers)))))
+ (goto-char (1+ url-http-end-of-headers))
+ (let ((raw (buffer-substring (point) (point-max))))
+ (gh-url-response-set-data resp raw)))
+ (kill-buffer buffer))
+ (gh-url-response-finalize resp)
resp)
-(defun gh-request-parse-response (resp)
- (let ((data (buffer-substring-no-properties (point-min) (point-max)))
- (transform (oref resp :transform)))
- (if transform
- (let ((json-array-type 'list))
- (funcall transform data))
- data)))
-
-(defun* gh-request-handle-success (resp &key response data
- &allow-other-keys)
- (oset resp :http-status (request-response-status-code response))
- (oset resp :headers (gh-url-parse-headers
- (request-response--raw-header response)))
- (let ((prev-data (oref resp :data))
- (req (oref resp :-req)))
- (oset resp :data (append prev-data data))
- (when (gh-api-paged-request-p req)
- (let ((next-link (gh-url-next-page (oref resp :headers))))
- (when next-link
- (oset req :url next-link)
- (gh-url-run-request req resp))))
- (unless prev-data
- (oset resp :data-received t)
- (gh-url-response-run-callbacks resp)))
- resp)
+(defun gh-url-set-response (status req-resp)
+ (set-buffer-multibyte t)
+ (destructuring-bind (req resp) req-resp
+ (condition-case err
+ (progn
+ (oset resp :-req req)
+ (gh-url-response-init resp (current-buffer)))
+ (error
+ (let ((num (oref req :num-retries)))
+ (if (or (null num) (zerop num))
+ (signal (car err) (cdr err))
+ (oset req :num-retries (1- num))
+ (gh-url-run-request req resp)))))))
+
+(defun gh-url-form-encode (form)
+ (mapconcat (lambda (x) (format "%s=%s" (car x) (cdr x)))
+ form "&"))
+
+(defun gh-url-params-encode (form)
+ (concat "?" (gh-url-form-encode form)))
-(defun* gh-request-handle-error (resp &key error-thrown
- &allow-other-keys)
- (let* ((req (oref resp :-req))
- (num (oref req :num-retries)))
- (if (or (null num) (zerop num))
- (signal (car error-thrown)
- (cdr error-thrown))
- (oset req :num-retries (1- num))
- (gh-url-run-request req resp))))
+(defmethod gh-url-run-request ((req gh-url-request) &optional resp)
+ (let ((url-request-method (oref req :method))
+ (url-request-data (oref req :data))
+ (url-request-extra-headers (oref req :headers))
+ (url (concat (oref req :url)
+ (let ((params (oref req :query)))
+ (if params
+ (gh-url-params-encode params)
+ "")))))
+ (if (oref req :async)
+ (let* ((resp (or resp (make-instance (oref req default-response-cls))))
+ (req-resp (list req resp)))
+ (url-retrieve url 'gh-url-set-response (list req-resp))
+ resp)
+ (let* ((resp (or resp (make-instance (oref req default-response-cls))))
+ (req-resp (list req resp)))
+ (with-current-buffer (url-retrieve-synchronously url)
+ (gh-url-set-response nil req-resp))
+ resp))))
(provide 'gh-url)
;;; gh-url.el ends here

0 comments on commit 08d830a

Please sign in to comment.
Something went wrong with that request. Please try again.