{{!

  Copyright (c) Meta Platforms, Inc. and affiliates.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

}}{{!

Thrift python client body definition, which is shared between normal clients and interaction clients

}}@staticmethod
def __get_thrift_name__() -> str:
    return "{{program:name}}.{{service:name}}"

@staticmethod
def __get_thrift_uri__() -> _typing.Optional[str]:
    return {{#service:thrift_uri}}"{{service:thrift_uri}}"{{/service:thrift_uri}}{{!
        }}{{^service:thrift_uri}}None{{/service:thrift_uri}}

@staticmethod
def __get_thrift_unstructured_annotations_DEPRECATED__() -> _typing.Mapping[str, str]:
    return {
        {{#service:annotations}}
        {{> common/annotation}}
        {{/service:annotations}}
    }

{{^service:interaction?}}
@staticmethod
def __get_metadata__() -> _fbthrift_metadata.ThriftMetadata:
    return {{program:module_path}}.thrift_metadata.gen_metadata_service_{{service:name}}()
{{/service:interaction?}}
{{#service:interaction?}}
@staticmethod
def __get_metadata__() -> _fbthrift_metadata.ThriftMetadata:
    return {{program:module_path}}.thrift_metadata.gen_metadata_service_{{service:parent_service_name}}_{{service:name}}()
{{/service:interaction?}}

class Async({{#service:extends}}{{!
    }}{{> services/client_module_path}}.Async{{!
}}{{/service:extends}}{{^service:extends?}}{{!
    }}_fbthrift_python_AsyncClient{{!
}}{{/service:extends?}}):
    @staticmethod
    def __get_thrift_name__() -> str:
        return "{{program:name}}.{{service:name}}"

    @staticmethod
    def __get_thrift_uri__() -> _typing.Optional[str]:
        return {{#service:thrift_uri}}"{{service:thrift_uri}}"{{/service:thrift_uri}}{{!
            }}{{^service:thrift_uri}}None{{/service:thrift_uri}}

    {{^service:interaction?}}
    @staticmethod
    def __get_metadata__() -> _fbthrift_metadata.ThriftMetadata:
        return {{program:module_path}}.thrift_metadata.gen_metadata_service_{{service:name}}()
    {{/service:interaction?}}
    {{#service:interaction?}}
    @staticmethod
    def __get_metadata__() -> _fbthrift_metadata.ThriftMetadata:
        return {{program:module_path}}.thrift_metadata.gen_metadata_service_{{service:parent_service_name}}_{{service:name}}()
    {{/service:interaction?}}
    {{#service:supported_functions}}

    async def {{function:name}}(
        self{{#function:args}},
        {{field:py_name}}: {{#field:type}}{{> types/pep484_type}}{{/field:type}}{{/function:args}},
        *,
        rpc_options: _typing.Optional[RpcOptions] = None,
    ) -> {{#function:return_type}}{{!
        }}{{#function:returns_tuple?}}_typing.Tuple[{{/function:returns_tuple?}}{{!
        }}{{#function:creates_interaction?}}{{service:name}}_{{function:created_interaction}}.Async{{^type:void?}}, {{> types/pep484_return_type}}{{/type:void?}}{{/function:creates_interaction?}}{{!
        }}{{^function:creates_interaction?}}{{> types/pep484_return_type}}{{/function:creates_interaction?}}{{!
        }}{{#function:returns_tuple?}}]{{/function:returns_tuple?}}{{!
    }}{{/function:return_type}}:
        {{#function:creates_interaction?}}
        _fbthrift_interaction = self.create{{function:created_interaction}}()
        {{/function:creates_interaction?}}
        _fbthrift_resp = await self._send_request(
            {{^service:interaction?}}"{{service:name}}"{{/service:interaction?}}{{#service:interaction?}}"{{service:parent_service_name}}"{{/service:interaction?}},
            {{#service:interaction?}}"{{service:name}}.{{function:name}}",{{/service:interaction?}}{{^service:interaction?}}"{{function:name}}",{{/service:interaction?}}
            {{> types/function_args_type}}({{#function:args}}
                {{field:py_name}}={{field:py_name}},{{/function:args}}),
            {{> clients/client_return_type}},
            qualifier = _fbthrift_FunctionQualifier.{{function:qualifier}},
            {{#function:creates_interaction?}}
            interaction_position=_fbthrift_InteractionMethodPosition.Factory,
            interaction_name="{{function:created_interaction}}",
            created_interaction = _fbthrift_interaction,
            {{/function:creates_interaction?}}
            {{#service:interaction?}}
            interaction_position=_fbthrift_InteractionMethodPosition.Member,
            interaction_name="{{service:name}}",
            {{/service:interaction?}}
            uri_or_name={{#service:thrift_uri}}"{{service:thrift_uri}}"{{/service:thrift_uri}}{{!
                }}{{^service:thrift_uri}}"{{service:name}}"{{/service:thrift_uri}},
            rpc_options=rpc_options,{{!
}}{{#program:generate_mutable_types}}
            is_mutable_types=True,{{!
}}{{/program:generate_mutable_types}}
        )
        {{#function:stream?}}
        _fbthrift_resp, _fbthrift_stream = _fbthrift_resp
        {{/function:stream?}}
        {{#function:return_type}}
        {{#function:early_client_return?}}
        # shortcut to success path for non-void returns
        if _fbthrift_resp.success is not None:
            return {{#function:creates_interaction?}}_fbthrift_interaction, {{/function:creates_interaction?}}{{!
          }}_fbthrift_resp.success{{#function:stream?}}, _fbthrift_stream{{/function:stream?}}
        {{/function:early_client_return?}}
        {{#function:exceptions}}
        if _fbthrift_resp.{{field:py_name}} is not None:
            raise _fbthrift_resp.{{field:py_name}}
        {{/function:exceptions}}
        {{^function:early_client_return?}}
        {{^function:void?}}
        return {{#function:creates_interaction?}}_fbthrift_interaction{{/function:creates_interaction?}}{{!
          }}{{#function:returns_tuple?}}, {{/function:returns_tuple?}}{{!
          }}{{#function:stream?}}_fbthrift_stream{{/function:stream?}}
        {{/function:void?}}
        {{/function:early_client_return?}}
        {{#function:with_regular_response?}}
        raise _fbthrift_python_exceptions.ApplicationError(
            _fbthrift_python_exceptions.ApplicationErrorType.MISSING_RESULT,
            "Empty Response",
        )
        {{/function:with_regular_response?}}
        {{/function:return_type}}
    {{/service:supported_functions}}

    {{#service:interactions}}
    def create{{service:name}}(
        self #{{service:parent_service_name}}
    ) -> {{service:parent_service_name}}_{{service:name}}.Async:
        return self._create_interaction("{{service:name}}", {{service:parent_service_name}}_{{service:name}}.Async)
    async def async_create{{service:name}}(
        self #{{service:parent_service_name}}
    ) -> {{service:parent_service_name}}_{{service:name}}.Async:
        return self.create{{service:name}}()
    {{/service:interactions}}
class Sync({{#service:extends}}{{!
    }}{{> services/client_module_path}}.Sync{{!
}}{{/service:extends}}{{^service:extends?}}{{!
    }}_fbthrift_python_SyncClient{{!
}}{{/service:extends?}}):
    @staticmethod
    def __get_thrift_name__() -> str:
        return "{{program:name}}.{{service:name}}"

    @staticmethod
    def __get_thrift_uri__() -> _typing.Optional[str]:
        return {{#service:thrift_uri}}"{{service:thrift_uri}}"{{/service:thrift_uri}}{{!
            }}{{^service:thrift_uri}}None{{/service:thrift_uri}}

    {{^service:interaction?}}
    @staticmethod
    def __get_metadata__() -> _fbthrift_metadata.ThriftMetadata:
        return {{program:module_path}}.thrift_metadata.gen_metadata_service_{{service:name}}()
    {{/service:interaction?}}
    {{#service:interaction?}}
    @staticmethod
    def __get_metadata__() -> _fbthrift_metadata.ThriftMetadata:
        return {{program:module_path}}.thrift_metadata.gen_metadata_service_{{service:parent_service_name}}_{{service:name}}()
    {{/service:interaction?}}
    {{#service:supported_functions}}
    {{^function:async_only?}}

    def {{function:name}}(
        self{{#function:args}},
        {{field:py_name}}: {{#field:type}}{{> types/pep484_type}}{{/field:type}}{{/function:args}},
        *,
        rpc_options: _typing.Optional[RpcOptions] = None,
    ) -> {{#function:return_type}}{{> types/pep484_return_type}}{{/function:return_type}}:
        _fbthrift_resp = self._send_request(
            "{{service:name}}",
            "{{function:name}}",
            {{> types/function_args_type}}({{#function:args}}
                {{field:py_name}}={{field:py_name}},{{/function:args}}),
            {{> clients/client_return_type}},
            uri_or_name={{#service:thrift_uri}}"{{service:thrift_uri}}"{{/service:thrift_uri}}{{!
                }}{{^service:thrift_uri}}"{{service:name}}"{{/service:thrift_uri}},
            rpc_options=rpc_options,{{!
}}{{#program:generate_mutable_types}}
            is_mutable_types=True,{{!
}}{{/program:generate_mutable_types}}
        )
        {{#function:return_type}}{{^type:void?}}
        # shortcut to success path for non-void returns
        if _fbthrift_resp.success is not None:
            return _fbthrift_resp.success
        {{/type:void?}}{{/function:return_type}}
        {{#function:exceptions}}
        if _fbthrift_resp.{{field:py_name}} is not None:
            raise _fbthrift_resp.{{field:py_name}}
        {{/function:exceptions}}
        {{#function:return_type}}{{^type:void?}}
        raise _fbthrift_python_exceptions.ApplicationError(
            _fbthrift_python_exceptions.ApplicationErrorType.MISSING_RESULT,
            "Empty Response",
        )
        {{/type:void?}}{{/function:return_type}}
    {{/function:async_only?}}
    {{/service:supported_functions}}
