Skip to content

Commit e3055e1

Browse files
authored
docs(samples): added samples and test to migrate key and get metrics (#161)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/python-recaptcha-enterprise/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes #160 🦕
1 parent 4045b5c commit e3055e1

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2021 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+
# [START recaptcha_enterprise_get_metrics_site_key]
16+
from google.cloud import recaptchaenterprise_v1
17+
18+
19+
def get_metrics(project_id: str, recaptcha_site_key: str) -> None:
20+
""" Get metrics specific to a recaptcha site key.
21+
E.g: score bucket count for a key or number of
22+
times the checkbox key failed/ passed etc.,
23+
Args:
24+
project_id: Google Cloud Project ID.
25+
recaptcha_site_key: Specify the site key to get metrics.
26+
"""
27+
28+
client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()
29+
30+
metrics_name = f"projects/{project_id}/keys/{recaptcha_site_key}/metrics"
31+
request = recaptchaenterprise_v1.GetMetricsRequest()
32+
request.name = metrics_name
33+
34+
response = client.get_metrics(request)
35+
36+
# Retrieve the metrics you want from the key.
37+
# If the site key is checkbox type: then use response.challenge_metrics
38+
# instead of response.score_metrics
39+
for day_metric in response.score_metrics:
40+
# Each 'day_metric' is in the granularity of one day.
41+
score_bucket_count = day_metric.overall_metrics.score_buckets
42+
print(score_bucket_count)
43+
44+
print(f"Retrieved the bucket count for score based key: {recaptcha_site_key}")
45+
46+
47+
# [END recaptcha_enterprise_get_metrics_site_key]

recaptcha_enterprise/snippets/list_site_keys.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
# [START recaptcha_enterprise_list_site_keys]
1616
from google.cloud import recaptchaenterprise_v1
1717

18+
from google.cloud.recaptchaenterprise_v1.services.recaptcha_enterprise_service.pagers import (
19+
ListKeysPager,
20+
)
1821

19-
def list_site_keys(project_id: str) -> None:
22+
23+
def list_site_keys(project_id: str) -> ListKeysPager:
2024
""" List all keys present under the given project ID.
2125
2226
Args:
@@ -36,6 +40,8 @@ def list_site_keys(project_id: str) -> None:
3640
for i, key in enumerate(response):
3741
print(f"{str(i)}. {key.name}")
3842

43+
return response
44+
3945

4046
# [END recaptcha_enterprise_list_site_keys]
4147

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2021 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+
# [START recaptcha_enterprise_migrate_site_key]
16+
from google.cloud import recaptchaenterprise_v1
17+
18+
from list_site_keys import list_site_keys
19+
20+
21+
def migrate_site_key(project_id: str, recaptcha_site_key: str) -> None:
22+
""" Migrate a key from reCAPTCHA (non-Enterprise) to reCAPTCHA Enterprise.
23+
If you created the key using Admin console: https://www.google.com/recaptcha/admin/site,
24+
then use this API to migrate to reCAPTCHA Enterprise.
25+
For more info, see: https://cloud.google.com/recaptcha-enterprise/docs/migrate-recaptcha
26+
Args:
27+
project_id: Google Cloud Project ID.
28+
recaptcha_site_key: Specify the site key to migrate.
29+
"""
30+
31+
client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()
32+
33+
# Specify the key name to migrate.
34+
name = f"projects/{project_id}/keys/{recaptcha_site_key}"
35+
request = recaptchaenterprise_v1.MigrateKeyRequest()
36+
request.name = name
37+
38+
response = client.migrate_key(request)
39+
# To verify if the site key has been migrated, use 'list_site_keys' to check if the
40+
# key is present.
41+
for key in list_site_keys(project_id):
42+
if key.name == response.name:
43+
print(f"Key migrated successfully: {recaptcha_site_key}")
44+
45+
46+
# [END recaptcha_enterprise_migrate_site_key]

recaptcha_enterprise/snippets/test_site_key.py

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from create_site_key import create_site_key
2121
from delete_site_key import delete_site_key
22+
from get_metrics import get_metrics
2223
from get_site_key import get_site_key
2324
from list_site_keys import list_site_keys
2425
from update_site_key import update_site_key
@@ -63,3 +64,11 @@ def test_update_site_key(capsys: CaptureFixture, recaptcha_site_key: str) -> Non
6364
)
6465
out, _ = capsys.readouterr()
6566
assert re.search("reCAPTCHA Site key successfully updated ! ", out)
67+
68+
69+
def test_get_metrics(capsys: CaptureFixture, recaptcha_site_key: str) -> None:
70+
get_metrics(project_id=GOOGLE_CLOUD_PROJECT, recaptcha_site_key=recaptcha_site_key)
71+
out, _ = capsys.readouterr()
72+
assert re.search(
73+
f"Retrieved the bucket count for score based key: {recaptcha_site_key}", out
74+
)

0 commit comments

Comments
 (0)