Skip to content

Commit 2588725

Browse files
Refactoring
1 parent dbd89d1 commit 2588725

4 files changed

Lines changed: 72 additions & 199 deletions

File tree

samples/authX-pro/create.ipynb

Lines changed: 23 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
"api_prefix = 'authX-pro-' # ENTER A PREFIX FOR THE APIS TO REDUCE COLLISION POTENTIAL WITH OTHER SAMPLES\n",
3434
"tags = ['authX-pro', 'jwt', 'policy-fragment'] # ENTER DESCRIPTIVE TAG(S)\n",
3535
"\n",
36-
"\n",
37-
"\n",
3836
"# ------------------------------\n",
3937
"# SYSTEM CONFIGURATION\n",
4038
"# ------------------------------\n",
@@ -49,18 +47,7 @@
4947
" INFRASTRUCTURE.APPGW_APIM_PE,\n",
5048
" INFRASTRUCTURE.SIMPLE_APIM\n",
5149
"]\n",
52-
"nb_helper = utils.NotebookHelper(\n",
53-
" sample_folder,\n",
54-
" rg_name,\n",
55-
" rg_location,\n",
56-
" deployment,\n",
57-
" supported_infras,\n",
58-
" True,\n",
59-
" index = index,\n",
60-
" apim_sku = apim_sku\n",
61-
")\n",
62-
"\n",
63-
"# Define the APIs and their operations and policies\n",
50+
"nb_helper = utils.NotebookHelper(sample_folder, rg_name, rg_location, deployment, supported_infras, True, index=index, apim_sku=apim_sku)\n",
6451
"\n",
6552
"# Set up the named values\n",
6653
"nvs: List[NamedValue] = [\n",
@@ -71,70 +58,50 @@
7158
"]\n",
7259
"\n",
7360
"# Set up the policy fragments\n",
74-
"pf_authx_hr_member_xml = utils.read_policy_xml('pf-authx-hr-member.xml', {\n",
75-
" 'jwt_signing_key' : nb_helper.jwt_key_name,\n",
76-
" 'hr_member_role_id' : 'HRMemberRoleId'\n",
77-
"}, sample_folder)\n",
78-
"\n",
61+
"pf_authx_hr_member_xml = utils.read_policy_xml(\n",
62+
" 'pf-authx-hr-member.xml', {'jwt_signing_key': nb_helper.jwt_key_name, 'hr_member_role_id': 'HRMemberRoleId'}, sample_folder\n",
63+
")\n",
7964
"pfs: List[PolicyFragment] = [\n",
8065
" PolicyFragment('AuthX-HR-Member', pf_authx_hr_member_xml, 'Authenticates and authorizes HR members.')\n",
8166
"]\n",
8267
"\n",
8368
"# Define the Products\n",
84-
"pol_hr_product = utils.read_policy_xml('hr_product.xml', {\n",
85-
" 'jwt_signing_key' : nb_helper.jwt_key_name,\n",
86-
" 'hr_member_role_id' : 'HRMemberRoleId'\n",
87-
"}, sample_folder)\n",
88-
"\n",
69+
"pol_hr_product = utils.read_policy_xml(\n",
70+
" 'hr_product.xml', {'jwt_signing_key': nb_helper.jwt_key_name, 'hr_member_role_id': 'HRMemberRoleId'}, sample_folder\n",
71+
")\n",
8972
"hr_product_name = 'hr'\n",
9073
"products: List[Product] = [\n",
9174
" Product(\n",
92-
" hr_product_name,\n",
93-
" 'Human Resources',\n",
94-
" 'Product for Human Resources APIs providing access to employee data, organizational structure, '\n",
95-
" 'benefits information, and HR management services. Includes JWT-based authentication for HR members.',\n",
96-
" 'published',\n",
97-
" True,\n",
98-
" False,\n",
99-
" pol_hr_product\n",
75+
" hr_product_name, 'Human Resources',\n",
76+
" 'Product for Human Resources APIs providing access to employee data, organizational structure, benefits information, and HR management '\n",
77+
" 'services. Includes JWT-based authentication for HR members.',\n",
78+
" 'published', True, False, pol_hr_product\n",
10079
" )\n",
10180
"]\n",
10281
"\n",
10382
"# Define the APIs and their operations and policies\n",
104-
"pol_hr_all_operations_pro = utils.read_policy_xml('hr_all_operations_pro.xml', sample_name = sample_folder)\n",
105-
"pol_hr_get = utils.read_policy_xml('hr_get.xml', sample_name = sample_folder)\n",
106-
"pol_hr_post = utils.read_policy_xml('hr_post.xml', sample_name = sample_folder)\n",
83+
"pol_hr_all_operations_pro = utils.read_policy_xml('hr_all_operations_pro.xml', sample_name=sample_folder)\n",
84+
"pol_hr_get = utils.read_policy_xml('hr_get.xml', sample_name=sample_folder)\n",
85+
"pol_hr_post = utils.read_policy_xml('hr_post.xml', sample_name=sample_folder)\n",
10786
"\n",
10887
"# API 1: Employees (HR)\n",
10988
"hr_employees_path = f'{api_prefix}employees'\n",
110-
"hr_employees_get = GET_APIOperation('Gets the employees', pol_hr_get,)\n",
89+
"hr_employees_get = GET_APIOperation('Gets the employees', pol_hr_get)\n",
11190
"hr_employees_post = POST_APIOperation('Creates a new employee', pol_hr_post)\n",
112-
"hr_employees = API(\n",
113-
" hr_employees_path,\n",
114-
" 'Employees Pro',\n",
115-
" hr_employees_path,\n",
116-
" 'This is a Human Resources API for employee information',\n",
117-
" pol_hr_all_operations_pro,\n",
118-
" operations = [hr_employees_get, hr_employees_post],\n",
119-
" tags = tags,\n",
120-
" productNames = [hr_product_name],\n",
121-
" subscriptionRequired = False\n",
91+
"hr_employees = API(\n",
92+
" hr_employees_path, 'Employees Pro', hr_employees_path, 'This is a Human Resources API for employee information',\n",
93+
" pol_hr_all_operations_pro, [hr_employees_get, hr_employees_post], tags,\n",
94+
" productNames=[hr_product_name], subscriptionRequired=False\n",
12295
")\n",
12396
"\n",
12497
"# API 2: Benefits (HR)\n",
12598
"hr_benefits_path = f'{api_prefix}benefits'\n",
12699
"hr_benefits_get = GET_APIOperation('Gets employee benefits', pol_hr_get)\n",
127100
"hr_benefits_post = POST_APIOperation('Creates employee benefits', pol_hr_post)\n",
128-
"hr_benefits = API(\n",
129-
" hr_benefits_path,\n",
130-
" 'Benefits Pro',\n",
131-
" hr_benefits_path,\n",
132-
" 'This is a Human Resources API for employee benefits',\n",
133-
" pol_hr_all_operations_pro,\n",
134-
" operations = [hr_benefits_get, hr_benefits_post],\n",
135-
" tags = tags,\n",
136-
" productNames = [hr_product_name],\n",
137-
" subscriptionRequired = False\n",
101+
"hr_benefits = API(\n",
102+
" hr_benefits_path, 'Benefits Pro', hr_benefits_path, 'This is a Human Resources API for employee benefits',\n",
103+
" pol_hr_all_operations_pro, [hr_benefits_get, hr_benefits_post], tags,\n",
104+
" productNames=[hr_product_name], subscriptionRequired=False\n",
138105
")\n",
139106
"\n",
140107
"# APIs Array\n",

