77A set of utils to go through c2j models and their corresponding endpoint rules
88"""
99import datetime
10+ import json
1011import os
1112import re
1213
1819 "transcribe-streaming" : "transcribestreaming" ,
1920 "streams.dynamodb" : "dynamodbstreams" }
2021
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+
2145# Regexp to parse C2J model filename to extract service name and date version
2246SERVICE_MODEL_FILENAME_PATTERN = re .compile (
2347 "^"
2953
3054class ServiceModel (object ):
3155 # 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 ):
3357 self .service_id = service_id # For debugging purposes, not used atm
3458 # only filenames, no filesystem path
3559 self .c2j_model = c2j_model
3660 self .endpoint_rule_set = endpoint_rule_set
3761 self .endpoint_tests = endpoint_tests
62+ self .use_smithy = use_smithy
3863
3964
4065class ModelUtils (object ):
@@ -113,7 +138,8 @@ def _collect_available_models(models_dir: str, endpoint_rules_dir: str) -> dict:
113138
114139 # fetch endpoint-rules filename which is based on ServiceId in c2j models:
115140 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 ,
117143 model_file_date [0 ])
118144
119145 if key == "s3" :
@@ -125,7 +151,8 @@ def _collect_available_models(models_dir: str, endpoint_rules_dir: str) -> dict:
125151 service_name_to_model_filename [key ] = ServiceModel (service_id = key ,
126152 c2j_model = model_file_date [0 ],
127153 endpoint_rule_set = None ,
128- endpoint_tests = None )
154+ endpoint_tests = None ,
155+ use_smithy = False )
129156 if missing :
130157 # TODO: re-enable with endpoints introduction
131158 # print(f"Missing endpoints for services: {missing}")
@@ -137,7 +164,25 @@ def _collect_available_models(models_dir: str, endpoint_rules_dir: str) -> dict:
137164 return service_name_to_model_filename
138165
139166 @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 :
141186 """Return a ServiceModel containing paths to the Service models: C2J model and endpoints (rules and tests).
142187
143188 :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
153198 match = SERVICE_MODEL_FILENAME_PATTERN .match (c2j_model_filename )
154199 service_id = match .group ("service" )
155200
201+ use_smithy = ModelUtils .is_smithy_enabled (service_id , models_dir , c2j_model_filename )
202+
156203 if os .path .exists (endpoint_rules_filepath ) and os .path .exists (endpoint_tests_filepath ):
157204 return ServiceModel (service_id = service_id ,
158205 c2j_model = c2j_model_filename ,
159206 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