harspec GitHub

Write a generator

A generator is a Python function that receives the OpenAPI spec (a dict) and returns {filename: content}.

Minimal example

from harspec.generators import register

@register("routes", "List of METHOD /path lines")
def routes(spec):
    lines = []
    for path, item in spec["paths"].items():
        for method in item:
            lines.append(f"{method.upper()} {path}")
    return {"routes.txt": "\n".join(lines)}

Run harspec capture.har -f routes and you get routes.txt.

Helpers

Import from harspec.generators.base:

HelperReturns
iter_ops(spec)Yields (path, METHOD, operation) for every endpoint.
base_url(spec)The server URL.
params_of(op, "path" | "query")Parameter objects with name, required, schema, example.
body_of(op)(mime, schema, example) or None.
success_schema(op)Schema of the first 2xx JSON response, or None.
auth_style(spec)(header_name, "Bearer {}") style tuple, or None.
snake(name)getUserPosts to get_user_posts.

Add it to harspec itself

  1. Create harspec/generators/yourname.py with the decorator above.
  2. Add yourname to the import line in load_all().
  3. Write a test that generates from examples/sample.har and checks the output.
  4. Open a pull request.

Ship it as a plugin package

You do not need to touch harspec. Publish your own package that registers through an entry point:

# pyproject.toml of your plugin
[project]
name = "harspec-go"
dependencies = ["harspec"]

[project.entry-points."harspec.generators"]
go = "harspec_go.generator"

After pip install harspec-go, the generator shows up in harspec --list-generators and works with -f go.

Ideas wanted

Go, Rust, Kotlin, Swift, Dart, Java, C#, PHP and Ruby clients. Bruno and Insomnia collections. k6 load tests. Playwright API tests. WireMock and MSW mock servers. Zod and Pydantic models. Markdown API docs.