blob: 100d98145668d7eab69866fb302aa650489c82e9 (
plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
;;;; lazybones-client.lisp -- macro to generate a set of http request functions given an APP instance
;; Copyright (C) 2022 Colin Okay
;; This program 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.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
(defpackage #:lazybones/client.dexador
(:use #:cl)
(:local-nicknames (#:a #:alexandria-2))
(:export #:generate))
(in-package :lazybones/client.dexador)
(defun endpoint-defun-name (ep)
"Returns the string name of a defun for making requests to
endpoint EP."
(with-output-to-string (*standard-output*)
(princ (string-downcase (symbol-name (lazybones::endpoint-method ep))))
(princ "-")
(loop for (term . more) on (lazybones::endpoint-dispatch-pattern ep)
when (and (stringp term) (plusp (length term)))
do (princ (string-downcase term))
when (listp term)
do (princ (string-downcase (car term)))
when (and more (plusp (length term)))
do (princ "/"))))
(defun endpoint-defun-route-var-names (ep)
"Returns a list of strings representing the names of route variables
extracted from endpoint EP, to be used as variable names in the defun
for making requests to that endpoint."
(lazybones::endpoint-route-vars ep))
(defun endpoint-defun-query-var-names (ep)
"Returns a list of strings representing the names of query parameter
variables extraced from the endpoint EP, to be used as variable names
in the defun for making request to that endpoint."
(mapcar (a:compose #'symbol-name #'first)
(lazybones::endpoint-params ep)))
(defun endpoint-accepts-body-p (ep)
(member (lazybones::endpoint-method ep) '(:post :put :patch)) )
(defun endpoint-defun-lambda-list (ep)
"Returns a string representation of the lambda list of the defun
for making requests to endpoint EP."
(format
nil
"(~{~a ~} %host &key ~:[~;%content-type %body ~] %headers %cookies)"
(append
(endpoint-defun-route-var-names ep)
(endpoint-defun-query-var-names ep))
(endpoint-accepts-body-p ep)))
(defun endpoint-defun-dexador-uri-route-format-string (ep)
"Returns a string representing a format string, intended to be
embedded into the body of a defun for making requests to the
endpoint Ep. It is designed to be passed to FORMAT, where route
variables are substituted into the string."
(str:join "/"
(mapcar (lambda (x) (if (listp x) "~a" x))
(lazybones::endpoint-dispatch-pattern ep))))
(defun endpoint-defun-dexador-uri-route-query-format-string (ep)
"Returns a string representing a format string, intended to be
embedded into the body of a defun for making requests to the
endpoint EP. It is desienged to be passed to FORMAT, where query
paramters are substituted into the string, if they exist."
(with-output-to-string (*standard-output*)
(loop
for first = t then nil
for varname in (endpoint-defun-query-var-names ep)
do
(princ "~@[")
(unless first (princ #\&))
(princ (string-upcase varname))
(princ "=~a~]"))))
(defun endpoint-defun-dexador-request-uri (app ep)
"Returns a string representation of code that generates a URI for
passing to the dexador request function within the body of the defun
for making requests to the endpoint EP of the application APP."
(concatenate
'string
"(format nil "
"\""
"~a"
(lazybones::app-prefix app)
(endpoint-defun-dexador-uri-route-format-string ep)
"?"
(endpoint-defun-dexador-uri-route-query-format-string ep)
"\" "
"%host "
(str:join " " (endpoint-defun-route-var-names ep))
" "
(str:join " " (endpoint-defun-query-var-names ep))
")"))
(defun endpoint-defun-body (app ep)
"Returns a string representation of the function body of a defun
for making requests to the endpoint EP in the app APP."
(format
nil
" (dexador:~a~% ~a~%~{ ~a~^~%~})"
(string-downcase (symbol-name (lazybones::endpoint-method ep)))
(endpoint-defun-dexador-request-uri app ep)
(append
(if (endpoint-accepts-body-p ep)
(list ":content %body"
":cookie-jar %cookies"
":headers (if %content-type (cons (cons \"Content-Type\" %content-type) %headers) %headers)")
(list ":cookie-jar %cookies"
":headers %headers")))))
(defun generate-defun-for-endpoint (app ep)
"Returns a string representation of a defun form for a function that
makes a request to the endpoint EP."
(format nil
"(defun ~a~% ~a~% ~s~%~a)"
(endpoint-defun-name ep)
(endpoint-defun-lambda-list ep)
(lazybones::endpoint-documentation ep)
(endpoint-defun-body app ep)))
(defun all-function-names (app)
(mapcar 'endpoint-defun-name (lazybones::app-endpoints app)))
(defun app-client-package-name (app)
(format nil "~a-CLIENT" (lazybones::app-name app)))
(defun generate-defsystem-for-client-of-app (app)
(with-output-to-string (*standard-output*)
(princ "(asdf:defsystem #:") (princ (app-client-package-name app))
(terpri)
(princ " :depends-on (#:dexador)")
(terpri)
(princ " :components ((:file ")
(princ #\")
(princ (string-downcase (app-client-package-name app)))
(princ #\")
(princ ")))")))
(defun generate-defpackage-for-client-of-app (app)
(with-output-to-string (out)
(format
out
"
;;;; DO NOT EDIT! THIS FILE HAS BEEN GENERATED BY LAZYBONES-CLIENT
(defpackage #:~a
(:use :cl)
(:export ~%~{ #:~a~^~%~}))"
(app-client-package-name app)
(all-function-names app))
(terpri out)
(format out "(in-package :~a)" (app-client-package-name app))
(terpri out)))
(defun client-asd-file-name (app)
(format nil "~a.asd" (string-downcase (app-client-package-name app))))
(defun client-lisp-file-name (app)
(format nil "~a.lisp" (string-downcase (app-client-package-name app))))
(defun generate-client-functions-for-app (app)
(loop for ep in (lazybones::app-endpoints app)
collect (generate-defun-for-endpoint app ep)))
(defun generate (directory app)
"Generate "
(assert (uiop:directory-exists-p directory))
(alexandria:write-string-into-file
(generate-defsystem-for-client-of-app app)
(merge-pathnames (client-asd-file-name app) directory))
(alexandria:write-string-into-file
(with-output-to-string (*standard-output*)
(princ (generate-defpackage-for-client-of-app app))
(princ #\newline) (princ #\newline)
(princ #\newline) (princ #\newline)
(dolist (defun-string (generate-client-functions-for-app app))
(princ defun-string)
(princ #\newline) (princ #\newline)))
(merge-pathnames (client-lisp-file-name app) directory))
:ok)
|