From 4d2afc4e3c6cc5ce5be4f0d421da49ca6cead09a Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Fri, 18 Feb 2022 13:55:10 -0600 Subject: logging in, redeeming tokens --- src/lib.lisp | 51 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 13 deletions(-) (limited to 'src') 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 -- cgit v1.2.3