diff options
-rw-r--r-- | oneliners.api-client.asd | 3 | ||||
-rw-r--r-- | oneliners.api-client.lisp | 149 |
2 files changed, 152 insertions, 0 deletions
diff --git a/oneliners.api-client.asd b/oneliners.api-client.asd new file mode 100644 index 0000000..3b75288 --- /dev/null +++ b/oneliners.api-client.asd @@ -0,0 +1,3 @@ +(asdf:defsystem #:ONELINERS.API-CLIENT + :depends-on (#:dexador) + :components ((:file "oneliners.api-client")))
\ No newline at end of file diff --git a/oneliners.api-client.lisp b/oneliners.api-client.lisp new file mode 100644 index 0000000..d3142fb --- /dev/null +++ b/oneliners.api-client.lisp @@ -0,0 +1,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*)))) + |