File size: 4,693 Bytes
1b7e88c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import logging
import os
import time
from typing import Any, Optional

from omagent_core.engine.configuration.settings.authentication_settings import \
    AuthenticationSettings
from pydantic import Field
from pydantic_settings import BaseSettings

TEMPLATE_CONFIG = {
    "name": "Configuration",
    "base_url": {
        "value": "http://localhost:8080",
        "description": "The Conductor Server API endpoint",
        "env_var": "CONDUCTOR_SERVER_URL",
    },
    "auth_key": {
        "value": None,
        "description": "The authorization key",
        "env_var": "AUTH_KEY",
    },
    "auth_secret": {
        "value": None,
        "description": "The authorization secret",
        "env_var": "CONDUCTOR_AUTH_SECRET",
    },
    "auth_token_ttl_min": {
        "value": 45,
        "description": "The authorization token refresh interval in minutes.",
        "env_var": "AUTH_TOKEN_TTL_MIN",
    },
    "debug": {"value": False, "description": "Debug mode", "env_var": "DEBUG"},
}


class Configuration(BaseSettings):
    class Config:
        """Configuration for this pydantic object."""

        extra = "allow"

    base_url: str = Field(
        default="http://localhost:8080", description="The Conductor Server API endpoint"
    )
    auth_key: Optional[str] = Field(default=None, description="The authorization key")
    auth_secret: Optional[str] = Field(
        default=None,
        description="The authorization secret",
    )
    auth_token_ttl_min: int = Field(
        default=45, description="The authorization token refresh interval in minutes."
    )
    debug: bool = Field(default=False, description="Debug mode")

    def model_post_init(self, __context: Any) -> None:
        self.__log_level = logging.DEBUG if self.debug else logging.INFO
        self.AUTH_TOKEN = None
        self.temp_folder_path = None
        self.host = self.base_url + "/api"
        self.__ui_host = self.host.replace("8080/api", "5000")
        if self.auth_key and self.auth_secret:
            self.authentication_settings = AuthenticationSettings(
                key_id=self.auth_key, key_secret=self.auth_secret
            )
        else:
            self.authentication_settings = None

        # Log format
        self.logger_format = "%(asctime)s %(name)-12s %(levelname)-8s %(message)s"

        # SSL/TLS verification
        # Set this to false to skip verifying SSL certificate when calling API
        # from https server.
        self.verify_ssl = True
        # Set this to customize the certificate file to verify the peer.
        self.ssl_ca_cert = None
        # client certificate file
        self.cert_file = None
        # client key file
        self.key_file = None
        # Set this to True/False to enable/disable SSL hostname verification.
        self.assert_hostname = None

        # Proxy URL
        self.proxy = None
        # Safe chars for path_param
        self.safe_chars_for_path_param = ""

        # Provide an alterative to requests.Session() for HTTP connection.
        self.http_connection = None

        # not updated yet
        self.token_update_time = 0
        self.auth_token_ttl_msec = self.auth_token_ttl_min * 60 * 1000

    @property
    def logger_format(self):
        """The logger format.

        The logger_formatter will be updated when sets logger_format.

        :param value: The format string.
        :type: str
        """
        return self.__logger_format

    @logger_format.setter
    def logger_format(self, value):
        """The logger format.

        The logger_formatter will be updated when sets logger_format.

        :param value: The format string.
        :type: str
        """
        self.__logger_format = value

    @property
    def log_level(self):
        """The log level.

        The log_level will be updated when sets logger_format.

        :param value: The format string.
        :type: str
        """
        return self.__log_level

    @property
    def ui_host(self):
        """

        The log_level will be updated when sets logger_format.

        :param value: The format string.
        :type: str
        """
        return self.__ui_host

    def apply_logging_config(self, log_format: str = None, level=None):
        if log_format is None:
            log_format = self.logger_format
        if level is None:
            level = self.__log_level
        logging.basicConfig(format=log_format, level=level)

    @staticmethod
    def get_logging_formatted_name(name):
        return f"[{os.getpid()}] {name}"

    def update_token(self, token: str) -> None:
        self.AUTH_TOKEN = token
        self.token_update_time = round(time.time() * 1000)