feat: gapic centralization into api core#17628
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a new client_helpers module in google-api-core containing helper functions for GAPIC client initialization, and adds global lazy imports control (__lazy_modules__) to google-cloud-compute. The review feedback suggests optimizing performance by compiling the mTLS endpoint regular expression at the module level rather than inside the function. Additionally, it recommends correcting the type annotations in get_api_endpoint to use Optional[Any] for client_cert_source and Optional[str] for the return type.
…rs.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
5e1eb20 to
ad1c01f
Compare
|
/gemini |
…al client_helpers methods
c27695d to
2353e82
Compare
1d7b5c2 to
3f02e35
Compare
3f02e35 to
3c40eed
Compare
Splits the monolithic client_helpers.py into routing.py, client_cert.py, config_helpers.py, and method_helpers.py to improve readability and maintainability. Updates gapic_v1 exports and test imports accordingly.
3c40eed to
7208833
Compare
| from google.api_core.gapic_v1 import ( | ||
| _client_cert, | ||
| _config_helpers, | ||
| _method_helpers, | ||
| _routing, |
There was a problem hiding this comment.
Exporting private module names ("_client_cert", "_config_helpers", "_method_helpers", "_routing") in __all__ is unconventional for top-level public API design.
| use_client_cert = use_client_cert_effective() | ||
| use_mtls_endpoint = os.getenv( | ||
| "GOOGLE_API_USE_MTLS_ENDPOINT", "auto" | ||
| ).lower() # noqa: E501 |
There was a problem hiding this comment.
nit: can we remove # noqa: E501 from here and other places?
| try: | ||
| import grpc # noqa: F401 | ||
| except ImportError: | ||
| pytest.skip("No GRPC", allow_module_level=True) |
There was a problem hiding this comment.
why are we skipping tests if grpc is not installed? Don't these functions work for REST too?
| # Backward compatibility aliases for private methods | ||
| # Previously, gapic-generator-python generated clients used these methods | ||
| _use_client_cert_effective = use_client_cert_effective | ||
| _get_client_cert_source = get_client_cert_source |
There was a problem hiding this comment.
Can you explain or provide the use case of these lines? How does they help?
|
|
||
|
|
||
| def get_client_cert_source( | ||
| provided_cert_source: Optional[Any], use_cert_flag: bool |
There was a problem hiding this comment.
iss this docstring correct? Can we confirm against google-auth?
|
Could we open a draft PR for gapic-generator with the template updates, temporarily pointing its test google-api-core dependency to this git branch? Running the gapic-generator-python CI (showcase and unit tests) on that draft PR will give us end-to-end verification that this branch integrates cleanly with generated code before we merge. |
Update and Release google-api-core
This PR introduces centralized helper functions into
google.api_core.gapic_v1. The goal of this centralization is to reduce package size, improve maintainability, and boost import performance of generated GAPIC clients by moving redundant, service-agnostic boilerplate logic intogoogle-api-core.Key Changes
google.api_core.gapic_v1:_routing.py(get_default_mtls_endpoint,get_api_endpoint,get_universe_domain)_client_cert.py(use_client_cert_effective,get_client_cert_source)_config_helpers.py(read_environment_variables)_method_helpers.py(setup_request_id)_prefix from these functions so they can be securely imported and utilized by external generated clients._use_client_cert_effective = use_client_cert_effective) to prevent breaking changes for existing GAPIC clients (over 2,400+ repos) that have not yet been regenerated.test_routing.py,test_client_cert.py,test_config_helpers.py,test_method_helpers.py).install_grpc=Falseenvironments.google-auth(e.g.<=2.14.1) pass in the CI test matrix by safely validatingshould_use_client_certavailability.Next Steps (Phase 2)
Once this PR is merged and a new minor version of
google-api-core(e.g.2.18.0) is released, we can update the GAPIC generator to utilize these centralized helpers and stop emitting the redundant logic.Note
Full details and baseline metrics are available in the attached Design Document.