Refactor requests

This commit is contained in:
Mike Russo 2025-04-18 15:40:05 -06:00
parent c7afa8eaab
commit 939193b30b

View file

@ -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)