Module: api_settings

Expand source code
# Copyright (C) 2023-present The Project Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from dataclasses import dataclass
from cl.runtime.settings.settings import Settings


@dataclass(slots=True, kw_only=True)
class ApiSettings(Settings):
    """REST API settings."""

    host_name: str = "localhost"
    """REST API host name (either host name or IP can be used to access the API)."""

    host_ip: str = "127.0.0.1"
    """REST API host IP (either host name or IP can be used to access the API)."""

    port: int = 7008
    """REST API port."""

    def init(self) -> None:
        """Same as __init__ but can be used when field values are set both during and after construction."""

        if not isinstance(self.host_name, str):
            raise RuntimeError(f"{type(self).__name__} field 'host_name' must be a string.")
        if not isinstance(self.host_ip, str):
            raise RuntimeError(f"{type(self).__name__} field 'host_ip' must be a string.")

        if isinstance(self.port, int):
            pass
        elif isinstance(self.port, str):
            if self.port.isdigit():
                self.port = int(self.port)
            else:
                raise RuntimeError(f"{type(self).__name__} field 'port' includes non-digit characters.")
        else:
            raise RuntimeError(f"{type(self).__name__} field 'port' must be an int or a string.")

    @classmethod
    def get_prefix(cls) -> str:
        return "runtime_api"

Classes

class ApiSettings (*, host_name: str = 'localhost', host_ip: str = '127.0.0.1', port: int = 7008)

REST API settings.

Expand source code
@dataclass(slots=True, kw_only=True)
class ApiSettings(Settings):
    """REST API settings."""

    host_name: str = "localhost"
    """REST API host name (either host name or IP can be used to access the API)."""

    host_ip: str = "127.0.0.1"
    """REST API host IP (either host name or IP can be used to access the API)."""

    port: int = 7008
    """REST API port."""

    def init(self) -> None:
        """Same as __init__ but can be used when field values are set both during and after construction."""

        if not isinstance(self.host_name, str):
            raise RuntimeError(f"{type(self).__name__} field 'host_name' must be a string.")
        if not isinstance(self.host_ip, str):
            raise RuntimeError(f"{type(self).__name__} field 'host_ip' must be a string.")

        if isinstance(self.port, int):
            pass
        elif isinstance(self.port, str):
            if self.port.isdigit():
                self.port = int(self.port)
            else:
                raise RuntimeError(f"{type(self).__name__} field 'port' includes non-digit characters.")
        else:
            raise RuntimeError(f"{type(self).__name__} field 'port' must be an int or a string.")

    @classmethod
    def get_prefix(cls) -> str:
        return "runtime_api"

Ancestors

Class variables

var is_inside_test

Inherited from: Settings.is_inside_test

True if we are inside a test.

var process_timestamp

Inherited from: Settings.process_timestamp

Unique UUIDv7-based timestamp set during the Python process launch.

Static methods

def get_prefix() -> str

Inherited from: Settings.get_prefix

Dynaconf fields will be filtered by ‘prefix_’ before being passed to the settings class constructor …

def get_project_root() -> str

Inherited from: Settings.get_project_root

Returns absolute path of the directory containing .env file, and if not present the directory containing the first Dynaconf settings file found. Error …

def instance() -> Self

Inherited from: Settings.instance

Return singleton instance.

def normalize_path(field_name: str, field_value: str | None) -> str

Inherited from: Settings.normalize_path

Convert to absolute path if path relative to the location of .env or Dynaconf file is specified.

def normalize_paths(field_name: str, field_value: Iterable[str] | str | None) -> List[str]

Inherited from: Settings.normalize_paths

Convert to absolute path if path relative to the location of .env or Dynaconf file is specified and convert to list if single value is specified.

Fields

var host_ip -> str

REST API host IP (either host name or IP can be used to access the API).

var host_name -> str

REST API host name (either host name or IP can be used to access the API).

var port -> int

REST API port.

Methods

def init(self) -> None

Same as init but can be used when field values are set both during and after construction.