Skip to content

feat: add pystapi-schema-generator #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ requires-python = ">=3.11"
dependencies = [
"pystapi-client",
"pystapi-validator",
"pystapi-schema-generator",
"stapi-pydantic",
"stapi-fastapi",
]
Expand Down Expand Up @@ -34,11 +35,18 @@ docs = [
default-groups = ["dev", "docs"]

[tool.uv.workspace]
members = ["pystapi-validator", "stapi-pydantic", "pystapi-client", "stapi-fastapi"]
members = [
"pystapi-client",
"pystapi-validator",
"pystapi-schema-generator",
"stapi-pydantic",
"stapi-fastapi"
]

[tool.uv.sources]
pystapi-client.workspace = true
pystapi-validator.workspace = true
pystapi-schema-generator.workspace = true
stapi-pydantic.workspace = true
stapi-fastapi.workspace = true

Expand Down Expand Up @@ -66,6 +74,7 @@ strict = true
files = [
"pystapi-client/src/pystapi_client/**/*.py",
"pystapi-validator/src/pystapi_validator/**/*.py",
"pystapi-schema-generator/src/pystapi_schema_generator/**/*.py",
"stapi-pydantic/src/stapi_pydantic/**/*.py",
"stapi-fastapi/src/stapi_fastapi/**/*.py"
]
Expand Down
16 changes: 16 additions & 0 deletions pystapi-schema-generator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.0.1] - 2025-04-01

### Added

- A spec product that tightly follows the [STAPI specification](https://github.com/stapi-spec/stapi-spec)
- A STAPI FastAPI application offering the spec product
- A script that creates the FastAPI application to create the OpenAPI schema
1 change: 1 addition & 0 deletions pystapi-schema-generator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# pystapi-schema-generator
26 changes: 26 additions & 0 deletions pystapi-schema-generator/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[project]
name = "pystapi-schema-generator"
version = "0.0.1"
description = "Schema Generator for the Satellite Tasking API (STAPI) Specification"
readme = "README.md"
authors = [
{ name = "Tobias Rohnstock", email = "[email protected]" },
{ name = "Justin Trautmann", email = "[email protected]" },
]
requires-python = ">=3.10"
dependencies = [
"uvicorn>=0.34",
"fastapi>=0.115",
"pydantic>=2.10",
"PyYAML>=6",
"types-PyYAML>=6",
"geojson-pydantic>=1.2",
"stapi-pydantic>=0.0.3",
]

[project.scripts]
stapi-schema-generator = "pystapi_schema_generator.application:main"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from fastapi import FastAPI
from stapi_pydantic import Product

from pystapi_schema_generator.router import RootRouter

router = RootRouter()
router.add_product(
Product(
id="{productId}",
description="An example product",
license="CC-BY-4.0",
links=[],
)
)

app: FastAPI = FastAPI(
openapi_tags=[
{
"name": "Core",
"description": "Core endpoints",
},
{
"name": "Products",
"description": "Products",
},
{
"name": "Orders",
"description": "Endpoint for creating and managing orders",
},
{
"name": "Opportunities",
"description": "Endpoint for viewing and accepting opportunities",
},
]
)
app.include_router(router, prefix="")


def main() -> None:
import argparse

import yaml

parser = argparse.ArgumentParser(description="Generate OpenAPI schema for STAPI")
parser.add_argument(
"--output",
"-o",
default="openapi.yml",
help="Output file path for the OpenAPI schema (default: openapi.yml)",
)
args = parser.parse_args()

with open(args.output, "w") as f:
yaml.dump(app.openapi(), f)

print(f"OpenAPI schema saved to '{args.output}'.")


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any

from fastapi import (
APIRouter,
Request,
Response,
status,
)
from stapi_fastapi.responses import GeoJSONResponse
from stapi_pydantic import (
Conformance,
OpportunityCollection,
OpportunityPayload,
Order,
OrderCollection,
OrderParameters,
OrderPayload,
OrderStatus,
Prefer,
Product,
Queryables,
)

if TYPE_CHECKING:
from .router import RootRouter


class ProductRouter(APIRouter):
def __init__(
self,
product: Product,
root_router: RootRouter,
*args: Any,
**kwargs: Any,
) -> None:
super().__init__(*args, **kwargs)

self.product = product
self.root_router = root_router

# Product endpoints
self.add_api_route(
path="",
endpoint=self.get_product,
methods=["GET"],
tags=["Products"],
summary="describe the product with id `productId`",
description="...",
)

self.add_api_route(
path="/conformance",
endpoint=self.get_conformance,
methods=["GET"],
tags=["Products"],
summary="describe the conformance for a product",
description="...",
)

self.add_api_route(
path="/queryables",
endpoint=self.get_queryables,
methods=["GET"],
tags=["Products"],
summary="describe the queryables for a product",
description="...",
)

self.add_api_route(
path="/order-parameters",
endpoint=self.get_order_parameters,
methods=["GET"],
tags=["Products"],
summary="describe the order parameters for a product",
description="...",
)

# Orders endpoints
self.add_api_route(
path="/orders",
endpoint=self.create_order,
methods=["POST"],
response_class=GeoJSONResponse,
status_code=status.HTTP_201_CREATED,
tags=["Orders"],
summary="create a new order for product with id `productId`",
description="...",
)

self.add_api_route(
path="/orders",
endpoint=self.get_orders,
methods=["GET"],
response_class=GeoJSONResponse,
tags=["Orders"],
summary="get a list of orders for the specific product",
description="...",
)

# Opportunities endpoints
self.add_api_route(
path="/opportunities",
endpoint=self.search_opportunities,
methods=["POST"],
tags=["Opportunities"],
summary="create a new opportunity request for product with id `productId`",
description="...",
)

# Product endpoints
def get_product(self) -> Product:
return None # type: ignore

def get_conformance(self) -> Conformance:
return None # type: ignore

def get_queryables(self) -> Queryables:
return None # type: ignore

def get_order_parameters(self) -> OrderParameters:
return None # type: ignore

# Orders endpoints
def create_order(self, payload: OrderPayload, request: Request, response: Response) -> Order: # type: ignore
return None # type: ignore

def get_orders(self, request: Request, next: str | None = None, limit: int = 10) -> OrderCollection[OrderStatus]:
return None # type: ignore

def get_opportunity_collection(self, opportunity_collection_id: str, request: Request) -> OpportunityCollection: # type: ignore
return None # type: ignore

def search_opportunities(
self,
search: OpportunityPayload,
request: Request,
response: Response,
prefer: Prefer | None,
) -> OpportunityCollection: # type: ignore
return None # type: ignore
Empty file.
Loading