|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +from typing import Optional |
| 18 | + |
| 19 | +from dubbo.common import constants as common_constants |
| 20 | +from dubbo.common.types import DeserializingFunction, SerializingFunction |
| 21 | +from dubbo.config import ReferenceConfig |
| 22 | +from dubbo.proxy import RpcCallable |
| 23 | +from dubbo.proxy.callables import MultipleRpcCallable |
| 24 | + |
| 25 | + |
| 26 | +class Client: |
| 27 | + |
| 28 | + __slots__ = ["_reference"] |
| 29 | + |
| 30 | + def __init__(self, reference: ReferenceConfig): |
| 31 | + self._reference = reference |
| 32 | + |
| 33 | + def unary( |
| 34 | + self, |
| 35 | + method_name: str, |
| 36 | + request_serializer: Optional[SerializingFunction] = None, |
| 37 | + response_deserializer: Optional[DeserializingFunction] = None, |
| 38 | + ) -> RpcCallable: |
| 39 | + return self._callable( |
| 40 | + common_constants.UNARY_CALL_VALUE, |
| 41 | + method_name, |
| 42 | + request_serializer, |
| 43 | + response_deserializer, |
| 44 | + ) |
| 45 | + |
| 46 | + def client_stream( |
| 47 | + self, |
| 48 | + method_name: str, |
| 49 | + request_serializer: Optional[SerializingFunction] = None, |
| 50 | + response_deserializer: Optional[DeserializingFunction] = None, |
| 51 | + ) -> RpcCallable: |
| 52 | + return self._callable( |
| 53 | + common_constants.CLIENT_STREAM_CALL_VALUE, |
| 54 | + method_name, |
| 55 | + request_serializer, |
| 56 | + response_deserializer, |
| 57 | + ) |
| 58 | + |
| 59 | + def server_stream( |
| 60 | + self, |
| 61 | + method_name: str, |
| 62 | + request_serializer: Optional[SerializingFunction] = None, |
| 63 | + response_deserializer: Optional[DeserializingFunction] = None, |
| 64 | + ) -> RpcCallable: |
| 65 | + return self._callable( |
| 66 | + common_constants.SERVER_STREAM_CALL_VALUE, |
| 67 | + method_name, |
| 68 | + request_serializer, |
| 69 | + response_deserializer, |
| 70 | + ) |
| 71 | + |
| 72 | + def bidi_stream( |
| 73 | + self, |
| 74 | + method_name: str, |
| 75 | + request_serializer: Optional[SerializingFunction] = None, |
| 76 | + response_deserializer: Optional[DeserializingFunction] = None, |
| 77 | + ) -> RpcCallable: |
| 78 | + return self._callable( |
| 79 | + common_constants.BI_STREAM_CALL_VALUE, |
| 80 | + method_name, |
| 81 | + request_serializer, |
| 82 | + response_deserializer, |
| 83 | + ) |
| 84 | + |
| 85 | + def _callable( |
| 86 | + self, |
| 87 | + call_type: str, |
| 88 | + method_name: str, |
| 89 | + request_serializer: Optional[SerializingFunction] = None, |
| 90 | + response_deserializer: Optional[DeserializingFunction] = None, |
| 91 | + ) -> RpcCallable: |
| 92 | + """ |
| 93 | + Generate a proxy for the given method |
| 94 | + :param call_type: The call type. |
| 95 | + :type call_type: str |
| 96 | + :param method_name: The method name. |
| 97 | + :type method_name: str |
| 98 | + :param request_serializer: The request serializer. |
| 99 | + :type request_serializer: Optional[SerializingFunction] |
| 100 | + :param response_deserializer: The response deserializer. |
| 101 | + :type response_deserializer: Optional[DeserializingFunction] |
| 102 | + :return: The proxy. |
| 103 | + :rtype: RpcCallable |
| 104 | + """ |
| 105 | + # get invoker |
| 106 | + invoker = self._reference.get_invoker() |
| 107 | + url = invoker.get_url() |
| 108 | + |
| 109 | + # clone url |
| 110 | + url = url.copy() |
| 111 | + url.parameters[common_constants.METHOD_KEY] = method_name |
| 112 | + url.parameters[common_constants.CALL_KEY] = call_type |
| 113 | + |
| 114 | + # set serializer and deserializer |
| 115 | + url.attributes[common_constants.SERIALIZER_KEY] = request_serializer |
| 116 | + url.attributes[common_constants.DESERIALIZER_KEY] = response_deserializer |
| 117 | + |
| 118 | + # create proxy |
| 119 | + return MultipleRpcCallable(invoker, url) |
0 commit comments