samples/authX/create.ipynb

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
"api_prefix = 'authX-' # ENTER A PREFIX FOR THE APIS TO REDUCE COLLISION POTENTIAL WITH OTHER SAMPLES\n",
3434
"tags = ['authX', 'jwt', 'hr'] # ENTER DESCRIPTIVE TAG(S)\n",
3535
"\n",
36-
"\n",
37-
"\n",
3836
"# ------------------------------\n",
3937
"# SYSTEM CONFIGURATION\n",
4038
"# ------------------------------\n",
@@ -49,18 +47,7 @@
4947
" INFRASTRUCTURE.APPGW_APIM_PE,\n",
5048
" INFRASTRUCTURE.SIMPLE_APIM\n",
5149
"]\n",
52-
"nb_helper = utils.NotebookHelper(\n",
53-
" sample_folder,\n",
54-
" rg_name,\n",
55-
" rg_location,\n",
56-
" deployment,\n",
57-
" supported_infras,\n",
58-
" True,\n",
59-
" index = index,\n",
60-
" apim_sku = apim_sku\n",
61-
")\n",
62-
"\n",
63-
"# Define the APIs and their operations and policies\n",
50+
"nb_helper = utils.NotebookHelper(sample_folder, rg_name, rg_location, deployment, supported_infras, True, index=index, apim_sku=apim_sku)\n",
6451
"\n",
6552
"# Set up the named values\n",
6653
"nvs: List[NamedValue] = [\n",
@@ -71,31 +58,23 @@
7158
"]\n",
7259
"\n",
7360
"# Named values must be set up a bit differently as they need to have two surrounding curly braces\n",
74-
"pol_hr_all_operations = utils.read_policy_xml('hr_all_operations.xml', sample_name = sample_folder).format(\n",
75-
" jwt_signing_key = '{{' + nb_helper.jwt_key_name + '}}',\n",
76-
" hr_member_role_id = '{{HRMemberRoleId}}'\n",
77-
")\n",
78-
"pol_hr_get = utils.read_policy_xml('hr_get.xml', sample_name = sample_folder).format(\n",
79-
" hr_administrator_role_id = '{{HRAdministratorRoleId}}',\n",
80-
" hr_associate_role_id = '{{HRAssociateRoleId}}'\n",
61+
"pol_hr_all_operations = utils.read_policy_xml('hr_all_operations.xml', sample_name=sample_folder).format(\n",
62+
" jwt_signing_key='{{' + nb_helper.jwt_key_name + '}}',\n",
63+
" hr_member_role_id='{{HRMemberRoleId}}'\n",
8164
")\n",
82-
"pol_hr_post = utils.read_policy_xml('hr_post.xml', sample_name = sample_folder).format(\n",
83-
" hr_administrator_role_id = '{{HRAdministratorRoleId}}'\n",
65+
"pol_hr_get = utils.read_policy_xml('hr_get.xml', sample_name=sample_folder).format(\n",
66+
" hr_administrator_role_id='{{HRAdministratorRoleId}}',\n",
67+
" hr_associate_role_id='{{HRAssociateRoleId}}'\n",
8468
")\n",
69+
"pol_hr_post = utils.read_policy_xml('hr_post.xml', sample_name=sample_folder).format(hr_administrator_role_id='{{HRAdministratorRoleId}}')\n",
8570
"\n",
8671
"# API 1: Employees (HR)\n",
8772
"hr_employees_path = f'{api_prefix}employees'\n",
8873
"hr_employees_get = GET_APIOperation('Gets the employees', pol_hr_get)\n",
8974
"hr_employees_post = POST_APIOperation('Creates a new employee', pol_hr_post)\n",
90-
"hr_employees = API(\n",
91-
" hr_employees_path,\n",
92-
" 'Employees',\n",
93-
" hr_employees_path,\n",
94-
" 'This is a Human Resources API to obtain employee information',\n",
95-
" pol_hr_all_operations,\n",
96-
" operations = [hr_employees_get, hr_employees_post],\n",
97-
" tags = tags,\n",
98-
" subscriptionRequired = True\n",
75+
"hr_employees = API(\n",
76+
" hr_employees_path, 'Employees', hr_employees_path, 'This is a Human Resources API to obtain employee information',\n",
77+
" pol_hr_all_operations, [hr_employees_get, hr_employees_post], tags\n",
9978
")\n",
10079
"\n",
10180
"# APIs Array\n",

