# DiHttpMessageProcessor

Bridge library that integrates [EdpDependencyInjection](https://pip.e-deploy.com.br) with [HttpMessageProcessor](https://pip.e-deploy.com.br), enabling dependency-injected HTTP route/processor resolution.

Instead of manually instantiating processors, this library resolves them from a `DependencyContext` at build time. Route definitions accept either a processor type (resolved via DI), a real `MessageProcessor` instance (used directly), or a `RouteDefinition` (auto-wires interactor + processor via DI).

## Installation

```bash
pip install DiHttpMessageProcessor --extra-index-url=https://pip.e-deploy.com.br
```

## Requirements

- Python >= 3.8
- `HttpMessageProcessor >=2.4,<3`
- `EdpDependencyInjection >=3.0,<4`

## Usage

### Basic route builder

Maps HTTP routes to processor types or instances resolved from the DI context. Processor types are instantiated via DI; pre-built instances are returned directly.

```python
from dihttpmessageprocessor import DiRouterProcessorBuilder
from simplehttprouter import Route

builder = DiRouterProcessorBuilder(
    dependency_context=dependency_context,
    route_definition={
        Route("GET", "/users"): ListUsersProcessor,           # type — resolved via DI
        Route("POST", "/users"): already_built_instance,      # instance — used directly
    }
)

found_route, processor = builder.get_processor(Route("GET", "/users"))
```

### Auto-mapped route builder (recommended)

`AutoMappedDiRouterProcessorBuilder` extends the basic builder with support for `RouteDefinition` and `BaseRouteDefinition`, which resolve processors through the DI context. For `RouteDefinition`, the interactor is also resolved via DI and injected into the processor. It also auto-discovers `AutoMappedRouteBuilder` implementations registered in the DI context.

```python
from dihttpmessageprocessor import AutoMappedDiRouterProcessorBuilder, RouteDefinition
from simplehttprouter import Route

builder = AutoMappedDiRouterProcessorBuilder(
    dependency_context=dependency_context,
    route_definition={
        Route("GET", "/orders"): RouteDefinition(
            interactor_type=ListOrdersInteractor,
            request_type=ListOrdersRequest,
            success_status=200,
        ),
        Route("POST", "/orders"): RouteDefinition(
            interactor_type=CreateOrderInteractor,
            request_type=CreateOrderRequest,
            success_status=201,
            exception_map={ValueError: 400},
        ),
        Route("GET", "/health"): HealthCheckProcessor,        # type reference also accepted
        Route("GET", "/status"): health_check_instance,       # pre-built instance also accepted
    }
)

found_route, processor = builder.get_processor(Route("GET", "/orders"))
```

### Auto-mapped builders

Modules can declare their route definitions by implementing `AutoMappedRouteBuilder`. These are automatically discovered by `AutoMappedDiRouterProcessorBuilder` via the DI context.

```python
from dihttpmessageprocessor import AutoMappedRouteBuilder, RouteDefinition
from simplehttprouter import Route

class OrderRouteBuilder(AutoMappedRouteBuilder):
    def build_routes(self):
        return {
            Route("GET", "/orders"): RouteDefinition(
                interactor_type=ListOrdersInteractor,
                request_type=ListOrdersRequest,
            ),
            Route("POST", "/orders"): CreateOrderProcessor,
        }
```

## Testing

```bash
pytest
```

## Authors

- Eduardo Almeida (eduardo.almeida@e-deploy.com.br)
- Max Guenes (max.santos@e-deploy.com.br)