Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

chore(samples): Retail Tutorials. Events (write, rejoin, purge) #303

Merged
merged 21 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .kokoro/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,30 @@ samples)
echo "no sample pom.xml found - skipping sample tests"
fi
;;
tutorials-samples)
SAMPLES_DIR=samples/interactive-tutorials

if [[ -f ${SAMPLES_DIR}/pom.xml ]]
then
for FILE in ${KOKORO_GFILE_DIR}/secret_manager/*-samples-secrets; do
[[ -f "$FILE" ]] || continue
source "$FILE"
done

pushd ${SAMPLES_DIR}
mvn -B \
-ntp \
-DtrimStackTrace=false \
-Dclirr.skip=true \
-Denforcer.skip=true \
-fae \
verify
RETURN_CODE=$?
popd
else
echo "no interactive-tutorials pom.xml found - skipping interactive-tutorials tests"
fi
;;
clirr)
mvn -B -ntp -Denforcer.skip=true clirr:check
RETURN_CODE=$?
Expand Down
33 changes: 33 additions & 0 deletions .kokoro/nightly/tutorials-samples.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Format: //devtools/kokoro/config/proto/build.proto
kurtisvg marked this conversation as resolved.
Show resolved Hide resolved

# Configure the docker image for kokoro-trampoline.
env_vars: {
key: "TRAMPOLINE_IMAGE"
value: "gcr.io/cloud-devrel-kokoro-resources/java8"
}

env_vars: {
key: "JOB_TYPE"
value: "tutorials-samples"
}

# TODO: remove this after we've migrated all tests and scripts
env_vars: {
key: "PROJECT_NUMBER"
value: "779844219229"
}

env_vars: {
key: "PROJECT_ID"
value: "java-docs-samples-testing"
}

env_vars: {
key: "GOOGLE_APPLICATION_CREDENTIALS"
value: "secret_manager/java-docs-samples-service-account"
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general we usually try to avoid putting secrets here and instead put them in secret-manager.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue is still open.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in secret manager. This just configures the env var for ADC


env_vars: {
key: "SECRET_MANAGER_KEYS"
value: "java-docs-samples-service-account"
}
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ If you are using Maven, add this to your pom.xml file:
If you are using Gradle without BOM, add this to your dependencies

```Groovy
implementation 'com.google.cloud:google-cloud-retail:2.0.8'
implementation 'com.google.cloud:google-cloud-retail:2.0.9'
```

If you are using SBT, add this to your dependencies

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-retail" % "2.0.8"
libraryDependencies += "com.google.cloud" % "google-cloud-retail" % "2.0.9"
```

## Authentication
Expand Down Expand Up @@ -71,6 +71,18 @@ use this Cloud Retail Client Library.



## Samples

Samples are in the [`samples/`](https://github.com/googleapis/java-retail/tree/main/samples) directory.

| Sample | Source Code | Try it |
| --------------------------- | --------------------------------- | ------ |
| Purge User Event | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/events/PurgeUserEvent.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/events/PurgeUserEvent.java) |
| Rejoin User Event | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/events/RejoinUserEvent.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/events/RejoinUserEvent.java) |
| Write User Event | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/events/WriteUserEvent.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/events/WriteUserEvent.java) |
| Setup Cleanup | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/setup/SetupCleanup.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/setup/SetupCleanup.java) |



## Troubleshooting

Expand Down
6 changes: 5 additions & 1 deletion owlbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
s.move(library)

s.remove_staging_dirs()
java.common_templates()
java.common_templates(
excludes=[
".kokoro/build.sh"
kurtisvg marked this conversation as resolved.
Show resolved Hide resolved
]
)
69 changes: 69 additions & 0 deletions samples/interactive-tutorials/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.cloud</groupId>
<artifactId>retail-interactive-tutorials</artifactId>
<packaging>jar</packaging>
<name>Google Cloud Retail Interactive Tutorials</name>
<url>https://github.com/googleapis/java-retail</url>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
</parent>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-retail</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigquery</artifactId>
<version>2.5.1</version>
kurtisvg marked this conversation as resolved.
Show resolved Hide resolved
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
kurtisvg marked this conversation as resolved.
Show resolved Hide resolved
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START retail_purge_user_event]

/*
* Import user events into a catalog from inline source using Retail API

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment does not look correct

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

*/

