summaryrefslogtreecommitdiff
path: root/py/utils.py
diff options
context:
space:
mode:
authorMartin Bielik <martin.bielik@instea.sk>2024-12-06 20:48:05 +0100
committerMartin Bielik <martin.bielik@instea.sk>2024-12-06 20:48:05 +0100
commit556819b8245133ebd9d6cbe77358f20df4cc9ca2 (patch)
tree33e53357e0da11b080e89ba38f98457bf864d494 /py/utils.py
parent9d42c36dd47060050b514a334c4d67a2e9358782 (diff)
downloadvim-ai-556819b8245133ebd9d6cbe77358f20df4cc9ca2.tar.gz
allow override global token config
Diffstat (limited to '')
-rw-r--r--py/utils.py11
1 files changed, 7 insertions, 4 deletions
diff --git a/py/utils.py b/py/utils.py
index 3382e59..d3b7c8d 100644
--- a/py/utils.py
+++ b/py/utils.py
@@ -19,11 +19,13 @@ debug_log_file = vim.eval("g:vim_ai_debug_log_file")
class KnownError(Exception):
pass
-def load_api_key():
- config_file_path = os.path.expanduser(vim.eval("g:vim_ai_token_file_path"))
+def load_api_key(config_token_file_path):
+ # token precedence: config file path, global file path, env variable
+ global_token_file_path = vim.eval("g:vim_ai_token_file_path")
api_key_param_value = os.getenv("OPENAI_API_KEY")
try:
- with open(config_file_path, 'r') as file:
+ token_file_path = config_token_file_path or global_token_file_path
+ with open(os.path.expanduser(token_file_path), 'r') as file:
api_key_param_value = file.read()
except Exception:
pass
@@ -67,6 +69,7 @@ def make_http_options(options):
return {
'request_timeout': float(options['request_timeout']),
'enable_auth': bool(int(options['enable_auth'])),
+ 'token_file_path': options['token_file_path'],
}
# During text manipulation in Vim's visual mode, we utilize "normal! c" command. This command deletes the highlighted text,
@@ -212,7 +215,7 @@ def openai_request(url, data, options):
"Content-Type": "application/json",
}
if enable_auth:
- (OPENAI_API_KEY, OPENAI_ORG_ID) = load_api_key()
+ (OPENAI_API_KEY, OPENAI_ORG_ID) = load_api_key(options['token_file_path'])
headers['Authorization'] = f"Bearer {OPENAI_API_KEY}"
if OPENAI_ORG_ID is not None: