use of com.google.cloud.tasks.v2.Queue in project nomulus by google.
the class CloudTasksUtils method createTask.
/**
* Create a {@link Task} to be enqueued.
*
* @param path the relative URI (staring with a slash and ending without one).
* @param method the HTTP method to be used for the request, only GET and POST are supported.
* @param service the App Engine service to route the request to. Note that with App Engine Task
* Queue API if no service is specified, the service which enqueues the task will be used to
* process the task. Cloud Tasks API does not support this feature so the service will always
* needs to be explicitly specified.
* @param params a multi-map of URL query parameters. Duplicate keys are saved as is, and it is up
* to the server to process the duplicate keys.
* @return the enqueued task.
* @see <a
* href=ttps://cloud.google.com/appengine/docs/standard/java/taskqueue/push/creating-tasks#target>Specifyinig
* the worker service</a>
*/
private Task createTask(String path, HttpMethod method, String service, Multimap<String, String> params) {
checkArgument(path != null && !path.isEmpty() && path.charAt(0) == '/', "The path must start with a '/'.");
checkArgument(method.equals(HttpMethod.GET) || method.equals(HttpMethod.POST), "HTTP method %s is used. Only GET and POST are allowed.", method);
AppEngineHttpRequest.Builder requestBuilder = AppEngineHttpRequest.newBuilder().setHttpMethod(method).setAppEngineRouting(AppEngineRouting.newBuilder().setService(service).build());
if (!CollectionUtils.isNullOrEmpty(params)) {
Escaper escaper = UrlEscapers.urlPathSegmentEscaper();
String encodedParams = Joiner.on("&").join(params.entries().stream().map(entry -> String.format("%s=%s", escaper.escape(entry.getKey()), escaper.escape(entry.getValue()))).collect(toImmutableList()));
if (method == HttpMethod.GET) {
path = String.format("%s?%s", path, encodedParams);
} else {
requestBuilder.putHeaders(HttpHeaders.CONTENT_TYPE, MediaType.FORM_DATA.toString()).setBody(ByteString.copyFrom(encodedParams, StandardCharsets.UTF_8));
}
}
requestBuilder.setRelativeUri(path);
return Task.newBuilder().setAppEngineHttpRequest(requestBuilder.build()).build();
}
Aggregations