Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Commit d132039

Browse files
committed
Move method body to async template
1 parent d4e0053 commit d132039

File tree

3 files changed

+64
-56
lines changed

3 files changed

+64
-56
lines changed

src/betterproto2_compiler/templates/service_stub.py.j2

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -27,61 +27,6 @@ class {% block class_name %}{% endblock %}({% block inherit_from %}{% endblock %
2727
{% endif %}
2828
{% endblock %}
2929

30-
{% if method.server_streaming %}
31-
{% if method.client_streaming %}
32-
async for response in self._stream_stream(
33-
"{{ method.route }}",
34-
messages,
35-
{{ method.py_input_message_type }},
36-
{{ method.py_output_message_type }},
37-
timeout=timeout,
38-
deadline=deadline,
39-
metadata=metadata,
40-
):
41-
yield response
42-
{% else %}{# i.e. not client streaming #}
43-
{% if method.is_input_msg_empty %}
44-
if message is None:
45-
message = {{ method.py_input_message_type }}()
46-
47-
{% endif %}
48-
async for response in self._unary_stream(
49-
"{{ method.route }}",
50-
message,
51-
{{ method.py_output_message_type }},
52-
timeout=timeout,
53-
deadline=deadline,
54-
metadata=metadata,
55-
):
56-
yield response
57-
58-
{% endif %}{# if client streaming #}
59-
{% else %}{# i.e. not server streaming #}
60-
{% if method.client_streaming %}
61-
return await self._stream_unary(
62-
"{{ method.route }}",
63-
messages,
64-
{{ method.py_input_message_type }},
65-
{{ method.py_output_message_type }},
66-
timeout=timeout,
67-
deadline=deadline,
68-
metadata=metadata,
69-
)
70-
{% else %}{# i.e. not client streaming #}
71-
{% if method.is_input_msg_empty %}
72-
if message is None:
73-
message = {{ method.py_input_message_type }}()
74-
75-
{% endif %}
76-
return await self._unary_unary(
77-
"{{ method.route }}",
78-
message,
79-
{{ method.py_output_message_type }},
80-
timeout=timeout,
81-
deadline=deadline,
82-
metadata=metadata,
83-
)
84-
{% endif %}{# client streaming #}
85-
{% endif %}
30+
{% block method_body scoped required %}{% endblock %}
8631

8732
{% endfor %}

src/betterproto2_compiler/templates/service_stub_async.py.j2

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,63 @@
2424
, deadline: "Deadline | None" = None
2525
, metadata: "MetadataLike | None" = None
2626
) -> "{% if method.server_streaming %}AsyncIterator[{{ method.py_output_message_type }}]{% else %}{{ method.py_output_message_type }}{% endif %}":
27+
{% endblock %}
28+
29+
{% block method_body %}
30+
{% if method.server_streaming %}
31+
{% if method.client_streaming %}
32+
async for response in self._stream_stream(
33+
"{{ method.route }}",
34+
messages,
35+
{{ method.py_input_message_type }},
36+
{{ method.py_output_message_type }},
37+
timeout=timeout,
38+
deadline=deadline,
39+
metadata=metadata,
40+
):
41+
yield response
42+
{% else %}
43+
{% if method.is_input_msg_empty %}
44+
if message is None:
45+
message = {{ method.py_input_message_type }}()
46+
47+
{% endif %}
48+
async for response in self._unary_stream(
49+
"{{ method.route }}",
50+
message,
51+
{{ method.py_output_message_type }},
52+
timeout=timeout,
53+
deadline=deadline,
54+
metadata=metadata,
55+
):
56+
yield response
57+
58+
{% endif %}
59+
{% else %}
60+
{% if method.client_streaming %}
61+
return await self._stream_unary(
62+
"{{ method.route }}",
63+
messages,
64+
{{ method.py_input_message_type }},
65+
{{ method.py_output_message_type }},
66+
timeout=timeout,
67+
deadline=deadline,
68+
metadata=metadata,
69+
)
70+
{% else %}
71+
{% if method.is_input_msg_empty %}
72+
if message is None:
73+
message = {{ method.py_input_message_type }}()
74+
75+
{% endif %}
76+
return await self._unary_unary(
77+
"{{ method.route }}",
78+
message,
79+
{{ method.py_output_message_type }},
80+
timeout=timeout,
81+
deadline=deadline,
82+
metadata=metadata,
83+
)
84+
{% endif %}
85+
{% endif %}
2786
{% endblock %}

src/betterproto2_compiler/templates/service_stub_sync.py.j2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@
3030
, metadata: "MetadataLike | None" = None
3131
) -> "{% if method.server_streaming %}Iterator[{{ method.py_output_message_type }}]{% else %}{{ method.py_output_message_type }}{% endif %}":
3232
{% endblock %}
33+
34+
{% block method_body %}
35+
pass
36+
{% endblock %}

0 commit comments

Comments
 (0)