Search in sources :

Example 26 with Queue

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();
}
Also used : AppEngineHttpRequest(com.google.cloud.tasks.v2.AppEngineHttpRequest) ByteString(com.google.protobuf.ByteString) Escaper(com.google.common.escape.Escaper)

Aggregations

CloudTasksClient (com.google.cloud.tasks.v2.CloudTasksClient)10 Task (com.google.cloud.tasks.v2.Task)8 In (edu.princeton.cs.algs4.In)5 Queue (edu.princeton.cs.algs4.Queue)5 Queue (chapter1.section3.Queue)4 LocationName (com.google.cloud.tasks.v2.LocationName)3 Queue (com.google.cloud.tasks.v2.Queue)3 ByteString (com.google.protobuf.ByteString)3 Test (org.junit.jupiter.api.Test)3 HashSet (chapter3.section5.HashSet)2 AppEngineHttpRequest (com.google.cloud.tasks.v2.AppEngineHttpRequest)2 Transaction (edu.princeton.cs.algs4.Transaction)2 StringJoiner (java.util.StringJoiner)2 UnionFind (chapter1.section5.UnionFind)1 PriorityQueueResize (chapter2.section4.PriorityQueueResize)1 SeparateChainingHashTable (chapter3.section4.SeparateChainingHashTable)1 ApiCallContext (com.google.api.gax.rpc.ApiCallContext)1 UnaryCallable (com.google.api.gax.rpc.UnaryCallable)1 CreateTaskRequest (com.google.cloud.tasks.v2.CreateTaskRequest)1 QueueName (com.google.cloud.tasks.v2.QueueName)1