{{!

  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.

}}{{!
Generated Python classes for Thrift clients
}}
{{> common/auto_generated_py}}

import typing as _typing

import {{program:root_module_prefix}}apache.thrift.metadata.lite_types as _fbthrift_metadata
import folly.iobuf as _fbthrift_iobuf
from {{program:base_library_package}}.client import (
    AsyncClient as _fbthrift_py3lite_AsyncClient,
    SyncClient as _fbthrift_py3lite_SyncClient,
    Client as _fbthrift_py3lite_Client,
)
import {{program:base_library_package}}.exceptions as _fbthrift_py3lite_exceptions
import {{program:base_library_package}}.types as _fbthrift_py3lite_types
import {{program:module_path}}.lite_types
import {{program:module_path}}.lite_metadata
{{#program:include_namespaces}}
{{#has_types?}}
import {{included_module_path}}.lite_types
{{/has_types?}}
{{#has_services?}}
import {{included_module_path}}.lite_clients
{{/has_services?}}
{{/program:include_namespaces}}


{{#program:services?}}
{{#program:services}}
class {{service:name}}(_fbthrift_py3lite_Client["{{service:name}}.Async", "{{service:name}}.Sync"]):
    @staticmethod
    def __get_thrift_name__() -> str:
        return "{{program:name}}.{{service:name}}"

    @staticmethod
    def __get_metadata__() -> _fbthrift_metadata.ThriftMetadata:
        return {{program:module_path}}.lite_metadata.gen_metadata_service_{{service:name}}()

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

        @staticmethod
        def __get_metadata__() -> _fbthrift_metadata.ThriftMetadata:
            return {{program:module_path}}.lite_metadata.gen_metadata_service_{{service:name}}()

        {{#service:supported_functions}}
        async def {{function:name}}(
            self{{#function:args}},
            {{field:py_name}}: {{#field:type}}{{> types/pep484_type}}{{/field:type}}{{/function:args}}
        ) -> {{#function:return_type}}{{> types/pep484_type}}{{/function:return_type}}:
            resp = await self._send_request(
                "{{service:name}}",
                "{{function:name}}",
                {{> types/function_args_type}}({{#function:args}}
                    {{field:py_name}}={{field:py_name}},{{/function:args}}),
                {{#function:oneway?}}None{{/function:oneway?}}{{!!
                }}{{^function:oneway?}}{{> types/function_return_type}}{{/function:oneway?}},
            )
            {{#function:return_type}}{{^type:void?}}
            # shortcut to success path for non-void returns
            if resp.success is not None:
                return resp.success
            {{/type:void?}}{{/function:return_type}}
            {{#function:exceptions}}
            if resp.{{field:py_name}} is not None:
                raise resp.{{field:py_name}}
            {{/function:exceptions}}
            {{#function:return_type}}{{^type:void?}}
            raise _fbthrift_py3lite_exceptions.ApplicationError(
                _fbthrift_py3lite_exceptions.ApplicationErrorType.MISSING_RESULT,
                "Empty Response",
            )
            {{/type:void?}}{{/function:return_type}}

        {{/service:supported_functions}}

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

        @staticmethod
        def __get_metadata__() -> _fbthrift_metadata.ThriftMetadata:
            return {{program:module_path}}.lite_metadata.gen_metadata_service_{{service:name}}()

        {{#service:supported_functions}}
        def {{function:name}}(
            self{{#function:args}},
            {{field:py_name}}: {{#field:type}}{{> types/pep484_type}}{{/field:type}}{{/function:args}}
        ) -> {{#function:return_type}}{{> types/pep484_type}}{{/function:return_type}}:
            resp = self._send_request(
                "{{service:name}}",
                "{{function:name}}",
                {{> types/function_args_type}}({{#function:args}}
                    {{field:py_name}}={{field:py_name}},{{/function:args}}),
                {{#function:oneway?}}None{{/function:oneway?}}{{!!
                }}{{^function:oneway?}}{{> types/function_return_type}}{{/function:oneway?}},
            )
            {{#function:return_type}}{{^type:void?}}
            # shortcut to success path for non-void returns
            if resp.success is not None:
                return resp.success
            {{/type:void?}}{{/function:return_type}}
            {{#function:exceptions}}
            if resp.{{field:py_name}} is not None:
                raise resp.{{field:py_name}}
            {{/function:exceptions}}
            {{#function:return_type}}{{^type:void?}}
            raise _fbthrift_py3lite_exceptions.ApplicationError(
                _fbthrift_py3lite_exceptions.ApplicationErrorType.MISSING_RESULT,
                "Empty Response",
            )
            {{/type:void?}}{{/function:return_type}}

        {{/service:supported_functions}}

{{/program:services}}
{{/program:services?}}
