diff options
author | Colin Okay <okay@toyful.space> | 2022-02-18 13:55:10 -0600 |
---|---|---|
committer | Colin Okay <okay@toyful.space> | 2022-02-18 13:55:10 -0600 |
commit | 4d2afc4e3c6cc5ce5be4f0d421da49ca6cead09a (patch) | |
tree | b35a02ecbade5622c0f4f54d450f54870a5dc10d /src | |
parent | f4b6c94bbd91d5061f1a449f407999be7e8e5814 (diff) |
logging in, redeeming tokens
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.lisp | 51 |
1 files changed, 38 insertions, 13 deletions
diff --git a/src/lib.lisp b/src/lib.lisp index 2480dab..720ff85 100644 --- a/src/lib.lisp +++ b/src/lib.lisp @@ -2,7 +2,8 @@ (defpackage oneliners.cli (:use :cl) - (:local-nicknames (#:api #:oneliners.api-client))) + (:local-nicknames (#:api #:oneliners.api-client) + (#:a #:alexandria))) (in-package :oneliners.cli) @@ -25,7 +26,13 @@ (let ((conf-file (config-file))) (ensure-directories-exist conf-file) (with-open-file (out conf-file :direction :output) - (print (make-config :host "https://oneliners.wiki") out)))) + (print (make-config :host "http://localhost:8888") out)))) + +(defun write-config-to-disk () + (let ((conf-file (config-file))) + (ensure-directories-exist conf-file) + (with-open-file (out conf-file :direction :output :if-exists :supersede) + (print *config* out)))) (defun fetch-config-from-disk () (let ((conf @@ -41,6 +48,9 @@ (defun host () (getf *config* :host)) (defun api-token () (getf *config* :api-token)) +(defun (setf api-token) (newval) + (setf (getf *config* :api-token) newval)) + (defun config-file () (merge-pathnames ".config/oneliners.config" (user-homedir-pathname))) @@ -53,20 +63,35 @@ (when (uiop:file-exists-p (last-search-file)) (nth n (uiop:read-file-form (last-search-file))))) -;;; SEARCHNG THE WIKI +;;; API REQUEST FUNCTIONS + +(defun login (user pass) + (ensure-config) + (a:when-let (response (jonathan:parse + (api:request-with + (:host (host)) + (api:post--token-contributor user :password pass)))) + (setf (api-token) (getf response :token)) + (write-config-to-disk) + (format t "Access token written to ~a~%" (config-file)))) + +(defun redeem-invite (token name pass) + (ensure-config ) + (api:request-with + (:host (host)) + (api:post--redeem-invite token + :username name + :password1 pass + :password2 pass))) (defun search-for-oneliners (terms limit not-flagged-p) (assert (loop for term in terms never (find #\, term) )) - (handler-case - (api:request-with - (:host (host)) - (api:get--search :tags (str:join ",") - :limit limit - :notflagged (if not-flagged-p "true" "false"))) - (dex:http-request-failed (e) - (format *error-output* "~a -- ~a" - (dex:response-status e) - (dex:response-body e))))) + (ensure-config) + (print (api:request-with + (:host (host)) + (api:get--search :tags (str:join "," terms) + :limit limit + :notflagged (if not-flagged-p "true" "false"))))) ;;; RUNNING COMMANDS |