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

Commit f760f73

Browse files
committed
Move method body to async template
1 parent 19acf0b commit f760f73

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
@@ -26,61 +26,6 @@ class {% block class_name %}{% endblock %}({% block inherit_from %}{% endblock %
2626
{% endif %}
2727
{% endblock %}
2828

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

8631
{% 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: "{{ output_file.typing_compiler.optional("Deadline") }}" = None
2525
, metadata: "{{ output_file.typing_compiler.optional("MetadataLike") }}" = None
2626
) -> "{% if method.server_streaming %}{{ output_file.typing_compiler.async_iterator(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+
{{ method.py_input_message_param }}_iterator,
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 {{ method.py_input_message_param }} is None:
45+
{{ method.py_input_message_param }} = {{ method.py_input_message_type }}()
46+
47+
{% endif %}
48+
async for response in self._unary_stream(
49+
"{{ method.route }}",
50+
{{ method.py_input_message_param }},
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+
{{ method.py_input_message_param }}_iterator,
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 {{ method.py_input_message_param }} is None:
73+
{{ method.py_input_message_param }} = {{ method.py_input_message_type }}()
74+
75+
{% endif %}
76+
return await self._unary_unary(
77+
"{{ method.route }}",
78+
{{ method.py_input_message_param }},
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: "{{ output_file.typing_compiler.optional("MetadataLike") }}" = None
3131
) -> "{% if method.server_streaming %}{{ output_file.typing_compiler.sync_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)