[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/srht 9163dd9eb0 5/5: Lazily initialize srht-token.
From: |
ELPA Syncer |
Subject: |
[elpa] externals/srht 9163dd9eb0 5/5: Lazily initialize srht-token. |
Date: |
Mon, 6 Jun 2022 12:57:52 -0400 (EDT) |
branch: externals/srht
commit 9163dd9eb0cc5c88e872e623eb1826c58957c320
Author: Tassilo Horn <tsdh@gnu.org>
Commit: Aleksandr Vityazev <avityazev@posteo.org>
Lazily initialize srht-token.
* lisp/srht.el (srht-token): Move initialization (in terms of
auth-source-search) into a function of the same name. Also signal an error
here if no token can be determined.
(srht--api-request): Use the function srht-token instead of the variable.
---
lisp/srht.el | 25 ++++++++++++++++++-------
1 file changed, 18 insertions(+), 7 deletions(-)
diff --git a/lisp/srht.el b/lisp/srht.el
index dbc003d561..ca9bb07491 100644
--- a/lisp/srht.el
+++ b/lisp/srht.el
@@ -47,10 +47,7 @@
(string :format "%v")))
:group 'srht)
-(defcustom srht-token
- (if-let ((f (plist-get (car (auth-source-search :host "sr.ht"))
- :secret)))
- (funcall f) "")
+(defcustom srht-token nil
"Personal access token for Sourcehut instance.
It is necessary to use Oauth personal token not Oauth2."
:type 'string
@@ -61,6 +58,22 @@ It is necessary to use Oauth personal token not Oauth2."
:type 'string
:group 'srht)
+(defun srht-token ()
+ "Lookup variable `srht-token' if needed and return it.
+If variable `srht-token' is nil or cannot be looked up or is empty, signal
+an error."
+ (if srht-token
+ srht-token
+ (let ((token (when-let ((f (plist-get
+ (car (auth-source-search :host "sr.ht"))
+ :secret)))
+ (funcall f))))
+ (unless token
+ (error "No srht-token"))
+ (when (string-empty-p token)
+ (error "srht-token must not be empty"))
+ token)))
+
(cl-defun srht--build-uri-string (scheme &key host path query)
"Construct a URI string.
SCHEME should be a symbol. HOST should be strings or nil
@@ -133,13 +146,11 @@ THEN (see `plz').
THEN is a callback function, which is called in the response data.
ELSE is an optional callback function called when the request
fails with one argument, a `plz-error' struct."
- (cl-assert (and (not (string-empty-p srht-token)) srht-token)
- nil "Need a token")
(let ((uri (srht--make-uri domain service path query))
(content-type (or form "application/json")))
(plz method uri
:headers `(,(cons "Content-Type" content-type)
- ,(cons "Authorization" (concat "token " srht-token)))
+ ,(cons "Authorization" (concat "token " (srht-token))))
:body body
:then then
:else else