Add mockapi source files and tests
This commit is contained in:
@@ -51,7 +51,7 @@ def validate(spec_file: str, fmt: Optional[str]):
|
|||||||
click.echo(f" - {error}", err=True)
|
click.echo(f" - {error}", err=True)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
click.echo("✓ Specification is valid!")
|
click.echo("\u2713 Specification is valid!")
|
||||||
click.echo(f" Paths: {len(spec.get('paths', {}))}")
|
click.echo(f" Paths: {len(spec.get('paths', {}))}")
|
||||||
click.echo(f" Schemas: {len(spec.get('components', {}).get('schemas', {}))}")
|
click.echo(f" Schemas: {len(spec.get('components', {}).get('schemas', {}))}")
|
||||||
|
|
||||||
@@ -62,53 +62,13 @@ def validate(spec_file: str, fmt: Optional[str]):
|
|||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
@click.argument("spec_file", type=click.Path(exists=True))
|
@click.argument("spec_file", type=click.Path(exists=True))
|
||||||
@click.option(
|
@click.option("--port", "-p", type=int, default=None, help="Port to run the mock server on")
|
||||||
"--port",
|
@click.option("--host", "-h", default=None, help="Host to bind to")
|
||||||
"-p",
|
@click.option("--delay", "-d", type=int, default=None, help="Fixed response delay in milliseconds")
|
||||||
type=int,
|
@click.option("--random-delay", is_flag=True, default=None, help="Use random delays")
|
||||||
default=None,
|
@click.option("--config", "-c", type=click.Path(exists=True), default=None, help="Path to mockapi.yaml")
|
||||||
help="Port to run the mock server on (default: from config or 8080)",
|
@click.option("--watch", "-w", is_flag=True, default=False, help="Enable hot-reload")
|
||||||
)
|
@click.option("--verbose", "-v", is_flag=True, default=False, help="Enable verbose output")
|
||||||
@click.option(
|
|
||||||
"--host",
|
|
||||||
"-h",
|
|
||||||
default=None,
|
|
||||||
help="Host to bind to (default: from config or 0.0.0.0)",
|
|
||||||
)
|
|
||||||
@click.option(
|
|
||||||
"--delay",
|
|
||||||
"-d",
|
|
||||||
type=int,
|
|
||||||
default=None,
|
|
||||||
help="Fixed response delay in milliseconds",
|
|
||||||
)
|
|
||||||
@click.option(
|
|
||||||
"--random-delay",
|
|
||||||
is_flag=True,
|
|
||||||
default=None,
|
|
||||||
help="Use random delays instead of fixed",
|
|
||||||
)
|
|
||||||
@click.option(
|
|
||||||
"--config",
|
|
||||||
"-c",
|
|
||||||
type=click.Path(exists=True),
|
|
||||||
default=None,
|
|
||||||
help="Path to mockapi.yaml configuration file",
|
|
||||||
)
|
|
||||||
@click.option(
|
|
||||||
"--watch",
|
|
||||||
"-w",
|
|
||||||
is_flag=True,
|
|
||||||
default=False,
|
|
||||||
help="Enable hot-reload on spec file changes",
|
|
||||||
)
|
|
||||||
@click.option(
|
|
||||||
"--verbose",
|
|
||||||
"-v",
|
|
||||||
is_flag=True,
|
|
||||||
default=False,
|
|
||||||
help="Enable verbose output",
|
|
||||||
)
|
|
||||||
def start(
|
def start(
|
||||||
spec_file: str,
|
spec_file: str,
|
||||||
port: Optional[int],
|
port: Optional[int],
|
||||||
@@ -119,10 +79,7 @@ def start(
|
|||||||
watch: bool,
|
watch: bool,
|
||||||
verbose: bool,
|
verbose: bool,
|
||||||
):
|
):
|
||||||
"""Start a mock API server from an OpenAPI specification.
|
"""Start a mock API server from an OpenAPI specification."""
|
||||||
|
|
||||||
SPEC_FILE: Path to the OpenAPI spec file (YAML or JSON)
|
|
||||||
"""
|
|
||||||
try:
|
try:
|
||||||
cfg = Config.load(config_path=config)
|
cfg = Config.load(config_path=config)
|
||||||
|
|
||||||
@@ -148,7 +105,6 @@ def start(
|
|||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
click.echo(f"Starting mock server on {cfg.host}:{cfg.port}")
|
click.echo(f"Starting mock server on {cfg.host}:{cfg.port}")
|
||||||
click.echo(f"Spec file: {spec_file}")
|
|
||||||
|
|
||||||
generator = MockServerGenerator(spec, cfg)
|
generator = MockServerGenerator(spec, cfg)
|
||||||
app = generator.generate()
|
app = generator.generate()
|
||||||
@@ -159,12 +115,7 @@ def start(
|
|||||||
reloader.start_watching()
|
reloader.start_watching()
|
||||||
else:
|
else:
|
||||||
import uvicorn
|
import uvicorn
|
||||||
uvicorn.run(
|
uvicorn.run(app, host=cfg.host, port=cfg.port, log_level="info" if verbose else "warning")
|
||||||
app,
|
|
||||||
host=cfg.host,
|
|
||||||
port=cfg.port,
|
|
||||||
log_level="info" if verbose else "warning",
|
|
||||||
)
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
click.echo(f"Error: {e}", err=True)
|
click.echo(f"Error: {e}", err=True)
|
||||||
@@ -176,18 +127,9 @@ def start(
|
|||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
@click.argument("spec_file", type=click.Path(exists=True))
|
@click.argument("spec_file", type=click.Path(exists=True))
|
||||||
@click.option(
|
@click.option("--output", "-o", type=click.Path(), default=None, help="Output file path")
|
||||||
"--output",
|
|
||||||
"-o",
|
|
||||||
type=click.Path(),
|
|
||||||
default=None,
|
|
||||||
help="Output file path (default: stdout)",
|
|
||||||
)
|
|
||||||
def generate(spec_file: str, output: Optional[str]):
|
def generate(spec_file: str, output: Optional[str]):
|
||||||
"""Generate code/structure from an OpenAPI spec (dry-run mode).
|
"""Generate code from an OpenAPI spec (dry-run mode)."""
|
||||||
|
|
||||||
SPEC_FILE: Path to the OpenAPI spec file (YAML or JSON)
|
|
||||||
"""
|
|
||||||
try:
|
try:
|
||||||
loader = SpecLoader(spec_file)
|
loader = SpecLoader(spec_file)
|
||||||
spec = loader.load()
|
spec = loader.load()
|
||||||
@@ -238,15 +180,9 @@ def generate(spec_file: str, output: Optional[str]):
|
|||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
@click.option(
|
@click.option("--config", "-c", type=click.Path(exists=True), default=None, help="Path to mockapi.yaml")
|
||||||
"--config",
|
|
||||||
"-c",
|
|
||||||
type=click.Path(exists=True),
|
|
||||||
default=None,
|
|
||||||
help="Path to mockapi.yaml configuration file",
|
|
||||||
)
|
|
||||||
def show_config(config: Optional[str]):
|
def show_config(config: Optional[str]):
|
||||||
"""Show the current configuration (from file and defaults)."""
|
"""Show the current configuration."""
|
||||||
try:
|
try:
|
||||||
cfg = Config.load(config_path=config)
|
cfg = Config.load(config_path=config)
|
||||||
click.echo("Current MockAPI Configuration:")
|
click.echo("Current MockAPI Configuration:")
|
||||||
@@ -262,4 +198,4 @@ def show_config(config: Optional[str]):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
cli()
|
cli()
|
||||||
|
|||||||
Reference in New Issue
Block a user