forked from sigma/gh.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gh-api.el
139 lines (113 loc) · 4.53 KB
/
gh-api.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
;;; gh-api.el --- api definition for gh.el
;; Copyright (C) 2011 Yann Hodique
;; Author: Yann Hodique <yann.hodique@gmail.com>
;; Keywords:
;; This file 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 2, or (at your option)
;; any later version.
;; This file 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 GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;;
;;; Code:
(require 'cl)
(require 'json)
(require 'gh-auth)
(defgroup gh-api nil
"Github API."
:group 'gh)
;;;###autoload
(defclass gh-api ()
((sync :initarg :sync :initform t)
(base :initarg :base :type string)
(auth :initarg :auth))
"Github API")
(defmethod gh-api-expand-resource ((api gh-api)
resource)
resource)
(defmethod gh-api-get-username ((api gh-api))
(oref (oref api :auth) :username))
;;;###autoload
(defclass gh-api-v3 (gh-api)
((base :initarg :base :initform "https://api.github.com")
(auth :initarg :auth))
"Github API v3")
(defcustom gh-api-v3-authenticator 'gh-oauth-authenticator
"Authenticator for Github API v3"
:type '(choice (const :tag "Password" gh-password-authenticator)
(const :tag "OAuth" gh-oauth-authenticator))
:group 'gh-api)
(defmethod constructor :static ((api gh-api-v3) newname &rest args)
(let ((obj (call-next-method)))
(oset obj :auth (funcall gh-api-v3-authenticator "auth"))
obj))
(defclass gh-api-request ()
((method :initarg :method :type string)
(url :initarg :url :type string)
(headers :initarg :headers)
(data :initarg :data :initform "" :type string)))
(defclass gh-api-response ()
((data :initarg :data :initform nil)
(callbacks :initarg :callbacks :initform nil))
"Class for API responses")
(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))
(defmethod gh-api-response-init ((resp gh-api-response)
buffer &optional transform)
(declare (special url-http-end-of-headers))
(with-current-buffer buffer
(goto-char (1+ url-http-end-of-headers))
(oset resp :data (let ((raw (buffer-substring (point) (point-max))))
(if transform
(funcall transform (gh-api-json-decode raw))
raw))))
(kill-buffer buffer)
(gh-api-response-run-callbacks resp)
resp)
(defun gh-api-set-response (status resp transform)
(gh-api-response-init resp (current-buffer) transform))
(defmethod gh-api-response-run-callbacks ((resp gh-api-response))
(let ((data (oref resp :data)))
(when data
(dolist (cb (copy-list (oref resp :callbacks)))
(funcall cb data)
(object-remove-from-list resp :callbacks cb)))))
(defmethod gh-api-add-response-callback ((resp gh-api-response) callback)
(object-add-to-list resp :callbacks callback t)
(gh-api-response-run-callbacks resp))
(defmethod gh-api-authenticated-request
((api gh-api) transformer method resource &optional data)
(let ((req (gh-auth-modify-request (oref api :auth)
(gh-api-request "request"
:method method
:url (concat (oref api :base)
(gh-api-expand-resource api resource))
:headers nil
:data (or (gh-api-json-encode data) "")))))
(let ((url-request-method (oref req :method))
(url-request-data (oref req :data))
(url-request-extra-headers (oref req :headers))
(url (oref req :url)))
(if (oref api :sync)
(let ((resp (gh-api-response "sync")))
(gh-api-response-init resp
(url-retrieve-synchronously url)
transformer)
resp)
(let ((resp (gh-api-response "async")))
(url-retrieve url 'gh-api-set-response (list resp transformer))
resp)))))
(provide 'gh-api)
;;; gh-api.el ends here