|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package software.amazon.smithy.python.aws.codegen; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | +import software.amazon.smithy.aws.traits.ServiceTrait; |
| 9 | +import software.amazon.smithy.codegen.core.Symbol; |
| 10 | +import software.amazon.smithy.python.codegen.CodegenUtils; |
| 11 | +import software.amazon.smithy.python.codegen.ConfigProperty; |
| 12 | +import software.amazon.smithy.python.codegen.GenerationContext; |
| 13 | +import software.amazon.smithy.python.codegen.integrations.PythonIntegration; |
| 14 | +import software.amazon.smithy.python.codegen.integrations.RuntimeClientPlugin; |
| 15 | +import software.amazon.smithy.python.codegen.sections.EndpointParametersSection; |
| 16 | +import software.amazon.smithy.python.codegen.sections.EndpointResolverSection; |
| 17 | +import software.amazon.smithy.python.codegen.sections.InitDefaultEndpointResolverSection; |
| 18 | +import software.amazon.smithy.python.codegen.writer.PythonWriter; |
| 19 | +import software.amazon.smithy.utils.CodeInterceptor; |
| 20 | +import software.amazon.smithy.utils.CodeSection; |
| 21 | + |
| 22 | +public class AwsStandardRegionalEndpointsIntegration implements PythonIntegration { |
| 23 | + @Override |
| 24 | + public List<RuntimeClientPlugin> getClientPlugins(GenerationContext context) { |
| 25 | + if (context.applicationProtocol().isHttpProtocol()) { |
| 26 | + var region = ConfigProperty.builder() |
| 27 | + .name("region") |
| 28 | + .type(Symbol.builder().name("str").build()) |
| 29 | + .documentation(" The AWS region to connect to. The configured region is used to " |
| 30 | + + "determine the service endpoint.") |
| 31 | + .build(); |
| 32 | + return List.of( |
| 33 | + RuntimeClientPlugin.builder() |
| 34 | + .addConfigProperty(region) |
| 35 | + .build()); |
| 36 | + } else { |
| 37 | + return List.of(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public List<? extends CodeInterceptor<? extends CodeSection, PythonWriter>> interceptors( |
| 43 | + GenerationContext context |
| 44 | + ) { |
| 45 | + return List.of( |
| 46 | + new RegionalEndpointParametersInterceptor(context), |
| 47 | + new RegionalEndpointResolverInterceptor(context), |
| 48 | + new RegionalInitEndpointResolverInterceptor(context)); |
| 49 | + } |
| 50 | + |
| 51 | + private static final class RegionalEndpointParametersInterceptor |
| 52 | + implements CodeInterceptor<EndpointParametersSection, PythonWriter> { |
| 53 | + |
| 54 | + private final GenerationContext context; |
| 55 | + |
| 56 | + public RegionalEndpointParametersInterceptor(GenerationContext context) { |
| 57 | + this.context = context; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public Class<EndpointParametersSection> sectionType() { |
| 62 | + return EndpointParametersSection.class; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void write(PythonWriter writer, String previousText, EndpointParametersSection section) { |
| 67 | + var params = CodegenUtils.getEndpointParametersSymbol(context.settings()); |
| 68 | + |
| 69 | + writer.write("from smithy_aws_core.endpoints.standard_regional import RegionalEndpointParameters"); |
| 70 | + writer.write("$L = RegionalEndpointParameters", params.getName()); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static final class RegionalEndpointResolverInterceptor |
| 75 | + implements CodeInterceptor<EndpointResolverSection, PythonWriter> { |
| 76 | + |
| 77 | + private final GenerationContext context; |
| 78 | + |
| 79 | + public RegionalEndpointResolverInterceptor(GenerationContext context) { |
| 80 | + this.context = context; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public Class<EndpointResolverSection> sectionType() { |
| 85 | + return EndpointResolverSection.class; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void write(PythonWriter writer, String previousText, EndpointResolverSection section) { |
| 90 | + var resolver = CodegenUtils.getEndpointResolverSymbol(context.settings()); |
| 91 | + |
| 92 | + writer.write("from smithy_aws_core.endpoints.standard_regional import StandardRegionalEndpointsResolver"); |
| 93 | + writer.write("$L = StandardRegionalEndpointsResolver", resolver.getName()); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private static final class RegionalInitEndpointResolverInterceptor |
| 98 | + implements CodeInterceptor<InitDefaultEndpointResolverSection, PythonWriter> { |
| 99 | + |
| 100 | + private final GenerationContext context; |
| 101 | + |
| 102 | + public RegionalInitEndpointResolverInterceptor(GenerationContext context) { |
| 103 | + this.context = context; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public Class<InitDefaultEndpointResolverSection> sectionType() { |
| 108 | + return InitDefaultEndpointResolverSection.class; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void write(PythonWriter writer, String previousText, InitDefaultEndpointResolverSection section) { |
| 113 | + var resolver = CodegenUtils.getEndpointResolverSymbol(context.settings()); |
| 114 | + |
| 115 | + String endpointPrefix = context.settings() |
| 116 | + .service(context.model()) |
| 117 | + .getTrait(ServiceTrait.class) |
| 118 | + .map(ServiceTrait::getEndpointPrefix) |
| 119 | + .orElse(context.settings().service().getName()); |
| 120 | + |
| 121 | + writer.write("self.endpoint_resolver = endpoint_resolver or $T(endpoint_prefix=$S)", |
| 122 | + resolver, |
| 123 | + endpointPrefix); |
| 124 | + |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments