Skip to main content

CLI tool to manage custom API protocols for Kaede API Creation package.

Project description

Kaede Protocol Manager

License: MIT Development Status :: 3 - Alpha Programming Language :: Python :: 3

A CLI tool to manage custom API protocols for the Kaede API Creation package.

The Kaede Protocol Manager (kaede-protocol-manager) is a command-line interface (CLI) utility designed to streamline the installation and management of custom API protocols. It works in conjunction with the Kaede API Creation package (kaede-api-creation) by providing a standardized way to package, install, and verify custom protocol definitions.

Features

  • Compressed Protocol Installation: Installs custom API protocols from a single, compressed .kprotocol file (.zip archive).
  • Protocol Initialization: Offers a command to initialize and perform basic validation of an installed protocol, ensuring it is correctly set up.
  • Bundled Protocol Definitions: Protocols are packaged as .kprotocol files, which are .zip archives containing all necessary definition files (data.json, request_parser.py, response_formatter.py).
  • Structured Data Storage (data.json): Protocol metadata, headers, and status codes are stored in a structured data.json file within the .kprotocol archive.
  • Python Logic Encapsulation: Protocol-specific request parsing and response formatting logic are contained within request_parser.py and response_formatter.py files within the .kprotocol archive.
  • Simple CLI Interface: Provides an intuitive command-line interface for managing protocols.

Installation

To install kaede-protocol-manager, navigate to the directory containing the setup.py file and execute the following command using pip:

pip install .

This command will install the kaede-protocol-manager package and make the kaede-protocol-manager command available in your system's command-line environment.

Usage

The kaede-protocol-manager CLI provides the following commands for managing custom API protocols:

install protocol

Installs a new API protocol from a .kprotocol file.

Usage:

kaede-protocol-manager install protocol --file <protocol_file_path> --name <protocol_name>

Options:

  • protocol: Specifies that you are installing a protocol (currently the only supported resource type).
  • --file <protocol_file_path>: (Required) The path to the .kprotocol file (which is a .zip archive) containing the protocol definition.
  • --name <protocol_name>: (Required) The name you wish to assign to this protocol. This name will be used to reference the protocol when creating APIs with kaede-api-creation.

Example:

kaede-protocol-manager install protocol --file my_custom_protocol.kprotocol --name my-protocol

This command will install the protocol defined in my_custom_protocol.kprotocol and name it my-protocol. The protocol definition will be extracted to the ~/.kaede_protocols/my-protocol directory.

initialize protocol

Initializes and validates an installed protocol. This command performs basic checks to ensure that the protocol directory exists and that essential files (data.json, request_parser.py, response_formatter.py) are present after installation.

Usage:

kaede-protocol-manager initialize protocol --name <protocol_name>

Options:

  • protocol: Specifies that you are initializing a protocol (currently the only supported resource type).
  • --name <protocol_name>: (Required) The name of the protocol to initialize.

Example:

kaede-protocol-manager initialize protocol --name my-protocol

This command will initialize the protocol named my-protocol, verifying the presence of the protocol directory and required definition files.

Creating a .kprotocol File

A .kprotocol file is a .zip archive that bundles all the necessary components of a custom API protocol. To create a .kprotocol file, you need to organize your protocol definition files into a specific directory structure and then compress them into a .zip archive, renaming the archive to have a .kprotocol extension.

Structure of a .kprotocol Archive:

A .kprotocol archive must contain the following files at the root level:

<protocol_name>.kprotocol (ZIP Archive)
│
└── data.json
│   └── Contains protocol metadata, headers, and status codes in JSON format.
│
└── request_parser.py
│   └── Python file defining the `parse_kaede_request` function.
│
└── response_formatter.py
    └── Python file defining the `format_kaede_response` function.

