private CompletableFuture<InvokeResponse> callLambda(InvocationType type, IN input) { log.debug("calling '{}' as {} with payload: {}", functionName, type, input); ByteBuffer payload = serialize(input); InvokeRequest request = InvokeRequest.builder() .functionName(functionName) .invocationType(type) .payload(payload) .build(); return lambda.invoke(request); }
@Override @SneakyThrows public CompletableFuture<InvokeResponse> invoke(final InvokeRequest invokeRequest) { ByteBufferBackedInputStream inputStream = new ByteBufferBackedInputStream(invokeRequest.payload()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); handler.handleRequest(inputStream, outputStream, null); return CompletableFuture.completedFuture( InvokeResponse.builder().payload(ByteBuffer.wrap(outputStream.toByteArray())).build()); } }
@Test public void passesCorrectFunctionName() { function.call(new TestInput("test")); verify(client).invoke(requestArgument.capture()); assertThat(requestArgument.getValue().functionName(), equalTo("TestFunction")); }
@Override public int hashCode() { int hashCode = 1; hashCode = 31 * hashCode + Objects.hashCode(functionName()); hashCode = 31 * hashCode + Objects.hashCode(invocationTypeAsString()); hashCode = 31 * hashCode + Objects.hashCode(logTypeAsString()); hashCode = 31 * hashCode + Objects.hashCode(clientContext()); hashCode = 31 * hashCode + Objects.hashCode(payload()); hashCode = 31 * hashCode + Objects.hashCode(qualifier()); return hashCode; }
@Test public void callUsesRequestReply() { function.callAsync(new TestInput("test")).join(); verify(client).invoke(requestArgument.capture()); assertThat( requestArgument.getValue().invocationType(), equalTo(InvocationType.RequestResponse.toString())); }
@Override public InvokeRequest build() { return new InvokeRequest(this); }
/** * Returns a string representation of this object. This is useful for testing and debugging. Sensitive data will be * redacted from this string using a placeholder value. */ @Override public String toString() { return ToString.builder("InvokeRequest").add("FunctionName", functionName()) .add("InvocationType", invocationTypeAsString()).add("LogType", logTypeAsString()) .add("ClientContext", clientContext()) .add("Payload", payload() == null ? null : "*** Sensitive Data Redacted ***").add("Qualifier", qualifier()) .build(); }
@Test public void triggerUsesEventInvocation() { function.triggerAsync(new TestInput("test")).join(); verify(client).invoke(requestArgument.capture()); assertThat( requestArgument.getValue().invocationType(), equalTo(InvocationType.Event.toString())); }
@Override public InvokeRequest build() { return new InvokeRequest(this); }
@Override public int hashCode() { int hashCode = 1; hashCode = 31 * hashCode + Objects.hashCode(functionName()); hashCode = 31 * hashCode + Objects.hashCode(invocationTypeAsString()); hashCode = 31 * hashCode + Objects.hashCode(logTypeAsString()); hashCode = 31 * hashCode + Objects.hashCode(clientContext()); hashCode = 31 * hashCode + Objects.hashCode(payload()); hashCode = 31 * hashCode + Objects.hashCode(qualifier()); return hashCode; }
/** * Invoke a JSON-RPC method asynchronously without compile-time type-checking. * * <p>This overload is private, because it should only be used by the {@link * JsonRpcLambdaProxyAdapter} (which requires a non-typesafe invoke method). */ private CompletableFuture<Object> invokeAsync(String methodName, Object argument, Type returnType) throws IOException { ByteBuffer requestPayload = writeRequest(methodName, argument); InvokeRequest invokeRequest = InvokeRequest.builder().functionName(functionName).payload(requestPayload).build(); CompletableFuture<InvokeResponse> pendingRequest = lambda.invoke(invokeRequest); return pendingRequest.thenApply(r -> readResponse(returnType, r)); }
@Test public void serializesInputsUsingMapper() throws Exception { TestOutput output = function.call(new TestInput("test")); verify(client).invoke(requestArgument.capture()); ByteBuffer actualPayload = requestArgument.getValue().payload(); assertThat( mapper .readValue(new ByteBufferBackedInputStream(actualPayload), TestInput.class) .getInput(), equalTo("test")); }
/** * Returns a string representation of this object. This is useful for testing and debugging. Sensitive data will be * redacted from this string using a placeholder value. */ @Override public String toString() { return ToString.builder("InvokeRequest").add("FunctionName", functionName()) .add("InvocationType", invocationTypeAsString()).add("LogType", logTypeAsString()) .add("ClientContext", clientContext()) .add("Payload", payload() == null ? null : "*** Sensitive Data Redacted ***").add("Qualifier", qualifier()) .build(); }
return invoke(InvokeRequest.builder().applyMutation(invokeRequest).build());
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof InvokeRequest)) { return false; } InvokeRequest other = (InvokeRequest) obj; return Objects.equals(functionName(), other.functionName()) && Objects.equals(invocationTypeAsString(), other.invocationTypeAsString()) && Objects.equals(logTypeAsString(), other.logTypeAsString()) && Objects.equals(clientContext(), other.clientContext()) && Objects.equals(payload(), other.payload()) && Objects.equals(qualifier(), other.qualifier()); }
KmsInvalidStateException, KmsAccessDeniedException, KmsNotFoundException, InvalidRuntimeException, AwsServiceException, SdkClientException, LambdaException { return invoke(InvokeRequest.builder().applyMutation(invokeRequest).build());
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof InvokeRequest)) { return false; } InvokeRequest other = (InvokeRequest) obj; return Objects.equals(functionName(), other.functionName()) && Objects.equals(invocationTypeAsString(), other.invocationTypeAsString()) && Objects.equals(logTypeAsString(), other.logTypeAsString()) && Objects.equals(clientContext(), other.clientContext()) && Objects.equals(payload(), other.payload()) && Objects.equals(qualifier(), other.qualifier()); }
return invoke(InvokeRequest.builder().applyMutation(invokeRequest).build());
public <T> Optional<T> getValueForField(String fieldName, Class<T> clazz) { switch (fieldName) { case "FunctionName": return Optional.ofNullable(clazz.cast(functionName())); case "InvocationType": return Optional.ofNullable(clazz.cast(invocationTypeAsString())); case "LogType": return Optional.ofNullable(clazz.cast(logTypeAsString())); case "ClientContext": return Optional.ofNullable(clazz.cast(clientContext())); case "Payload": return Optional.ofNullable(clazz.cast(payload())); case "Qualifier": return Optional.ofNullable(clazz.cast(qualifier())); default: return Optional.empty(); } }
KmsInvalidStateException, KmsAccessDeniedException, KmsNotFoundException, InvalidRuntimeException, AwsServiceException, SdkClientException, LambdaException { return invoke(InvokeRequest.builder().applyMutation(invokeRequest).build());