|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.modelbundle.api; |
| 7 | + |
| 8 | +import static software.amazon.smithy.modelbundle.api.StaticAuthSchemeResolver.staticScheme; |
| 9 | + |
| 10 | +import software.amazon.smithy.java.client.core.RequestOverrideConfig; |
| 11 | +import software.amazon.smithy.java.client.core.auth.identity.IdentityResolver; |
| 12 | +import software.amazon.smithy.java.client.core.auth.scheme.AuthScheme; |
| 13 | +import software.amazon.smithy.java.client.core.endpoint.EndpointResolver; |
| 14 | +import software.amazon.smithy.java.core.serde.document.Document; |
| 15 | +import software.amazon.smithy.model.shapes.ShapeId; |
| 16 | + |
| 17 | +/** |
| 18 | + * A ConfigProvider is used to parse a bundle of service information (model, auth configuration, endpoints, etc.) and |
| 19 | + * configure outgoing client calls as necessary. |
| 20 | + * |
| 21 | + * <p>Implementations of this interface can define a wrapper type that adds additional parameters to vended MCP tools. |
| 22 | + * For example, an AWS auth provider can make a wrapper that adds the region and AWS credential profile name as |
| 23 | + * arguments to tools generated for AWS APIs. A wrapper type does not need to be defined if no per-request parameters |
| 24 | + * need to be injected. |
| 25 | + * |
| 26 | + * <p>The ConfigProvider is responsible for configuring outbound client calls with endpoint, identity, and auth resolver |
| 27 | + * mechanisms. The default implementation of {@link #adaptConfig(T)} orchestrates the calls to all other ConfigProvider |
| 28 | + * APIs and should not be overridden. If an override is needed, the {@code super} method should be called and the |
| 29 | + * returned RequestOverrideConfig.Builder should be modified. |
| 30 | + * |
| 31 | + * @param <T> the type of configuration parsed by this ConfigProvider |
| 32 | + */ |
| 33 | +public interface ConfigProvider<T> { |
| 34 | + /** |
| 35 | + * Returns the ShapeId of the wrapper type that this config provider uses. |
| 36 | + * |
| 37 | + * @return this config provider's wrapper type, or {@code null} if it doesn't use a wrapper |
| 38 | + */ |
| 39 | + default ShapeId wrapperType() { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Parses the given document into this ConfigProvider's {@linkplain #wrapperType() wrapper type}. |
| 45 | + * If this ConfigProvider has no wrapper type, this method returns null. |
| 46 | + * |
| 47 | + * @param input the document to parse |
| 48 | + * @return the parsed wrapper type |
| 49 | + */ |
| 50 | + T parse(Document input); |
| 51 | + |
| 52 | + /** |
| 53 | + * Returns an identity resolver for the service being called, with optional values provided by the |
| 54 | + * parsed wrapper (if present). |
| 55 | + * |
| 56 | + * @param args the {@linkplain #parse(Document) parsed data wrapper} containing provider-specific arguments |
| 57 | + * @return an {@link IdentityResolver} that provides identity information |
| 58 | + */ |
| 59 | + IdentityResolver<?> identityResolver(T args); |
| 60 | + |
| 61 | + /** |
| 62 | + * Returns an auth scheme for the service being called, with optional values provided by the |
| 63 | + * parsed wrapper (if present). |
| 64 | + * |
| 65 | + * @param args the {@linkplain #parse(Document) parsed data wrapper} containing provider-specific arguments |
| 66 | + * @return an {@link AuthScheme} that implements the service's required auth mechanism |
| 67 | + */ |
| 68 | + AuthScheme<?, ?> authScheme(T args); |
| 69 | + |
| 70 | + /** |
| 71 | + * Returns an endpoint resolver for the service being called, with optional values provided by the |
| 72 | + * parsed wrapper (if present). |
| 73 | + * |
| 74 | + * @param args the {@linkplain #parse(Document) parsed data wrapper} containing provider-specific arguments |
| 75 | + * @return an {@link EndpointResolver} that provides the endpoint to call |
| 76 | + */ |
| 77 | + EndpointResolver endpointResolver(T args); |
| 78 | + |
| 79 | + /** |
| 80 | + * Adapts an outgoing request to use the {@linkplain #authScheme(Object) auth}, {@linkplain #identityResolver(Object) identity}, |
| 81 | + * and {@linkplain #endpointResolver(Object) endpoint} specified by this ConfigProvider. |
| 82 | + * |
| 83 | + * @param args the {@linkplain #parse(Document) parsed data wrapper} containing provider-specific arguments |
| 84 | + * @return a fully-configured {@link RequestOverrideConfig.Builder} that can be used to make the request |
| 85 | + */ |
| 86 | + default RequestOverrideConfig.Builder adaptConfig(T args) { |
| 87 | + return RequestOverrideConfig.builder() |
| 88 | + .authSchemeResolver(StaticAuthSchemeResolver.INSTANCE) |
| 89 | + .putSupportedAuthSchemes(staticScheme(authScheme(args))) |
| 90 | + .addIdentityResolver(identityResolver(args)) |
| 91 | + .endpointResolver(endpointResolver(args)); |
| 92 | + } |
| 93 | +} |
0 commit comments