Skip to content
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

Documenting webhooks #553

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
38 changes: 37 additions & 1 deletion flask_smorest/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Api extension initialization"""
from collections import OrderedDict

from webargs.flaskparser import abort # noqa

Expand All @@ -7,7 +8,11 @@
from .pagination import Page # noqa
from .error_handler import ErrorHandlerMixin
from .globals import current_api # noqa
from .utils import PrefixedMappingProxy, normalize_config_prefix
from .utils import (
PrefixedMappingProxy,
normalize_config_prefix,
resolve_schema_instance,
)

__version__ = "0.42.1"

Expand Down Expand Up @@ -115,3 +120,34 @@

# Add tag relative to this resource to the global tag list
self.spec.tag({"name": blp_name, "description": blp.description})

def add_webhook_doc(
self,
endpoint: str,
schema,
request_description: str,
response_description: str,
response_status_code: int = 200,
):
"""Register a webhook along with a schema."""
instance = resolve_schema_instance(schema)
schema_title = instance.__class__.__name__.split("Schema")[0]

Check warning on line 134 in flask_smorest/__init__.py

View check run for this annotation

Codecov / codecov/patch

flask_smorest/__init__.py#L133-L134

Added lines #L133 - L134 were not covered by tests

self.spec.components.schema(schema_title, schema=schema)

Check warning on line 136 in flask_smorest/__init__.py

View check run for this annotation

Codecov / codecov/patch

flask_smorest/__init__.py#L136

Added line #L136 was not covered by tests

webhooks = self.spec.options.setdefault("webhooks", OrderedDict())
webhooks[endpoint] = {

Check warning on line 139 in flask_smorest/__init__.py

View check run for this annotation

Codecov / codecov/patch

flask_smorest/__init__.py#L138-L139

Added lines #L138 - L139 were not covered by tests
"post": {
"requestBody": {
"description": request_description,
"content": {
"application/json": {
"schema": {"$ref": f"#/components/schemas/{schema_title}"}
}
},
},
"responses": {
f"{response_status_code}": {"description": response_description}
},
}
}