7
7
A set of utils to go through c2j models and their corresponding endpoint rules
8
8
"""
9
9
import datetime
10
+ import json
10
11
import os
11
12
import re
12
13
18
19
"transcribe-streaming" : "transcribestreaming" ,
19
20
"streams.dynamodb" : "dynamodbstreams" }
20
21
22
+ SMITHY_EXCLUSION_CLIENTS = {
23
+ # multi auth
24
+ "eventbridge"
25
+ , "cloudfront-keyvaluestore"
26
+ , "cognito-identity"
27
+ , "cognito-idp"
28
+ # customization
29
+ , "machinelearning"
30
+ , "apigatewayv2"
31
+ , "apigateway"
32
+ , "eventbridge"
33
+ , "glacier"
34
+ , "lambda"
35
+ , "polly"
36
+ , "sqs"
37
+ # bearer token
38
+ # ,"codecatalyst"
39
+ # bidirectional streaming
40
+ , "lexv2-runtime"
41
+ , "qbusiness"
42
+ , "transcribestreaming"
43
+ }
44
+
21
45
# Regexp to parse C2J model filename to extract service name and date version
22
46
SERVICE_MODEL_FILENAME_PATTERN = re .compile (
23
47
"^"
29
53
30
54
class ServiceModel (object ):
31
55
# A helper class to store C2j model info and metadata (endpoint rules and tests)
32
- def __init__ (self , service_id , c2j_model , endpoint_rule_set , endpoint_tests ):
56
+ def __init__ (self , service_id : str , c2j_model : str , endpoint_rule_set : str , endpoint_tests : str , use_smithy : bool ):
33
57
self .service_id = service_id # For debugging purposes, not used atm
34
58
# only filenames, no filesystem path
35
59
self .c2j_model = c2j_model
36
60
self .endpoint_rule_set = endpoint_rule_set
37
61
self .endpoint_tests = endpoint_tests
62
+ self .use_smithy = use_smithy
38
63
39
64
40
65
class ModelUtils (object ):
@@ -113,7 +138,8 @@ def _collect_available_models(models_dir: str, endpoint_rules_dir: str) -> dict:
113
138
114
139
# fetch endpoint-rules filename which is based on ServiceId in c2j models:
115
140
try :
116
- service_name_to_model_filename [key ] = ModelUtils ._build_service_model (endpoint_rules_dir ,
141
+ service_name_to_model_filename [key ] = ModelUtils ._build_service_model (models_dir ,
142
+ endpoint_rules_dir ,
117
143
model_file_date [0 ])
118
144
119
145
if key == "s3" :
@@ -125,7 +151,8 @@ def _collect_available_models(models_dir: str, endpoint_rules_dir: str) -> dict:
125
151
service_name_to_model_filename [key ] = ServiceModel (service_id = key ,
126
152
c2j_model = model_file_date [0 ],
127
153
endpoint_rule_set = None ,
128
- endpoint_tests = None )
154
+ endpoint_tests = None ,
155
+ use_smithy = False )
129
156
if missing :
130
157
# TODO: re-enable with endpoints introduction
131
158
# print(f"Missing endpoints for services: {missing}")
@@ -137,7 +164,25 @@ def _collect_available_models(models_dir: str, endpoint_rules_dir: str) -> dict:
137
164
return service_name_to_model_filename
138
165
139
166
@staticmethod
140
- def _build_service_model (endpoint_rules_dir : str , c2j_model_filename ) -> ServiceModel :
167
+ def is_smithy_enabled (service_id , models_dir , c2j_model_filename ):
168
+ """Return true if given service id and c2j model file should enable smithy client generation path
169
+
170
+ :param service_id:
171
+ :param models_dir:
172
+ :param c2j_model_filename:
173
+ :return:
174
+ """
175
+ use_smithy = False
176
+ if service_id not in SMITHY_EXCLUSION_CLIENTS :
177
+ with open (models_dir + "/" + c2j_model_filename , 'r' ) as json_file :
178
+ model = json .load (json_file )
179
+ model_protocol = model .get ("metadata" , dict ()).get ("protocol" , "UNKNOWN_PROTOCOL" )
180
+ if model_protocol in {"json" , "rest-json" }:
181
+ use_smithy = True
182
+ return use_smithy
183
+
184
+ @staticmethod
185
+ def _build_service_model (models_dir : str , endpoint_rules_dir : str , c2j_model_filename ) -> ServiceModel :
141
186
"""Return a ServiceModel containing paths to the Service models: C2J model and endpoints (rules and tests).
142
187
143
188
:param models_dir (str): filepath (absolute or relative) to the dir with c2j models
@@ -153,8 +198,11 @@ def _build_service_model(endpoint_rules_dir: str, c2j_model_filename) -> Service
153
198
match = SERVICE_MODEL_FILENAME_PATTERN .match (c2j_model_filename )
154
199
service_id = match .group ("service" )
155
200
201
+ use_smithy = ModelUtils .is_smithy_enabled (service_id , models_dir , c2j_model_filename )
202
+
156
203
if os .path .exists (endpoint_rules_filepath ) and os .path .exists (endpoint_tests_filepath ):
157
204
return ServiceModel (service_id = service_id ,
158
205
c2j_model = c2j_model_filename ,
159
206
endpoint_rule_set = endpoint_rules_filename ,
160
- endpoint_tests = endpoint_tests_filename )
207
+ endpoint_tests = endpoint_tests_filename ,
208
+ use_smithy = use_smithy )
0 commit comments