Files
motia-iii/steps/advoware_proxy/advoware_api_proxy_post_step.py

62 lines
1.8 KiB
Python

"""Advoware API Proxy - POST requests"""
from typing import Any
from motia import FlowContext, http, ApiRequest, ApiResponse
from services.advoware import AdvowareAPI
config = {
"name": "Advoware Proxy POST",
"description": "Universal proxy for Advoware API (POST requests)",
"flows": ["advoware-proxy"],
"triggers": [
http("POST", "/advoware/proxy")
],
"enqueues": [],
}
async def handler(request: ApiRequest, ctx: FlowContext[Any]) -> ApiResponse:
"""
Proxy POST requests to Advoware API.
Query parameters:
- endpoint: Advoware API endpoint (required)
- any other params are forwarded to Advoware
Body: JSON payload to forward to Advoware
"""
try:
# Extract endpoint from query parameters
endpoint = request.query_params.get('endpoint', '')
if not endpoint:
return ApiResponse(
status=400,
body={'error': 'Endpoint required as query parameter'}
)
# Initialize Advoware client
advoware = AdvowareAPI(ctx)
# Forward all query params except 'endpoint'
params = {k: v for k, v in request.query_params.items() if k != 'endpoint'}
# Get request body
json_data = request.body
ctx.logger.info(f"Proxying POST request to Advoware: {endpoint}")
result = await advoware.api_call(
endpoint,
method='POST',
params=params,
json_data=json_data
)
return ApiResponse(status=200, body={'result': result})
except Exception as e:
ctx.logger.error(f"Proxy error: {e}")
return ApiResponse(
status=500,
body={'error': 'Internal server error', 'details': str(e)}
)