File Descriptions within the .kprotocol Archive:

  • data.json: This file is a JSON document that defines the structured data for your protocol. It should contain the following top-level keys (all are required):

    {
      "PROTOCOL": {
        "name": "<protocol_name>",        // (Required) - Name of the protocol (must match installation name in most cases)
        "version": "<protocol_version>",    // (Required) - Version of the protocol (e.g., "1.0", "v2")
        "description": "<protocol_description>" // (Optional) - Human-readable description
      },
      "HEADERS": {                      // (Optional) - Default headers for responses
        "<header_name_1>": "<header_value_1>",
        "<header_name_2>": "<header_value_2>",
        // ... more headers ...
      },
      "STATUS_CODES": {                 // (Optional) - Custom status codes
        "<status_code_number_1>": "<status_code_name_1>", // Status code number (string) and name
        "<status_code_number_2>": "<status_code_name_2>",
        // ... more status codes ...
      }
    }
    
    • PROTOCOL Block:

      • name: The name of the protocol (string). It is recommended to keep this consistent with the --name used during installation.
      • version: The protocol version (string or number).
      • description (Optional): A human-readable description of the protocol.
    • HEADERS Block (Optional): Defines default headers (key-value pairs) that will be included in responses when using this protocol.

    • STATUS_CODES Block (Optional): Defines custom status codes (key-value pairs where keys are status code numbers as strings, and values are status code names).

  • request_parser.py: This Python file must define a function named parse_kaede_request. This function is responsible for parsing raw request data (received by the server) and converting it into a Request object that can be understood by your API handlers.

    # Example content of request_parser.py
    class MyProtocolRequest: # Custom Request class (optional, but recommended)
        def __init__(self, method, path, headers, body):
            self.method = method
            self.path = path
            self.headers = headers
            self.body = body
    
    def parse_kaede_request(raw_request_data):
        """Parses raw request data and returns a Request object."""
        # ... (Your protocol's parsing logic here) ...
        request = MyProtocolRequest(method="GET", path="/", headers={}, body="") # Example
        return request
    
  • response_formatter.py: This Python file must define a function named format_kaede_response. This function takes a Response object (returned by your API handlers) and formats it into bytes that can be sent back as a response over the network.

    # Example content of response_formatter.py
    def format_kaede_response(response):
        """Formats a Response object into bytes for sending."""
        # ... (Your protocol's formatting logic here) ...
        response_bytes = b"Example Response Data" # Example
        return response_bytes
    

Steps to Create a .kprotocol File:

  1. Create a directory (e.g., my_protocol_source/).
  2. Inside this directory, create:
    • data.json (with your protocol's metadata, headers, status codes).
    • request_parser.py (with your parse_kaede_request function).
    • response_formatter.py (with your format_kaede_response function).
  3. Compress the contents of my_protocol_source/ into a .zip archive. Do not zip the directory itself, but the files inside it.
  4. Rename the created .zip archive to have a .kprotocol extension (e.g., my_protocol.kprotocol).

You can then use this .kprotocol file to install your custom protocol using the kaede-protocol-manager install protocol command.

For Developers

Contributions to kaede-protocol-manager are welcome! Please refer to the project's repository on GitHub for contribution guidelines, issue tracking, and to submit pull requests.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Author

Kaede Dev Kento Hinode - cleaverdeath@gmail.com

GitHub Repository

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kaede_protocol_manager-0.1.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kaede_protocol_manager-0.1.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file kaede_protocol_manager-0.1.0.tar.gz.

File metadata

  • Download URL: kaede_protocol_manager-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for kaede_protocol_manager-0.1.0.tar.gz
Algorithm Hash digest
SHA256 605d377c533af681523bc4c118088d4b766b12c69bace637d41946efc1d0e33b
MD5 2eb7a02dfdf13284a242c33e9b2ad0f5
BLAKE2b-256 d4311d1cff665fab6a9b50d3cefba1462c87752fb8f6b76ae84009955c2a30b0

See more details on using hashes here.

File details

Details for the file kaede_protocol_manager-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for kaede_protocol_manager-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6a882fad00120d085157b5a5ca97318d41a78e7e6799d31970e4c9cb79385972
MD5 07601a4000b9d59091a50ae178e94f04
BLAKE2b-256 263cb6c0b849f19c7c14ad7c23c15573f2f0c0f22562e6ed1547aaeb0958cefb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page