-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathapi.lisp
166 lines (141 loc) · 6.54 KB
/
api.lisp
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
(in-package #:org.shirakumo.radiance.core)
;;; Formats
(defvar *api-formats* (make-hash-table :test 'equalp))
(defvar *default-api-format*)
(defun api-format (name)
(gethash (string name) *api-formats*))
(defun (setf api-format) (parse-func name)
(setf (gethash (string name) *api-formats*) parse-func))
(defun remove-api-format (name)
(remhash (string name) *api-formats*))
(defun list-api-formats ()
(loop for name being the hash-keys of *api-formats* collect name))
(defmacro define-api-format (name (argsvar) &body body)
`(setf (api-format ,(string name))
(lambda (,argsvar)
(block ,name
,@body))))
(defun api-output (data &rest metadata &key (status 200) (message "Ok.") (format (post/get "data-format")) &allow-other-keys)
(let ((format (or format *default-api-format*)))
(funcall (or (api-format format)
(error 'api-unknown-format :format format))
(let ((table (make-hash-table :test 'equal)))
(setf (gethash "status" table) status
(gethash "message" table) message
(gethash "data" table) data)
(loop for (key val) on metadata by #'cddr
do (unless (or (eql key :status) (eql key :message) (eql key :format))
(setf (gethash (string-downcase key) table) val)))
table))))
(defgeneric api-serialize (object))
(defmethod no-applicable-method ((func (eql (symbol-function 'api-serialize))) &rest args)
(declare (ignore func))
(error 'api-unserializable-object :object (first args)))
;;; Pages
(defvar *api-endpoints* (make-hash-table :test 'equalp))
(defun api-endpoint (path)
(gethash (string path) *api-endpoints*))
(defun (setf api-endpoint) (page path)
(setf (gethash (string path) *api-endpoints*) page))
(defun remove-api-endpoint (path)
(remhash (string path) *api-endpoints*))
(defun list-api-endpoints ()
(loop for page being the hash-values of *api-endpoints* collect page))
(defun ensure-api-endpoint (thing)
(etypecase thing
(string (or (api-endpoint thing)
(error "No such api-endpoint ~s." thing)))
(symbol (api-endpoint (string thing)))
(api-endpoint thing)))
(define-documentable api-endpoint ()
((name :initarg :name :accessor name)
(handler :initarg :handler :accessor handler)
(argslist :initarg :argslist :accessor argslist)
(request-handler :initarg :request-handler :accessor request-handler))
(:default-initargs
:name (error "NAME required")
:handler (error "HANDLER function required")
:argslist ()
:request-handler ())
(:find-function api-endpoint))
(defun make-request-handler-function (function &optional (arglist (arg:arglist function)))
(let ((request (gensym "REQUEST")))
(flet ((parse (arg optional)
(cond ((not optional)
`(,arg (or* (post/get ,(string arg) ,request) (error 'api-argument-missing :argument ',arg))))
((listp arg)
`(,(first arg) (or* (post/get ,(string (first arg)) ,request) ,(second arg))))
(T
`(,arg (or* (post/get ,(string arg) ,request)))))))
`(lambda (,request)
(declare (ignorable ,request))
(let* ,(loop with in-optional = NIL
for arg in arglist
do (when (eql arg '&optional) (setf in-optional T))
unless (eql arg '&optional)
collect (parse arg in-optional))
(funcall ,function ,@(extract-lambda-vars arglist)))))))
(defmethod initialize-instance :after ((page api-endpoint) &key)
(unless (slot-boundp page 'argslist)
(setf (slot-value page 'argslist)
(arg:arglist (handler page))))
(unless (request-handler page)
(setf (slot-value page 'request-handler)
(ecompile NIL (make-request-handler-function (handler page) (argslist page))))))
(defmethod print-object ((api api-endpoint) stream)
(print-unreadable-object (api stream :type T)
(format stream "~a ~s" (name api) (argslist api))))
(defun call-api-request (api-endpoint &optional (request *request*))
(let ((*request* request))
(funcall (request-handler (ensure-api-endpoint api-endpoint))
request)))
(defun call-api (api-endpoint &rest arguments)
(apply (handler (ensure-api-endpoint api-endpoint))
arguments))
(defmacro define-api (name args options &body body)
(let ((handler (gensym "HANDLER")))
(multiple-value-bind (body forms) (expand-options 'api options name body args)
`(eval-when (:compile-toplevel :load-toplevel :execute)
,@forms
,@(when (module)
`((pushnew ,(string name) (module-storage ,(module) 'radiance-apis) :test #'equalp)))
(flet ((,handler ,args
(block ,(when (symbolp name) name)
,@body)))
(setf (api-endpoint ,(string name))
(make-instance
'api-endpoint
:name ,(string name)
:argslist ',args
:handler #',handler
:request-handler ,(make-request-handler-function `#',handler args)
:documentation ,(form-fiddle:lambda-docstring `(lambda () ,@body)))))))))
(define-delete-hook (module 'radiance-destroy-apis)
(dolist (page (module-storage module 'radiance-apis))
(remove-api-endpoint page)))
(defmacro api-error (format-string &rest format-args)
`(error 'api-error :message (format NIL ,format-string ,@format-args)))
;;; Actual page handler
(define-uri-dispatcher api ("/^api/.*" 100)
(let* ((path (path (uri *request*)))
(slashpos (position #\/ path))
(subpath (subseq path (1+ slashpos)))
(api-endpoint (or (api-endpoint subpath) (api-endpoint ""))))
(flet ((output-error (err code)
(let ((message (or (message err) (princ-to-string err))))
(if (and (string= (post/get "browser") "true") (referer))
(redirect (merge-url (referer) :parameters `(("error" . ,(princ-to-string message)))))
(api-output err :status code :message message)))))
(handler-case
(restart-case
(handler-bind ((api-error #'maybe-invoke-debugger))
(call-api-request api-endpoint *request*))
(api-error (err)
:report "Treat as API error."
(output-error err 500)))
(api-error (err)
(output-error err 500))
(request-not-found (err)
(output-error err 403))
(request-denied (err)
(output-error err 404))))))