import { File, Text } from "@asyncapi/generator-react-sdk";
import { PythonGenerator, FormatHelpers } from "@asyncapi/modelina";
export default async function({ asyncapi, params, originalAsyncAPI }) {
const files = [];
// Generate Python data models using Modelina
const pythonGenerator = new PythonGenerator();
const models = await pythonGenerator.generate(asyncapi);
// Create models.py file
const modelsContent = models.map(model =>
`# ${model.modelName}\n${model.result}\n`
).join('\n');
files.push(
"""Auto-generated Python models from AsyncAPI spec."""
{modelsContent}
);
// Generate async client
files.push(
{generateClientCode(asyncapi, params)}
);
return files;
}
function generateClientCode(asyncapi, params) {
const clientName = params.client_name || "AsyncClient";
const servers = asyncapi.servers().all();
let code = `"""Auto-generated async client for ${asyncapi.info().title()}"""\n\n`;
code += `import asyncio\nimport aiohttp\n\n`;
code += `class ${clientName}:\n`;
code += ` """Async client for ${asyncapi.info().title()}"""\n\n`;
code += ` def __init__(self, host: str):\n`;
code += ` self.host = host\n`;
code += ` self.session = None\n\n`;
// Add methods for each channel/operation
asyncapi.channels().all().forEach(channel => {
const channelName = FormatHelpers.toCamelCase(channel.address() || channel.name());
code += ` async def ${channelName}_publish(self, payload):\n`;
code += ` """Publish to ${channel.name()}"""\n`;
code += ` if not self.session:\n`;
code += ` self.session = aiohttp.ClientSession()\n`;
code += ` # Implementation for ${channel.name()}\n\n`;
});
return code;
}