Skip to main content

A lightweight HTTP router library providing bare minimum routing functionality, designed for easy embedding within other applications.

Project description

Hammal

PyPI - Version

Hammal is a fast, minimalist Python web framework inspired by Echo. It is designed to be simple and easy to embed within other applications, providing the bare minimum router functionality.

Installation

Install Hammal using pip:

pip install hammal

Quick Start

Here's how you can get started with Hammal:

Example: Hello World

import json
from hammal import Hammal, RequestContext

def hello_handler(context: RequestContext) -> None:
    context.response.body = json.dumps({"message": "Hello, World!"})

router = Hammal()
router.get("/", hello_handler)
router.start()

Run the script and visit http://localhost:8000 in your browser to see "Hello, World!".

Example: Handling Parameters

def greet_handler(context: RequestContext) -> None:
    name = context.path_params.get("name", "Guest")
    context.response.body = json.dumps({"message": f"Hello, {name}!"})

router.get("/greet/:name", greet_handler)

Accessing http://localhost:8080/greet/Pikachu will return "Hello, Pikachu!".

Example: Middleware

import logging

def logging_middleware(context: RequestContext) -> bool:
    logging.info(f"{context.method} request for {context.path} with body: {context.body}")
    return True

def auth_middleware(context: RequestContext) -> bool:
    token = context.headers.get("Authorization")
    if not token == MONITORING_API_KEY:
        context.response.status = 401
        return False
    return True

router.use(logging_middleware)
router.use(auth_middleware)

Example: Extensibility

Hammal is designed to be extensible, allowing you to integrate it into other applications seamlessly. Here's an example of extending Hammal:

from hammal import Hammal

class BotWithHammal(Hammal):
    def __init__(self, bot, *args, **kwargs):
        self.bot = bot
        super().__init__(*args, **kwargs)

    def start_async(self, *args, **kwargs):
        super().start_async(*args, **kwargs)
        self.bot.polling(none_stop=True, timeout=60)

app = BotWithHammal(bot)
# add routes and middlewares
app.start_async()

More Examples

The examples directory.

Contributing

Contributions are welcome! Feel free to fork the project and submit a pull request.

License

Hammal is open-source software licensed under the MIT License.

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

hammal-0.1.3.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

hammal-0.1.3-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file hammal-0.1.3.tar.gz.

File metadata

  • Download URL: hammal-0.1.3.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.13

File hashes

Hashes for hammal-0.1.3.tar.gz
Algorithm Hash digest
SHA256 7761875e795997e6c70182913a38d77c100dca986123f65f8ef745c801d80c6a
MD5 ca770bb403fbe7228123e9f3d65895de
BLAKE2b-256 e697dbd79c404aa0e6722bd8e7f48fa6bced1d083d8ac1615e2d344def11293b

See more details on using hashes here.

File details

Details for the file hammal-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: hammal-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.13

File hashes

Hashes for hammal-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 73a9360043f31ecd16257f32b1969e2e2e387bf9d2b86d99f36c801d87017abb
MD5 8523ab43d0a34fe54ba5490024f5c1d0
BLAKE2b-256 e443898c787eff846dc94419bef1cacc5099f7bb905cc1c7df5a94e261076bfa

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