|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// [START retail_rejoin_user_event] |
| 18 | + |
| 19 | +/* |
| 20 | + * Rejoin user events into a catalog from inline source using Retail API |
| 21 | + */ |
| 22 | + |
| 23 | +package events; |
| 24 | + |
| 25 | +import static setup.SetupCleanup.purgeUserEvent; |
| 26 | +import static setup.SetupCleanup.writeUserEvent; |
| 27 | + |
| 28 | +import com.google.api.gax.longrunning.OperationFuture; |
| 29 | +import com.google.cloud.retail.v2.RejoinUserEventsMetadata; |
| 30 | +import com.google.cloud.retail.v2.RejoinUserEventsRequest; |
| 31 | +import com.google.cloud.retail.v2.RejoinUserEventsRequest.UserEventRejoinScope; |
| 32 | +import com.google.cloud.retail.v2.RejoinUserEventsResponse; |
| 33 | +import com.google.cloud.retail.v2.UserEventServiceClient; |
| 34 | +import java.io.IOException; |
| 35 | +import java.util.UUID; |
| 36 | +import java.util.concurrent.ExecutionException; |
| 37 | + |
| 38 | +public class RejoinUserEvent { |
| 39 | + |
| 40 | + public static void main(String[] args) |
| 41 | + throws IOException, ExecutionException, InterruptedException { |
| 42 | + // TODO(developer): Replace these variables before running the sample. |
| 43 | + String projectId = System.getenv("PROJECT_ID"); |
| 44 | + String defaultCatalog = |
| 45 | + String.format("projects/%s/locations/global/catalogs/default_catalog", projectId); |
| 46 | + // visitorId generated randomly. |
| 47 | + String visitorId = UUID.randomUUID().toString(); |
| 48 | + |
| 49 | + callRejoinUserEvents(defaultCatalog, visitorId); |
| 50 | + } |
| 51 | + |
| 52 | + public static void callRejoinUserEvents(String defaultCatalog, String visitorId) |
| 53 | + throws IOException, ExecutionException, InterruptedException { |
| 54 | + writeUserEvent(visitorId); |
| 55 | + |
| 56 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 57 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 58 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 59 | + try (UserEventServiceClient userEventServiceClient = UserEventServiceClient.create()) { |
| 60 | + RejoinUserEventsRequest rejoinUserEventsRequest = |
| 61 | + RejoinUserEventsRequest.newBuilder() |
| 62 | + .setParent(defaultCatalog) |
| 63 | + .setUserEventRejoinScope(UserEventRejoinScope.UNJOINED_EVENTS) |
| 64 | + .build(); |
| 65 | + System.out.printf("Rejoin user events request: %s%n", rejoinUserEventsRequest); |
| 66 | + |
| 67 | + OperationFuture<RejoinUserEventsResponse, RejoinUserEventsMetadata> rejoinOperation = |
| 68 | + userEventServiceClient.rejoinUserEventsAsync(rejoinUserEventsRequest); |
| 69 | + |
| 70 | + System.out.printf("The rejoin operation was started: %s%n", rejoinOperation.getName()); |
| 71 | + } |
| 72 | + |
| 73 | + purgeUserEvent(visitorId); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// [END retail_rejoin_user_event] |
0 commit comments