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
|
(defpackage #:lazybones-client.shared
(:intern #:req-string)
(:export #:*host* #:*body* #:*headers* #:*cookies*))
(defpackage #:ONELINERS.API-CLIENT
(:use :cl :lazybones-client.shared)
(:export #:*host* #:*body* #:*headers* #:*cookies* #:request-with
#:POST--AUTH
#:POST--ONELINER
#:PUT--ONELINER-ONELINER
#:GET--SEARCH))
(in-package :ONELINERS.API-CLIENT)
(defvar *host* nil
"The host to which the client will send its requests.")
(defvar *body* nil
"Body passed to client post, put, and patch requests")
(defvar *cookies* nil
"An instance of CL-COOKIE:COOKIE-JAR.")
(defvar *headers* nil
"A liist of (header-name . header-value) pairs.")
(defmacro request-with ((&key host body headers content-type cookies) &body forms)
"Make a request in a specific context.
HOST is a string, the hostname where the request will be sent. Defaults
to *HOST*.
BODY should be a string, an alist, or a pathname. Default to *BODY*
HEADERS should be an ALIST of (header-name . header-value) string
pairs. Defaults to *HEADERS*.
CONTENT-TYPE is a convenience for supplying just the Content-Type
header.
COOKIES should be an instance of CL-COOKIE:COOKIE-JAR. Defaults to
*COOKIES*.
"
(let ((content-type-var (gensym)))
`(let ((*host* (or ,host *host*))
(*body* (or ,body *body*))
(*headers* (or ,headers *headers*))
(*cookies* (or ,cookies *cookies*))
(,content-type-var ,content-type))
(when ,content-type-var
(push (cons "Content-Type" ,content-type-var) *headers*))
,@forms)))
(DEFUN POST--AUTH ()
"Requests an authorization token"
(LET ((LAZYBONES-CLIENT.SHARED::REQ-STRING
(APPLY #'CONCATENATE 'STRING LAZYBONES-CLIENT.SHARED:*HOST*
(FORMAT NIL "/auth") (WHEN (OR) (LIST "?")))))
(IF LAZYBONES-CLIENT.SHARED:*BODY*
(DEXADOR:POST LAZYBONES-CLIENT.SHARED::REQ-STRING :CONTENT
LAZYBONES-CLIENT.SHARED:*BODY* :COOKIE-JAR
LAZYBONES-CLIENT.SHARED:*COOKIES* :HEADERS
LAZYBONES-CLIENT.SHARED:*HEADERS*)
(DEXADOR:POST LAZYBONES-CLIENT.SHARED::REQ-STRING :COOKIE-JAR
LAZYBONES-CLIENT.SHARED:*COOKIES* :HEADERS
LAZYBONES-CLIENT.SHARED:*HEADERS*))))
(DEFUN POST--ONELINER ()
"Adds a new oneliner entry to the wiki database."
(LET ((LAZYBONES-CLIENT.SHARED::REQ-STRING
(APPLY #'CONCATENATE 'STRING LAZYBONES-CLIENT.SHARED:*HOST*
(FORMAT NIL "/oneliner") (WHEN (OR) (LIST "?")))))
(IF LAZYBONES-CLIENT.SHARED:*BODY*
(DEXADOR:POST LAZYBONES-CLIENT.SHARED::REQ-STRING :CONTENT
LAZYBONES-CLIENT.SHARED:*BODY* :COOKIE-JAR
LAZYBONES-CLIENT.SHARED:*COOKIES* :HEADERS
LAZYBONES-CLIENT.SHARED:*HEADERS*)
(DEXADOR:POST LAZYBONES-CLIENT.SHARED::REQ-STRING :COOKIE-JAR
LAZYBONES-CLIENT.SHARED:*COOKIES* :HEADERS
LAZYBONES-CLIENT.SHARED:*HEADERS*))))
(DEFUN PUT--ONELINER-ONELINER (ONELINER)
"Updates a oneliner entry in the wiki database."
(LET ((LAZYBONES-CLIENT.SHARED::REQ-STRING
(APPLY #'CONCATENATE 'STRING LAZYBONES-CLIENT.SHARED:*HOST*
(FORMAT NIL "/oneliner/~a" ONELINER) (WHEN (OR) (LIST "?")))))
(IF LAZYBONES-CLIENT.SHARED:*BODY*
(DEXADOR:PUT LAZYBONES-CLIENT.SHARED::REQ-STRING :CONTENT
LAZYBONES-CLIENT.SHARED:*BODY* :COOKIE-JAR
LAZYBONES-CLIENT.SHARED:*COOKIES* :HEADERS
LAZYBONES-CLIENT.SHARED:*HEADERS*)
(DEXADOR:PUT LAZYBONES-CLIENT.SHARED::REQ-STRING :COOKIE-JAR
LAZYBONES-CLIENT.SHARED:*COOKIES* :HEADERS
LAZYBONES-CLIENT.SHARED:*HEADERS*))))
(DEFUN GET--SEARCH
(&KEY COMMANDS KEYWORDS LIMIT PAGE NEXTPAGE NOTFLAGGED ONLYAUDITED)
"A search endpoint returning a JSON encoded array of Oneliner Entries.
**Note**: either command or keywords are required.
"
(LET ((LAZYBONES-CLIENT.SHARED::REQ-STRING
(APPLY #'CONCATENATE 'STRING LAZYBONES-CLIENT.SHARED:*HOST*
(FORMAT NIL "/search")
(WHEN
(OR COMMANDS KEYWORDS LIMIT PAGE NEXTPAGE NOTFLAGGED
ONLYAUDITED)
(LIST "?"
(IF COMMANDS
(CONCATENATE 'STRING (SYMBOL-NAME 'COMMANDS) "="
(FORMAT NIL "~a" COMMANDS))
"")
(IF KEYWORDS
(CONCATENATE 'STRING "&" (SYMBOL-NAME 'KEYWORDS)
"=" (FORMAT NIL "~a" KEYWORDS))
"")
(IF LIMIT
(CONCATENATE 'STRING "&" (SYMBOL-NAME 'LIMIT) "="
(FORMAT NIL "~a" LIMIT))
"")
(IF PAGE
(CONCATENATE 'STRING "&" (SYMBOL-NAME 'PAGE) "="
(FORMAT NIL "~a" PAGE))
"")
(IF NEXTPAGE
(CONCATENATE 'STRING "&" (SYMBOL-NAME 'NEXTPAGE)
"=" (FORMAT NIL "~a" NEXTPAGE))
"")
(IF NOTFLAGGED
(CONCATENATE 'STRING "&" (SYMBOL-NAME 'NOTFLAGGED)
"=" (FORMAT NIL "~a" NOTFLAGGED))
"")
(IF ONLYAUDITED
(CONCATENATE 'STRING "&" (SYMBOL-NAME 'ONLYAUDITED)
"=" (FORMAT NIL "~a" ONLYAUDITED))
""))))))
(IF LAZYBONES-CLIENT.SHARED:*BODY*
(DEXADOR:GET LAZYBONES-CLIENT.SHARED::REQ-STRING :CONTENT
LAZYBONES-CLIENT.SHARED:*BODY* :COOKIE-JAR
LAZYBONES-CLIENT.SHARED:*COOKIES* :HEADERS
LAZYBONES-CLIENT.SHARED:*HEADERS*)
(DEXADOR:GET LAZYBONES-CLIENT.SHARED::REQ-STRING :COOKIE-JAR
LAZYBONES-CLIENT.SHARED:*COOKIES* :HEADERS
LAZYBONES-CLIENT.SHARED:*HEADERS*))))
|