Files
nats-python/my_template/template/index.js
T
2026-06-03 10:36:11 +02:00

57 lines
1.8 KiB
JavaScript

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(
<File name="models.py">
<Text>"""Auto-generated Python models from AsyncAPI spec."""</Text>
<Text>{modelsContent}</Text>
</File>
);
// Generate async client
files.push(
<File name="client.py">
<Text>{generateClientCode(asyncapi, params)}</Text>
</File>
);
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;
}