Skip to content

python: Update api_resource template to support sub-resources #1840

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

Merged
merged 3 commits into from
Mar 21, 2025
Merged
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
2 changes: 1 addition & 1 deletion codegen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ output_dir = "svix-cli/src/cmds/api"
[python]
template_dir = "python/templates"
extra_shell_commands = [
"rm python/svix/api/{environment,health,ingest,operational_webhook}.py",
"rm python/svix/api/{health,ingest}.py",
]
[[python.task]]
template = "python/templates/api_resource.py.jinja"
Expand Down
94 changes: 94 additions & 0 deletions python/svix/api/environment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# This file is @generated
import typing as t
from dataclasses import dataclass

from ..models import EnvironmentIn, EnvironmentOut
from .common import ApiBase, BaseOptions, serialize_params


@dataclass
class EnvironmentExportOptions(BaseOptions):
idempotency_key: t.Optional[str] = None

def _header_params(self) -> t.Dict[str, str]:
return serialize_params(
{
"idempotency-key": self.idempotency_key,
}
)


@dataclass
class EnvironmentImportOptions(BaseOptions):
idempotency_key: t.Optional[str] = None

def _header_params(self) -> t.Dict[str, str]:
return serialize_params(
{
"idempotency-key": self.idempotency_key,
}
)


class EnvironmentAsync(ApiBase):
async def export(
self, options: EnvironmentExportOptions = EnvironmentExportOptions()
) -> EnvironmentOut:
"""Download a JSON file containing all org-settings and event types."""
response = await self._request_asyncio(
method="post",
path="/api/v1/environment/export",
path_params={},
query_params=options._query_params(),
header_params=options._header_params(),
)
return EnvironmentOut.model_validate(response.json())

async def import_(
self,
environment_in: EnvironmentIn,
options: EnvironmentImportOptions = EnvironmentImportOptions(),
) -> None:
"""Import a configuration into the active organization.

It doesn't delete anything, only adds / updates what was passed to it."""
await self._request_asyncio(
method="post",
path="/api/v1/environment/import",
path_params={},
query_params=options._query_params(),
header_params=options._header_params(),
json_body=environment_in.model_dump_json(exclude_unset=True, by_alias=True),
)


class Environment(ApiBase):
def export(
self, options: EnvironmentExportOptions = EnvironmentExportOptions()
) -> EnvironmentOut:
"""Download a JSON file containing all org-settings and event types."""
response = self._request_sync(
method="post",
path="/api/v1/environment/export",
path_params={},
query_params=options._query_params(),
header_params=options._header_params(),
)
return EnvironmentOut.model_validate(response.json())

def import_(
self,
environment_in: EnvironmentIn,
options: EnvironmentImportOptions = EnvironmentImportOptions(),
) -> None:
"""Import a configuration into the active organization.

It doesn't delete anything, only adds / updates what was passed to it."""
self._request_sync(
method="post",
path="/api/v1/environment/import",
path_params={},
query_params=options._query_params(),
header_params=options._header_params(),
json_body=environment_in.model_dump_json(exclude_unset=True, by_alias=True),
)
18 changes: 18 additions & 0 deletions python/svix/api/operational_webhook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This file is @generated
from .common import ApiBase
from .operational_webhook_endpoint import (
OperationalWebhookEndpoint,
OperationalWebhookEndpointAsync,
)


class OperationalWebhookAsync(ApiBase):
@property
def endpoint(self) -> OperationalWebhookEndpointAsync:
return OperationalWebhookEndpointAsync(self._client)


class OperationalWebhook(ApiBase):
@property
def endpoint(self) -> OperationalWebhookEndpoint:
return OperationalWebhookEndpoint(self._client)
18 changes: 18 additions & 0 deletions python/svix/api/svix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
from .application import Application, ApplicationAsync
from .authentication import Authentication, AuthenticationAsync
from .endpoint import Endpoint, EndpointAsync
from .environment import Environment, EnvironmentAsync
from .event_type import EventType, EventTypeAsync
from .integration import Integration, IntegrationAsync
from .message import Message, MessageAsync
from .message_attempt import MessageAttempt, MessageAttemptAsync
from .operational_webhook import OperationalWebhook, OperationalWebhookAsync
from .operational_webhook_endpoint import (
OperationalWebhookEndpoint,
OperationalWebhookEndpointAsync,
Expand Down Expand Up @@ -87,6 +89,10 @@ def application(self) -> ApplicationAsync:
def endpoint(self) -> EndpointAsync:
return EndpointAsync(self._client)

@property
def environment(self) -> EnvironmentAsync:
return EnvironmentAsync(self._client)

@property
def event_type(self) -> EventTypeAsync:
return EventTypeAsync(self._client)
Expand All @@ -107,6 +113,10 @@ def message_attempt(self) -> MessageAttemptAsync:
def statistics(self) -> StatisticsAsync:
return StatisticsAsync(self._client)

@property
def operational_webhook(self) -> OperationalWebhookAsync:
return OperationalWebhookAsync(self._client)

@property
def operational_webhook_endpoint(self) -> OperationalWebhookEndpointAsync:
return OperationalWebhookEndpointAsync(self._client)
Expand All @@ -125,6 +135,10 @@ def application(self) -> Application:
def endpoint(self) -> Endpoint:
return Endpoint(self._client)

@property
def environment(self) -> Environment:
return Environment(self._client)

@property
def event_type(self) -> EventType:
return EventType(self._client)
Expand All @@ -145,6 +159,10 @@ def message_attempt(self) -> MessageAttempt:
def statistics(self) -> Statistics:
return Statistics(self._client)

@property
def operational_webhook(self) -> OperationalWebhook:
return OperationalWebhook(self._client)

@property
def operational_webhook_endpoint(self) -> OperationalWebhookEndpoint:
return OperationalWebhookEndpoint(self._client)
21 changes: 18 additions & 3 deletions python/templates/api_resource.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ from .. import models

{% for c in referenced_components -%}
from ..models import {{ c | to_upper_camel_case }}
{% endfor -%}
{% for _, sub in resource.subresources | items -%}
from .{{ sub.name | to_snake_case }} import (
{{ sub.name | to_upper_camel_case }},
{{ sub.name | to_upper_camel_case }}Async,
)
{% endfor %}


Expand Down Expand Up @@ -65,7 +71,15 @@ class {{ resource_type_name }}{{ op.name | to_upper_camel_case }}Options(BaseOpt

{% for is_async in [true, false] %}
class {{ resource.name | to_upper_camel_case }}{% if is_async %}Async{% endif %}(ApiBase):
{%- if resource.operations | length != 0 %}
{%- for name, sub in resource.subresources | items %}
{%- set sub_type_name %}{{ sub.name | to_upper_camel_case }}{% if is_async %}Async{% endif %}{% endset %}
@property
def {{ name | to_snake_case }}(self) -> {{ sub_type_name }}:
return {{ sub_type_name }}(self._client)

{% endfor %}

{%- if resource.operations | length > 0 %}
{%- for op in resource.operations %}
{% if op.response_body_schema_name is defined -%}
{% set return_type = op.response_body_schema_name | to_upper_camel_case -%}
Expand Down Expand Up @@ -142,8 +156,9 @@ class {{ resource.name | to_upper_camel_case }}{% if is_async %}Async{% endif %}

{{ code | indent(4, true) }}
{% endfor -%}
{% else %}
{# empty class with no functions, so we have a pass here #}
{% endif %}
{% if resource.subresources | length == 0 and resource.operations | length == 0 %}
{# empty class with no members, so we have a pass here -#}
pass
{% endif %}
{% endfor %}