Metadata-Version: 2.4
Name: ulfblk-gateway
Version: 0.1.0
Summary: API Gateway: rate limiting (Redis/in-memory), reverse proxy, and circuit breaker
Project-URL: Homepage, https://github.com/abelardodiaz/web25-991-bloques-reciclables
Project-URL: Documentation, https://github.com/abelardodiaz/web25-991-bloques-reciclables/tree/main/packages/python/ulfblk-gateway
Project-URL: Repository, https://github.com/abelardodiaz/web25-991-bloques-reciclables
Project-URL: Issues, https://github.com/abelardodiaz/web25-991-bloques-reciclables/issues
Author-email: Abelardo Diaz <abelardo@bloques.dev>
License-Expression: MIT
Keywords: async,circuit-breaker,gateway,proxy,rate-limiting
Classifier: Development Status :: 3 - Alpha
Classifier: Framework :: AsyncIO
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Software Development :: Libraries
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: httpx>=0.27
Requires-Dist: ulfblk-core
Provides-Extra: dev
Requires-Dist: fakeredis[lua]>=2.21; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.24; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Description-Content-Type: text/markdown

# ulfblk-gateway

API Gateway con rate limiting (Redis/in-memory), reverse proxy via httpx, y circuit breaker para proteger upstreams.

## Instalacion

```bash
uv add ulfblk-gateway
```

## Rate Limiting

```python
from ulfblk_gateway.rate_limiter import (
    InMemoryBackend,
    RateLimiterMiddleware,
    RateLimiterSettings,
)

settings = RateLimiterSettings(
    requests=100,          # max por ventana
    window_seconds=60,
    exclude_paths=["/health"],
)
backend = InMemoryBackend()

app.add_middleware(RateLimiterMiddleware, settings=settings, backend=backend)
```

Con Redis (soft dependency via ulfblk-redis):

```python
from ulfblk_gateway.rate_limiter import RedisBackend, RateLimiterMiddleware, RateLimiterSettings
from ulfblk_redis import RedisManager

redis = RedisManager()
await redis.connect()

backend = RedisBackend(redis)
app.add_middleware(
    RateLimiterMiddleware,
    settings=RateLimiterSettings(requests=100, window_seconds=60),
    backend=backend,
)
```

## Reverse Proxy

```python
from ulfblk_gateway.proxy import ProxyHandler, ProxyMiddleware, ProxyRoute, ProxySettings

settings = ProxySettings(
    routes=[
        ProxyRoute(path_prefix="/api/users", upstream_url="http://user-service:8001"),
        ProxyRoute(path_prefix="/api/orders", upstream_url="http://order-service:8002"),
    ]
)

handler = ProxyHandler(settings)
await handler.start()

app.add_middleware(ProxyMiddleware, handler=handler)
```

## Circuit Breaker

```python
from ulfblk_gateway.circuit_breaker import CircuitBreaker, CircuitBreakerSettings

cb = CircuitBreaker("user-service", CircuitBreakerSettings(
    failure_threshold=5,
    recovery_timeout=30.0,
    success_threshold=2,
))

# Integrado con proxy middleware
app.add_middleware(
    ProxyMiddleware,
    handler=handler,
    circuit_breakers={"http://user-service:8001": cb},
)
```

## Health Check

```python
from ulfblk_gateway.health import gateway_health_check

status = await gateway_health_check(handler)
# {"gateway": True, "upstream:http://user-service:8001": True}
```

## Dependencias

- `ulfblk-core` (obligatorio)
- `httpx>=0.27` (obligatorio)
- `ulfblk-redis` (opcional, para RedisBackend)

## License

MIT
