Client and Service Stubs take 1 request parameter, not one for each field (#311)
This commit is contained in:
		| @@ -79,51 +79,21 @@ class {{ service.py_name }}Stub(betterproto.ServiceStub): | ||||
|     {% for method in service.methods %} | ||||
|     async def {{ method.py_name }}(self | ||||
|         {%- if not method.client_streaming -%} | ||||
|             {%- if method.py_input_message and method.py_input_message.fields -%}, *, | ||||
|                 {%- for field in method.py_input_message.fields -%} | ||||
|                     {{ field.py_name }}: {% if field.py_name in method.mutable_default_args and not field.annotation.startswith("Optional[") -%} | ||||
|                                             Optional[{{ field.annotation }}] | ||||
|                                          {%- else -%} | ||||
|                                             {{ field.annotation }} | ||||
|                                          {%- endif -%} = | ||||
|                                             {%- if field.py_name not in method.mutable_default_args -%} | ||||
|                                                 {{ field.default_value_string }} | ||||
|                                             {%- else -%} | ||||
|                                                 None | ||||
|                                             {% endif -%} | ||||
|                     {%- if not loop.last %}, {% endif -%} | ||||
|                 {%- endfor -%} | ||||
|             {%- endif -%} | ||||
|             {%- if method.py_input_message -%}, {{ method.py_input_message_param }}: "{{ method.py_input_message_type }}"{%- endif -%} | ||||
|         {%- else -%} | ||||
|             {# Client streaming: need a request iterator instead #} | ||||
|             , request_iterator: Union[AsyncIterable["{{ method.py_input_message_type }}"], Iterable["{{ method.py_input_message_type }}"]] | ||||
|             , {{ method.py_input_message_param }}_iterator: Union[AsyncIterable["{{ method.py_input_message_type }}"], Iterable["{{ method.py_input_message_type }}"]] | ||||
|         {%- endif -%} | ||||
|             ) -> {% if method.server_streaming %}AsyncIterator["{{ method.py_output_message_type }}"]{% else %}"{{ method.py_output_message_type }}"{% endif %}: | ||||
|         {% if method.comment %} | ||||
| {{ method.comment }} | ||||
|  | ||||
|         {% endif %} | ||||
|         {%- for py_name, zero in method.mutable_default_args.items() %} | ||||
|         {{ py_name }} = {{ py_name }} or {{ zero }} | ||||
|         {% endfor %} | ||||
|  | ||||
|         {% if not method.client_streaming %} | ||||
|         request = {{ method.py_input_message_type }}() | ||||
|         {% for field in method.py_input_message.fields %} | ||||
|             {% if field.field_type == 'message' %} | ||||
|         if {{ field.py_name }} is not None: | ||||
|             request.{{ field.py_name }} = {{ field.py_name }} | ||||
|             {% else %} | ||||
|         request.{{ field.py_name }} = {{ field.py_name }} | ||||
|             {% endif %} | ||||
|         {% endfor %} | ||||
|         {% endif %} | ||||
|  | ||||
|         {% if method.server_streaming %} | ||||
|             {% if method.client_streaming %} | ||||
|         async for response in self._stream_stream( | ||||
|             "{{ method.route }}", | ||||
|             request_iterator, | ||||
|             {{ method.py_input_message_param }}_iterator, | ||||
|             {{ method.py_input_message_type }}, | ||||
|             {{ method.py_output_message_type.strip('"') }}, | ||||
|         ): | ||||
| @@ -131,7 +101,7 @@ class {{ service.py_name }}Stub(betterproto.ServiceStub): | ||||
|             {% else %}{# i.e. not client streaming #} | ||||
|         async for response in self._unary_stream( | ||||
|             "{{ method.route }}", | ||||
|             request, | ||||
|             {{ method.py_input_message_param }}, | ||||
|             {{ method.py_output_message_type.strip('"') }}, | ||||
|         ): | ||||
|             yield response | ||||
| @@ -141,14 +111,14 @@ class {{ service.py_name }}Stub(betterproto.ServiceStub): | ||||
|             {% if method.client_streaming %} | ||||
|         return await self._stream_unary( | ||||
|             "{{ method.route }}", | ||||
|             request_iterator, | ||||
|             {{ method.py_input_message_param }}_iterator, | ||||
|             {{ method.py_input_message_type }}, | ||||
|             {{ method.py_output_message_type.strip('"') }} | ||||
|         ) | ||||
|             {% else %}{# i.e. not client streaming #} | ||||
|         return await self._unary_unary( | ||||
|             "{{ method.route }}", | ||||
|             request, | ||||
|             {{ method.py_input_message_param }}, | ||||
|             {{ method.py_output_message_type.strip('"') }} | ||||
|         ) | ||||
|             {% endif %}{# client streaming #} | ||||
| @@ -167,19 +137,10 @@ class {{ service.py_name }}Base(ServiceBase): | ||||
|     {% for method in service.methods %} | ||||
|     async def {{ method.py_name }}(self | ||||
|         {%- if not method.client_streaming -%} | ||||
|             {%- if method.py_input_message and method.py_input_message.fields -%}, | ||||
|                 {%- for field in method.py_input_message.fields -%} | ||||
|                     {{ field.py_name }}: {% if field.py_name in method.mutable_default_args and not field.annotation.startswith("Optional[") -%} | ||||
|                                             Optional[{{ field.annotation }}] | ||||
|                                          {%- else -%} | ||||
|                                             {{ field.annotation }} | ||||
|                                          {%- endif -%} | ||||
|                     {%- if not loop.last %}, {% endif -%} | ||||
|                 {%- endfor -%} | ||||
|             {%- endif -%} | ||||
|             {%- if method.py_input_message -%}, {{ method.py_input_message_param }}: "{{ method.py_input_message_type }}"{%- endif -%} | ||||
|         {%- else -%} | ||||
|             {# Client streaming: need a request iterator instead #} | ||||
|             , request_iterator: AsyncIterator["{{ method.py_input_message_type }}"] | ||||
|             , {{ method.py_input_message_param }}_iterator: AsyncIterator["{{ method.py_input_message_type }}"] | ||||
|         {%- endif -%} | ||||
|             ) -> {% if method.server_streaming %}AsyncIterator["{{ method.py_output_message_type }}"]{% else %}"{{ method.py_output_message_type }}"{% endif %}: | ||||
|         {% if method.comment %} | ||||
| @@ -194,25 +155,17 @@ class {{ service.py_name }}Base(ServiceBase): | ||||
|     async def __rpc_{{ method.py_name }}(self, stream: grpclib.server.Stream) -> None: | ||||
|         {% if not method.client_streaming %} | ||||
|         request = await stream.recv_message() | ||||
|  | ||||
|         request_kwargs = { | ||||
|         {% for field in method.py_input_message.fields %} | ||||
|             "{{ field.py_name }}": request.{{ field.py_name }}, | ||||
|         {% endfor %} | ||||
|         } | ||||
|  | ||||
|         {% else %} | ||||
|         request_kwargs = {"request_iterator": stream.__aiter__()} | ||||
|         request = stream.__aiter__() | ||||
|         {% endif %} | ||||
|  | ||||
|         {% if not method.server_streaming %} | ||||
|         response = await self.{{ method.py_name }}(**request_kwargs) | ||||
|         response = await self.{{ method.py_name }}(request) | ||||
|         await stream.send_message(response) | ||||
|         {% else %} | ||||
|         await self._call_rpc_handler_server_stream( | ||||
|             self.{{ method.py_name }}, | ||||
|             stream, | ||||
|             request_kwargs, | ||||
|             request, | ||||
|         ) | ||||
|         {% endif %} | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user