# Code generated by protoc-gen-meshpy. DO NOT EDIT.
# source: meshtrade/iam/user/v1/service.proto

"""
UserService gRPC service wrapper with authentication, timeouts, and resource management.

This module provides a high-level gRPC service for the UserService service that combines
the service interface with resource management capabilities, providing authentication,
timeouts, and proper connection handling.
"""

from collections.abc import Iterator
from datetime import timedelta
from typing import Optional

from meshtrade.common import BaseGRPCClient
from meshtrade.iam.api_user.v1.api_credentials import find_credentials

from .user_pb2 import User
from meshtrade.common.service_options import ServiceOptions
from .service_pb2 import (
AssignRolesToUserRequest,
CreateUserRequest,
GetUserByEmailRequest,
GetUserRequest,
ListUsersRequest,
ListUsersResponse,
RevokeRolesFromUserRequest,
UpdateUserRequest,
)
from .service_pb2_grpc import UserServiceStub


def _create_userservice_stub(channel):
    """Factory function to create UserServiceStub from gRPC channel."""
    return UserServiceStub(channel)


class UserService(BaseGRPCClient):
    """UserService gRPC service with authentication, timeouts, and resource management.

    This service provides a complete implementation of the UserService with proper authentication, timeout handling, and automatic resource cleanup.

    Full Service documentation: https://meshtrade.github.io/api/docs/api-reference/iam/user/v1

    Basic service usage with default SDK Configuration:
    ```python
    service = UserService()

    with service:   # `with` ensures proper clean up of underlying connection after use
        response = service.get_api_user(request)
    ```
        
    With default configuration API credentials are searched for using the standard discovery hierarchy:
    
    1. MESH_API_CREDENTIALS environment variable
    
    2. Default credential file location:
    
    - Linux:   `$XDG_CONFIG_HOME/mesh/credentials.json` or fallback to `$HOME/.config/mesh/credentials.json`
    - macOS:   `$HOME/Library/Application Support/mesh/credentials.json`
    - Windows: `C:\\Users\\<user>\\AppData\\Roaming\\mesh\\credentials.json`
    
    For more information on authentication: https://meshtrade.github.io/api/docs/architecture/authentication

    The service may also be configured with custom options:
    ```python
    from .service_options_meshpy import ServiceOptions
    from datetime import timedelta

    options = ServiceOptions(
        url="api.staging.example.com",
        port=443,
        api_key="your-api-key",
        group="groups/your-group-id",
        timeout=timedelta(seconds=60)
    )

    service = UserService(options)

    with service: # `with` ensures proper clean up of underlying connection after use
        response = service.get_api_user(request)
    ```

    For more information on service configuration: https://meshtrade.github.io/api/docs/architecture/sdk-configuration
    """

    def __init__(self, options: Optional[ServiceOptions] = None):
        """Construct and initialize the UserService service.

        Full Service documentation: https://meshtrade.github.io/api/docs/api-reference/iam/user/v1

        Args:
            options: Optional ServiceOptions for configuring the service.
                    If None, service is constructed with default configuration.
                    With default configuration API credentials are searched for using the standard discovery hierarchy:
                    
                    1. MESH_API_CREDENTIALS environment variable
                    
                    2. Default credential file location:
                    
                    - Linux:   `$XDG_CONFIG_HOME/mesh/credentials.json` or fallback to `$HOME/.config/mesh/credentials.json`
                    - macOS:   `$HOME/Library/Application Support/mesh/credentials.json`
                    - Windows: `C:\\Users\\<user>\\AppData\\Roaming\\mesh\\credentials.json`      

                    For more information on authentication: https://meshtrade.github.io/api/docs/architecture/authentication

                    For more information on service configuration: https://meshtrade.github.io/api/docs/architecture/sdk-configuration            

        Example:
        ```python
        # construct with default configuration
        service = UserService()

        # construct with custom configuration
        options = ServiceOptions(
            url="api.example.com",
            api_key="your-key",
            group="groups/your-group"
        )
        service = UserService(options)
        ```
        """
        if options is None:
            options = ServiceOptions()

        # Initialize the base client with all common functionality
        super().__init__(
            service_name="UserService",
            stub_factory=_create_userservice_stub,
            find_credentials_func=find_credentials,
            url=options.url,
            port=options.port,
            api_key=options.api_key,
            group=options.group,
            timeout=options.timeout,
            tls=options.tls,
        )

    def assign_roles_to_user(self, request: AssignRolesToUserRequest, timeout: Optional[timedelta] = None) -> User:
        """AssignRolesToUser method.

        Args:
            request: The AssignRolesToUser request message
            timeout: Optional timeout override for this call

        Returns:
            The AssignRolesToUser response message

        Raises:
            grpc.RpcError: If the gRPC call fails
            ValueError: If authentication credentials are missing
        """
        return self._execute_method("AssignRolesToUser", request, timeout)

    def revoke_roles_from_user(self, request: RevokeRolesFromUserRequest, timeout: Optional[timedelta] = None) -> User:
        """RevokeRolesFromUser method.

        Args:
            request: The RevokeRolesFromUser request message
            timeout: Optional timeout override for this call

        Returns:
            The RevokeRolesFromUser response message

        Raises:
            grpc.RpcError: If the gRPC call fails
            ValueError: If authentication credentials are missing
        """
        return self._execute_method("RevokeRolesFromUser", request, timeout)

    def get_user(self, request: GetUserRequest, timeout: Optional[timedelta] = None) -> User:
        """GetUser method.

        Args:
            request: The GetUser request message
            timeout: Optional timeout override for this call

        Returns:
            The GetUser response message

        Raises:
            grpc.RpcError: If the gRPC call fails
            ValueError: If authentication credentials are missing
        """
        return self._execute_method("GetUser", request, timeout)

    def get_user_by_email(self, request: GetUserByEmailRequest, timeout: Optional[timedelta] = None) -> User:
        """GetUserByEmail method.

        Args:
            request: The GetUserByEmail request message
            timeout: Optional timeout override for this call

        Returns:
            The GetUserByEmail response message

        Raises:
            grpc.RpcError: If the gRPC call fails
            ValueError: If authentication credentials are missing
        """
        return self._execute_method("GetUserByEmail", request, timeout)

    def list_users(self, request: ListUsersRequest, timeout: Optional[timedelta] = None) -> ListUsersResponse:
        """ListUsers method.

        Args:
            request: The ListUsers request message
            timeout: Optional timeout override for this call

        Returns:
            The ListUsers response message

        Raises:
            grpc.RpcError: If the gRPC call fails
            ValueError: If authentication credentials are missing
        """
        return self._execute_method("ListUsers", request, timeout)

    def create_user(self, request: CreateUserRequest, timeout: Optional[timedelta] = None) -> User:
        """CreateUser method.

        Args:
            request: The CreateUser request message
            timeout: Optional timeout override for this call

        Returns:
            The CreateUser response message

        Raises:
            grpc.RpcError: If the gRPC call fails
            ValueError: If authentication credentials are missing
        """
        return self._execute_method("CreateUser", request, timeout)

    def update_user(self, request: UpdateUserRequest, timeout: Optional[timedelta] = None) -> User:
        """UpdateUser method.

        Args:
            request: The UpdateUser request message
            timeout: Optional timeout override for this call

        Returns:
            The UpdateUser response message

        Raises:
            grpc.RpcError: If the gRPC call fails
            ValueError: If authentication credentials are missing
        """
        return self._execute_method("UpdateUser", request, timeout)


# Create aliases to match expected exports
UserServiceGRPCClient = UserService
UserServiceGRPCClientInterface = UserService
