44 */
55package software .amazon .smithy .python .codegen ;
66
7+ import static software .amazon .smithy .python .codegen .SymbolProperties .OPERATION_METHOD ;
8+
79import java .util .Collection ;
810import java .util .LinkedHashSet ;
911import java .util .Set ;
12+ import software .amazon .smithy .codegen .core .SymbolProvider ;
1013import software .amazon .smithy .codegen .core .SymbolReference ;
14+ import software .amazon .smithy .model .Model ;
1115import software .amazon .smithy .model .knowledge .EventStreamIndex ;
1216import software .amazon .smithy .model .knowledge .EventStreamInfo ;
1317import software .amazon .smithy .model .knowledge .ServiceIndex ;
3438final class ClientGenerator implements Runnable {
3539
3640 private final GenerationContext context ;
41+ private final Model model ;
3742 private final ServiceShape service ;
43+ private final SymbolProvider symbolProvider ;
3844
3945 ClientGenerator (GenerationContext context , ServiceShape service ) {
4046 this .context = context ;
47+ this .symbolProvider = context .symbolProvider ();
48+ this .model = context .model ();
4149 this .service = service ;
4250 }
4351
@@ -47,7 +55,7 @@ public void run() {
4755 }
4856
4957 private void generateService (PythonWriter writer ) {
50- var serviceSymbol = context . symbolProvider () .toSymbol (service );
58+ var serviceSymbol = symbolProvider .toSymbol (service );
5159 var configSymbol = CodegenUtils .getConfigSymbol (context .settings ());
5260 var pluginSymbol = CodegenUtils .getPluginSymbol (context .settings ());
5361 writer .addLogger ();
@@ -77,7 +85,7 @@ private void generateService(PythonWriter writer) {
7785
7886 for (PythonIntegration integration : context .integrations ()) {
7987 for (RuntimeClientPlugin runtimeClientPlugin : integration .getClientPlugins (context )) {
80- if (runtimeClientPlugin .matchesService (context . model () , service )) {
88+ if (runtimeClientPlugin .matchesService (model , service )) {
8189 runtimeClientPlugin .getPythonPlugin ().ifPresent (defaultPlugins ::add );
8290 }
8391 }
@@ -97,8 +105,8 @@ def __init__(self, config: $1T | None = None, plugins: list[$2T] | None = None):
97105 plugin(self._config)
98106 """ , configSymbol , pluginSymbol , writer .consumer (w -> writeDefaultPlugins (w , defaultPlugins )));
99107
100- var topDownIndex = TopDownIndex .of (context . model () );
101- var eventStreamIndex = EventStreamIndex .of (context . model () );
108+ var topDownIndex = TopDownIndex .of (model );
109+ var eventStreamIndex = EventStreamIndex .of (model );
102110 for (OperationShape operation : topDownIndex .getContainedOperations (service )) {
103111 if (eventStreamIndex .getInputInfo (operation ).isPresent ()
104112 || eventStreamIndex .getOutputInfo (operation ).isPresent ()) {
@@ -253,7 +261,7 @@ async def _duplex_stream(
253261 writer .consumer (w -> context .protocolGenerator ().wrapOutputStream (context , w )),
254262 writer .consumer (w -> context .protocolGenerator ().wrapDuplexStream (context , w )));
255263 }
256-
264+ writer . addStdlibImport ( "typing" , "Any" );
257265 writer .write (
258266 """
259267 async def _execute_operation(
@@ -448,7 +456,7 @@ async def _handle_attempt(
448456 errorSymbol ,
449457 configSymbol );
450458
451- boolean supportsAuth = !ServiceIndex .of (context . model () ).getAuthSchemes (service ).isEmpty ();
459+ boolean supportsAuth = !ServiceIndex .of (model ).getAuthSchemes (service ).isEmpty ();
452460 writer .pushState (new ResolveIdentitySection ());
453461 if (context .applicationProtocol ().isHttpProtocol () && supportsAuth ) {
454462 writer .pushState (new InitializeHttpAuthParametersSection ());
@@ -731,8 +739,8 @@ async def _finalize_execution(
731739 }
732740
733741 private boolean hasEventStream () {
734- var streamIndex = EventStreamIndex .of (context . model () );
735- var topDownIndex = TopDownIndex .of (context . model () );
742+ var streamIndex = EventStreamIndex .of (model );
743+ var topDownIndex = TopDownIndex .of (model );
736744 for (OperationShape operation : topDownIndex .getContainedOperations (context .settings ().service ())) {
737745 if (streamIndex .getInputInfo (operation ).isPresent () || streamIndex .getOutputInfo (operation ).isPresent ()) {
738746 return true ;
@@ -745,7 +753,7 @@ private void initializeHttpAuthParameters(PythonWriter writer) {
745753 var derived = new LinkedHashSet <DerivedProperty >();
746754 for (PythonIntegration integration : context .integrations ()) {
747755 for (RuntimeClientPlugin plugin : integration .getClientPlugins (context )) {
748- if (plugin .matchesService (context . model () , service )
756+ if (plugin .matchesService (model , service )
749757 && plugin .getAuthScheme ().isPresent ()
750758 && plugin .getAuthScheme ().get ().getApplicationProtocol ().isHttpProtocol ()) {
751759 derived .addAll (plugin .getAuthScheme ().get ().getAuthProperties ());
@@ -773,18 +781,18 @@ private void writeDefaultPlugins(PythonWriter writer, Collection<SymbolReference
773781 * Generates the function for a single operation.
774782 */
775783 private void generateOperation (PythonWriter writer , OperationShape operation ) {
776- var operationSymbol = context . symbolProvider () .toSymbol (operation );
784+ var operationMethodSymbol = symbolProvider .toSymbol (operation ). expectProperty ( OPERATION_METHOD );
777785 var pluginSymbol = CodegenUtils .getPluginSymbol (context .settings ());
778786
779- var input = context . model () .expectShape (operation .getInputShape ());
780- var inputSymbol = context . symbolProvider () .toSymbol (input );
787+ var input = model .expectShape (operation .getInputShape ());
788+ var inputSymbol = symbolProvider .toSymbol (input );
781789
782- var output = context . model () .expectShape (operation .getOutputShape ());
783- var outputSymbol = context . symbolProvider () .toSymbol (output );
790+ var output = model .expectShape (operation .getOutputShape ());
791+ var outputSymbol = symbolProvider .toSymbol (output );
784792
785793 writer .openBlock ("async def $L(self, input: $T, plugins: list[$T] | None = None) -> $T:" ,
786794 "" ,
787- operationSymbol .getName (),
795+ operationMethodSymbol .getName (),
788796 inputSymbol ,
789797 pluginSymbol ,
790798 outputSymbol ,
@@ -834,7 +842,7 @@ private void writeSharedOperationInit(PythonWriter writer, OperationShape operat
834842 var defaultPlugins = new LinkedHashSet <SymbolReference >();
835843 for (PythonIntegration integration : context .integrations ()) {
836844 for (RuntimeClientPlugin runtimeClientPlugin : integration .getClientPlugins (context )) {
837- if (runtimeClientPlugin .matchesOperation (context . model () , service , operation )) {
845+ if (runtimeClientPlugin .matchesOperation (model , service , operation )) {
838846 runtimeClientPlugin .getPythonPlugin ().ifPresent (defaultPlugins ::add );
839847 }
840848 }
@@ -852,29 +860,29 @@ private void writeSharedOperationInit(PythonWriter writer, OperationShape operat
852860 private void generateEventStreamOperation (PythonWriter writer , OperationShape operation ) {
853861 writer .pushState ();
854862 writer .addDependency (SmithyPythonDependency .SMITHY_EVENT_STREAM );
855- var operationSymbol = context . symbolProvider () .toSymbol (operation );
856- writer .putContext ("operationName" , operationSymbol .getName ());
863+ var operationMethodSymbol = symbolProvider .toSymbol (operation ). expectProperty ( OPERATION_METHOD );
864+ writer .putContext ("operationName" , operationMethodSymbol .getName ());
857865 var pluginSymbol = CodegenUtils .getPluginSymbol (context .settings ());
858866 writer .putContext ("plugin" , pluginSymbol );
859867
860- var input = context . model () .expectShape (operation .getInputShape ());
861- var inputSymbol = context . symbolProvider () .toSymbol (input );
868+ var input = model .expectShape (operation .getInputShape ());
869+ var inputSymbol = symbolProvider .toSymbol (input );
862870 writer .putContext ("input" , inputSymbol );
863871
864- var eventStreamIndex = EventStreamIndex .of (context . model () );
872+ var eventStreamIndex = EventStreamIndex .of (model );
865873 var inputStreamSymbol = eventStreamIndex .getInputInfo (operation )
866874 .map (EventStreamInfo ::getEventStreamTarget )
867- .map (target -> context . symbolProvider (). toSymbol ( target ) )
875+ .map (symbolProvider :: toSymbol )
868876 .orElse (null );
869877 writer .putContext ("inputStream" , inputStreamSymbol );
870878
871- var output = context . model () .expectShape (operation .getOutputShape ());
872- var outputSymbol = context . symbolProvider () .toSymbol (output );
879+ var output = model .expectShape (operation .getOutputShape ());
880+ var outputSymbol = symbolProvider .toSymbol (output );
873881 writer .putContext ("output" , outputSymbol );
874882
875883 var outputStreamSymbol = eventStreamIndex .getOutputInfo (operation )
876884 .map (EventStreamInfo ::getEventStreamTarget )
877- .map (target -> context . symbolProvider (). toSymbol ( target ) )
885+ .map (symbolProvider :: toSymbol )
878886 .orElse (null );
879887 writer .putContext ("outputStream" , outputStreamSymbol );
880888
0 commit comments