Skip to content

Commit 2f3eae1

Browse files
author
Josh Marchán
committed
Less repetitive interface for HANDLE-REQUEST.
1 parent 2e7ce63 commit 2f3eae1

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

src/db.lisp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -177,23 +177,23 @@ with a particular CouchDB database.")
177177
(:reply ((db =database=) uri &rest all-keys)
178178
(apply #'couch-request (server db) (strcat (name db) "/" uri) all-keys)))
179179

180-
(defmacro handle-request ((result-var request) &body expected-responses)
180+
(defmacro handle-request ((result-var db uri &rest db-request-keys) &body expected-responses)
181181
"Provides a nice interface to the relatively manual, low-level status-code checking that
182182
Chillax uses to understand CouchDB's responses. The format for EXPECTED-RESPONSES is the same as
183183
the CASE macro: The keys should be either keywords, or lists of keywords (not evaluated), which
184184
correspond to translated HTTP status code names. See *status-codes* for all the currently-recognized
185185
keywords."
186186
(let ((status-code (gensym "STATUS-CODE-")))
187187
`(multiple-value-bind (,result-var ,status-code)
188-
,request
188+
(db-request ,db ,uri ,@db-request-keys)
189189
(case ,status-code
190190
,@expected-responses
191191
(otherwise (error 'unexpected-response :status-code ,status-code :response ,result-var))))))
192192

193193
(defmessage db-info (db)
194194
(:documentation "Fetches info about a given database from the CouchDB server.")
195195
(:reply ((db =database=))
196-
(handle-request (response (db-request db ""))
196+
(handle-request (response db "")
197197
(:ok response)
198198
(:internal-server-error (error "Illegal database name: ~A" (name db)))
199199
(:not-found (error 'db-not-found :uri (db-namestring db))))))
@@ -208,7 +208,7 @@ that can be used to perform operations on it."
208208
(defun create-db (name &key (prototype =database=) (server =json-server=))
209209
"Creates a new CouchDB database. Returns a database object that can be used to operate on it."
210210
(let ((db (create prototype 'server server 'name name)))
211-
(handle-request (response (db-request db "" :method :put))
211+
(handle-request (response db "" :method :put)
212212
(:created db)
213213
(:internal-server-error (error "Illegal database name: ~A" name))
214214
(:precondition-failed (error 'db-already-exists :uri (db-namestring db))))))
@@ -222,20 +222,20 @@ that can be used to perform operations on it."
222222
(defmessage delete-db (db &key)
223223
(:documentation "Deletes a CouchDB database.")
224224
(:reply ((db =database=) &key)
225-
(handle-request (response (db-request db "" :method :delete))
225+
(handle-request (response db "" :method :delete)
226226
(:ok response)
227227
(:not-found (error 'db-not-found :uri (db-namestring db))))))
228228

229229
(defmessage compact-db (db)
230230
(:documentation "Triggers a database compaction.")
231231
(:reply ((db =database=))
232-
(handle-request (response (db-request db "_compact" :method :post))
232+
(handle-request (response db "_compact" :method :post)
233233
(:accepted response))))
234234

235235
(defmessage changes (db)
236236
(:documentation "Returns the changes feed for DB")
237237
(:reply ((db =database=))
238-
(handle-request (response (db-request db "_changes"))
238+
(handle-request (response db "_changes")
239239
(:ok response))))
240240

241241
;;;
@@ -244,7 +244,7 @@ that can be used to perform operations on it."
244244
(defmessage get-document (db id)
245245
(:documentation "Returns an CouchDB document from DB as an alist.")
246246
(:reply ((db =database=) id)
247-
(handle-request (response (db-request db id))
247+
(handle-request (response db id)
248248
(:ok response)
249249
(:not-found (error 'document-not-found :db db :id id)))))
250250

@@ -255,22 +255,22 @@ that can be used to perform operations on it."
255255
(when endkey (push `("endkey" . ,(prin1-to-string endkey)) params))
256256
(when limit (push `("limit" . ,(prin1-to-string limit)) params))
257257
(when include-docs (push `("include_docs" . "true") params))
258-
(handle-request (response (db-request db "_all_docs" :parameters params))
258+
(handle-request (response db "_all_docs" :parameters params)
259259
(:ok response))))
260260

261261
(defmessage batch-get-documents (db &rest doc-ids)
262262
(:documentation "Uses _all_docs to quickly fetch the given DOC-IDs in a single request.")
263263
(:reply ((db =database=) &rest doc-ids)
264-
(handle-request (response (db-request db "_all_docs" :method :post
265-
:parameters '(("include_docs" . "true"))
266-
:content (format nil "{\"foo\":[~{~S~^,~}]}" doc-ids)))
264+
(handle-request (response db "_all_docs" :method :post
265+
:parameters '(("include_docs" . "true"))
266+
:content (format nil "{\"foo\":[~{~S~^,~}]}" doc-ids))
267267
(:ok response))))
268268

269269
(defmessage put-document (db id doc &key)
270270
(:documentation "Puts a document into DB, using ID.")
271271
(:reply ((db =database=) id doc &key batch-ok-p)
272-
(handle-request (response (db-request db id :method :put :content doc
273-
:parameters (when batch-ok-p '(("batch" . "ok")))))
272+
(handle-request (response db id :method :put :content doc
273+
:parameters (when batch-ok-p '(("batch" . "ok"))))
274274
((:created :accepted) response)
275275
(:conflict (error 'document-conflict :id id :doc doc)))))
276276

@@ -280,20 +280,20 @@ document does not already exist. Note that using this function is discouraged in
280280
documentation, since it may result in duplicate documents because of proxies and other network
281281
intermediaries.")
282282
(:reply ((db =database=) doc)
283-
(handle-request (response (db-request db "" :method :post :content doc))
283+
(handle-request (response db "" :method :post :content doc)
284284
((:created :accepted) response)
285285
(:conflict (error 'document-conflict :doc doc)))))
286286

287287
(defmessage delete-document (db id revision)
288288
(:documentation "Deletes an existing document.")
289289
(:reply ((db =database=) id revision)
290-
(handle-request (response (db-request db (format nil "~A?rev=~A" id revision) :method :delete))
290+
(handle-request (response db (format nil "~A?rev=~A" id revision) :method :delete)
291291
(:ok response))))
292292

293293
(defmessage copy-document (db from-id to-id &key)
294294
(:documentation "Copies a document's content in-database.")
295295
(:reply ((db =database=) from-id to-id &key revision)
296-
(handle-request (response (db-request db from-id :method :copy
297-
:additional-headers `(("Destination" . ,to-id))
298-
:parameters `(,(when revision `("rev" . ,revision)))))
296+
(handle-request (response db from-id :method :copy
297+
:additional-headers `(("Destination" . ,to-id))
298+
:parameters `(,(when revision `("rev" . ,revision))))
299299
(:created response))))

0 commit comments

Comments
 (0)