From 929c44c466fa1cb08255c0be730b2a9d1d2e2c04 Mon Sep 17 00:00:00 2001 From: Yoshi Automation Bot Date: Mon, 16 Nov 2020 15:39:38 -0800 Subject: [PATCH] feat: add support for realtime reports (#12) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * changes without context autosynth cannot find the source of changes triggered by earlier changes in this repository, or by version upgrades to tools such as linters. * chore(py-library): update decrypt secrets file * chore(py-library): update decrypt secrets file From https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/scripts/decrypt-secrets.sh * docs: explain conditional Co-authored-by: Jeffrey Rennie Source-Author: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Source-Date: Tue Sep 8 11:35:59 2020 -0600 Source-Repo: googleapis/synthtool Source-Sha: d302f93d7f47e2852e585ac35ab2d15585717ec0 Source-Link: https://github.com/googleapis/synthtool/commit/d302f93d7f47e2852e585ac35ab2d15585717ec0 * chore(python-library): use sphinx 1.5.5 for the docfx job Originally tested at: https://github.com/googleapis/python-texttospeech/pull/89 This change will fix the missing docstring in the yaml files. Source-Author: Takashi Matsuo Source-Date: Thu Sep 10 04:12:14 2020 +0000 Source-Repo: googleapis/synthtool Source-Sha: ffcee7952b74f647cbb3ef021d95422f10816fca Source-Link: https://github.com/googleapis/synthtool/commit/ffcee7952b74f647cbb3ef021d95422f10816fca * build(python): use release-publish app for notifying GitHub of releas… * build(python): use release-publish app for notifying GitHub of release status * fix: re-add pypi password Source-Author: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Source-Date: Wed Sep 16 08:46:42 2020 -0600 Source-Repo: googleapis/synthtool Source-Sha: 257fda18168bedb76985024bd198ed1725485488 Source-Link: https://github.com/googleapis/synthtool/commit/257fda18168bedb76985024bd198ed1725485488 * build(python): add secret manager in kokoro Source-Author: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Source-Date: Wed Sep 16 10:24:40 2020 -0600 Source-Repo: googleapis/synthtool Source-Sha: dba48bb9bc6959c232bec9150ac6313b608fe7bd Source-Link: https://github.com/googleapis/synthtool/commit/dba48bb9bc6959c232bec9150ac6313b608fe7bd * chore(python): add sphinx doctest extension Source-Author: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Source-Date: Mon Sep 21 13:09:57 2020 -0600 Source-Repo: googleapis/synthtool Source-Sha: 27f4406999b1eee29e04b09b2423a8e4646c7e24 Source-Link: https://github.com/googleapis/synthtool/commit/27f4406999b1eee29e04b09b2423a8e4646c7e24 * chore(python): remove note about editable installs `pip install -e .` is supported and is how we install the library for tests. Source-Author: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Source-Date: Tue Sep 22 12:06:12 2020 -0600 Source-Repo: googleapis/synthtool Source-Sha: a651c5fb763c69a921aecdd3e1d8dc51dbf20f8d Source-Link: https://github.com/googleapis/synthtool/commit/a651c5fb763c69a921aecdd3e1d8dc51dbf20f8d * chore(python): use BUILD_SPECIFIC_GCLOUD_PROJECT for samples https://github.com/googleapis/python-talent/blob/ef045e8eb348db36d7a2a611e6f26b11530d273b/samples/snippets/noxfile_config.py#L27-L32 `BUILD_SPECIFIC_GCLOUD_PROJECT` is an alternate project used for sample tests that do poorly with concurrent runs on the same project. Source-Author: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Source-Date: Wed Sep 30 13:06:03 2020 -0600 Source-Repo: googleapis/synthtool Source-Sha: 9b0da5204ab90bcc36f8cd4e5689eff1a54cc3e4 Source-Link: https://github.com/googleapis/synthtool/commit/9b0da5204ab90bcc36f8cd4e5689eff1a54cc3e4 * build(python): samples tests should pass if no samples exist Source-Author: Daniel Sanche Source-Date: Wed Oct 14 08:00:06 2020 -0700 Source-Repo: googleapis/synthtool Source-Sha: 477764cc4ee6db346d3febef2bb1ea0abf27de52 Source-Link: https://github.com/googleapis/synthtool/commit/477764cc4ee6db346d3febef2bb1ea0abf27de52 * chore(python_library): change the docs bucket name Source-Author: Takashi Matsuo Source-Date: Fri Oct 16 09:58:05 2020 -0700 Source-Repo: googleapis/synthtool Source-Sha: da5c6050d13b4950c82666a81d8acd25157664ae Source-Link: https://github.com/googleapis/synthtool/commit/da5c6050d13b4950c82666a81d8acd25157664ae * chore(docs): update code of conduct of synthtool and templates Source-Author: Christopher Wilcox Source-Date: Thu Oct 22 14:22:01 2020 -0700 Source-Repo: googleapis/synthtool Source-Sha: 5f6ef0ec5501d33c4667885b37a7685a30d41a76 Source-Link: https://github.com/googleapis/synthtool/commit/5f6ef0ec5501d33c4667885b37a7685a30d41a76 * docs: add proto-plus to intersphinx mapping Source-Author: Tim Swast Source-Date: Tue Oct 27 12:01:14 2020 -0500 Source-Repo: googleapis/synthtool Source-Sha: ea52b8a0bd560f72f376efcf45197fb7c8869120 Source-Link: https://github.com/googleapis/synthtool/commit/ea52b8a0bd560f72f376efcf45197fb7c8869120 * fix(python_library): fix external unit test dependencies I recently submitted https://github.com/googleapis/synthtool/pull/811/files, allowing external dependencies for unit tests. This fixes a small missing comma bug Source-Author: Daniel Sanche Source-Date: Thu Oct 29 16:58:01 2020 -0700 Source-Repo: googleapis/synthtool Source-Sha: 6542bd723403513626f61642fc02ddca528409aa Source-Link: https://github.com/googleapis/synthtool/commit/6542bd723403513626f61642fc02ddca528409aa * chore: manual regen * chore: fix name Co-authored-by: Bu Sun Kim --- .kokoro/docs/common.cfg | 2 +- .kokoro/populate-secrets.sh | 43 + .kokoro/release/common.cfg | 50 +- .kokoro/samples/python3.6/common.cfg | 6 + .kokoro/samples/python3.7/common.cfg | 6 + .kokoro/samples/python3.8/common.cfg | 6 + .kokoro/test-samples.sh | 8 +- .kokoro/trampoline.sh | 15 +- CODE_OF_CONDUCT.md | 123 ++- CONTRIBUTING.rst | 19 - docs/conf.py | 4 +- docs/data_v1alpha/types.rst | 1 + google/analytics/data/__init__.py | 8 + google/analytics/data_v1alpha/__init__.py | 4 + .../alpha_analytics_data/async_client.py | 155 ++- .../services/alpha_analytics_data/client.py | 268 +++++- .../alpha_analytics_data/transports/base.py | 15 + .../alpha_analytics_data/transports/grpc.py | 111 ++- .../transports/grpc_asyncio.py | 102 +- .../analytics/data_v1alpha/types/__init__.py | 4 + .../data_v1alpha/types/analytics_data_api.py | 178 +++- google/analytics/data_v1alpha/types/data.py | 168 ++-- noxfile.py | 10 +- scripts/decrypt-secrets.sh | 15 +- synth.metadata | 15 +- .../data_v1alpha/test_alpha_analytics_data.py | 898 ++++++++++++------ 26 files changed, 1654 insertions(+), 580 deletions(-) create mode 100755 .kokoro/populate-secrets.sh diff --git a/.kokoro/docs/common.cfg b/.kokoro/docs/common.cfg index 55bb9de..cc08746 100644 --- a/.kokoro/docs/common.cfg +++ b/.kokoro/docs/common.cfg @@ -30,7 +30,7 @@ env_vars: { env_vars: { key: "V2_STAGING_BUCKET" - value: "docs-staging-v2-staging" + value: "docs-staging-v2" } # It will upload the docker image after successful builds. diff --git a/.kokoro/populate-secrets.sh b/.kokoro/populate-secrets.sh new file mode 100755 index 0000000..f525142 --- /dev/null +++ b/.kokoro/populate-secrets.sh @@ -0,0 +1,43 @@ +#!/bin/bash +# Copyright 2020 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. + +set -eo pipefail + +function now { date +"%Y-%m-%d %H:%M:%S" | tr -d '\n' ;} +function msg { println "$*" >&2 ;} +function println { printf '%s\n' "$(now) $*" ;} + + +# Populates requested secrets set in SECRET_MANAGER_KEYS from service account: +# kokoro-trampoline@cloud-devrel-kokoro-resources.iam.gserviceaccount.com +SECRET_LOCATION="${KOKORO_GFILE_DIR}/secret_manager" +msg "Creating folder on disk for secrets: ${SECRET_LOCATION}" +mkdir -p ${SECRET_LOCATION} +for key in $(echo ${SECRET_MANAGER_KEYS} | sed "s/,/ /g") +do + msg "Retrieving secret ${key}" + docker run --entrypoint=gcloud \ + --volume=${KOKORO_GFILE_DIR}:${KOKORO_GFILE_DIR} \ + gcr.io/google.com/cloudsdktool/cloud-sdk \ + secrets versions access latest \ + --project cloud-devrel-kokoro-resources \ + --secret ${key} > \ + "${SECRET_LOCATION}/${key}" + if [[ $? == 0 ]]; then + msg "Secret written to ${SECRET_LOCATION}/${key}" + else + msg "Error retrieving secret ${key}" + fi +done diff --git a/.kokoro/release/common.cfg b/.kokoro/release/common.cfg index 963da8b..f0df3a7 100644 --- a/.kokoro/release/common.cfg +++ b/.kokoro/release/common.cfg @@ -23,42 +23,18 @@ env_vars: { value: "github/python-analytics-data/.kokoro/release.sh" } -# Fetch the token needed for reporting release status to GitHub -before_action { - fetch_keystore { - keystore_resource { - keystore_config_id: 73713 - keyname: "yoshi-automation-github-key" - } - } -} - -# Fetch PyPI password -before_action { - fetch_keystore { - keystore_resource { - keystore_config_id: 73713 - keyname: "google_cloud_pypi_password" - } - } -} - -# Fetch magictoken to use with Magic Github Proxy -before_action { - fetch_keystore { - keystore_resource { - keystore_config_id: 73713 - keyname: "releasetool-magictoken" - } - } +# Fetch PyPI password +before_action { + fetch_keystore { + keystore_resource { + keystore_config_id: 73713 + keyname: "google_cloud_pypi_password" + } + } } -# Fetch api key to use with Magic Github Proxy -before_action { - fetch_keystore { - keystore_resource { - keystore_config_id: 73713 - keyname: "magic-github-proxy-api-key" - } - } -} +# Tokens needed to report release status back to GitHub +env_vars: { + key: "SECRET_MANAGER_KEYS" + value: "releasetool-publish-reporter-app,releasetool-publish-reporter-googleapis-installation,releasetool-publish-reporter-pem" +} \ No newline at end of file diff --git a/.kokoro/samples/python3.6/common.cfg b/.kokoro/samples/python3.6/common.cfg index 01a27bd..9df042d 100644 --- a/.kokoro/samples/python3.6/common.cfg +++ b/.kokoro/samples/python3.6/common.cfg @@ -13,6 +13,12 @@ env_vars: { value: "py-3.6" } +# Declare build specific Cloud project. +env_vars: { + key: "BUILD_SPECIFIC_GCLOUD_PROJECT" + value: "python-docs-samples-tests-py36" +} + env_vars: { key: "TRAMPOLINE_BUILD_FILE" value: "github/python-analytics-data/.kokoro/test-samples.sh" diff --git a/.kokoro/samples/python3.7/common.cfg b/.kokoro/samples/python3.7/common.cfg index 47b9404..a66ab73 100644 --- a/.kokoro/samples/python3.7/common.cfg +++ b/.kokoro/samples/python3.7/common.cfg @@ -13,6 +13,12 @@ env_vars: { value: "py-3.7" } +# Declare build specific Cloud project. +env_vars: { + key: "BUILD_SPECIFIC_GCLOUD_PROJECT" + value: "python-docs-samples-tests-py37" +} + env_vars: { key: "TRAMPOLINE_BUILD_FILE" value: "github/python-analytics-data/.kokoro/test-samples.sh" diff --git a/.kokoro/samples/python3.8/common.cfg b/.kokoro/samples/python3.8/common.cfg index 8fe0c85..1f908d1 100644 --- a/.kokoro/samples/python3.8/common.cfg +++ b/.kokoro/samples/python3.8/common.cfg @@ -13,6 +13,12 @@ env_vars: { value: "py-3.8" } +# Declare build specific Cloud project. +env_vars: { + key: "BUILD_SPECIFIC_GCLOUD_PROJECT" + value: "python-docs-samples-tests-py38" +} + env_vars: { key: "TRAMPOLINE_BUILD_FILE" value: "github/python-analytics-data/.kokoro/test-samples.sh" diff --git a/.kokoro/test-samples.sh b/.kokoro/test-samples.sh index b0df5e2..459e0fc 100755 --- a/.kokoro/test-samples.sh +++ b/.kokoro/test-samples.sh @@ -28,6 +28,12 @@ if [[ $KOKORO_BUILD_ARTIFACTS_SUBDIR = *"periodic"* ]]; then git checkout $LATEST_RELEASE fi +# Exit early if samples directory doesn't exist +if [ ! -d "./samples" ]; then + echo "No tests run. `./samples` not found" + exit 0 +fi + # Disable buffering, so that the logs stream through. export PYTHONUNBUFFERED=1 @@ -101,4 +107,4 @@ cd "$ROOT" # Workaround for Kokoro permissions issue: delete secrets rm testing/{test-env.sh,client-secrets.json,service-account.json} -exit "$RTN" \ No newline at end of file +exit "$RTN" diff --git a/.kokoro/trampoline.sh b/.kokoro/trampoline.sh index e8c4251..f39236e 100755 --- a/.kokoro/trampoline.sh +++ b/.kokoro/trampoline.sh @@ -15,9 +15,14 @@ set -eo pipefail -python3 "${KOKORO_GFILE_DIR}/trampoline_v1.py" || ret_code=$? +# Always run the cleanup script, regardless of the success of bouncing into +# the container. +function cleanup() { + chmod +x ${KOKORO_GFILE_DIR}/trampoline_cleanup.sh + ${KOKORO_GFILE_DIR}/trampoline_cleanup.sh + echo "cleanup"; +} +trap cleanup EXIT -chmod +x ${KOKORO_GFILE_DIR}/trampoline_cleanup.sh -${KOKORO_GFILE_DIR}/trampoline_cleanup.sh || true - -exit ${ret_code} +$(dirname $0)/populate-secrets.sh # Secret Manager secrets. +python3 "${KOKORO_GFILE_DIR}/trampoline_v1.py" \ No newline at end of file diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index b3d1f60..039f436 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -1,44 +1,95 @@ -# Contributor Code of Conduct +# Code of Conduct -As contributors and maintainers of this project, -and in the interest of fostering an open and welcoming community, -we pledge to respect all people who contribute through reporting issues, -posting feature requests, updating documentation, -submitting pull requests or patches, and other activities. +## Our Pledge -We are committed to making participation in this project -a harassment-free experience for everyone, -regardless of level of experience, gender, gender identity and expression, -sexual orientation, disability, personal appearance, -body size, race, ethnicity, age, religion, or nationality. +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, gender identity and expression, level of +experience, education, socio-economic status, nationality, personal appearance, +race, religion, or sexual identity and orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members Examples of unacceptable behavior by participants include: -* The use of sexualized language or imagery -* Personal attacks -* Trolling or insulting/derogatory comments -* Public or private harassment -* Publishing other's private information, -such as physical or electronic -addresses, without explicit permission -* Other unethical or unprofessional conduct. +* The use of sexualized language or imagery and unwelcome sexual attention or + advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic + address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. Project maintainers have the right and responsibility to remove, edit, or reject -comments, commits, code, wiki edits, issues, and other contributions -that are not aligned to this Code of Conduct. -By adopting this Code of Conduct, -project maintainers commit themselves to fairly and consistently -applying these principles to every aspect of managing this project. -Project maintainers who do not follow or enforce the Code of Conduct -may be permanently removed from the project team. - -This code of conduct applies both within project spaces and in public spaces -when an individual is representing the project or its community. - -Instances of abusive, harassing, or otherwise unacceptable behavior -may be reported by opening an issue -or contacting one or more of the project maintainers. - -This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, -available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/) +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, or to ban temporarily or permanently any +contributor for other behaviors that they deem inappropriate, threatening, +offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +This Code of Conduct also applies outside the project spaces when the Project +Steward has a reasonable belief that an individual's behavior may have a +negative impact on the project or its community. + +## Conflict Resolution + +We do not believe that all conflict is bad; healthy debate and disagreement +often yield positive results. However, it is never okay to be disrespectful or +to engage in behavior that violates the project’s code of conduct. + +If you see someone violating the code of conduct, you are encouraged to address +the behavior directly with those involved. Many issues can be resolved quickly +and easily, and this gives people more control over the outcome of their +dispute. If you are unable to resolve the matter for any reason, or if the +behavior is threatening or harassing, report it. We are dedicated to providing +an environment where participants feel welcome and safe. + + +Reports should be directed to *googleapis-stewards@google.com*, the +Project Steward(s) for *Google Cloud Client Libraries*. It is the Project Steward’s duty to +receive and address reported violations of the code of conduct. They will then +work with a committee consisting of representatives from the Open Source +Programs Office and the Google Open Source Strategy team. If for any reason you +are uncomfortable reaching out to the Project Steward, please email +opensource@google.com. + +We will investigate every complaint, but you may not receive a direct response. +We will use our discretion in determining when and how to follow up on reported +incidents, which may range from not taking action to permanent expulsion from +the project and project-sponsored spaces. We will notify the accused of the +report and provide them an opportunity to discuss it before any action is taken. +The identity of the reporter will be omitted from the details of the report +supplied to the accused. In potentially harmful situations, such as ongoing +harassment or threats to anyone's safety, we may take action without notice. + +## Attribution + +This Code of Conduct is adapted from the Contributor Covenant, version 1.4, +available at +https://www.contributor-covenant.org/version/1/4/code-of-conduct.html \ No newline at end of file diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 2deb9db..4c414fa 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -80,25 +80,6 @@ We use `nox `__ to instrument our tests. .. nox: https://pypi.org/project/nox/ -Note on Editable Installs / Develop Mode -======================================== - -- As mentioned previously, using ``setuptools`` in `develop mode`_ - or a ``pip`` `editable install`_ is not possible with this - library. This is because this library uses `namespace packages`_. - For context see `Issue #2316`_ and the relevant `PyPA issue`_. - - Since ``editable`` / ``develop`` mode can't be used, packages - need to be installed directly. Hence your changes to the source - tree don't get incorporated into the **already installed** - package. - -.. _namespace packages: https://www.python.org/dev/peps/pep-0420/ -.. _Issue #2316: https://github.com/GoogleCloudPlatform/google-cloud-python/issues/2316 -.. _PyPA issue: https://github.com/pypa/packaging-problems/issues/12 -.. _develop mode: https://setuptools.readthedocs.io/en/latest/setuptools.html#development-mode -.. _editable install: https://pip.pypa.io/en/stable/reference/pip_install/#editable-installs - ***************************************** I'm getting weird errors... Can you help? ***************************************** diff --git a/docs/conf.py b/docs/conf.py index 3555589..cea6615 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -29,7 +29,7 @@ # -- General configuration ------------------------------------------------ # If your documentation needs a minimal Sphinx version, state it here. -needs_sphinx = "1.6.3" +needs_sphinx = "1.5.5" # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom @@ -39,6 +39,7 @@ "sphinx.ext.autosummary", "sphinx.ext.intersphinx", "sphinx.ext.coverage", + "sphinx.ext.doctest", "sphinx.ext.napoleon", "sphinx.ext.todo", "sphinx.ext.viewcode", @@ -348,6 +349,7 @@ "google-auth": ("https://google-auth.readthedocs.io/en/stable", None), "google.api_core": ("https://googleapis.dev/python/google-api-core/latest/", None,), "grpc": ("https://grpc.io/grpc/python/", None), + "proto-plus": ("https://proto-plus-python.readthedocs.io/en/latest/", None), } diff --git a/docs/data_v1alpha/types.rst b/docs/data_v1alpha/types.rst index 6af066a..c0378d5 100644 --- a/docs/data_v1alpha/types.rst +++ b/docs/data_v1alpha/types.rst @@ -3,3 +3,4 @@ Types for Google Analytics Data v1alpha API .. automodule:: google.analytics.data_v1alpha.types :members: + :show-inheritance: diff --git a/google/analytics/data/__init__.py b/google/analytics/data/__init__.py index fbc139b..53752ab 100644 --- a/google/analytics/data/__init__.py +++ b/google/analytics/data/__init__.py @@ -39,6 +39,12 @@ from google.analytics.data_v1alpha.types.analytics_data_api import ( RunPivotReportResponse, ) +from google.analytics.data_v1alpha.types.analytics_data_api import ( + RunRealtimeReportRequest, +) +from google.analytics.data_v1alpha.types.analytics_data_api import ( + RunRealtimeReportResponse, +) from google.analytics.data_v1alpha.types.analytics_data_api import RunReportRequest from google.analytics.data_v1alpha.types.analytics_data_api import RunReportResponse from google.analytics.data_v1alpha.types.data import Cohort @@ -111,6 +117,8 @@ "Row", "RunPivotReportRequest", "RunPivotReportResponse", + "RunRealtimeReportRequest", + "RunRealtimeReportResponse", "RunReportRequest", "RunReportResponse", ) diff --git a/google/analytics/data_v1alpha/__init__.py b/google/analytics/data_v1alpha/__init__.py index f60a986..63ad392 100644 --- a/google/analytics/data_v1alpha/__init__.py +++ b/google/analytics/data_v1alpha/__init__.py @@ -24,6 +24,8 @@ from .types.analytics_data_api import Metadata from .types.analytics_data_api import RunPivotReportRequest from .types.analytics_data_api import RunPivotReportResponse +from .types.analytics_data_api import RunRealtimeReportRequest +from .types.analytics_data_api import RunRealtimeReportResponse from .types.analytics_data_api import RunReportRequest from .types.analytics_data_api import RunReportResponse from .types.data import Cohort @@ -95,6 +97,8 @@ "Row", "RunPivotReportRequest", "RunPivotReportResponse", + "RunRealtimeReportRequest", + "RunRealtimeReportResponse", "RunReportRequest", "RunReportResponse", "AlphaAnalyticsDataClient", diff --git a/google/analytics/data_v1alpha/services/alpha_analytics_data/async_client.py b/google/analytics/data_v1alpha/services/alpha_analytics_data/async_client.py index b65424f..f39cc9d 100644 --- a/google/analytics/data_v1alpha/services/alpha_analytics_data/async_client.py +++ b/google/analytics/data_v1alpha/services/alpha_analytics_data/async_client.py @@ -44,9 +44,50 @@ class AlphaAnalyticsDataAsyncClient: DEFAULT_ENDPOINT = AlphaAnalyticsDataClient.DEFAULT_ENDPOINT DEFAULT_MTLS_ENDPOINT = AlphaAnalyticsDataClient.DEFAULT_MTLS_ENDPOINT + metadata_path = staticmethod(AlphaAnalyticsDataClient.metadata_path) + parse_metadata_path = staticmethod(AlphaAnalyticsDataClient.parse_metadata_path) + + common_billing_account_path = staticmethod( + AlphaAnalyticsDataClient.common_billing_account_path + ) + parse_common_billing_account_path = staticmethod( + AlphaAnalyticsDataClient.parse_common_billing_account_path + ) + + common_folder_path = staticmethod(AlphaAnalyticsDataClient.common_folder_path) + parse_common_folder_path = staticmethod( + AlphaAnalyticsDataClient.parse_common_folder_path + ) + + common_organization_path = staticmethod( + AlphaAnalyticsDataClient.common_organization_path + ) + parse_common_organization_path = staticmethod( + AlphaAnalyticsDataClient.parse_common_organization_path + ) + + common_project_path = staticmethod(AlphaAnalyticsDataClient.common_project_path) + parse_common_project_path = staticmethod( + AlphaAnalyticsDataClient.parse_common_project_path + ) + + common_location_path = staticmethod(AlphaAnalyticsDataClient.common_location_path) + parse_common_location_path = staticmethod( + AlphaAnalyticsDataClient.parse_common_location_path + ) + from_service_account_file = AlphaAnalyticsDataClient.from_service_account_file from_service_account_json = from_service_account_file + @property + def transport(self) -> AlphaAnalyticsDataTransport: + """Return the transport used by the client instance. + + Returns: + AlphaAnalyticsDataTransport: The transport used by the client instance. + """ + return self._client.transport + get_transport_class = functools.partial( type(AlphaAnalyticsDataClient).get_transport_class, type(AlphaAnalyticsDataClient), @@ -74,16 +115,19 @@ def __init__( client_options (ClientOptions): Custom options for the client. It won't take effect if a ``transport`` instance is provided. (1) The ``api_endpoint`` property can be used to override the - default endpoint provided by the client. GOOGLE_API_USE_MTLS + default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT environment variable can also be used to override the endpoint: "always" (always use the default mTLS endpoint), "never" (always - use the default regular endpoint, this is the default value for - the environment variable) and "auto" (auto switch to the default - mTLS endpoint if client SSL credentials is present). However, - the ``api_endpoint`` property takes precedence if provided. - (2) The ``client_cert_source`` property is used to provide client - SSL credentials for mutual TLS transport. If not provided, the - default SSL credentials will be used if present. + use the default regular endpoint) and "auto" (auto switch to the + default mTLS endpoint if client certificate is present, this is + the default value). However, the ``api_endpoint`` property takes + precedence if provided. + (2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable + is "true", then the ``client_cert_source`` property can be used + to provide client certificate for mutual TLS transport. If + not provided, the default SSL client certificate will be used if + present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not + set, no client certificate will be used. Raises: google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport @@ -300,23 +344,35 @@ async def get_metadata( timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), ) -> analytics_data_api.Metadata: - r"""Returns metadata for dimensions and metrics available - in reporting methods. Used to explore the dimensions and - metrics. Dimensions and metrics will be mostly added - over time, but renames and deletions may occur. + r"""Returns metadata for dimensions and metrics available in + reporting methods. Used to explore the dimensions and metrics. + In this method, a Google Analytics GA4 Property Identifier is + specified in the request, and the metadata response includes + Custom dimensions and metrics as well as Universal metadata. + + For example if a custom metric with parameter name + ``levels_unlocked`` is registered to a property, the Metadata + response will contain ``customEvent:levels_unlocked``. Universal + metadata are dimensions and metrics applicable to any property + such as ``country`` and ``totalUsers``. Args: request (:class:`~.analytics_data_api.GetMetadataRequest`): - The request object. Request for dimension and metric - metadata. + The request object. Request for a property's dimension + and metric metadata. name (:class:`str`): - Required. The name of the metadata to - retrieve. Either has the form 'metadata' - or 'properties/{property}/metadata'. - This name field is specified in the URL - path and not URL parameters. Property is - a numeric Google Analytics App + Web - Property Id. + Required. The resource name of the metadata to retrieve. + This name field is specified in the URL path and not URL + parameters. Property is a numeric Google Analytics GA4 + Property identifier. To learn more, see `where to find + your Property + ID `__. + + Example: properties/1234/metadata + + Set the Property ID to 0 for dimensions and metrics + common to all properties. In this special mode, this + method will not return custom dimensions and metrics. This corresponds to the ``name`` field on the ``request`` instance; if ``request`` is provided, this should not be set. @@ -336,7 +392,8 @@ async def get_metadata( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([name]): + has_flattened_params = any([name]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." @@ -370,6 +427,60 @@ async def get_metadata( # Done; return the response. return response + async def run_realtime_report( + self, + request: analytics_data_api.RunRealtimeReportRequest = None, + *, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> analytics_data_api.RunRealtimeReportResponse: + r"""The Google Analytics Realtime API returns a + customized report of realtime event data for your + property. These reports show events and usage from the + last 30 minutes. + + Args: + request (:class:`~.analytics_data_api.RunRealtimeReportRequest`): + The request object. The request to generate a realtime + report. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.analytics_data_api.RunRealtimeReportResponse: + The response realtime report table + corresponding to a request. + + """ + # Create or coerce a protobuf request object. + + request = analytics_data_api.RunRealtimeReportRequest(request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = gapic_v1.method_async.wrap_method( + self._client._transport.run_realtime_report, + default_timeout=None, + client_info=DEFAULT_CLIENT_INFO, + ) + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("property", request.property),)), + ) + + # Send the request. + response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/google/analytics/data_v1alpha/services/alpha_analytics_data/client.py b/google/analytics/data_v1alpha/services/alpha_analytics_data/client.py index ff2a5e6..27046e4 100644 --- a/google/analytics/data_v1alpha/services/alpha_analytics_data/client.py +++ b/google/analytics/data_v1alpha/services/alpha_analytics_data/client.py @@ -16,17 +16,19 @@ # from collections import OrderedDict +from distutils import util import os import re -from typing import Callable, Dict, Sequence, Tuple, Type, Union +from typing import Callable, Dict, Optional, Sequence, Tuple, Type, Union import pkg_resources -import google.api_core.client_options as ClientOptions # type: ignore +from google.api_core import client_options as client_options_lib # type: ignore from google.api_core import exceptions # type: ignore from google.api_core import gapic_v1 # type: ignore from google.api_core import retry as retries # type: ignore from google.auth import credentials # type: ignore from google.auth.transport import mtls # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore @@ -130,12 +132,91 @@ def from_service_account_file(cls, filename: str, *args, **kwargs): from_service_account_json = from_service_account_file + @property + def transport(self) -> AlphaAnalyticsDataTransport: + """Return the transport used by the client instance. + + Returns: + AlphaAnalyticsDataTransport: The transport used by the client instance. + """ + return self._transport + + @staticmethod + def metadata_path(property: str,) -> str: + """Return a fully-qualified metadata string.""" + return "properties/{property}/metadata".format(property=property,) + + @staticmethod + def parse_metadata_path(path: str) -> Dict[str, str]: + """Parse a metadata path into its component segments.""" + m = re.match(r"^properties/(?P.+?)/metadata$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_billing_account_path(billing_account: str,) -> str: + """Return a fully-qualified billing_account string.""" + return "billingAccounts/{billing_account}".format( + billing_account=billing_account, + ) + + @staticmethod + def parse_common_billing_account_path(path: str) -> Dict[str, str]: + """Parse a billing_account path into its component segments.""" + m = re.match(r"^billingAccounts/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_folder_path(folder: str,) -> str: + """Return a fully-qualified folder string.""" + return "folders/{folder}".format(folder=folder,) + + @staticmethod + def parse_common_folder_path(path: str) -> Dict[str, str]: + """Parse a folder path into its component segments.""" + m = re.match(r"^folders/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_organization_path(organization: str,) -> str: + """Return a fully-qualified organization string.""" + return "organizations/{organization}".format(organization=organization,) + + @staticmethod + def parse_common_organization_path(path: str) -> Dict[str, str]: + """Parse a organization path into its component segments.""" + m = re.match(r"^organizations/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_project_path(project: str,) -> str: + """Return a fully-qualified project string.""" + return "projects/{project}".format(project=project,) + + @staticmethod + def parse_common_project_path(path: str) -> Dict[str, str]: + """Parse a project path into its component segments.""" + m = re.match(r"^projects/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_location_path(project: str, location: str,) -> str: + """Return a fully-qualified location string.""" + return "projects/{project}/locations/{location}".format( + project=project, location=location, + ) + + @staticmethod + def parse_common_location_path(path: str) -> Dict[str, str]: + """Parse a location path into its component segments.""" + m = re.match(r"^projects/(?P.+?)/locations/(?P.+?)$", path) + return m.groupdict() if m else {} + def __init__( self, *, - credentials: credentials.Credentials = None, - transport: Union[str, AlphaAnalyticsDataTransport] = None, - client_options: ClientOptions = None, + credentials: Optional[credentials.Credentials] = None, + transport: Union[str, AlphaAnalyticsDataTransport, None] = None, + client_options: Optional[client_options_lib.ClientOptions] = None, client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: """Instantiate the alpha analytics data client. @@ -149,23 +230,26 @@ def __init__( transport (Union[str, ~.AlphaAnalyticsDataTransport]): The transport to use. If set to None, a transport is chosen automatically. - client_options (ClientOptions): Custom options for the client. It - won't take effect if a ``transport`` instance is provided. + client_options (client_options_lib.ClientOptions): Custom options for the + client. It won't take effect if a ``transport`` instance is provided. (1) The ``api_endpoint`` property can be used to override the - default endpoint provided by the client. GOOGLE_API_USE_MTLS + default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT environment variable can also be used to override the endpoint: "always" (always use the default mTLS endpoint), "never" (always - use the default regular endpoint, this is the default value for - the environment variable) and "auto" (auto switch to the default - mTLS endpoint if client SSL credentials is present). However, - the ``api_endpoint`` property takes precedence if provided. - (2) The ``client_cert_source`` property is used to provide client - SSL credentials for mutual TLS transport. If not provided, the - default SSL credentials will be used if present. - client_info (google.api_core.gapic_v1.client_info.ClientInfo): - The client info used to send a user-agent string along with - API requests. If ``None``, then default info will be used. - Generally, you only need to set this if you're developing + use the default regular endpoint) and "auto" (auto switch to the + default mTLS endpoint if client certificate is present, this is + the default value). However, the ``api_endpoint`` property takes + precedence if provided. + (2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable + is "true", then the ``client_cert_source`` property can be used + to provide client certificate for mutual TLS transport. If + not provided, the default SSL client certificate will be used if + present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not + set, no client certificate will be used. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing your own client library. Raises: @@ -173,29 +257,47 @@ def __init__( creation failed for any reason. """ if isinstance(client_options, dict): - client_options = ClientOptions.from_dict(client_options) + client_options = client_options_lib.from_dict(client_options) if client_options is None: - client_options = ClientOptions.ClientOptions() + client_options = client_options_lib.ClientOptions() - if client_options.api_endpoint is None: - use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS", "never") + # Create SSL credentials for mutual TLS if needed. + use_client_cert = bool( + util.strtobool(os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")) + ) + + ssl_credentials = None + is_mtls = False + if use_client_cert: + if client_options.client_cert_source: + import grpc # type: ignore + + cert, key = client_options.client_cert_source() + ssl_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + is_mtls = True + else: + creds = SslCredentials() + is_mtls = creds.is_mtls + ssl_credentials = creds.ssl_credentials if is_mtls else None + + # Figure out which api endpoint to use. + if client_options.api_endpoint is not None: + api_endpoint = client_options.api_endpoint + else: + use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_env == "never": - client_options.api_endpoint = self.DEFAULT_ENDPOINT + api_endpoint = self.DEFAULT_ENDPOINT elif use_mtls_env == "always": - client_options.api_endpoint = self.DEFAULT_MTLS_ENDPOINT + api_endpoint = self.DEFAULT_MTLS_ENDPOINT elif use_mtls_env == "auto": - has_client_cert_source = ( - client_options.client_cert_source is not None - or mtls.has_default_client_cert_source() - ) - client_options.api_endpoint = ( - self.DEFAULT_MTLS_ENDPOINT - if has_client_cert_source - else self.DEFAULT_ENDPOINT + api_endpoint = ( + self.DEFAULT_MTLS_ENDPOINT if is_mtls else self.DEFAULT_ENDPOINT ) else: raise MutualTLSChannelError( - "Unsupported GOOGLE_API_USE_MTLS value. Accepted values: never, auto, always" + "Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted values: never, auto, always" ) # Save or instantiate the transport. @@ -219,10 +321,9 @@ def __init__( self._transport = Transport( credentials=credentials, credentials_file=client_options.credentials_file, - host=client_options.api_endpoint, + host=api_endpoint, scopes=client_options.scopes, - api_mtls_endpoint=client_options.api_endpoint, - client_cert_source=client_options.client_cert_source, + ssl_channel_credentials=ssl_credentials, quota_project_id=client_options.quota_project_id, client_info=client_info, ) @@ -434,23 +535,35 @@ def get_metadata( timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), ) -> analytics_data_api.Metadata: - r"""Returns metadata for dimensions and metrics available - in reporting methods. Used to explore the dimensions and - metrics. Dimensions and metrics will be mostly added - over time, but renames and deletions may occur. + r"""Returns metadata for dimensions and metrics available in + reporting methods. Used to explore the dimensions and metrics. + In this method, a Google Analytics GA4 Property Identifier is + specified in the request, and the metadata response includes + Custom dimensions and metrics as well as Universal metadata. + + For example if a custom metric with parameter name + ``levels_unlocked`` is registered to a property, the Metadata + response will contain ``customEvent:levels_unlocked``. Universal + metadata are dimensions and metrics applicable to any property + such as ``country`` and ``totalUsers``. Args: request (:class:`~.analytics_data_api.GetMetadataRequest`): - The request object. Request for dimension and metric - metadata. + The request object. Request for a property's dimension + and metric metadata. name (:class:`str`): - Required. The name of the metadata to - retrieve. Either has the form 'metadata' - or 'properties/{property}/metadata'. - This name field is specified in the URL - path and not URL parameters. Property is - a numeric Google Analytics App + Web - Property Id. + Required. The resource name of the metadata to retrieve. + This name field is specified in the URL path and not URL + parameters. Property is a numeric Google Analytics GA4 + Property identifier. To learn more, see `where to find + your Property + ID `__. + + Example: properties/1234/metadata + + Set the Property ID to 0 for dimensions and metrics + common to all properties. In this special mode, this + method will not return custom dimensions and metrics. This corresponds to the ``name`` field on the ``request`` instance; if ``request`` is provided, this should not be set. @@ -506,6 +619,61 @@ def get_metadata( # Done; return the response. return response + def run_realtime_report( + self, + request: analytics_data_api.RunRealtimeReportRequest = None, + *, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> analytics_data_api.RunRealtimeReportResponse: + r"""The Google Analytics Realtime API returns a + customized report of realtime event data for your + property. These reports show events and usage from the + last 30 minutes. + + Args: + request (:class:`~.analytics_data_api.RunRealtimeReportRequest`): + The request object. The request to generate a realtime + report. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.analytics_data_api.RunRealtimeReportResponse: + The response realtime report table + corresponding to a request. + + """ + # Create or coerce a protobuf request object. + + # Minor optimization to avoid making a copy if the user passes + # in a analytics_data_api.RunRealtimeReportRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, analytics_data_api.RunRealtimeReportRequest): + request = analytics_data_api.RunRealtimeReportRequest(request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.run_realtime_report] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("property", request.property),)), + ) + + # Send the request. + response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/google/analytics/data_v1alpha/services/alpha_analytics_data/transports/base.py b/google/analytics/data_v1alpha/services/alpha_analytics_data/transports/base.py index 93b1fd4..1aff5ca 100644 --- a/google/analytics/data_v1alpha/services/alpha_analytics_data/transports/base.py +++ b/google/analytics/data_v1alpha/services/alpha_analytics_data/transports/base.py @@ -124,6 +124,9 @@ def _prep_wrapped_messages(self, client_info): self.get_metadata: gapic_v1.method.wrap_method( self.get_metadata, default_timeout=None, client_info=client_info, ), + self.run_realtime_report: gapic_v1.method.wrap_method( + self.run_realtime_report, default_timeout=None, client_info=client_info, + ), } @property @@ -185,5 +188,17 @@ def get_metadata( ]: raise NotImplementedError() + @property + def run_realtime_report( + self, + ) -> typing.Callable[ + [analytics_data_api.RunRealtimeReportRequest], + typing.Union[ + analytics_data_api.RunRealtimeReportResponse, + typing.Awaitable[analytics_data_api.RunRealtimeReportResponse], + ], + ]: + raise NotImplementedError() + __all__ = ("AlphaAnalyticsDataTransport",) diff --git a/google/analytics/data_v1alpha/services/alpha_analytics_data/transports/grpc.py b/google/analytics/data_v1alpha/services/alpha_analytics_data/transports/grpc.py index 32e2bdc..239fb3e 100644 --- a/google/analytics/data_v1alpha/services/alpha_analytics_data/transports/grpc.py +++ b/google/analytics/data_v1alpha/services/alpha_analytics_data/transports/grpc.py @@ -15,6 +15,7 @@ # limitations under the License. # +import warnings from typing import Callable, Dict, Optional, Sequence, Tuple from google.api_core import grpc_helpers # type: ignore @@ -23,7 +24,6 @@ from google.auth import credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore - import grpc # type: ignore from google.analytics.data_v1alpha.types import analytics_data_api @@ -56,6 +56,7 @@ def __init__( channel: grpc.Channel = None, api_mtls_endpoint: str = None, client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, + ssl_channel_credentials: grpc.ChannelCredentials = None, quota_project_id: Optional[str] = None, client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: @@ -76,20 +77,22 @@ def __init__( ignored if ``channel`` is provided. channel (Optional[grpc.Channel]): A ``Channel`` instance through which to make calls. - api_mtls_endpoint (Optional[str]): The mutual TLS endpoint. If - provided, it overrides the ``host`` argument and tries to create + api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. + If provided, it overrides the ``host`` argument and tries to create a mutual TLS channel with client SSL credentials from ``client_cert_source`` or applicatin default SSL credentials. - client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): A - callback to provide client SSL certificate bytes and private key - bytes, both in PEM format. It is ignored if ``api_mtls_endpoint`` - is None. + client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): + Deprecated. A callback to provide client SSL certificate bytes and + private key bytes, both in PEM format. It is ignored if + ``api_mtls_endpoint`` is None. + ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials + for grpc channel. It is ignored if ``channel`` is provided. quota_project_id (Optional[str]): An optional project to use for billing and quota. - client_info (google.api_core.gapic_v1.client_info.ClientInfo): - The client info used to send a user-agent string along with - API requests. If ``None``, then default info will be used. - Generally, you only need to set this if you're developing + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing your own client library. Raises: @@ -98,6 +101,8 @@ def __init__( google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` and ``credentials_file`` are passed. """ + self._ssl_channel_credentials = ssl_channel_credentials + if channel: # Sanity check: Ensure that channel and credentials are not both # provided. @@ -105,7 +110,13 @@ def __init__( # If a channel was explicitly provided, set it. self._grpc_channel = channel + self._ssl_channel_credentials = None elif api_mtls_endpoint: + warnings.warn( + "api_mtls_endpoint and client_cert_source are deprecated", + DeprecationWarning, + ) + host = ( api_mtls_endpoint if ":" in api_mtls_endpoint @@ -136,6 +147,24 @@ def __init__( scopes=scopes or self.AUTH_SCOPES, quota_project_id=quota_project_id, ) + self._ssl_channel_credentials = ssl_credentials + else: + host = host if ":" in host else host + ":443" + + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + + # create a new channel. The provided one is ignored. + self._grpc_channel = type(self).create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + ssl_credentials=ssl_channel_credentials, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + ) self._stubs = {} # type: Dict[str, Callable] @@ -196,19 +225,8 @@ def create_channel( @property def grpc_channel(self) -> grpc.Channel: - """Create the channel designed to connect to this service. - - This property caches on the instance; repeated calls return - the same channel. + """Return the channel designed to connect to this service. """ - # Sanity check: Only create a new channel if we do not already - # have one. - if not hasattr(self, "_grpc_channel"): - self._grpc_channel = self.create_channel( - self._host, credentials=self._credentials, - ) - - # Return the channel from cache. return self._grpc_channel @property @@ -347,10 +365,17 @@ def get_metadata( ) -> Callable[[analytics_data_api.GetMetadataRequest], analytics_data_api.Metadata]: r"""Return a callable for the get metadata method over gRPC. - Returns metadata for dimensions and metrics available - in reporting methods. Used to explore the dimensions and - metrics. Dimensions and metrics will be mostly added - over time, but renames and deletions may occur. + Returns metadata for dimensions and metrics available in + reporting methods. Used to explore the dimensions and metrics. + In this method, a Google Analytics GA4 Property Identifier is + specified in the request, and the metadata response includes + Custom dimensions and metrics as well as Universal metadata. + + For example if a custom metric with parameter name + ``levels_unlocked`` is registered to a property, the Metadata + response will contain ``customEvent:levels_unlocked``. Universal + metadata are dimensions and metrics applicable to any property + such as ``country`` and ``totalUsers``. Returns: Callable[[~.GetMetadataRequest], @@ -370,5 +395,37 @@ def get_metadata( ) return self._stubs["get_metadata"] + @property + def run_realtime_report( + self, + ) -> Callable[ + [analytics_data_api.RunRealtimeReportRequest], + analytics_data_api.RunRealtimeReportResponse, + ]: + r"""Return a callable for the run realtime report method over gRPC. + + The Google Analytics Realtime API returns a + customized report of realtime event data for your + property. These reports show events and usage from the + last 30 minutes. + + Returns: + Callable[[~.RunRealtimeReportRequest], + ~.RunRealtimeReportResponse]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "run_realtime_report" not in self._stubs: + self._stubs["run_realtime_report"] = self.grpc_channel.unary_unary( + "/google.analytics.data.v1alpha.AlphaAnalyticsData/RunRealtimeReport", + request_serializer=analytics_data_api.RunRealtimeReportRequest.serialize, + response_deserializer=analytics_data_api.RunRealtimeReportResponse.deserialize, + ) + return self._stubs["run_realtime_report"] + __all__ = ("AlphaAnalyticsDataGrpcTransport",) diff --git a/google/analytics/data_v1alpha/services/alpha_analytics_data/transports/grpc_asyncio.py b/google/analytics/data_v1alpha/services/alpha_analytics_data/transports/grpc_asyncio.py index 09a92d2..2a02979 100644 --- a/google/analytics/data_v1alpha/services/alpha_analytics_data/transports/grpc_asyncio.py +++ b/google/analytics/data_v1alpha/services/alpha_analytics_data/transports/grpc_asyncio.py @@ -15,10 +15,12 @@ # limitations under the License. # +import warnings from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple from google.api_core import gapic_v1 # type: ignore from google.api_core import grpc_helpers_async # type: ignore +from google import auth # type: ignore from google.auth import credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore @@ -98,6 +100,7 @@ def __init__( channel: aio.Channel = None, api_mtls_endpoint: str = None, client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, + ssl_channel_credentials: grpc.ChannelCredentials = None, quota_project_id=None, client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: @@ -119,14 +122,16 @@ def __init__( are passed to :func:`google.auth.default`. channel (Optional[aio.Channel]): A ``Channel`` instance through which to make calls. - api_mtls_endpoint (Optional[str]): The mutual TLS endpoint. If - provided, it overrides the ``host`` argument and tries to create + api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. + If provided, it overrides the ``host`` argument and tries to create a mutual TLS channel with client SSL credentials from ``client_cert_source`` or applicatin default SSL credentials. - client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): A - callback to provide client SSL certificate bytes and private key - bytes, both in PEM format. It is ignored if ``api_mtls_endpoint`` - is None. + client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): + Deprecated. A callback to provide client SSL certificate bytes and + private key bytes, both in PEM format. It is ignored if + ``api_mtls_endpoint`` is None. + ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials + for grpc channel. It is ignored if ``channel`` is provided. quota_project_id (Optional[str]): An optional project to use for billing and quota. client_info (google.api_core.gapic_v1.client_info.ClientInfo): @@ -141,6 +146,8 @@ def __init__( google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` and ``credentials_file`` are passed. """ + self._ssl_channel_credentials = ssl_channel_credentials + if channel: # Sanity check: Ensure that channel and credentials are not both # provided. @@ -148,13 +155,24 @@ def __init__( # If a channel was explicitly provided, set it. self._grpc_channel = channel + self._ssl_channel_credentials = None elif api_mtls_endpoint: + warnings.warn( + "api_mtls_endpoint and client_cert_source are deprecated", + DeprecationWarning, + ) + host = ( api_mtls_endpoint if ":" in api_mtls_endpoint else api_mtls_endpoint + ":443" ) + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + # Create SSL credentials with client_cert_source or application # default SSL credentials. if client_cert_source: @@ -174,6 +192,24 @@ def __init__( scopes=scopes or self.AUTH_SCOPES, quota_project_id=quota_project_id, ) + self._ssl_channel_credentials = ssl_credentials + else: + host = host if ":" in host else host + ":443" + + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + + # create a new channel. The provided one is ignored. + self._grpc_channel = type(self).create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + ssl_credentials=ssl_channel_credentials, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + ) # Run the base constructor. super().__init__( @@ -194,13 +230,6 @@ def grpc_channel(self) -> aio.Channel: This property caches on the instance; repeated calls return the same channel. """ - # Sanity check: Only create a new channel if we do not already - # have one. - if not hasattr(self, "_grpc_channel"): - self._grpc_channel = self.create_channel( - self._host, credentials=self._credentials, - ) - # Return the channel from cache. return self._grpc_channel @@ -343,10 +372,17 @@ def get_metadata( ]: r"""Return a callable for the get metadata method over gRPC. - Returns metadata for dimensions and metrics available - in reporting methods. Used to explore the dimensions and - metrics. Dimensions and metrics will be mostly added - over time, but renames and deletions may occur. + Returns metadata for dimensions and metrics available in + reporting methods. Used to explore the dimensions and metrics. + In this method, a Google Analytics GA4 Property Identifier is + specified in the request, and the metadata response includes + Custom dimensions and metrics as well as Universal metadata. + + For example if a custom metric with parameter name + ``levels_unlocked`` is registered to a property, the Metadata + response will contain ``customEvent:levels_unlocked``. Universal + metadata are dimensions and metrics applicable to any property + such as ``country`` and ``totalUsers``. Returns: Callable[[~.GetMetadataRequest], @@ -366,5 +402,37 @@ def get_metadata( ) return self._stubs["get_metadata"] + @property + def run_realtime_report( + self, + ) -> Callable[ + [analytics_data_api.RunRealtimeReportRequest], + Awaitable[analytics_data_api.RunRealtimeReportResponse], + ]: + r"""Return a callable for the run realtime report method over gRPC. + + The Google Analytics Realtime API returns a + customized report of realtime event data for your + property. These reports show events and usage from the + last 30 minutes. + + Returns: + Callable[[~.RunRealtimeReportRequest], + Awaitable[~.RunRealtimeReportResponse]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "run_realtime_report" not in self._stubs: + self._stubs["run_realtime_report"] = self.grpc_channel.unary_unary( + "/google.analytics.data.v1alpha.AlphaAnalyticsData/RunRealtimeReport", + request_serializer=analytics_data_api.RunRealtimeReportRequest.serialize, + response_deserializer=analytics_data_api.RunRealtimeReportResponse.deserialize, + ) + return self._stubs["run_realtime_report"] + __all__ = ("AlphaAnalyticsDataGrpcAsyncIOTransport",) diff --git a/google/analytics/data_v1alpha/types/__init__.py b/google/analytics/data_v1alpha/types/__init__.py index 0433873..c6be9f3 100644 --- a/google/analytics/data_v1alpha/types/__init__.py +++ b/google/analytics/data_v1alpha/types/__init__.py @@ -55,6 +55,8 @@ BatchRunPivotReportsRequest, BatchRunPivotReportsResponse, GetMetadataRequest, + RunRealtimeReportRequest, + RunRealtimeReportResponse, ) @@ -96,4 +98,6 @@ "BatchRunPivotReportsRequest", "BatchRunPivotReportsResponse", "GetMetadataRequest", + "RunRealtimeReportRequest", + "RunRealtimeReportResponse", ) diff --git a/google/analytics/data_v1alpha/types/analytics_data_api.py b/google/analytics/data_v1alpha/types/analytics_data_api.py index b925cde..e23a091 100644 --- a/google/analytics/data_v1alpha/types/analytics_data_api.py +++ b/google/analytics/data_v1alpha/types/analytics_data_api.py @@ -34,6 +34,8 @@ "BatchRunPivotReportsRequest", "BatchRunPivotReportsResponse", "GetMetadataRequest", + "RunRealtimeReportRequest", + "RunRealtimeReportResponse", }, ) @@ -46,7 +48,7 @@ class Metadata(proto.Message): name (str): Resource name of this metadata. dimensions (Sequence[~.data.DimensionMetadata]): - The dimensions descriptions. + The dimension descriptions. metrics (Sequence[~.data.MetricMetadata]): The metric descriptions. """ @@ -81,12 +83,17 @@ class RunReportRequest(proto.Message): both date ranges. In a cohort request, this ``dateRanges`` must be unspecified. offset (int): - The row count of the start row. The first row - is counted as row 0. + The row count of the start row. The first row is counted as + row 0. + + To learn more about this pagination parameter, see + `Pagination `__. limit (int): - The number of rows to return. If unspecified, - 10 rows are returned. If -1, all rows are - returned. + The number of rows to return. If unspecified, 10 rows are + returned. If -1, all rows are returned. + + To learn more about this pagination parameter, see + `Pagination `__. metric_aggregations (Sequence[~.data.MetricAggregation]): Aggregation of metrics. Aggregated metric values will be shown in rows where the dimension_values are set to @@ -178,6 +185,15 @@ class RunReportResponse(proto.Message): If requested, the maximum values of metrics. minimums (Sequence[~.data.Row]): If requested, the minimum values of metrics. + row_count (int): + The total number of rows in the query result, regardless of + the number of rows returned in the response. For example if + a query returns 175 rows and includes limit = 50 in the API + request, the response will contain row_count = 175 but only + 50 rows. + + To learn more about this pagination parameter, see + `Pagination `__. metadata (~.data.ResponseMetaData): Metadata for the report. property_quota (~.data.PropertyQuota): @@ -201,6 +217,8 @@ class RunReportResponse(proto.Message): minimums = proto.RepeatedField(proto.MESSAGE, number=10, message=data.Row,) + row_count = proto.Field(proto.INT32, number=12) + metadata = proto.Field(proto.MESSAGE, number=6, message=data.ResponseMetaData,) property_quota = proto.Field(proto.MESSAGE, number=7, message=data.PropertyQuota,) @@ -402,7 +420,7 @@ class BatchRunReportsRequest(proto.Message): entity = proto.Field(proto.MESSAGE, number=1, message=data.Entity,) - requests = proto.RepeatedField(proto.MESSAGE, number=2, message=RunReportRequest,) + requests = proto.RepeatedField(proto.MESSAGE, number=2, message="RunReportRequest",) class BatchRunReportsResponse(proto.Message): @@ -414,7 +432,7 @@ class BatchRunReportsResponse(proto.Message): separate report request. """ - reports = proto.RepeatedField(proto.MESSAGE, number=1, message=RunReportResponse,) + reports = proto.RepeatedField(proto.MESSAGE, number=1, message="RunReportResponse",) class BatchRunPivotReportsRequest(proto.Message): @@ -435,7 +453,7 @@ class BatchRunPivotReportsRequest(proto.Message): entity = proto.Field(proto.MESSAGE, number=1, message=data.Entity,) requests = proto.RepeatedField( - proto.MESSAGE, number=2, message=RunPivotReportRequest, + proto.MESSAGE, number=2, message="RunPivotReportRequest", ) @@ -449,24 +467,150 @@ class BatchRunPivotReportsResponse(proto.Message): """ pivot_reports = proto.RepeatedField( - proto.MESSAGE, number=1, message=RunPivotReportResponse, + proto.MESSAGE, number=1, message="RunPivotReportResponse", ) class GetMetadataRequest(proto.Message): - r"""Request for dimension and metric metadata. + r"""Request for a property's dimension and metric metadata. Attributes: name (str): - Required. The name of the metadata to - retrieve. Either has the form 'metadata' or - 'properties/{property}/metadata'. This name - field is specified in the URL path and not URL - parameters. Property is a numeric Google - Analytics App + Web Property Id. + Required. The resource name of the metadata to retrieve. + This name field is specified in the URL path and not URL + parameters. Property is a numeric Google Analytics GA4 + Property identifier. To learn more, see `where to find your + Property + ID `__. + + Example: properties/1234/metadata + + Set the Property ID to 0 for dimensions and metrics common + to all properties. In this special mode, this method will + not return custom dimensions and metrics. """ name = proto.Field(proto.STRING, number=1) +class RunRealtimeReportRequest(proto.Message): + r"""The request to generate a realtime report. + + Attributes: + property (str): + A Google Analytics GA4 property identifier whose events are + tracked. Specified in the URL path and not the body. To + learn more, see `where to find your Property + ID `__. + + Example: properties/1234 + dimensions (Sequence[~.data.Dimension]): + The dimensions requested and displayed. + metrics (Sequence[~.data.Metric]): + The metrics requested and displayed. + limit (int): + The number of rows to return. If unspecified, + 10 rows are returned. If -1, all rows are + returned. + dimension_filter (~.data.FilterExpression): + The filter clause of dimensions. Dimensions + must be requested to be used in this filter. + Metrics cannot be used in this filter. + metric_filter (~.data.FilterExpression): + The filter clause of metrics. Applied at post + aggregation phase, similar to SQL having-clause. + Metrics must be requested to be used in this + filter. Dimensions cannot be used in this + filter. + metric_aggregations (Sequence[~.data.MetricAggregation]): + Aggregation of metrics. Aggregated metric values will be + shown in rows where the dimension_values are set to + "RESERVED_(MetricAggregation)". + order_bys (Sequence[~.data.OrderBy]): + Specifies how rows are ordered in the + response. + return_property_quota (bool): + Toggles whether to return the current state of this + Analytics Property's Realtime quota. Quota is returned in + `PropertyQuota <#PropertyQuota>`__. + """ + + property = proto.Field(proto.STRING, number=1) + + dimensions = proto.RepeatedField(proto.MESSAGE, number=2, message=data.Dimension,) + + metrics = proto.RepeatedField(proto.MESSAGE, number=3, message=data.Metric,) + + limit = proto.Field(proto.INT64, number=4) + + dimension_filter = proto.Field( + proto.MESSAGE, number=5, message=data.FilterExpression, + ) + + metric_filter = proto.Field(proto.MESSAGE, number=6, message=data.FilterExpression,) + + metric_aggregations = proto.RepeatedField( + proto.ENUM, number=7, enum=data.MetricAggregation, + ) + + order_bys = proto.RepeatedField(proto.MESSAGE, number=8, message=data.OrderBy,) + + return_property_quota = proto.Field(proto.BOOL, number=9) + + +class RunRealtimeReportResponse(proto.Message): + r"""The response realtime report table corresponding to a + request. + + Attributes: + dimension_headers (Sequence[~.data.DimensionHeader]): + Describes dimension columns. The number of + DimensionHeaders and ordering of + DimensionHeaders matches the dimensions present + in rows. + metric_headers (Sequence[~.data.MetricHeader]): + Describes metric columns. The number of + MetricHeaders and ordering of MetricHeaders + matches the metrics present in rows. + rows (Sequence[~.data.Row]): + Rows of dimension value combinations and + metric values in the report. + totals (Sequence[~.data.Row]): + If requested, the totaled values of metrics. + maximums (Sequence[~.data.Row]): + If requested, the maximum values of metrics. + minimums (Sequence[~.data.Row]): + If requested, the minimum values of metrics. + row_count (int): + The total number of rows in the query result, regardless of + the number of rows returned in the response. For example if + a query returns 175 rows and includes limit = 50 in the API + request, the response will contain row_count = 175 but only + 50 rows. + property_quota (~.data.PropertyQuota): + This Analytics Property's Realtime quota + state including this request. + """ + + dimension_headers = proto.RepeatedField( + proto.MESSAGE, number=1, message=data.DimensionHeader, + ) + + metric_headers = proto.RepeatedField( + proto.MESSAGE, number=2, message=data.MetricHeader, + ) + + rows = proto.RepeatedField(proto.MESSAGE, number=3, message=data.Row,) + + totals = proto.RepeatedField(proto.MESSAGE, number=4, message=data.Row,) + + maximums = proto.RepeatedField(proto.MESSAGE, number=5, message=data.Row,) + + minimums = proto.RepeatedField(proto.MESSAGE, number=6, message=data.Row,) + + row_count = proto.Field(proto.INT32, number=7) + + property_quota = proto.Field(proto.MESSAGE, number=8, message=data.PropertyQuota,) + + __all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/google/analytics/data_v1alpha/types/data.py b/google/analytics/data_v1alpha/types/data.py index 66fe3eb..24d2e77 100644 --- a/google/analytics/data_v1alpha/types/data.py +++ b/google/analytics/data_v1alpha/types/data.py @@ -69,13 +69,20 @@ class MetricType(proto.Enum): TYPE_INTEGER = 1 TYPE_FLOAT = 2 TYPE_SECONDS = 4 + TYPE_MILLISECONDS = 5 + TYPE_MINUTES = 6 + TYPE_HOURS = 7 + TYPE_STANDARD = 8 TYPE_CURRENCY = 9 + TYPE_FEET = 10 + TYPE_MILES = 11 + TYPE_METERS = 12 + TYPE_KILOMETERS = 13 class DateRange(proto.Message): r"""A contiguous set of days: startDate, startDate + 1, ..., - endDate. Requests are allowed up to 4 date ranges, and the union - of the ranges can cover up to 1 year. + endDate. Requests are allowed up to 4 date ranges. Attributes: start_date (str): @@ -111,7 +118,9 @@ class Entity(proto.Message): Attributes: property_id (str): - A Google Analytics App + Web property id. + A Google Analytics GA4 property id. To learn more, see + `where to find your Property + ID `__. """ property_id = proto.Field(proto.STRING, number=1) @@ -119,13 +128,25 @@ class Entity(proto.Message): class Dimension(proto.Message): r"""Dimensions are attributes of your data. For example, the - dimension City indicates the city, for example, "Paris" or "New - York", from which an event originates. Requests are allowed up - to 8 dimensions. + dimension city indicates the city from which an event + originates. Dimension values in report responses are strings; + for example, city could be "Paris" or "New York". Requests are + allowed up to 8 dimensions. Attributes: name (str): - The name of the dimension. + The name of the dimension. See the `API + Dimensions `__ + for the list of dimension names. + + If ``dimensionExpression`` is specified, ``name`` can be any + string that you would like. For example if a + ``dimensionExpression`` concatenates ``country`` and + ``city``, you could call that dimension ``countryAndCity``. + + Dimensions are referenced by ``name`` in + ``dimensionFilter``, ``orderBys``, ``dimensionExpression``, + and ``pivots``. dimension_expression (~.data.DimensionExpression): One dimension can be the result of an expression of multiple dimensions. For example, @@ -208,22 +229,32 @@ class ConcatenateExpression(proto.Message): class Metric(proto.Message): - r"""The quantitative measurements of a report. For example, the - metric eventCount is the total number of events. Requests are - allowed up to 10 metrics. + r"""The quantitative measurements of a report. For example, the metric + ``eventCount`` is the total number of events. Requests are allowed + up to 10 metrics. Attributes: name (str): - The name of the metric. + The name of the metric. See the `API + Metrics `__ + for the list of metric names. + + If ``expression`` is specified, ``name`` can be any string + that you would like. For example if ``expression`` is + ``screenPageViews/sessions``, you could call that metric's + name = ``viewsPerSession``. + + Metrics are referenced by ``name`` in ``metricFilter``, + ``orderBys``, and metric ``expression``. expression (str): - A mathematical expression for derived - metrics. For example, the metric Event count per - user is eventCount/totalUsers. + A mathematical expression for derived metrics. For example, + the metric Event count per user is + ``eventCount/totalUsers``. invisible (bool): - Indicates if a metric is invisible. If a metric is - invisible, the metric is not in the response, but can be - used in filters, order_bys or being referred to in a metric - expression. + Indicates if a metric is invisible in the report response. + If a metric is invisible, the metric will not produce a + column in the response, but can be used in ``metricFilter``, + ``orderBys``, or a metric ``expression``. """ name = proto.Field(proto.STRING, number=1) @@ -275,7 +306,7 @@ class FilterExpressionList(proto.Message): """ expressions = proto.RepeatedField( - proto.MESSAGE, number=1, message=FilterExpression, + proto.MESSAGE, number=1, message="FilterExpression", ) @@ -287,7 +318,12 @@ class Filter(proto.Message): The dimension name or metric name. Must be a name defined in dimensions or metrics. null_filter (bool): - A filter for null values. + A filter for null values. If True, a null + dimension value is matched by this filter. Null + filter is commonly used inside a NOT filter + expression. For example, a NOT expression of a + null filter removes rows when a dimension is + null. string_filter (~.data.Filter.StringFilter): Strings related filter. in_list_filter (~.data.Filter.InListFilter): @@ -555,7 +591,7 @@ class Pivot(proto.Message): field_names = proto.RepeatedField(proto.STRING, number=1) - order_bys = proto.RepeatedField(proto.MESSAGE, number=2, message=OrderBy,) + order_bys = proto.RepeatedField(proto.MESSAGE, number=2, message="OrderBy",) offset = proto.Field(proto.INT64, number=3) @@ -623,7 +659,7 @@ class Cohort(proto.Message): dimension = proto.Field(proto.STRING, number=2) - date_range = proto.Field(proto.MESSAGE, number=3, message=DateRange,) + date_range = proto.Field(proto.MESSAGE, number=3, message="DateRange",) class CohortReportSettings(proto.Message): @@ -708,13 +744,13 @@ class MetricHeader(proto.Message): Attributes: name (str): The metric's name. - type (~.data.MetricType): + type_ (~.data.MetricType): The metric's data type. """ name = proto.Field(proto.STRING, number=1) - type = proto.Field(proto.ENUM, number=2, enum="MetricType",) + type_ = proto.Field(proto.ENUM, number=2, enum="MetricType",) class PivotHeader(proto.Message): @@ -726,7 +762,10 @@ class PivotHeader(proto.Message): the corresponding dimension combinations. row_count (int): The cardinality of the pivot as if offset = 0 - and limit = -1. + and limit = -1. The total number of rows for + this pivot's fields regardless of how the + parameters offset and limit are specified in the + request. """ pivot_dimension_headers = proto.RepeatedField( @@ -754,28 +793,38 @@ class Row(proto.Message): .. code:: none - dimensions { - name: "eventName" - } - dimensions { - name: "countryId" - } - metrics { - name: "eventCount" - } - - One row with 'in_app_purchase' as the eventName, 'us' as the + "dimensions": [ + { + "name": "eventName" + }, + { + "name": "countryId" + } + ], + "metrics": [ + { + "name": "eventCount" + } + ] + + One row with 'in_app_purchase' as the eventName, 'JP' as the countryId, and 15 as the eventCount, would be: .. code:: none - dimension_values { - name: 'in_app_purchase' - name: 'us' - } - metric_values { - int64_value: 15 - } + "dimensionValues": [ + { + "value": "in_app_purchase" + }, + { + "value": "JP" + } + ], + "metricValues": [ + { + "value": "15" + } + ] Attributes: dimension_values (Sequence[~.data.DimensionValue]): @@ -838,20 +887,27 @@ class PropertyQuota(proto.Message): Attributes: tokens_per_day (~.data.QuotaStatus): - Analytics Properties can use up to 25,000 - tokens per day. Most requests consume fewer than - 10 tokens. + Standard Analytics Properties can use up to + 25,000 tokens per day; Analytics 360 Properties + can use 250,000 tokens per day. Most requests + consume fewer than 10 tokens. tokens_per_hour (~.data.QuotaStatus): - Analytics Properties can use up to 5,000 - tokens per day. An API request consumes a single - number of tokens, and that number is deducted - from both the hourly and daily quotas. + Standard Analytics Properties can use up to + 5,000 tokens per day; Analytics 360 Properties + can use 50,000 tokens per day. An API request + consumes a single number of tokens, and that + number is deducted from both the hourly and + daily quotas. concurrent_requests (~.data.QuotaStatus): - Analytics Properties can send up to 10 - concurrent requests. + Standard Analytics Properties can send up to + 10 concurrent requests; Analytics 360 Properties + can use up to 50 concurrent requests. server_errors_per_project_per_hour (~.data.QuotaStatus): - Analytics Properties and cloud project pairs - can have up to 10 server errors per hour. + Standard Analytics Properties and cloud + project pairs can have up to 10 server errors + per hour; Analytics 360 Properties and cloud + project pairs can have up to 50 server errors + per hour. """ tokens_per_day = proto.Field(proto.MESSAGE, number=1, message="QuotaStatus",) @@ -930,7 +986,7 @@ class MetricMetadata(proto.Message): one of ``deprecatedApiNames`` for a period of time. After the deprecation period, the metric will be available only by ``apiName``. - type (~.data.MetricType): + type_ (~.data.MetricType): The type of this metric. expression (str): The mathematical expression for this derived metric. Can be @@ -947,7 +1003,7 @@ class MetricMetadata(proto.Message): deprecated_api_names = proto.RepeatedField(proto.STRING, number=4) - type = proto.Field(proto.ENUM, number=5, enum="MetricType",) + type_ = proto.Field(proto.ENUM, number=5, enum="MetricType",) expression = proto.Field(proto.STRING, number=6) diff --git a/noxfile.py b/noxfile.py index 62b2de4..7bbf2c3 100644 --- a/noxfile.py +++ b/noxfile.py @@ -28,7 +28,7 @@ DEFAULT_PYTHON_VERSION = "3.8" SYSTEM_TEST_PYTHON_VERSIONS = ["3.8"] -UNIT_TEST_PYTHON_VERSIONS = ["3.6", "3.7", "3.8"] +UNIT_TEST_PYTHON_VERSIONS = ["3.6", "3.7", "3.8", "3.9"] @nox.session(python=DEFAULT_PYTHON_VERSION) @@ -72,7 +72,9 @@ def default(session): # Install all test dependencies, then install this package in-place. session.install("asyncmock", "pytest-asyncio") - session.install("mock", "pytest", "pytest-cov") + session.install( + "mock", "pytest", "pytest-cov", + ) session.install("-e", ".") # Run py.test against the unit tests. @@ -173,7 +175,9 @@ def docfx(session): """Build the docfx yaml files for this library.""" session.install("-e", ".") - session.install("sphinx", "alabaster", "recommonmark", "sphinx-docfx-yaml") + # sphinx-docfx-yaml supports up to sphinx version 1.5.5. + # https://github.com/docascode/sphinx-docfx-yaml/issues/97 + session.install("sphinx==1.5.5", "alabaster", "recommonmark", "sphinx-docfx-yaml") shutil.rmtree(os.path.join("docs", "_build"), ignore_errors=True) session.run( diff --git a/scripts/decrypt-secrets.sh b/scripts/decrypt-secrets.sh index ff599eb..21f6d2a 100755 --- a/scripts/decrypt-secrets.sh +++ b/scripts/decrypt-secrets.sh @@ -20,14 +20,27 @@ ROOT=$( dirname "$DIR" ) # Work from the project root. cd $ROOT +# Prevent it from overriding files. +# We recommend that sample authors use their own service account files and cloud project. +# In that case, they are supposed to prepare these files by themselves. +if [[ -f "testing/test-env.sh" ]] || \ + [[ -f "testing/service-account.json" ]] || \ + [[ -f "testing/client-secrets.json" ]]; then + echo "One or more target files exist, aborting." + exit 1 +fi + # Use SECRET_MANAGER_PROJECT if set, fallback to cloud-devrel-kokoro-resources. PROJECT_ID="${SECRET_MANAGER_PROJECT:-cloud-devrel-kokoro-resources}" gcloud secrets versions access latest --secret="python-docs-samples-test-env" \ + --project="${PROJECT_ID}" \ > testing/test-env.sh gcloud secrets versions access latest \ --secret="python-docs-samples-service-account" \ + --project="${PROJECT_ID}" \ > testing/service-account.json gcloud secrets versions access latest \ --secret="python-docs-samples-client-secrets" \ - > testing/client-secrets.json \ No newline at end of file + --project="${PROJECT_ID}" \ + > testing/client-secrets.json diff --git a/synth.metadata b/synth.metadata index 252eb97..6dd04c0 100644 --- a/synth.metadata +++ b/synth.metadata @@ -3,21 +3,30 @@ { "git": { "name": ".", - "remote": "sso://devrel/cloud/libraries/python/python-analytics-data" + "remote": "git@github.com:googleapis/python-analytics-data", + "sha": "18c45b829791509a8ae08a14089803718fa7b388" + } + }, + { + "git": { + "name": "googleapis", + "remote": "https://github.com/googleapis/googleapis.git", + "sha": "6a69c750c3f01a69017662395f90515bbf1fe1ff", + "internalRef": "342721036" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "b2c32f1c8a4094f0f47fcf5d10f0b6f2bfb3387d" + "sha": "d5fc0bcf9ea9789c5b0e3154a9e3b29e5cea6116" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "b2c32f1c8a4094f0f47fcf5d10f0b6f2bfb3387d" + "sha": "d5fc0bcf9ea9789c5b0e3154a9e3b29e5cea6116" } } ], diff --git a/tests/unit/gapic/data_v1alpha/test_alpha_analytics_data.py b/tests/unit/gapic/data_v1alpha/test_alpha_analytics_data.py index 0790b24..560ab56 100644 --- a/tests/unit/gapic/data_v1alpha/test_alpha_analytics_data.py +++ b/tests/unit/gapic/data_v1alpha/test_alpha_analytics_data.py @@ -99,12 +99,12 @@ def test_alpha_analytics_data_client_from_service_account_file(client_class): ) as factory: factory.return_value = creds client = client_class.from_service_account_file("dummy/file/path.json") - assert client._transport._credentials == creds + assert client.transport._credentials == creds client = client_class.from_service_account_json("dummy/file/path.json") - assert client._transport._credentials == creds + assert client.transport._credentials == creds - assert client._transport._host == "analyticsdata.googleapis.com:443" + assert client.transport._host == "analyticsdata.googleapis.com:443" def test_alpha_analytics_data_client_get_transport_class(): @@ -160,15 +160,14 @@ def test_alpha_analytics_data_client_client_options( credentials_file=None, host="squid.clam.whelk", scopes=None, - api_mtls_endpoint="squid.clam.whelk", - client_cert_source=None, + ssl_channel_credentials=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) - # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS is + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is # "never". - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "never"}): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}): with mock.patch.object(transport_class, "__init__") as patched: patched.return_value = None client = client_class() @@ -177,15 +176,14 @@ def test_alpha_analytics_data_client_client_options( credentials_file=None, host=client.DEFAULT_ENDPOINT, scopes=None, - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) - # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS is + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is # "always". - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "always"}): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}): with mock.patch.object(transport_class, "__init__") as patched: patched.return_value = None client = client_class() @@ -194,95 +192,185 @@ def test_alpha_analytics_data_client_client_options( credentials_file=None, host=client.DEFAULT_MTLS_ENDPOINT, scopes=None, - api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) - # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is - # "auto", and client_cert_source is provided. - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "auto"}): + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has + # unsupported value. + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}): + with pytest.raises(MutualTLSChannelError): + client = client_class() + + # Check the case GOOGLE_API_USE_CLIENT_CERTIFICATE has unsupported value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"} + ): + with pytest.raises(ValueError): + client = client_class() + + # Check the case quota_project_id is provided + options = client_options.ClientOptions(quota_project_id="octopus") + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + ssl_channel_credentials=None, + quota_project_id="octopus", + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name,use_client_cert_env", + [ + ( + AlphaAnalyticsDataClient, + transports.AlphaAnalyticsDataGrpcTransport, + "grpc", + "true", + ), + ( + AlphaAnalyticsDataAsyncClient, + transports.AlphaAnalyticsDataGrpcAsyncIOTransport, + "grpc_asyncio", + "true", + ), + ( + AlphaAnalyticsDataClient, + transports.AlphaAnalyticsDataGrpcTransport, + "grpc", + "false", + ), + ( + AlphaAnalyticsDataAsyncClient, + transports.AlphaAnalyticsDataGrpcAsyncIOTransport, + "grpc_asyncio", + "false", + ), + ], +) +@mock.patch.object( + AlphaAnalyticsDataClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(AlphaAnalyticsDataClient), +) +@mock.patch.object( + AlphaAnalyticsDataAsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(AlphaAnalyticsDataAsyncClient), +) +@mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) +def test_alpha_analytics_data_client_mtls_env_auto( + client_class, transport_class, transport_name, use_client_cert_env +): + # This tests the endpoint autoswitch behavior. Endpoint is autoswitched to the default + # mtls endpoint, if GOOGLE_API_USE_CLIENT_CERTIFICATE is "true" and client cert exists. + + # Check the case client_cert_source is provided. Whether client cert is used depends on + # GOOGLE_API_USE_CLIENT_CERTIFICATE value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): options = client_options.ClientOptions( client_cert_source=client_cert_source_callback ) with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class(client_options=options) - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client.DEFAULT_MTLS_ENDPOINT, - scopes=None, - api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, - client_cert_source=client_cert_source_callback, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - ) - - # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is - # "auto", and default_client_cert_source is provided. - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "auto"}): - with mock.patch.object(transport_class, "__init__") as patched: + ssl_channel_creds = mock.Mock() with mock.patch( - "google.auth.transport.mtls.has_default_client_cert_source", - return_value=True, + "grpc.ssl_channel_credentials", return_value=ssl_channel_creds ): patched.return_value = None - client = client_class() + client = client_class(client_options=options) + + if use_client_cert_env == "false": + expected_ssl_channel_creds = None + expected_host = client.DEFAULT_ENDPOINT + else: + expected_ssl_channel_creds = ssl_channel_creds + expected_host = client.DEFAULT_MTLS_ENDPOINT + patched.assert_called_once_with( credentials=None, credentials_file=None, - host=client.DEFAULT_MTLS_ENDPOINT, + host=expected_host, scopes=None, - api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=expected_ssl_channel_creds, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) - # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is - # "auto", but client_cert_source and default_client_cert_source are None. - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "auto"}): + # Check the case ADC client cert is provided. Whether client cert is used depends on + # GOOGLE_API_USE_CLIENT_CERTIFICATE value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): with mock.patch.object(transport_class, "__init__") as patched: with mock.patch( - "google.auth.transport.mtls.has_default_client_cert_source", - return_value=False, + "google.auth.transport.grpc.SslCredentials.__init__", return_value=None ): - patched.return_value = None - client = client_class() - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client.DEFAULT_ENDPOINT, - scopes=None, - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - ) - - # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS has - # unsupported value. - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "Unsupported"}): - with pytest.raises(MutualTLSChannelError): - client = client_class() - - # Check the case quota_project_id is provided - options = client_options.ClientOptions(quota_project_id="octopus") - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class(client_options=options) - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client.DEFAULT_ENDPOINT, - scopes=None, - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, - quota_project_id="octopus", - client_info=transports.base.DEFAULT_CLIENT_INFO, - ) + with mock.patch( + "google.auth.transport.grpc.SslCredentials.is_mtls", + new_callable=mock.PropertyMock, + ) as is_mtls_mock: + with mock.patch( + "google.auth.transport.grpc.SslCredentials.ssl_credentials", + new_callable=mock.PropertyMock, + ) as ssl_credentials_mock: + if use_client_cert_env == "false": + is_mtls_mock.return_value = False + ssl_credentials_mock.return_value = None + expected_host = client.DEFAULT_ENDPOINT + expected_ssl_channel_creds = None + else: + is_mtls_mock.return_value = True + ssl_credentials_mock.return_value = mock.Mock() + expected_host = client.DEFAULT_MTLS_ENDPOINT + expected_ssl_channel_creds = ( + ssl_credentials_mock.return_value + ) + + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=expected_host, + scopes=None, + ssl_channel_credentials=expected_ssl_channel_creds, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + # Check the case client_cert_source and ADC client cert are not provided. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): + with mock.patch.object(transport_class, "__init__") as patched: + with mock.patch( + "google.auth.transport.grpc.SslCredentials.__init__", return_value=None + ): + with mock.patch( + "google.auth.transport.grpc.SslCredentials.is_mtls", + new_callable=mock.PropertyMock, + ) as is_mtls_mock: + is_mtls_mock.return_value = False + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) @pytest.mark.parametrize( @@ -309,8 +397,7 @@ def test_alpha_analytics_data_client_client_options_scopes( credentials_file=None, host=client.DEFAULT_ENDPOINT, scopes=["1", "2"], - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -340,8 +427,7 @@ def test_alpha_analytics_data_client_client_options_credentials_file( credentials_file="credentials.json", host=client.DEFAULT_ENDPOINT, scopes=None, - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -360,8 +446,7 @@ def test_alpha_analytics_data_client_client_options_from_dict(): credentials_file=None, host="squid.clam.whelk", scopes=None, - api_mtls_endpoint="squid.clam.whelk", - client_cert_source=None, + ssl_channel_credentials=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -379,9 +464,9 @@ def test_run_report( request = request_type() # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client._transport.run_report), "__call__") as call: + with mock.patch.object(type(client.transport.run_report), "__call__") as call: # Designate an appropriate return value for the call. - call.return_value = analytics_data_api.RunReportResponse() + call.return_value = analytics_data_api.RunReportResponse(row_count=992,) response = client.run_report(request) @@ -392,30 +477,33 @@ def test_run_report( assert args[0] == analytics_data_api.RunReportRequest() # Establish that the response is the type that we expect. + assert isinstance(response, analytics_data_api.RunReportResponse) + assert response.row_count == 992 + def test_run_report_from_dict(): test_run_report(request_type=dict) @pytest.mark.asyncio -async def test_run_report_async(transport: str = "grpc_asyncio"): +async def test_run_report_async( + transport: str = "grpc_asyncio", request_type=analytics_data_api.RunReportRequest +): client = AlphaAnalyticsDataAsyncClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = analytics_data_api.RunReportRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client._client._transport.run_report), "__call__" - ) as call: + with mock.patch.object(type(client.transport.run_report), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - analytics_data_api.RunReportResponse() + analytics_data_api.RunReportResponse(row_count=992,) ) response = await client.run_report(request) @@ -424,11 +512,18 @@ async def test_run_report_async(transport: str = "grpc_asyncio"): assert len(call.mock_calls) _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == analytics_data_api.RunReportRequest() # Establish that the response is the type that we expect. assert isinstance(response, analytics_data_api.RunReportResponse) + assert response.row_count == 992 + + +@pytest.mark.asyncio +async def test_run_report_async_from_dict(): + await test_run_report_async(request_type=dict) + def test_run_pivot_report( transport: str = "grpc", request_type=analytics_data_api.RunPivotReportRequest @@ -442,9 +537,7 @@ def test_run_pivot_report( request = request_type() # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client._transport.run_pivot_report), "__call__" - ) as call: + with mock.patch.object(type(client.transport.run_pivot_report), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = analytics_data_api.RunPivotReportResponse() @@ -457,6 +550,7 @@ def test_run_pivot_report( assert args[0] == analytics_data_api.RunPivotReportRequest() # Establish that the response is the type that we expect. + assert isinstance(response, analytics_data_api.RunPivotReportResponse) @@ -465,19 +559,20 @@ def test_run_pivot_report_from_dict(): @pytest.mark.asyncio -async def test_run_pivot_report_async(transport: str = "grpc_asyncio"): +async def test_run_pivot_report_async( + transport: str = "grpc_asyncio", + request_type=analytics_data_api.RunPivotReportRequest, +): client = AlphaAnalyticsDataAsyncClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = analytics_data_api.RunPivotReportRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client._client._transport.run_pivot_report), "__call__" - ) as call: + with mock.patch.object(type(client.transport.run_pivot_report), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( analytics_data_api.RunPivotReportResponse() @@ -489,12 +584,17 @@ async def test_run_pivot_report_async(transport: str = "grpc_asyncio"): assert len(call.mock_calls) _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == analytics_data_api.RunPivotReportRequest() # Establish that the response is the type that we expect. assert isinstance(response, analytics_data_api.RunPivotReportResponse) +@pytest.mark.asyncio +async def test_run_pivot_report_async_from_dict(): + await test_run_pivot_report_async(request_type=dict) + + def test_batch_run_reports( transport: str = "grpc", request_type=analytics_data_api.BatchRunReportsRequest ): @@ -508,7 +608,7 @@ def test_batch_run_reports( # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._transport.batch_run_reports), "__call__" + type(client.transport.batch_run_reports), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = analytics_data_api.BatchRunReportsResponse() @@ -522,6 +622,7 @@ def test_batch_run_reports( assert args[0] == analytics_data_api.BatchRunReportsRequest() # Establish that the response is the type that we expect. + assert isinstance(response, analytics_data_api.BatchRunReportsResponse) @@ -530,18 +631,21 @@ def test_batch_run_reports_from_dict(): @pytest.mark.asyncio -async def test_batch_run_reports_async(transport: str = "grpc_asyncio"): +async def test_batch_run_reports_async( + transport: str = "grpc_asyncio", + request_type=analytics_data_api.BatchRunReportsRequest, +): client = AlphaAnalyticsDataAsyncClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = analytics_data_api.BatchRunReportsRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._client._transport.batch_run_reports), "__call__" + type(client.transport.batch_run_reports), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( @@ -554,12 +658,17 @@ async def test_batch_run_reports_async(transport: str = "grpc_asyncio"): assert len(call.mock_calls) _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == analytics_data_api.BatchRunReportsRequest() # Establish that the response is the type that we expect. assert isinstance(response, analytics_data_api.BatchRunReportsResponse) +@pytest.mark.asyncio +async def test_batch_run_reports_async_from_dict(): + await test_batch_run_reports_async(request_type=dict) + + def test_batch_run_pivot_reports( transport: str = "grpc", request_type=analytics_data_api.BatchRunPivotReportsRequest ): @@ -573,7 +682,7 @@ def test_batch_run_pivot_reports( # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._transport.batch_run_pivot_reports), "__call__" + type(client.transport.batch_run_pivot_reports), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = analytics_data_api.BatchRunPivotReportsResponse() @@ -587,6 +696,7 @@ def test_batch_run_pivot_reports( assert args[0] == analytics_data_api.BatchRunPivotReportsRequest() # Establish that the response is the type that we expect. + assert isinstance(response, analytics_data_api.BatchRunPivotReportsResponse) @@ -595,18 +705,21 @@ def test_batch_run_pivot_reports_from_dict(): @pytest.mark.asyncio -async def test_batch_run_pivot_reports_async(transport: str = "grpc_asyncio"): +async def test_batch_run_pivot_reports_async( + transport: str = "grpc_asyncio", + request_type=analytics_data_api.BatchRunPivotReportsRequest, +): client = AlphaAnalyticsDataAsyncClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = analytics_data_api.BatchRunPivotReportsRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._client._transport.batch_run_pivot_reports), "__call__" + type(client.transport.batch_run_pivot_reports), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( @@ -619,12 +732,17 @@ async def test_batch_run_pivot_reports_async(transport: str = "grpc_asyncio"): assert len(call.mock_calls) _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == analytics_data_api.BatchRunPivotReportsRequest() # Establish that the response is the type that we expect. assert isinstance(response, analytics_data_api.BatchRunPivotReportsResponse) +@pytest.mark.asyncio +async def test_batch_run_pivot_reports_async_from_dict(): + await test_batch_run_pivot_reports_async(request_type=dict) + + def test_get_metadata( transport: str = "grpc", request_type=analytics_data_api.GetMetadataRequest ): @@ -637,7 +755,7 @@ def test_get_metadata( request = request_type() # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client._transport.get_metadata), "__call__") as call: + with mock.patch.object(type(client.transport.get_metadata), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = analytics_data_api.Metadata(name="name_value",) @@ -650,6 +768,7 @@ def test_get_metadata( assert args[0] == analytics_data_api.GetMetadataRequest() # Establish that the response is the type that we expect. + assert isinstance(response, analytics_data_api.Metadata) assert response.name == "name_value" @@ -660,19 +779,19 @@ def test_get_metadata_from_dict(): @pytest.mark.asyncio -async def test_get_metadata_async(transport: str = "grpc_asyncio"): +async def test_get_metadata_async( + transport: str = "grpc_asyncio", request_type=analytics_data_api.GetMetadataRequest +): client = AlphaAnalyticsDataAsyncClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = analytics_data_api.GetMetadataRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client._client._transport.get_metadata), "__call__" - ) as call: + with mock.patch.object(type(client.transport.get_metadata), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( analytics_data_api.Metadata(name="name_value",) @@ -684,7 +803,7 @@ async def test_get_metadata_async(transport: str = "grpc_asyncio"): assert len(call.mock_calls) _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == analytics_data_api.GetMetadataRequest() # Establish that the response is the type that we expect. assert isinstance(response, analytics_data_api.Metadata) @@ -692,6 +811,11 @@ async def test_get_metadata_async(transport: str = "grpc_asyncio"): assert response.name == "name_value" +@pytest.mark.asyncio +async def test_get_metadata_async_from_dict(): + await test_get_metadata_async(request_type=dict) + + def test_get_metadata_field_headers(): client = AlphaAnalyticsDataClient(credentials=credentials.AnonymousCredentials(),) @@ -701,7 +825,7 @@ def test_get_metadata_field_headers(): request.name = "name/value" # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client._transport.get_metadata), "__call__") as call: + with mock.patch.object(type(client.transport.get_metadata), "__call__") as call: call.return_value = analytics_data_api.Metadata() client.get_metadata(request) @@ -728,9 +852,7 @@ async def test_get_metadata_field_headers_async(): request.name = "name/value" # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client._client._transport.get_metadata), "__call__" - ) as call: + with mock.patch.object(type(client.transport.get_metadata), "__call__") as call: call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( analytics_data_api.Metadata() ) @@ -751,7 +873,7 @@ def test_get_metadata_flattened(): client = AlphaAnalyticsDataClient(credentials=credentials.AnonymousCredentials(),) # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client._transport.get_metadata), "__call__") as call: + with mock.patch.object(type(client.transport.get_metadata), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = analytics_data_api.Metadata() @@ -785,9 +907,7 @@ async def test_get_metadata_flattened_async(): ) # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client._client._transport.get_metadata), "__call__" - ) as call: + with mock.patch.object(type(client.transport.get_metadata), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = analytics_data_api.Metadata() @@ -820,6 +940,141 @@ async def test_get_metadata_flattened_error_async(): ) +def test_run_realtime_report( + transport: str = "grpc", request_type=analytics_data_api.RunRealtimeReportRequest +): + client = AlphaAnalyticsDataClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.run_realtime_report), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = analytics_data_api.RunRealtimeReportResponse(row_count=992,) + + response = client.run_realtime_report(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + + assert args[0] == analytics_data_api.RunRealtimeReportRequest() + + # Establish that the response is the type that we expect. + + assert isinstance(response, analytics_data_api.RunRealtimeReportResponse) + + assert response.row_count == 992 + + +def test_run_realtime_report_from_dict(): + test_run_realtime_report(request_type=dict) + + +@pytest.mark.asyncio +async def test_run_realtime_report_async( + transport: str = "grpc_asyncio", + request_type=analytics_data_api.RunRealtimeReportRequest, +): + client = AlphaAnalyticsDataAsyncClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.run_realtime_report), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + analytics_data_api.RunRealtimeReportResponse(row_count=992,) + ) + + response = await client.run_realtime_report(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + + assert args[0] == analytics_data_api.RunRealtimeReportRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, analytics_data_api.RunRealtimeReportResponse) + + assert response.row_count == 992 + + +@pytest.mark.asyncio +async def test_run_realtime_report_async_from_dict(): + await test_run_realtime_report_async(request_type=dict) + + +def test_run_realtime_report_field_headers(): + client = AlphaAnalyticsDataClient(credentials=credentials.AnonymousCredentials(),) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = analytics_data_api.RunRealtimeReportRequest() + request.property = "property/value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.run_realtime_report), "__call__" + ) as call: + call.return_value = analytics_data_api.RunRealtimeReportResponse() + + client.run_realtime_report(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ("x-goog-request-params", "property=property/value",) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_run_realtime_report_field_headers_async(): + client = AlphaAnalyticsDataAsyncClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = analytics_data_api.RunRealtimeReportRequest() + request.property = "property/value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.run_realtime_report), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + analytics_data_api.RunRealtimeReportResponse() + ) + + await client.run_realtime_report(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ("x-goog-request-params", "property=property/value",) in kw["metadata"] + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.AlphaAnalyticsDataGrpcTransport( @@ -856,7 +1111,7 @@ def test_transport_instance(): credentials=credentials.AnonymousCredentials(), ) client = AlphaAnalyticsDataClient(transport=transport) - assert client._transport is transport + assert client.transport is transport def test_transport_get_channel(): @@ -874,10 +1129,25 @@ def test_transport_get_channel(): assert channel +@pytest.mark.parametrize( + "transport_class", + [ + transports.AlphaAnalyticsDataGrpcTransport, + transports.AlphaAnalyticsDataGrpcAsyncIOTransport, + ], +) +def test_transport_adc(transport_class): + # Test default credentials are used if not provided. + with mock.patch.object(auth, "default") as adc: + adc.return_value = (credentials.AnonymousCredentials(), None) + transport_class() + adc.assert_called_once() + + def test_transport_grpc_default(): # A client should use the gRPC transport by default. client = AlphaAnalyticsDataClient(credentials=credentials.AnonymousCredentials(),) - assert isinstance(client._transport, transports.AlphaAnalyticsDataGrpcTransport,) + assert isinstance(client.transport, transports.AlphaAnalyticsDataGrpcTransport,) def test_alpha_analytics_data_base_transport_error(): @@ -907,6 +1177,7 @@ def test_alpha_analytics_data_base_transport(): "batch_run_reports", "batch_run_pivot_reports", "get_metadata", + "run_realtime_report", ) for method in methods: with pytest.raises(NotImplementedError): @@ -935,6 +1206,17 @@ def test_alpha_analytics_data_base_transport_with_credentials_file(): ) +def test_alpha_analytics_data_base_transport_with_adc(): + # Test the default credentials are used if credentials and credentials_file are None. + with mock.patch.object(auth, "default") as adc, mock.patch( + "google.analytics.data_v1alpha.services.alpha_analytics_data.transports.AlphaAnalyticsDataTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + adc.return_value = (credentials.AnonymousCredentials(), None) + transport = transports.AlphaAnalyticsDataTransport() + adc.assert_called_once() + + def test_alpha_analytics_data_auth_adc(): # If no credentials are provided, we should use ADC credentials. with mock.patch.object(auth, "default") as adc: @@ -973,7 +1255,7 @@ def test_alpha_analytics_data_host_no_port(): api_endpoint="analyticsdata.googleapis.com" ), ) - assert client._transport._host == "analyticsdata.googleapis.com:443" + assert client.transport._host == "analyticsdata.googleapis.com:443" def test_alpha_analytics_data_host_with_port(): @@ -983,197 +1265,245 @@ def test_alpha_analytics_data_host_with_port(): api_endpoint="analyticsdata.googleapis.com:8000" ), ) - assert client._transport._host == "analyticsdata.googleapis.com:8000" + assert client.transport._host == "analyticsdata.googleapis.com:8000" def test_alpha_analytics_data_grpc_transport_channel(): channel = grpc.insecure_channel("http://localhost/") - # Check that if channel is provided, mtls endpoint and client_cert_source - # won't be used. - callback = mock.MagicMock() + # Check that channel is used if provided. transport = transports.AlphaAnalyticsDataGrpcTransport( - host="squid.clam.whelk", - channel=channel, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=callback, + host="squid.clam.whelk", channel=channel, ) assert transport.grpc_channel == channel assert transport._host == "squid.clam.whelk:443" - assert not callback.called + assert transport._ssl_channel_credentials == None def test_alpha_analytics_data_grpc_asyncio_transport_channel(): channel = aio.insecure_channel("http://localhost/") - # Check that if channel is provided, mtls endpoint and client_cert_source - # won't be used. - callback = mock.MagicMock() + # Check that channel is used if provided. transport = transports.AlphaAnalyticsDataGrpcAsyncIOTransport( - host="squid.clam.whelk", - channel=channel, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=callback, + host="squid.clam.whelk", channel=channel, ) assert transport.grpc_channel == channel assert transport._host == "squid.clam.whelk:443" - assert not callback.called + assert transport._ssl_channel_credentials == None -@mock.patch("grpc.ssl_channel_credentials", autospec=True) -@mock.patch("google.api_core.grpc_helpers.create_channel", autospec=True) -def test_alpha_analytics_data_grpc_transport_channel_mtls_with_client_cert_source( - grpc_create_channel, grpc_ssl_channel_cred +@pytest.mark.parametrize( + "transport_class", + [ + transports.AlphaAnalyticsDataGrpcTransport, + transports.AlphaAnalyticsDataGrpcAsyncIOTransport, + ], +) +def test_alpha_analytics_data_transport_channel_mtls_with_client_cert_source( + transport_class, ): - # Check that if channel is None, but api_mtls_endpoint and client_cert_source - # are provided, then a mTLS channel will be created. - mock_cred = mock.Mock() + with mock.patch( + "grpc.ssl_channel_credentials", autospec=True + ) as grpc_ssl_channel_cred: + with mock.patch.object( + transport_class, "create_channel", autospec=True + ) as grpc_create_channel: + mock_ssl_cred = mock.Mock() + grpc_ssl_channel_cred.return_value = mock_ssl_cred + + mock_grpc_channel = mock.Mock() + grpc_create_channel.return_value = mock_grpc_channel + + cred = credentials.AnonymousCredentials() + with pytest.warns(DeprecationWarning): + with mock.patch.object(auth, "default") as adc: + adc.return_value = (cred, None) + transport = transport_class( + host="squid.clam.whelk", + api_mtls_endpoint="mtls.squid.clam.whelk", + client_cert_source=client_cert_source_callback, + ) + adc.assert_called_once() + + grpc_ssl_channel_cred.assert_called_once_with( + certificate_chain=b"cert bytes", private_key=b"key bytes" + ) + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=( + "https://www.googleapis.com/auth/analytics", + "https://www.googleapis.com/auth/analytics.readonly", + ), + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + ) + assert transport.grpc_channel == mock_grpc_channel + assert transport._ssl_channel_credentials == mock_ssl_cred + +@pytest.mark.parametrize( + "transport_class", + [ + transports.AlphaAnalyticsDataGrpcTransport, + transports.AlphaAnalyticsDataGrpcAsyncIOTransport, + ], +) +def test_alpha_analytics_data_transport_channel_mtls_with_adc(transport_class): mock_ssl_cred = mock.Mock() - grpc_ssl_channel_cred.return_value = mock_ssl_cred + with mock.patch.multiple( + "google.auth.transport.grpc.SslCredentials", + __init__=mock.Mock(return_value=None), + ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), + ): + with mock.patch.object( + transport_class, "create_channel", autospec=True + ) as grpc_create_channel: + mock_grpc_channel = mock.Mock() + grpc_create_channel.return_value = mock_grpc_channel + mock_cred = mock.Mock() + + with pytest.warns(DeprecationWarning): + transport = transport_class( + host="squid.clam.whelk", + credentials=mock_cred, + api_mtls_endpoint="mtls.squid.clam.whelk", + client_cert_source=None, + ) - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=mock_cred, + credentials_file=None, + scopes=( + "https://www.googleapis.com/auth/analytics", + "https://www.googleapis.com/auth/analytics.readonly", + ), + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + ) + assert transport.grpc_channel == mock_grpc_channel - transport = transports.AlphaAnalyticsDataGrpcTransport( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=client_cert_source_callback, - ) - grpc_ssl_channel_cred.assert_called_once_with( - certificate_chain=b"cert bytes", private_key=b"key bytes" - ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=( - "https://www.googleapis.com/auth/analytics", - "https://www.googleapis.com/auth/analytics.readonly", - ), - ssl_credentials=mock_ssl_cred, - quota_project_id=None, - ) - assert transport.grpc_channel == mock_grpc_channel +def test_metadata_path(): + property = "squid" -@mock.patch("grpc.ssl_channel_credentials", autospec=True) -@mock.patch("google.api_core.grpc_helpers_async.create_channel", autospec=True) -def test_alpha_analytics_data_grpc_asyncio_transport_channel_mtls_with_client_cert_source( - grpc_create_channel, grpc_ssl_channel_cred -): - # Check that if channel is None, but api_mtls_endpoint and client_cert_source - # are provided, then a mTLS channel will be created. - mock_cred = mock.Mock() + expected = "properties/{property}/metadata".format(property=property,) + actual = AlphaAnalyticsDataClient.metadata_path(property) + assert expected == actual - mock_ssl_cred = mock.Mock() - grpc_ssl_channel_cred.return_value = mock_ssl_cred - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel +def test_parse_metadata_path(): + expected = { + "property": "clam", + } + path = AlphaAnalyticsDataClient.metadata_path(**expected) - transport = transports.AlphaAnalyticsDataGrpcAsyncIOTransport( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=client_cert_source_callback, - ) - grpc_ssl_channel_cred.assert_called_once_with( - certificate_chain=b"cert bytes", private_key=b"key bytes" - ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=( - "https://www.googleapis.com/auth/analytics", - "https://www.googleapis.com/auth/analytics.readonly", - ), - ssl_credentials=mock_ssl_cred, - quota_project_id=None, + # Check that the path construction is reversible. + actual = AlphaAnalyticsDataClient.parse_metadata_path(path) + assert expected == actual + + +def test_common_billing_account_path(): + billing_account = "whelk" + + expected = "billingAccounts/{billing_account}".format( + billing_account=billing_account, ) - assert transport.grpc_channel == mock_grpc_channel + actual = AlphaAnalyticsDataClient.common_billing_account_path(billing_account) + assert expected == actual -@pytest.mark.parametrize( - "api_mtls_endpoint", ["mtls.squid.clam.whelk", "mtls.squid.clam.whelk:443"] -) -@mock.patch("google.api_core.grpc_helpers.create_channel", autospec=True) -def test_alpha_analytics_data_grpc_transport_channel_mtls_with_adc( - grpc_create_channel, api_mtls_endpoint -): - # Check that if channel and client_cert_source are None, but api_mtls_endpoint - # is provided, then a mTLS channel will be created with SSL ADC. - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel +def test_parse_common_billing_account_path(): + expected = { + "billing_account": "octopus", + } + path = AlphaAnalyticsDataClient.common_billing_account_path(**expected) - # Mock google.auth.transport.grpc.SslCredentials class. - mock_ssl_cred = mock.Mock() - with mock.patch.multiple( - "google.auth.transport.grpc.SslCredentials", - __init__=mock.Mock(return_value=None), - ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), - ): - mock_cred = mock.Mock() - transport = transports.AlphaAnalyticsDataGrpcTransport( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint=api_mtls_endpoint, - client_cert_source=None, - ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=( - "https://www.googleapis.com/auth/analytics", - "https://www.googleapis.com/auth/analytics.readonly", - ), - ssl_credentials=mock_ssl_cred, - quota_project_id=None, - ) - assert transport.grpc_channel == mock_grpc_channel + # Check that the path construction is reversible. + actual = AlphaAnalyticsDataClient.parse_common_billing_account_path(path) + assert expected == actual -@pytest.mark.parametrize( - "api_mtls_endpoint", ["mtls.squid.clam.whelk", "mtls.squid.clam.whelk:443"] -) -@mock.patch("google.api_core.grpc_helpers_async.create_channel", autospec=True) -def test_alpha_analytics_data_grpc_asyncio_transport_channel_mtls_with_adc( - grpc_create_channel, api_mtls_endpoint -): - # Check that if channel and client_cert_source are None, but api_mtls_endpoint - # is provided, then a mTLS channel will be created with SSL ADC. - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel +def test_common_folder_path(): + folder = "oyster" - # Mock google.auth.transport.grpc.SslCredentials class. - mock_ssl_cred = mock.Mock() - with mock.patch.multiple( - "google.auth.transport.grpc.SslCredentials", - __init__=mock.Mock(return_value=None), - ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), - ): - mock_cred = mock.Mock() - transport = transports.AlphaAnalyticsDataGrpcAsyncIOTransport( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint=api_mtls_endpoint, - client_cert_source=None, - ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=( - "https://www.googleapis.com/auth/analytics", - "https://www.googleapis.com/auth/analytics.readonly", - ), - ssl_credentials=mock_ssl_cred, - quota_project_id=None, - ) - assert transport.grpc_channel == mock_grpc_channel + expected = "folders/{folder}".format(folder=folder,) + actual = AlphaAnalyticsDataClient.common_folder_path(folder) + assert expected == actual + + +def test_parse_common_folder_path(): + expected = { + "folder": "nudibranch", + } + path = AlphaAnalyticsDataClient.common_folder_path(**expected) + + # Check that the path construction is reversible. + actual = AlphaAnalyticsDataClient.parse_common_folder_path(path) + assert expected == actual + + +def test_common_organization_path(): + organization = "cuttlefish" + + expected = "organizations/{organization}".format(organization=organization,) + actual = AlphaAnalyticsDataClient.common_organization_path(organization) + assert expected == actual + + +def test_parse_common_organization_path(): + expected = { + "organization": "mussel", + } + path = AlphaAnalyticsDataClient.common_organization_path(**expected) + + # Check that the path construction is reversible. + actual = AlphaAnalyticsDataClient.parse_common_organization_path(path) + assert expected == actual + + +def test_common_project_path(): + project = "winkle" + + expected = "projects/{project}".format(project=project,) + actual = AlphaAnalyticsDataClient.common_project_path(project) + assert expected == actual + + +def test_parse_common_project_path(): + expected = { + "project": "nautilus", + } + path = AlphaAnalyticsDataClient.common_project_path(**expected) + + # Check that the path construction is reversible. + actual = AlphaAnalyticsDataClient.parse_common_project_path(path) + assert expected == actual + + +def test_common_location_path(): + project = "scallop" + location = "abalone" + + expected = "projects/{project}/locations/{location}".format( + project=project, location=location, + ) + actual = AlphaAnalyticsDataClient.common_location_path(project, location) + assert expected == actual + + +def test_parse_common_location_path(): + expected = { + "project": "squid", + "location": "clam", + } + path = AlphaAnalyticsDataClient.common_location_path(**expected) + + # Check that the path construction is reversible. + actual = AlphaAnalyticsDataClient.parse_common_location_path(path) + assert expected == actual def test_client_withDEFAULT_CLIENT_INFO():