forked from manictime/manictime-api-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfiguration.py
More file actions
55 lines (45 loc) · 2.24 KB
/
Copy pathconfiguration.py
File metadata and controls
55 lines (45 loc) · 2.24 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os
from dotenv import load_dotenv
load_dotenv()
class Config:
"""Configuration management class"""
def __init__(self, server_url=None, auth_type=None, username=None,
password=None, domain=None, token=None, timeout=30):
self.server_url = server_url or os.getenv('MANICTIME_SERVER_URL', 'http://localhost:8080')
self.auth_type = auth_type or os.getenv('MANICTIME_AUTH_TYPE')
self.username = username or os.getenv('MANICTIME_USERNAME')
self.password = password or os.getenv('MANICTIME_PASSWORD')
self.domain = domain or os.getenv('MANICTIME_DOMAIN')
self.token = token or os.getenv('MANICTIME_TOKEN')
# Handle timeout conversion
if isinstance(timeout, str):
self.timeout = int(timeout)
else:
self.timeout = timeout
@classmethod
def from_env(cls):
"""Create a new Config instance from environment variables"""
# Re-load environment variables to ensure we have the latest values
load_dotenv()
# Convert timeout to int if present
timeout_str = os.getenv('MANICTIME_TIMEOUT')
timeout = int(timeout_str) if timeout_str else 30
return cls(
server_url=os.getenv('MANICTIME_SERVER_URL'),
auth_type=os.getenv('MANICTIME_AUTH_TYPE'),
username=os.getenv('MANICTIME_USERNAME'),
password=os.getenv('MANICTIME_PASSWORD'),
domain=os.getenv('MANICTIME_DOMAIN'),
token=os.getenv('MANICTIME_TOKEN'),
timeout=timeout
)
def validate(self):
"""Validate configuration"""
if not self.server_url:
raise ValueError("MANICTIME_SERVER_URL must be set")
if self.auth_type not in [None, 'ntlm', 'bearer']:
raise ValueError("MANICTIME_AUTH_TYPE must be one of: ntlm, bearer")
if self.auth_type == 'bearer' and not (self.token or (self.username and self.password)):
raise ValueError("Bearer auth requires either token or username/password")
if self.auth_type == 'ntlm' and not (self.username and self.password):
raise ValueError("NTLM auth requires username and password")