From 939193b30b80c41466c4ca1f5804773d3d085720 Mon Sep 17 00:00:00 2001 From: Mike Russo Date: Fri, 18 Apr 2025 15:40:05 -0600 Subject: [PATCH] Refactor requests --- bot.lisp | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/bot.lisp b/bot.lisp index 6c819f0..8f3c446 100644 --- a/bot.lisp +++ b/bot.lisp @@ -5,12 +5,12 @@ (in-package :birthday-boy) (load (sb-ext:posix-getenv "ASDF")) -(asdf:load-systems :dexador :local-time :jonathan :cl-yaml :cl-dotenv) -;; -(defvar *birthdays* nil) -(defvar *config* nil) -;; (.env:load-env (merge-pathnames ".env")) -;; (ql:quickload '(:dexador :cl-dotenv :local-time :jonathan)) +(asdf:load-systems :dexador :local-time :jonathan :cl-yaml) + +(defvar *birthdays* nil + "Hashtable of birthdays.") +(defvar *config* nil + "Hashtable of needed configuration for the bot.") (defun now-string () (local-time:format-timestring @@ -23,27 +23,29 @@ (defun make-txn-id () (format nil "~A~A" (now-string) (incf *txn-counter*))) -(defun put-json-content (uri json-content) - (dex:put uri - :headers '((:content-type . "application/json")) - :content (jojo:to-json json-content))) +(defun make-json-request (method uri json-content &key auth) + "Generic HTTP request function using dex:request with JSON content" + (let ((headers (if auth + `((:|Authorization| . ,(format nil "Bearer ~A" (gethash "bot_access_token" *config*))) + (:content-type . "application/json")) + '((:content-type . "application/json"))))) + (dex:request uri + :method method + :headers headers + :content (jojo:to-json json-content)))) +;; Replace your existing functions with these: +(defun put-json-content (uri json-content) + (make-json-request 'put uri json-content)) (defun put-json-content-auth (uri json-content) - (dex:put uri - :headers `((:|Authorization| . ,(format nil "Bearer ~A" (gethash "bot_access_token" *config*)) ) - (:content-type . "application/json")) - :content (jojo:to-json json-content))) + (make-json-request 'put uri json-content :auth t)) (defun post-json-content (uri json-content) - (dex:post uri - :headers '((:content-type . "application/json")) - :content (jojo:to-json json-content))) + (make-json-request 'post uri json-content)) + (defun post-json-content-auth (uri json-content) - (dex:post uri - :headers `((:|Authorization| . ,(format nil "Bearer ~A" (gethash "bot_access_token" *config*)) ) - (:content-type . "application/json")) - :content (jojo:to-json json-content))) + (make-json-request 'post uri json-content :auth t)) (defun make-homeserver-endpoint (endpoint)