function get_filecloud_client
Singleton factory function that returns a globally cached FileCloud API client instance, handling initialization, authentication, and re-authentication as needed.
/tf/active/vicechatdev/CDocs/controllers/filecloud_controller.py
42 - 80
moderate
Purpose
This function implements a singleton pattern to manage a single FileCloud API client connection throughout the application lifecycle. It initializes the client on first call using configuration settings, authenticates with the FileCloud server, and maintains the connection. On subsequent calls, it returns the cached client instance and re-authenticates if the session has expired. This ensures efficient resource usage by avoiding multiple connections and provides a centralized point for FileCloud API access.
Source Code
def get_filecloud_client() -> FileCloudAPI:
"""
Get or initialize the FileCloud API client.
Returns:
Initialized FileCloud API client
Raises:
IntegrationError: If connection to FileCloud fails
"""
global _filecloud_client
if _filecloud_client is None:
try:
# Initialize client from settings
_filecloud_client = FileCloudAPI(
server_url=settings.FILECLOUD_SERVER_URL,
username=settings.FILECLOUD_USERNAME,
password=settings.FILECLOUD_PASSWORD
)
# Authenticate
if not _filecloud_client.login():
logger.error("Failed to authenticate with FileCloud")
raise IntegrationError("Failed to authenticate with FileCloud")
logger.info("Successfully connected to FileCloud server")
except Exception as e:
logger.error(f"Error initializing FileCloud client: {e}")
raise IntegrationError(f"Error connecting to FileCloud: {e}")
# If client exists but is not authenticated, try to re-authenticate
if not _filecloud_client.authenticated:
if not _filecloud_client.login():
logger.error("Failed to re-authenticate with FileCloud")
raise IntegrationError("Failed to re-authenticate with FileCloud")
return _filecloud_client
Return Value
Type: FileCloudAPI
Returns an initialized and authenticated FileCloudAPI client instance. The client is ready to perform FileCloud operations such as file uploads, downloads, and management. The same instance is returned on subsequent calls (singleton pattern).
Dependencies
loggingCDocs.config.settingsCDocs.utils.FC_api.FileCloudAPICDocs.controllers.IntegrationError
Required Imports
import logging
from CDocs.config import settings
from CDocs.utils.FC_api import FileCloudAPI
from CDocs.controllers import IntegrationError
Usage Example
# Module-level setup required
import logging
from CDocs.config import settings
from CDocs.utils.FC_api import FileCloudAPI
from CDocs.controllers import IntegrationError
logger = logging.getLogger(__name__)
_filecloud_client = None
# Using the function
try:
client = get_filecloud_client()
# Now use the client for FileCloud operations
files = client.list_files('/path/to/folder')
# Subsequent calls return the same authenticated client
same_client = get_filecloud_client()
assert client is same_client # True - singleton pattern
except IntegrationError as e:
logger.error(f"Failed to connect to FileCloud: {e}")
# Handle connection failure
Best Practices
- Ensure the global variable _filecloud_client is declared at module level before calling this function
- Configure a logger instance at module level for proper error tracking
- Set all required FileCloud configuration values in settings before calling this function
- Handle IntegrationError exceptions when calling this function to gracefully manage connection failures
- The function automatically handles re-authentication if the session expires, but be aware of potential delays
- This is a singleton pattern - the same client instance is shared across all calls, so be mindful of thread safety in multi-threaded environments
- Do not manually modify the _filecloud_client global variable as it may break the singleton pattern
- Consider implementing connection pooling or retry logic at a higher level if dealing with unreliable network conditions
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function reset_filecloud_client 73.0% similar
-
function _get_filecloud_integration_v1 71.0% similar
-
function _get_filecloud_integration 70.8% similar
-
class FileCloudClient 70.2% similar
-
class FileCloudClient_v1 69.5% similar