package events;

import static setup.SetupCleanup.writeUserEvent;

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.retail.v2.PurgeMetadata;
import com.google.cloud.retail.v2.PurgeUserEventsRequest;
import com.google.cloud.retail.v2.PurgeUserEventsResponse;
import com.google.cloud.retail.v2.UserEventServiceClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class PurgeUserEvent {

private static final String PROJECT_ID = System.getenv("PROJECT_ID");
private static final String DEFAULT_CATALOG =
String.format("projects/%s/locations/global/catalogs/default_catalog", PROJECT_ID);
private static final String VISITOR_ID = "test_visitor_id";

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException {
writeUserEvent(VISITOR_ID);
callPurgeUserEvents();
}
kurtisvg marked this conversation as resolved.
Show resolved Hide resolved

public static void callPurgeUserEvents()
throws IOException, ExecutionException, InterruptedException {
OperationFuture<PurgeUserEventsResponse, PurgeMetadata> purgeOperation =
UserEventServiceClient.create().purgeUserEventsAsync(getPurgeUserEventRequest());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


System.out.printf("The purge operation was started: %s%n", purgeOperation.getName());
}

private static PurgeUserEventsRequest getPurgeUserEventRequest() {
PurgeUserEventsRequest purgeUserEventsRequest =
PurgeUserEventsRequest.newBuilder()
// TO CHECK ERROR HANDLING SET INVALID FILTER HERE:
.setFilter(String.format("visitorId=\"%s\"", VISITOR_ID))
.setParent(DEFAULT_CATALOG)
.setForce(true)
.build();

System.out.printf("Purge user events request: %s%n", purgeUserEventsRequest);

return purgeUserEventsRequest;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to return the operation name to assert it in the test

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the style guide:

Snippet methods should specify a return type of void and avoid returning any value wherever possible. Instead, show the user how to interact with a returned object programmatically by printing some example attributes to the console.

Assert the behavior is correct from the output to console, since that is what the user will use to verify the output is correct.

}
}

// [END retail_purge_user_event]
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START retail_rejoin_user_event]

/*
* Import user events into a catalog from inline source using Retail API

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment not correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

*/

package events;

import static setup.SetupCleanup.purgeUserEvent;
import static setup.SetupCleanup.writeUserEvent;

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.retail.v2.RejoinUserEventsMetadata;
import com.google.cloud.retail.v2.RejoinUserEventsRequest;
import com.google.cloud.retail.v2.RejoinUserEventsRequest.UserEventRejoinScope;
import com.google.cloud.retail.v2.RejoinUserEventsResponse;
import com.google.cloud.retail.v2.UserEventServiceClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class RejoinUserEvent {

private static final String PROJECT_ID = System.getenv("PROJECT_ID");
private static final String DEFAULT_CATALOG =
String.format("projects/%s/locations/global/catalogs/default_catalog", PROJECT_ID);
// TO CHECK THE ERROR HANDLING TRY TO PASS INVALID CATALOG:
// 'invalid_catalog' INSTEAD OF 'default_catalog'
private static final String VISITOR_ID = "test_visitor_id";

public static void main(final String[] args)
throws IOException, ExecutionException, InterruptedException {
writeUserEvent(VISITOR_ID);
callRejoinUserEvents();
purgeUserEvent(VISITOR_ID);
}

public static void callRejoinUserEvents()
throws IOException, ExecutionException, InterruptedException {
OperationFuture<RejoinUserEventsResponse, RejoinUserEventsMetadata> rejoinOperation =
UserEventServiceClient.create().rejoinUserEventsAsync(getRejoinUserEventRequest());

System.out.printf("The rejoin operation was started: %s%n", rejoinOperation.getName());
}

public static RejoinUserEventsRequest getRejoinUserEventRequest() {
RejoinUserEventsRequest rejoinUserEventsRequest =
RejoinUserEventsRequest.newBuilder()
.setParent(DEFAULT_CATALOG)
.setUserEventRejoinScope(UserEventRejoinScope.UNJOINED_EVENTS)
.build();

System.out.printf("Rejoin user events request: %s%n", rejoinUserEventsRequest);

return rejoinUserEventsRequest;
}
}

// [END retail_rejoin_user_event]
Loading