@Override public Headers getHeaders() { return Headers.emptyHeaders(); }
/** * Create an output event from a byte array * * @param bytes the byte array to write to the output * @param status the status code to report * @param contentType the content type to present on HTTP responses * @return a new output event */ static OutputEvent fromBytes(byte[] bytes, Status status, String contentType) { return fromBytes(bytes, status, contentType, Headers.emptyHeaders()); }
/** * Invoke a function by ID with no headers * <p> * * @param functionId Function ID of function to invoke - this should be the function ID returned by `fn inspect function appName fnName` * @param method HTTP method to invoke function * @return a future which completes normally if the function succeeded and fails if it fails * @see #invokeFunction(String, HttpMethod, Headers, byte[]) */ default FlowFuture<HttpResponse> invokeFunction(String functionId, HttpMethod method) { return invokeFunction(functionId, method, Headers.emptyHeaders(), new byte[]{}); }
/** * Invoke a function by ID using input and output coercion * <p> * This currently only maps to JSON via the default JSON mapper in the FDK * * @param functionId Function ID of function to invoke - this should be the function ID returned by `fn inspect function appName fnName` * @param input The input object to send to the function input * @param responseType The expected response type of the target function * @param <T> The Response type * @param <U> The Input type of the function * @return a flow future that completes with the result of the function, or an error if the function invocation failed * @throws IllegalArgumentException if the input cannot be coerced to the callA */ default <T extends Serializable, U> FlowFuture<T> invokeFunction(String functionId, U input, Class<T> responseType) { return invokeFunction(functionId, HttpMethod.POST, Headers.emptyHeaders(), input, responseType); }
/** * Invoke a function by ID using input and output coercion, default method (POST) and no response type * <p> * Returns a future that completes with the HttpResponse of the function on success * if the function returns a successful http response, and completes with an {@link FunctionInvocationException} if the function invocation fails with a non-succesful http status * <p> * This currently only maps to JSON via the default JSON mapper in the FDK * * @param functionId Function ID of function to invoke - this should be the function ID returned by `fn inspect function appName fnName` * @param input The input object to send to the function input * @param <U> The Input type of the function * @return a flow future that completes with the result of the function, or an error if the function invocation failed * @throws IllegalArgumentException if the input cannot be coerced to the call */ default <U> FlowFuture<HttpResponse> invokeFunction(String functionId, U input) { return invokeFunction(functionId, HttpMethod.POST, Headers.emptyHeaders(), input); }
@Override public FlowTesting withAction(ExternalFunctionAction f) { functionStubs.put(id, (HttpMethod method, Headers headers, byte[] body) -> { try { return new DefaultHttpResponse(200, Headers.emptyHeaders(), f.apply(body)); } catch (FunctionError functionError) { return new DefaultHttpResponse(500, Headers.emptyHeaders(), functionError.getMessage().getBytes()); } catch (PlatformError platformError) { throw new RuntimeException("Platform Error"); } }); return FlowTesting.this; } };
/** * Create an output event from a byte array * * @param bytes the byte array to write to the output * @param status the status code of this event * @param contentType the content type to present on HTTP responses or null * @param headers any additional headers to supply with HTTP responses * @return a new output event */ static OutputEvent fromBytes(final byte[] bytes, final Status status, final String contentType, final Headers headers) { Objects.requireNonNull(bytes, "bytes"); Objects.requireNonNull(status, "status"); Objects.requireNonNull(headers, "headers"); final Headers newHeaders = contentType== null?Headers.emptyHeaders():headers.setHeader("Content-Type",contentType); return new OutputEvent() { @Override public Status getStatus() { return status; } @Override public Headers getHeaders() { return newHeaders; } @Override public void writeToOutput(OutputStream out) throws IOException { out.write(bytes); } }; }
public FunctionHTTPGatewayContext(InvocationContext invocationContext) { this.invocationContext = Objects.requireNonNull(invocationContext, "invocationContext"); Map<String, List<String>> myHeaders = new HashMap<>(); String requestUri = ""; String method = ""; for (Map.Entry<String, List<String>> e : invocationContext.getRequestHeaders().asMap().entrySet()) { String key = e.getKey(); if (key.startsWith("Fn-Http-H-")) { String httpKey = key.substring("Fn-Http-H-".length()); if (httpKey.length() > 0) { myHeaders.put(httpKey, e.getValue()); } } if (key.equals("Fn-Http-Request-Url")) { requestUri = e.getValue().get(0); } if (key.equals("Fn-Http-Method")) { method = e.getValue().get(0); } } this.queryParameters = QueryParametersParser.getParams(requestUri); this.requestUrl = requestUri; this.method = method; this.httpRequestHeaders = Headers.emptyHeaders().setHeaders(myHeaders); }
Headers headersIn = Headers.emptyHeaders();
public Response handleRequest(byte[] imageBuffer) { String id = java.util.UUID.randomUUID().toString(); Flow runtime = Flows.currentFlow(); runtime.allOf( runtime.invokeFunction(resize128ID, HttpMethod.POST, Headers.emptyHeaders(), imageBuffer) .thenAccept((img) -> objectUpload(img.getBodyAsBytes(), id + "-128.png")), runtime.invokeFunction(resize256ID, HttpMethod.POST, Headers.emptyHeaders(), imageBuffer) .thenAccept((img) -> objectUpload(img.getBodyAsBytes(), id + "-256.png")), runtime.invokeFunction(resize512ID, HttpMethod.POST, Headers.emptyHeaders(), imageBuffer) .thenAccept((img) -> objectUpload(img.getBodyAsBytes(), id + "-512.png")), runtime.supply(() -> objectUpload(imageBuffer, id + ".png")) ); return new Response(id); }