|
| 1 | +package software.amazon.smithy.aws.typescript.codegen; |
| 2 | + |
| 3 | +import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_CONFIG; |
| 4 | +import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_MIDDLEWARE; |
| 5 | + |
| 6 | + |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Set; |
| 11 | +import java.util.function.Consumer; |
| 12 | + |
| 13 | +import software.amazon.smithy.codegen.core.SymbolProvider; |
| 14 | +import software.amazon.smithy.model.Model; |
| 15 | +import software.amazon.smithy.model.knowledge.EventStreamIndex; |
| 16 | +import software.amazon.smithy.model.knowledge.TopDownIndex; |
| 17 | +import software.amazon.smithy.model.shapes.OperationShape; |
| 18 | +import software.amazon.smithy.model.shapes.ServiceShape; |
| 19 | +import software.amazon.smithy.typescript.codegen.LanguageTarget; |
| 20 | +import software.amazon.smithy.typescript.codegen.TypeScriptDependency; |
| 21 | +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; |
| 22 | +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; |
| 23 | +import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin; |
| 24 | +import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; |
| 25 | +import software.amazon.smithy.utils.ListUtils; |
| 26 | +import software.amazon.smithy.utils.MapUtils; |
| 27 | + |
| 28 | +/** |
| 29 | + * Adds runtime client plugins that handle the eventstream flow in request, |
| 30 | + * including eventstream payload signing. |
| 31 | + */ |
| 32 | +public class AddEventStreamHandlingDependency implements TypeScriptIntegration { |
| 33 | + @Override |
| 34 | + public List<RuntimeClientPlugin> getClientPlugins() { |
| 35 | + return ListUtils.of( |
| 36 | + RuntimeClientPlugin.builder() |
| 37 | + .withConventions(AwsDependency.MIDDLEWARE_EVENTSTREAM.dependency, |
| 38 | + "EventStream", HAS_CONFIG) |
| 39 | + .servicePredicate(AddEventStreamHandlingDependency::hasEventStreamInput) |
| 40 | + .build(), |
| 41 | + RuntimeClientPlugin.builder() |
| 42 | + .withConventions(AwsDependency.MIDDLEWARE_EVENTSTREAM.dependency, |
| 43 | + "EventStream", HAS_MIDDLEWARE) |
| 44 | + .operationPredicate(AddEventStreamHandlingDependency::hasEventStreamInput) |
| 45 | + .build() |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void addConfigInterfaceFields( |
| 51 | + TypeScriptSettings settings, |
| 52 | + Model model, |
| 53 | + SymbolProvider symbolProvider, |
| 54 | + TypeScriptWriter writer |
| 55 | + ) { |
| 56 | + if (hasEventStreamInput(model, settings.getService(model))) { |
| 57 | + writer.addImport("EventStreamPayloadHandlerProvider", "__EventStreamPayloadHandlerProvider", |
| 58 | + TypeScriptDependency.AWS_SDK_TYPES.packageName); |
| 59 | + writer.writeDocs("The function that provides necessary utilities for handling request event stream."); |
| 60 | + writer.write("eventStreamPayloadHandlerProvider?: __EventStreamPayloadHandlerProvider;\n"); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters( |
| 66 | + TypeScriptSettings settings, |
| 67 | + Model model, |
| 68 | + SymbolProvider symbolProvider, |
| 69 | + LanguageTarget target |
| 70 | + ) { |
| 71 | + ServiceShape service = settings.getService(model); |
| 72 | + if (!hasEventStreamInput(model, service)) { |
| 73 | + return Collections.emptyMap(); |
| 74 | + } |
| 75 | + |
| 76 | + switch (target) { |
| 77 | + case NODE: |
| 78 | + return MapUtils.of("eventStreamPayloadHandlerProvider", writer -> { |
| 79 | + writer.addDependency(AwsDependency.AWS_SDK_EVENTSTREAM_HANDLER_NODE); |
| 80 | + writer.addImport("eventStreamPayloadHandlerProvider", "eventStreamPayloadHandlerProvider", |
| 81 | + AwsDependency.AWS_SDK_EVENTSTREAM_HANDLER_NODE.packageName); |
| 82 | + writer.write("eventStreamPayloadHandlerProvider,"); |
| 83 | + }); |
| 84 | + case BROWSER: |
| 85 | + /** |
| 86 | + * Browser doesn't support streaming requests as of March 2020. |
| 87 | + * Each service client needs to support eventstream request in browser individually. |
| 88 | + * Services like TranscribeStreaming support it via WebSocket. |
| 89 | + */ |
| 90 | + return MapUtils.of("eventStreamPayloadHandlerProvider", writer -> { |
| 91 | + writer.addDependency(TypeScriptDependency.INVALID_DEPENDENCY); |
| 92 | + writer.addImport("invalidFunction", "invalidFunction", |
| 93 | + TypeScriptDependency.INVALID_DEPENDENCY.packageName); |
| 94 | + writer.openBlock("eventStreamPayloadHandlerProvider: () => ({", "}),", () -> { |
| 95 | + writer.write("handle: invalidFunction(\"event stream request is not supported in browser.\"),"); |
| 96 | + }); |
| 97 | + }); |
| 98 | + case REACT_NATIVE: |
| 99 | + /** |
| 100 | + * ReactNative doesn't support streaming requests as of March 2020. |
| 101 | + * Here we don't supply invalidFunction. Each service client needs to support eventstream request |
| 102 | + * in RN has to implement a customization providing its own eventStreamSignerProvider |
| 103 | + */ |
| 104 | + return MapUtils.of("eventStreamPayloadHandlerProvider", writer -> { |
| 105 | + writer.addDependency(TypeScriptDependency.INVALID_DEPENDENCY); |
| 106 | + writer.addImport("invalidFunction", "invalidFunction", |
| 107 | + TypeScriptDependency.INVALID_DEPENDENCY.packageName); |
| 108 | + writer.openBlock("eventStreamPayloadHandlerProvider: () => ({", "}),", () -> { |
| 109 | + writer.write("handle: invalidFunction(\"event stream request is not supported in ReactNative.\"),"); |
| 110 | + }); |
| 111 | + }); |
| 112 | + default: |
| 113 | + return Collections.emptyMap(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private static boolean hasEventStreamInput(Model model, ServiceShape service) { |
| 118 | + TopDownIndex topDownIndex = model.getKnowledge(TopDownIndex.class); |
| 119 | + Set<OperationShape> operations = topDownIndex.getContainedOperations(service); |
| 120 | + EventStreamIndex eventStreamIndex = model.getKnowledge(EventStreamIndex.class); |
| 121 | + for (OperationShape operation : operations) { |
| 122 | + if (eventStreamIndex.getInputInfo(operation).isPresent()) { |
| 123 | + return true; |
| 124 | + } |
| 125 | + } |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + private static boolean hasEventStreamInput(Model model, ServiceShape service, OperationShape operation) { |
| 130 | + EventStreamIndex eventStreamIndex = model.getKnowledge(EventStreamIndex.class); |
| 131 | + return eventStreamIndex.getInputInfo(operation).isPresent(); |
| 132 | + } |
| 133 | +} |
0 commit comments