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) (in-package :birthday-boy)
(load (sb-ext:posix-getenv "ASDF")) (load (sb-ext:posix-getenv "ASDF"))
(asdf:load-systems :dexador :local-time :jonathan :cl-yaml :cl-dotenv) (asdf:load-systems :dexador :local-time :jonathan :cl-yaml)
;;
(defvar *birthdays* nil) (defvar *birthdays* nil
(defvar *config* nil) "Hashtable of birthdays.")
;; (.env:load-env (merge-pathnames ".env")) (defvar *config* nil
;; (ql:quickload '(:dexador :cl-dotenv :local-time :jonathan)) "Hashtable of needed configuration for the bot.")
(defun now-string () (defun now-string ()
(local-time:format-timestring (local-time:format-timestring
@ -23,27 +23,29 @@
(defun make-txn-id () (defun make-txn-id ()
(format nil "~A~A" (now-string) (incf *txn-counter*))) (format nil "~A~A" (now-string) (incf *txn-counter*)))
(defun put-json-content (uri json-content) (defun make-json-request (method uri json-content &key auth)
(dex:put uri "Generic HTTP request function using dex:request with JSON content"
:headers '((:content-type . "application/json")) (let ((headers (if auth
:content (jojo:to-json json-content))) `((:|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) (defun put-json-content-auth (uri json-content)
(dex:put uri (make-json-request 'put uri json-content :auth t))
:headers `((:|Authorization| . ,(format nil "Bearer ~A" (gethash "bot_access_token" *config*)) )
(:content-type . "application/json"))
:content (jojo:to-json json-content)))
(defun post-json-content (uri json-content) (defun post-json-content (uri json-content)
(dex:post uri (make-json-request 'post uri json-content))
:headers '((:content-type . "application/json"))
:content (jojo:to-json json-content)))
(defun post-json-content-auth (uri json-content) (defun post-json-content-auth (uri json-content)
(dex:post uri (make-json-request 'post uri json-content :auth t))
:headers `((:|Authorization| . ,(format nil "Bearer ~A" (gethash "bot_access_token" *config*)) )
(:content-type . "application/json"))
:content (jojo:to-json json-content)))
(defun make-homeserver-endpoint (endpoint) (defun make-homeserver-endpoint (endpoint)