diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/client.lisp | 141 | ||||
-rw-r--r-- | lib/state.lisp | 9 |
2 files changed, 65 insertions, 85 deletions
diff --git a/lib/client.lisp b/lib/client.lisp index ae5534f..a526b5c 100644 --- a/lib/client.lisp +++ b/lib/client.lisp @@ -30,11 +30,10 @@ (defun search-for-oneliners (terms limit &optional not-flagged-p all-flagged-p newestp) (assert (loop for term in terms never (find #\, term)) () "Search terms may not contain commas.") (let ((json - (api:get--oneliners :tags (str:join "," terms) - :limit limit - :notflagged (true-or-false not-flagged-p) - :newest (true-or-false newestp) - :onlyflagged (true-or-false all-flagged-p)))) + (api:get-oneliners (str:join "," terms) + limit + not-flagged-p + *host*))) (cache-and-print-search-response json))) (defun the-oneliner (name-or-id) @@ -42,7 +41,7 @@ not in the local cache, try to fetch from configured server." (a:if-let ((ol (get-cached name-or-id))) ol - (let ((ol (jonathan:parse (api:get--oneliner-entry name-or-id)))) + (let ((ol (jonathan:parse (api:get-oneliner/entry name-or-id *host*)))) (merge-oneliners (list ol)) ol))) @@ -63,16 +62,12 @@ running the body. If such a oneliner can be found." (defun newest-oneliners (&optional limit) (let ((response - (if limit - (api:get--oneliners-newest :limit limit) - (api:get--oneliners-newest)))) + (api:get-oneliners/newest (or limit 10) *host*))) (cache-and-print-search-response response))) (defun all-flagged-oneliners (&optional limit) (let ((response - (if limit - (api:get--oneliners-all-flagged :limit limit) - (api:get--oneliners-all-flagged)))) + (api:get-oneliners/all-flagged (or limit 10) *host*))) (cache-and-print-search-response response))) (defun print-item-explanation (name-or-number) @@ -95,16 +90,15 @@ running the body. If such a oneliner can be found." (format t "Draft Name: ~a~%" (first draft)) (print-oneliner-result-for-user (rest draft))))) -;;; RUNNING ONELINERS +;; ;;; running ONELINERS (defvar *ol-output-timeout* 1) -(defun run-item (ident args &key force-clip (timeout nil timeout-p) draftp verbose confirm) - "Runs a oneliner identified by IDENT (if available) with arguments ARGS." - (let ((ol (if draftp (fetch-draft ident) (the-oneliner ident)))) - (when ol - (let ((*ol-output-timeout* (if timeout-p timeout *ol-output-timeout*))) - (bind-vars-and-run-oneliner ol args force-clip verbose confirm))))) +(defun handle-run-oneliner (ol &optional clip) + (if clip + (progn (trivial-clipboard:text ol) + (format t "Copied oneliner to clipboard~%")) + (run-with-shell ol :shell-name (or (shell) "bash") :await-output-p *ol-output-timeout*))) (defun bind-vars-and-run-oneliner (ol args &optional force-clip verbose confirm) (let* ((oneliner (oneliner-oneliner ol)) @@ -137,11 +131,13 @@ running the body. If such a oneliner can be found." (y-or-n-p "Proceed?")) (handle-run-oneliner oneliner (or force-clip (equalp runstyle "manual"))))))) -(defun handle-run-oneliner (ol &optional clip) - (if clip - (progn (trivial-clipboard:text ol) - (format t "Copied oneliner to clipboard~%")) - (run-with-shell ol :shell-name (or (shell) "bash") :await-output-p *ol-output-timeout*))) +(defun run-item (ident args &key force-clip (timeout nil timeout-p) draftp verbose confirm) + "Runs a oneliner identified by IDENT (if available) with arguments ARGS." + (let ((ol (if draftp (fetch-draft ident) (the-oneliner ident)))) + (when ol + (let ((*ol-output-timeout* (if timeout-p timeout *ol-output-timeout*))) + (bind-vars-and-run-oneliner ol args force-clip verbose confirm))))) + ;;; ADDING ONELINERS @@ -271,62 +267,50 @@ running the body. If such a oneliner can be found." (defun publish-draft (name) (when-draft (ol name) - (api:request-with - (:body (oneliner-to-json-body ol) - :content-type "application/json") - (let ((updated - (jonathan:parse (if (oneliner-id ol) - (api:patch--oneliner-entry-edit (oneliner-id ol) :token (api-token)) - (api:post--oneliner :token (api-token)))))) - (merge-oneliners (list updated)) - (drop-draft name) - (format t "Draft ~a published and removed from drafts.~%" name))))) - - -;;; ADMIN OF ONELINER ENTRIES + (let ((updated + (jonathan:parse + (a:if-let (id (oneliner-id ol)) + (api:patch-oneliner/entry/edit id (api-token) *host* + :%content-type "application/json" + :%body (oneliner-to-json-body ol)) + (api:post-oneliner (api-token) *host* + :%content-type "application/json" + :%body (oneliner-to-json-body ol)))))) + (merge-oneliners (list updated)) + (drop-draft name) + (format t "Draft ~a published and removed from drafts.~%" name)))) + + +;; ;;; ADMIN OF ONELINER ENTRIES (defun delete-item (ident) (when-oneliner (ol ident) - (api:delete--oneliner-oneliner - ident - :token (api-token)) + (api:delete-oneliner/oneliner ident (api-token) *host*) ;; if we've made it this far no http error has been returned, ;; hence we can delete it from the cache (remove-from-cache ident))) (defun flag-item (ident) (when-oneliner (ol ident) - (api:put--oneliner-entry-flag - ident - :token (api-token) - :value "true") + (api:put-oneliner/entry/flag ident (api-token) "true" *host*) ;; no http error, so we flag the cached version, ol. (setf (oneliner-isflagged ol) t))) (defun unflag-item (ident) (when-oneliner (ol ident) - (api:put--oneliner-entry-flag - ident - :token (api-token) - :value "false") + (api:put-oneliner/entry/flag ident (api-token) "false" *host*) ;; no http error, so we can unflag the cached version, ol (setf (oneliner-isflagged ol) nil))) (defun lock-item (ident) (when-oneliner (ol ident) - (api:put--oneliner-oneliner-locked - ident - :token (api-token) - :value "true") + (api:put-oneliner/oneliner/locked ident (api-token) "true" *host*) ;; no http error, so we can lock the cached version, ol (setf (oneliner-islocked ol) t))) (defun unlock-item (ident) (when-oneliner (ol ident) - (api:put--oneliner-oneliner-locked - ident - :token (api-token) - :value "false") + (api:put-oneliner/oneliner/locked ident (api-token) "false" *host*) ;; no http error, so we can unlock the cached version, ol (setf (oneliner-islocked ol) nil))) @@ -334,7 +318,9 @@ running the body. If such a oneliner can be found." ;;; ACCOUNT AND INVITE STUFF (defun request-invite-code () - (let ((invite (jonathan:parse (api:post--invite :token (api-token))))) + (let ((invite + (jonathan:parse + (api:post-invite (api-token) *host*)))) (format t "Invite Code: ~a~%Expires: ~a~%" (getf invite :code) (getf invite :expires)))) @@ -342,10 +328,9 @@ running the body. If such a oneliner can be found." (defun login (user pass) (let ((response (jonathan:parse - (api:request-with - (:body (jonathan:to-json (list :password pass :handle user)) - :content-type "application/json") - (api:post--access))))) + (api:post-access *host* + :%content-type "application/json" + :%body (jonathan:to-json (list :password pass :handle user)))))) (setf (api-token) (getf response :token) (handle) user) (format t "Access token written to ~a~%You may now make contributions to the wiki!.~%" @@ -354,29 +339,22 @@ running the body. If such a oneliner can be found." (defun change-pw (current new repeated) (unless (equal new repeated) (error "The new password doesn't match the repeated value. Double check.")) - (api:put--contributor-who-password (handle) - :token (api-token) - :value new - :repeated new - :current current)) + (api:put-contributor/who/password (handle) new repeated current (api-token) *host*)) (defun change-signature () (let ((new-sig (prompt-for-signature))) (ensure-config) - (api:request-with - (:host (host) - :body (jonathan:to-json (list :signature new-sig)) - :content-type "application/json") - (api:put--contributor-who-signature (handle) :token (api-token)) - (format t "Your signature was changed.~%")))) + (api:put-contributor/who/signature (handle) (api-token) *host* + :%content-type "application/json" + :%body (jonathan:to-json (list :signature new-sig))) + (format t "Your signature was changed.~%"))) (defun show-contributor (name) - (let ((contributor (api:get--contributor-who name))) + (let ((contributor (api:get-contributor/who name *host*))) (print-contributor (jonathan:parse contributor)))) - (defparameter +agree-to-the-unlicense+ "By creating this contributor account, I agree that my contributions be released into the public domain, for the benefit of the public at @@ -388,17 +366,16 @@ running the body. If such a oneliner can be found." (defun redeem-invite (token name pass) (when (yes-or-no-p +agree-to-the-unlicense+) - (api:request-with - (:body (jonathan:to-json (list :handle name + (api:post-invite/redeem/code token *host* + :%content-type "application/json" + :%body (jonathan:to-json (list :handle name :password1 pass :password2 pass - :signature (prompt-for-signature))) - :content-type "application/json") - (api:post--invite-redeem-code token) - (format t "Account made for ~a. You may log in now~%" name)))) + :signature (prompt-for-signature)))) + (format t "Account made for ~a. You may log in now~%" name))) -;;TODO: check this .. shouldnt access be a username??? +;;TODO: check this .. shouldnt access be a username??? yes! (defun revoke-access () - (api:delete--access-access (api-token) :token (api-token)) + (api:delete-access/access (api-token) (api-token) *host*) (format t "You were logged out~%")) diff --git a/lib/state.lisp b/lib/state.lisp index e690f37..1f9f36b 100644 --- a/lib/state.lisp +++ b/lib/state.lisp @@ -160,6 +160,9 @@ user for some input if it does not." (read-config-file) (make-fresh-config))) +;;; HOSTS + +(defvar *host* nil) ;;; STATE LOADING MACRO @@ -169,18 +172,18 @@ sets the api's *host* variable. If BODY produces no errors, the " `(let* ((*config* (ensure-config)) (*cache* (read-cache-file)) (*drafts* (read-drafts-file)) - (api:*host* (config-host *config*)) + (*host* (config-host *config*)) (errorsp nil)) (unwind-protect (handler-case (progn - (assert api:*host* () "ol must be configured with a server host.") + (assert *host* () "ol must be configured with a server host.") (set-term-width) ,@body) (dexador.error:http-request-failed (e) (setf errorsp t) (format *error-output* "Operation failed. The server at ~a returned with ~a~%" - api:*host* + *host* (dexador.error:response-status e))) (error (e) (setf errorsp t) |