samples/dynamic-cors/create.ipynb

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,7 @@
5151
" INFRASTRUCTURE.APPGW_APIM_PE,\n",
5252
" INFRASTRUCTURE.SIMPLE_APIM\n",
5353
"]\n",
54-
"nb_helper = utils.NotebookHelper(\n",
55-
" sample_folder,\n",
56-
" rg_name,\n",
57-
" rg_location,\n",
58-
" deployment,\n",
59-
" supported_infras,\n",
60-
" index = index,\n",
61-
" apim_sku = apim_sku\n",
62-
")\n",
54+
"nb_helper = utils.NotebookHelper(sample_folder, rg_name, rg_location, deployment, supported_infras, index = index, apim_sku = apim_sku)\n",
6355
"\n",
6456
"# API paths (2 per option, all coexisting side-by-side)\n",
6557
"bl_products_path = f'{api_prefix}bl-products'\n",
@@ -125,11 +117,9 @@
125117
"pol_baseline = utils.read_policy_xml('cors-baseline-native.xml', sample_name = sample_folder)\n",
126118
"\n",
127119
"apis_baseline: List[API] = [\n",
128-
" API(bl_products_path, 'Baseline Products', bl_products_path,\n",
129-
" 'Products API with native <cors> policy (static origins)',\n",
120+
" API(bl_products_path, 'Baseline Products', bl_products_path, 'Products API with native <cors> policy (static origins)',\n",
130121
" pol_baseline, [cors_get_op, cors_options_op], tags, subscriptionRequired = False),\n",
131-
" API(bl_analytics_path, 'Baseline Analytics', bl_analytics_path,\n",
132-
" 'Analytics API with native <cors> policy (static origins)',\n",
122+
" API(bl_analytics_path, 'Baseline Analytics', bl_analytics_path, 'Analytics API with native <cors> policy (static origins)',\n",
133123
" pol_baseline, [cors_get_op, cors_options_op], tags, subscriptionRequired = False),\n",
134124
"]\n",
135125
"\n",
@@ -144,11 +134,9 @@
144134
"pol_api_opt1 = pol_api_opt1.replace('{fragment_id}', 'DynamicCorsHardcoded')\n",
145135
"\n",
146136
"apis_opt1: List[API] = [\n",
147-
" API(opt1_products_path, 'Option 1 Products', opt1_products_path,\n",
148-
" 'Products API with hard-coded CORS fragment',\n",
137+
" API(opt1_products_path, 'Option 1 Products', opt1_products_path, 'Products API with hard-coded CORS fragment',\n",
149138
" pol_api_opt1, [cors_get_op, cors_options_op], tags, subscriptionRequired = False),\n",
150-
" API(opt1_analytics_path, 'Option 1 Analytics', opt1_analytics_path,\n",
151-
" 'Analytics API with hard-coded CORS fragment',\n",
139+
" API(opt1_analytics_path, 'Option 1 Analytics', opt1_analytics_path, 'Analytics API with hard-coded CORS fragment',\n",
152140
" pol_api_opt1, [cors_get_op, cors_options_op], tags, subscriptionRequired = False),\n",
153141
"]\n",
154142
"\n",
@@ -162,11 +150,9 @@
162150
"pol_api_opt2 = pol_api_opt2.replace('{fragment_id}', 'DynamicCorsNamedValues')\n",
163151
"\n",
164152
"apis_opt2: List[API] = [\n",
165-
" API(opt2_products_path, 'Option 2 Products', opt2_products_path,\n",
166-
" 'Products API with Named Values CORS fragment',\n",
153+
" API(opt2_products_path, 'Option 2 Products', opt2_products_path, 'Products API with Named Values CORS fragment',\n",
167154
" pol_api_opt2, [cors_get_op, cors_options_op], tags, subscriptionRequired = False),\n",
168-
" API(opt2_analytics_path, 'Option 2 Analytics', opt2_analytics_path,\n",
169-
" 'Analytics API with Named Values CORS fragment',\n",
155+
" API(opt2_analytics_path, 'Option 2 Analytics', opt2_analytics_path, 'Analytics API with Named Values CORS fragment',\n",
170156
" pol_api_opt2, [cors_get_op, cors_options_op], tags, subscriptionRequired = False),\n",
171157
"]\n",
172158
"\n",
@@ -184,11 +170,9 @@
184170
"cache_key_param = [{'name': 'cacheKey', 'required': True, 'type': 'string', 'description': 'The cache key to operate on'}]\n",
185171
"\n",
186172
"apis_opt3: List[API] = [\n",
187-
" API(opt3_products_path, 'Option 3 Products', opt3_products_path,\n",
188-
" 'Products API with cache-backed CORS fragment',\n",
173+
" API(opt3_products_path, 'Option 3 Products', opt3_products_path, 'Products API with cache-backed CORS fragment',\n",
189174
" pol_api_opt3, [cors_get_op, cors_options_op], tags, subscriptionRequired = False),\n",
190-
" API(opt3_analytics_path, 'Option 3 Analytics', opt3_analytics_path,\n",
191-
" 'Analytics API with cache-backed CORS fragment',\n",
175+
" API(opt3_analytics_path, 'Option 3 Analytics', opt3_analytics_path, 'Analytics API with cache-backed CORS fragment',\n",
192176
" pol_api_opt3, [cors_get_op, cors_options_op], tags, subscriptionRequired = False),\n",
193177
"]\n",
194178
"\n",
@@ -214,31 +198,24 @@
214198
"pol_api_opt4 = pol_api_opt4.replace('{fragment_id}', 'DynamicCorsCachedPerApi')\n",
215199
"\n",
216200
"apis_opt4: List[API] = [\n",
217-
" API(opt4_products_path, 'Option 4 Products', opt4_products_path,\n",
218-
" 'Products API with per-API cache-backed CORS fragment',\n",
201+
" API(opt4_products_path, 'Option 4 Products', opt4_products_path, 'Products API with per-API cache-backed CORS fragment',\n",
219202
" pol_api_opt4, [cors_get_op, cors_options_op], tags, subscriptionRequired = False),\n",
220-
" API(opt4_analytics_path, 'Option 4 Analytics', opt4_analytics_path,\n",
221-
" 'Analytics API with per-API cache-backed CORS fragment',\n",
203+
" API(opt4_analytics_path, 'Option 4 Analytics', opt4_analytics_path, 'Analytics API with per-API cache-backed CORS fragment',\n",
222204
" pol_api_opt4, [cors_get_op, cors_options_op], tags, subscriptionRequired = False),\n",
223205
"]\n",
224206
"\n",
225207
"\n",
226208
"# ---- Option 5: Per-API Named Value fragment ----\n",
227209
"\n",
228210
"pf_nv_per_api_xml = utils.read_policy_xml('pf-dynamic-cors-named-values-per-api.xml', sample_name = sample_folder)\n",
229-
"\n",
230211
"pol_api_nv = utils.read_policy_xml('cors-api-policy-named-values.xml', sample_name = sample_folder)\n",
231-
"\n",
232212
"pol_api_opt5_products = pol_api_nv.replace('{allowed_origins_nv}', f'{{{{{opt5_nv_products_name}}}}}')\n",
233-
"\n",
234213
"pol_api_opt5_analytics = pol_api_nv.replace('{allowed_origins_nv}', f'{{{{{opt5_nv_analytics_name}}}}}')\n",
235214
"\n",
236215
"apis_opt5: List[API] = [\n",
237-
" API(opt5_products_path, 'Option 5 Products', opt5_products_path,\n",
238-
" 'Products API with per-API Named Value CORS fragment',\n",
216+
" API(opt5_products_path, 'Option 5 Products', opt5_products_path, 'Products API with per-API Named Value CORS fragment',\n",
239217
" pol_api_opt5_products, [cors_get_op, cors_options_op], tags, subscriptionRequired = False),\n",
240-
" API(opt5_analytics_path, 'Option 5 Analytics', opt5_analytics_path,\n",
241-
" 'Analytics API with per-API Named Value CORS fragment',\n",
218+
" API(opt5_analytics_path, 'Option 5 Analytics', opt5_analytics_path, 'Analytics API with per-API Named Value CORS fragment',\n",
242219
" pol_api_opt5_analytics, [cors_get_op, cors_options_op], tags, subscriptionRequired = False),\n",
243220
"]\n",
244221
"\n",
@@ -268,7 +245,6 @@
268245
"# Combine all APIs for a single deployment\n",
269246
"all_apis = apis_baseline + apis_opt1 + apis_opt2 + apis_opt3 + apis_opt4 + apis_opt5 + [api_admin]\n",
270247
"\n",
271-
"\n",
272248
"print_ok('Notebook initialized')"
273249
]
274250
},

0 commit comments

Comments
 (0)