|
| 1 | +"""SAP Ariba Output Management Service SDK for Python.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +import os |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | +from .client import OutputManagementClient |
| 8 | +from ._service_client import OutputManagementServiceClient |
| 9 | +from ._models import ( |
| 10 | + OutputRequest, |
| 11 | + OutputRequestBuilder, |
| 12 | + OutputResponse, |
| 13 | + EmailConfiguration, |
| 14 | + AttachmentConfig, |
| 15 | + OutputManagementInfo, |
| 16 | + OutputRequestData, |
| 17 | + DirectShareConfiguration, |
| 18 | + FormConfiguration, |
| 19 | + PreGeneratedAttachment, |
| 20 | +) |
| 21 | +from .config import DestinationCredentialConfig |
| 22 | +from .constants import FileFormat, Channel |
| 23 | +from .exceptions import ( |
| 24 | + OutputManagementException, |
| 25 | + AuthenticationException, |
| 26 | + ValidationException, |
| 27 | + NetworkException, |
| 28 | + DestinationNotFoundException, |
| 29 | + DestinationAccessException, |
| 30 | +) |
| 31 | + |
| 32 | +logger = logging.getLogger(__name__) |
| 33 | + |
| 34 | + |
| 35 | +def create_client( |
| 36 | + destination_credential_config: Optional[DestinationCredentialConfig] = None, |
| 37 | + destination_name: Optional[str] = None, |
| 38 | + access_strategy: Optional[str] = None, |
| 39 | + instance: Optional[str] = None, |
| 40 | +) -> OutputManagementClient: |
| 41 | + """ |
| 42 | + Create an Output Management client with configuration from environment or parameters. |
| 43 | +
|
| 44 | + This is the recommended factory function for creating clients. It follows the SDK's |
| 45 | + standard pattern of reading configuration from environment variables with optional overrides. |
| 46 | +
|
| 47 | + Environment Variables: |
| 48 | + - CLOUD_SDK_OMS_DESTINATION_NAME: Default destination name |
| 49 | + - CLOUD_SDK_OMS_ACCESS_STRATEGY: Default access strategy (PROVIDER_ONLY or SUBSCRIBER_ONLY) |
| 50 | + - CLOUD_SDK_OMS_INSTANCE: Default destination service instance name |
| 51 | +
|
| 52 | + Args: |
| 53 | + destination_credential_config: Pre-configured DestinationCredentialConfig object. |
| 54 | + If provided, other parameters are ignored. |
| 55 | + destination_name: Name of the destination. If not provided, reads from |
| 56 | + CLOUD_SDK_OMS_DESTINATION_NAME environment variable. |
| 57 | + access_strategy: Destination access strategy. If not provided, reads from |
| 58 | + CLOUD_SDK_OMS_ACCESS_STRATEGY environment variable or defaults to "PROVIDER_ONLY". |
| 59 | + instance: Destination service instance name. If not provided, reads from |
| 60 | + CLOUD_SDK_OMS_INSTANCE environment variable or defaults to "default". |
| 61 | +
|
| 62 | + Returns: |
| 63 | + Configured OutputManagementClient instance |
| 64 | +
|
| 65 | + Raises: |
| 66 | + ValidationException: If destination_name is not provided and not found in environment |
| 67 | + (when destination_credential_config is not provided) |
| 68 | +
|
| 69 | + Example: |
| 70 | + ```python |
| 71 | + from sap_cloud_sdk.outputmanagement import create_client, DestinationCredentialConfig |
| 72 | +
|
| 73 | + # Using environment variables |
| 74 | + client = create_client() |
| 75 | +
|
| 76 | + # With explicit parameters |
| 77 | + client = create_client( |
| 78 | + destination_name="MY_OMS_DESTINATION", |
| 79 | + access_strategy="PROVIDER_ONLY", |
| 80 | + instance="default" |
| 81 | + ) |
| 82 | +
|
| 83 | + # With DestinationCredentialConfig |
| 84 | + config = DestinationCredentialConfig( |
| 85 | + destination_name="MY_OMS_DESTINATION", |
| 86 | + access_strategy="PROVIDER_ONLY", |
| 87 | + instance="default" |
| 88 | + ) |
| 89 | + client = create_client(destination_credential_config=config) |
| 90 | +
|
| 91 | + # Use the client's 4 methods |
| 92 | + response = client.send_email( |
| 93 | + notification_template_key="PO_NOTIFICATION", |
| 94 | + to=["user@example.com"], |
| 95 | + business_document={"PurchaseOrder": {"id": "PO-123"}} |
| 96 | + ) |
| 97 | + ``` |
| 98 | + """ |
| 99 | + # If destination_credential_config is provided, use it directly |
| 100 | + if destination_credential_config is not None: |
| 101 | + destination_config = destination_credential_config |
| 102 | + logger.info( |
| 103 | + f"Creating Output Management client with provided DestinationCredentialConfig: " |
| 104 | + f"destination '{destination_config.destination_name}'" |
| 105 | + ) |
| 106 | + else: |
| 107 | + # Validate and build config from individual parameters |
| 108 | + # Read from environment variables with parameter overrides |
| 109 | + dest_name = destination_name or os.getenv("CLOUD_SDK_OMS_DESTINATION_NAME") |
| 110 | + access_strat = access_strategy or os.getenv( |
| 111 | + "CLOUD_SDK_OMS_ACCESS_STRATEGY", "PROVIDER_ONLY" |
| 112 | + ) |
| 113 | + inst = instance or os.getenv("CLOUD_SDK_OMS_INSTANCE", "default") |
| 114 | + |
| 115 | + if not dest_name: |
| 116 | + raise ValidationException( |
| 117 | + "Destination name must be provided either as parameter or via " |
| 118 | + "CLOUD_SDK_OMS_DESTINATION_NAME environment variable", |
| 119 | + error_code="MISSING_DESTINATION_NAME", |
| 120 | + ) |
| 121 | + |
| 122 | + logger.info( |
| 123 | + f"Creating Output Management client with destination '{dest_name}', " |
| 124 | + f"access_strategy '{access_strat}', instance '{inst}'" |
| 125 | + ) |
| 126 | + |
| 127 | + # Create destination config |
| 128 | + destination_config = DestinationCredentialConfig( |
| 129 | + destination_name=dest_name, |
| 130 | + access_strategy=access_strat, |
| 131 | + instance=inst, |
| 132 | + ) |
| 133 | + |
| 134 | + # Get the destination object |
| 135 | + http_destination = destination_config.get_destination() |
| 136 | + |
| 137 | + # Get the base URL from destination |
| 138 | + base_url = destination_config.get_base_url() |
| 139 | + logger.info(f"Retrieved destination base URL: {base_url}") |
| 140 | + |
| 141 | + # Create service client directly |
| 142 | + service_client = OutputManagementServiceClient( |
| 143 | + base_url=base_url, |
| 144 | + destination=http_destination, |
| 145 | + destination_instance=destination_config.instance or "default", |
| 146 | + ) |
| 147 | + |
| 148 | + # Wrap it in the unified OutputManagementClient |
| 149 | + return OutputManagementClient(service_client) |
| 150 | + |
| 151 | + |
| 152 | +__all__ = [ |
| 153 | + # Main client and factory function |
| 154 | + "OutputManagementClient", |
| 155 | + "create_client", |
| 156 | + # Models |
| 157 | + "OutputRequest", |
| 158 | + "OutputRequestBuilder", |
| 159 | + "OutputResponse", |
| 160 | + "EmailConfiguration", |
| 161 | + "AttachmentConfig", |
| 162 | + "OutputManagementInfo", |
| 163 | + "OutputRequestData", |
| 164 | + "DirectShareConfiguration", |
| 165 | + "FormConfiguration", |
| 166 | + "PreGeneratedAttachment", |
| 167 | + # Configuration |
| 168 | + "DestinationCredentialConfig", |
| 169 | + # Constants/Enums |
| 170 | + "FileFormat", |
| 171 | + "Channel", |
| 172 | + # Exceptions |
| 173 | + "OutputManagementException", |
| 174 | + "AuthenticationException", |
| 175 | + "ValidationException", |
| 176 | + "NetworkException", |
| 177 | + "DestinationNotFoundException", |
| 178 | + "DestinationAccessException", |
| 179 | +] |
0 commit comments