diff options
Diffstat (limited to 'rt-liberation-rest.el')
-rw-r--r-- | rt-liberation-rest.el | 40 |
1 files changed, 32 insertions, 8 deletions
diff --git a/rt-liberation-rest.el b/rt-liberation-rest.el index 99f6ce9..a7eb076 100644 --- a/rt-liberation-rest.el +++ b/rt-liberation-rest.el @@ -1,4 +1,4 @@ -;;; rt-liberation-rest.el --- Interface to the RT REST API +;;; rt-liberation-rest.el --- Interface to the RT REST API -*- lexical-binding: t; -*- ;; Copyright (C) 2014-2015 Free Software Foundation, Inc. ;; @@ -31,8 +31,12 @@ (require 'url) (require 'url-util) +(require 'auth-source) +;;; ------------------------------------------------------------------ +;;; variables and constants +;;; ------------------------------------------------------------------ (defvar rt-liber-rest-debug-buffer-name "*rt-liber-rest debug log*" "Buffer name of debug capture.") @@ -45,16 +49,19 @@ (defvar rt-liber-rest-url "" "URL of RT installation.") -(defvar rt-liber-rest-username "" +(defvar rt-liber-rest-username nil "Username of RT account.") -(defvar rt-liber-rest-password "" +(defvar rt-liber-rest-password nil "Password of RT account.") (defvar rt-liber-rest-verbose-p t "If non-nil, be verbose about what's happening.") +;;; ------------------------------------------------------------------ +;;; functions +;;; ------------------------------------------------------------------ (defun rt-liber-rest-write-debug (str) "Write to debug buffer." (when (not (stringp str)) @@ -65,6 +72,21 @@ (goto-char (point-max)) (insert str)))) +(defun rt-liber-rest-auth () + "Try to get the REST credentials." + (if (and (stringp rt-liber-rest-username) + (stringp rt-liber-rest-password) + (< 0 (length rt-liber-rest-username)) + (< 0 (length rt-liber-rest-password))) + t + (message "rt-liber: no REST credentials set, so attempting auth-source") + (let ((auth-source-found-p + (auth-source-search :host "rt-liberation" :require '(:user :secret) :create nil))) + (when (not auth-source-found-p) + (error "no auth-source found for login")) + (setq rt-liber-rest-password (funcall (plist-get (nth 0 auth-source-found-p) :secret)) + rt-liber-rest-username (plist-get (nth 0 auth-source-found-p) :user))))) + (defun rt-liber-rest-search-string (scheme url username password query) "Return the search query string." (let ((user (url-encode-url username)) @@ -124,11 +146,11 @@ str) (setq str (decode-coding-string - (with-current-buffer response - (buffer-substring-no-properties (point-min) - (point-max))) - 'utf-8)) - + (with-current-buffer response + (buffer-substring-no-properties (point-min) + (point-max))) + 'utf-8)) + (message "done") (rt-liber-rest-write-debug (format "outgoing rest call -->\n%s\n<-- incoming\n%s\n" url str)) str))) @@ -138,6 +160,7 @@ (when (or (not (stringp op)) (not (stringp query-string))) (error "bad arguments")) + (rt-liber-rest-auth) (cond ((string= op "ls") (rt-liber-rest-call (rt-liber-rest-search-string rt-liber-rest-scheme @@ -215,6 +238,7 @@ "Run edit comment to set FIELD to VALUE." (message "started edit command at %s..." (current-time-string)) (message "ticket #%s, %s <- %s" ticket-id field value) + (rt-liber-rest-auth) (let ((request-data (format "content=%s: %s" (url-hexify-string field) |