Skip to content

Commit 81cc97f

Browse files
tetiana-karasovagcf-owl-bot[bot]parthea
authored andcommitted
docs(samples): add samples for write/rejoin/purge user events (#157)
* feat: User Events: write/rejoin/purge * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * lint fix * change the copyright year * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix tests * remove endpoint * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent 9d36026 commit 81cc97f

8 files changed

+301
-3
lines changed

generated_samples/interactive-tutorials/events/noxfile_config.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020 Google LLC
1+
# Copyright 2022 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -24,7 +24,6 @@
2424
# to 'PROJECT_NUMBER' if you want to opt in using a
2525
# build specific Cloud project. You can also use your own string
2626
# to use your own Cloud project.
27-
# 'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
2827
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
2928
# A dictionary you want to inject into your test. Don't put any
3029
# secrets here. These values will override predefined values.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright 2022 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
# [START retail_purge_user_event]
17+
# Import user events into a catalog from inline source using Retail API
18+
#
19+
import os
20+
21+
from google.cloud.retail import PurgeUserEventsRequest, UserEventServiceClient
22+
23+
from setup_events.setup_cleanup import write_user_event
24+
25+
project_id = os.getenv("GOOGLE_CLOUD_PROJECT")
26+
27+
default_catalog = "projects/{0}/locations/global/catalogs/default_catalog".format(
28+
project_id
29+
)
30+
visitor_id = "test_visitor_id"
31+
32+
33+
# get purge user event request
34+
def get_purge_user_event_request():
35+
purge_user_event_request = PurgeUserEventsRequest()
36+
# TO CHECK ERROR HANDLING SET INVALID FILTER HERE:
37+
purge_user_event_request.filter = 'visitorId="{}"'.format(visitor_id)
38+
purge_user_event_request.parent = default_catalog
39+
purge_user_event_request.force = True
40+
print("---purge user events request---")
41+
print(purge_user_event_request)
42+
return purge_user_event_request
43+
44+
45+
# call the Retail API to purge user event
46+
def call_purge_user_events():
47+
purge_operation = UserEventServiceClient().purge_user_events(
48+
get_purge_user_event_request()
49+
)
50+
51+
print("---the purge operation was started:----")
52+
print(purge_operation.operation.name)
53+
54+
55+
write_user_event(visitor_id)
56+
call_purge_user_events()
57+
# [END retail_purge_user_event]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2022 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import re
16+
import subprocess
17+
18+
19+
def test_create_product():
20+
output = str(subprocess.check_output("python purge_user_event.py", shell=True))
21+
22+
assert re.match(".*the user event is written.*", output)
23+
assert re.match(
24+
'.*purge user events request.*?parent: "projects/.*?/locations/global/catalogs/default_catalog.*',
25+
output,
26+
)
27+
assert re.match(
28+
'.*purge user events request.*?filter: "visitorId=.*?test_visitor_id.*?".*',
29+
output,
30+
)
31+
assert re.match(
32+
'.*purge user events request.*?parent: "projects/.*?/locations/global/catalogs/default_catalog.*',
33+
output,
34+
)
35+
assert re.match(".*purge user events request.*?force: true.*", output)
36+
assert re.match(
37+
".*the purge operation was started.*?projects/.*?/locations/global/catalogs/default_catalog/operations/purge-user-events.*",
38+
output,
39+
)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright 2022 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
# [START retail_rejoin_user_event]
17+
# Import user events into a catalog from inline source using Retail API
18+
#
19+
import os
20+
21+
from google.cloud.retail import RejoinUserEventsRequest, UserEventServiceClient
22+
23+
from setup_events.setup_cleanup import purge_user_event, write_user_event
24+
25+
project_id = os.getenv("GOOGLE_CLOUD_PROJECT")
26+
27+
default_catalog = "projects/{0}/locations/global/catalogs/default_catalog".format(
28+
project_id
29+
)
30+
visitor_id = "test_visitor_id"
31+
32+
33+
# get rejoin user event request
34+
def get_rejoin_user_event_request():
35+
# TO CHECK THE ERROR HANDLING TRY TO PASS INVALID CATALOG:
36+
# default_catalog = "projects/{0}/locations/global/catalogs/invalid_catalog".format(project_number)
37+
rejoin_user_event_request = RejoinUserEventsRequest()
38+
rejoin_user_event_request.parent = default_catalog
39+
rejoin_user_event_request.user_event_rejoin_scope = (
40+
RejoinUserEventsRequest.UserEventRejoinScope.UNJOINED_EVENTS
41+
)
42+
print("---rejoin user events request---")
43+
print(rejoin_user_event_request)
44+
return rejoin_user_event_request
45+
46+
47+
# call the Retail API to rejoin user event
48+
def call_rejoin_user_events():
49+
rejoin_operation = UserEventServiceClient().rejoin_user_events(
50+
get_rejoin_user_event_request()
51+
)
52+
53+
print("---the rejoin operation was started:----")
54+
print(rejoin_operation.operation.name)
55+
56+
57+
write_user_event(visitor_id)
58+
call_rejoin_user_events()
59+
purge_user_event(visitor_id)
60+
61+
# [END retail_rejoin_user_event]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2022 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import re
16+
import subprocess
17+
18+
19+
def test_create_product():
20+
output = str(subprocess.check_output("python rejoin_user_event.py", shell=True))
21+
22+
assert re.match(".*the user event is written.*", output)
23+
assert re.match(
24+
'.*rejoin user events request.*?parent: "projects/.*?/locations/global/catalogs/default_catalog.*',
25+
output,
26+
)
27+
assert re.match(
28+
".*rejoin user events request.*?user_event_rejoin_scope: UNJOINED_EVENTS.*",
29+
output,
30+
)
31+
assert re.match(
32+
".*the rejoin operation was started.*?projects/.*?/locations/global/catalogs/default_catalog/operations/rejoin-user-events.*",
33+
output,
34+
)
35+
assert re.match(
36+
".*the purge operation was started.*?projects/.*?/locations/global/catalogs/default_catalog/operations/purge-user-events.*",
37+
output,
38+
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
google==3.0.0
22
google-cloud-retail==1.3.0
33
google-cloud-storage==2.1.0
4-
google-cloud-bigquery==2.33.0
4+
google-cloud-bigquery==2.33.0
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copyright 2022 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
# [START retail_write_user_event]
17+
# Import user events into a catalog from inline source using Retail API
18+
#
19+
import datetime
20+
import os
21+
22+
from google.cloud.retail import UserEvent, UserEventServiceClient, WriteUserEventRequest
23+
from google.protobuf.timestamp_pb2 import Timestamp
24+
25+
from setup_events.setup_cleanup import purge_user_event
26+
27+
project_id = os.getenv("GOOGLE_CLOUD_PROJECT")
28+
29+
default_catalog = "projects/{0}/locations/global/catalogs/default_catalog".format(
30+
project_id
31+
)
32+
visitor_id = "test_visitor_id"
33+
34+
35+
# get user event
36+
def get_user_event():
37+
timestamp = Timestamp()
38+
timestamp.seconds = int(datetime.datetime.now().timestamp())
39+
40+
user_event = UserEvent()
41+
user_event.event_type = "home-page-view"
42+
user_event.visitor_id = visitor_id
43+
user_event.event_time = timestamp
44+
45+
print(user_event)
46+
return user_event
47+
48+
49+
# get write user event request
50+
def get_write_event_request(user_event):
51+
# TO CHECK THE ERROR HANDLING TRY TO PASS INVALID CATALOG:
52+
# default_catalog = "projects/{0}/locations/global/catalogs/invalid_catalog"
53+
# .format(project_number)
54+
write_user_event_request = WriteUserEventRequest()
55+
write_user_event_request.user_event = user_event
56+
write_user_event_request.parent = default_catalog
57+
58+
print("---write user event request---")
59+
print(write_user_event_request)
60+
61+
return write_user_event_request
62+
63+
64+
# call the Retail API to write user event
65+
def write_user_event():
66+
write_user_event_request = get_write_event_request(get_user_event())
67+
user_event = UserEventServiceClient().write_user_event(write_user_event_request)
68+
69+
print("---written user event:---")
70+
print(user_event)
71+
return user_event
72+
73+
74+
write_user_event()
75+
purge_user_event(visitor_id)
76+
77+
# [END retail_write_user_event]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2022 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import re
16+
import subprocess
17+
18+
19+
def test_create_product():
20+
output = str(subprocess.check_output("python write_user_event.py", shell=True))
21+
22+
assert re.match(
23+
'.*write user event request.*?user_event.*?event_type: "home-page-view".*',
24+
output,
25+
)
26+
assert re.match('.*written user event.*?event_type: "home-page-view".*', output)
27+
assert re.match('.*written user event.*?visitor_id: "test_visitor_id".*', output)

0 commit comments

Comments
 (0)