import os
import yaml
import anthropic
import re
import claudeapi_key
CLAUDE_PROMPT_TEMPLATE = """
You are a backend software engineer. Given the following OpenAPI 3.0 YAML specification, generate a complete C# .NET 8.0 Web API microservice project.
The project should be production-ready and must include:
A .csproj file with necessary packages
Program.cs using minimal hosting model in .NET 8 including swagger support and auto-open in browser
appsettings.json with default config
Models/ folder with all data models defined in the YAML
Controllers/ folder with REST endpoints for all operations in the paths section
Input validation based on the schema (required, format, etc.)
Proper response codes and JSON formatting
Use appropriate attributes like [ApiController], [HttpPost], [FromBody], etc.
The final output should be structured as clearly labeled code blocks with file names. For example:
import os import yaml import anthropic import re import claudeapi_key
CLAUDE_PROMPT_TEMPLATE = """ You are a backend software engineer. Given the following OpenAPI 3.0 YAML specification, generate a complete C# .NET 8.0 Web API microservice project.
The project should be production-ready and must include:
.csproj
file with necessary packagesProgram.cs
using minimal hosting model in .NET 8 including swagger support and auto-open in browserappsettings.json
with default configModels/
folder with all data models defined in the YAMLControllers/
folder with REST endpoints for all operations in thepaths
sectionrequired
,format
, etc.)[ApiController]
,[HttpPost]
,[FromBody]
, etc.The final output should be structured as clearly labeled code blocks with file names. For example:
// Program.cs
// Models/User.cs
"""
INPUT_DIR = "C:/Users/hiren/DotNetAPIGenerator/" OUTPUT_DIR = "C:/Users/hiren/DotNetAPIGenerator/GeneratedApi/Claude_generated_services_v3" CLAUDE_API_KEY = claudeapi_key.apikey
client = anthropic.Anthropic(api_key=CLAUDE_API_KEY)
def read_yaml_file(filepath): with open(filepath, "r", encoding="utf-8") as file: return file.read()
def call_claude_api(prompt): response = client.messages.create( model="claude-3-opus-20240229", # Change model as per availability max_tokens=4000, temperature=0.2, system="You are a senior .NET backend engineer.", messages=[{"role": "user", "content": prompt}] ) return response.content[0].text
def clean_filename(raw_filename):
Extract only the first line and strip extra characters
def save_code_blocks(output_text, service_name): service_dir = os.path.join(OUTPUT_DIR, service_name) os.makedirs(service_dir, exist_ok=True)
def enhance_generated_code(service_dir):
=== Inject Swagger support into Program.cs ===
def main(): os.makedirs(OUTPUT_DIR, exist_ok=True)
import webbrowser
def open_swagger_ui(port="5001"): url = f"https://localhost:{port}/swagger" print(f"š Launching Swagger UI: {url}") webbrowser.open(url)
import subprocess
def run_dotnet_project(project_dir): print(f"š Running project at: {project_dir}") subprocess.Popen(["dotnet", "run"], cwd=project_dir, shell=True)
if name == "main": main()