add example code generator

This commit is contained in:
marys
2026-06-03 10:36:11 +02:00
parent b4ad1d2fd7
commit 03b5f3c2ea
17 changed files with 308 additions and 56 deletions
+24
View File
@@ -0,0 +1,24 @@
{
"name": "@your-org/asyncapi-python-template",
"version": "1.0.0",
"description": "Custom Python async client generator for AsyncAPI",
"generator": {
"supportedProtocols": ["mqtt", "kafka", "nats"],
"parameters": {
"client_name": {
"description": "Name of the generated client class",
"default": "AsyncClient",
"required": false
},
"async_mode": {
"description": "Enable async/await support",
"default": true,
"required": false
}
}
},
"dependencies": {
"@asyncapi/generator-react-sdk": "^1.1.0",
"@asyncapi/modelina": "^4.0.0"
}
}
+8
View File
@@ -0,0 +1,8 @@
module.exports = {
'generate:before': ({ asyncapi, templateParams = {} }) => {
// Modify the AsyncAPI document before generation
if (templateParams.version) {
asyncapi._json.info.version = templateParams.version;
}
}
};
+56
View File
@@ -0,0 +1,56 @@
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;
}
+8
View File
@@ -0,0 +1,8 @@
{
"name": "python-async-template",
"generator": {},
"dependencies": {
"@asyncapi/generator-react-sdk": "^1.1.0",
"@asyncapi/modelina": "^4.0.0"
}
}