|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Example usage of templateParameters support in APIMTypes. |
| 4 | +
|
| 5 | +This example demonstrates how to create API operations with template parameters |
| 6 | +that match URL template variables used in APIM operations. |
| 7 | +""" |
| 8 | + |
| 9 | +import sys |
| 10 | +import os |
| 11 | + |
| 12 | +# Add the shared python module to the path |
| 13 | +sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'shared', 'python')) |
| 14 | + |
| 15 | +from apimtypes import API, APIOperation, GET_APIOperation2, HTTP_VERB |
| 16 | + |
| 17 | +def create_blob_access_api_example(): |
| 18 | + """ |
| 19 | + Example: Create an API with a blob access operation that uses template parameters. |
| 20 | + |
| 21 | + This matches the secure-blob-access sample where we need: |
| 22 | + - URL Template: /blobs/{blob-name} |
| 23 | + - Template Parameter: blob-name (required, string) |
| 24 | + """ |
| 25 | + |
| 26 | + # Define template parameters for the blob name |
| 27 | + blob_template_parameters = [ |
| 28 | + { |
| 29 | + "name": "blob-name", |
| 30 | + "description": "The name of the blob to access", |
| 31 | + "type": "string", |
| 32 | + "required": True |
| 33 | + } |
| 34 | + ] |
| 35 | + |
| 36 | + # Read the policy XML (in a real scenario, you'd read from the actual file) |
| 37 | + # For this example, we'll use a simple mock policy |
| 38 | + blob_policy_xml = """<policies> |
| 39 | + <inbound> |
| 40 | + <base /> |
| 41 | + <set-backend-service base-url="https://yourstorageaccount.blob.core.windows.net/container" /> |
| 42 | + <rewrite-uri template="/{{blob-name}}" /> |
| 43 | + </inbound> |
| 44 | + <backend> |
| 45 | + <base /> |
| 46 | + </backend> |
| 47 | + <outbound> |
| 48 | + <base /> |
| 49 | + </outbound> |
| 50 | + <on-error> |
| 51 | + <base /> |
| 52 | + </on-error> |
| 53 | +</policies>""" |
| 54 | + |
| 55 | + # Create the GET operation with template parameters |
| 56 | + blob_get_operation = GET_APIOperation2( |
| 57 | + name="get-blob", |
| 58 | + displayName="Get Blob", |
| 59 | + urlTemplate="/blobs/{blob-name}", |
| 60 | + description="Retrieve a blob by name", |
| 61 | + policyXml=blob_policy_xml, |
| 62 | + templateParameters=blob_template_parameters |
| 63 | + ) |
| 64 | + # Create the API with the operation (use explicit policy to avoid file path issues) |
| 65 | + simple_policy = """<policies> |
| 66 | + <inbound> |
| 67 | + <base /> |
| 68 | + </inbound> |
| 69 | + <backend> |
| 70 | + <base /> |
| 71 | + </backend> |
| 72 | + <outbound> |
| 73 | + <base /> |
| 74 | + </outbound> |
| 75 | + <on-error> |
| 76 | + <base /> |
| 77 | + </on-error> |
| 78 | +</policies>""" |
| 79 | + |
| 80 | + blob_api = API( |
| 81 | + name="blob-access-api", |
| 82 | + displayName="Blob Access API", |
| 83 | + path="/blob", |
| 84 | + description="API for secure blob access", |
| 85 | + policyXml=simple_policy, |
| 86 | + operations=[blob_get_operation] |
| 87 | + ) |
| 88 | + |
| 89 | + return blob_api |
| 90 | + |
| 91 | +def create_user_management_api_example(): |
| 92 | + """ |
| 93 | + Example: Create a user management API with multiple template parameters. |
| 94 | + """ |
| 95 | + |
| 96 | + # Template parameters for user operations |
| 97 | + user_template_parameters = [ |
| 98 | + { |
| 99 | + "name": "user-id", |
| 100 | + "description": "The unique identifier of the user", |
| 101 | + "type": "string", |
| 102 | + "required": True |
| 103 | + }, |
| 104 | + { |
| 105 | + "name": "department", |
| 106 | + "description": "The department code", |
| 107 | + "type": "string", |
| 108 | + "required": False |
| 109 | + } |
| 110 | + ] |
| 111 | + # Create a user operation using the base APIOperation class (with explicit policy) |
| 112 | + user_policy = """<policies> |
| 113 | + <inbound> |
| 114 | + <base /> |
| 115 | + </inbound> |
| 116 | + <backend> |
| 117 | + <base /> |
| 118 | + </backend> |
| 119 | + <outbound> |
| 120 | + <base /> |
| 121 | + </outbound> |
| 122 | + <on-error> |
| 123 | + <base /> |
| 124 | + </on-error> |
| 125 | +</policies>""" |
| 126 | + |
| 127 | + get_user_operation = APIOperation( |
| 128 | + name="get-user-by-dept", |
| 129 | + displayName="Get User by Department", |
| 130 | + urlTemplate="/users/{user-id}/department/{department}", |
| 131 | + method=HTTP_VERB.GET, |
| 132 | + description="Get user information filtered by department", |
| 133 | + policyXml=user_policy, |
| 134 | + templateParameters=user_template_parameters |
| 135 | + ) |
| 136 | + |
| 137 | + # Create the API (with explicit policy) |
| 138 | + user_api = API( |
| 139 | + name="user-management-api", |
| 140 | + displayName="User Management API", |
| 141 | + path="/users", |
| 142 | + description="API for user management operations", |
| 143 | + policyXml=user_policy, |
| 144 | + operations=[get_user_operation] |
| 145 | + ) |
| 146 | + |
| 147 | + return user_api |
| 148 | + |
| 149 | +def main(): |
| 150 | + """ |
| 151 | + Demonstrate the usage of templateParameters in API operations. |
| 152 | + """ |
| 153 | + |
| 154 | + print("=== APIMTypes templateParameters Usage Examples ===\n") |
| 155 | + |
| 156 | + # Example 1: Blob access API |
| 157 | + print("1. Blob Access API Example:") |
| 158 | + blob_api = create_blob_access_api_example() |
| 159 | + blob_dict = blob_api.to_dict() |
| 160 | + |
| 161 | + print(f" API Name: {blob_dict['name']}") |
| 162 | + print(f" API Path: {blob_dict['path']}") |
| 163 | + |
| 164 | + if blob_dict['operations']: |
| 165 | + operation = blob_dict['operations'][0] |
| 166 | + print(f" Operation: {operation['name']}") |
| 167 | + print(f" URL Template: {operation['urlTemplate']}") |
| 168 | + print(f" Template Parameters: {len(operation.get('templateParameters', []))}") |
| 169 | + |
| 170 | + for param in operation.get('templateParameters', []): |
| 171 | + print(f" - {param['name']}: {param['description']} (required: {param.get('required', False)})") |
| 172 | + |
| 173 | + print() |
| 174 | + |
| 175 | + # Example 2: User management API |
| 176 | + print("2. User Management API Example:") |
| 177 | + user_api = create_user_management_api_example() |
| 178 | + user_dict = user_api.to_dict() |
| 179 | + |
| 180 | + print(f" API Name: {user_dict['name']}") |
| 181 | + print(f" API Path: {user_dict['path']}") |
| 182 | + |
| 183 | + if user_dict['operations']: |
| 184 | + operation = user_dict['operations'][0] |
| 185 | + print(f" Operation: {operation['name']}") |
| 186 | + print(f" URL Template: {operation['urlTemplate']}") |
| 187 | + print(f" Template Parameters: {len(operation.get('templateParameters', []))}") |
| 188 | + |
| 189 | + for param in operation.get('templateParameters', []): |
| 190 | + print(f" - {param['name']}: {param['description']} (required: {param.get('required', False)})") |
| 191 | + |
| 192 | + print("\n=== Conversion to Bicep-compatible format ===") |
| 193 | + print("These API objects can now be serialized and passed to Bicep templates") |
| 194 | + print("that support the templateParameters property for APIM operations.") |
| 195 | + |
| 196 | +if __name__ == "__main__": |
| 197 | + main() |
0 commit comments