forked from incjung/cl-swagger-codegen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
templates.lisp
87 lines (77 loc) · 3.49 KB
/
templates.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
(in-package :cl-swagger)
;;; FIXME: these could all be macros instead of strings. With them being strings,
;;; we can't really edit them easily in lisp mode.
(define wrapper-call-template-v2
"
;;
;; summary : {{summary}}
;; description : {{{description}}}
;; * path : {{paths}}
;;
(defun {{first-name}}-{{path-name}} (&key (base-url \"{{baseurl}}\") param content basic-authorization)
(multiple-value-bind (stream code header)
(drakma:http-request (concatenate 'string base-url \"/\" \"{{path-url}}?\" param) :basic-authorization basic-authorization :accept \"{{accept}}\" :content-type \"{{accept-type}}\" :content content :want-stream t :method {{method}})
(if (and (< code 300) (>= code 200))
(progn (setf (flexi-streams:flexi-stream-external-format stream) :utf-8)
(cl-json:decode-json stream))
(format t \"failed - code : ~a\" code))))")
(define stream-to-string-function
"
(defun stream-to-string (stream)
(with-output-to-string (out)
(loop :for new = (read-line stream nil nil)
:while new
:do (print new out))))
")
(define rest-call-function
"
(defun rest-call (host url-path
&key params content basic-authorization
(method :get)
(accept \"application/json\")
(content-type \"application/json\"))
\"call http-request with basic params and conteent and authorization\"
(multiple-value-bind (stream code)
(drakma:http-request (format nil \"~a~a\" host url-path) :parameters params :content content :basic-authorization basic-authorization :accept accept :content-type content-type :want-stream t :method method)
(if (and (< code 300) (>= code 200))
(progn (setf (flexi-streams:flexi-stream-external-format stream) :utf-8)
(cl-json:decode-json stream))
(let ((response-body (stream-to-string stream)))
(error \"Unsuccessful HTTP ~a. Code: ~a.~%Response header:~%~a~%Response body: ~a\"
method code header response-body))))))")
(define rest-call-template-v1
"
;;
;; {{description}}
;; * path-url : {{paths}}
;;
(defun {{first-name}}-{{path-name}} (&key (base-url \"{{baseurl}}\") params content basic-authorization)
(rest-call base-url \"{{path-url}}\" :params params :content content
:basic-authorization basic-authorization
:method {{method}}
:accept \"{{accept}}\"
:content-type \"{{accept-type}}\"))")
(define rest-call-template-v2
"
;;
;; {{description}}
;; * path-url : {{paths}}
;;
(defun {{first-name}}-{{path-name}} (path-url &key (base-url \"{{baseurl}}\") params content basic-authorization)
(rest-call base-url path-url :params params :content content
:basic-authorization basic-authorization
:method {{method}}
:accept \"{{accept}}\"
:content-type \"{{accept-type}}\"))")
(define convert-json-template
"
;;
;; (convert-json #'function \"/path\" content-json)
;;
(defun convert-json (query-fun path body)
(multiple-value-bind (code stream head)
(funcall query-fun path body)
(if (and (>= code 200) (< code 300))
(progn (setf (flexi-streams:flexi-stream-external-format stream) :utf-8)
(cl-json:decode-json stream))
(error \"CONVERT-JSON failed with code ~a\" code))))")