Skip to content

fake-api-server.ci.surveillance.model.config.api_config

This module provides data models and utilities for managing API documentation configuration settings for section fake-api-server, handling subcommand-line arguments, and deserializing data into structured configurations used in a fake API server.

BaseArgsAdapter() dataclass

BaseArgsAdapter serves as an abstract base class for adapting arguments in a consistent manner with a specific subcommand model.

This class is designed to enforce the implementation of the to_subcmd_model method in all derived classes, ensuring that they convert arguments to a ParserArguments instance. It is intended to be subclassed and used in contexts where argument transformation and adherence to a particular subcommand model are required.

ATTRIBUTE DESCRIPTION
__abstractmethods__

Defines abstract methods that must be implemented by any subclass deriving from BaseArgsAdapter.

FakeAPIConfigSetting(server_type=str(), subcmd=dict()) dataclass

Bases: _BaseModel

Represents the configuration settings for a fake API.

This class provides the structure and logic for handling and deserializing configuration settings related to a fake API system. It includes attributes for specifying the type of server and subcommands with their respective configuration details. The class supports deserialization from a given mapping to produce a fully-initialized configuration setting object.

ATTRIBUTE DESCRIPTION
server_type

Specifies the type of the server.

TYPE: str

subcmd

Maps the subcommand enums to their corresponding configuration details.

TYPE: Dict[SubCommandLine, SubCmdConfig]

PullApiDocConfigArgs(config_path='./api.yaml', include_template_config=False, base_file_path='./', base_url='', dry_run=False, divide_api=False, divide_http=False, divide_http_request=False, divide_http_response=False) dataclass

Bases: _BaseModel, BaseArgsAdapter

Configuration arguments for pulling API documentation.

This dataclass is designed to encapsulate the configuration details required to pull API documentation. It includes settings for config paths, base URLs, division of API elements, and other related parameters. This class also provides methods to deserialize data into an instance of the class and to convert it into a subcommand-compatible model format.

ATTRIBUTE DESCRIPTION
config_path

The file path to the API configuration YAML file.

TYPE: str

include_template_config

Boolean flag indicating whether to include the template configuration in the process.

TYPE: bool

base_file_path

The path to the base directory for generated files.

TYPE: str

base_url

The base URL for the API.

TYPE: str

dry_run

Boolean flag indicating whether the operation should be a simulation without actual execution.

TYPE: bool

divide_api

Boolean flag indicating whether to divide the API components during the process.

TYPE: bool

divide_http

Boolean flag indicating whether to divide HTTP components in the API process.

TYPE: bool

divide_http_request

Boolean flag indicating whether to divide the HTTP request parts of the API.

TYPE: bool

divide_http_response

Boolean flag indicating whether to divide the HTTP response parts of the API.

TYPE: bool

to_subcmd_model()

Transforms and maps the internal configuration objects and attributes into a SubcmdPullArguments model. This method provides the necessary arguments and configuration for constructing a pull command subparser model and ensures appropriate data is passed for command execution.

RETURNS DESCRIPTION
SubcmdPullArguments

A SubcmdPullArguments object that encapsulates the required and optional data for executing the Pull sub-command.

Source code in fake_api_server_plugin/ci/surveillance/model/config/api_config.py
def to_subcmd_model(self) -> SubcmdPullArguments:
    """
    Transforms and maps the internal configuration objects and attributes into
    a `SubcmdPullArguments` model. This method provides the necessary arguments
    and configuration for constructing a pull command subparser model and
    ensures appropriate data is passed for command execution.

    :rtype: SubcmdPullArguments
    :return: A `SubcmdPullArguments` object that encapsulates the required
        and optional data for executing the Pull sub-command.
    """
    return SubcmdPullArguments(
        # Unnecessary in Fake-API-Server-Surveillance
        subparser_structure=SysArg(subcmd=SubCommandLine.Pull),
        source="",
        source_file="",
        request_with_https=False,
        # Necessary in Fake-API-Server-Surveillance
        config_path=self.config_path,
        base_file_path=self.base_file_path,
        base_url=self.base_url,
        include_template_config=self.include_template_config,
        divide_api=self.divide_api,
        divide_http=self.divide_http,
        divide_http_request=self.divide_http_request,
        divide_http_response=self.divide_http_response,
        dry_run=self.dry_run,
    )

SubCmdConfig(args) dataclass

Bases: _BaseModel

Represents the configuration for a sub-command in the application.

The SubCmdConfig class is used for handling sub-command configuration and its conversion to a specific model. It provides functionality to deserialize raw data into a SubCmdConfig instance and to transform the arguments into a sub-command argument model format.

ATTRIBUTE DESCRIPTION
args

List of command-line arguments.

TYPE: List[str]

to_subcmd_args(subcmd_arg_model)

Converts a list of command-line arguments into a model instance by mapping argument keys and values into the appropriate format. This method parses arguments, verifies their validity, and applies them to create and populate an instance of the given model class.

PARAMETER DESCRIPTION
subcmd_arg_model

The model class (BaseArgsAdapter) to which the parsed arguments will be applied. Must subclass BaseArgsAdapter.

TYPE: Type[BaseArgsAdapter]

RETURNS DESCRIPTION
`BaseArgsAdapter`

An instance of the provided subcmd_arg_model populated with values derived from the list of command-line arguments.

Source code in fake_api_server_plugin/ci/surveillance/model/config/api_config.py
def to_subcmd_args(self, subcmd_arg_model: Type[BaseArgsAdapter]) -> BaseArgsAdapter:
    """
    Converts a list of command-line arguments into a model instance by mapping
    argument keys and values into the appropriate format. This method parses
    arguments, verifies their validity, and applies them to create and populate
    an instance of the given model class.

    :param subcmd_arg_model: The model class (`BaseArgsAdapter`) to which the
        parsed arguments will be applied. Must subclass `BaseArgsAdapter`.

    :return: An instance of the provided `subcmd_arg_model` populated with
        values derived from the list of command-line arguments.

    :rtype: `BaseArgsAdapter`
    """
    param_with_key: Dict[str, str] = {}
    for arg in self.args:
        arg_eles = arg.split("=")
        assert len(arg_eles) <= 2, f"Invalid subcmd arg: {arg}"
        arg_with_value = arg_eles if len(arg_eles) == 2 else [arg_eles[0], True]  # type: ignore[list-item]
        assert len(arg_with_value) == 2
        arg_key, arg_value = arg_with_value
        arg_key = arg_key.replace("--", "").replace("-", "_")
        param_with_key[arg_key] = arg_value
    return subcmd_arg_model(**param_with_key)