aboutsummaryrefslogtreecommitdiff
path: root/src/client/dexador
diff options
context:
space:
mode:
authorcolin <colin@cicadas.surf>2024-06-23 14:43:20 -0700
committercolin <colin@cicadas.surf>2024-06-23 14:43:20 -0700
commit30a034b30b66694447fc38172a2265a1095af222 (patch)
treed25eedc14ad0e313cc156eb71e2be9a34f971c44 /src/client/dexador
parentc4dd08d03d4440744a4061141f8cec54f1360a29 (diff)
Add: client generator for dexador
Diffstat (limited to 'src/client/dexador')
-rw-r--r--src/client/dexador/generate.lisp63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/client/dexador/generate.lisp b/src/client/dexador/generate.lisp
new file mode 100644
index 0000000..dd4652c
--- /dev/null
+++ b/src/client/dexador/generate.lisp
@@ -0,0 +1,63 @@
+(defpackage #:weekend.client.dexador
+ (:use #:cl)
+ (:import-from #:flatbind #:do>)
+ (:local-nicknames
+ (#:wknd #:weekend)
+ (#:a #:alexandria-2))
+ (:export #:generate))
+
+(in-package #:weekend.client.dexador)
+
+(defun generate-client-for (class)
+ (when (symbolp class) (setf class (find-class class)))
+
+ (let*
+ ((args
+ (mapcar (a:compose #'intern #'string)
+ (wknd:class-initargs class)))
+
+ (parts
+ (loop
+ :for part :in (wknd:route-builder-parts class)
+ :when (or (stringp part) (characterp part))
+ :collect part
+ :else
+ :collect (intern (string part))))
+ (method
+ (wknd:request-method class))
+
+ (dexador-fn
+ (ecase method
+ (:get 'dexador:get)
+ (:post 'dexador:post)
+ (:put 'dexador:put)
+ (:delete 'dexador:delete)
+ (:patch 'dexador:patch)
+ (:head 'dexador:head)))
+
+ (reqbody
+ (when (wknd:body-expected-p method)
+ `(mapcar #'cons
+ ',(mapcar #'string (wknd:class-initargs class))
+ (list ,@args))))
+
+ (host
+ (loop :with var := "HOST"
+ :while (find var args :test #'string-equal)
+ :do (setf var (concatenate 'string var "%"))
+ :finally (return (intern var))))
+ (dexador-kwargs
+ (loop :with var := "DEXADOR-KWARGS"
+ :while (find var args :test #'string-equal)
+ :do (setf var (concatenate 'string var "%"))
+ :finally (return (intern var)))))
+
+ `(defun ,(class-name class) (,host ,@args &rest ,dexador-kwargs)
+ (apply #',dexador-fn
+ (concatenate 'string ,host ,@parts)
+ ,@(when reqbody
+ (list :content reqbody))
+ ,dexador-kwargs))))
+
+(defun generate (class)
+ (eval (generate-client-for class)))