-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_keys.py
More file actions
38 lines (30 loc) · 935 Bytes
/
Copy pathapi_keys.py
File metadata and controls
38 lines (30 loc) · 935 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from typing import NamedTuple
import functools
import os
class APIKeyNotFound(Exception):
"""The API key was not found."""
class APIKeyResolver(NamedTuple):
"""Looks for an API key."""
name: str
env_var: str
file_path: str
@functools.lru_cache
def get(self) -> str:
if os.environ.get(self.env_var):
return os.environ[self.env_var]
try:
with open(os.path.expanduser(self.file_path), encoding='utf-8') as f:
return f.read().strip()
except FileNotFoundError:
# pylint: disable=raise-missing-from
raise APIKeyNotFound(
f'Plese provide {self.name} API key in environment variable '
f'{self.env_var} or in file {self.file_path}.')
OPENAI_API_KEY = APIKeyResolver(
name='OpenAI',
env_var='OPENAI_API_KEY',
file_path='~/openai.key')
REPLICATE_API_KEY = APIKeyResolver(
name='Replicate',
env_var='REPLICATE_API_KEY',
file_path='~/replicate.key')