(defun add-auth-headers (opts token)
(cons `(header ,(++ "Authorization: Bearer " token))
opts))
(defun add-timeout (opts ms)
(cons `(timeout ,ms) opts))
;; Usage:
(-> '((method post))
(add-timeout 5000)
(add-auth-headers "abc123"))
;; ((header "Authorization: Bearer abc123") (timeout 5000) (method post))
(defun get-credentials (opts)
(let ((username (proplists:get_value 'username opts))
(password (proplists:get_value 'password opts)))
(tuple username password)))
(defun validate-opts (opts required-keys)
(lists:all
(lambda (key) (proplists:is_defined key opts))
required-keys))
;; Usage:
(validate-opts '((host "localhost") (port 8080))
'(host port))
;; true
(validate-opts '((host "localhost"))
'(host port))
;; false
;; Proplist to keyword list (for external APIs)
(defun to-keyword-list (proplist)
(lists:flatmap
(lambda (entry)
(case entry
((tuple k v) (list (tuple ':key (atom_to_binary k 'utf8))
(tuple ':value v)))
(atom (list (tuple ':key (atom_to_binary atom 'utf8))
(tuple ':value 'true)))))
proplist))