Initial upload: API Mock CLI v0.1.0
Some checks failed
CI / test (3.10) (push) Has been cancelled
CI / test (3.11) (push) Has been cancelled
CI / test (3.12) (push) Has been cancelled
CI / test (3.9) (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / type-check (push) Has been cancelled
CI / build (push) Has been cancelled
Some checks failed
CI / test (3.10) (push) Has been cancelled
CI / test (3.11) (push) Has been cancelled
CI / test (3.12) (push) Has been cancelled
CI / test (3.9) (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / type-check (push) Has been cancelled
CI / build (push) Has been cancelled
This commit is contained in:
84
src/cli/commands/generate.py
Normal file
84
src/cli/commands/generate.py
Normal file
@@ -0,0 +1,84 @@
|
||||
from pathlib import Path
|
||||
import click
|
||||
from src.utils.file import FileUtils
|
||||
|
||||
|
||||
@click.command("generate")
|
||||
@click.argument("name")
|
||||
@click.option(
|
||||
"--method",
|
||||
type=click.Choice(["GET", "POST", "PUT", "DELETE", "PATCH"]),
|
||||
default="GET",
|
||||
help="HTTP method"
|
||||
)
|
||||
@click.option(
|
||||
"--path",
|
||||
type=str,
|
||||
default=None,
|
||||
help="Endpoint path"
|
||||
)
|
||||
@click.option(
|
||||
"--status-code",
|
||||
type=int,
|
||||
default=200,
|
||||
help="Response status code"
|
||||
)
|
||||
@click.option(
|
||||
"--output",
|
||||
type=click.Path(),
|
||||
default="endpoints.yaml",
|
||||
help="Output file"
|
||||
)
|
||||
@click.option(
|
||||
"--interactive/--no-interactive",
|
||||
default=True,
|
||||
help="Interactive mode for response template"
|
||||
)
|
||||
def generate_command(
|
||||
name: str,
|
||||
method: str,
|
||||
path: str,
|
||||
status_code: int,
|
||||
output: str,
|
||||
interactive: bool
|
||||
):
|
||||
endpoint_path = path or f"/api/{name.lower().replace(' ', '-')}"
|
||||
endpoint = {
|
||||
"path": endpoint_path,
|
||||
"method": method,
|
||||
"name": name,
|
||||
"response": {
|
||||
"status_code": status_code,
|
||||
"body": {
|
||||
"message": "Success",
|
||||
"data": {}
|
||||
}
|
||||
}
|
||||
}
|
||||
response_body = {}
|
||||
if interactive:
|
||||
key = click.prompt("Response body key (or 'done' to finish)", default="done")
|
||||
while key != "done":
|
||||
value_type = click.prompt("Value type (string, number, boolean, array, object)", default="string")
|
||||
if value_type == "string":
|
||||
response_body[key] = "sample_value"
|
||||
elif value_type == "number":
|
||||
response_body[key] = 123
|
||||
elif value_type == "boolean":
|
||||
response_body[key] = True
|
||||
elif value_type == "array":
|
||||
response_body[key] = []
|
||||
elif value_type == "object":
|
||||
response_body[key] = {}
|
||||
response_body[key] = endpoint["response"]["body"].get(key, {})
|
||||
key = click.prompt("Response body key (or 'done' to finish)", default="done")
|
||||
existing_data = {"endpoints": []}
|
||||
if Path(output).exists():
|
||||
existing_data = FileUtils.load_yaml(output)
|
||||
if "endpoints" not in existing_data:
|
||||
existing_data["endpoints"] = []
|
||||
endpoint["response"]["body"] = response_body if interactive else endpoint["response"]["body"]
|
||||
existing_data["endpoints"].append(endpoint)
|
||||
FileUtils.write_yaml(output, existing_data)
|
||||
click.echo(f"Generated endpoint: {method} {endpoint_path}")
|
||||
click.echo(f"Saved to: {output}")
|
||||
Reference in New Issue
Block a user