diff --git a/packages/google-cloud-apihub/google/cloud/apihub/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub/__init__.py index f0e503b0afe4..674c0ff9e0a9 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub/__init__.py @@ -18,21 +18,49 @@ __version__ = package_version.__version__ +from google.cloud.apihub_v1.services.api_hub.async_client import ApiHubAsyncClient from google.cloud.apihub_v1.services.api_hub.client import ApiHubClient +from google.cloud.apihub_v1.services.api_hub_collect.async_client import ( + ApiHubCollectAsyncClient, +) from google.cloud.apihub_v1.services.api_hub_collect.client import ApiHubCollectClient +from google.cloud.apihub_v1.services.api_hub_curate.async_client import ( + ApiHubCurateAsyncClient, +) from google.cloud.apihub_v1.services.api_hub_curate.client import ApiHubCurateClient +from google.cloud.apihub_v1.services.api_hub_dependencies.async_client import ( + ApiHubDependenciesAsyncClient, +) from google.cloud.apihub_v1.services.api_hub_dependencies.client import ( ApiHubDependenciesClient, ) +from google.cloud.apihub_v1.services.api_hub_discovery.async_client import ( + ApiHubDiscoveryAsyncClient, +) from google.cloud.apihub_v1.services.api_hub_discovery.client import ( ApiHubDiscoveryClient, ) +from google.cloud.apihub_v1.services.api_hub_plugin.async_client import ( + ApiHubPluginAsyncClient, +) from google.cloud.apihub_v1.services.api_hub_plugin.client import ApiHubPluginClient +from google.cloud.apihub_v1.services.host_project_registration_service.async_client import ( + HostProjectRegistrationServiceAsyncClient, +) from google.cloud.apihub_v1.services.host_project_registration_service.client import ( HostProjectRegistrationServiceClient, ) +from google.cloud.apihub_v1.services.linting_service.async_client import ( + LintingServiceAsyncClient, +) from google.cloud.apihub_v1.services.linting_service.client import LintingServiceClient +from google.cloud.apihub_v1.services.provisioning.async_client import ( + ProvisioningAsyncClient, +) from google.cloud.apihub_v1.services.provisioning.client import ProvisioningClient +from google.cloud.apihub_v1.services.runtime_project_attachment_service.async_client import ( + RuntimeProjectAttachmentServiceAsyncClient, +) from google.cloud.apihub_v1.services.runtime_project_attachment_service.client import ( RuntimeProjectAttachmentServiceClient, ) @@ -232,15 +260,25 @@ __all__ = ( "ApiHubClient", + "ApiHubAsyncClient", "ApiHubCollectClient", + "ApiHubCollectAsyncClient", "ApiHubCurateClient", + "ApiHubCurateAsyncClient", "ApiHubDependenciesClient", + "ApiHubDependenciesAsyncClient", "ApiHubDiscoveryClient", + "ApiHubDiscoveryAsyncClient", "ApiHubPluginClient", + "ApiHubPluginAsyncClient", "HostProjectRegistrationServiceClient", + "HostProjectRegistrationServiceAsyncClient", "LintingServiceClient", + "LintingServiceAsyncClient", "ProvisioningClient", + "ProvisioningAsyncClient", "RuntimeProjectAttachmentServiceClient", + "RuntimeProjectAttachmentServiceAsyncClient", "ApiHubResource", "CreateApiOperationRequest", "CreateApiRequest", diff --git a/packages/google-cloud-apihub/google/cloud/apihub/gapic_version.py b/packages/google-cloud-apihub/google/cloud/apihub/gapic_version.py index 5e9f73f4e3f5..20a9cd975b02 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub/gapic_version.py +++ b/packages/google-cloud-apihub/google/cloud/apihub/gapic_version.py @@ -13,4 +13,4 @@ # See the License for the specific language governing permissions and # limitations under the License. # -__version__ = "0.2.7" # {x-release-please-version} +__version__ = "0.0.0" # {x-release-please-version} diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/__init__.py index 89c41c52fd1f..9dd916132a23 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/__init__.py @@ -18,18 +18,26 @@ __version__ = package_version.__version__ -from .services.api_hub import ApiHubClient -from .services.api_hub_collect import ApiHubCollectClient -from .services.api_hub_curate import ApiHubCurateClient -from .services.api_hub_dependencies import ApiHubDependenciesClient -from .services.api_hub_discovery import ApiHubDiscoveryClient -from .services.api_hub_plugin import ApiHubPluginClient +from .services.api_hub import ApiHubAsyncClient, ApiHubClient +from .services.api_hub_collect import ApiHubCollectAsyncClient, ApiHubCollectClient +from .services.api_hub_curate import ApiHubCurateAsyncClient, ApiHubCurateClient +from .services.api_hub_dependencies import ( + ApiHubDependenciesAsyncClient, + ApiHubDependenciesClient, +) +from .services.api_hub_discovery import ( + ApiHubDiscoveryAsyncClient, + ApiHubDiscoveryClient, +) +from .services.api_hub_plugin import ApiHubPluginAsyncClient, ApiHubPluginClient from .services.host_project_registration_service import ( + HostProjectRegistrationServiceAsyncClient, HostProjectRegistrationServiceClient, ) -from .services.linting_service import LintingServiceClient -from .services.provisioning import ProvisioningClient +from .services.linting_service import LintingServiceAsyncClient, LintingServiceClient +from .services.provisioning import ProvisioningAsyncClient, ProvisioningClient from .services.runtime_project_attachment_service import ( + RuntimeProjectAttachmentServiceAsyncClient, RuntimeProjectAttachmentServiceClient, ) from .types.apihub_service import ( @@ -227,6 +235,16 @@ ) __all__ = ( + "ApiHubAsyncClient", + "ApiHubCollectAsyncClient", + "ApiHubCurateAsyncClient", + "ApiHubDependenciesAsyncClient", + "ApiHubDiscoveryAsyncClient", + "ApiHubPluginAsyncClient", + "HostProjectRegistrationServiceAsyncClient", + "LintingServiceAsyncClient", + "ProvisioningAsyncClient", + "RuntimeProjectAttachmentServiceAsyncClient", "APIMetadata", "ActionExecutionDetail", "ActionType", diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/gapic_metadata.json b/packages/google-cloud-apihub/google/cloud/apihub_v1/gapic_metadata.json index 8c1851b38279..83f66d558e1b 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/gapic_metadata.json +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/gapic_metadata.json @@ -7,7 +7,7 @@ "services": { "ApiHub": { "clients": { - "rest": { + "grpc": { "libraryClient": "ApiHubClient", "rpcs": { "CreateApi": { @@ -201,283 +201,1213 @@ ] } } - } - } - }, - "ApiHubCollect": { - "clients": { - "rest": { - "libraryClient": "ApiHubCollectClient", + }, + "grpc-async": { + "libraryClient": "ApiHubAsyncClient", "rpcs": { - "CollectApiData": { + "CreateApi": { "methods": [ - "collect_api_data" + "create_api" ] - } - } - } - } - }, - "ApiHubCurate": { - "clients": { - "rest": { - "libraryClient": "ApiHubCurateClient", - "rpcs": { - "CreateCuration": { + }, + "CreateApiOperation": { "methods": [ - "create_curation" + "create_api_operation" ] }, - "DeleteCuration": { + "CreateAttribute": { "methods": [ - "delete_curation" + "create_attribute" ] }, - "GetCuration": { + "CreateDeployment": { "methods": [ - "get_curation" + "create_deployment" ] }, - "ListCurations": { + "CreateExternalApi": { "methods": [ - "list_curations" + "create_external_api" ] }, - "UpdateCuration": { + "CreateSpec": { "methods": [ - "update_curation" + "create_spec" ] - } - } - } - } - }, - "ApiHubDependencies": { - "clients": { - "rest": { - "libraryClient": "ApiHubDependenciesClient", - "rpcs": { - "CreateDependency": { + }, + "CreateVersion": { "methods": [ - "create_dependency" + "create_version" ] }, - "DeleteDependency": { + "DeleteApi": { "methods": [ - "delete_dependency" + "delete_api" ] }, - "GetDependency": { + "DeleteApiOperation": { "methods": [ - "get_dependency" + "delete_api_operation" ] }, - "ListDependencies": { + "DeleteAttribute": { "methods": [ - "list_dependencies" + "delete_attribute" ] }, - "UpdateDependency": { + "DeleteDeployment": { "methods": [ - "update_dependency" + "delete_deployment" ] - } - } - } - } - }, - "ApiHubDiscovery": { - "clients": { - "rest": { - "libraryClient": "ApiHubDiscoveryClient", - "rpcs": { - "GetDiscoveredApiObservation": { + }, + "DeleteExternalApi": { "methods": [ - "get_discovered_api_observation" + "delete_external_api" ] }, - "GetDiscoveredApiOperation": { + "DeleteSpec": { "methods": [ - "get_discovered_api_operation" + "delete_spec" ] }, - "ListDiscoveredApiObservations": { + "DeleteVersion": { "methods": [ - "list_discovered_api_observations" + "delete_version" ] }, - "ListDiscoveredApiOperations": { + "GetApi": { "methods": [ - "list_discovered_api_operations" + "get_api" ] - } - } - } - } - }, - "ApiHubPlugin": { - "clients": { - "rest": { - "libraryClient": "ApiHubPluginClient", - "rpcs": { - "CreatePlugin": { + }, + "GetApiOperation": { "methods": [ - "create_plugin" + "get_api_operation" ] }, - "CreatePluginInstance": { + "GetAttribute": { "methods": [ - "create_plugin_instance" + "get_attribute" ] }, - "DeletePlugin": { + "GetDefinition": { "methods": [ - "delete_plugin" + "get_definition" ] }, - "DeletePluginInstance": { + "GetDeployment": { "methods": [ - "delete_plugin_instance" + "get_deployment" ] }, - "DisablePlugin": { + "GetExternalApi": { "methods": [ - "disable_plugin" + "get_external_api" ] }, - "DisablePluginInstanceAction": { + "GetSpec": { "methods": [ - "disable_plugin_instance_action" + "get_spec" ] }, - "EnablePlugin": { + "GetSpecContents": { "methods": [ - "enable_plugin" + "get_spec_contents" ] }, - "EnablePluginInstanceAction": { + "GetVersion": { "methods": [ - "enable_plugin_instance_action" + "get_version" ] }, - "ExecutePluginInstanceAction": { + "ListApiOperations": { "methods": [ - "execute_plugin_instance_action" + "list_api_operations" ] }, - "GetPlugin": { + "ListApis": { "methods": [ - "get_plugin" + "list_apis" ] }, - "GetPluginInstance": { + "ListAttributes": { "methods": [ - "get_plugin_instance" + "list_attributes" ] }, - "ListPluginInstances": { + "ListDeployments": { "methods": [ - "list_plugin_instances" + "list_deployments" ] }, - "ListPlugins": { + "ListExternalApis": { "methods": [ - "list_plugins" + "list_external_apis" ] }, - "UpdatePluginInstance": { + "ListSpecs": { "methods": [ - "update_plugin_instance" + "list_specs" ] - } - } - } - } - }, - "HostProjectRegistrationService": { - "clients": { - "rest": { - "libraryClient": "HostProjectRegistrationServiceClient", - "rpcs": { - "CreateHostProjectRegistration": { + }, + "ListVersions": { "methods": [ - "create_host_project_registration" + "list_versions" ] }, - "GetHostProjectRegistration": { + "SearchResources": { "methods": [ - "get_host_project_registration" + "search_resources" ] }, - "ListHostProjectRegistrations": { + "UpdateApi": { "methods": [ - "list_host_project_registrations" + "update_api" ] - } - } - } - } - }, - "LintingService": { - "clients": { - "rest": { - "libraryClient": "LintingServiceClient", - "rpcs": { - "GetStyleGuide": { + }, + "UpdateApiOperation": { "methods": [ - "get_style_guide" + "update_api_operation" ] }, - "GetStyleGuideContents": { + "UpdateAttribute": { "methods": [ - "get_style_guide_contents" + "update_attribute" ] }, - "LintSpec": { + "UpdateDeployment": { "methods": [ - "lint_spec" + "update_deployment" ] }, - "UpdateStyleGuide": { + "UpdateExternalApi": { "methods": [ - "update_style_guide" + "update_external_api" + ] + }, + "UpdateSpec": { + "methods": [ + "update_spec" + ] + }, + "UpdateVersion": { + "methods": [ + "update_version" ] } } - } - } - }, - "Provisioning": { - "clients": { + }, "rest": { - "libraryClient": "ProvisioningClient", + "libraryClient": "ApiHubClient", "rpcs": { - "CreateApiHubInstance": { + "CreateApi": { "methods": [ - "create_api_hub_instance" + "create_api" ] }, - "DeleteApiHubInstance": { + "CreateApiOperation": { "methods": [ - "delete_api_hub_instance" + "create_api_operation" ] }, - "GetApiHubInstance": { + "CreateAttribute": { "methods": [ - "get_api_hub_instance" + "create_attribute" ] }, - "LookupApiHubInstance": { + "CreateDeployment": { "methods": [ - "lookup_api_hub_instance" + "create_deployment" ] - } - } - } - } - }, + }, + "CreateExternalApi": { + "methods": [ + "create_external_api" + ] + }, + "CreateSpec": { + "methods": [ + "create_spec" + ] + }, + "CreateVersion": { + "methods": [ + "create_version" + ] + }, + "DeleteApi": { + "methods": [ + "delete_api" + ] + }, + "DeleteApiOperation": { + "methods": [ + "delete_api_operation" + ] + }, + "DeleteAttribute": { + "methods": [ + "delete_attribute" + ] + }, + "DeleteDeployment": { + "methods": [ + "delete_deployment" + ] + }, + "DeleteExternalApi": { + "methods": [ + "delete_external_api" + ] + }, + "DeleteSpec": { + "methods": [ + "delete_spec" + ] + }, + "DeleteVersion": { + "methods": [ + "delete_version" + ] + }, + "GetApi": { + "methods": [ + "get_api" + ] + }, + "GetApiOperation": { + "methods": [ + "get_api_operation" + ] + }, + "GetAttribute": { + "methods": [ + "get_attribute" + ] + }, + "GetDefinition": { + "methods": [ + "get_definition" + ] + }, + "GetDeployment": { + "methods": [ + "get_deployment" + ] + }, + "GetExternalApi": { + "methods": [ + "get_external_api" + ] + }, + "GetSpec": { + "methods": [ + "get_spec" + ] + }, + "GetSpecContents": { + "methods": [ + "get_spec_contents" + ] + }, + "GetVersion": { + "methods": [ + "get_version" + ] + }, + "ListApiOperations": { + "methods": [ + "list_api_operations" + ] + }, + "ListApis": { + "methods": [ + "list_apis" + ] + }, + "ListAttributes": { + "methods": [ + "list_attributes" + ] + }, + "ListDeployments": { + "methods": [ + "list_deployments" + ] + }, + "ListExternalApis": { + "methods": [ + "list_external_apis" + ] + }, + "ListSpecs": { + "methods": [ + "list_specs" + ] + }, + "ListVersions": { + "methods": [ + "list_versions" + ] + }, + "SearchResources": { + "methods": [ + "search_resources" + ] + }, + "UpdateApi": { + "methods": [ + "update_api" + ] + }, + "UpdateApiOperation": { + "methods": [ + "update_api_operation" + ] + }, + "UpdateAttribute": { + "methods": [ + "update_attribute" + ] + }, + "UpdateDeployment": { + "methods": [ + "update_deployment" + ] + }, + "UpdateExternalApi": { + "methods": [ + "update_external_api" + ] + }, + "UpdateSpec": { + "methods": [ + "update_spec" + ] + }, + "UpdateVersion": { + "methods": [ + "update_version" + ] + } + } + } + } + }, + "ApiHubCollect": { + "clients": { + "grpc": { + "libraryClient": "ApiHubCollectClient", + "rpcs": { + "CollectApiData": { + "methods": [ + "collect_api_data" + ] + } + } + }, + "grpc-async": { + "libraryClient": "ApiHubCollectAsyncClient", + "rpcs": { + "CollectApiData": { + "methods": [ + "collect_api_data" + ] + } + } + }, + "rest": { + "libraryClient": "ApiHubCollectClient", + "rpcs": { + "CollectApiData": { + "methods": [ + "collect_api_data" + ] + } + } + } + } + }, + "ApiHubCurate": { + "clients": { + "grpc": { + "libraryClient": "ApiHubCurateClient", + "rpcs": { + "CreateCuration": { + "methods": [ + "create_curation" + ] + }, + "DeleteCuration": { + "methods": [ + "delete_curation" + ] + }, + "GetCuration": { + "methods": [ + "get_curation" + ] + }, + "ListCurations": { + "methods": [ + "list_curations" + ] + }, + "UpdateCuration": { + "methods": [ + "update_curation" + ] + } + } + }, + "grpc-async": { + "libraryClient": "ApiHubCurateAsyncClient", + "rpcs": { + "CreateCuration": { + "methods": [ + "create_curation" + ] + }, + "DeleteCuration": { + "methods": [ + "delete_curation" + ] + }, + "GetCuration": { + "methods": [ + "get_curation" + ] + }, + "ListCurations": { + "methods": [ + "list_curations" + ] + }, + "UpdateCuration": { + "methods": [ + "update_curation" + ] + } + } + }, + "rest": { + "libraryClient": "ApiHubCurateClient", + "rpcs": { + "CreateCuration": { + "methods": [ + "create_curation" + ] + }, + "DeleteCuration": { + "methods": [ + "delete_curation" + ] + }, + "GetCuration": { + "methods": [ + "get_curation" + ] + }, + "ListCurations": { + "methods": [ + "list_curations" + ] + }, + "UpdateCuration": { + "methods": [ + "update_curation" + ] + } + } + } + } + }, + "ApiHubDependencies": { + "clients": { + "grpc": { + "libraryClient": "ApiHubDependenciesClient", + "rpcs": { + "CreateDependency": { + "methods": [ + "create_dependency" + ] + }, + "DeleteDependency": { + "methods": [ + "delete_dependency" + ] + }, + "GetDependency": { + "methods": [ + "get_dependency" + ] + }, + "ListDependencies": { + "methods": [ + "list_dependencies" + ] + }, + "UpdateDependency": { + "methods": [ + "update_dependency" + ] + } + } + }, + "grpc-async": { + "libraryClient": "ApiHubDependenciesAsyncClient", + "rpcs": { + "CreateDependency": { + "methods": [ + "create_dependency" + ] + }, + "DeleteDependency": { + "methods": [ + "delete_dependency" + ] + }, + "GetDependency": { + "methods": [ + "get_dependency" + ] + }, + "ListDependencies": { + "methods": [ + "list_dependencies" + ] + }, + "UpdateDependency": { + "methods": [ + "update_dependency" + ] + } + } + }, + "rest": { + "libraryClient": "ApiHubDependenciesClient", + "rpcs": { + "CreateDependency": { + "methods": [ + "create_dependency" + ] + }, + "DeleteDependency": { + "methods": [ + "delete_dependency" + ] + }, + "GetDependency": { + "methods": [ + "get_dependency" + ] + }, + "ListDependencies": { + "methods": [ + "list_dependencies" + ] + }, + "UpdateDependency": { + "methods": [ + "update_dependency" + ] + } + } + } + } + }, + "ApiHubDiscovery": { + "clients": { + "grpc": { + "libraryClient": "ApiHubDiscoveryClient", + "rpcs": { + "GetDiscoveredApiObservation": { + "methods": [ + "get_discovered_api_observation" + ] + }, + "GetDiscoveredApiOperation": { + "methods": [ + "get_discovered_api_operation" + ] + }, + "ListDiscoveredApiObservations": { + "methods": [ + "list_discovered_api_observations" + ] + }, + "ListDiscoveredApiOperations": { + "methods": [ + "list_discovered_api_operations" + ] + } + } + }, + "grpc-async": { + "libraryClient": "ApiHubDiscoveryAsyncClient", + "rpcs": { + "GetDiscoveredApiObservation": { + "methods": [ + "get_discovered_api_observation" + ] + }, + "GetDiscoveredApiOperation": { + "methods": [ + "get_discovered_api_operation" + ] + }, + "ListDiscoveredApiObservations": { + "methods": [ + "list_discovered_api_observations" + ] + }, + "ListDiscoveredApiOperations": { + "methods": [ + "list_discovered_api_operations" + ] + } + } + }, + "rest": { + "libraryClient": "ApiHubDiscoveryClient", + "rpcs": { + "GetDiscoveredApiObservation": { + "methods": [ + "get_discovered_api_observation" + ] + }, + "GetDiscoveredApiOperation": { + "methods": [ + "get_discovered_api_operation" + ] + }, + "ListDiscoveredApiObservations": { + "methods": [ + "list_discovered_api_observations" + ] + }, + "ListDiscoveredApiOperations": { + "methods": [ + "list_discovered_api_operations" + ] + } + } + } + } + }, + "ApiHubPlugin": { + "clients": { + "grpc": { + "libraryClient": "ApiHubPluginClient", + "rpcs": { + "CreatePlugin": { + "methods": [ + "create_plugin" + ] + }, + "CreatePluginInstance": { + "methods": [ + "create_plugin_instance" + ] + }, + "DeletePlugin": { + "methods": [ + "delete_plugin" + ] + }, + "DeletePluginInstance": { + "methods": [ + "delete_plugin_instance" + ] + }, + "DisablePlugin": { + "methods": [ + "disable_plugin" + ] + }, + "DisablePluginInstanceAction": { + "methods": [ + "disable_plugin_instance_action" + ] + }, + "EnablePlugin": { + "methods": [ + "enable_plugin" + ] + }, + "EnablePluginInstanceAction": { + "methods": [ + "enable_plugin_instance_action" + ] + }, + "ExecutePluginInstanceAction": { + "methods": [ + "execute_plugin_instance_action" + ] + }, + "GetPlugin": { + "methods": [ + "get_plugin" + ] + }, + "GetPluginInstance": { + "methods": [ + "get_plugin_instance" + ] + }, + "ListPluginInstances": { + "methods": [ + "list_plugin_instances" + ] + }, + "ListPlugins": { + "methods": [ + "list_plugins" + ] + }, + "UpdatePluginInstance": { + "methods": [ + "update_plugin_instance" + ] + } + } + }, + "grpc-async": { + "libraryClient": "ApiHubPluginAsyncClient", + "rpcs": { + "CreatePlugin": { + "methods": [ + "create_plugin" + ] + }, + "CreatePluginInstance": { + "methods": [ + "create_plugin_instance" + ] + }, + "DeletePlugin": { + "methods": [ + "delete_plugin" + ] + }, + "DeletePluginInstance": { + "methods": [ + "delete_plugin_instance" + ] + }, + "DisablePlugin": { + "methods": [ + "disable_plugin" + ] + }, + "DisablePluginInstanceAction": { + "methods": [ + "disable_plugin_instance_action" + ] + }, + "EnablePlugin": { + "methods": [ + "enable_plugin" + ] + }, + "EnablePluginInstanceAction": { + "methods": [ + "enable_plugin_instance_action" + ] + }, + "ExecutePluginInstanceAction": { + "methods": [ + "execute_plugin_instance_action" + ] + }, + "GetPlugin": { + "methods": [ + "get_plugin" + ] + }, + "GetPluginInstance": { + "methods": [ + "get_plugin_instance" + ] + }, + "ListPluginInstances": { + "methods": [ + "list_plugin_instances" + ] + }, + "ListPlugins": { + "methods": [ + "list_plugins" + ] + }, + "UpdatePluginInstance": { + "methods": [ + "update_plugin_instance" + ] + } + } + }, + "rest": { + "libraryClient": "ApiHubPluginClient", + "rpcs": { + "CreatePlugin": { + "methods": [ + "create_plugin" + ] + }, + "CreatePluginInstance": { + "methods": [ + "create_plugin_instance" + ] + }, + "DeletePlugin": { + "methods": [ + "delete_plugin" + ] + }, + "DeletePluginInstance": { + "methods": [ + "delete_plugin_instance" + ] + }, + "DisablePlugin": { + "methods": [ + "disable_plugin" + ] + }, + "DisablePluginInstanceAction": { + "methods": [ + "disable_plugin_instance_action" + ] + }, + "EnablePlugin": { + "methods": [ + "enable_plugin" + ] + }, + "EnablePluginInstanceAction": { + "methods": [ + "enable_plugin_instance_action" + ] + }, + "ExecutePluginInstanceAction": { + "methods": [ + "execute_plugin_instance_action" + ] + }, + "GetPlugin": { + "methods": [ + "get_plugin" + ] + }, + "GetPluginInstance": { + "methods": [ + "get_plugin_instance" + ] + }, + "ListPluginInstances": { + "methods": [ + "list_plugin_instances" + ] + }, + "ListPlugins": { + "methods": [ + "list_plugins" + ] + }, + "UpdatePluginInstance": { + "methods": [ + "update_plugin_instance" + ] + } + } + } + } + }, + "HostProjectRegistrationService": { + "clients": { + "grpc": { + "libraryClient": "HostProjectRegistrationServiceClient", + "rpcs": { + "CreateHostProjectRegistration": { + "methods": [ + "create_host_project_registration" + ] + }, + "GetHostProjectRegistration": { + "methods": [ + "get_host_project_registration" + ] + }, + "ListHostProjectRegistrations": { + "methods": [ + "list_host_project_registrations" + ] + } + } + }, + "grpc-async": { + "libraryClient": "HostProjectRegistrationServiceAsyncClient", + "rpcs": { + "CreateHostProjectRegistration": { + "methods": [ + "create_host_project_registration" + ] + }, + "GetHostProjectRegistration": { + "methods": [ + "get_host_project_registration" + ] + }, + "ListHostProjectRegistrations": { + "methods": [ + "list_host_project_registrations" + ] + } + } + }, + "rest": { + "libraryClient": "HostProjectRegistrationServiceClient", + "rpcs": { + "CreateHostProjectRegistration": { + "methods": [ + "create_host_project_registration" + ] + }, + "GetHostProjectRegistration": { + "methods": [ + "get_host_project_registration" + ] + }, + "ListHostProjectRegistrations": { + "methods": [ + "list_host_project_registrations" + ] + } + } + } + } + }, + "LintingService": { + "clients": { + "grpc": { + "libraryClient": "LintingServiceClient", + "rpcs": { + "GetStyleGuide": { + "methods": [ + "get_style_guide" + ] + }, + "GetStyleGuideContents": { + "methods": [ + "get_style_guide_contents" + ] + }, + "LintSpec": { + "methods": [ + "lint_spec" + ] + }, + "UpdateStyleGuide": { + "methods": [ + "update_style_guide" + ] + } + } + }, + "grpc-async": { + "libraryClient": "LintingServiceAsyncClient", + "rpcs": { + "GetStyleGuide": { + "methods": [ + "get_style_guide" + ] + }, + "GetStyleGuideContents": { + "methods": [ + "get_style_guide_contents" + ] + }, + "LintSpec": { + "methods": [ + "lint_spec" + ] + }, + "UpdateStyleGuide": { + "methods": [ + "update_style_guide" + ] + } + } + }, + "rest": { + "libraryClient": "LintingServiceClient", + "rpcs": { + "GetStyleGuide": { + "methods": [ + "get_style_guide" + ] + }, + "GetStyleGuideContents": { + "methods": [ + "get_style_guide_contents" + ] + }, + "LintSpec": { + "methods": [ + "lint_spec" + ] + }, + "UpdateStyleGuide": { + "methods": [ + "update_style_guide" + ] + } + } + } + } + }, + "Provisioning": { + "clients": { + "grpc": { + "libraryClient": "ProvisioningClient", + "rpcs": { + "CreateApiHubInstance": { + "methods": [ + "create_api_hub_instance" + ] + }, + "DeleteApiHubInstance": { + "methods": [ + "delete_api_hub_instance" + ] + }, + "GetApiHubInstance": { + "methods": [ + "get_api_hub_instance" + ] + }, + "LookupApiHubInstance": { + "methods": [ + "lookup_api_hub_instance" + ] + } + } + }, + "grpc-async": { + "libraryClient": "ProvisioningAsyncClient", + "rpcs": { + "CreateApiHubInstance": { + "methods": [ + "create_api_hub_instance" + ] + }, + "DeleteApiHubInstance": { + "methods": [ + "delete_api_hub_instance" + ] + }, + "GetApiHubInstance": { + "methods": [ + "get_api_hub_instance" + ] + }, + "LookupApiHubInstance": { + "methods": [ + "lookup_api_hub_instance" + ] + } + } + }, + "rest": { + "libraryClient": "ProvisioningClient", + "rpcs": { + "CreateApiHubInstance": { + "methods": [ + "create_api_hub_instance" + ] + }, + "DeleteApiHubInstance": { + "methods": [ + "delete_api_hub_instance" + ] + }, + "GetApiHubInstance": { + "methods": [ + "get_api_hub_instance" + ] + }, + "LookupApiHubInstance": { + "methods": [ + "lookup_api_hub_instance" + ] + } + } + } + } + }, "RuntimeProjectAttachmentService": { "clients": { + "grpc": { + "libraryClient": "RuntimeProjectAttachmentServiceClient", + "rpcs": { + "CreateRuntimeProjectAttachment": { + "methods": [ + "create_runtime_project_attachment" + ] + }, + "DeleteRuntimeProjectAttachment": { + "methods": [ + "delete_runtime_project_attachment" + ] + }, + "GetRuntimeProjectAttachment": { + "methods": [ + "get_runtime_project_attachment" + ] + }, + "ListRuntimeProjectAttachments": { + "methods": [ + "list_runtime_project_attachments" + ] + }, + "LookupRuntimeProjectAttachment": { + "methods": [ + "lookup_runtime_project_attachment" + ] + } + } + }, + "grpc-async": { + "libraryClient": "RuntimeProjectAttachmentServiceAsyncClient", + "rpcs": { + "CreateRuntimeProjectAttachment": { + "methods": [ + "create_runtime_project_attachment" + ] + }, + "DeleteRuntimeProjectAttachment": { + "methods": [ + "delete_runtime_project_attachment" + ] + }, + "GetRuntimeProjectAttachment": { + "methods": [ + "get_runtime_project_attachment" + ] + }, + "ListRuntimeProjectAttachments": { + "methods": [ + "list_runtime_project_attachments" + ] + }, + "LookupRuntimeProjectAttachment": { + "methods": [ + "lookup_runtime_project_attachment" + ] + } + } + }, "rest": { "libraryClient": "RuntimeProjectAttachmentServiceClient", "rpcs": { diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/gapic_version.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/gapic_version.py index 5e9f73f4e3f5..20a9cd975b02 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/gapic_version.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/gapic_version.py @@ -13,4 +13,4 @@ # See the License for the specific language governing permissions and # limitations under the License. # -__version__ = "0.2.7" # {x-release-please-version} +__version__ = "0.0.0" # {x-release-please-version} diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/__init__.py index 17ed93c5a71b..51a19cd026d2 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/__init__.py @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from .async_client import ApiHubAsyncClient from .client import ApiHubClient -__all__ = ("ApiHubClient",) +__all__ = ( + "ApiHubClient", + "ApiHubAsyncClient", +) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/async_client.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/async_client.py index 9d1b494ddccf..2cdf8b393f9c 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/async_client.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/async_client.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,6 +14,7 @@ # limitations under the License. # from collections import OrderedDict +import logging as std_logging import re from typing import ( Callable, @@ -34,6 +35,7 @@ from google.api_core.client_options import ClientOptions from google.auth import credentials as ga_credentials # type: ignore from google.oauth2 import service_account # type: ignore +import google.protobuf from google.cloud.apihub_v1 import gapic_version as package_version @@ -54,6 +56,15 @@ from .transports.base import DEFAULT_CLIENT_INFO, ApiHubTransport from .transports.grpc_asyncio import ApiHubGrpcAsyncIOTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + class ApiHubAsyncClient: """This service provides all methods related to the API hub.""" @@ -79,6 +90,8 @@ class ApiHubAsyncClient: parse_deployment_path = staticmethod(ApiHubClient.parse_deployment_path) external_api_path = staticmethod(ApiHubClient.external_api_path) parse_external_api_path = staticmethod(ApiHubClient.parse_external_api_path) + plugin_instance_path = staticmethod(ApiHubClient.plugin_instance_path) + parse_plugin_instance_path = staticmethod(ApiHubClient.parse_plugin_instance_path) spec_path = staticmethod(ApiHubClient.spec_path) parse_spec_path = staticmethod(ApiHubClient.parse_spec_path) version_path = staticmethod(ApiHubClient.version_path) @@ -263,6 +276,28 @@ def __init__( client_info=client_info, ) + if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ): # pragma: NO COVER + _LOGGER.debug( + "Created client `google.cloud.apihub_v1.ApiHubAsyncClient`.", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHub", + "universeDomain": getattr( + self._client._transport._credentials, "universe_domain", "" + ), + "credentialsType": f"{type(self._client._transport._credentials).__module__}.{type(self._client._transport._credentials).__qualname__}", + "credentialsInfo": getattr( + self.transport._credentials, "get_cred_info", lambda: None + )(), + } + if hasattr(self._client._transport, "_credentials") + else { + "serviceName": "google.cloud.apihub.v1.ApiHub", + "credentialsType": None, + }, + ) + async def create_api( self, request: Optional[Union[apihub_service.CreateApiRequest, dict]] = None, @@ -272,7 +307,7 @@ async def create_api( api_id: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.Api: r"""Create an API resource in the API hub. Once an API resource is created, versions can be added @@ -329,13 +364,13 @@ async def sample_create_api(): become the final component of the API's resource name. This field is optional. - - If provided, the same will be used. The service will - throw an error if the specified id is already used by - another API resource in the API hub. - - If not provided, a system generated id will be used. + - If provided, the same will be used. The service will + throw an error if the specified id is already used by + another API resource in the API hub. + - If not provided, a system generated id will be used. This value should be 4-500 characters, and valid - characters are /[a-z][A-Z][0-9]-_/. + characters are /[a-z][A-Z][0-9]-\_/. This corresponds to the ``api_id`` field on the ``request`` instance; if ``request`` is provided, this @@ -343,8 +378,10 @@ async def sample_create_api(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Api: @@ -353,7 +390,10 @@ async def sample_create_api(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([parent, api, api_id]) + flattened_params = [parent, api, api_id] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -407,7 +447,7 @@ async def get_api( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.Api: r"""Get API resource details including the API versions contained in it. @@ -453,8 +493,10 @@ async def sample_get_api(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Api: @@ -463,7 +505,10 @@ async def sample_get_api(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -511,7 +556,7 @@ async def list_apis( parent: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> pagers.ListApisAsyncPager: r"""List API resources in the API hub. @@ -557,8 +602,10 @@ async def sample_list_apis(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.services.api_hub.pagers.ListApisAsyncPager: @@ -572,7 +619,10 @@ async def sample_list_apis(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([parent]) + flattened_params = [parent] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -634,20 +684,21 @@ async def update_api( update_mask: Optional[field_mask_pb2.FieldMask] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.Api: r"""Update an API resource in the API hub. The following fields in - the [API][] can be updated: - - - [display_name][google.cloud.apihub.v1.Api.display_name] - - [description][google.cloud.apihub.v1.Api.description] - - [owner][google.cloud.apihub.v1.Api.owner] - - [documentation][google.cloud.apihub.v1.Api.documentation] - - [target_user][google.cloud.apihub.v1.Api.target_user] - - [team][google.cloud.apihub.v1.Api.team] - - [business_unit][google.cloud.apihub.v1.Api.business_unit] - - [maturity_level][google.cloud.apihub.v1.Api.maturity_level] - - [attributes][google.cloud.apihub.v1.Api.attributes] + the [API][google.cloud.apihub.v1.Api] can be updated: + + - [display_name][google.cloud.apihub.v1.Api.display_name] + - [description][google.cloud.apihub.v1.Api.description] + - [owner][google.cloud.apihub.v1.Api.owner] + - [documentation][google.cloud.apihub.v1.Api.documentation] + - [target_user][google.cloud.apihub.v1.Api.target_user] + - [team][google.cloud.apihub.v1.Api.team] + - [business_unit][google.cloud.apihub.v1.Api.business_unit] + - [maturity_level][google.cloud.apihub.v1.Api.maturity_level] + - [api_style][google.cloud.apihub.v1.Api.api_style] + - [attributes][google.cloud.apihub.v1.Api.attributes] The [update_mask][google.cloud.apihub.v1.UpdateApiRequest.update_mask] @@ -709,8 +760,10 @@ async def sample_update_api(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Api: @@ -719,7 +772,10 @@ async def sample_update_api(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([api, update_mask]) + flattened_params = [api, update_mask] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -771,7 +827,7 @@ async def delete_api( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Delete an API resource in the API hub. API can only be deleted if all underlying versions are deleted. @@ -814,13 +870,18 @@ async def sample_delete_api(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. """ # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -869,7 +930,7 @@ async def create_version( version_id: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.Version: r"""Create an API version for an API resource in the API hub. @@ -926,13 +987,16 @@ async def sample_create_version(): become the final component of the version's resource name. This field is optional. - - If provided, the same will be used. The service will - throw an error if the specified id is already used by - another version in the API resource. - - If not provided, a system generated id will be used. + - If provided, the same will be used. The service will + throw an error if the specified id is already used by + another version in the API resource. + - If not provided, a system generated id will be used. - This value should be 4-500 characters, and valid - characters are /[a-z][A-Z][0-9]-_/. + This value should be 4-500 characters, overall resource + name which will be of format + ``projects/{project}/locations/{location}/apis/{api}/versions/{version}``, + its length is limited to 700 characters and valid + characters are /[a-z][A-Z][0-9]-\_/. This corresponds to the ``version_id`` field on the ``request`` instance; if ``request`` is provided, this @@ -940,8 +1004,10 @@ async def sample_create_version(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Version: @@ -953,7 +1019,10 @@ async def sample_create_version(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([parent, version, version_id]) + flattened_params = [parent, version, version_id] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -1007,7 +1076,7 @@ async def get_version( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.Version: r"""Get details about the API version of an API resource. This will include information about the specs and @@ -1056,8 +1125,10 @@ async def sample_get_version(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Version: @@ -1069,7 +1140,10 @@ async def sample_get_version(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -1119,7 +1193,7 @@ async def list_versions( parent: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> pagers.ListVersionsAsyncPager: r"""List API versions of an API resource in the API hub. @@ -1166,8 +1240,10 @@ async def sample_list_versions(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.services.api_hub.pagers.ListVersionsAsyncPager: @@ -1181,7 +1257,10 @@ async def sample_list_versions(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([parent]) + flattened_params = [parent] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -1243,20 +1322,20 @@ async def update_version( update_mask: Optional[field_mask_pb2.FieldMask] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.Version: r"""Update API version. The following fields in the [version][google.cloud.apihub.v1.Version] can be updated currently: - - [display_name][google.cloud.apihub.v1.Version.display_name] - - [description][google.cloud.apihub.v1.Version.description] - - [documentation][google.cloud.apihub.v1.Version.documentation] - - [deployments][google.cloud.apihub.v1.Version.deployments] - - [lifecycle][google.cloud.apihub.v1.Version.lifecycle] - - [compliance][google.cloud.apihub.v1.Version.compliance] - - [accreditation][google.cloud.apihub.v1.Version.accreditation] - - [attributes][google.cloud.apihub.v1.Version.attributes] + - [display_name][google.cloud.apihub.v1.Version.display_name] + - [description][google.cloud.apihub.v1.Version.description] + - [documentation][google.cloud.apihub.v1.Version.documentation] + - [deployments][google.cloud.apihub.v1.Version.deployments] + - [lifecycle][google.cloud.apihub.v1.Version.lifecycle] + - [compliance][google.cloud.apihub.v1.Version.compliance] + - [accreditation][google.cloud.apihub.v1.Version.accreditation] + - [attributes][google.cloud.apihub.v1.Version.attributes] The [update_mask][google.cloud.apihub.v1.UpdateVersionRequest.update_mask] @@ -1316,8 +1395,10 @@ async def sample_update_version(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Version: @@ -1329,7 +1410,10 @@ async def sample_update_version(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([version, update_mask]) + flattened_params = [version, update_mask] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -1383,7 +1467,7 @@ async def delete_version( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Delete an API version. Version can only be deleted if all underlying specs, operations, definitions and linked @@ -1427,13 +1511,18 @@ async def sample_delete_version(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. """ # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -1482,7 +1571,7 @@ async def create_spec( spec_id: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.Spec: r"""Add a spec to an API version in the API hub. Multiple specs can be added to an API version. Note, while adding a spec, at least @@ -1562,13 +1651,16 @@ async def sample_create_spec(): the final component of the spec's resource name. This field is optional. - - If provided, the same will be used. The service will - throw an error if the specified id is already used by - another spec in the API resource. - - If not provided, a system generated id will be used. + - If provided, the same will be used. The service will + throw an error if the specified id is already used by + another spec in the API resource. + - If not provided, a system generated id will be used. - This value should be 4-500 characters, and valid - characters are /[a-z][A-Z][0-9]-_/. + This value should be 4-500 characters, overall resource + name which will be of format + ``projects/{project}/locations/{location}/apis/{api}/versions/{version}/specs/{spec}``, + its length is limited to 1000 characters and valid + characters are /[a-z][A-Z][0-9]-\_/. This corresponds to the ``spec_id`` field on the ``request`` instance; if ``request`` is provided, this @@ -1576,8 +1668,10 @@ async def sample_create_spec(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Spec: @@ -1591,7 +1685,10 @@ async def sample_create_spec(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([parent, spec, spec_id]) + flattened_params = [parent, spec, spec_id] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -1645,7 +1742,7 @@ async def get_spec( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.Spec: r"""Get details about the information parsed from a spec. Note that this method does not return the raw spec contents. Use @@ -1692,8 +1789,10 @@ async def sample_get_spec(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Spec: @@ -1707,7 +1806,10 @@ async def sample_get_spec(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -1755,7 +1857,7 @@ async def get_spec_contents( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.SpecContents: r"""Get spec contents. @@ -1801,8 +1903,10 @@ async def sample_get_spec_contents(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.SpecContents: @@ -1811,7 +1915,10 @@ async def sample_get_spec_contents(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -1861,7 +1968,7 @@ async def list_specs( parent: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> pagers.ListSpecsAsyncPager: r"""List specs corresponding to a particular API resource. @@ -1907,8 +2014,10 @@ async def sample_list_specs(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.services.api_hub.pagers.ListSpecsAsyncPager: @@ -1922,7 +2031,10 @@ async def sample_list_specs(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([parent]) + flattened_params = [parent] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -1984,17 +2096,17 @@ async def update_spec( update_mask: Optional[field_mask_pb2.FieldMask] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.Spec: r"""Update spec. The following fields in the [spec][google.cloud.apihub.v1.Spec] can be updated: - - [display_name][google.cloud.apihub.v1.Spec.display_name] - - [source_uri][google.cloud.apihub.v1.Spec.source_uri] - - [lint_response][google.cloud.apihub.v1.Spec.lint_response] - - [attributes][google.cloud.apihub.v1.Spec.attributes] - - [contents][google.cloud.apihub.v1.Spec.contents] - - [spec_type][google.cloud.apihub.v1.Spec.spec_type] + - [display_name][google.cloud.apihub.v1.Spec.display_name] + - [source_uri][google.cloud.apihub.v1.Spec.source_uri] + - [lint_response][google.cloud.apihub.v1.Spec.lint_response] + - [attributes][google.cloud.apihub.v1.Spec.attributes] + - [contents][google.cloud.apihub.v1.Spec.contents] + - [spec_type][google.cloud.apihub.v1.Spec.spec_type] In case of an OAS spec, updating spec contents can lead to: @@ -2066,8 +2178,10 @@ async def sample_update_spec(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Spec: @@ -2081,7 +2195,10 @@ async def sample_update_spec(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([spec, update_mask]) + flattened_params = [spec, update_mask] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -2135,7 +2252,7 @@ async def delete_spec( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Delete a spec. Deleting a spec will also delete the associated @@ -2179,13 +2296,18 @@ async def sample_delete_spec(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. """ # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -2225,6 +2347,167 @@ async def sample_delete_spec(): metadata=metadata, ) + async def create_api_operation( + self, + request: Optional[Union[apihub_service.CreateApiOperationRequest, dict]] = None, + *, + parent: Optional[str] = None, + api_operation: Optional[common_fields.ApiOperation] = None, + api_operation_id: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> common_fields.ApiOperation: + r"""Create an apiOperation in an API version. + An apiOperation can be created only if the version has + no apiOperations which were created by parsing a spec. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_create_api_operation(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.CreateApiOperationRequest( + parent="parent_value", + ) + + # Make the request + response = await client.create_api_operation(request=request) + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.CreateApiOperationRequest, dict]]): + The request object. The + [CreateApiOperation][google.cloud.apihub.v1.ApiHub.CreateApiOperation] + method's request. + parent (:class:`str`): + Required. The parent resource for the operation + resource. Format: + ``projects/{project}/locations/{location}/apis/{api}/versions/{version}`` + + This corresponds to the ``parent`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + api_operation (:class:`google.cloud.apihub_v1.types.ApiOperation`): + Required. The operation resource to + create. + + This corresponds to the ``api_operation`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + api_operation_id (:class:`str`): + Optional. The ID to use for the operation resource, + which will become the final component of the operation's + resource name. This field is optional. + + - If provided, the same will be used. The service will + throw an error if the specified id is already used by + another operation resource in the API hub. + - If not provided, a system generated id will be used. + + This value should be 4-500 characters, overall resource + name which will be of format + ``projects/{project}/locations/{location}/apis/{api}/versions/{version}/operations/{operation}``, + its length is limited to 700 characters, and valid + characters are /[a-z][A-Z][0-9]-\_/. + + This corresponds to the ``api_operation_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + + Returns: + google.cloud.apihub_v1.types.ApiOperation: + Represents an operation contained in + an API version in the API Hub. An + operation is added/updated/deleted in an + API version when a new spec is added or + an existing spec is updated/deleted in a + version. Currently, an operation will be + created only corresponding to OpenAPI + spec as parsing is supported for OpenAPI + spec. + Alternatively operations can be managed + via create,update and delete APIs, + creation of apiOperation can be possible + only for version with no parsed + operations and update/delete can be + possible only for operations created via + create API. + + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [parent, api_operation, api_operation_id] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, apihub_service.CreateApiOperationRequest): + request = apihub_service.CreateApiOperationRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent + if api_operation is not None: + request.api_operation = api_operation + if api_operation_id is not None: + request.api_operation_id = api_operation_id + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.create_api_operation + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + async def get_api_operation( self, request: Optional[Union[apihub_service.GetApiOperationRequest, dict]] = None, @@ -2232,7 +2515,7 @@ async def get_api_operation( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.ApiOperation: r"""Get details about a particular operation in API version. @@ -2278,8 +2561,10 @@ async def sample_get_api_operation(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.ApiOperation: @@ -2292,12 +2577,22 @@ async def sample_get_api_operation(): created only corresponding to OpenAPI spec as parsing is supported for OpenAPI spec. + Alternatively operations can be managed + via create,update and delete APIs, + creation of apiOperation can be possible + only for version with no parsed + operations and update/delete can be + possible only for operations created via + create API. """ # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -2347,7 +2642,7 @@ async def list_api_operations( parent: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> pagers.ListApiOperationsAsyncPager: r"""List operations in an API version. @@ -2394,8 +2689,10 @@ async def sample_list_api_operations(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.services.api_hub.pagers.ListApiOperationsAsyncPager: @@ -2409,7 +2706,10 @@ async def sample_list_api_operations(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([parent]) + flattened_params = [parent] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -2463,6 +2763,269 @@ async def sample_list_api_operations(): # Done; return the response. return response + async def update_api_operation( + self, + request: Optional[Union[apihub_service.UpdateApiOperationRequest, dict]] = None, + *, + api_operation: Optional[common_fields.ApiOperation] = None, + update_mask: Optional[field_mask_pb2.FieldMask] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> common_fields.ApiOperation: + r"""Update an operation in an API version. The following fields in + the [ApiOperation resource][google.cloud.apihub.v1.ApiOperation] + can be updated: + + - [details.description][ApiOperation.details.description] + - [details.documentation][ApiOperation.details.documentation] + - [details.http_operation.path][ApiOperation.details.http_operation.path.path] + - [details.http_operation.method][ApiOperation.details.http_operation.method] + - [details.deprecated][ApiOperation.details.deprecated] + - [attributes][google.cloud.apihub.v1.ApiOperation.attributes] + + The + [update_mask][google.cloud.apihub.v1.UpdateApiOperationRequest.update_mask] + should be used to specify the fields being updated. + + An operation can be updated only if the operation was created + via + [CreateApiOperation][google.cloud.apihub.v1.ApiHub.CreateApiOperation] + API. If the operation was created by parsing the spec, then it + can be edited by updating the spec. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_update_api_operation(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.UpdateApiOperationRequest( + ) + + # Make the request + response = await client.update_api_operation(request=request) + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.UpdateApiOperationRequest, dict]]): + The request object. The + [UpdateApiOperation][google.cloud.apihub.v1.ApiHub.UpdateApiOperation] + method's request. + api_operation (:class:`google.cloud.apihub_v1.types.ApiOperation`): + Required. The apiOperation resource to update. + + The operation resource's ``name`` field is used to + identify the operation resource to update. Format: + ``projects/{project}/locations/{location}/apis/{api}/versions/{version}/operations/{operation}`` + + This corresponds to the ``api_operation`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + update_mask (:class:`google.protobuf.field_mask_pb2.FieldMask`): + Required. The list of fields to + update. + + This corresponds to the ``update_mask`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + + Returns: + google.cloud.apihub_v1.types.ApiOperation: + Represents an operation contained in + an API version in the API Hub. An + operation is added/updated/deleted in an + API version when a new spec is added or + an existing spec is updated/deleted in a + version. Currently, an operation will be + created only corresponding to OpenAPI + spec as parsing is supported for OpenAPI + spec. + Alternatively operations can be managed + via create,update and delete APIs, + creation of apiOperation can be possible + only for version with no parsed + operations and update/delete can be + possible only for operations created via + create API. + + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [api_operation, update_mask] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, apihub_service.UpdateApiOperationRequest): + request = apihub_service.UpdateApiOperationRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if api_operation is not None: + request.api_operation = api_operation + if update_mask is not None: + request.update_mask = update_mask + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.update_api_operation + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata( + (("api_operation.name", request.api_operation.name),) + ), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def delete_api_operation( + self, + request: Optional[Union[apihub_service.DeleteApiOperationRequest, dict]] = None, + *, + name: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> None: + r"""Delete an operation in an API version and we can + delete only the operations created via create API. If + the operation was created by parsing the spec, then it + can be deleted by editing or deleting the spec. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_delete_api_operation(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DeleteApiOperationRequest( + name="name_value", + ) + + # Make the request + await client.delete_api_operation(request=request) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.DeleteApiOperationRequest, dict]]): + The request object. The + [DeleteApiOperation][google.cloud.apihub.v1.ApiHub.DeleteApiOperation] + method's request. + name (:class:`str`): + Required. The name of the operation resource to delete. + Format: + ``projects/{project}/locations/{location}/apis/{api}/versions/{version}/operations/{operation}`` + + This corresponds to the ``name`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, apihub_service.DeleteApiOperationRequest): + request = apihub_service.DeleteApiOperationRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if name is not None: + request.name = name + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.delete_api_operation + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + async def get_definition( self, request: Optional[Union[apihub_service.GetDefinitionRequest, dict]] = None, @@ -2470,7 +3033,7 @@ async def get_definition( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.Definition: r"""Get details about a definition in an API version. @@ -2516,8 +3079,10 @@ async def sample_get_definition(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Definition: @@ -2534,7 +3099,10 @@ async def sample_get_definition(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -2586,7 +3154,7 @@ async def create_deployment( deployment_id: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.Deployment: r"""Create a deployment resource in the API hub. Once a deployment resource is created, it can be @@ -2651,13 +3219,13 @@ async def sample_create_deployment(): which will become the final component of the deployment's resource name. This field is optional. - - If provided, the same will be used. The service will - throw an error if the specified id is already used by - another deployment resource in the API hub. - - If not provided, a system generated id will be used. + - If provided, the same will be used. The service will + throw an error if the specified id is already used by + another deployment resource in the API hub. + - If not provided, a system generated id will be used. This value should be 4-500 characters, and valid - characters are /[a-z][A-Z][0-9]-_/. + characters are /[a-z][A-Z][0-9]-\_/. This corresponds to the ``deployment_id`` field on the ``request`` instance; if ``request`` is provided, this @@ -2665,8 +3233,10 @@ async def sample_create_deployment(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Deployment: @@ -2682,7 +3252,10 @@ async def sample_create_deployment(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([parent, deployment, deployment_id]) + flattened_params = [parent, deployment, deployment_id] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -2736,7 +3309,7 @@ async def get_deployment( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.Deployment: r"""Get details about a deployment and the API versions linked to it. @@ -2783,8 +3356,10 @@ async def sample_get_deployment(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Deployment: @@ -2800,7 +3375,10 @@ async def sample_get_deployment(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -2850,7 +3428,7 @@ async def list_deployments( parent: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> pagers.ListDeploymentsAsyncPager: r"""List deployment resources in the API hub. @@ -2897,8 +3475,10 @@ async def sample_list_deployments(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.services.api_hub.pagers.ListDeploymentsAsyncPager: @@ -2912,7 +3492,10 @@ async def sample_list_deployments(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([parent]) + flattened_params = [parent] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -2974,25 +3557,29 @@ async def update_deployment( update_mask: Optional[field_mask_pb2.FieldMask] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.Deployment: r"""Update a deployment resource in the API hub. The following fields in the [deployment resource][google.cloud.apihub.v1.Deployment] can be updated: - - [display_name][google.cloud.apihub.v1.Deployment.display_name] - - [description][google.cloud.apihub.v1.Deployment.description] - - [documentation][google.cloud.apihub.v1.Deployment.documentation] - - [deployment_type][google.cloud.apihub.v1.Deployment.deployment_type] - - [resource_uri][google.cloud.apihub.v1.Deployment.resource_uri] - - [endpoints][google.cloud.apihub.v1.Deployment.endpoints] - - [slo][google.cloud.apihub.v1.Deployment.slo] - - [environment][google.cloud.apihub.v1.Deployment.environment] - - [attributes][google.cloud.apihub.v1.Deployment.attributes] - - The - [update_mask][google.cloud.apihub.v1.UpdateDeploymentRequest.update_mask] - should be used to specify the fields being updated. + - [display_name][google.cloud.apihub.v1.Deployment.display_name] + - [description][google.cloud.apihub.v1.Deployment.description] + - [documentation][google.cloud.apihub.v1.Deployment.documentation] + - [deployment_type][google.cloud.apihub.v1.Deployment.deployment_type] + - [resource_uri][google.cloud.apihub.v1.Deployment.resource_uri] + - [endpoints][google.cloud.apihub.v1.Deployment.endpoints] + - [slo][google.cloud.apihub.v1.Deployment.slo] + - [environment][google.cloud.apihub.v1.Deployment.environment] + - [attributes][google.cloud.apihub.v1.Deployment.attributes] + - [source_project] + [google.cloud.apihub.v1.Deployment.source_project] + - [source_environment] + [google.cloud.apihub.v1.Deployment.source_environment] + - [management_url][google.cloud.apihub.v1.Deployment.management_url] + - [source_uri][google.cloud.apihub.v1.Deployment.source_uri] The + [update_mask][google.cloud.apihub.v1.UpdateDeploymentRequest.update_mask] + should be used to specify the fields being updated. .. code-block:: python @@ -3052,8 +3639,10 @@ async def sample_update_deployment(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Deployment: @@ -3069,7 +3658,10 @@ async def sample_update_deployment(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([deployment, update_mask]) + flattened_params = [deployment, update_mask] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -3123,7 +3715,7 @@ async def delete_deployment( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Delete a deployment resource in the API hub. @@ -3166,13 +3758,18 @@ async def sample_delete_deployment(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. """ # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -3221,7 +3818,7 @@ async def create_attribute( attribute_id: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.Attribute: r"""Create a user defined attribute. @@ -3252,7 +3849,7 @@ async def sample_create_attribute(): attribute = apihub_v1.Attribute() attribute.display_name = "display_name_value" attribute.scope = "PLUGIN" - attribute.data_type = "STRING" + attribute.data_type = "URI" request = apihub_v1.CreateAttributeRequest( parent="parent_value", @@ -3287,13 +3884,13 @@ async def sample_create_attribute(): become the final component of the attribute's resource name. This field is optional. - - If provided, the same will be used. The service will - throw an error if the specified id is already used by - another attribute resource in the API hub. - - If not provided, a system generated id will be used. + - If provided, the same will be used. The service will + throw an error if the specified id is already used by + another attribute resource in the API hub. + - If not provided, a system generated id will be used. This value should be 4-500 characters, and valid - characters are /[a-z][A-Z][0-9]-_/. + characters are /[a-z][A-Z][0-9]-\_/. This corresponds to the ``attribute_id`` field on the ``request`` instance; if ``request`` is provided, this @@ -3301,8 +3898,10 @@ async def sample_create_attribute(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Attribute: @@ -3318,7 +3917,10 @@ async def sample_create_attribute(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([parent, attribute, attribute_id]) + flattened_params = [parent, attribute, attribute_id] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -3372,7 +3974,7 @@ async def get_attribute( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.Attribute: r"""Get details about the attribute. @@ -3417,8 +4019,10 @@ async def sample_get_attribute(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Attribute: @@ -3434,7 +4038,10 @@ async def sample_get_attribute(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -3485,30 +4092,30 @@ async def update_attribute( update_mask: Optional[field_mask_pb2.FieldMask] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.Attribute: r"""Update the attribute. The following fields in the [Attribute resource][google.cloud.apihub.v1.Attribute] can be updated: - - [display_name][google.cloud.apihub.v1.Attribute.display_name] - The display name can be updated for user defined attributes - only. - - [description][google.cloud.apihub.v1.Attribute.description] - The description can be updated for user defined attributes - only. - - [allowed_values][google.cloud.apihub.v1.Attribute.allowed_values] - To update the list of allowed values, clients need to use the - fetched list of allowed values and add or remove values to or - from the same list. The mutable allowed values can be updated - for both user defined and System defined attributes. The - immutable allowed values cannot be updated or deleted. The - updated list of allowed values cannot be empty. If an allowed - value that is already used by some resource's attribute is - deleted, then the association between the resource and the - attribute value will also be deleted. - - [cardinality][google.cloud.apihub.v1.Attribute.cardinality] - The cardinality can be updated for user defined attributes - only. Cardinality can only be increased during an update. + - [display_name][google.cloud.apihub.v1.Attribute.display_name] + The display name can be updated for user defined attributes + only. + - [description][google.cloud.apihub.v1.Attribute.description] + The description can be updated for user defined attributes + only. + - [allowed_values][google.cloud.apihub.v1.Attribute.allowed_values] + To update the list of allowed values, clients need to use the + fetched list of allowed values and add or remove values to or + from the same list. The mutable allowed values can be updated + for both user defined and System defined attributes. The + immutable allowed values cannot be updated or deleted. The + updated list of allowed values cannot be empty. If an allowed + value that is already used by some resource's attribute is + deleted, then the association between the resource and the + attribute value will also be deleted. + - [cardinality][google.cloud.apihub.v1.Attribute.cardinality] + The cardinality can be updated for user defined attributes + only. Cardinality can only be increased during an update. The [update_mask][google.cloud.apihub.v1.UpdateAttributeRequest.update_mask] @@ -3533,7 +4140,7 @@ async def sample_update_attribute(): attribute = apihub_v1.Attribute() attribute.display_name = "display_name_value" attribute.scope = "PLUGIN" - attribute.data_type = "STRING" + attribute.data_type = "URI" request = apihub_v1.UpdateAttributeRequest( attribute=attribute, @@ -3570,8 +4177,10 @@ async def sample_update_attribute(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Attribute: @@ -3587,7 +4196,10 @@ async def sample_update_attribute(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([attribute, update_mask]) + flattened_params = [attribute, update_mask] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -3641,7 +4253,7 @@ async def delete_attribute( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Delete an attribute. @@ -3687,13 +4299,18 @@ async def sample_delete_attribute(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. """ # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -3740,7 +4357,7 @@ async def list_attributes( parent: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> pagers.ListAttributesAsyncPager: r"""List all attributes. @@ -3786,8 +4403,10 @@ async def sample_list_attributes(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.services.api_hub.pagers.ListAttributesAsyncPager: @@ -3801,7 +4420,10 @@ async def sample_list_attributes(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([parent]) + flattened_params = [parent] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -3863,7 +4485,7 @@ async def search_resources( query: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> pagers.SearchResourcesAsyncPager: r"""Search across API-Hub resources. @@ -3923,8 +4545,10 @@ async def sample_search_resources(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.services.api_hub.pagers.SearchResourcesAsyncPager: @@ -3939,7 +4563,10 @@ async def sample_search_resources(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([location, query]) + flattened_params = [location, query] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -4004,7 +4631,7 @@ async def create_external_api( external_api_id: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.ExternalApi: r"""Create an External API resource in the API hub. @@ -4063,13 +4690,13 @@ async def sample_create_external_api(): which will become the final component of the External API's resource name. This field is optional. - - If provided, the same will be used. The service will - throw an error if the specified id is already used by - another External API resource in the API hub. - - If not provided, a system generated id will be used. + - If provided, the same will be used. The service will + throw an error if the specified id is already used by + another External API resource in the API hub. + - If not provided, a system generated id will be used. This value should be 4-500 characters, and valid - characters are /[a-z][A-Z][0-9]-_/. + characters are /[a-z][A-Z][0-9]-\_/. This corresponds to the ``external_api_id`` field on the ``request`` instance; if ``request`` is provided, this @@ -4077,8 +4704,10 @@ async def sample_create_external_api(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.ExternalApi: @@ -4091,7 +4720,10 @@ async def sample_create_external_api(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([parent, external_api, external_api_id]) + flattened_params = [parent, external_api, external_api_id] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -4145,7 +4777,7 @@ async def get_external_api( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.ExternalApi: r"""Get details about an External API resource in the API hub. @@ -4192,8 +4824,10 @@ async def sample_get_external_api(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.ExternalApi: @@ -4206,7 +4840,10 @@ async def sample_get_external_api(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -4257,16 +4894,16 @@ async def update_external_api( update_mask: Optional[field_mask_pb2.FieldMask] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.ExternalApi: r"""Update an External API resource in the API hub. The following fields can be updated: - - [display_name][google.cloud.apihub.v1.ExternalApi.display_name] - - [description][google.cloud.apihub.v1.ExternalApi.description] - - [documentation][google.cloud.apihub.v1.ExternalApi.documentation] - - [endpoints][google.cloud.apihub.v1.ExternalApi.endpoints] - - [paths][google.cloud.apihub.v1.ExternalApi.paths] + - [display_name][google.cloud.apihub.v1.ExternalApi.display_name] + - [description][google.cloud.apihub.v1.ExternalApi.description] + - [documentation][google.cloud.apihub.v1.ExternalApi.documentation] + - [endpoints][google.cloud.apihub.v1.ExternalApi.endpoints] + - [paths][google.cloud.apihub.v1.ExternalApi.paths] The [update_mask][google.cloud.apihub.v1.UpdateExternalApiRequest.update_mask] @@ -4326,8 +4963,10 @@ async def sample_update_external_api(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.ExternalApi: @@ -4340,7 +4979,10 @@ async def sample_update_external_api(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([external_api, update_mask]) + flattened_params = [external_api, update_mask] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -4394,7 +5036,7 @@ async def delete_external_api( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Delete an External API resource in the API hub. @@ -4437,13 +5079,18 @@ async def sample_delete_external_api(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. """ # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -4490,7 +5137,7 @@ async def list_external_apis( parent: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> pagers.ListExternalApisAsyncPager: r"""List External API resources in the API hub. @@ -4537,8 +5184,10 @@ async def sample_list_external_apis(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.services.api_hub.pagers.ListExternalApisAsyncPager: @@ -4552,7 +5201,10 @@ async def sample_list_external_apis(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([parent]) + flattened_params = [parent] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -4612,7 +5264,7 @@ async def list_operations( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> operations_pb2.ListOperationsResponse: r"""Lists operations that match the specified filter in the request. @@ -4623,8 +5275,10 @@ async def list_operations( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.operations_pb2.ListOperationsResponse: Response message for ``ListOperations`` method. @@ -4637,11 +5291,7 @@ async def list_operations( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.list_operations, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.list_operations] # Certain fields should be provided within the metadata header; # add these here. @@ -4669,7 +5319,7 @@ async def get_operation( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> operations_pb2.Operation: r"""Gets the latest state of a long-running operation. @@ -4680,8 +5330,10 @@ async def get_operation( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.operations_pb2.Operation: An ``Operation`` object. @@ -4694,11 +5346,7 @@ async def get_operation( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.get_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.get_operation] # Certain fields should be provided within the metadata header; # add these here. @@ -4726,7 +5374,7 @@ async def delete_operation( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Deletes a long-running operation. @@ -4742,8 +5390,10 @@ async def delete_operation( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: None """ @@ -4755,11 +5405,7 @@ async def delete_operation( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.delete_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.delete_operation] # Certain fields should be provided within the metadata header; # add these here. @@ -4784,7 +5430,7 @@ async def cancel_operation( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Starts asynchronous cancellation on a long-running operation. @@ -4799,8 +5445,10 @@ async def cancel_operation( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: None """ @@ -4812,11 +5460,7 @@ async def cancel_operation( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.cancel_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.cancel_operation] # Certain fields should be provided within the metadata header; # add these here. @@ -4841,7 +5485,7 @@ async def get_location( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> locations_pb2.Location: r"""Gets information about a location. @@ -4852,8 +5496,10 @@ async def get_location( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.location_pb2.Location: Location object. @@ -4866,11 +5512,7 @@ async def get_location( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.get_location, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.get_location] # Certain fields should be provided within the metadata header; # add these here. @@ -4898,7 +5540,7 @@ async def list_locations( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> locations_pb2.ListLocationsResponse: r"""Lists information about the supported locations for this service. @@ -4909,8 +5551,10 @@ async def list_locations( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.location_pb2.ListLocationsResponse: Response message for ``ListLocations`` method. @@ -4923,11 +5567,7 @@ async def list_locations( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.list_locations, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.list_locations] # Certain fields should be provided within the metadata header; # add these here. @@ -4960,5 +5600,8 @@ async def __aexit__(self, exc_type, exc, tb): gapic_version=package_version.__version__ ) +if hasattr(DEFAULT_CLIENT_INFO, "protobuf_runtime_version"): # pragma: NO COVER + DEFAULT_CLIENT_INFO.protobuf_runtime_version = google.protobuf.__version__ + __all__ = ("ApiHubAsyncClient",) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/client.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/client.py index e2452353246b..2734cc892f41 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/client.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/client.py @@ -70,6 +70,8 @@ from google.cloud.apihub_v1.types import apihub_service, common_fields from .transports.base import DEFAULT_CLIENT_INFO, ApiHubTransport +from .transports.grpc import ApiHubGrpcTransport +from .transports.grpc_asyncio import ApiHubGrpcAsyncIOTransport from .transports.rest import ApiHubRestTransport @@ -82,6 +84,8 @@ class ApiHubClientMeta(type): """ _transport_registry = OrderedDict() # type: Dict[str, Type[ApiHubTransport]] + _transport_registry["grpc"] = ApiHubGrpcTransport + _transport_registry["grpc_asyncio"] = ApiHubGrpcAsyncIOTransport _transport_registry["rest"] = ApiHubRestTransport def get_transport_class( diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/pagers.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/pagers.py index 909b27b7a245..9b6140febab2 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/pagers.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/pagers.py @@ -117,6 +117,86 @@ def __repr__(self) -> str: return "{0}<{1!r}>".format(self.__class__.__name__, self._response) +class ListApisAsyncPager: + """A pager for iterating through ``list_apis`` requests. + + This class thinly wraps an initial + :class:`google.cloud.apihub_v1.types.ListApisResponse` object, and + provides an ``__aiter__`` method to iterate through its + ``apis`` field. + + If there are more pages, the ``__aiter__`` method will make additional + ``ListApis`` requests and continue to iterate + through the ``apis`` field on the + corresponding responses. + + All the usual :class:`google.cloud.apihub_v1.types.ListApisResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., Awaitable[apihub_service.ListApisResponse]], + request: apihub_service.ListApisRequest, + response: apihub_service.ListApisResponse, + *, + retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = () + ): + """Instantiates the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.apihub_v1.types.ListApisRequest): + The initial request object. + response (google.cloud.apihub_v1.types.ListApisResponse): + The initial response object. + retry (google.api_core.retry.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + """ + self._method = method + self._request = apihub_service.ListApisRequest(request) + self._response = response + self._retry = retry + self._timeout = timeout + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + async def pages(self) -> AsyncIterator[apihub_service.ListApisResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = await self._method( + self._request, + retry=self._retry, + timeout=self._timeout, + metadata=self._metadata, + ) + yield self._response + + def __aiter__(self) -> AsyncIterator[common_fields.Api]: + async def async_generator(): + async for page in self.pages: + for response in page.apis: + yield response + + return async_generator() + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + class ListVersionsPager: """A pager for iterating through ``list_versions`` requests. @@ -145,16 +225,404 @@ def __init__( timeout: Union[float, object] = gapic_v1.method.DEFAULT, metadata: Sequence[Tuple[str, Union[str, bytes]]] = () ): - """Instantiate the pager. + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.apihub_v1.types.ListVersionsRequest): + The initial request object. + response (google.cloud.apihub_v1.types.ListVersionsResponse): + The initial response object. + 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, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + """ + self._method = method + self._request = apihub_service.ListVersionsRequest(request) + self._response = response + self._retry = retry + self._timeout = timeout + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterator[apihub_service.ListVersionsResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method( + self._request, + retry=self._retry, + timeout=self._timeout, + metadata=self._metadata, + ) + yield self._response + + def __iter__(self) -> Iterator[common_fields.Version]: + for page in self.pages: + yield from page.versions + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListVersionsAsyncPager: + """A pager for iterating through ``list_versions`` requests. + + This class thinly wraps an initial + :class:`google.cloud.apihub_v1.types.ListVersionsResponse` object, and + provides an ``__aiter__`` method to iterate through its + ``versions`` field. + + If there are more pages, the ``__aiter__`` method will make additional + ``ListVersions`` requests and continue to iterate + through the ``versions`` field on the + corresponding responses. + + All the usual :class:`google.cloud.apihub_v1.types.ListVersionsResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., Awaitable[apihub_service.ListVersionsResponse]], + request: apihub_service.ListVersionsRequest, + response: apihub_service.ListVersionsResponse, + *, + retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = () + ): + """Instantiates the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.apihub_v1.types.ListVersionsRequest): + The initial request object. + response (google.cloud.apihub_v1.types.ListVersionsResponse): + The initial response object. + retry (google.api_core.retry.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + """ + self._method = method + self._request = apihub_service.ListVersionsRequest(request) + self._response = response + self._retry = retry + self._timeout = timeout + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + async def pages(self) -> AsyncIterator[apihub_service.ListVersionsResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = await self._method( + self._request, + retry=self._retry, + timeout=self._timeout, + metadata=self._metadata, + ) + yield self._response + + def __aiter__(self) -> AsyncIterator[common_fields.Version]: + async def async_generator(): + async for page in self.pages: + for response in page.versions: + yield response + + return async_generator() + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListSpecsPager: + """A pager for iterating through ``list_specs`` requests. + + This class thinly wraps an initial + :class:`google.cloud.apihub_v1.types.ListSpecsResponse` object, and + provides an ``__iter__`` method to iterate through its + ``specs`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``ListSpecs`` requests and continue to iterate + through the ``specs`` field on the + corresponding responses. + + All the usual :class:`google.cloud.apihub_v1.types.ListSpecsResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., apihub_service.ListSpecsResponse], + request: apihub_service.ListSpecsRequest, + response: apihub_service.ListSpecsResponse, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.apihub_v1.types.ListSpecsRequest): + The initial request object. + response (google.cloud.apihub_v1.types.ListSpecsResponse): + The initial response object. + 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, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + """ + self._method = method + self._request = apihub_service.ListSpecsRequest(request) + self._response = response + self._retry = retry + self._timeout = timeout + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterator[apihub_service.ListSpecsResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method( + self._request, + retry=self._retry, + timeout=self._timeout, + metadata=self._metadata, + ) + yield self._response + + def __iter__(self) -> Iterator[common_fields.Spec]: + for page in self.pages: + yield from page.specs + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListSpecsAsyncPager: + """A pager for iterating through ``list_specs`` requests. + + This class thinly wraps an initial + :class:`google.cloud.apihub_v1.types.ListSpecsResponse` object, and + provides an ``__aiter__`` method to iterate through its + ``specs`` field. + + If there are more pages, the ``__aiter__`` method will make additional + ``ListSpecs`` requests and continue to iterate + through the ``specs`` field on the + corresponding responses. + + All the usual :class:`google.cloud.apihub_v1.types.ListSpecsResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., Awaitable[apihub_service.ListSpecsResponse]], + request: apihub_service.ListSpecsRequest, + response: apihub_service.ListSpecsResponse, + *, + retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = () + ): + """Instantiates the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.apihub_v1.types.ListSpecsRequest): + The initial request object. + response (google.cloud.apihub_v1.types.ListSpecsResponse): + The initial response object. + retry (google.api_core.retry.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + """ + self._method = method + self._request = apihub_service.ListSpecsRequest(request) + self._response = response + self._retry = retry + self._timeout = timeout + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + async def pages(self) -> AsyncIterator[apihub_service.ListSpecsResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = await self._method( + self._request, + retry=self._retry, + timeout=self._timeout, + metadata=self._metadata, + ) + yield self._response + + def __aiter__(self) -> AsyncIterator[common_fields.Spec]: + async def async_generator(): + async for page in self.pages: + for response in page.specs: + yield response + + return async_generator() + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListApiOperationsPager: + """A pager for iterating through ``list_api_operations`` requests. + + This class thinly wraps an initial + :class:`google.cloud.apihub_v1.types.ListApiOperationsResponse` object, and + provides an ``__iter__`` method to iterate through its + ``api_operations`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``ListApiOperations`` requests and continue to iterate + through the ``api_operations`` field on the + corresponding responses. + + All the usual :class:`google.cloud.apihub_v1.types.ListApiOperationsResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., apihub_service.ListApiOperationsResponse], + request: apihub_service.ListApiOperationsRequest, + response: apihub_service.ListApiOperationsResponse, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.apihub_v1.types.ListApiOperationsRequest): + The initial request object. + response (google.cloud.apihub_v1.types.ListApiOperationsResponse): + The initial response object. + 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, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + """ + self._method = method + self._request = apihub_service.ListApiOperationsRequest(request) + self._response = response + self._retry = retry + self._timeout = timeout + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterator[apihub_service.ListApiOperationsResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method( + self._request, + retry=self._retry, + timeout=self._timeout, + metadata=self._metadata, + ) + yield self._response + + def __iter__(self) -> Iterator[common_fields.ApiOperation]: + for page in self.pages: + yield from page.api_operations + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListApiOperationsAsyncPager: + """A pager for iterating through ``list_api_operations`` requests. + + This class thinly wraps an initial + :class:`google.cloud.apihub_v1.types.ListApiOperationsResponse` object, and + provides an ``__aiter__`` method to iterate through its + ``api_operations`` field. + + If there are more pages, the ``__aiter__`` method will make additional + ``ListApiOperations`` requests and continue to iterate + through the ``api_operations`` field on the + corresponding responses. + + All the usual :class:`google.cloud.apihub_v1.types.ListApiOperationsResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., Awaitable[apihub_service.ListApiOperationsResponse]], + request: apihub_service.ListApiOperationsRequest, + response: apihub_service.ListApiOperationsResponse, + *, + retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = () + ): + """Instantiates the pager. Args: method (Callable): The method that was originally called, and which instantiated this pager. - request (google.cloud.apihub_v1.types.ListVersionsRequest): + request (google.cloud.apihub_v1.types.ListApiOperationsRequest): The initial request object. - response (google.cloud.apihub_v1.types.ListVersionsResponse): + response (google.cloud.apihub_v1.types.ListApiOperationsResponse): The initial response object. - retry (google.api_core.retry.Retry): Designation of what errors, + retry (google.api_core.retry.AsyncRetry): Designation of what errors, if any, should be retried. timeout (float): The timeout for this request. metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be @@ -163,7 +631,7 @@ def __init__( be of type `bytes`. """ self._method = method - self._request = apihub_service.ListVersionsRequest(request) + self._request = apihub_service.ListApiOperationsRequest(request) self._response = response self._retry = retry self._timeout = timeout @@ -173,11 +641,11 @@ def __getattr__(self, name: str) -> Any: return getattr(self._response, name) @property - def pages(self) -> Iterator[apihub_service.ListVersionsResponse]: + async def pages(self) -> AsyncIterator[apihub_service.ListApiOperationsResponse]: yield self._response while self._response.next_page_token: self._request.page_token = self._response.next_page_token - self._response = self._method( + self._response = await self._method( self._request, retry=self._retry, timeout=self._timeout, @@ -185,37 +653,41 @@ def pages(self) -> Iterator[apihub_service.ListVersionsResponse]: ) yield self._response - def __iter__(self) -> Iterator[common_fields.Version]: - for page in self.pages: - yield from page.versions + def __aiter__(self) -> AsyncIterator[common_fields.ApiOperation]: + async def async_generator(): + async for page in self.pages: + for response in page.api_operations: + yield response + + return async_generator() def __repr__(self) -> str: return "{0}<{1!r}>".format(self.__class__.__name__, self._response) -class ListSpecsPager: - """A pager for iterating through ``list_specs`` requests. +class ListDeploymentsPager: + """A pager for iterating through ``list_deployments`` requests. This class thinly wraps an initial - :class:`google.cloud.apihub_v1.types.ListSpecsResponse` object, and + :class:`google.cloud.apihub_v1.types.ListDeploymentsResponse` object, and provides an ``__iter__`` method to iterate through its - ``specs`` field. + ``deployments`` field. If there are more pages, the ``__iter__`` method will make additional - ``ListSpecs`` requests and continue to iterate - through the ``specs`` field on the + ``ListDeployments`` requests and continue to iterate + through the ``deployments`` field on the corresponding responses. - All the usual :class:`google.cloud.apihub_v1.types.ListSpecsResponse` + All the usual :class:`google.cloud.apihub_v1.types.ListDeploymentsResponse` attributes are available on the pager. If multiple requests are made, only the most recent response is retained, and thus used for attribute lookup. """ def __init__( self, - method: Callable[..., apihub_service.ListSpecsResponse], - request: apihub_service.ListSpecsRequest, - response: apihub_service.ListSpecsResponse, + method: Callable[..., apihub_service.ListDeploymentsResponse], + request: apihub_service.ListDeploymentsRequest, + response: apihub_service.ListDeploymentsResponse, *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, @@ -226,9 +698,9 @@ def __init__( Args: method (Callable): The method that was originally called, and which instantiated this pager. - request (google.cloud.apihub_v1.types.ListSpecsRequest): + request (google.cloud.apihub_v1.types.ListDeploymentsRequest): The initial request object. - response (google.cloud.apihub_v1.types.ListSpecsResponse): + response (google.cloud.apihub_v1.types.ListDeploymentsResponse): The initial response object. retry (google.api_core.retry.Retry): Designation of what errors, if any, should be retried. @@ -239,7 +711,7 @@ def __init__( be of type `bytes`. """ self._method = method - self._request = apihub_service.ListSpecsRequest(request) + self._request = apihub_service.ListDeploymentsRequest(request) self._response = response self._retry = retry self._timeout = timeout @@ -249,7 +721,7 @@ def __getattr__(self, name: str) -> Any: return getattr(self._response, name) @property - def pages(self) -> Iterator[apihub_service.ListSpecsResponse]: + def pages(self) -> Iterator[apihub_service.ListDeploymentsResponse]: yield self._response while self._response.next_page_token: self._request.page_token = self._response.next_page_token @@ -261,52 +733,52 @@ def pages(self) -> Iterator[apihub_service.ListSpecsResponse]: ) yield self._response - def __iter__(self) -> Iterator[common_fields.Spec]: + def __iter__(self) -> Iterator[common_fields.Deployment]: for page in self.pages: - yield from page.specs + yield from page.deployments def __repr__(self) -> str: return "{0}<{1!r}>".format(self.__class__.__name__, self._response) -class ListApiOperationsPager: - """A pager for iterating through ``list_api_operations`` requests. +class ListDeploymentsAsyncPager: + """A pager for iterating through ``list_deployments`` requests. This class thinly wraps an initial - :class:`google.cloud.apihub_v1.types.ListApiOperationsResponse` object, and - provides an ``__iter__`` method to iterate through its - ``api_operations`` field. + :class:`google.cloud.apihub_v1.types.ListDeploymentsResponse` object, and + provides an ``__aiter__`` method to iterate through its + ``deployments`` field. - If there are more pages, the ``__iter__`` method will make additional - ``ListApiOperations`` requests and continue to iterate - through the ``api_operations`` field on the + If there are more pages, the ``__aiter__`` method will make additional + ``ListDeployments`` requests and continue to iterate + through the ``deployments`` field on the corresponding responses. - All the usual :class:`google.cloud.apihub_v1.types.ListApiOperationsResponse` + All the usual :class:`google.cloud.apihub_v1.types.ListDeploymentsResponse` attributes are available on the pager. If multiple requests are made, only the most recent response is retained, and thus used for attribute lookup. """ def __init__( self, - method: Callable[..., apihub_service.ListApiOperationsResponse], - request: apihub_service.ListApiOperationsRequest, - response: apihub_service.ListApiOperationsResponse, + method: Callable[..., Awaitable[apihub_service.ListDeploymentsResponse]], + request: apihub_service.ListDeploymentsRequest, + response: apihub_service.ListDeploymentsResponse, *, - retry: OptionalRetry = gapic_v1.method.DEFAULT, + retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, metadata: Sequence[Tuple[str, Union[str, bytes]]] = () ): - """Instantiate the pager. + """Instantiates the pager. Args: method (Callable): The method that was originally called, and which instantiated this pager. - request (google.cloud.apihub_v1.types.ListApiOperationsRequest): + request (google.cloud.apihub_v1.types.ListDeploymentsRequest): The initial request object. - response (google.cloud.apihub_v1.types.ListApiOperationsResponse): + response (google.cloud.apihub_v1.types.ListDeploymentsResponse): The initial response object. - retry (google.api_core.retry.Retry): Designation of what errors, + retry (google.api_core.retry.AsyncRetry): Designation of what errors, if any, should be retried. timeout (float): The timeout for this request. metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be @@ -315,7 +787,7 @@ def __init__( be of type `bytes`. """ self._method = method - self._request = apihub_service.ListApiOperationsRequest(request) + self._request = apihub_service.ListDeploymentsRequest(request) self._response = response self._retry = retry self._timeout = timeout @@ -325,11 +797,11 @@ def __getattr__(self, name: str) -> Any: return getattr(self._response, name) @property - def pages(self) -> Iterator[apihub_service.ListApiOperationsResponse]: + async def pages(self) -> AsyncIterator[apihub_service.ListDeploymentsResponse]: yield self._response while self._response.next_page_token: self._request.page_token = self._response.next_page_token - self._response = self._method( + self._response = await self._method( self._request, retry=self._retry, timeout=self._timeout, @@ -337,37 +809,41 @@ def pages(self) -> Iterator[apihub_service.ListApiOperationsResponse]: ) yield self._response - def __iter__(self) -> Iterator[common_fields.ApiOperation]: - for page in self.pages: - yield from page.api_operations + def __aiter__(self) -> AsyncIterator[common_fields.Deployment]: + async def async_generator(): + async for page in self.pages: + for response in page.deployments: + yield response + + return async_generator() def __repr__(self) -> str: return "{0}<{1!r}>".format(self.__class__.__name__, self._response) -class ListDeploymentsPager: - """A pager for iterating through ``list_deployments`` requests. +class ListAttributesPager: + """A pager for iterating through ``list_attributes`` requests. This class thinly wraps an initial - :class:`google.cloud.apihub_v1.types.ListDeploymentsResponse` object, and + :class:`google.cloud.apihub_v1.types.ListAttributesResponse` object, and provides an ``__iter__`` method to iterate through its - ``deployments`` field. + ``attributes`` field. If there are more pages, the ``__iter__`` method will make additional - ``ListDeployments`` requests and continue to iterate - through the ``deployments`` field on the + ``ListAttributes`` requests and continue to iterate + through the ``attributes`` field on the corresponding responses. - All the usual :class:`google.cloud.apihub_v1.types.ListDeploymentsResponse` + All the usual :class:`google.cloud.apihub_v1.types.ListAttributesResponse` attributes are available on the pager. If multiple requests are made, only the most recent response is retained, and thus used for attribute lookup. """ def __init__( self, - method: Callable[..., apihub_service.ListDeploymentsResponse], - request: apihub_service.ListDeploymentsRequest, - response: apihub_service.ListDeploymentsResponse, + method: Callable[..., apihub_service.ListAttributesResponse], + request: apihub_service.ListAttributesRequest, + response: apihub_service.ListAttributesResponse, *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, @@ -378,9 +854,9 @@ def __init__( Args: method (Callable): The method that was originally called, and which instantiated this pager. - request (google.cloud.apihub_v1.types.ListDeploymentsRequest): + request (google.cloud.apihub_v1.types.ListAttributesRequest): The initial request object. - response (google.cloud.apihub_v1.types.ListDeploymentsResponse): + response (google.cloud.apihub_v1.types.ListAttributesResponse): The initial response object. retry (google.api_core.retry.Retry): Designation of what errors, if any, should be retried. @@ -391,7 +867,7 @@ def __init__( be of type `bytes`. """ self._method = method - self._request = apihub_service.ListDeploymentsRequest(request) + self._request = apihub_service.ListAttributesRequest(request) self._response = response self._retry = retry self._timeout = timeout @@ -401,7 +877,7 @@ def __getattr__(self, name: str) -> Any: return getattr(self._response, name) @property - def pages(self) -> Iterator[apihub_service.ListDeploymentsResponse]: + def pages(self) -> Iterator[apihub_service.ListAttributesResponse]: yield self._response while self._response.next_page_token: self._request.page_token = self._response.next_page_token @@ -413,23 +889,23 @@ def pages(self) -> Iterator[apihub_service.ListDeploymentsResponse]: ) yield self._response - def __iter__(self) -> Iterator[common_fields.Deployment]: + def __iter__(self) -> Iterator[common_fields.Attribute]: for page in self.pages: - yield from page.deployments + yield from page.attributes def __repr__(self) -> str: return "{0}<{1!r}>".format(self.__class__.__name__, self._response) -class ListAttributesPager: +class ListAttributesAsyncPager: """A pager for iterating through ``list_attributes`` requests. This class thinly wraps an initial :class:`google.cloud.apihub_v1.types.ListAttributesResponse` object, and - provides an ``__iter__`` method to iterate through its + provides an ``__aiter__`` method to iterate through its ``attributes`` field. - If there are more pages, the ``__iter__`` method will make additional + If there are more pages, the ``__aiter__`` method will make additional ``ListAttributes`` requests and continue to iterate through the ``attributes`` field on the corresponding responses. @@ -441,15 +917,15 @@ class ListAttributesPager: def __init__( self, - method: Callable[..., apihub_service.ListAttributesResponse], + method: Callable[..., Awaitable[apihub_service.ListAttributesResponse]], request: apihub_service.ListAttributesRequest, response: apihub_service.ListAttributesResponse, *, - retry: OptionalRetry = gapic_v1.method.DEFAULT, + retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, metadata: Sequence[Tuple[str, Union[str, bytes]]] = () ): - """Instantiate the pager. + """Instantiates the pager. Args: method (Callable): The method that was originally called, and @@ -458,7 +934,7 @@ def __init__( The initial request object. response (google.cloud.apihub_v1.types.ListAttributesResponse): The initial response object. - retry (google.api_core.retry.Retry): Designation of what errors, + retry (google.api_core.retry.AsyncRetry): Designation of what errors, if any, should be retried. timeout (float): The timeout for this request. metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be @@ -477,11 +953,11 @@ def __getattr__(self, name: str) -> Any: return getattr(self._response, name) @property - def pages(self) -> Iterator[apihub_service.ListAttributesResponse]: + async def pages(self) -> AsyncIterator[apihub_service.ListAttributesResponse]: yield self._response while self._response.next_page_token: self._request.page_token = self._response.next_page_token - self._response = self._method( + self._response = await self._method( self._request, retry=self._retry, timeout=self._timeout, @@ -489,9 +965,13 @@ def pages(self) -> Iterator[apihub_service.ListAttributesResponse]: ) yield self._response - def __iter__(self) -> Iterator[common_fields.Attribute]: - for page in self.pages: - yield from page.attributes + def __aiter__(self) -> AsyncIterator[common_fields.Attribute]: + async def async_generator(): + async for page in self.pages: + for response in page.attributes: + yield response + + return async_generator() def __repr__(self) -> str: return "{0}<{1!r}>".format(self.__class__.__name__, self._response) @@ -573,6 +1053,86 @@ def __repr__(self) -> str: return "{0}<{1!r}>".format(self.__class__.__name__, self._response) +class SearchResourcesAsyncPager: + """A pager for iterating through ``search_resources`` requests. + + This class thinly wraps an initial + :class:`google.cloud.apihub_v1.types.SearchResourcesResponse` object, and + provides an ``__aiter__`` method to iterate through its + ``search_results`` field. + + If there are more pages, the ``__aiter__`` method will make additional + ``SearchResources`` requests and continue to iterate + through the ``search_results`` field on the + corresponding responses. + + All the usual :class:`google.cloud.apihub_v1.types.SearchResourcesResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., Awaitable[apihub_service.SearchResourcesResponse]], + request: apihub_service.SearchResourcesRequest, + response: apihub_service.SearchResourcesResponse, + *, + retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = () + ): + """Instantiates the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.apihub_v1.types.SearchResourcesRequest): + The initial request object. + response (google.cloud.apihub_v1.types.SearchResourcesResponse): + The initial response object. + retry (google.api_core.retry.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + """ + self._method = method + self._request = apihub_service.SearchResourcesRequest(request) + self._response = response + self._retry = retry + self._timeout = timeout + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + async def pages(self) -> AsyncIterator[apihub_service.SearchResourcesResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = await self._method( + self._request, + retry=self._retry, + timeout=self._timeout, + metadata=self._metadata, + ) + yield self._response + + def __aiter__(self) -> AsyncIterator[apihub_service.SearchResult]: + async def async_generator(): + async for page in self.pages: + for response in page.search_results: + yield response + + return async_generator() + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + class ListExternalApisPager: """A pager for iterating through ``list_external_apis`` requests. @@ -647,3 +1207,83 @@ def __iter__(self) -> Iterator[common_fields.ExternalApi]: def __repr__(self) -> str: return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListExternalApisAsyncPager: + """A pager for iterating through ``list_external_apis`` requests. + + This class thinly wraps an initial + :class:`google.cloud.apihub_v1.types.ListExternalApisResponse` object, and + provides an ``__aiter__`` method to iterate through its + ``external_apis`` field. + + If there are more pages, the ``__aiter__`` method will make additional + ``ListExternalApis`` requests and continue to iterate + through the ``external_apis`` field on the + corresponding responses. + + All the usual :class:`google.cloud.apihub_v1.types.ListExternalApisResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., Awaitable[apihub_service.ListExternalApisResponse]], + request: apihub_service.ListExternalApisRequest, + response: apihub_service.ListExternalApisResponse, + *, + retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = () + ): + """Instantiates the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.apihub_v1.types.ListExternalApisRequest): + The initial request object. + response (google.cloud.apihub_v1.types.ListExternalApisResponse): + The initial response object. + retry (google.api_core.retry.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + """ + self._method = method + self._request = apihub_service.ListExternalApisRequest(request) + self._response = response + self._retry = retry + self._timeout = timeout + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + async def pages(self) -> AsyncIterator[apihub_service.ListExternalApisResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = await self._method( + self._request, + retry=self._retry, + timeout=self._timeout, + metadata=self._metadata, + ) + yield self._response + + def __aiter__(self) -> AsyncIterator[common_fields.ExternalApi]: + async def async_generator(): + async for page in self.pages: + for response in page.external_apis: + yield response + + return async_generator() + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/transports/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/transports/__init__.py index a9d2b49c1f80..e960685a2fd3 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/transports/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/transports/__init__.py @@ -17,14 +17,20 @@ from typing import Dict, Type from .base import ApiHubTransport +from .grpc import ApiHubGrpcTransport +from .grpc_asyncio import ApiHubGrpcAsyncIOTransport from .rest import ApiHubRestInterceptor, ApiHubRestTransport # Compile a registry of transports. _transport_registry = OrderedDict() # type: Dict[str, Type[ApiHubTransport]] +_transport_registry["grpc"] = ApiHubGrpcTransport +_transport_registry["grpc_asyncio"] = ApiHubGrpcAsyncIOTransport _transport_registry["rest"] = ApiHubRestTransport __all__ = ( "ApiHubTransport", + "ApiHubGrpcTransport", + "ApiHubGrpcAsyncIOTransport", "ApiHubRestTransport", "ApiHubRestInterceptor", ) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/transports/grpc.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/transports/grpc.py index 81496577ce93..e489ec66cc6e 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/transports/grpc.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/transports/grpc.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,6 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import json +import logging as std_logging +import pickle from typing import Callable, Dict, Optional, Sequence, Tuple, Union import warnings @@ -23,12 +26,89 @@ from google.cloud.location import locations_pb2 # type: ignore from google.longrunning import operations_pb2 # type: ignore from google.protobuf import empty_pb2 # type: ignore +from google.protobuf.json_format import MessageToJson +import google.protobuf.message import grpc # type: ignore +import proto # type: ignore from google.cloud.apihub_v1.types import apihub_service, common_fields from .base import DEFAULT_CLIENT_INFO, ApiHubTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class _LoggingClientInterceptor(grpc.UnaryUnaryClientInterceptor): # pragma: NO COVER + def intercept_unary_unary(self, continuation, client_call_details, request): + logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ) + if logging_enabled: # pragma: NO COVER + request_metadata = client_call_details.metadata + if isinstance(request, proto.Message): + request_payload = type(request).to_json(request) + elif isinstance(request, google.protobuf.message.Message): + request_payload = MessageToJson(request) + else: + request_payload = f"{type(request).__name__}: {pickle.dumps(request)}" + + request_metadata = { + key: value.decode("utf-8") if isinstance(value, bytes) else value + for key, value in request_metadata + } + grpc_request = { + "payload": request_payload, + "requestMethod": "grpc", + "metadata": dict(request_metadata), + } + _LOGGER.debug( + f"Sending request for {client_call_details.method}", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHub", + "rpcName": str(client_call_details.method), + "request": grpc_request, + "metadata": grpc_request["metadata"], + }, + ) + response = continuation(client_call_details, request) + if logging_enabled: # pragma: NO COVER + response_metadata = response.trailing_metadata() + # Convert gRPC metadata `` to list of tuples + metadata = ( + dict([(k, str(v)) for k, v in response_metadata]) + if response_metadata + else None + ) + result = response.result() + if isinstance(result, proto.Message): + response_payload = type(result).to_json(result) + elif isinstance(result, google.protobuf.message.Message): + response_payload = MessageToJson(result) + else: + response_payload = f"{type(result).__name__}: {pickle.dumps(result)}" + grpc_response = { + "payload": response_payload, + "metadata": metadata, + "status": "OK", + } + _LOGGER.debug( + f"Received response for {client_call_details.method}.", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHub", + "rpcName": client_call_details.method, + "response": grpc_response, + "metadata": grpc_response["metadata"], + }, + ) + return response + class ApiHubGrpcTransport(ApiHubTransport): """gRPC backend transport for ApiHub. @@ -182,7 +262,12 @@ def __init__( ], ) - # Wrap messages. This must be done after self._grpc_channel exists + self._interceptor = _LoggingClientInterceptor() + self._logged_channel = grpc.intercept_channel( + self._grpc_channel, self._interceptor + ) + + # Wrap messages. This must be done after self._logged_channel exists self._prep_wrapped_messages(client_info) @classmethod @@ -258,7 +343,7 @@ def create_api( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "create_api" not in self._stubs: - self._stubs["create_api"] = self.grpc_channel.unary_unary( + self._stubs["create_api"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/CreateApi", request_serializer=apihub_service.CreateApiRequest.serialize, response_deserializer=common_fields.Api.deserialize, @@ -283,7 +368,7 @@ def get_api(self) -> Callable[[apihub_service.GetApiRequest], common_fields.Api] # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_api" not in self._stubs: - self._stubs["get_api"] = self.grpc_channel.unary_unary( + self._stubs["get_api"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/GetApi", request_serializer=apihub_service.GetApiRequest.serialize, response_deserializer=common_fields.Api.deserialize, @@ -309,7 +394,7 @@ def list_apis( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_apis" not in self._stubs: - self._stubs["list_apis"] = self.grpc_channel.unary_unary( + self._stubs["list_apis"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/ListApis", request_serializer=apihub_service.ListApisRequest.serialize, response_deserializer=apihub_service.ListApisResponse.deserialize, @@ -323,17 +408,18 @@ def update_api( r"""Return a callable for the update api method over gRPC. Update an API resource in the API hub. The following fields in - the [API][] can be updated: - - - [display_name][google.cloud.apihub.v1.Api.display_name] - - [description][google.cloud.apihub.v1.Api.description] - - [owner][google.cloud.apihub.v1.Api.owner] - - [documentation][google.cloud.apihub.v1.Api.documentation] - - [target_user][google.cloud.apihub.v1.Api.target_user] - - [team][google.cloud.apihub.v1.Api.team] - - [business_unit][google.cloud.apihub.v1.Api.business_unit] - - [maturity_level][google.cloud.apihub.v1.Api.maturity_level] - - [attributes][google.cloud.apihub.v1.Api.attributes] + the [API][google.cloud.apihub.v1.Api] can be updated: + + - [display_name][google.cloud.apihub.v1.Api.display_name] + - [description][google.cloud.apihub.v1.Api.description] + - [owner][google.cloud.apihub.v1.Api.owner] + - [documentation][google.cloud.apihub.v1.Api.documentation] + - [target_user][google.cloud.apihub.v1.Api.target_user] + - [team][google.cloud.apihub.v1.Api.team] + - [business_unit][google.cloud.apihub.v1.Api.business_unit] + - [maturity_level][google.cloud.apihub.v1.Api.maturity_level] + - [api_style][google.cloud.apihub.v1.Api.api_style] + - [attributes][google.cloud.apihub.v1.Api.attributes] The [update_mask][google.cloud.apihub.v1.UpdateApiRequest.update_mask] @@ -353,7 +439,7 @@ def update_api( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "update_api" not in self._stubs: - self._stubs["update_api"] = self.grpc_channel.unary_unary( + self._stubs["update_api"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/UpdateApi", request_serializer=apihub_service.UpdateApiRequest.serialize, response_deserializer=common_fields.Api.deserialize, @@ -380,7 +466,7 @@ def delete_api( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_api" not in self._stubs: - self._stubs["delete_api"] = self.grpc_channel.unary_unary( + self._stubs["delete_api"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/DeleteApi", request_serializer=apihub_service.DeleteApiRequest.serialize, response_deserializer=empty_pb2.Empty.FromString, @@ -407,7 +493,7 @@ def create_version( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "create_version" not in self._stubs: - self._stubs["create_version"] = self.grpc_channel.unary_unary( + self._stubs["create_version"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/CreateVersion", request_serializer=apihub_service.CreateVersionRequest.serialize, response_deserializer=common_fields.Version.deserialize, @@ -436,7 +522,7 @@ def get_version( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_version" not in self._stubs: - self._stubs["get_version"] = self.grpc_channel.unary_unary( + self._stubs["get_version"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/GetVersion", request_serializer=apihub_service.GetVersionRequest.serialize, response_deserializer=common_fields.Version.deserialize, @@ -464,7 +550,7 @@ def list_versions( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_versions" not in self._stubs: - self._stubs["list_versions"] = self.grpc_channel.unary_unary( + self._stubs["list_versions"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/ListVersions", request_serializer=apihub_service.ListVersionsRequest.serialize, response_deserializer=apihub_service.ListVersionsResponse.deserialize, @@ -481,14 +567,14 @@ def update_version( [version][google.cloud.apihub.v1.Version] can be updated currently: - - [display_name][google.cloud.apihub.v1.Version.display_name] - - [description][google.cloud.apihub.v1.Version.description] - - [documentation][google.cloud.apihub.v1.Version.documentation] - - [deployments][google.cloud.apihub.v1.Version.deployments] - - [lifecycle][google.cloud.apihub.v1.Version.lifecycle] - - [compliance][google.cloud.apihub.v1.Version.compliance] - - [accreditation][google.cloud.apihub.v1.Version.accreditation] - - [attributes][google.cloud.apihub.v1.Version.attributes] + - [display_name][google.cloud.apihub.v1.Version.display_name] + - [description][google.cloud.apihub.v1.Version.description] + - [documentation][google.cloud.apihub.v1.Version.documentation] + - [deployments][google.cloud.apihub.v1.Version.deployments] + - [lifecycle][google.cloud.apihub.v1.Version.lifecycle] + - [compliance][google.cloud.apihub.v1.Version.compliance] + - [accreditation][google.cloud.apihub.v1.Version.accreditation] + - [attributes][google.cloud.apihub.v1.Version.attributes] The [update_mask][google.cloud.apihub.v1.UpdateVersionRequest.update_mask] @@ -505,7 +591,7 @@ def update_version( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "update_version" not in self._stubs: - self._stubs["update_version"] = self.grpc_channel.unary_unary( + self._stubs["update_version"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/UpdateVersion", request_serializer=apihub_service.UpdateVersionRequest.serialize, response_deserializer=common_fields.Version.deserialize, @@ -533,7 +619,7 @@ def delete_version( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_version" not in self._stubs: - self._stubs["delete_version"] = self.grpc_channel.unary_unary( + self._stubs["delete_version"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/DeleteVersion", request_serializer=apihub_service.DeleteVersionRequest.serialize, response_deserializer=empty_pb2.Empty.FromString, @@ -581,7 +667,7 @@ def create_spec( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "create_spec" not in self._stubs: - self._stubs["create_spec"] = self.grpc_channel.unary_unary( + self._stubs["create_spec"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/CreateSpec", request_serializer=apihub_service.CreateSpecRequest.serialize, response_deserializer=common_fields.Spec.deserialize, @@ -608,7 +694,7 @@ def get_spec(self) -> Callable[[apihub_service.GetSpecRequest], common_fields.Sp # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_spec" not in self._stubs: - self._stubs["get_spec"] = self.grpc_channel.unary_unary( + self._stubs["get_spec"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/GetSpec", request_serializer=apihub_service.GetSpecRequest.serialize, response_deserializer=common_fields.Spec.deserialize, @@ -634,7 +720,7 @@ def get_spec_contents( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_spec_contents" not in self._stubs: - self._stubs["get_spec_contents"] = self.grpc_channel.unary_unary( + self._stubs["get_spec_contents"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/GetSpecContents", request_serializer=apihub_service.GetSpecContentsRequest.serialize, response_deserializer=common_fields.SpecContents.deserialize, @@ -661,7 +747,7 @@ def list_specs( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_specs" not in self._stubs: - self._stubs["list_specs"] = self.grpc_channel.unary_unary( + self._stubs["list_specs"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/ListSpecs", request_serializer=apihub_service.ListSpecsRequest.serialize, response_deserializer=apihub_service.ListSpecsResponse.deserialize, @@ -677,12 +763,12 @@ def update_spec( Update spec. The following fields in the [spec][google.cloud.apihub.v1.Spec] can be updated: - - [display_name][google.cloud.apihub.v1.Spec.display_name] - - [source_uri][google.cloud.apihub.v1.Spec.source_uri] - - [lint_response][google.cloud.apihub.v1.Spec.lint_response] - - [attributes][google.cloud.apihub.v1.Spec.attributes] - - [contents][google.cloud.apihub.v1.Spec.contents] - - [spec_type][google.cloud.apihub.v1.Spec.spec_type] + - [display_name][google.cloud.apihub.v1.Spec.display_name] + - [source_uri][google.cloud.apihub.v1.Spec.source_uri] + - [lint_response][google.cloud.apihub.v1.Spec.lint_response] + - [attributes][google.cloud.apihub.v1.Spec.attributes] + - [contents][google.cloud.apihub.v1.Spec.contents] + - [spec_type][google.cloud.apihub.v1.Spec.spec_type] In case of an OAS spec, updating spec contents can lead to: @@ -709,7 +795,7 @@ def update_spec( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "update_spec" not in self._stubs: - self._stubs["update_spec"] = self.grpc_channel.unary_unary( + self._stubs["update_spec"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/UpdateSpec", request_serializer=apihub_service.UpdateSpecRequest.serialize, response_deserializer=common_fields.Spec.deserialize, @@ -737,13 +823,43 @@ def delete_spec( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_spec" not in self._stubs: - self._stubs["delete_spec"] = self.grpc_channel.unary_unary( + self._stubs["delete_spec"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/DeleteSpec", request_serializer=apihub_service.DeleteSpecRequest.serialize, response_deserializer=empty_pb2.Empty.FromString, ) return self._stubs["delete_spec"] + @property + def create_api_operation( + self, + ) -> Callable[ + [apihub_service.CreateApiOperationRequest], common_fields.ApiOperation + ]: + r"""Return a callable for the create api operation method over gRPC. + + Create an apiOperation in an API version. + An apiOperation can be created only if the version has + no apiOperations which were created by parsing a spec. + + Returns: + Callable[[~.CreateApiOperationRequest], + ~.ApiOperation]: + 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 "create_api_operation" not in self._stubs: + self._stubs["create_api_operation"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHub/CreateApiOperation", + request_serializer=apihub_service.CreateApiOperationRequest.serialize, + response_deserializer=common_fields.ApiOperation.deserialize, + ) + return self._stubs["create_api_operation"] + @property def get_api_operation( self, @@ -764,7 +880,7 @@ def get_api_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_api_operation" not in self._stubs: - self._stubs["get_api_operation"] = self.grpc_channel.unary_unary( + self._stubs["get_api_operation"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/GetApiOperation", request_serializer=apihub_service.GetApiOperationRequest.serialize, response_deserializer=common_fields.ApiOperation.deserialize, @@ -793,13 +909,89 @@ def list_api_operations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_api_operations" not in self._stubs: - self._stubs["list_api_operations"] = self.grpc_channel.unary_unary( + self._stubs["list_api_operations"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/ListApiOperations", request_serializer=apihub_service.ListApiOperationsRequest.serialize, response_deserializer=apihub_service.ListApiOperationsResponse.deserialize, ) return self._stubs["list_api_operations"] + @property + def update_api_operation( + self, + ) -> Callable[ + [apihub_service.UpdateApiOperationRequest], common_fields.ApiOperation + ]: + r"""Return a callable for the update api operation method over gRPC. + + Update an operation in an API version. The following fields in + the [ApiOperation resource][google.cloud.apihub.v1.ApiOperation] + can be updated: + + - [details.description][ApiOperation.details.description] + - [details.documentation][ApiOperation.details.documentation] + - [details.http_operation.path][ApiOperation.details.http_operation.path.path] + - [details.http_operation.method][ApiOperation.details.http_operation.method] + - [details.deprecated][ApiOperation.details.deprecated] + - [attributes][google.cloud.apihub.v1.ApiOperation.attributes] + + The + [update_mask][google.cloud.apihub.v1.UpdateApiOperationRequest.update_mask] + should be used to specify the fields being updated. + + An operation can be updated only if the operation was created + via + [CreateApiOperation][google.cloud.apihub.v1.ApiHub.CreateApiOperation] + API. If the operation was created by parsing the spec, then it + can be edited by updating the spec. + + Returns: + Callable[[~.UpdateApiOperationRequest], + ~.ApiOperation]: + 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 "update_api_operation" not in self._stubs: + self._stubs["update_api_operation"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHub/UpdateApiOperation", + request_serializer=apihub_service.UpdateApiOperationRequest.serialize, + response_deserializer=common_fields.ApiOperation.deserialize, + ) + return self._stubs["update_api_operation"] + + @property + def delete_api_operation( + self, + ) -> Callable[[apihub_service.DeleteApiOperationRequest], empty_pb2.Empty]: + r"""Return a callable for the delete api operation method over gRPC. + + Delete an operation in an API version and we can + delete only the operations created via create API. If + the operation was created by parsing the spec, then it + can be deleted by editing or deleting the spec. + + Returns: + Callable[[~.DeleteApiOperationRequest], + ~.Empty]: + 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 "delete_api_operation" not in self._stubs: + self._stubs["delete_api_operation"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHub/DeleteApiOperation", + request_serializer=apihub_service.DeleteApiOperationRequest.serialize, + response_deserializer=empty_pb2.Empty.FromString, + ) + return self._stubs["delete_api_operation"] + @property def get_definition( self, @@ -819,7 +1011,7 @@ def get_definition( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_definition" not in self._stubs: - self._stubs["get_definition"] = self.grpc_channel.unary_unary( + self._stubs["get_definition"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/GetDefinition", request_serializer=apihub_service.GetDefinitionRequest.serialize, response_deserializer=common_fields.Definition.deserialize, @@ -847,7 +1039,7 @@ def create_deployment( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "create_deployment" not in self._stubs: - self._stubs["create_deployment"] = self.grpc_channel.unary_unary( + self._stubs["create_deployment"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/CreateDeployment", request_serializer=apihub_service.CreateDeploymentRequest.serialize, response_deserializer=common_fields.Deployment.deserialize, @@ -874,7 +1066,7 @@ def get_deployment( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_deployment" not in self._stubs: - self._stubs["get_deployment"] = self.grpc_channel.unary_unary( + self._stubs["get_deployment"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/GetDeployment", request_serializer=apihub_service.GetDeploymentRequest.serialize, response_deserializer=common_fields.Deployment.deserialize, @@ -902,7 +1094,7 @@ def list_deployments( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_deployments" not in self._stubs: - self._stubs["list_deployments"] = self.grpc_channel.unary_unary( + self._stubs["list_deployments"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/ListDeployments", request_serializer=apihub_service.ListDeploymentsRequest.serialize, response_deserializer=apihub_service.ListDeploymentsResponse.deserialize, @@ -919,19 +1111,23 @@ def update_deployment( fields in the [deployment resource][google.cloud.apihub.v1.Deployment] can be updated: - - [display_name][google.cloud.apihub.v1.Deployment.display_name] - - [description][google.cloud.apihub.v1.Deployment.description] - - [documentation][google.cloud.apihub.v1.Deployment.documentation] - - [deployment_type][google.cloud.apihub.v1.Deployment.deployment_type] - - [resource_uri][google.cloud.apihub.v1.Deployment.resource_uri] - - [endpoints][google.cloud.apihub.v1.Deployment.endpoints] - - [slo][google.cloud.apihub.v1.Deployment.slo] - - [environment][google.cloud.apihub.v1.Deployment.environment] - - [attributes][google.cloud.apihub.v1.Deployment.attributes] - - The - [update_mask][google.cloud.apihub.v1.UpdateDeploymentRequest.update_mask] - should be used to specify the fields being updated. + - [display_name][google.cloud.apihub.v1.Deployment.display_name] + - [description][google.cloud.apihub.v1.Deployment.description] + - [documentation][google.cloud.apihub.v1.Deployment.documentation] + - [deployment_type][google.cloud.apihub.v1.Deployment.deployment_type] + - [resource_uri][google.cloud.apihub.v1.Deployment.resource_uri] + - [endpoints][google.cloud.apihub.v1.Deployment.endpoints] + - [slo][google.cloud.apihub.v1.Deployment.slo] + - [environment][google.cloud.apihub.v1.Deployment.environment] + - [attributes][google.cloud.apihub.v1.Deployment.attributes] + - [source_project] + [google.cloud.apihub.v1.Deployment.source_project] + - [source_environment] + [google.cloud.apihub.v1.Deployment.source_environment] + - [management_url][google.cloud.apihub.v1.Deployment.management_url] + - [source_uri][google.cloud.apihub.v1.Deployment.source_uri] The + [update_mask][google.cloud.apihub.v1.UpdateDeploymentRequest.update_mask] + should be used to specify the fields being updated. Returns: Callable[[~.UpdateDeploymentRequest], @@ -944,7 +1140,7 @@ def update_deployment( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "update_deployment" not in self._stubs: - self._stubs["update_deployment"] = self.grpc_channel.unary_unary( + self._stubs["update_deployment"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/UpdateDeployment", request_serializer=apihub_service.UpdateDeploymentRequest.serialize, response_deserializer=common_fields.Deployment.deserialize, @@ -970,7 +1166,7 @@ def delete_deployment( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_deployment" not in self._stubs: - self._stubs["delete_deployment"] = self.grpc_channel.unary_unary( + self._stubs["delete_deployment"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/DeleteDeployment", request_serializer=apihub_service.DeleteDeploymentRequest.serialize, response_deserializer=empty_pb2.Empty.FromString, @@ -1004,7 +1200,7 @@ def create_attribute( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "create_attribute" not in self._stubs: - self._stubs["create_attribute"] = self.grpc_channel.unary_unary( + self._stubs["create_attribute"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/CreateAttribute", request_serializer=apihub_service.CreateAttributeRequest.serialize, response_deserializer=common_fields.Attribute.deserialize, @@ -1030,7 +1226,7 @@ def get_attribute( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_attribute" not in self._stubs: - self._stubs["get_attribute"] = self.grpc_channel.unary_unary( + self._stubs["get_attribute"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/GetAttribute", request_serializer=apihub_service.GetAttributeRequest.serialize, response_deserializer=common_fields.Attribute.deserialize, @@ -1046,25 +1242,25 @@ def update_attribute( Update the attribute. The following fields in the [Attribute resource][google.cloud.apihub.v1.Attribute] can be updated: - - [display_name][google.cloud.apihub.v1.Attribute.display_name] - The display name can be updated for user defined attributes - only. - - [description][google.cloud.apihub.v1.Attribute.description] - The description can be updated for user defined attributes - only. - - [allowed_values][google.cloud.apihub.v1.Attribute.allowed_values] - To update the list of allowed values, clients need to use the - fetched list of allowed values and add or remove values to or - from the same list. The mutable allowed values can be updated - for both user defined and System defined attributes. The - immutable allowed values cannot be updated or deleted. The - updated list of allowed values cannot be empty. If an allowed - value that is already used by some resource's attribute is - deleted, then the association between the resource and the - attribute value will also be deleted. - - [cardinality][google.cloud.apihub.v1.Attribute.cardinality] - The cardinality can be updated for user defined attributes - only. Cardinality can only be increased during an update. + - [display_name][google.cloud.apihub.v1.Attribute.display_name] + The display name can be updated for user defined attributes + only. + - [description][google.cloud.apihub.v1.Attribute.description] + The description can be updated for user defined attributes + only. + - [allowed_values][google.cloud.apihub.v1.Attribute.allowed_values] + To update the list of allowed values, clients need to use the + fetched list of allowed values and add or remove values to or + from the same list. The mutable allowed values can be updated + for both user defined and System defined attributes. The + immutable allowed values cannot be updated or deleted. The + updated list of allowed values cannot be empty. If an allowed + value that is already used by some resource's attribute is + deleted, then the association between the resource and the + attribute value will also be deleted. + - [cardinality][google.cloud.apihub.v1.Attribute.cardinality] + The cardinality can be updated for user defined attributes + only. Cardinality can only be increased during an update. The [update_mask][google.cloud.apihub.v1.UpdateAttributeRequest.update_mask] @@ -1081,7 +1277,7 @@ def update_attribute( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "update_attribute" not in self._stubs: - self._stubs["update_attribute"] = self.grpc_channel.unary_unary( + self._stubs["update_attribute"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/UpdateAttribute", request_serializer=apihub_service.UpdateAttributeRequest.serialize, response_deserializer=common_fields.Attribute.deserialize, @@ -1111,7 +1307,7 @@ def delete_attribute( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_attribute" not in self._stubs: - self._stubs["delete_attribute"] = self.grpc_channel.unary_unary( + self._stubs["delete_attribute"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/DeleteAttribute", request_serializer=apihub_service.DeleteAttributeRequest.serialize, response_deserializer=empty_pb2.Empty.FromString, @@ -1139,7 +1335,7 @@ def list_attributes( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_attributes" not in self._stubs: - self._stubs["list_attributes"] = self.grpc_channel.unary_unary( + self._stubs["list_attributes"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/ListAttributes", request_serializer=apihub_service.ListAttributesRequest.serialize, response_deserializer=apihub_service.ListAttributesResponse.deserialize, @@ -1167,7 +1363,7 @@ def search_resources( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "search_resources" not in self._stubs: - self._stubs["search_resources"] = self.grpc_channel.unary_unary( + self._stubs["search_resources"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/SearchResources", request_serializer=apihub_service.SearchResourcesRequest.serialize, response_deserializer=apihub_service.SearchResourcesResponse.deserialize, @@ -1193,7 +1389,7 @@ def create_external_api( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "create_external_api" not in self._stubs: - self._stubs["create_external_api"] = self.grpc_channel.unary_unary( + self._stubs["create_external_api"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/CreateExternalApi", request_serializer=apihub_service.CreateExternalApiRequest.serialize, response_deserializer=common_fields.ExternalApi.deserialize, @@ -1220,7 +1416,7 @@ def get_external_api( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_external_api" not in self._stubs: - self._stubs["get_external_api"] = self.grpc_channel.unary_unary( + self._stubs["get_external_api"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/GetExternalApi", request_serializer=apihub_service.GetExternalApiRequest.serialize, response_deserializer=common_fields.ExternalApi.deserialize, @@ -1236,11 +1432,11 @@ def update_external_api( Update an External API resource in the API hub. The following fields can be updated: - - [display_name][google.cloud.apihub.v1.ExternalApi.display_name] - - [description][google.cloud.apihub.v1.ExternalApi.description] - - [documentation][google.cloud.apihub.v1.ExternalApi.documentation] - - [endpoints][google.cloud.apihub.v1.ExternalApi.endpoints] - - [paths][google.cloud.apihub.v1.ExternalApi.paths] + - [display_name][google.cloud.apihub.v1.ExternalApi.display_name] + - [description][google.cloud.apihub.v1.ExternalApi.description] + - [documentation][google.cloud.apihub.v1.ExternalApi.documentation] + - [endpoints][google.cloud.apihub.v1.ExternalApi.endpoints] + - [paths][google.cloud.apihub.v1.ExternalApi.paths] The [update_mask][google.cloud.apihub.v1.UpdateExternalApiRequest.update_mask] @@ -1257,7 +1453,7 @@ def update_external_api( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "update_external_api" not in self._stubs: - self._stubs["update_external_api"] = self.grpc_channel.unary_unary( + self._stubs["update_external_api"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/UpdateExternalApi", request_serializer=apihub_service.UpdateExternalApiRequest.serialize, response_deserializer=common_fields.ExternalApi.deserialize, @@ -1283,7 +1479,7 @@ def delete_external_api( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_external_api" not in self._stubs: - self._stubs["delete_external_api"] = self.grpc_channel.unary_unary( + self._stubs["delete_external_api"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/DeleteExternalApi", request_serializer=apihub_service.DeleteExternalApiRequest.serialize, response_deserializer=empty_pb2.Empty.FromString, @@ -1312,7 +1508,7 @@ def list_external_apis( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_external_apis" not in self._stubs: - self._stubs["list_external_apis"] = self.grpc_channel.unary_unary( + self._stubs["list_external_apis"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/ListExternalApis", request_serializer=apihub_service.ListExternalApisRequest.serialize, response_deserializer=apihub_service.ListExternalApisResponse.deserialize, @@ -1320,7 +1516,7 @@ def list_external_apis( return self._stubs["list_external_apis"] def close(self): - self.grpc_channel.close() + self._logged_channel.close() @property def delete_operation( @@ -1332,7 +1528,7 @@ def delete_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_operation" not in self._stubs: - self._stubs["delete_operation"] = self.grpc_channel.unary_unary( + self._stubs["delete_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/DeleteOperation", request_serializer=operations_pb2.DeleteOperationRequest.SerializeToString, response_deserializer=None, @@ -1349,7 +1545,7 @@ def cancel_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "cancel_operation" not in self._stubs: - self._stubs["cancel_operation"] = self.grpc_channel.unary_unary( + self._stubs["cancel_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/CancelOperation", request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, response_deserializer=None, @@ -1366,7 +1562,7 @@ def get_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_operation" not in self._stubs: - self._stubs["get_operation"] = self.grpc_channel.unary_unary( + self._stubs["get_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/GetOperation", request_serializer=operations_pb2.GetOperationRequest.SerializeToString, response_deserializer=operations_pb2.Operation.FromString, @@ -1385,7 +1581,7 @@ def list_operations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_operations" not in self._stubs: - self._stubs["list_operations"] = self.grpc_channel.unary_unary( + self._stubs["list_operations"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/ListOperations", request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, response_deserializer=operations_pb2.ListOperationsResponse.FromString, @@ -1404,7 +1600,7 @@ def list_locations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_locations" not in self._stubs: - self._stubs["list_locations"] = self.grpc_channel.unary_unary( + self._stubs["list_locations"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/ListLocations", request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, response_deserializer=locations_pb2.ListLocationsResponse.FromString, @@ -1421,7 +1617,7 @@ def get_location( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_location" not in self._stubs: - self._stubs["get_location"] = self.grpc_channel.unary_unary( + self._stubs["get_location"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/GetLocation", request_serializer=locations_pb2.GetLocationRequest.SerializeToString, response_deserializer=locations_pb2.Location.FromString, diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/transports/grpc_asyncio.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/transports/grpc_asyncio.py index 30051bed6768..a0762ee9df6a 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/transports/grpc_asyncio.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub/transports/grpc_asyncio.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import inspect +import json +import logging as std_logging +import pickle from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union import warnings @@ -24,14 +28,93 @@ from google.cloud.location import locations_pb2 # type: ignore from google.longrunning import operations_pb2 # type: ignore from google.protobuf import empty_pb2 # type: ignore +from google.protobuf.json_format import MessageToJson +import google.protobuf.message import grpc # type: ignore from grpc.experimental import aio # type: ignore +import proto # type: ignore from google.cloud.apihub_v1.types import apihub_service, common_fields from .base import DEFAULT_CLIENT_INFO, ApiHubTransport from .grpc import ApiHubGrpcTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class _LoggingClientAIOInterceptor( + grpc.aio.UnaryUnaryClientInterceptor +): # pragma: NO COVER + async def intercept_unary_unary(self, continuation, client_call_details, request): + logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ) + if logging_enabled: # pragma: NO COVER + request_metadata = client_call_details.metadata + if isinstance(request, proto.Message): + request_payload = type(request).to_json(request) + elif isinstance(request, google.protobuf.message.Message): + request_payload = MessageToJson(request) + else: + request_payload = f"{type(request).__name__}: {pickle.dumps(request)}" + + request_metadata = { + key: value.decode("utf-8") if isinstance(value, bytes) else value + for key, value in request_metadata + } + grpc_request = { + "payload": request_payload, + "requestMethod": "grpc", + "metadata": dict(request_metadata), + } + _LOGGER.debug( + f"Sending request for {client_call_details.method}", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHub", + "rpcName": str(client_call_details.method), + "request": grpc_request, + "metadata": grpc_request["metadata"], + }, + ) + response = await continuation(client_call_details, request) + if logging_enabled: # pragma: NO COVER + response_metadata = await response.trailing_metadata() + # Convert gRPC metadata `` to list of tuples + metadata = ( + dict([(k, str(v)) for k, v in response_metadata]) + if response_metadata + else None + ) + result = await response + if isinstance(result, proto.Message): + response_payload = type(result).to_json(result) + elif isinstance(result, google.protobuf.message.Message): + response_payload = MessageToJson(result) + else: + response_payload = f"{type(result).__name__}: {pickle.dumps(result)}" + grpc_response = { + "payload": response_payload, + "metadata": metadata, + "status": "OK", + } + _LOGGER.debug( + f"Received response to rpc {client_call_details.method}.", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHub", + "rpcName": str(client_call_details.method), + "response": grpc_response, + "metadata": grpc_response["metadata"], + }, + ) + return response + class ApiHubGrpcAsyncIOTransport(ApiHubTransport): """gRPC AsyncIO backend transport for ApiHub. @@ -228,7 +311,13 @@ def __init__( ], ) - # Wrap messages. This must be done after self._grpc_channel exists + self._interceptor = _LoggingClientAIOInterceptor() + self._grpc_channel._unary_unary_interceptors.append(self._interceptor) + self._logged_channel = self._grpc_channel + self._wrap_with_kind = ( + "kind" in inspect.signature(gapic_v1.method_async.wrap_method).parameters + ) + # Wrap messages. This must be done after self._logged_channel exists self._prep_wrapped_messages(client_info) @property @@ -262,7 +351,7 @@ def create_api( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "create_api" not in self._stubs: - self._stubs["create_api"] = self.grpc_channel.unary_unary( + self._stubs["create_api"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/CreateApi", request_serializer=apihub_service.CreateApiRequest.serialize, response_deserializer=common_fields.Api.deserialize, @@ -289,7 +378,7 @@ def get_api( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_api" not in self._stubs: - self._stubs["get_api"] = self.grpc_channel.unary_unary( + self._stubs["get_api"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/GetApi", request_serializer=apihub_service.GetApiRequest.serialize, response_deserializer=common_fields.Api.deserialize, @@ -317,7 +406,7 @@ def list_apis( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_apis" not in self._stubs: - self._stubs["list_apis"] = self.grpc_channel.unary_unary( + self._stubs["list_apis"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/ListApis", request_serializer=apihub_service.ListApisRequest.serialize, response_deserializer=apihub_service.ListApisResponse.deserialize, @@ -331,17 +420,18 @@ def update_api( r"""Return a callable for the update api method over gRPC. Update an API resource in the API hub. The following fields in - the [API][] can be updated: - - - [display_name][google.cloud.apihub.v1.Api.display_name] - - [description][google.cloud.apihub.v1.Api.description] - - [owner][google.cloud.apihub.v1.Api.owner] - - [documentation][google.cloud.apihub.v1.Api.documentation] - - [target_user][google.cloud.apihub.v1.Api.target_user] - - [team][google.cloud.apihub.v1.Api.team] - - [business_unit][google.cloud.apihub.v1.Api.business_unit] - - [maturity_level][google.cloud.apihub.v1.Api.maturity_level] - - [attributes][google.cloud.apihub.v1.Api.attributes] + the [API][google.cloud.apihub.v1.Api] can be updated: + + - [display_name][google.cloud.apihub.v1.Api.display_name] + - [description][google.cloud.apihub.v1.Api.description] + - [owner][google.cloud.apihub.v1.Api.owner] + - [documentation][google.cloud.apihub.v1.Api.documentation] + - [target_user][google.cloud.apihub.v1.Api.target_user] + - [team][google.cloud.apihub.v1.Api.team] + - [business_unit][google.cloud.apihub.v1.Api.business_unit] + - [maturity_level][google.cloud.apihub.v1.Api.maturity_level] + - [api_style][google.cloud.apihub.v1.Api.api_style] + - [attributes][google.cloud.apihub.v1.Api.attributes] The [update_mask][google.cloud.apihub.v1.UpdateApiRequest.update_mask] @@ -361,7 +451,7 @@ def update_api( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "update_api" not in self._stubs: - self._stubs["update_api"] = self.grpc_channel.unary_unary( + self._stubs["update_api"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/UpdateApi", request_serializer=apihub_service.UpdateApiRequest.serialize, response_deserializer=common_fields.Api.deserialize, @@ -388,7 +478,7 @@ def delete_api( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_api" not in self._stubs: - self._stubs["delete_api"] = self.grpc_channel.unary_unary( + self._stubs["delete_api"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/DeleteApi", request_serializer=apihub_service.DeleteApiRequest.serialize, response_deserializer=empty_pb2.Empty.FromString, @@ -417,7 +507,7 @@ def create_version( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "create_version" not in self._stubs: - self._stubs["create_version"] = self.grpc_channel.unary_unary( + self._stubs["create_version"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/CreateVersion", request_serializer=apihub_service.CreateVersionRequest.serialize, response_deserializer=common_fields.Version.deserialize, @@ -446,7 +536,7 @@ def get_version( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_version" not in self._stubs: - self._stubs["get_version"] = self.grpc_channel.unary_unary( + self._stubs["get_version"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/GetVersion", request_serializer=apihub_service.GetVersionRequest.serialize, response_deserializer=common_fields.Version.deserialize, @@ -475,7 +565,7 @@ def list_versions( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_versions" not in self._stubs: - self._stubs["list_versions"] = self.grpc_channel.unary_unary( + self._stubs["list_versions"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/ListVersions", request_serializer=apihub_service.ListVersionsRequest.serialize, response_deserializer=apihub_service.ListVersionsResponse.deserialize, @@ -494,14 +584,14 @@ def update_version( [version][google.cloud.apihub.v1.Version] can be updated currently: - - [display_name][google.cloud.apihub.v1.Version.display_name] - - [description][google.cloud.apihub.v1.Version.description] - - [documentation][google.cloud.apihub.v1.Version.documentation] - - [deployments][google.cloud.apihub.v1.Version.deployments] - - [lifecycle][google.cloud.apihub.v1.Version.lifecycle] - - [compliance][google.cloud.apihub.v1.Version.compliance] - - [accreditation][google.cloud.apihub.v1.Version.accreditation] - - [attributes][google.cloud.apihub.v1.Version.attributes] + - [display_name][google.cloud.apihub.v1.Version.display_name] + - [description][google.cloud.apihub.v1.Version.description] + - [documentation][google.cloud.apihub.v1.Version.documentation] + - [deployments][google.cloud.apihub.v1.Version.deployments] + - [lifecycle][google.cloud.apihub.v1.Version.lifecycle] + - [compliance][google.cloud.apihub.v1.Version.compliance] + - [accreditation][google.cloud.apihub.v1.Version.accreditation] + - [attributes][google.cloud.apihub.v1.Version.attributes] The [update_mask][google.cloud.apihub.v1.UpdateVersionRequest.update_mask] @@ -518,7 +608,7 @@ def update_version( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "update_version" not in self._stubs: - self._stubs["update_version"] = self.grpc_channel.unary_unary( + self._stubs["update_version"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/UpdateVersion", request_serializer=apihub_service.UpdateVersionRequest.serialize, response_deserializer=common_fields.Version.deserialize, @@ -546,7 +636,7 @@ def delete_version( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_version" not in self._stubs: - self._stubs["delete_version"] = self.grpc_channel.unary_unary( + self._stubs["delete_version"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/DeleteVersion", request_serializer=apihub_service.DeleteVersionRequest.serialize, response_deserializer=empty_pb2.Empty.FromString, @@ -594,7 +684,7 @@ def create_spec( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "create_spec" not in self._stubs: - self._stubs["create_spec"] = self.grpc_channel.unary_unary( + self._stubs["create_spec"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/CreateSpec", request_serializer=apihub_service.CreateSpecRequest.serialize, response_deserializer=common_fields.Spec.deserialize, @@ -623,7 +713,7 @@ def get_spec( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_spec" not in self._stubs: - self._stubs["get_spec"] = self.grpc_channel.unary_unary( + self._stubs["get_spec"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/GetSpec", request_serializer=apihub_service.GetSpecRequest.serialize, response_deserializer=common_fields.Spec.deserialize, @@ -651,7 +741,7 @@ def get_spec_contents( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_spec_contents" not in self._stubs: - self._stubs["get_spec_contents"] = self.grpc_channel.unary_unary( + self._stubs["get_spec_contents"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/GetSpecContents", request_serializer=apihub_service.GetSpecContentsRequest.serialize, response_deserializer=common_fields.SpecContents.deserialize, @@ -680,7 +770,7 @@ def list_specs( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_specs" not in self._stubs: - self._stubs["list_specs"] = self.grpc_channel.unary_unary( + self._stubs["list_specs"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/ListSpecs", request_serializer=apihub_service.ListSpecsRequest.serialize, response_deserializer=apihub_service.ListSpecsResponse.deserialize, @@ -696,12 +786,12 @@ def update_spec( Update spec. The following fields in the [spec][google.cloud.apihub.v1.Spec] can be updated: - - [display_name][google.cloud.apihub.v1.Spec.display_name] - - [source_uri][google.cloud.apihub.v1.Spec.source_uri] - - [lint_response][google.cloud.apihub.v1.Spec.lint_response] - - [attributes][google.cloud.apihub.v1.Spec.attributes] - - [contents][google.cloud.apihub.v1.Spec.contents] - - [spec_type][google.cloud.apihub.v1.Spec.spec_type] + - [display_name][google.cloud.apihub.v1.Spec.display_name] + - [source_uri][google.cloud.apihub.v1.Spec.source_uri] + - [lint_response][google.cloud.apihub.v1.Spec.lint_response] + - [attributes][google.cloud.apihub.v1.Spec.attributes] + - [contents][google.cloud.apihub.v1.Spec.contents] + - [spec_type][google.cloud.apihub.v1.Spec.spec_type] In case of an OAS spec, updating spec contents can lead to: @@ -728,7 +818,7 @@ def update_spec( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "update_spec" not in self._stubs: - self._stubs["update_spec"] = self.grpc_channel.unary_unary( + self._stubs["update_spec"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/UpdateSpec", request_serializer=apihub_service.UpdateSpecRequest.serialize, response_deserializer=common_fields.Spec.deserialize, @@ -756,13 +846,44 @@ def delete_spec( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_spec" not in self._stubs: - self._stubs["delete_spec"] = self.grpc_channel.unary_unary( + self._stubs["delete_spec"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/DeleteSpec", request_serializer=apihub_service.DeleteSpecRequest.serialize, response_deserializer=empty_pb2.Empty.FromString, ) return self._stubs["delete_spec"] + @property + def create_api_operation( + self, + ) -> Callable[ + [apihub_service.CreateApiOperationRequest], + Awaitable[common_fields.ApiOperation], + ]: + r"""Return a callable for the create api operation method over gRPC. + + Create an apiOperation in an API version. + An apiOperation can be created only if the version has + no apiOperations which were created by parsing a spec. + + Returns: + Callable[[~.CreateApiOperationRequest], + Awaitable[~.ApiOperation]]: + 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 "create_api_operation" not in self._stubs: + self._stubs["create_api_operation"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHub/CreateApiOperation", + request_serializer=apihub_service.CreateApiOperationRequest.serialize, + response_deserializer=common_fields.ApiOperation.deserialize, + ) + return self._stubs["create_api_operation"] + @property def get_api_operation( self, @@ -785,7 +906,7 @@ def get_api_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_api_operation" not in self._stubs: - self._stubs["get_api_operation"] = self.grpc_channel.unary_unary( + self._stubs["get_api_operation"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/GetApiOperation", request_serializer=apihub_service.GetApiOperationRequest.serialize, response_deserializer=common_fields.ApiOperation.deserialize, @@ -814,13 +935,92 @@ def list_api_operations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_api_operations" not in self._stubs: - self._stubs["list_api_operations"] = self.grpc_channel.unary_unary( + self._stubs["list_api_operations"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/ListApiOperations", request_serializer=apihub_service.ListApiOperationsRequest.serialize, response_deserializer=apihub_service.ListApiOperationsResponse.deserialize, ) return self._stubs["list_api_operations"] + @property + def update_api_operation( + self, + ) -> Callable[ + [apihub_service.UpdateApiOperationRequest], + Awaitable[common_fields.ApiOperation], + ]: + r"""Return a callable for the update api operation method over gRPC. + + Update an operation in an API version. The following fields in + the [ApiOperation resource][google.cloud.apihub.v1.ApiOperation] + can be updated: + + - [details.description][ApiOperation.details.description] + - [details.documentation][ApiOperation.details.documentation] + - [details.http_operation.path][ApiOperation.details.http_operation.path.path] + - [details.http_operation.method][ApiOperation.details.http_operation.method] + - [details.deprecated][ApiOperation.details.deprecated] + - [attributes][google.cloud.apihub.v1.ApiOperation.attributes] + + The + [update_mask][google.cloud.apihub.v1.UpdateApiOperationRequest.update_mask] + should be used to specify the fields being updated. + + An operation can be updated only if the operation was created + via + [CreateApiOperation][google.cloud.apihub.v1.ApiHub.CreateApiOperation] + API. If the operation was created by parsing the spec, then it + can be edited by updating the spec. + + Returns: + Callable[[~.UpdateApiOperationRequest], + Awaitable[~.ApiOperation]]: + 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 "update_api_operation" not in self._stubs: + self._stubs["update_api_operation"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHub/UpdateApiOperation", + request_serializer=apihub_service.UpdateApiOperationRequest.serialize, + response_deserializer=common_fields.ApiOperation.deserialize, + ) + return self._stubs["update_api_operation"] + + @property + def delete_api_operation( + self, + ) -> Callable[ + [apihub_service.DeleteApiOperationRequest], Awaitable[empty_pb2.Empty] + ]: + r"""Return a callable for the delete api operation method over gRPC. + + Delete an operation in an API version and we can + delete only the operations created via create API. If + the operation was created by parsing the spec, then it + can be deleted by editing or deleting the spec. + + Returns: + Callable[[~.DeleteApiOperationRequest], + Awaitable[~.Empty]]: + 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 "delete_api_operation" not in self._stubs: + self._stubs["delete_api_operation"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHub/DeleteApiOperation", + request_serializer=apihub_service.DeleteApiOperationRequest.serialize, + response_deserializer=empty_pb2.Empty.FromString, + ) + return self._stubs["delete_api_operation"] + @property def get_definition( self, @@ -842,7 +1042,7 @@ def get_definition( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_definition" not in self._stubs: - self._stubs["get_definition"] = self.grpc_channel.unary_unary( + self._stubs["get_definition"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/GetDefinition", request_serializer=apihub_service.GetDefinitionRequest.serialize, response_deserializer=common_fields.Definition.deserialize, @@ -872,7 +1072,7 @@ def create_deployment( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "create_deployment" not in self._stubs: - self._stubs["create_deployment"] = self.grpc_channel.unary_unary( + self._stubs["create_deployment"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/CreateDeployment", request_serializer=apihub_service.CreateDeploymentRequest.serialize, response_deserializer=common_fields.Deployment.deserialize, @@ -901,7 +1101,7 @@ def get_deployment( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_deployment" not in self._stubs: - self._stubs["get_deployment"] = self.grpc_channel.unary_unary( + self._stubs["get_deployment"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/GetDeployment", request_serializer=apihub_service.GetDeploymentRequest.serialize, response_deserializer=common_fields.Deployment.deserialize, @@ -930,7 +1130,7 @@ def list_deployments( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_deployments" not in self._stubs: - self._stubs["list_deployments"] = self.grpc_channel.unary_unary( + self._stubs["list_deployments"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/ListDeployments", request_serializer=apihub_service.ListDeploymentsRequest.serialize, response_deserializer=apihub_service.ListDeploymentsResponse.deserialize, @@ -949,19 +1149,23 @@ def update_deployment( fields in the [deployment resource][google.cloud.apihub.v1.Deployment] can be updated: - - [display_name][google.cloud.apihub.v1.Deployment.display_name] - - [description][google.cloud.apihub.v1.Deployment.description] - - [documentation][google.cloud.apihub.v1.Deployment.documentation] - - [deployment_type][google.cloud.apihub.v1.Deployment.deployment_type] - - [resource_uri][google.cloud.apihub.v1.Deployment.resource_uri] - - [endpoints][google.cloud.apihub.v1.Deployment.endpoints] - - [slo][google.cloud.apihub.v1.Deployment.slo] - - [environment][google.cloud.apihub.v1.Deployment.environment] - - [attributes][google.cloud.apihub.v1.Deployment.attributes] - - The - [update_mask][google.cloud.apihub.v1.UpdateDeploymentRequest.update_mask] - should be used to specify the fields being updated. + - [display_name][google.cloud.apihub.v1.Deployment.display_name] + - [description][google.cloud.apihub.v1.Deployment.description] + - [documentation][google.cloud.apihub.v1.Deployment.documentation] + - [deployment_type][google.cloud.apihub.v1.Deployment.deployment_type] + - [resource_uri][google.cloud.apihub.v1.Deployment.resource_uri] + - [endpoints][google.cloud.apihub.v1.Deployment.endpoints] + - [slo][google.cloud.apihub.v1.Deployment.slo] + - [environment][google.cloud.apihub.v1.Deployment.environment] + - [attributes][google.cloud.apihub.v1.Deployment.attributes] + - [source_project] + [google.cloud.apihub.v1.Deployment.source_project] + - [source_environment] + [google.cloud.apihub.v1.Deployment.source_environment] + - [management_url][google.cloud.apihub.v1.Deployment.management_url] + - [source_uri][google.cloud.apihub.v1.Deployment.source_uri] The + [update_mask][google.cloud.apihub.v1.UpdateDeploymentRequest.update_mask] + should be used to specify the fields being updated. Returns: Callable[[~.UpdateDeploymentRequest], @@ -974,7 +1178,7 @@ def update_deployment( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "update_deployment" not in self._stubs: - self._stubs["update_deployment"] = self.grpc_channel.unary_unary( + self._stubs["update_deployment"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/UpdateDeployment", request_serializer=apihub_service.UpdateDeploymentRequest.serialize, response_deserializer=common_fields.Deployment.deserialize, @@ -1000,7 +1204,7 @@ def delete_deployment( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_deployment" not in self._stubs: - self._stubs["delete_deployment"] = self.grpc_channel.unary_unary( + self._stubs["delete_deployment"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/DeleteDeployment", request_serializer=apihub_service.DeleteDeploymentRequest.serialize, response_deserializer=empty_pb2.Empty.FromString, @@ -1036,7 +1240,7 @@ def create_attribute( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "create_attribute" not in self._stubs: - self._stubs["create_attribute"] = self.grpc_channel.unary_unary( + self._stubs["create_attribute"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/CreateAttribute", request_serializer=apihub_service.CreateAttributeRequest.serialize, response_deserializer=common_fields.Attribute.deserialize, @@ -1064,7 +1268,7 @@ def get_attribute( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_attribute" not in self._stubs: - self._stubs["get_attribute"] = self.grpc_channel.unary_unary( + self._stubs["get_attribute"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/GetAttribute", request_serializer=apihub_service.GetAttributeRequest.serialize, response_deserializer=common_fields.Attribute.deserialize, @@ -1082,25 +1286,25 @@ def update_attribute( Update the attribute. The following fields in the [Attribute resource][google.cloud.apihub.v1.Attribute] can be updated: - - [display_name][google.cloud.apihub.v1.Attribute.display_name] - The display name can be updated for user defined attributes - only. - - [description][google.cloud.apihub.v1.Attribute.description] - The description can be updated for user defined attributes - only. - - [allowed_values][google.cloud.apihub.v1.Attribute.allowed_values] - To update the list of allowed values, clients need to use the - fetched list of allowed values and add or remove values to or - from the same list. The mutable allowed values can be updated - for both user defined and System defined attributes. The - immutable allowed values cannot be updated or deleted. The - updated list of allowed values cannot be empty. If an allowed - value that is already used by some resource's attribute is - deleted, then the association between the resource and the - attribute value will also be deleted. - - [cardinality][google.cloud.apihub.v1.Attribute.cardinality] - The cardinality can be updated for user defined attributes - only. Cardinality can only be increased during an update. + - [display_name][google.cloud.apihub.v1.Attribute.display_name] + The display name can be updated for user defined attributes + only. + - [description][google.cloud.apihub.v1.Attribute.description] + The description can be updated for user defined attributes + only. + - [allowed_values][google.cloud.apihub.v1.Attribute.allowed_values] + To update the list of allowed values, clients need to use the + fetched list of allowed values and add or remove values to or + from the same list. The mutable allowed values can be updated + for both user defined and System defined attributes. The + immutable allowed values cannot be updated or deleted. The + updated list of allowed values cannot be empty. If an allowed + value that is already used by some resource's attribute is + deleted, then the association between the resource and the + attribute value will also be deleted. + - [cardinality][google.cloud.apihub.v1.Attribute.cardinality] + The cardinality can be updated for user defined attributes + only. Cardinality can only be increased during an update. The [update_mask][google.cloud.apihub.v1.UpdateAttributeRequest.update_mask] @@ -1117,7 +1321,7 @@ def update_attribute( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "update_attribute" not in self._stubs: - self._stubs["update_attribute"] = self.grpc_channel.unary_unary( + self._stubs["update_attribute"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/UpdateAttribute", request_serializer=apihub_service.UpdateAttributeRequest.serialize, response_deserializer=common_fields.Attribute.deserialize, @@ -1147,7 +1351,7 @@ def delete_attribute( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_attribute" not in self._stubs: - self._stubs["delete_attribute"] = self.grpc_channel.unary_unary( + self._stubs["delete_attribute"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/DeleteAttribute", request_serializer=apihub_service.DeleteAttributeRequest.serialize, response_deserializer=empty_pb2.Empty.FromString, @@ -1176,7 +1380,7 @@ def list_attributes( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_attributes" not in self._stubs: - self._stubs["list_attributes"] = self.grpc_channel.unary_unary( + self._stubs["list_attributes"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/ListAttributes", request_serializer=apihub_service.ListAttributesRequest.serialize, response_deserializer=apihub_service.ListAttributesResponse.deserialize, @@ -1205,7 +1409,7 @@ def search_resources( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "search_resources" not in self._stubs: - self._stubs["search_resources"] = self.grpc_channel.unary_unary( + self._stubs["search_resources"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/SearchResources", request_serializer=apihub_service.SearchResourcesRequest.serialize, response_deserializer=apihub_service.SearchResourcesResponse.deserialize, @@ -1233,7 +1437,7 @@ def create_external_api( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "create_external_api" not in self._stubs: - self._stubs["create_external_api"] = self.grpc_channel.unary_unary( + self._stubs["create_external_api"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/CreateExternalApi", request_serializer=apihub_service.CreateExternalApiRequest.serialize, response_deserializer=common_fields.ExternalApi.deserialize, @@ -1262,7 +1466,7 @@ def get_external_api( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_external_api" not in self._stubs: - self._stubs["get_external_api"] = self.grpc_channel.unary_unary( + self._stubs["get_external_api"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/GetExternalApi", request_serializer=apihub_service.GetExternalApiRequest.serialize, response_deserializer=common_fields.ExternalApi.deserialize, @@ -1280,11 +1484,11 @@ def update_external_api( Update an External API resource in the API hub. The following fields can be updated: - - [display_name][google.cloud.apihub.v1.ExternalApi.display_name] - - [description][google.cloud.apihub.v1.ExternalApi.description] - - [documentation][google.cloud.apihub.v1.ExternalApi.documentation] - - [endpoints][google.cloud.apihub.v1.ExternalApi.endpoints] - - [paths][google.cloud.apihub.v1.ExternalApi.paths] + - [display_name][google.cloud.apihub.v1.ExternalApi.display_name] + - [description][google.cloud.apihub.v1.ExternalApi.description] + - [documentation][google.cloud.apihub.v1.ExternalApi.documentation] + - [endpoints][google.cloud.apihub.v1.ExternalApi.endpoints] + - [paths][google.cloud.apihub.v1.ExternalApi.paths] The [update_mask][google.cloud.apihub.v1.UpdateExternalApiRequest.update_mask] @@ -1301,7 +1505,7 @@ def update_external_api( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "update_external_api" not in self._stubs: - self._stubs["update_external_api"] = self.grpc_channel.unary_unary( + self._stubs["update_external_api"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/UpdateExternalApi", request_serializer=apihub_service.UpdateExternalApiRequest.serialize, response_deserializer=common_fields.ExternalApi.deserialize, @@ -1329,7 +1533,7 @@ def delete_external_api( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_external_api" not in self._stubs: - self._stubs["delete_external_api"] = self.grpc_channel.unary_unary( + self._stubs["delete_external_api"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/DeleteExternalApi", request_serializer=apihub_service.DeleteExternalApiRequest.serialize, response_deserializer=empty_pb2.Empty.FromString, @@ -1358,7 +1562,7 @@ def list_external_apis( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_external_apis" not in self._stubs: - self._stubs["list_external_apis"] = self.grpc_channel.unary_unary( + self._stubs["list_external_apis"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHub/ListExternalApis", request_serializer=apihub_service.ListExternalApisRequest.serialize, response_deserializer=apihub_service.ListExternalApisResponse.deserialize, @@ -1368,12 +1572,12 @@ def list_external_apis( def _prep_wrapped_messages(self, client_info): """Precompute the wrapped methods, overriding the base class method to use async wrappers.""" self._wrapped_methods = { - self.create_api: gapic_v1.method_async.wrap_method( + self.create_api: self._wrap_method( self.create_api, default_timeout=60.0, client_info=client_info, ), - self.get_api: gapic_v1.method_async.wrap_method( + self.get_api: self._wrap_method( self.get_api, default_retry=retries.AsyncRetry( initial=1.0, @@ -1387,7 +1591,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.list_apis: gapic_v1.method_async.wrap_method( + self.list_apis: self._wrap_method( self.list_apis, default_retry=retries.AsyncRetry( initial=1.0, @@ -1401,22 +1605,22 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.update_api: gapic_v1.method_async.wrap_method( + self.update_api: self._wrap_method( self.update_api, default_timeout=60.0, client_info=client_info, ), - self.delete_api: gapic_v1.method_async.wrap_method( + self.delete_api: self._wrap_method( self.delete_api, - default_timeout=60.0, + default_timeout=300.0, client_info=client_info, ), - self.create_version: gapic_v1.method_async.wrap_method( + self.create_version: self._wrap_method( self.create_version, default_timeout=60.0, client_info=client_info, ), - self.get_version: gapic_v1.method_async.wrap_method( + self.get_version: self._wrap_method( self.get_version, default_retry=retries.AsyncRetry( initial=1.0, @@ -1430,7 +1634,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.list_versions: gapic_v1.method_async.wrap_method( + self.list_versions: self._wrap_method( self.list_versions, default_retry=retries.AsyncRetry( initial=1.0, @@ -1444,22 +1648,22 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.update_version: gapic_v1.method_async.wrap_method( + self.update_version: self._wrap_method( self.update_version, default_timeout=60.0, client_info=client_info, ), - self.delete_version: gapic_v1.method_async.wrap_method( + self.delete_version: self._wrap_method( self.delete_version, - default_timeout=60.0, + default_timeout=300.0, client_info=client_info, ), - self.create_spec: gapic_v1.method_async.wrap_method( + self.create_spec: self._wrap_method( self.create_spec, default_timeout=60.0, client_info=client_info, ), - self.get_spec: gapic_v1.method_async.wrap_method( + self.get_spec: self._wrap_method( self.get_spec, default_retry=retries.AsyncRetry( initial=1.0, @@ -1473,7 +1677,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.get_spec_contents: gapic_v1.method_async.wrap_method( + self.get_spec_contents: self._wrap_method( self.get_spec_contents, default_retry=retries.AsyncRetry( initial=1.0, @@ -1487,7 +1691,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.list_specs: gapic_v1.method_async.wrap_method( + self.list_specs: self._wrap_method( self.list_specs, default_retry=retries.AsyncRetry( initial=1.0, @@ -1501,17 +1705,22 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.update_spec: gapic_v1.method_async.wrap_method( + self.update_spec: self._wrap_method( self.update_spec, default_timeout=60.0, client_info=client_info, ), - self.delete_spec: gapic_v1.method_async.wrap_method( + self.delete_spec: self._wrap_method( self.delete_spec, - default_timeout=60.0, + default_timeout=300.0, client_info=client_info, ), - self.get_api_operation: gapic_v1.method_async.wrap_method( + self.create_api_operation: self._wrap_method( + self.create_api_operation, + default_timeout=None, + client_info=client_info, + ), + self.get_api_operation: self._wrap_method( self.get_api_operation, default_retry=retries.AsyncRetry( initial=1.0, @@ -1525,7 +1734,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.list_api_operations: gapic_v1.method_async.wrap_method( + self.list_api_operations: self._wrap_method( self.list_api_operations, default_retry=retries.AsyncRetry( initial=1.0, @@ -1539,7 +1748,17 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.get_definition: gapic_v1.method_async.wrap_method( + self.update_api_operation: self._wrap_method( + self.update_api_operation, + default_timeout=None, + client_info=client_info, + ), + self.delete_api_operation: self._wrap_method( + self.delete_api_operation, + default_timeout=None, + client_info=client_info, + ), + self.get_definition: self._wrap_method( self.get_definition, default_retry=retries.AsyncRetry( initial=1.0, @@ -1553,12 +1772,12 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.create_deployment: gapic_v1.method_async.wrap_method( + self.create_deployment: self._wrap_method( self.create_deployment, default_timeout=60.0, client_info=client_info, ), - self.get_deployment: gapic_v1.method_async.wrap_method( + self.get_deployment: self._wrap_method( self.get_deployment, default_retry=retries.AsyncRetry( initial=1.0, @@ -1572,7 +1791,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.list_deployments: gapic_v1.method_async.wrap_method( + self.list_deployments: self._wrap_method( self.list_deployments, default_retry=retries.AsyncRetry( initial=1.0, @@ -1586,22 +1805,22 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.update_deployment: gapic_v1.method_async.wrap_method( + self.update_deployment: self._wrap_method( self.update_deployment, default_timeout=60.0, client_info=client_info, ), - self.delete_deployment: gapic_v1.method_async.wrap_method( + self.delete_deployment: self._wrap_method( self.delete_deployment, default_timeout=60.0, client_info=client_info, ), - self.create_attribute: gapic_v1.method_async.wrap_method( + self.create_attribute: self._wrap_method( self.create_attribute, default_timeout=60.0, client_info=client_info, ), - self.get_attribute: gapic_v1.method_async.wrap_method( + self.get_attribute: self._wrap_method( self.get_attribute, default_retry=retries.AsyncRetry( initial=1.0, @@ -1615,17 +1834,17 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.update_attribute: gapic_v1.method_async.wrap_method( + self.update_attribute: self._wrap_method( self.update_attribute, default_timeout=60.0, client_info=client_info, ), - self.delete_attribute: gapic_v1.method_async.wrap_method( + self.delete_attribute: self._wrap_method( self.delete_attribute, default_timeout=60.0, client_info=client_info, ), - self.list_attributes: gapic_v1.method_async.wrap_method( + self.list_attributes: self._wrap_method( self.list_attributes, default_retry=retries.AsyncRetry( initial=1.0, @@ -1639,7 +1858,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.search_resources: gapic_v1.method_async.wrap_method( + self.search_resources: self._wrap_method( self.search_resources, default_retry=retries.AsyncRetry( initial=1.0, @@ -1653,12 +1872,12 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.create_external_api: gapic_v1.method_async.wrap_method( + self.create_external_api: self._wrap_method( self.create_external_api, default_timeout=60.0, client_info=client_info, ), - self.get_external_api: gapic_v1.method_async.wrap_method( + self.get_external_api: self._wrap_method( self.get_external_api, default_retry=retries.AsyncRetry( initial=1.0, @@ -1672,17 +1891,17 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.update_external_api: gapic_v1.method_async.wrap_method( + self.update_external_api: self._wrap_method( self.update_external_api, default_timeout=60.0, client_info=client_info, ), - self.delete_external_api: gapic_v1.method_async.wrap_method( + self.delete_external_api: self._wrap_method( self.delete_external_api, default_timeout=60.0, client_info=client_info, ), - self.list_external_apis: gapic_v1.method_async.wrap_method( + self.list_external_apis: self._wrap_method( self.list_external_apis, default_retry=retries.AsyncRetry( initial=1.0, @@ -1696,10 +1915,49 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), + self.get_location: self._wrap_method( + self.get_location, + default_timeout=None, + client_info=client_info, + ), + self.list_locations: self._wrap_method( + self.list_locations, + default_timeout=None, + client_info=client_info, + ), + self.cancel_operation: self._wrap_method( + self.cancel_operation, + default_timeout=None, + client_info=client_info, + ), + self.delete_operation: self._wrap_method( + self.delete_operation, + default_timeout=None, + client_info=client_info, + ), + self.get_operation: self._wrap_method( + self.get_operation, + default_timeout=None, + client_info=client_info, + ), + self.list_operations: self._wrap_method( + self.list_operations, + default_timeout=None, + client_info=client_info, + ), } + def _wrap_method(self, func, *args, **kwargs): + if self._wrap_with_kind: # pragma: NO COVER + kwargs["kind"] = self.kind + return gapic_v1.method_async.wrap_method(func, *args, **kwargs) + def close(self): - return self.grpc_channel.close() + return self._logged_channel.close() + + @property + def kind(self) -> str: + return "grpc_asyncio" @property def delete_operation( @@ -1711,7 +1969,7 @@ def delete_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_operation" not in self._stubs: - self._stubs["delete_operation"] = self.grpc_channel.unary_unary( + self._stubs["delete_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/DeleteOperation", request_serializer=operations_pb2.DeleteOperationRequest.SerializeToString, response_deserializer=None, @@ -1728,7 +1986,7 @@ def cancel_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "cancel_operation" not in self._stubs: - self._stubs["cancel_operation"] = self.grpc_channel.unary_unary( + self._stubs["cancel_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/CancelOperation", request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, response_deserializer=None, @@ -1745,7 +2003,7 @@ def get_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_operation" not in self._stubs: - self._stubs["get_operation"] = self.grpc_channel.unary_unary( + self._stubs["get_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/GetOperation", request_serializer=operations_pb2.GetOperationRequest.SerializeToString, response_deserializer=operations_pb2.Operation.FromString, @@ -1764,7 +2022,7 @@ def list_operations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_operations" not in self._stubs: - self._stubs["list_operations"] = self.grpc_channel.unary_unary( + self._stubs["list_operations"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/ListOperations", request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, response_deserializer=operations_pb2.ListOperationsResponse.FromString, @@ -1783,7 +2041,7 @@ def list_locations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_locations" not in self._stubs: - self._stubs["list_locations"] = self.grpc_channel.unary_unary( + self._stubs["list_locations"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/ListLocations", request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, response_deserializer=locations_pb2.ListLocationsResponse.FromString, @@ -1800,7 +2058,7 @@ def get_location( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_location" not in self._stubs: - self._stubs["get_location"] = self.grpc_channel.unary_unary( + self._stubs["get_location"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/GetLocation", request_serializer=locations_pb2.GetLocationRequest.SerializeToString, response_deserializer=locations_pb2.Location.FromString, diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/__init__.py index 2867f1d5d152..0132d202edbd 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/__init__.py @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from .async_client import ApiHubCollectAsyncClient from .client import ApiHubCollectClient -__all__ = ("ApiHubCollectClient",) +__all__ = ( + "ApiHubCollectClient", + "ApiHubCollectAsyncClient", +) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/async_client.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/async_client.py new file mode 100644 index 000000000000..8d87ae3be421 --- /dev/null +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/async_client.py @@ -0,0 +1,816 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +from collections import OrderedDict +import logging as std_logging +import re +from typing import ( + Callable, + Dict, + Mapping, + MutableMapping, + MutableSequence, + Optional, + Sequence, + Tuple, + Type, + Union, +) + +from google.api_core import exceptions as core_exceptions +from google.api_core import gapic_v1 +from google.api_core import retry_async as retries +from google.api_core.client_options import ClientOptions +from google.auth import credentials as ga_credentials # type: ignore +from google.oauth2 import service_account # type: ignore +import google.protobuf + +from google.cloud.apihub_v1 import gapic_version as package_version + +try: + OptionalRetry = Union[retries.AsyncRetry, gapic_v1.method._MethodDefault, None] +except AttributeError: # pragma: NO COVER + OptionalRetry = Union[retries.AsyncRetry, object, None] # type: ignore + +from google.api_core import operation # type: ignore +from google.api_core import operation_async # type: ignore +from google.cloud.location import locations_pb2 # type: ignore +from google.longrunning import operations_pb2 # type: ignore + +from google.cloud.apihub_v1.types import collect_service, common_fields + +from .client import ApiHubCollectClient +from .transports.base import DEFAULT_CLIENT_INFO, ApiHubCollectTransport +from .transports.grpc_asyncio import ApiHubCollectGrpcAsyncIOTransport + +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class ApiHubCollectAsyncClient: + """This service exposes methods used for collecting various + types of data from different first party and third party sources + and push it to Hub's collect layer. + """ + + _client: ApiHubCollectClient + + # Copy defaults from the synchronous client for use here. + # Note: DEFAULT_ENDPOINT is deprecated. Use _DEFAULT_ENDPOINT_TEMPLATE instead. + DEFAULT_ENDPOINT = ApiHubCollectClient.DEFAULT_ENDPOINT + DEFAULT_MTLS_ENDPOINT = ApiHubCollectClient.DEFAULT_MTLS_ENDPOINT + _DEFAULT_ENDPOINT_TEMPLATE = ApiHubCollectClient._DEFAULT_ENDPOINT_TEMPLATE + _DEFAULT_UNIVERSE = ApiHubCollectClient._DEFAULT_UNIVERSE + + api_path = staticmethod(ApiHubCollectClient.api_path) + parse_api_path = staticmethod(ApiHubCollectClient.parse_api_path) + api_operation_path = staticmethod(ApiHubCollectClient.api_operation_path) + parse_api_operation_path = staticmethod( + ApiHubCollectClient.parse_api_operation_path + ) + attribute_path = staticmethod(ApiHubCollectClient.attribute_path) + parse_attribute_path = staticmethod(ApiHubCollectClient.parse_attribute_path) + definition_path = staticmethod(ApiHubCollectClient.definition_path) + parse_definition_path = staticmethod(ApiHubCollectClient.parse_definition_path) + deployment_path = staticmethod(ApiHubCollectClient.deployment_path) + parse_deployment_path = staticmethod(ApiHubCollectClient.parse_deployment_path) + plugin_instance_path = staticmethod(ApiHubCollectClient.plugin_instance_path) + parse_plugin_instance_path = staticmethod( + ApiHubCollectClient.parse_plugin_instance_path + ) + spec_path = staticmethod(ApiHubCollectClient.spec_path) + parse_spec_path = staticmethod(ApiHubCollectClient.parse_spec_path) + version_path = staticmethod(ApiHubCollectClient.version_path) + parse_version_path = staticmethod(ApiHubCollectClient.parse_version_path) + common_billing_account_path = staticmethod( + ApiHubCollectClient.common_billing_account_path + ) + parse_common_billing_account_path = staticmethod( + ApiHubCollectClient.parse_common_billing_account_path + ) + common_folder_path = staticmethod(ApiHubCollectClient.common_folder_path) + parse_common_folder_path = staticmethod( + ApiHubCollectClient.parse_common_folder_path + ) + common_organization_path = staticmethod( + ApiHubCollectClient.common_organization_path + ) + parse_common_organization_path = staticmethod( + ApiHubCollectClient.parse_common_organization_path + ) + common_project_path = staticmethod(ApiHubCollectClient.common_project_path) + parse_common_project_path = staticmethod( + ApiHubCollectClient.parse_common_project_path + ) + common_location_path = staticmethod(ApiHubCollectClient.common_location_path) + parse_common_location_path = staticmethod( + ApiHubCollectClient.parse_common_location_path + ) + + @classmethod + def from_service_account_info(cls, info: dict, *args, **kwargs): + """Creates an instance of this client using the provided credentials + info. + + Args: + info (dict): The service account private key info. + args: Additional arguments to pass to the constructor. + kwargs: Additional arguments to pass to the constructor. + + Returns: + ApiHubCollectAsyncClient: The constructed client. + """ + return ApiHubCollectClient.from_service_account_info.__func__(ApiHubCollectAsyncClient, info, *args, **kwargs) # type: ignore + + @classmethod + def from_service_account_file(cls, filename: str, *args, **kwargs): + """Creates an instance of this client using the provided credentials + file. + + Args: + filename (str): The path to the service account private key json + file. + args: Additional arguments to pass to the constructor. + kwargs: Additional arguments to pass to the constructor. + + Returns: + ApiHubCollectAsyncClient: The constructed client. + """ + return ApiHubCollectClient.from_service_account_file.__func__(ApiHubCollectAsyncClient, filename, *args, **kwargs) # type: ignore + + from_service_account_json = from_service_account_file + + @classmethod + def get_mtls_endpoint_and_cert_source( + cls, client_options: Optional[ClientOptions] = None + ): + """Return the API endpoint and client cert source for mutual TLS. + + The client cert source is determined in the following order: + (1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the + client cert source is None. + (2) if `client_options.client_cert_source` is provided, use the provided one; if the + default client cert source exists, use the default one; otherwise the client cert + source is None. + + The API endpoint is determined in the following order: + (1) if `client_options.api_endpoint` if provided, use the provided one. + (2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the + default mTLS endpoint; if the environment variable is "never", use the default API + endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise + use the default API endpoint. + + More details can be found at https://google.aip.dev/auth/4114. + + Args: + client_options (google.api_core.client_options.ClientOptions): Custom options for the + client. Only the `api_endpoint` and `client_cert_source` properties may be used + in this method. + + Returns: + Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the + client cert source to use. + + Raises: + google.auth.exceptions.MutualTLSChannelError: If any errors happen. + """ + return ApiHubCollectClient.get_mtls_endpoint_and_cert_source(client_options) # type: ignore + + @property + def transport(self) -> ApiHubCollectTransport: + """Returns the transport used by the client instance. + + Returns: + ApiHubCollectTransport: The transport used by the client instance. + """ + return self._client.transport + + @property + def api_endpoint(self): + """Return the API endpoint used by the client instance. + + Returns: + str: The API endpoint used by the client instance. + """ + return self._client._api_endpoint + + @property + def universe_domain(self) -> str: + """Return the universe domain used by the client instance. + + Returns: + str: The universe domain used + by the client instance. + """ + return self._client._universe_domain + + get_transport_class = ApiHubCollectClient.get_transport_class + + def __init__( + self, + *, + credentials: Optional[ga_credentials.Credentials] = None, + transport: Optional[ + Union[str, ApiHubCollectTransport, Callable[..., ApiHubCollectTransport]] + ] = "grpc_asyncio", + client_options: Optional[ClientOptions] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + ) -> None: + """Instantiates the api hub collect async client. + + Args: + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + transport (Optional[Union[str,ApiHubCollectTransport,Callable[..., ApiHubCollectTransport]]]): + The transport to use, or a Callable that constructs and returns a new transport to use. + If a Callable is given, it will be called with the same set of initialization + arguments as used in the ApiHubCollectTransport constructor. + If set to None, a transport is chosen automatically. + client_options (Optional[Union[google.api_core.client_options.ClientOptions, dict]]): + Custom options for the client. + + 1. The ``api_endpoint`` property can be used to override the + default endpoint provided by the client when ``transport`` is + not explicitly provided. Only if this property is not set and + ``transport`` was not explicitly provided, the endpoint is + determined by the GOOGLE_API_USE_MTLS_ENDPOINT environment + variable, which have one of the following values: + "always" (always use the default mTLS endpoint), "never" (always + use the default regular endpoint) and "auto" (auto-switch to the + default mTLS endpoint if client certificate is present; this is + the default value). + + 2. If the GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable + is "true", then the ``client_cert_source`` property can be used + to provide a client certificate for mTLS 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. + + 3. The ``universe_domain`` property can be used to override the + default "googleapis.com" universe. Note that ``api_endpoint`` + property still takes precedence; and ``universe_domain`` is + currently not supported for mTLS. + + 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: + google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport + creation failed for any reason. + """ + self._client = ApiHubCollectClient( + credentials=credentials, + transport=transport, + client_options=client_options, + client_info=client_info, + ) + + if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ): # pragma: NO COVER + _LOGGER.debug( + "Created client `google.cloud.apihub_v1.ApiHubCollectAsyncClient`.", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubCollect", + "universeDomain": getattr( + self._client._transport._credentials, "universe_domain", "" + ), + "credentialsType": f"{type(self._client._transport._credentials).__module__}.{type(self._client._transport._credentials).__qualname__}", + "credentialsInfo": getattr( + self.transport._credentials, "get_cred_info", lambda: None + )(), + } + if hasattr(self._client._transport, "_credentials") + else { + "serviceName": "google.cloud.apihub.v1.ApiHubCollect", + "credentialsType": None, + }, + ) + + async def collect_api_data( + self, + request: Optional[Union[collect_service.CollectApiDataRequest, dict]] = None, + *, + location: Optional[str] = None, + collection_type: Optional[collect_service.CollectionType] = None, + api_data: Optional[collect_service.ApiData] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> operation_async.AsyncOperation: + r"""Collect API data from a source and push it to Hub's + collect layer. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_collect_api_data(): + # Create a client + client = apihub_v1.ApiHubCollectAsyncClient() + + # Initialize request argument(s) + api_data = apihub_v1.ApiData() + api_data.api_metadata_list.api_metadata.api.display_name = "display_name_value" + + request = apihub_v1.CollectApiDataRequest( + location="location_value", + collection_type="COLLECTION_TYPE_DELETE", + plugin_instance="plugin_instance_value", + action_id="action_id_value", + api_data=api_data, + ) + + # Make the request + operation = client.collect_api_data(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.CollectApiDataRequest, dict]]): + The request object. The CollectApiData method's request. + location (:class:`str`): + Required. The regional location of the API hub instance + and its resources. Format: + ``projects/{project}/locations/{location}`` + + This corresponds to the ``location`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + collection_type (:class:`google.cloud.apihub_v1.types.CollectionType`): + Required. The type of collection. Applies to all entries + in + [api_data][google.cloud.apihub.v1.CollectApiDataRequest.api_data]. + + This corresponds to the ``collection_type`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + api_data (:class:`google.cloud.apihub_v1.types.ApiData`): + Required. The API data to be + collected. + + This corresponds to the ``api_data`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + + Returns: + google.api_core.operation_async.AsyncOperation: + An object representing a long-running operation. + + The result type for the operation will be + :class:`google.cloud.apihub_v1.types.CollectApiDataResponse` + The CollectApiData method's response. + + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [location, collection_type, api_data] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, collect_service.CollectApiDataRequest): + request = collect_service.CollectApiDataRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if location is not None: + request.location = location + if collection_type is not None: + request.collection_type = collection_type + if api_data is not None: + request.api_data = api_data + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.collect_api_data + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("location", request.location),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Wrap the response in an operation future. + response = operation_async.from_gapic( + response, + self._client._transport.operations_client, + collect_service.CollectApiDataResponse, + metadata_type=common_fields.OperationMetadata, + ) + + # Done; return the response. + return response + + async def list_operations( + self, + request: Optional[operations_pb2.ListOperationsRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> operations_pb2.ListOperationsResponse: + r"""Lists operations that match the specified filter in the request. + + Args: + request (:class:`~.operations_pb2.ListOperationsRequest`): + The request object. Request message for + `ListOperations` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + ~.operations_pb2.ListOperationsResponse: + Response message for ``ListOperations`` method. + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = operations_pb2.ListOperationsRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.list_operations] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def get_operation( + self, + request: Optional[operations_pb2.GetOperationRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> operations_pb2.Operation: + r"""Gets the latest state of a long-running operation. + + Args: + request (:class:`~.operations_pb2.GetOperationRequest`): + The request object. Request message for + `GetOperation` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + ~.operations_pb2.Operation: + An ``Operation`` object. + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = operations_pb2.GetOperationRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.get_operation] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def delete_operation( + self, + request: Optional[operations_pb2.DeleteOperationRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> None: + r"""Deletes a long-running operation. + + This method indicates that the client is no longer interested + in the operation result. It does not cancel the operation. + If the server doesn't support this method, it returns + `google.rpc.Code.UNIMPLEMENTED`. + + Args: + request (:class:`~.operations_pb2.DeleteOperationRequest`): + The request object. Request message for + `DeleteOperation` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + None + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = operations_pb2.DeleteOperationRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.delete_operation] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + async def cancel_operation( + self, + request: Optional[operations_pb2.CancelOperationRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> None: + r"""Starts asynchronous cancellation on a long-running operation. + + The server makes a best effort to cancel the operation, but success + is not guaranteed. If the server doesn't support this method, it returns + `google.rpc.Code.UNIMPLEMENTED`. + + Args: + request (:class:`~.operations_pb2.CancelOperationRequest`): + The request object. Request message for + `CancelOperation` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + None + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = operations_pb2.CancelOperationRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.cancel_operation] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + async def get_location( + self, + request: Optional[locations_pb2.GetLocationRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> locations_pb2.Location: + r"""Gets information about a location. + + Args: + request (:class:`~.location_pb2.GetLocationRequest`): + The request object. Request message for + `GetLocation` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + ~.location_pb2.Location: + Location object. + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = locations_pb2.GetLocationRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.get_location] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def list_locations( + self, + request: Optional[locations_pb2.ListLocationsRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> locations_pb2.ListLocationsResponse: + r"""Lists information about the supported locations for this service. + + Args: + request (:class:`~.location_pb2.ListLocationsRequest`): + The request object. Request message for + `ListLocations` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + ~.location_pb2.ListLocationsResponse: + Response message for ``ListLocations`` method. + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = locations_pb2.ListLocationsRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.list_locations] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def __aenter__(self) -> "ApiHubCollectAsyncClient": + return self + + async def __aexit__(self, exc_type, exc, tb): + await self.transport.close() + + +DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=package_version.__version__ +) + +if hasattr(DEFAULT_CLIENT_INFO, "protobuf_runtime_version"): # pragma: NO COVER + DEFAULT_CLIENT_INFO.protobuf_runtime_version = google.protobuf.__version__ + + +__all__ = ("ApiHubCollectAsyncClient",) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/client.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/client.py index 620764107d0d..6e122a108eca 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/client.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/client.py @@ -69,6 +69,8 @@ from google.cloud.apihub_v1.types import collect_service, common_fields from .transports.base import DEFAULT_CLIENT_INFO, ApiHubCollectTransport +from .transports.grpc import ApiHubCollectGrpcTransport +from .transports.grpc_asyncio import ApiHubCollectGrpcAsyncIOTransport from .transports.rest import ApiHubCollectRestTransport @@ -81,6 +83,8 @@ class ApiHubCollectClientMeta(type): """ _transport_registry = OrderedDict() # type: Dict[str, Type[ApiHubCollectTransport]] + _transport_registry["grpc"] = ApiHubCollectGrpcTransport + _transport_registry["grpc_asyncio"] = ApiHubCollectGrpcAsyncIOTransport _transport_registry["rest"] = ApiHubCollectRestTransport def get_transport_class( diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/transports/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/transports/__init__.py index 946eb5be4e61..5a0cbef516e5 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/transports/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/transports/__init__.py @@ -17,14 +17,20 @@ from typing import Dict, Type from .base import ApiHubCollectTransport +from .grpc import ApiHubCollectGrpcTransport +from .grpc_asyncio import ApiHubCollectGrpcAsyncIOTransport from .rest import ApiHubCollectRestInterceptor, ApiHubCollectRestTransport # Compile a registry of transports. _transport_registry = OrderedDict() # type: Dict[str, Type[ApiHubCollectTransport]] +_transport_registry["grpc"] = ApiHubCollectGrpcTransport +_transport_registry["grpc_asyncio"] = ApiHubCollectGrpcAsyncIOTransport _transport_registry["rest"] = ApiHubCollectRestTransport __all__ = ( "ApiHubCollectTransport", + "ApiHubCollectGrpcTransport", + "ApiHubCollectGrpcAsyncIOTransport", "ApiHubCollectRestTransport", "ApiHubCollectRestInterceptor", ) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/transports/grpc.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/transports/grpc.py new file mode 100644 index 000000000000..58e29da2f5d7 --- /dev/null +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/transports/grpc.py @@ -0,0 +1,484 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +import json +import logging as std_logging +import pickle +from typing import Callable, Dict, Optional, Sequence, Tuple, Union +import warnings + +from google.api_core import gapic_v1, grpc_helpers, operations_v1 +import google.auth # type: ignore +from google.auth import credentials as ga_credentials # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore +from google.cloud.location import locations_pb2 # type: ignore +from google.longrunning import operations_pb2 # type: ignore +from google.protobuf.json_format import MessageToJson +import google.protobuf.message +import grpc # type: ignore +import proto # type: ignore + +from google.cloud.apihub_v1.types import collect_service + +from .base import DEFAULT_CLIENT_INFO, ApiHubCollectTransport + +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class _LoggingClientInterceptor(grpc.UnaryUnaryClientInterceptor): # pragma: NO COVER + def intercept_unary_unary(self, continuation, client_call_details, request): + logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ) + if logging_enabled: # pragma: NO COVER + request_metadata = client_call_details.metadata + if isinstance(request, proto.Message): + request_payload = type(request).to_json(request) + elif isinstance(request, google.protobuf.message.Message): + request_payload = MessageToJson(request) + else: + request_payload = f"{type(request).__name__}: {pickle.dumps(request)}" + + request_metadata = { + key: value.decode("utf-8") if isinstance(value, bytes) else value + for key, value in request_metadata + } + grpc_request = { + "payload": request_payload, + "requestMethod": "grpc", + "metadata": dict(request_metadata), + } + _LOGGER.debug( + f"Sending request for {client_call_details.method}", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubCollect", + "rpcName": str(client_call_details.method), + "request": grpc_request, + "metadata": grpc_request["metadata"], + }, + ) + response = continuation(client_call_details, request) + if logging_enabled: # pragma: NO COVER + response_metadata = response.trailing_metadata() + # Convert gRPC metadata `` to list of tuples + metadata = ( + dict([(k, str(v)) for k, v in response_metadata]) + if response_metadata + else None + ) + result = response.result() + if isinstance(result, proto.Message): + response_payload = type(result).to_json(result) + elif isinstance(result, google.protobuf.message.Message): + response_payload = MessageToJson(result) + else: + response_payload = f"{type(result).__name__}: {pickle.dumps(result)}" + grpc_response = { + "payload": response_payload, + "metadata": metadata, + "status": "OK", + } + _LOGGER.debug( + f"Received response for {client_call_details.method}.", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubCollect", + "rpcName": client_call_details.method, + "response": grpc_response, + "metadata": grpc_response["metadata"], + }, + ) + return response + + +class ApiHubCollectGrpcTransport(ApiHubCollectTransport): + """gRPC backend transport for ApiHubCollect. + + This service exposes methods used for collecting various + types of data from different first party and third party sources + and push it to Hub's collect layer. + + This class defines the same methods as the primary client, so the + primary client can load the underlying transport implementation + and call it. + + It sends protocol buffers over the wire using gRPC (which is built on + top of HTTP/2); the ``grpcio`` package must be installed. + """ + + _stubs: Dict[str, Callable] + + def __init__( + self, + *, + host: str = "apihub.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + channel: Optional[Union[grpc.Channel, Callable[..., grpc.Channel]]] = None, + api_mtls_endpoint: Optional[str] = None, + client_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + ssl_channel_credentials: Optional[grpc.ChannelCredentials] = None, + client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + quota_project_id: Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + always_use_jwt_access: Optional[bool] = False, + api_audience: Optional[str] = None, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): + The hostname to connect to (default: 'apihub.googleapis.com'). + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + This argument is ignored if a ``channel`` instance is provided. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is ignored if a ``channel`` instance is provided. + scopes (Optional(Sequence[str])): A list of scopes. This argument is + ignored if a ``channel`` instance is provided. + channel (Optional[Union[grpc.Channel, Callable[..., grpc.Channel]]]): + A ``Channel`` instance through which to make calls, or a Callable + that constructs and returns one. If set to None, ``self.create_channel`` + is used to create the channel. If a Callable is given, it will be called + with the same arguments as used in ``self.create_channel``. + 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 application default SSL credentials. + 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 the grpc channel. It is ignored if a ``channel`` instance is provided. + client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]): + A callback to provide client certificate bytes and private key bytes, + both in PEM format. It is used to configure a mutual TLS channel. It is + ignored if a ``channel`` instance or ``ssl_channel_credentials`` 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 + your own client library. + always_use_jwt_access (Optional[bool]): Whether self signed JWT should + be used for service account credentials. + + Raises: + google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport + creation failed for any reason. + google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` + and ``credentials_file`` are passed. + """ + self._grpc_channel = None + self._ssl_channel_credentials = ssl_channel_credentials + self._stubs: Dict[str, Callable] = {} + self._operations_client: Optional[operations_v1.OperationsClient] = None + + if api_mtls_endpoint: + warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning) + if client_cert_source: + warnings.warn("client_cert_source is deprecated", DeprecationWarning) + + if isinstance(channel, grpc.Channel): + # Ignore credentials if a channel was passed. + credentials = None + self._ignore_credentials = True + # If a channel was explicitly provided, set it. + self._grpc_channel = channel + self._ssl_channel_credentials = None + + else: + if api_mtls_endpoint: + host = api_mtls_endpoint + + # Create SSL credentials with client_cert_source or application + # default SSL credentials. + if client_cert_source: + cert, key = client_cert_source() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + else: + self._ssl_channel_credentials = SslCredentials().ssl_credentials + + else: + if client_cert_source_for_mtls and not ssl_channel_credentials: + cert, key = client_cert_source_for_mtls() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + + # The base transport sets the host, credentials and scopes + super().__init__( + host=host, + credentials=credentials, + credentials_file=credentials_file, + scopes=scopes, + quota_project_id=quota_project_id, + client_info=client_info, + always_use_jwt_access=always_use_jwt_access, + api_audience=api_audience, + ) + + if not self._grpc_channel: + # initialize with the provided callable or the default channel + channel_init = channel or type(self).create_channel + self._grpc_channel = channel_init( + self._host, + # use the credentials which are saved + credentials=self._credentials, + # Set ``credentials_file`` to ``None`` here as + # the credentials that we saved earlier should be used. + credentials_file=None, + scopes=self._scopes, + ssl_credentials=self._ssl_channel_credentials, + quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + self._interceptor = _LoggingClientInterceptor() + self._logged_channel = grpc.intercept_channel( + self._grpc_channel, self._interceptor + ) + + # Wrap messages. This must be done after self._logged_channel exists + self._prep_wrapped_messages(client_info) + + @classmethod + def create_channel( + cls, + host: str = "apihub.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + quota_project_id: Optional[str] = None, + **kwargs, + ) -> grpc.Channel: + """Create and return a gRPC channel object. + Args: + host (Optional[str]): The host for the channel to use. + credentials (Optional[~.Credentials]): The + authorization credentials to attach to requests. These + credentials identify this application to the service. If + none are specified, the client will attempt to ascertain + the credentials from the environment. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is mutually exclusive with credentials. + scopes (Optional[Sequence[str]]): A optional list of scopes needed for this + service. These are only used when credentials are not specified and + are passed to :func:`google.auth.default`. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + kwargs (Optional[dict]): Keyword arguments, which are passed to the + channel creation. + Returns: + grpc.Channel: A gRPC channel object. + + Raises: + google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` + and ``credentials_file`` are passed. + """ + + return grpc_helpers.create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + quota_project_id=quota_project_id, + default_scopes=cls.AUTH_SCOPES, + scopes=scopes, + default_host=cls.DEFAULT_HOST, + **kwargs, + ) + + @property + def grpc_channel(self) -> grpc.Channel: + """Return the channel designed to connect to this service.""" + return self._grpc_channel + + @property + def operations_client(self) -> operations_v1.OperationsClient: + """Create the client designed to process long-running operations. + + This property caches on the instance; repeated calls return the same + client. + """ + # Quick check: Only create a new client if we do not already have one. + if self._operations_client is None: + self._operations_client = operations_v1.OperationsClient( + self._logged_channel + ) + + # Return the client from cache. + return self._operations_client + + @property + def collect_api_data( + self, + ) -> Callable[[collect_service.CollectApiDataRequest], operations_pb2.Operation]: + r"""Return a callable for the collect api data method over gRPC. + + Collect API data from a source and push it to Hub's + collect layer. + + Returns: + Callable[[~.CollectApiDataRequest], + ~.Operation]: + 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 "collect_api_data" not in self._stubs: + self._stubs["collect_api_data"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubCollect/CollectApiData", + request_serializer=collect_service.CollectApiDataRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["collect_api_data"] + + def close(self): + self._logged_channel.close() + + @property + def delete_operation( + self, + ) -> Callable[[operations_pb2.DeleteOperationRequest], None]: + r"""Return a callable for the delete_operation method over gRPC.""" + # 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 "delete_operation" not in self._stubs: + self._stubs["delete_operation"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/DeleteOperation", + request_serializer=operations_pb2.DeleteOperationRequest.SerializeToString, + response_deserializer=None, + ) + return self._stubs["delete_operation"] + + @property + def cancel_operation( + self, + ) -> Callable[[operations_pb2.CancelOperationRequest], None]: + r"""Return a callable for the cancel_operation method over gRPC.""" + # 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 "cancel_operation" not in self._stubs: + self._stubs["cancel_operation"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/CancelOperation", + request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, + response_deserializer=None, + ) + return self._stubs["cancel_operation"] + + @property + def get_operation( + self, + ) -> Callable[[operations_pb2.GetOperationRequest], operations_pb2.Operation]: + r"""Return a callable for the get_operation method over gRPC.""" + # 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 "get_operation" not in self._stubs: + self._stubs["get_operation"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/GetOperation", + request_serializer=operations_pb2.GetOperationRequest.SerializeToString, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["get_operation"] + + @property + def list_operations( + self, + ) -> Callable[ + [operations_pb2.ListOperationsRequest], operations_pb2.ListOperationsResponse + ]: + r"""Return a callable for the list_operations method over gRPC.""" + # 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 "list_operations" not in self._stubs: + self._stubs["list_operations"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/ListOperations", + request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, + response_deserializer=operations_pb2.ListOperationsResponse.FromString, + ) + return self._stubs["list_operations"] + + @property + def list_locations( + self, + ) -> Callable[ + [locations_pb2.ListLocationsRequest], locations_pb2.ListLocationsResponse + ]: + r"""Return a callable for the list locations method over gRPC.""" + # 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 "list_locations" not in self._stubs: + self._stubs["list_locations"] = self._logged_channel.unary_unary( + "/google.cloud.location.Locations/ListLocations", + request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, + response_deserializer=locations_pb2.ListLocationsResponse.FromString, + ) + return self._stubs["list_locations"] + + @property + def get_location( + self, + ) -> Callable[[locations_pb2.GetLocationRequest], locations_pb2.Location]: + r"""Return a callable for the list locations method over gRPC.""" + # 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 "get_location" not in self._stubs: + self._stubs["get_location"] = self._logged_channel.unary_unary( + "/google.cloud.location.Locations/GetLocation", + request_serializer=locations_pb2.GetLocationRequest.SerializeToString, + response_deserializer=locations_pb2.Location.FromString, + ) + return self._stubs["get_location"] + + @property + def kind(self) -> str: + return "grpc" + + +__all__ = ("ApiHubCollectGrpcTransport",) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/transports/grpc_asyncio.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/transports/grpc_asyncio.py new file mode 100644 index 000000000000..7af096010941 --- /dev/null +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_collect/transports/grpc_asyncio.py @@ -0,0 +1,539 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +import inspect +import json +import logging as std_logging +import pickle +from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union +import warnings + +from google.api_core import exceptions as core_exceptions +from google.api_core import gapic_v1, grpc_helpers_async, operations_v1 +from google.api_core import retry_async as retries +from google.auth import credentials as ga_credentials # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore +from google.cloud.location import locations_pb2 # type: ignore +from google.longrunning import operations_pb2 # type: ignore +from google.protobuf.json_format import MessageToJson +import google.protobuf.message +import grpc # type: ignore +from grpc.experimental import aio # type: ignore +import proto # type: ignore + +from google.cloud.apihub_v1.types import collect_service + +from .base import DEFAULT_CLIENT_INFO, ApiHubCollectTransport +from .grpc import ApiHubCollectGrpcTransport + +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class _LoggingClientAIOInterceptor( + grpc.aio.UnaryUnaryClientInterceptor +): # pragma: NO COVER + async def intercept_unary_unary(self, continuation, client_call_details, request): + logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ) + if logging_enabled: # pragma: NO COVER + request_metadata = client_call_details.metadata + if isinstance(request, proto.Message): + request_payload = type(request).to_json(request) + elif isinstance(request, google.protobuf.message.Message): + request_payload = MessageToJson(request) + else: + request_payload = f"{type(request).__name__}: {pickle.dumps(request)}" + + request_metadata = { + key: value.decode("utf-8") if isinstance(value, bytes) else value + for key, value in request_metadata + } + grpc_request = { + "payload": request_payload, + "requestMethod": "grpc", + "metadata": dict(request_metadata), + } + _LOGGER.debug( + f"Sending request for {client_call_details.method}", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubCollect", + "rpcName": str(client_call_details.method), + "request": grpc_request, + "metadata": grpc_request["metadata"], + }, + ) + response = await continuation(client_call_details, request) + if logging_enabled: # pragma: NO COVER + response_metadata = await response.trailing_metadata() + # Convert gRPC metadata `` to list of tuples + metadata = ( + dict([(k, str(v)) for k, v in response_metadata]) + if response_metadata + else None + ) + result = await response + if isinstance(result, proto.Message): + response_payload = type(result).to_json(result) + elif isinstance(result, google.protobuf.message.Message): + response_payload = MessageToJson(result) + else: + response_payload = f"{type(result).__name__}: {pickle.dumps(result)}" + grpc_response = { + "payload": response_payload, + "metadata": metadata, + "status": "OK", + } + _LOGGER.debug( + f"Received response to rpc {client_call_details.method}.", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubCollect", + "rpcName": str(client_call_details.method), + "response": grpc_response, + "metadata": grpc_response["metadata"], + }, + ) + return response + + +class ApiHubCollectGrpcAsyncIOTransport(ApiHubCollectTransport): + """gRPC AsyncIO backend transport for ApiHubCollect. + + This service exposes methods used for collecting various + types of data from different first party and third party sources + and push it to Hub's collect layer. + + This class defines the same methods as the primary client, so the + primary client can load the underlying transport implementation + and call it. + + It sends protocol buffers over the wire using gRPC (which is built on + top of HTTP/2); the ``grpcio`` package must be installed. + """ + + _grpc_channel: aio.Channel + _stubs: Dict[str, Callable] = {} + + @classmethod + def create_channel( + cls, + host: str = "apihub.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + quota_project_id: Optional[str] = None, + **kwargs, + ) -> aio.Channel: + """Create and return a gRPC AsyncIO channel object. + Args: + host (Optional[str]): The host for the channel to use. + credentials (Optional[~.Credentials]): The + authorization credentials to attach to requests. These + credentials identify this application to the service. If + none are specified, the client will attempt to ascertain + the credentials from the environment. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + scopes (Optional[Sequence[str]]): A optional list of scopes needed for this + service. These are only used when credentials are not specified and + are passed to :func:`google.auth.default`. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + kwargs (Optional[dict]): Keyword arguments, which are passed to the + channel creation. + Returns: + aio.Channel: A gRPC AsyncIO channel object. + """ + + return grpc_helpers_async.create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + quota_project_id=quota_project_id, + default_scopes=cls.AUTH_SCOPES, + scopes=scopes, + default_host=cls.DEFAULT_HOST, + **kwargs, + ) + + def __init__( + self, + *, + host: str = "apihub.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + channel: Optional[Union[aio.Channel, Callable[..., aio.Channel]]] = None, + api_mtls_endpoint: Optional[str] = None, + client_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + ssl_channel_credentials: Optional[grpc.ChannelCredentials] = None, + client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + quota_project_id: Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + always_use_jwt_access: Optional[bool] = False, + api_audience: Optional[str] = None, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): + The hostname to connect to (default: 'apihub.googleapis.com'). + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + This argument is ignored if a ``channel`` instance is provided. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is ignored if a ``channel`` instance is provided. + scopes (Optional[Sequence[str]]): A optional list of scopes needed for this + service. These are only used when credentials are not specified and + are passed to :func:`google.auth.default`. + channel (Optional[Union[aio.Channel, Callable[..., aio.Channel]]]): + A ``Channel`` instance through which to make calls, or a Callable + that constructs and returns one. If set to None, ``self.create_channel`` + is used to create the channel. If a Callable is given, it will be called + with the same arguments as used in ``self.create_channel``. + 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 application default SSL credentials. + 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 the grpc channel. It is ignored if a ``channel`` instance is provided. + client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]): + A callback to provide client certificate bytes and private key bytes, + both in PEM format. It is used to configure a mutual TLS channel. It is + ignored if a ``channel`` instance or ``ssl_channel_credentials`` 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 + your own client library. + always_use_jwt_access (Optional[bool]): Whether self signed JWT should + be used for service account credentials. + + Raises: + google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport + creation failed for any reason. + google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` + and ``credentials_file`` are passed. + """ + self._grpc_channel = None + self._ssl_channel_credentials = ssl_channel_credentials + self._stubs: Dict[str, Callable] = {} + self._operations_client: Optional[operations_v1.OperationsAsyncClient] = None + + if api_mtls_endpoint: + warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning) + if client_cert_source: + warnings.warn("client_cert_source is deprecated", DeprecationWarning) + + if isinstance(channel, aio.Channel): + # Ignore credentials if a channel was passed. + credentials = None + self._ignore_credentials = True + # If a channel was explicitly provided, set it. + self._grpc_channel = channel + self._ssl_channel_credentials = None + else: + if api_mtls_endpoint: + host = api_mtls_endpoint + + # Create SSL credentials with client_cert_source or application + # default SSL credentials. + if client_cert_source: + cert, key = client_cert_source() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + else: + self._ssl_channel_credentials = SslCredentials().ssl_credentials + + else: + if client_cert_source_for_mtls and not ssl_channel_credentials: + cert, key = client_cert_source_for_mtls() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + + # The base transport sets the host, credentials and scopes + super().__init__( + host=host, + credentials=credentials, + credentials_file=credentials_file, + scopes=scopes, + quota_project_id=quota_project_id, + client_info=client_info, + always_use_jwt_access=always_use_jwt_access, + api_audience=api_audience, + ) + + if not self._grpc_channel: + # initialize with the provided callable or the default channel + channel_init = channel or type(self).create_channel + self._grpc_channel = channel_init( + self._host, + # use the credentials which are saved + credentials=self._credentials, + # Set ``credentials_file`` to ``None`` here as + # the credentials that we saved earlier should be used. + credentials_file=None, + scopes=self._scopes, + ssl_credentials=self._ssl_channel_credentials, + quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + self._interceptor = _LoggingClientAIOInterceptor() + self._grpc_channel._unary_unary_interceptors.append(self._interceptor) + self._logged_channel = self._grpc_channel + self._wrap_with_kind = ( + "kind" in inspect.signature(gapic_v1.method_async.wrap_method).parameters + ) + # Wrap messages. This must be done after self._logged_channel exists + self._prep_wrapped_messages(client_info) + + @property + def grpc_channel(self) -> aio.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 from cache. + return self._grpc_channel + + @property + def operations_client(self) -> operations_v1.OperationsAsyncClient: + """Create the client designed to process long-running operations. + + This property caches on the instance; repeated calls return the same + client. + """ + # Quick check: Only create a new client if we do not already have one. + if self._operations_client is None: + self._operations_client = operations_v1.OperationsAsyncClient( + self._logged_channel + ) + + # Return the client from cache. + return self._operations_client + + @property + def collect_api_data( + self, + ) -> Callable[ + [collect_service.CollectApiDataRequest], Awaitable[operations_pb2.Operation] + ]: + r"""Return a callable for the collect api data method over gRPC. + + Collect API data from a source and push it to Hub's + collect layer. + + Returns: + Callable[[~.CollectApiDataRequest], + Awaitable[~.Operation]]: + 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 "collect_api_data" not in self._stubs: + self._stubs["collect_api_data"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubCollect/CollectApiData", + request_serializer=collect_service.CollectApiDataRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["collect_api_data"] + + def _prep_wrapped_messages(self, client_info): + """Precompute the wrapped methods, overriding the base class method to use async wrappers.""" + self._wrapped_methods = { + self.collect_api_data: self._wrap_method( + self.collect_api_data, + default_timeout=None, + client_info=client_info, + ), + self.get_location: self._wrap_method( + self.get_location, + default_timeout=None, + client_info=client_info, + ), + self.list_locations: self._wrap_method( + self.list_locations, + default_timeout=None, + client_info=client_info, + ), + self.cancel_operation: self._wrap_method( + self.cancel_operation, + default_timeout=None, + client_info=client_info, + ), + self.delete_operation: self._wrap_method( + self.delete_operation, + default_timeout=None, + client_info=client_info, + ), + self.get_operation: self._wrap_method( + self.get_operation, + default_timeout=None, + client_info=client_info, + ), + self.list_operations: self._wrap_method( + self.list_operations, + default_timeout=None, + client_info=client_info, + ), + } + + def _wrap_method(self, func, *args, **kwargs): + if self._wrap_with_kind: # pragma: NO COVER + kwargs["kind"] = self.kind + return gapic_v1.method_async.wrap_method(func, *args, **kwargs) + + def close(self): + return self._logged_channel.close() + + @property + def kind(self) -> str: + return "grpc_asyncio" + + @property + def delete_operation( + self, + ) -> Callable[[operations_pb2.DeleteOperationRequest], None]: + r"""Return a callable for the delete_operation method over gRPC.""" + # 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 "delete_operation" not in self._stubs: + self._stubs["delete_operation"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/DeleteOperation", + request_serializer=operations_pb2.DeleteOperationRequest.SerializeToString, + response_deserializer=None, + ) + return self._stubs["delete_operation"] + + @property + def cancel_operation( + self, + ) -> Callable[[operations_pb2.CancelOperationRequest], None]: + r"""Return a callable for the cancel_operation method over gRPC.""" + # 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 "cancel_operation" not in self._stubs: + self._stubs["cancel_operation"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/CancelOperation", + request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, + response_deserializer=None, + ) + return self._stubs["cancel_operation"] + + @property + def get_operation( + self, + ) -> Callable[[operations_pb2.GetOperationRequest], operations_pb2.Operation]: + r"""Return a callable for the get_operation method over gRPC.""" + # 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 "get_operation" not in self._stubs: + self._stubs["get_operation"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/GetOperation", + request_serializer=operations_pb2.GetOperationRequest.SerializeToString, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["get_operation"] + + @property + def list_operations( + self, + ) -> Callable[ + [operations_pb2.ListOperationsRequest], operations_pb2.ListOperationsResponse + ]: + r"""Return a callable for the list_operations method over gRPC.""" + # 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 "list_operations" not in self._stubs: + self._stubs["list_operations"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/ListOperations", + request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, + response_deserializer=operations_pb2.ListOperationsResponse.FromString, + ) + return self._stubs["list_operations"] + + @property + def list_locations( + self, + ) -> Callable[ + [locations_pb2.ListLocationsRequest], locations_pb2.ListLocationsResponse + ]: + r"""Return a callable for the list locations method over gRPC.""" + # 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 "list_locations" not in self._stubs: + self._stubs["list_locations"] = self._logged_channel.unary_unary( + "/google.cloud.location.Locations/ListLocations", + request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, + response_deserializer=locations_pb2.ListLocationsResponse.FromString, + ) + return self._stubs["list_locations"] + + @property + def get_location( + self, + ) -> Callable[[locations_pb2.GetLocationRequest], locations_pb2.Location]: + r"""Return a callable for the list locations method over gRPC.""" + # 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 "get_location" not in self._stubs: + self._stubs["get_location"] = self._logged_channel.unary_unary( + "/google.cloud.location.Locations/GetLocation", + request_serializer=locations_pb2.GetLocationRequest.SerializeToString, + response_deserializer=locations_pb2.Location.FromString, + ) + return self._stubs["get_location"] + + +__all__ = ("ApiHubCollectGrpcAsyncIOTransport",) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/__init__.py index e9a6881317fc..610b4f87877f 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/__init__.py @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from .async_client import ApiHubCurateAsyncClient from .client import ApiHubCurateClient -__all__ = ("ApiHubCurateClient",) +__all__ = ( + "ApiHubCurateClient", + "ApiHubCurateAsyncClient", +) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/async_client.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/async_client.py new file mode 100644 index 000000000000..8b7a73079607 --- /dev/null +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/async_client.py @@ -0,0 +1,1265 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +from collections import OrderedDict +import logging as std_logging +import re +from typing import ( + Callable, + Dict, + Mapping, + MutableMapping, + MutableSequence, + Optional, + Sequence, + Tuple, + Type, + Union, +) + +from google.api_core import exceptions as core_exceptions +from google.api_core import gapic_v1 +from google.api_core import retry_async as retries +from google.api_core.client_options import ClientOptions +from google.auth import credentials as ga_credentials # type: ignore +from google.oauth2 import service_account # type: ignore +import google.protobuf + +from google.cloud.apihub_v1 import gapic_version as package_version + +try: + OptionalRetry = Union[retries.AsyncRetry, gapic_v1.method._MethodDefault, None] +except AttributeError: # pragma: NO COVER + OptionalRetry = Union[retries.AsyncRetry, object, None] # type: ignore + +from google.cloud.location import locations_pb2 # type: ignore +from google.longrunning import operations_pb2 # type: ignore +from google.protobuf import field_mask_pb2 # type: ignore +from google.protobuf import timestamp_pb2 # type: ignore + +from google.cloud.apihub_v1.services.api_hub_curate import pagers +from google.cloud.apihub_v1.types import curate_service + +from .client import ApiHubCurateClient +from .transports.base import DEFAULT_CLIENT_INFO, ApiHubCurateTransport +from .transports.grpc_asyncio import ApiHubCurateGrpcAsyncIOTransport + +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class ApiHubCurateAsyncClient: + """This service is used for managing curations for processing + API data consumed from collect layer. + """ + + _client: ApiHubCurateClient + + # Copy defaults from the synchronous client for use here. + # Note: DEFAULT_ENDPOINT is deprecated. Use _DEFAULT_ENDPOINT_TEMPLATE instead. + DEFAULT_ENDPOINT = ApiHubCurateClient.DEFAULT_ENDPOINT + DEFAULT_MTLS_ENDPOINT = ApiHubCurateClient.DEFAULT_MTLS_ENDPOINT + _DEFAULT_ENDPOINT_TEMPLATE = ApiHubCurateClient._DEFAULT_ENDPOINT_TEMPLATE + _DEFAULT_UNIVERSE = ApiHubCurateClient._DEFAULT_UNIVERSE + + curation_path = staticmethod(ApiHubCurateClient.curation_path) + parse_curation_path = staticmethod(ApiHubCurateClient.parse_curation_path) + plugin_instance_path = staticmethod(ApiHubCurateClient.plugin_instance_path) + parse_plugin_instance_path = staticmethod( + ApiHubCurateClient.parse_plugin_instance_path + ) + common_billing_account_path = staticmethod( + ApiHubCurateClient.common_billing_account_path + ) + parse_common_billing_account_path = staticmethod( + ApiHubCurateClient.parse_common_billing_account_path + ) + common_folder_path = staticmethod(ApiHubCurateClient.common_folder_path) + parse_common_folder_path = staticmethod(ApiHubCurateClient.parse_common_folder_path) + common_organization_path = staticmethod(ApiHubCurateClient.common_organization_path) + parse_common_organization_path = staticmethod( + ApiHubCurateClient.parse_common_organization_path + ) + common_project_path = staticmethod(ApiHubCurateClient.common_project_path) + parse_common_project_path = staticmethod( + ApiHubCurateClient.parse_common_project_path + ) + common_location_path = staticmethod(ApiHubCurateClient.common_location_path) + parse_common_location_path = staticmethod( + ApiHubCurateClient.parse_common_location_path + ) + + @classmethod + def from_service_account_info(cls, info: dict, *args, **kwargs): + """Creates an instance of this client using the provided credentials + info. + + Args: + info (dict): The service account private key info. + args: Additional arguments to pass to the constructor. + kwargs: Additional arguments to pass to the constructor. + + Returns: + ApiHubCurateAsyncClient: The constructed client. + """ + return ApiHubCurateClient.from_service_account_info.__func__(ApiHubCurateAsyncClient, info, *args, **kwargs) # type: ignore + + @classmethod + def from_service_account_file(cls, filename: str, *args, **kwargs): + """Creates an instance of this client using the provided credentials + file. + + Args: + filename (str): The path to the service account private key json + file. + args: Additional arguments to pass to the constructor. + kwargs: Additional arguments to pass to the constructor. + + Returns: + ApiHubCurateAsyncClient: The constructed client. + """ + return ApiHubCurateClient.from_service_account_file.__func__(ApiHubCurateAsyncClient, filename, *args, **kwargs) # type: ignore + + from_service_account_json = from_service_account_file + + @classmethod + def get_mtls_endpoint_and_cert_source( + cls, client_options: Optional[ClientOptions] = None + ): + """Return the API endpoint and client cert source for mutual TLS. + + The client cert source is determined in the following order: + (1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the + client cert source is None. + (2) if `client_options.client_cert_source` is provided, use the provided one; if the + default client cert source exists, use the default one; otherwise the client cert + source is None. + + The API endpoint is determined in the following order: + (1) if `client_options.api_endpoint` if provided, use the provided one. + (2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the + default mTLS endpoint; if the environment variable is "never", use the default API + endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise + use the default API endpoint. + + More details can be found at https://google.aip.dev/auth/4114. + + Args: + client_options (google.api_core.client_options.ClientOptions): Custom options for the + client. Only the `api_endpoint` and `client_cert_source` properties may be used + in this method. + + Returns: + Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the + client cert source to use. + + Raises: + google.auth.exceptions.MutualTLSChannelError: If any errors happen. + """ + return ApiHubCurateClient.get_mtls_endpoint_and_cert_source(client_options) # type: ignore + + @property + def transport(self) -> ApiHubCurateTransport: + """Returns the transport used by the client instance. + + Returns: + ApiHubCurateTransport: The transport used by the client instance. + """ + return self._client.transport + + @property + def api_endpoint(self): + """Return the API endpoint used by the client instance. + + Returns: + str: The API endpoint used by the client instance. + """ + return self._client._api_endpoint + + @property + def universe_domain(self) -> str: + """Return the universe domain used by the client instance. + + Returns: + str: The universe domain used + by the client instance. + """ + return self._client._universe_domain + + get_transport_class = ApiHubCurateClient.get_transport_class + + def __init__( + self, + *, + credentials: Optional[ga_credentials.Credentials] = None, + transport: Optional[ + Union[str, ApiHubCurateTransport, Callable[..., ApiHubCurateTransport]] + ] = "grpc_asyncio", + client_options: Optional[ClientOptions] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + ) -> None: + """Instantiates the api hub curate async client. + + Args: + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + transport (Optional[Union[str,ApiHubCurateTransport,Callable[..., ApiHubCurateTransport]]]): + The transport to use, or a Callable that constructs and returns a new transport to use. + If a Callable is given, it will be called with the same set of initialization + arguments as used in the ApiHubCurateTransport constructor. + If set to None, a transport is chosen automatically. + client_options (Optional[Union[google.api_core.client_options.ClientOptions, dict]]): + Custom options for the client. + + 1. The ``api_endpoint`` property can be used to override the + default endpoint provided by the client when ``transport`` is + not explicitly provided. Only if this property is not set and + ``transport`` was not explicitly provided, the endpoint is + determined by the GOOGLE_API_USE_MTLS_ENDPOINT environment + variable, which have one of the following values: + "always" (always use the default mTLS endpoint), "never" (always + use the default regular endpoint) and "auto" (auto-switch to the + default mTLS endpoint if client certificate is present; this is + the default value). + + 2. If the GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable + is "true", then the ``client_cert_source`` property can be used + to provide a client certificate for mTLS 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. + + 3. The ``universe_domain`` property can be used to override the + default "googleapis.com" universe. Note that ``api_endpoint`` + property still takes precedence; and ``universe_domain`` is + currently not supported for mTLS. + + 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: + google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport + creation failed for any reason. + """ + self._client = ApiHubCurateClient( + credentials=credentials, + transport=transport, + client_options=client_options, + client_info=client_info, + ) + + if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ): # pragma: NO COVER + _LOGGER.debug( + "Created client `google.cloud.apihub_v1.ApiHubCurateAsyncClient`.", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubCurate", + "universeDomain": getattr( + self._client._transport._credentials, "universe_domain", "" + ), + "credentialsType": f"{type(self._client._transport._credentials).__module__}.{type(self._client._transport._credentials).__qualname__}", + "credentialsInfo": getattr( + self.transport._credentials, "get_cred_info", lambda: None + )(), + } + if hasattr(self._client._transport, "_credentials") + else { + "serviceName": "google.cloud.apihub.v1.ApiHubCurate", + "credentialsType": None, + }, + ) + + async def create_curation( + self, + request: Optional[Union[curate_service.CreateCurationRequest, dict]] = None, + *, + parent: Optional[str] = None, + curation: Optional[curate_service.Curation] = None, + curation_id: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> curate_service.Curation: + r"""Create a curation resource in the API hub. + Once a curation resource is created, plugin instances + can start using it. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_create_curation(): + # Create a client + client = apihub_v1.ApiHubCurateAsyncClient() + + # Initialize request argument(s) + curation = apihub_v1.Curation() + curation.display_name = "display_name_value" + curation.endpoint.application_integration_endpoint_details.uri = "uri_value" + curation.endpoint.application_integration_endpoint_details.trigger_id = "trigger_id_value" + + request = apihub_v1.CreateCurationRequest( + parent="parent_value", + curation=curation, + ) + + # Make the request + response = await client.create_curation(request=request) + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.CreateCurationRequest, dict]]): + The request object. The [CreateCuration][ApiHub.CreateCuration] method's + request. + parent (:class:`str`): + Required. The parent resource for the curation resource. + Format: ``projects/{project}/locations/{location}`` + + This corresponds to the ``parent`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + curation (:class:`google.cloud.apihub_v1.types.Curation`): + Required. The curation resource to + create. + + This corresponds to the ``curation`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + curation_id (:class:`str`): + Optional. The ID to use for the curation resource, which + will become the final component of the curations's + resource name. This field is optional. + + - If provided, the same will be used. The service will + throw an error if the specified ID is already used by + another curation resource in the API hub. + - If not provided, a system generated ID will be used. + + This value should be 4-500 characters, and valid + characters are /[a-z][A-Z][0-9]-\_/. + + This corresponds to the ``curation_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + + Returns: + google.cloud.apihub_v1.types.Curation: + A curation resource in the API Hub. + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [parent, curation, curation_id] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, curate_service.CreateCurationRequest): + request = curate_service.CreateCurationRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent + if curation is not None: + request.curation = curation + if curation_id is not None: + request.curation_id = curation_id + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.create_curation + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def get_curation( + self, + request: Optional[Union[curate_service.GetCurationRequest, dict]] = None, + *, + name: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> curate_service.Curation: + r"""Get curation resource details. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_get_curation(): + # Create a client + client = apihub_v1.ApiHubCurateAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetCurationRequest( + name="name_value", + ) + + # Make the request + response = await client.get_curation(request=request) + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.GetCurationRequest, dict]]): + The request object. The [GetCuration][ApiHub.GetCuration] method's request. + name (:class:`str`): + Required. The name of the curation resource to retrieve. + Format: + ``projects/{project}/locations/{location}/curations/{curation}`` + + This corresponds to the ``name`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + + Returns: + google.cloud.apihub_v1.types.Curation: + A curation resource in the API Hub. + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, curate_service.GetCurationRequest): + request = curate_service.GetCurationRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if name is not None: + request.name = name + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.get_curation + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def list_curations( + self, + request: Optional[Union[curate_service.ListCurationsRequest, dict]] = None, + *, + parent: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> pagers.ListCurationsAsyncPager: + r"""List curation resources in the API hub. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_list_curations(): + # Create a client + client = apihub_v1.ApiHubCurateAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.ListCurationsRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_curations(request=request) + + # Handle the response + async for response in page_result: + print(response) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.ListCurationsRequest, dict]]): + The request object. The [ListCurations][ApiHub.ListCurations] method's + request. + parent (:class:`str`): + Required. The parent, which owns this collection of + curation resources. Format: + ``projects/{project}/locations/{location}`` + + This corresponds to the ``parent`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + + Returns: + google.cloud.apihub_v1.services.api_hub_curate.pagers.ListCurationsAsyncPager: + The [ListCurations][ApiHub.ListCurations] method's + response. + + Iterating over this object will yield results and + resolve additional pages automatically. + + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [parent] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, curate_service.ListCurationsRequest): + request = curate_service.ListCurationsRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.list_curations + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # This method is paged; wrap the response in a pager, which provides + # an `__aiter__` convenience method. + response = pagers.ListCurationsAsyncPager( + method=rpc, + request=request, + response=response, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def update_curation( + self, + request: Optional[Union[curate_service.UpdateCurationRequest, dict]] = None, + *, + curation: Optional[curate_service.Curation] = None, + update_mask: Optional[field_mask_pb2.FieldMask] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> curate_service.Curation: + r"""Update a curation resource in the API hub. The following fields + in the [curation][google.cloud.apihub.v1.Curation] can be + updated: + + - [display_name][google.cloud.apihub.v1.Curation.display_name] + - [description][google.cloud.apihub.v1.Curation.description] + + The + [update_mask][google.cloud.apihub.v1.UpdateApiRequest.update_mask] + should be used to specify the fields being updated. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_update_curation(): + # Create a client + client = apihub_v1.ApiHubCurateAsyncClient() + + # Initialize request argument(s) + curation = apihub_v1.Curation() + curation.display_name = "display_name_value" + curation.endpoint.application_integration_endpoint_details.uri = "uri_value" + curation.endpoint.application_integration_endpoint_details.trigger_id = "trigger_id_value" + + request = apihub_v1.UpdateCurationRequest( + curation=curation, + ) + + # Make the request + response = await client.update_curation(request=request) + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.UpdateCurationRequest, dict]]): + The request object. The [UpdateCuration][ApiHub.UpdateCuration] method's + request. + curation (:class:`google.cloud.apihub_v1.types.Curation`): + Required. The curation resource to update. + + The curation resource's ``name`` field is used to + identify the curation resource to update. Format: + ``projects/{project}/locations/{location}/curations/{curation}`` + + This corresponds to the ``curation`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + update_mask (:class:`google.protobuf.field_mask_pb2.FieldMask`): + Optional. The list of fields to + update. + + This corresponds to the ``update_mask`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + + Returns: + google.cloud.apihub_v1.types.Curation: + A curation resource in the API Hub. + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [curation, update_mask] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, curate_service.UpdateCurationRequest): + request = curate_service.UpdateCurationRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if curation is not None: + request.curation = curation + if update_mask is not None: + request.update_mask = update_mask + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.update_curation + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata( + (("curation.name", request.curation.name),) + ), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def delete_curation( + self, + request: Optional[Union[curate_service.DeleteCurationRequest, dict]] = None, + *, + name: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> None: + r"""Delete a curation resource in the API hub. A curation + can only be deleted if it's not being used by any plugin + instance. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_delete_curation(): + # Create a client + client = apihub_v1.ApiHubCurateAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DeleteCurationRequest( + name="name_value", + ) + + # Make the request + await client.delete_curation(request=request) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.DeleteCurationRequest, dict]]): + The request object. The [DeleteCuration][ApiHub.DeleteCuration] method's + request. + name (:class:`str`): + Required. The name of the curation resource to delete. + Format: + ``projects/{project}/locations/{location}/curations/{curation}`` + + This corresponds to the ``name`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, curate_service.DeleteCurationRequest): + request = curate_service.DeleteCurationRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if name is not None: + request.name = name + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.delete_curation + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + async def list_operations( + self, + request: Optional[operations_pb2.ListOperationsRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> operations_pb2.ListOperationsResponse: + r"""Lists operations that match the specified filter in the request. + + Args: + request (:class:`~.operations_pb2.ListOperationsRequest`): + The request object. Request message for + `ListOperations` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + ~.operations_pb2.ListOperationsResponse: + Response message for ``ListOperations`` method. + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = operations_pb2.ListOperationsRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.list_operations] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def get_operation( + self, + request: Optional[operations_pb2.GetOperationRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> operations_pb2.Operation: + r"""Gets the latest state of a long-running operation. + + Args: + request (:class:`~.operations_pb2.GetOperationRequest`): + The request object. Request message for + `GetOperation` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + ~.operations_pb2.Operation: + An ``Operation`` object. + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = operations_pb2.GetOperationRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.get_operation] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def delete_operation( + self, + request: Optional[operations_pb2.DeleteOperationRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> None: + r"""Deletes a long-running operation. + + This method indicates that the client is no longer interested + in the operation result. It does not cancel the operation. + If the server doesn't support this method, it returns + `google.rpc.Code.UNIMPLEMENTED`. + + Args: + request (:class:`~.operations_pb2.DeleteOperationRequest`): + The request object. Request message for + `DeleteOperation` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + None + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = operations_pb2.DeleteOperationRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.delete_operation] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + async def cancel_operation( + self, + request: Optional[operations_pb2.CancelOperationRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> None: + r"""Starts asynchronous cancellation on a long-running operation. + + The server makes a best effort to cancel the operation, but success + is not guaranteed. If the server doesn't support this method, it returns + `google.rpc.Code.UNIMPLEMENTED`. + + Args: + request (:class:`~.operations_pb2.CancelOperationRequest`): + The request object. Request message for + `CancelOperation` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + None + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = operations_pb2.CancelOperationRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.cancel_operation] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + async def get_location( + self, + request: Optional[locations_pb2.GetLocationRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> locations_pb2.Location: + r"""Gets information about a location. + + Args: + request (:class:`~.location_pb2.GetLocationRequest`): + The request object. Request message for + `GetLocation` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + ~.location_pb2.Location: + Location object. + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = locations_pb2.GetLocationRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.get_location] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def list_locations( + self, + request: Optional[locations_pb2.ListLocationsRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> locations_pb2.ListLocationsResponse: + r"""Lists information about the supported locations for this service. + + Args: + request (:class:`~.location_pb2.ListLocationsRequest`): + The request object. Request message for + `ListLocations` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + ~.location_pb2.ListLocationsResponse: + Response message for ``ListLocations`` method. + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = locations_pb2.ListLocationsRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.list_locations] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def __aenter__(self) -> "ApiHubCurateAsyncClient": + return self + + async def __aexit__(self, exc_type, exc, tb): + await self.transport.close() + + +DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=package_version.__version__ +) + +if hasattr(DEFAULT_CLIENT_INFO, "protobuf_runtime_version"): # pragma: NO COVER + DEFAULT_CLIENT_INFO.protobuf_runtime_version = google.protobuf.__version__ + + +__all__ = ("ApiHubCurateAsyncClient",) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/client.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/client.py index 79b6fcf4c92c..a4dc2df704ad 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/client.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/client.py @@ -70,6 +70,8 @@ from google.cloud.apihub_v1.types import curate_service from .transports.base import DEFAULT_CLIENT_INFO, ApiHubCurateTransport +from .transports.grpc import ApiHubCurateGrpcTransport +from .transports.grpc_asyncio import ApiHubCurateGrpcAsyncIOTransport from .transports.rest import ApiHubCurateRestTransport @@ -82,6 +84,8 @@ class ApiHubCurateClientMeta(type): """ _transport_registry = OrderedDict() # type: Dict[str, Type[ApiHubCurateTransport]] + _transport_registry["grpc"] = ApiHubCurateGrpcTransport + _transport_registry["grpc_asyncio"] = ApiHubCurateGrpcAsyncIOTransport _transport_registry["rest"] = ApiHubCurateRestTransport def get_transport_class( diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/pagers.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/pagers.py index 5e27fa7b9dec..f290d270c004 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/pagers.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/pagers.py @@ -115,3 +115,83 @@ def __iter__(self) -> Iterator[curate_service.Curation]: def __repr__(self) -> str: return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListCurationsAsyncPager: + """A pager for iterating through ``list_curations`` requests. + + This class thinly wraps an initial + :class:`google.cloud.apihub_v1.types.ListCurationsResponse` object, and + provides an ``__aiter__`` method to iterate through its + ``curations`` field. + + If there are more pages, the ``__aiter__`` method will make additional + ``ListCurations`` requests and continue to iterate + through the ``curations`` field on the + corresponding responses. + + All the usual :class:`google.cloud.apihub_v1.types.ListCurationsResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., Awaitable[curate_service.ListCurationsResponse]], + request: curate_service.ListCurationsRequest, + response: curate_service.ListCurationsResponse, + *, + retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = () + ): + """Instantiates the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.apihub_v1.types.ListCurationsRequest): + The initial request object. + response (google.cloud.apihub_v1.types.ListCurationsResponse): + The initial response object. + retry (google.api_core.retry.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + """ + self._method = method + self._request = curate_service.ListCurationsRequest(request) + self._response = response + self._retry = retry + self._timeout = timeout + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + async def pages(self) -> AsyncIterator[curate_service.ListCurationsResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = await self._method( + self._request, + retry=self._retry, + timeout=self._timeout, + metadata=self._metadata, + ) + yield self._response + + def __aiter__(self) -> AsyncIterator[curate_service.Curation]: + async def async_generator(): + async for page in self.pages: + for response in page.curations: + yield response + + return async_generator() + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/transports/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/transports/__init__.py index 915a575fe1ce..859ed2af7519 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/transports/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/transports/__init__.py @@ -17,14 +17,20 @@ from typing import Dict, Type from .base import ApiHubCurateTransport +from .grpc import ApiHubCurateGrpcTransport +from .grpc_asyncio import ApiHubCurateGrpcAsyncIOTransport from .rest import ApiHubCurateRestInterceptor, ApiHubCurateRestTransport # Compile a registry of transports. _transport_registry = OrderedDict() # type: Dict[str, Type[ApiHubCurateTransport]] +_transport_registry["grpc"] = ApiHubCurateGrpcTransport +_transport_registry["grpc_asyncio"] = ApiHubCurateGrpcAsyncIOTransport _transport_registry["rest"] = ApiHubCurateRestTransport __all__ = ( "ApiHubCurateTransport", + "ApiHubCurateGrpcTransport", + "ApiHubCurateGrpcAsyncIOTransport", "ApiHubCurateRestTransport", "ApiHubCurateRestInterceptor", ) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/transports/grpc.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/transports/grpc.py new file mode 100644 index 000000000000..30e1e29c1774 --- /dev/null +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/transports/grpc.py @@ -0,0 +1,585 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +import json +import logging as std_logging +import pickle +from typing import Callable, Dict, Optional, Sequence, Tuple, Union +import warnings + +from google.api_core import gapic_v1, grpc_helpers +import google.auth # type: ignore +from google.auth import credentials as ga_credentials # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore +from google.cloud.location import locations_pb2 # type: ignore +from google.longrunning import operations_pb2 # type: ignore +from google.protobuf import empty_pb2 # type: ignore +from google.protobuf.json_format import MessageToJson +import google.protobuf.message +import grpc # type: ignore +import proto # type: ignore + +from google.cloud.apihub_v1.types import curate_service + +from .base import DEFAULT_CLIENT_INFO, ApiHubCurateTransport + +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class _LoggingClientInterceptor(grpc.UnaryUnaryClientInterceptor): # pragma: NO COVER + def intercept_unary_unary(self, continuation, client_call_details, request): + logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ) + if logging_enabled: # pragma: NO COVER + request_metadata = client_call_details.metadata + if isinstance(request, proto.Message): + request_payload = type(request).to_json(request) + elif isinstance(request, google.protobuf.message.Message): + request_payload = MessageToJson(request) + else: + request_payload = f"{type(request).__name__}: {pickle.dumps(request)}" + + request_metadata = { + key: value.decode("utf-8") if isinstance(value, bytes) else value + for key, value in request_metadata + } + grpc_request = { + "payload": request_payload, + "requestMethod": "grpc", + "metadata": dict(request_metadata), + } + _LOGGER.debug( + f"Sending request for {client_call_details.method}", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubCurate", + "rpcName": str(client_call_details.method), + "request": grpc_request, + "metadata": grpc_request["metadata"], + }, + ) + response = continuation(client_call_details, request) + if logging_enabled: # pragma: NO COVER + response_metadata = response.trailing_metadata() + # Convert gRPC metadata `` to list of tuples + metadata = ( + dict([(k, str(v)) for k, v in response_metadata]) + if response_metadata + else None + ) + result = response.result() + if isinstance(result, proto.Message): + response_payload = type(result).to_json(result) + elif isinstance(result, google.protobuf.message.Message): + response_payload = MessageToJson(result) + else: + response_payload = f"{type(result).__name__}: {pickle.dumps(result)}" + grpc_response = { + "payload": response_payload, + "metadata": metadata, + "status": "OK", + } + _LOGGER.debug( + f"Received response for {client_call_details.method}.", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubCurate", + "rpcName": client_call_details.method, + "response": grpc_response, + "metadata": grpc_response["metadata"], + }, + ) + return response + + +class ApiHubCurateGrpcTransport(ApiHubCurateTransport): + """gRPC backend transport for ApiHubCurate. + + This service is used for managing curations for processing + API data consumed from collect layer. + + This class defines the same methods as the primary client, so the + primary client can load the underlying transport implementation + and call it. + + It sends protocol buffers over the wire using gRPC (which is built on + top of HTTP/2); the ``grpcio`` package must be installed. + """ + + _stubs: Dict[str, Callable] + + def __init__( + self, + *, + host: str = "apihub.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + channel: Optional[Union[grpc.Channel, Callable[..., grpc.Channel]]] = None, + api_mtls_endpoint: Optional[str] = None, + client_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + ssl_channel_credentials: Optional[grpc.ChannelCredentials] = None, + client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + quota_project_id: Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + always_use_jwt_access: Optional[bool] = False, + api_audience: Optional[str] = None, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): + The hostname to connect to (default: 'apihub.googleapis.com'). + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + This argument is ignored if a ``channel`` instance is provided. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is ignored if a ``channel`` instance is provided. + scopes (Optional(Sequence[str])): A list of scopes. This argument is + ignored if a ``channel`` instance is provided. + channel (Optional[Union[grpc.Channel, Callable[..., grpc.Channel]]]): + A ``Channel`` instance through which to make calls, or a Callable + that constructs and returns one. If set to None, ``self.create_channel`` + is used to create the channel. If a Callable is given, it will be called + with the same arguments as used in ``self.create_channel``. + 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 application default SSL credentials. + 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 the grpc channel. It is ignored if a ``channel`` instance is provided. + client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]): + A callback to provide client certificate bytes and private key bytes, + both in PEM format. It is used to configure a mutual TLS channel. It is + ignored if a ``channel`` instance or ``ssl_channel_credentials`` 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 + your own client library. + always_use_jwt_access (Optional[bool]): Whether self signed JWT should + be used for service account credentials. + + Raises: + google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport + creation failed for any reason. + google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` + and ``credentials_file`` are passed. + """ + self._grpc_channel = None + self._ssl_channel_credentials = ssl_channel_credentials + self._stubs: Dict[str, Callable] = {} + + if api_mtls_endpoint: + warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning) + if client_cert_source: + warnings.warn("client_cert_source is deprecated", DeprecationWarning) + + if isinstance(channel, grpc.Channel): + # Ignore credentials if a channel was passed. + credentials = None + self._ignore_credentials = True + # If a channel was explicitly provided, set it. + self._grpc_channel = channel + self._ssl_channel_credentials = None + + else: + if api_mtls_endpoint: + host = api_mtls_endpoint + + # Create SSL credentials with client_cert_source or application + # default SSL credentials. + if client_cert_source: + cert, key = client_cert_source() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + else: + self._ssl_channel_credentials = SslCredentials().ssl_credentials + + else: + if client_cert_source_for_mtls and not ssl_channel_credentials: + cert, key = client_cert_source_for_mtls() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + + # The base transport sets the host, credentials and scopes + super().__init__( + host=host, + credentials=credentials, + credentials_file=credentials_file, + scopes=scopes, + quota_project_id=quota_project_id, + client_info=client_info, + always_use_jwt_access=always_use_jwt_access, + api_audience=api_audience, + ) + + if not self._grpc_channel: + # initialize with the provided callable or the default channel + channel_init = channel or type(self).create_channel + self._grpc_channel = channel_init( + self._host, + # use the credentials which are saved + credentials=self._credentials, + # Set ``credentials_file`` to ``None`` here as + # the credentials that we saved earlier should be used. + credentials_file=None, + scopes=self._scopes, + ssl_credentials=self._ssl_channel_credentials, + quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + self._interceptor = _LoggingClientInterceptor() + self._logged_channel = grpc.intercept_channel( + self._grpc_channel, self._interceptor + ) + + # Wrap messages. This must be done after self._logged_channel exists + self._prep_wrapped_messages(client_info) + + @classmethod + def create_channel( + cls, + host: str = "apihub.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + quota_project_id: Optional[str] = None, + **kwargs, + ) -> grpc.Channel: + """Create and return a gRPC channel object. + Args: + host (Optional[str]): The host for the channel to use. + credentials (Optional[~.Credentials]): The + authorization credentials to attach to requests. These + credentials identify this application to the service. If + none are specified, the client will attempt to ascertain + the credentials from the environment. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is mutually exclusive with credentials. + scopes (Optional[Sequence[str]]): A optional list of scopes needed for this + service. These are only used when credentials are not specified and + are passed to :func:`google.auth.default`. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + kwargs (Optional[dict]): Keyword arguments, which are passed to the + channel creation. + Returns: + grpc.Channel: A gRPC channel object. + + Raises: + google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` + and ``credentials_file`` are passed. + """ + + return grpc_helpers.create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + quota_project_id=quota_project_id, + default_scopes=cls.AUTH_SCOPES, + scopes=scopes, + default_host=cls.DEFAULT_HOST, + **kwargs, + ) + + @property + def grpc_channel(self) -> grpc.Channel: + """Return the channel designed to connect to this service.""" + return self._grpc_channel + + @property + def create_curation( + self, + ) -> Callable[[curate_service.CreateCurationRequest], curate_service.Curation]: + r"""Return a callable for the create curation method over gRPC. + + Create a curation resource in the API hub. + Once a curation resource is created, plugin instances + can start using it. + + Returns: + Callable[[~.CreateCurationRequest], + ~.Curation]: + 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 "create_curation" not in self._stubs: + self._stubs["create_curation"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubCurate/CreateCuration", + request_serializer=curate_service.CreateCurationRequest.serialize, + response_deserializer=curate_service.Curation.deserialize, + ) + return self._stubs["create_curation"] + + @property + def get_curation( + self, + ) -> Callable[[curate_service.GetCurationRequest], curate_service.Curation]: + r"""Return a callable for the get curation method over gRPC. + + Get curation resource details. + + Returns: + Callable[[~.GetCurationRequest], + ~.Curation]: + 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 "get_curation" not in self._stubs: + self._stubs["get_curation"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubCurate/GetCuration", + request_serializer=curate_service.GetCurationRequest.serialize, + response_deserializer=curate_service.Curation.deserialize, + ) + return self._stubs["get_curation"] + + @property + def list_curations( + self, + ) -> Callable[ + [curate_service.ListCurationsRequest], curate_service.ListCurationsResponse + ]: + r"""Return a callable for the list curations method over gRPC. + + List curation resources in the API hub. + + Returns: + Callable[[~.ListCurationsRequest], + ~.ListCurationsResponse]: + 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 "list_curations" not in self._stubs: + self._stubs["list_curations"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubCurate/ListCurations", + request_serializer=curate_service.ListCurationsRequest.serialize, + response_deserializer=curate_service.ListCurationsResponse.deserialize, + ) + return self._stubs["list_curations"] + + @property + def update_curation( + self, + ) -> Callable[[curate_service.UpdateCurationRequest], curate_service.Curation]: + r"""Return a callable for the update curation method over gRPC. + + Update a curation resource in the API hub. The following fields + in the [curation][google.cloud.apihub.v1.Curation] can be + updated: + + - [display_name][google.cloud.apihub.v1.Curation.display_name] + - [description][google.cloud.apihub.v1.Curation.description] + + The + [update_mask][google.cloud.apihub.v1.UpdateApiRequest.update_mask] + should be used to specify the fields being updated. + + Returns: + Callable[[~.UpdateCurationRequest], + ~.Curation]: + 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 "update_curation" not in self._stubs: + self._stubs["update_curation"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubCurate/UpdateCuration", + request_serializer=curate_service.UpdateCurationRequest.serialize, + response_deserializer=curate_service.Curation.deserialize, + ) + return self._stubs["update_curation"] + + @property + def delete_curation( + self, + ) -> Callable[[curate_service.DeleteCurationRequest], empty_pb2.Empty]: + r"""Return a callable for the delete curation method over gRPC. + + Delete a curation resource in the API hub. A curation + can only be deleted if it's not being used by any plugin + instance. + + Returns: + Callable[[~.DeleteCurationRequest], + ~.Empty]: + 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 "delete_curation" not in self._stubs: + self._stubs["delete_curation"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubCurate/DeleteCuration", + request_serializer=curate_service.DeleteCurationRequest.serialize, + response_deserializer=empty_pb2.Empty.FromString, + ) + return self._stubs["delete_curation"] + + def close(self): + self._logged_channel.close() + + @property + def delete_operation( + self, + ) -> Callable[[operations_pb2.DeleteOperationRequest], None]: + r"""Return a callable for the delete_operation method over gRPC.""" + # 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 "delete_operation" not in self._stubs: + self._stubs["delete_operation"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/DeleteOperation", + request_serializer=operations_pb2.DeleteOperationRequest.SerializeToString, + response_deserializer=None, + ) + return self._stubs["delete_operation"] + + @property + def cancel_operation( + self, + ) -> Callable[[operations_pb2.CancelOperationRequest], None]: + r"""Return a callable for the cancel_operation method over gRPC.""" + # 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 "cancel_operation" not in self._stubs: + self._stubs["cancel_operation"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/CancelOperation", + request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, + response_deserializer=None, + ) + return self._stubs["cancel_operation"] + + @property + def get_operation( + self, + ) -> Callable[[operations_pb2.GetOperationRequest], operations_pb2.Operation]: + r"""Return a callable for the get_operation method over gRPC.""" + # 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 "get_operation" not in self._stubs: + self._stubs["get_operation"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/GetOperation", + request_serializer=operations_pb2.GetOperationRequest.SerializeToString, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["get_operation"] + + @property + def list_operations( + self, + ) -> Callable[ + [operations_pb2.ListOperationsRequest], operations_pb2.ListOperationsResponse + ]: + r"""Return a callable for the list_operations method over gRPC.""" + # 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 "list_operations" not in self._stubs: + self._stubs["list_operations"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/ListOperations", + request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, + response_deserializer=operations_pb2.ListOperationsResponse.FromString, + ) + return self._stubs["list_operations"] + + @property + def list_locations( + self, + ) -> Callable[ + [locations_pb2.ListLocationsRequest], locations_pb2.ListLocationsResponse + ]: + r"""Return a callable for the list locations method over gRPC.""" + # 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 "list_locations" not in self._stubs: + self._stubs["list_locations"] = self._logged_channel.unary_unary( + "/google.cloud.location.Locations/ListLocations", + request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, + response_deserializer=locations_pb2.ListLocationsResponse.FromString, + ) + return self._stubs["list_locations"] + + @property + def get_location( + self, + ) -> Callable[[locations_pb2.GetLocationRequest], locations_pb2.Location]: + r"""Return a callable for the list locations method over gRPC.""" + # 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 "get_location" not in self._stubs: + self._stubs["get_location"] = self._logged_channel.unary_unary( + "/google.cloud.location.Locations/GetLocation", + request_serializer=locations_pb2.GetLocationRequest.SerializeToString, + response_deserializer=locations_pb2.Location.FromString, + ) + return self._stubs["get_location"] + + @property + def kind(self) -> str: + return "grpc" + + +__all__ = ("ApiHubCurateGrpcTransport",) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/transports/grpc_asyncio.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/transports/grpc_asyncio.py new file mode 100644 index 000000000000..a731982ef03d --- /dev/null +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_curate/transports/grpc_asyncio.py @@ -0,0 +1,665 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +import inspect +import json +import logging as std_logging +import pickle +from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union +import warnings + +from google.api_core import exceptions as core_exceptions +from google.api_core import gapic_v1, grpc_helpers_async +from google.api_core import retry_async as retries +from google.auth import credentials as ga_credentials # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore +from google.cloud.location import locations_pb2 # type: ignore +from google.longrunning import operations_pb2 # type: ignore +from google.protobuf import empty_pb2 # type: ignore +from google.protobuf.json_format import MessageToJson +import google.protobuf.message +import grpc # type: ignore +from grpc.experimental import aio # type: ignore +import proto # type: ignore + +from google.cloud.apihub_v1.types import curate_service + +from .base import DEFAULT_CLIENT_INFO, ApiHubCurateTransport +from .grpc import ApiHubCurateGrpcTransport + +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class _LoggingClientAIOInterceptor( + grpc.aio.UnaryUnaryClientInterceptor +): # pragma: NO COVER + async def intercept_unary_unary(self, continuation, client_call_details, request): + logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ) + if logging_enabled: # pragma: NO COVER + request_metadata = client_call_details.metadata + if isinstance(request, proto.Message): + request_payload = type(request).to_json(request) + elif isinstance(request, google.protobuf.message.Message): + request_payload = MessageToJson(request) + else: + request_payload = f"{type(request).__name__}: {pickle.dumps(request)}" + + request_metadata = { + key: value.decode("utf-8") if isinstance(value, bytes) else value + for key, value in request_metadata + } + grpc_request = { + "payload": request_payload, + "requestMethod": "grpc", + "metadata": dict(request_metadata), + } + _LOGGER.debug( + f"Sending request for {client_call_details.method}", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubCurate", + "rpcName": str(client_call_details.method), + "request": grpc_request, + "metadata": grpc_request["metadata"], + }, + ) + response = await continuation(client_call_details, request) + if logging_enabled: # pragma: NO COVER + response_metadata = await response.trailing_metadata() + # Convert gRPC metadata `` to list of tuples + metadata = ( + dict([(k, str(v)) for k, v in response_metadata]) + if response_metadata + else None + ) + result = await response + if isinstance(result, proto.Message): + response_payload = type(result).to_json(result) + elif isinstance(result, google.protobuf.message.Message): + response_payload = MessageToJson(result) + else: + response_payload = f"{type(result).__name__}: {pickle.dumps(result)}" + grpc_response = { + "payload": response_payload, + "metadata": metadata, + "status": "OK", + } + _LOGGER.debug( + f"Received response to rpc {client_call_details.method}.", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubCurate", + "rpcName": str(client_call_details.method), + "response": grpc_response, + "metadata": grpc_response["metadata"], + }, + ) + return response + + +class ApiHubCurateGrpcAsyncIOTransport(ApiHubCurateTransport): + """gRPC AsyncIO backend transport for ApiHubCurate. + + This service is used for managing curations for processing + API data consumed from collect layer. + + This class defines the same methods as the primary client, so the + primary client can load the underlying transport implementation + and call it. + + It sends protocol buffers over the wire using gRPC (which is built on + top of HTTP/2); the ``grpcio`` package must be installed. + """ + + _grpc_channel: aio.Channel + _stubs: Dict[str, Callable] = {} + + @classmethod + def create_channel( + cls, + host: str = "apihub.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + quota_project_id: Optional[str] = None, + **kwargs, + ) -> aio.Channel: + """Create and return a gRPC AsyncIO channel object. + Args: + host (Optional[str]): The host for the channel to use. + credentials (Optional[~.Credentials]): The + authorization credentials to attach to requests. These + credentials identify this application to the service. If + none are specified, the client will attempt to ascertain + the credentials from the environment. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + scopes (Optional[Sequence[str]]): A optional list of scopes needed for this + service. These are only used when credentials are not specified and + are passed to :func:`google.auth.default`. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + kwargs (Optional[dict]): Keyword arguments, which are passed to the + channel creation. + Returns: + aio.Channel: A gRPC AsyncIO channel object. + """ + + return grpc_helpers_async.create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + quota_project_id=quota_project_id, + default_scopes=cls.AUTH_SCOPES, + scopes=scopes, + default_host=cls.DEFAULT_HOST, + **kwargs, + ) + + def __init__( + self, + *, + host: str = "apihub.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + channel: Optional[Union[aio.Channel, Callable[..., aio.Channel]]] = None, + api_mtls_endpoint: Optional[str] = None, + client_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + ssl_channel_credentials: Optional[grpc.ChannelCredentials] = None, + client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + quota_project_id: Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + always_use_jwt_access: Optional[bool] = False, + api_audience: Optional[str] = None, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): + The hostname to connect to (default: 'apihub.googleapis.com'). + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + This argument is ignored if a ``channel`` instance is provided. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is ignored if a ``channel`` instance is provided. + scopes (Optional[Sequence[str]]): A optional list of scopes needed for this + service. These are only used when credentials are not specified and + are passed to :func:`google.auth.default`. + channel (Optional[Union[aio.Channel, Callable[..., aio.Channel]]]): + A ``Channel`` instance through which to make calls, or a Callable + that constructs and returns one. If set to None, ``self.create_channel`` + is used to create the channel. If a Callable is given, it will be called + with the same arguments as used in ``self.create_channel``. + 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 application default SSL credentials. + 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 the grpc channel. It is ignored if a ``channel`` instance is provided. + client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]): + A callback to provide client certificate bytes and private key bytes, + both in PEM format. It is used to configure a mutual TLS channel. It is + ignored if a ``channel`` instance or ``ssl_channel_credentials`` 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 + your own client library. + always_use_jwt_access (Optional[bool]): Whether self signed JWT should + be used for service account credentials. + + Raises: + google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport + creation failed for any reason. + google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` + and ``credentials_file`` are passed. + """ + self._grpc_channel = None + self._ssl_channel_credentials = ssl_channel_credentials + self._stubs: Dict[str, Callable] = {} + + if api_mtls_endpoint: + warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning) + if client_cert_source: + warnings.warn("client_cert_source is deprecated", DeprecationWarning) + + if isinstance(channel, aio.Channel): + # Ignore credentials if a channel was passed. + credentials = None + self._ignore_credentials = True + # If a channel was explicitly provided, set it. + self._grpc_channel = channel + self._ssl_channel_credentials = None + else: + if api_mtls_endpoint: + host = api_mtls_endpoint + + # Create SSL credentials with client_cert_source or application + # default SSL credentials. + if client_cert_source: + cert, key = client_cert_source() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + else: + self._ssl_channel_credentials = SslCredentials().ssl_credentials + + else: + if client_cert_source_for_mtls and not ssl_channel_credentials: + cert, key = client_cert_source_for_mtls() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + + # The base transport sets the host, credentials and scopes + super().__init__( + host=host, + credentials=credentials, + credentials_file=credentials_file, + scopes=scopes, + quota_project_id=quota_project_id, + client_info=client_info, + always_use_jwt_access=always_use_jwt_access, + api_audience=api_audience, + ) + + if not self._grpc_channel: + # initialize with the provided callable or the default channel + channel_init = channel or type(self).create_channel + self._grpc_channel = channel_init( + self._host, + # use the credentials which are saved + credentials=self._credentials, + # Set ``credentials_file`` to ``None`` here as + # the credentials that we saved earlier should be used. + credentials_file=None, + scopes=self._scopes, + ssl_credentials=self._ssl_channel_credentials, + quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + self._interceptor = _LoggingClientAIOInterceptor() + self._grpc_channel._unary_unary_interceptors.append(self._interceptor) + self._logged_channel = self._grpc_channel + self._wrap_with_kind = ( + "kind" in inspect.signature(gapic_v1.method_async.wrap_method).parameters + ) + # Wrap messages. This must be done after self._logged_channel exists + self._prep_wrapped_messages(client_info) + + @property + def grpc_channel(self) -> aio.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 from cache. + return self._grpc_channel + + @property + def create_curation( + self, + ) -> Callable[ + [curate_service.CreateCurationRequest], Awaitable[curate_service.Curation] + ]: + r"""Return a callable for the create curation method over gRPC. + + Create a curation resource in the API hub. + Once a curation resource is created, plugin instances + can start using it. + + Returns: + Callable[[~.CreateCurationRequest], + Awaitable[~.Curation]]: + 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 "create_curation" not in self._stubs: + self._stubs["create_curation"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubCurate/CreateCuration", + request_serializer=curate_service.CreateCurationRequest.serialize, + response_deserializer=curate_service.Curation.deserialize, + ) + return self._stubs["create_curation"] + + @property + def get_curation( + self, + ) -> Callable[ + [curate_service.GetCurationRequest], Awaitable[curate_service.Curation] + ]: + r"""Return a callable for the get curation method over gRPC. + + Get curation resource details. + + Returns: + Callable[[~.GetCurationRequest], + Awaitable[~.Curation]]: + 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 "get_curation" not in self._stubs: + self._stubs["get_curation"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubCurate/GetCuration", + request_serializer=curate_service.GetCurationRequest.serialize, + response_deserializer=curate_service.Curation.deserialize, + ) + return self._stubs["get_curation"] + + @property + def list_curations( + self, + ) -> Callable[ + [curate_service.ListCurationsRequest], + Awaitable[curate_service.ListCurationsResponse], + ]: + r"""Return a callable for the list curations method over gRPC. + + List curation resources in the API hub. + + Returns: + Callable[[~.ListCurationsRequest], + Awaitable[~.ListCurationsResponse]]: + 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 "list_curations" not in self._stubs: + self._stubs["list_curations"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubCurate/ListCurations", + request_serializer=curate_service.ListCurationsRequest.serialize, + response_deserializer=curate_service.ListCurationsResponse.deserialize, + ) + return self._stubs["list_curations"] + + @property + def update_curation( + self, + ) -> Callable[ + [curate_service.UpdateCurationRequest], Awaitable[curate_service.Curation] + ]: + r"""Return a callable for the update curation method over gRPC. + + Update a curation resource in the API hub. The following fields + in the [curation][google.cloud.apihub.v1.Curation] can be + updated: + + - [display_name][google.cloud.apihub.v1.Curation.display_name] + - [description][google.cloud.apihub.v1.Curation.description] + + The + [update_mask][google.cloud.apihub.v1.UpdateApiRequest.update_mask] + should be used to specify the fields being updated. + + Returns: + Callable[[~.UpdateCurationRequest], + Awaitable[~.Curation]]: + 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 "update_curation" not in self._stubs: + self._stubs["update_curation"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubCurate/UpdateCuration", + request_serializer=curate_service.UpdateCurationRequest.serialize, + response_deserializer=curate_service.Curation.deserialize, + ) + return self._stubs["update_curation"] + + @property + def delete_curation( + self, + ) -> Callable[[curate_service.DeleteCurationRequest], Awaitable[empty_pb2.Empty]]: + r"""Return a callable for the delete curation method over gRPC. + + Delete a curation resource in the API hub. A curation + can only be deleted if it's not being used by any plugin + instance. + + Returns: + Callable[[~.DeleteCurationRequest], + Awaitable[~.Empty]]: + 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 "delete_curation" not in self._stubs: + self._stubs["delete_curation"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubCurate/DeleteCuration", + request_serializer=curate_service.DeleteCurationRequest.serialize, + response_deserializer=empty_pb2.Empty.FromString, + ) + return self._stubs["delete_curation"] + + def _prep_wrapped_messages(self, client_info): + """Precompute the wrapped methods, overriding the base class method to use async wrappers.""" + self._wrapped_methods = { + self.create_curation: self._wrap_method( + self.create_curation, + default_timeout=None, + client_info=client_info, + ), + self.get_curation: self._wrap_method( + self.get_curation, + default_timeout=None, + client_info=client_info, + ), + self.list_curations: self._wrap_method( + self.list_curations, + default_timeout=None, + client_info=client_info, + ), + self.update_curation: self._wrap_method( + self.update_curation, + default_timeout=None, + client_info=client_info, + ), + self.delete_curation: self._wrap_method( + self.delete_curation, + default_timeout=None, + client_info=client_info, + ), + self.get_location: self._wrap_method( + self.get_location, + default_timeout=None, + client_info=client_info, + ), + self.list_locations: self._wrap_method( + self.list_locations, + default_timeout=None, + client_info=client_info, + ), + self.cancel_operation: self._wrap_method( + self.cancel_operation, + default_timeout=None, + client_info=client_info, + ), + self.delete_operation: self._wrap_method( + self.delete_operation, + default_timeout=None, + client_info=client_info, + ), + self.get_operation: self._wrap_method( + self.get_operation, + default_timeout=None, + client_info=client_info, + ), + self.list_operations: self._wrap_method( + self.list_operations, + default_timeout=None, + client_info=client_info, + ), + } + + def _wrap_method(self, func, *args, **kwargs): + if self._wrap_with_kind: # pragma: NO COVER + kwargs["kind"] = self.kind + return gapic_v1.method_async.wrap_method(func, *args, **kwargs) + + def close(self): + return self._logged_channel.close() + + @property + def kind(self) -> str: + return "grpc_asyncio" + + @property + def delete_operation( + self, + ) -> Callable[[operations_pb2.DeleteOperationRequest], None]: + r"""Return a callable for the delete_operation method over gRPC.""" + # 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 "delete_operation" not in self._stubs: + self._stubs["delete_operation"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/DeleteOperation", + request_serializer=operations_pb2.DeleteOperationRequest.SerializeToString, + response_deserializer=None, + ) + return self._stubs["delete_operation"] + + @property + def cancel_operation( + self, + ) -> Callable[[operations_pb2.CancelOperationRequest], None]: + r"""Return a callable for the cancel_operation method over gRPC.""" + # 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 "cancel_operation" not in self._stubs: + self._stubs["cancel_operation"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/CancelOperation", + request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, + response_deserializer=None, + ) + return self._stubs["cancel_operation"] + + @property + def get_operation( + self, + ) -> Callable[[operations_pb2.GetOperationRequest], operations_pb2.Operation]: + r"""Return a callable for the get_operation method over gRPC.""" + # 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 "get_operation" not in self._stubs: + self._stubs["get_operation"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/GetOperation", + request_serializer=operations_pb2.GetOperationRequest.SerializeToString, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["get_operation"] + + @property + def list_operations( + self, + ) -> Callable[ + [operations_pb2.ListOperationsRequest], operations_pb2.ListOperationsResponse + ]: + r"""Return a callable for the list_operations method over gRPC.""" + # 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 "list_operations" not in self._stubs: + self._stubs["list_operations"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/ListOperations", + request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, + response_deserializer=operations_pb2.ListOperationsResponse.FromString, + ) + return self._stubs["list_operations"] + + @property + def list_locations( + self, + ) -> Callable[ + [locations_pb2.ListLocationsRequest], locations_pb2.ListLocationsResponse + ]: + r"""Return a callable for the list locations method over gRPC.""" + # 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 "list_locations" not in self._stubs: + self._stubs["list_locations"] = self._logged_channel.unary_unary( + "/google.cloud.location.Locations/ListLocations", + request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, + response_deserializer=locations_pb2.ListLocationsResponse.FromString, + ) + return self._stubs["list_locations"] + + @property + def get_location( + self, + ) -> Callable[[locations_pb2.GetLocationRequest], locations_pb2.Location]: + r"""Return a callable for the list locations method over gRPC.""" + # 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 "get_location" not in self._stubs: + self._stubs["get_location"] = self._logged_channel.unary_unary( + "/google.cloud.location.Locations/GetLocation", + request_serializer=locations_pb2.GetLocationRequest.SerializeToString, + response_deserializer=locations_pb2.Location.FromString, + ) + return self._stubs["get_location"] + + +__all__ = ("ApiHubCurateGrpcAsyncIOTransport",) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/__init__.py index a86ece7d6693..73c4666c44fd 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/__init__.py @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from .async_client import ApiHubDependenciesAsyncClient from .client import ApiHubDependenciesClient -__all__ = ("ApiHubDependenciesClient",) +__all__ = ( + "ApiHubDependenciesClient", + "ApiHubDependenciesAsyncClient", +) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/async_client.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/async_client.py index 42f5c884a8ec..cd2ab0ee2790 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/async_client.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/async_client.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,6 +14,7 @@ # limitations under the License. # from collections import OrderedDict +import logging as std_logging import re from typing import ( Callable, @@ -34,6 +35,7 @@ from google.api_core.client_options import ClientOptions from google.auth import credentials as ga_credentials # type: ignore from google.oauth2 import service_account # type: ignore +import google.protobuf from google.cloud.apihub_v1 import gapic_version as package_version @@ -54,6 +56,15 @@ from .transports.base import DEFAULT_CLIENT_INFO, ApiHubDependenciesTransport from .transports.grpc_asyncio import ApiHubDependenciesGrpcAsyncIOTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + class ApiHubDependenciesAsyncClient: """This service provides methods for various operations related to a @@ -267,6 +278,28 @@ def __init__( client_info=client_info, ) + if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ): # pragma: NO COVER + _LOGGER.debug( + "Created client `google.cloud.apihub_v1.ApiHubDependenciesAsyncClient`.", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubDependencies", + "universeDomain": getattr( + self._client._transport._credentials, "universe_domain", "" + ), + "credentialsType": f"{type(self._client._transport._credentials).__module__}.{type(self._client._transport._credentials).__qualname__}", + "credentialsInfo": getattr( + self.transport._credentials, "get_cred_info", lambda: None + )(), + } + if hasattr(self._client._transport, "_credentials") + else { + "serviceName": "google.cloud.apihub.v1.ApiHubDependencies", + "credentialsType": None, + }, + ) + async def create_dependency( self, request: Optional[Union[apihub_service.CreateDependencyRequest, dict]] = None, @@ -276,7 +309,7 @@ async def create_dependency( dependency_id: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.Dependency: r"""Create a dependency between two entities in the API hub. @@ -337,10 +370,10 @@ async def sample_create_dependency(): which will become the final component of the dependency's resource name. This field is optional. - - If provided, the same will be used. The service will - throw an error if duplicate id is provided by the - client. - - If not provided, a system generated id will be used. + - If provided, the same will be used. The service will + throw an error if duplicate id is provided by the + client. + - If not provided, a system generated id will be used. This value should be 4-500 characters, and valid characters are ``[a-z][A-Z][0-9]-_``. @@ -351,24 +384,29 @@ async def sample_create_dependency(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Dependency: A dependency resource defined in the API hub describes a dependency directed from a consumer to a supplier entity. A dependency can be defined between two - [Operations][google.cloud.apihub.v1.Operation] or + [Operations][google.cloud.apihub.v1.ApiOperation] or between an - [Operation][google.cloud.apihub.v1.Operation] and + [Operation][google.cloud.apihub.v1.ApiOperation] and [External API][google.cloud.apihub.v1.ExternalApi]. """ # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([parent, dependency, dependency_id]) + flattened_params = [parent, dependency, dependency_id] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -422,7 +460,7 @@ async def get_dependency( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.Dependency: r"""Get details about a dependency resource in the API hub. @@ -468,24 +506,29 @@ async def sample_get_dependency(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Dependency: A dependency resource defined in the API hub describes a dependency directed from a consumer to a supplier entity. A dependency can be defined between two - [Operations][google.cloud.apihub.v1.Operation] or + [Operations][google.cloud.apihub.v1.ApiOperation] or between an - [Operation][google.cloud.apihub.v1.Operation] and + [Operation][google.cloud.apihub.v1.ApiOperation] and [External API][google.cloud.apihub.v1.ExternalApi]. """ # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -536,7 +579,7 @@ async def update_dependency( update_mask: Optional[field_mask_pb2.FieldMask] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.Dependency: r"""Update a dependency based on the [update_mask][google.cloud.apihub.v1.UpdateDependencyRequest.update_mask] @@ -545,7 +588,7 @@ async def update_dependency( The following fields in the [dependency][google.cloud.apihub.v1.Dependency] can be updated: - - [description][google.cloud.apihub.v1.Dependency.description] + - [description][google.cloud.apihub.v1.Dependency.description] .. code-block:: python @@ -602,24 +645,29 @@ async def sample_update_dependency(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Dependency: A dependency resource defined in the API hub describes a dependency directed from a consumer to a supplier entity. A dependency can be defined between two - [Operations][google.cloud.apihub.v1.Operation] or + [Operations][google.cloud.apihub.v1.ApiOperation] or between an - [Operation][google.cloud.apihub.v1.Operation] and + [Operation][google.cloud.apihub.v1.ApiOperation] and [External API][google.cloud.apihub.v1.ExternalApi]. """ # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([dependency, update_mask]) + flattened_params = [dependency, update_mask] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -673,7 +721,7 @@ async def delete_dependency( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Delete the dependency resource. @@ -716,13 +764,18 @@ async def sample_delete_dependency(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. """ # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -769,7 +822,7 @@ async def list_dependencies( parent: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> pagers.ListDependenciesAsyncPager: r"""List dependencies based on the provided filter and pagination parameters. @@ -817,8 +870,10 @@ async def sample_list_dependencies(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.services.api_hub_dependencies.pagers.ListDependenciesAsyncPager: @@ -833,7 +888,10 @@ async def sample_list_dependencies(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([parent]) + flattened_params = [parent] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -893,7 +951,7 @@ async def list_operations( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> operations_pb2.ListOperationsResponse: r"""Lists operations that match the specified filter in the request. @@ -904,8 +962,10 @@ async def list_operations( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.operations_pb2.ListOperationsResponse: Response message for ``ListOperations`` method. @@ -918,11 +978,7 @@ async def list_operations( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.list_operations, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.list_operations] # Certain fields should be provided within the metadata header; # add these here. @@ -950,7 +1006,7 @@ async def get_operation( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> operations_pb2.Operation: r"""Gets the latest state of a long-running operation. @@ -961,8 +1017,10 @@ async def get_operation( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.operations_pb2.Operation: An ``Operation`` object. @@ -975,11 +1033,7 @@ async def get_operation( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.get_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.get_operation] # Certain fields should be provided within the metadata header; # add these here. @@ -1007,7 +1061,7 @@ async def delete_operation( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Deletes a long-running operation. @@ -1023,8 +1077,10 @@ async def delete_operation( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: None """ @@ -1036,11 +1092,7 @@ async def delete_operation( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.delete_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.delete_operation] # Certain fields should be provided within the metadata header; # add these here. @@ -1065,7 +1117,7 @@ async def cancel_operation( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Starts asynchronous cancellation on a long-running operation. @@ -1080,8 +1132,10 @@ async def cancel_operation( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: None """ @@ -1093,11 +1147,7 @@ async def cancel_operation( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.cancel_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.cancel_operation] # Certain fields should be provided within the metadata header; # add these here. @@ -1122,7 +1172,7 @@ async def get_location( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> locations_pb2.Location: r"""Gets information about a location. @@ -1133,8 +1183,10 @@ async def get_location( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.location_pb2.Location: Location object. @@ -1147,11 +1199,7 @@ async def get_location( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.get_location, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.get_location] # Certain fields should be provided within the metadata header; # add these here. @@ -1179,7 +1227,7 @@ async def list_locations( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> locations_pb2.ListLocationsResponse: r"""Lists information about the supported locations for this service. @@ -1190,8 +1238,10 @@ async def list_locations( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.location_pb2.ListLocationsResponse: Response message for ``ListLocations`` method. @@ -1204,11 +1254,7 @@ async def list_locations( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.list_locations, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.list_locations] # Certain fields should be provided within the metadata header; # add these here. @@ -1241,5 +1287,8 @@ async def __aexit__(self, exc_type, exc, tb): gapic_version=package_version.__version__ ) +if hasattr(DEFAULT_CLIENT_INFO, "protobuf_runtime_version"): # pragma: NO COVER + DEFAULT_CLIENT_INFO.protobuf_runtime_version = google.protobuf.__version__ + __all__ = ("ApiHubDependenciesAsyncClient",) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/client.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/client.py index 87c44de65912..9a11c92229b1 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/client.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/client.py @@ -70,6 +70,8 @@ from google.cloud.apihub_v1.types import apihub_service, common_fields from .transports.base import DEFAULT_CLIENT_INFO, ApiHubDependenciesTransport +from .transports.grpc import ApiHubDependenciesGrpcTransport +from .transports.grpc_asyncio import ApiHubDependenciesGrpcAsyncIOTransport from .transports.rest import ApiHubDependenciesRestTransport @@ -84,6 +86,8 @@ class ApiHubDependenciesClientMeta(type): _transport_registry = ( OrderedDict() ) # type: Dict[str, Type[ApiHubDependenciesTransport]] + _transport_registry["grpc"] = ApiHubDependenciesGrpcTransport + _transport_registry["grpc_asyncio"] = ApiHubDependenciesGrpcAsyncIOTransport _transport_registry["rest"] = ApiHubDependenciesRestTransport def get_transport_class( diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/pagers.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/pagers.py index 613b0230673b..63d33f7ec4be 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/pagers.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/pagers.py @@ -115,3 +115,83 @@ def __iter__(self) -> Iterator[common_fields.Dependency]: def __repr__(self) -> str: return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListDependenciesAsyncPager: + """A pager for iterating through ``list_dependencies`` requests. + + This class thinly wraps an initial + :class:`google.cloud.apihub_v1.types.ListDependenciesResponse` object, and + provides an ``__aiter__`` method to iterate through its + ``dependencies`` field. + + If there are more pages, the ``__aiter__`` method will make additional + ``ListDependencies`` requests and continue to iterate + through the ``dependencies`` field on the + corresponding responses. + + All the usual :class:`google.cloud.apihub_v1.types.ListDependenciesResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., Awaitable[apihub_service.ListDependenciesResponse]], + request: apihub_service.ListDependenciesRequest, + response: apihub_service.ListDependenciesResponse, + *, + retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = () + ): + """Instantiates the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.apihub_v1.types.ListDependenciesRequest): + The initial request object. + response (google.cloud.apihub_v1.types.ListDependenciesResponse): + The initial response object. + retry (google.api_core.retry.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + """ + self._method = method + self._request = apihub_service.ListDependenciesRequest(request) + self._response = response + self._retry = retry + self._timeout = timeout + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + async def pages(self) -> AsyncIterator[apihub_service.ListDependenciesResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = await self._method( + self._request, + retry=self._retry, + timeout=self._timeout, + metadata=self._metadata, + ) + yield self._response + + def __aiter__(self) -> AsyncIterator[common_fields.Dependency]: + async def async_generator(): + async for page in self.pages: + for response in page.dependencies: + yield response + + return async_generator() + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/transports/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/transports/__init__.py index 8d67588f6c06..e94033b587e8 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/transports/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/transports/__init__.py @@ -17,16 +17,22 @@ from typing import Dict, Type from .base import ApiHubDependenciesTransport +from .grpc import ApiHubDependenciesGrpcTransport +from .grpc_asyncio import ApiHubDependenciesGrpcAsyncIOTransport from .rest import ApiHubDependenciesRestInterceptor, ApiHubDependenciesRestTransport # Compile a registry of transports. _transport_registry = ( OrderedDict() ) # type: Dict[str, Type[ApiHubDependenciesTransport]] +_transport_registry["grpc"] = ApiHubDependenciesGrpcTransport +_transport_registry["grpc_asyncio"] = ApiHubDependenciesGrpcAsyncIOTransport _transport_registry["rest"] = ApiHubDependenciesRestTransport __all__ = ( "ApiHubDependenciesTransport", + "ApiHubDependenciesGrpcTransport", + "ApiHubDependenciesGrpcAsyncIOTransport", "ApiHubDependenciesRestTransport", "ApiHubDependenciesRestInterceptor", ) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/transports/grpc.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/transports/grpc.py index 38c7bda2bef2..b526c79cb4e7 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/transports/grpc.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/transports/grpc.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,6 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import json +import logging as std_logging +import pickle from typing import Callable, Dict, Optional, Sequence, Tuple, Union import warnings @@ -23,12 +26,89 @@ from google.cloud.location import locations_pb2 # type: ignore from google.longrunning import operations_pb2 # type: ignore from google.protobuf import empty_pb2 # type: ignore +from google.protobuf.json_format import MessageToJson +import google.protobuf.message import grpc # type: ignore +import proto # type: ignore from google.cloud.apihub_v1.types import apihub_service, common_fields from .base import DEFAULT_CLIENT_INFO, ApiHubDependenciesTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class _LoggingClientInterceptor(grpc.UnaryUnaryClientInterceptor): # pragma: NO COVER + def intercept_unary_unary(self, continuation, client_call_details, request): + logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ) + if logging_enabled: # pragma: NO COVER + request_metadata = client_call_details.metadata + if isinstance(request, proto.Message): + request_payload = type(request).to_json(request) + elif isinstance(request, google.protobuf.message.Message): + request_payload = MessageToJson(request) + else: + request_payload = f"{type(request).__name__}: {pickle.dumps(request)}" + + request_metadata = { + key: value.decode("utf-8") if isinstance(value, bytes) else value + for key, value in request_metadata + } + grpc_request = { + "payload": request_payload, + "requestMethod": "grpc", + "metadata": dict(request_metadata), + } + _LOGGER.debug( + f"Sending request for {client_call_details.method}", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubDependencies", + "rpcName": str(client_call_details.method), + "request": grpc_request, + "metadata": grpc_request["metadata"], + }, + ) + response = continuation(client_call_details, request) + if logging_enabled: # pragma: NO COVER + response_metadata = response.trailing_metadata() + # Convert gRPC metadata `` to list of tuples + metadata = ( + dict([(k, str(v)) for k, v in response_metadata]) + if response_metadata + else None + ) + result = response.result() + if isinstance(result, proto.Message): + response_payload = type(result).to_json(result) + elif isinstance(result, google.protobuf.message.Message): + response_payload = MessageToJson(result) + else: + response_payload = f"{type(result).__name__}: {pickle.dumps(result)}" + grpc_response = { + "payload": response_payload, + "metadata": metadata, + "status": "OK", + } + _LOGGER.debug( + f"Received response for {client_call_details.method}.", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubDependencies", + "rpcName": client_call_details.method, + "response": grpc_response, + "metadata": grpc_response["metadata"], + }, + ) + return response + class ApiHubDependenciesGrpcTransport(ApiHubDependenciesTransport): """gRPC backend transport for ApiHubDependencies. @@ -183,7 +263,12 @@ def __init__( ], ) - # Wrap messages. This must be done after self._grpc_channel exists + self._interceptor = _LoggingClientInterceptor() + self._logged_channel = grpc.intercept_channel( + self._grpc_channel, self._interceptor + ) + + # Wrap messages. This must be done after self._logged_channel exists self._prep_wrapped_messages(client_info) @classmethod @@ -258,7 +343,7 @@ def create_dependency( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "create_dependency" not in self._stubs: - self._stubs["create_dependency"] = self.grpc_channel.unary_unary( + self._stubs["create_dependency"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHubDependencies/CreateDependency", request_serializer=apihub_service.CreateDependencyRequest.serialize, response_deserializer=common_fields.Dependency.deserialize, @@ -285,7 +370,7 @@ def get_dependency( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_dependency" not in self._stubs: - self._stubs["get_dependency"] = self.grpc_channel.unary_unary( + self._stubs["get_dependency"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHubDependencies/GetDependency", request_serializer=apihub_service.GetDependencyRequest.serialize, response_deserializer=common_fields.Dependency.deserialize, @@ -305,7 +390,7 @@ def update_dependency( The following fields in the [dependency][google.cloud.apihub.v1.Dependency] can be updated: - - [description][google.cloud.apihub.v1.Dependency.description] + - [description][google.cloud.apihub.v1.Dependency.description] Returns: Callable[[~.UpdateDependencyRequest], @@ -318,7 +403,7 @@ def update_dependency( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "update_dependency" not in self._stubs: - self._stubs["update_dependency"] = self.grpc_channel.unary_unary( + self._stubs["update_dependency"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHubDependencies/UpdateDependency", request_serializer=apihub_service.UpdateDependencyRequest.serialize, response_deserializer=common_fields.Dependency.deserialize, @@ -344,7 +429,7 @@ def delete_dependency( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_dependency" not in self._stubs: - self._stubs["delete_dependency"] = self.grpc_channel.unary_unary( + self._stubs["delete_dependency"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHubDependencies/DeleteDependency", request_serializer=apihub_service.DeleteDependencyRequest.serialize, response_deserializer=empty_pb2.Empty.FromString, @@ -374,7 +459,7 @@ def list_dependencies( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_dependencies" not in self._stubs: - self._stubs["list_dependencies"] = self.grpc_channel.unary_unary( + self._stubs["list_dependencies"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHubDependencies/ListDependencies", request_serializer=apihub_service.ListDependenciesRequest.serialize, response_deserializer=apihub_service.ListDependenciesResponse.deserialize, @@ -382,7 +467,7 @@ def list_dependencies( return self._stubs["list_dependencies"] def close(self): - self.grpc_channel.close() + self._logged_channel.close() @property def delete_operation( @@ -394,7 +479,7 @@ def delete_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_operation" not in self._stubs: - self._stubs["delete_operation"] = self.grpc_channel.unary_unary( + self._stubs["delete_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/DeleteOperation", request_serializer=operations_pb2.DeleteOperationRequest.SerializeToString, response_deserializer=None, @@ -411,7 +496,7 @@ def cancel_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "cancel_operation" not in self._stubs: - self._stubs["cancel_operation"] = self.grpc_channel.unary_unary( + self._stubs["cancel_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/CancelOperation", request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, response_deserializer=None, @@ -428,7 +513,7 @@ def get_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_operation" not in self._stubs: - self._stubs["get_operation"] = self.grpc_channel.unary_unary( + self._stubs["get_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/GetOperation", request_serializer=operations_pb2.GetOperationRequest.SerializeToString, response_deserializer=operations_pb2.Operation.FromString, @@ -447,7 +532,7 @@ def list_operations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_operations" not in self._stubs: - self._stubs["list_operations"] = self.grpc_channel.unary_unary( + self._stubs["list_operations"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/ListOperations", request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, response_deserializer=operations_pb2.ListOperationsResponse.FromString, @@ -466,7 +551,7 @@ def list_locations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_locations" not in self._stubs: - self._stubs["list_locations"] = self.grpc_channel.unary_unary( + self._stubs["list_locations"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/ListLocations", request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, response_deserializer=locations_pb2.ListLocationsResponse.FromString, @@ -483,7 +568,7 @@ def get_location( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_location" not in self._stubs: - self._stubs["get_location"] = self.grpc_channel.unary_unary( + self._stubs["get_location"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/GetLocation", request_serializer=locations_pb2.GetLocationRequest.SerializeToString, response_deserializer=locations_pb2.Location.FromString, diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/transports/grpc_asyncio.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/transports/grpc_asyncio.py index 07929dbda3e9..dbef2b24d821 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/transports/grpc_asyncio.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_dependencies/transports/grpc_asyncio.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import inspect +import json +import logging as std_logging +import pickle from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union import warnings @@ -24,14 +28,93 @@ from google.cloud.location import locations_pb2 # type: ignore from google.longrunning import operations_pb2 # type: ignore from google.protobuf import empty_pb2 # type: ignore +from google.protobuf.json_format import MessageToJson +import google.protobuf.message import grpc # type: ignore from grpc.experimental import aio # type: ignore +import proto # type: ignore from google.cloud.apihub_v1.types import apihub_service, common_fields from .base import DEFAULT_CLIENT_INFO, ApiHubDependenciesTransport from .grpc import ApiHubDependenciesGrpcTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class _LoggingClientAIOInterceptor( + grpc.aio.UnaryUnaryClientInterceptor +): # pragma: NO COVER + async def intercept_unary_unary(self, continuation, client_call_details, request): + logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ) + if logging_enabled: # pragma: NO COVER + request_metadata = client_call_details.metadata + if isinstance(request, proto.Message): + request_payload = type(request).to_json(request) + elif isinstance(request, google.protobuf.message.Message): + request_payload = MessageToJson(request) + else: + request_payload = f"{type(request).__name__}: {pickle.dumps(request)}" + + request_metadata = { + key: value.decode("utf-8") if isinstance(value, bytes) else value + for key, value in request_metadata + } + grpc_request = { + "payload": request_payload, + "requestMethod": "grpc", + "metadata": dict(request_metadata), + } + _LOGGER.debug( + f"Sending request for {client_call_details.method}", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubDependencies", + "rpcName": str(client_call_details.method), + "request": grpc_request, + "metadata": grpc_request["metadata"], + }, + ) + response = await continuation(client_call_details, request) + if logging_enabled: # pragma: NO COVER + response_metadata = await response.trailing_metadata() + # Convert gRPC metadata `` to list of tuples + metadata = ( + dict([(k, str(v)) for k, v in response_metadata]) + if response_metadata + else None + ) + result = await response + if isinstance(result, proto.Message): + response_payload = type(result).to_json(result) + elif isinstance(result, google.protobuf.message.Message): + response_payload = MessageToJson(result) + else: + response_payload = f"{type(result).__name__}: {pickle.dumps(result)}" + grpc_response = { + "payload": response_payload, + "metadata": metadata, + "status": "OK", + } + _LOGGER.debug( + f"Received response to rpc {client_call_details.method}.", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubDependencies", + "rpcName": str(client_call_details.method), + "response": grpc_response, + "metadata": grpc_response["metadata"], + }, + ) + return response + class ApiHubDependenciesGrpcAsyncIOTransport(ApiHubDependenciesTransport): """gRPC AsyncIO backend transport for ApiHubDependencies. @@ -229,7 +312,13 @@ def __init__( ], ) - # Wrap messages. This must be done after self._grpc_channel exists + self._interceptor = _LoggingClientAIOInterceptor() + self._grpc_channel._unary_unary_interceptors.append(self._interceptor) + self._logged_channel = self._grpc_channel + self._wrap_with_kind = ( + "kind" in inspect.signature(gapic_v1.method_async.wrap_method).parameters + ) + # Wrap messages. This must be done after self._logged_channel exists self._prep_wrapped_messages(client_info) @property @@ -264,7 +353,7 @@ def create_dependency( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "create_dependency" not in self._stubs: - self._stubs["create_dependency"] = self.grpc_channel.unary_unary( + self._stubs["create_dependency"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHubDependencies/CreateDependency", request_serializer=apihub_service.CreateDependencyRequest.serialize, response_deserializer=common_fields.Dependency.deserialize, @@ -293,7 +382,7 @@ def get_dependency( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_dependency" not in self._stubs: - self._stubs["get_dependency"] = self.grpc_channel.unary_unary( + self._stubs["get_dependency"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHubDependencies/GetDependency", request_serializer=apihub_service.GetDependencyRequest.serialize, response_deserializer=common_fields.Dependency.deserialize, @@ -315,7 +404,7 @@ def update_dependency( The following fields in the [dependency][google.cloud.apihub.v1.Dependency] can be updated: - - [description][google.cloud.apihub.v1.Dependency.description] + - [description][google.cloud.apihub.v1.Dependency.description] Returns: Callable[[~.UpdateDependencyRequest], @@ -328,7 +417,7 @@ def update_dependency( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "update_dependency" not in self._stubs: - self._stubs["update_dependency"] = self.grpc_channel.unary_unary( + self._stubs["update_dependency"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHubDependencies/UpdateDependency", request_serializer=apihub_service.UpdateDependencyRequest.serialize, response_deserializer=common_fields.Dependency.deserialize, @@ -354,7 +443,7 @@ def delete_dependency( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_dependency" not in self._stubs: - self._stubs["delete_dependency"] = self.grpc_channel.unary_unary( + self._stubs["delete_dependency"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHubDependencies/DeleteDependency", request_serializer=apihub_service.DeleteDependencyRequest.serialize, response_deserializer=empty_pb2.Empty.FromString, @@ -384,7 +473,7 @@ def list_dependencies( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_dependencies" not in self._stubs: - self._stubs["list_dependencies"] = self.grpc_channel.unary_unary( + self._stubs["list_dependencies"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHubDependencies/ListDependencies", request_serializer=apihub_service.ListDependenciesRequest.serialize, response_deserializer=apihub_service.ListDependenciesResponse.deserialize, @@ -394,12 +483,12 @@ def list_dependencies( def _prep_wrapped_messages(self, client_info): """Precompute the wrapped methods, overriding the base class method to use async wrappers.""" self._wrapped_methods = { - self.create_dependency: gapic_v1.method_async.wrap_method( + self.create_dependency: self._wrap_method( self.create_dependency, default_timeout=60.0, client_info=client_info, ), - self.get_dependency: gapic_v1.method_async.wrap_method( + self.get_dependency: self._wrap_method( self.get_dependency, default_retry=retries.AsyncRetry( initial=1.0, @@ -413,17 +502,17 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.update_dependency: gapic_v1.method_async.wrap_method( + self.update_dependency: self._wrap_method( self.update_dependency, default_timeout=60.0, client_info=client_info, ), - self.delete_dependency: gapic_v1.method_async.wrap_method( + self.delete_dependency: self._wrap_method( self.delete_dependency, default_timeout=60.0, client_info=client_info, ), - self.list_dependencies: gapic_v1.method_async.wrap_method( + self.list_dependencies: self._wrap_method( self.list_dependencies, default_retry=retries.AsyncRetry( initial=1.0, @@ -437,10 +526,49 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), + self.get_location: self._wrap_method( + self.get_location, + default_timeout=None, + client_info=client_info, + ), + self.list_locations: self._wrap_method( + self.list_locations, + default_timeout=None, + client_info=client_info, + ), + self.cancel_operation: self._wrap_method( + self.cancel_operation, + default_timeout=None, + client_info=client_info, + ), + self.delete_operation: self._wrap_method( + self.delete_operation, + default_timeout=None, + client_info=client_info, + ), + self.get_operation: self._wrap_method( + self.get_operation, + default_timeout=None, + client_info=client_info, + ), + self.list_operations: self._wrap_method( + self.list_operations, + default_timeout=None, + client_info=client_info, + ), } + def _wrap_method(self, func, *args, **kwargs): + if self._wrap_with_kind: # pragma: NO COVER + kwargs["kind"] = self.kind + return gapic_v1.method_async.wrap_method(func, *args, **kwargs) + def close(self): - return self.grpc_channel.close() + return self._logged_channel.close() + + @property + def kind(self) -> str: + return "grpc_asyncio" @property def delete_operation( @@ -452,7 +580,7 @@ def delete_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_operation" not in self._stubs: - self._stubs["delete_operation"] = self.grpc_channel.unary_unary( + self._stubs["delete_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/DeleteOperation", request_serializer=operations_pb2.DeleteOperationRequest.SerializeToString, response_deserializer=None, @@ -469,7 +597,7 @@ def cancel_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "cancel_operation" not in self._stubs: - self._stubs["cancel_operation"] = self.grpc_channel.unary_unary( + self._stubs["cancel_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/CancelOperation", request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, response_deserializer=None, @@ -486,7 +614,7 @@ def get_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_operation" not in self._stubs: - self._stubs["get_operation"] = self.grpc_channel.unary_unary( + self._stubs["get_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/GetOperation", request_serializer=operations_pb2.GetOperationRequest.SerializeToString, response_deserializer=operations_pb2.Operation.FromString, @@ -505,7 +633,7 @@ def list_operations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_operations" not in self._stubs: - self._stubs["list_operations"] = self.grpc_channel.unary_unary( + self._stubs["list_operations"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/ListOperations", request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, response_deserializer=operations_pb2.ListOperationsResponse.FromString, @@ -524,7 +652,7 @@ def list_locations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_locations" not in self._stubs: - self._stubs["list_locations"] = self.grpc_channel.unary_unary( + self._stubs["list_locations"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/ListLocations", request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, response_deserializer=locations_pb2.ListLocationsResponse.FromString, @@ -541,7 +669,7 @@ def get_location( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_location" not in self._stubs: - self._stubs["get_location"] = self.grpc_channel.unary_unary( + self._stubs["get_location"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/GetLocation", request_serializer=locations_pb2.GetLocationRequest.SerializeToString, response_deserializer=locations_pb2.Location.FromString, diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/__init__.py index d7cae177030f..332143a75361 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/__init__.py @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from .async_client import ApiHubDiscoveryAsyncClient from .client import ApiHubDiscoveryClient -__all__ = ("ApiHubDiscoveryClient",) +__all__ = ( + "ApiHubDiscoveryClient", + "ApiHubDiscoveryAsyncClient", +) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/async_client.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/async_client.py new file mode 100644 index 000000000000..cd8daddeb2e5 --- /dev/null +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/async_client.py @@ -0,0 +1,1156 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +from collections import OrderedDict +import logging as std_logging +import re +from typing import ( + Callable, + Dict, + Mapping, + MutableMapping, + MutableSequence, + Optional, + Sequence, + Tuple, + Type, + Union, +) + +from google.api_core import exceptions as core_exceptions +from google.api_core import gapic_v1 +from google.api_core import retry_async as retries +from google.api_core.client_options import ClientOptions +from google.auth import credentials as ga_credentials # type: ignore +from google.oauth2 import service_account # type: ignore +import google.protobuf + +from google.cloud.apihub_v1 import gapic_version as package_version + +try: + OptionalRetry = Union[retries.AsyncRetry, gapic_v1.method._MethodDefault, None] +except AttributeError: # pragma: NO COVER + OptionalRetry = Union[retries.AsyncRetry, object, None] # type: ignore + +from google.cloud.location import locations_pb2 # type: ignore +from google.longrunning import operations_pb2 # type: ignore +from google.protobuf import timestamp_pb2 # type: ignore + +from google.cloud.apihub_v1.services.api_hub_discovery import pagers +from google.cloud.apihub_v1.types import common_fields, discovery_service + +from .client import ApiHubDiscoveryClient +from .transports.base import DEFAULT_CLIENT_INFO, ApiHubDiscoveryTransport +from .transports.grpc_asyncio import ApiHubDiscoveryGrpcAsyncIOTransport + +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class ApiHubDiscoveryAsyncClient: + """This service exposes methods used to manage + DiscoveredApiObservations and DiscoveredApiOperations. + """ + + _client: ApiHubDiscoveryClient + + # Copy defaults from the synchronous client for use here. + # Note: DEFAULT_ENDPOINT is deprecated. Use _DEFAULT_ENDPOINT_TEMPLATE instead. + DEFAULT_ENDPOINT = ApiHubDiscoveryClient.DEFAULT_ENDPOINT + DEFAULT_MTLS_ENDPOINT = ApiHubDiscoveryClient.DEFAULT_MTLS_ENDPOINT + _DEFAULT_ENDPOINT_TEMPLATE = ApiHubDiscoveryClient._DEFAULT_ENDPOINT_TEMPLATE + _DEFAULT_UNIVERSE = ApiHubDiscoveryClient._DEFAULT_UNIVERSE + + discovered_api_observation_path = staticmethod( + ApiHubDiscoveryClient.discovered_api_observation_path + ) + parse_discovered_api_observation_path = staticmethod( + ApiHubDiscoveryClient.parse_discovered_api_observation_path + ) + discovered_api_operation_path = staticmethod( + ApiHubDiscoveryClient.discovered_api_operation_path + ) + parse_discovered_api_operation_path = staticmethod( + ApiHubDiscoveryClient.parse_discovered_api_operation_path + ) + plugin_instance_path = staticmethod(ApiHubDiscoveryClient.plugin_instance_path) + parse_plugin_instance_path = staticmethod( + ApiHubDiscoveryClient.parse_plugin_instance_path + ) + common_billing_account_path = staticmethod( + ApiHubDiscoveryClient.common_billing_account_path + ) + parse_common_billing_account_path = staticmethod( + ApiHubDiscoveryClient.parse_common_billing_account_path + ) + common_folder_path = staticmethod(ApiHubDiscoveryClient.common_folder_path) + parse_common_folder_path = staticmethod( + ApiHubDiscoveryClient.parse_common_folder_path + ) + common_organization_path = staticmethod( + ApiHubDiscoveryClient.common_organization_path + ) + parse_common_organization_path = staticmethod( + ApiHubDiscoveryClient.parse_common_organization_path + ) + common_project_path = staticmethod(ApiHubDiscoveryClient.common_project_path) + parse_common_project_path = staticmethod( + ApiHubDiscoveryClient.parse_common_project_path + ) + common_location_path = staticmethod(ApiHubDiscoveryClient.common_location_path) + parse_common_location_path = staticmethod( + ApiHubDiscoveryClient.parse_common_location_path + ) + + @classmethod + def from_service_account_info(cls, info: dict, *args, **kwargs): + """Creates an instance of this client using the provided credentials + info. + + Args: + info (dict): The service account private key info. + args: Additional arguments to pass to the constructor. + kwargs: Additional arguments to pass to the constructor. + + Returns: + ApiHubDiscoveryAsyncClient: The constructed client. + """ + return ApiHubDiscoveryClient.from_service_account_info.__func__(ApiHubDiscoveryAsyncClient, info, *args, **kwargs) # type: ignore + + @classmethod + def from_service_account_file(cls, filename: str, *args, **kwargs): + """Creates an instance of this client using the provided credentials + file. + + Args: + filename (str): The path to the service account private key json + file. + args: Additional arguments to pass to the constructor. + kwargs: Additional arguments to pass to the constructor. + + Returns: + ApiHubDiscoveryAsyncClient: The constructed client. + """ + return ApiHubDiscoveryClient.from_service_account_file.__func__(ApiHubDiscoveryAsyncClient, filename, *args, **kwargs) # type: ignore + + from_service_account_json = from_service_account_file + + @classmethod + def get_mtls_endpoint_and_cert_source( + cls, client_options: Optional[ClientOptions] = None + ): + """Return the API endpoint and client cert source for mutual TLS. + + The client cert source is determined in the following order: + (1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the + client cert source is None. + (2) if `client_options.client_cert_source` is provided, use the provided one; if the + default client cert source exists, use the default one; otherwise the client cert + source is None. + + The API endpoint is determined in the following order: + (1) if `client_options.api_endpoint` if provided, use the provided one. + (2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the + default mTLS endpoint; if the environment variable is "never", use the default API + endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise + use the default API endpoint. + + More details can be found at https://google.aip.dev/auth/4114. + + Args: + client_options (google.api_core.client_options.ClientOptions): Custom options for the + client. Only the `api_endpoint` and `client_cert_source` properties may be used + in this method. + + Returns: + Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the + client cert source to use. + + Raises: + google.auth.exceptions.MutualTLSChannelError: If any errors happen. + """ + return ApiHubDiscoveryClient.get_mtls_endpoint_and_cert_source(client_options) # type: ignore + + @property + def transport(self) -> ApiHubDiscoveryTransport: + """Returns the transport used by the client instance. + + Returns: + ApiHubDiscoveryTransport: The transport used by the client instance. + """ + return self._client.transport + + @property + def api_endpoint(self): + """Return the API endpoint used by the client instance. + + Returns: + str: The API endpoint used by the client instance. + """ + return self._client._api_endpoint + + @property + def universe_domain(self) -> str: + """Return the universe domain used by the client instance. + + Returns: + str: The universe domain used + by the client instance. + """ + return self._client._universe_domain + + get_transport_class = ApiHubDiscoveryClient.get_transport_class + + def __init__( + self, + *, + credentials: Optional[ga_credentials.Credentials] = None, + transport: Optional[ + Union[ + str, ApiHubDiscoveryTransport, Callable[..., ApiHubDiscoveryTransport] + ] + ] = "grpc_asyncio", + client_options: Optional[ClientOptions] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + ) -> None: + """Instantiates the api hub discovery async client. + + Args: + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + transport (Optional[Union[str,ApiHubDiscoveryTransport,Callable[..., ApiHubDiscoveryTransport]]]): + The transport to use, or a Callable that constructs and returns a new transport to use. + If a Callable is given, it will be called with the same set of initialization + arguments as used in the ApiHubDiscoveryTransport constructor. + If set to None, a transport is chosen automatically. + client_options (Optional[Union[google.api_core.client_options.ClientOptions, dict]]): + Custom options for the client. + + 1. The ``api_endpoint`` property can be used to override the + default endpoint provided by the client when ``transport`` is + not explicitly provided. Only if this property is not set and + ``transport`` was not explicitly provided, the endpoint is + determined by the GOOGLE_API_USE_MTLS_ENDPOINT environment + variable, which have one of the following values: + "always" (always use the default mTLS endpoint), "never" (always + use the default regular endpoint) and "auto" (auto-switch to the + default mTLS endpoint if client certificate is present; this is + the default value). + + 2. If the GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable + is "true", then the ``client_cert_source`` property can be used + to provide a client certificate for mTLS 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. + + 3. The ``universe_domain`` property can be used to override the + default "googleapis.com" universe. Note that ``api_endpoint`` + property still takes precedence; and ``universe_domain`` is + currently not supported for mTLS. + + 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: + google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport + creation failed for any reason. + """ + self._client = ApiHubDiscoveryClient( + credentials=credentials, + transport=transport, + client_options=client_options, + client_info=client_info, + ) + + if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ): # pragma: NO COVER + _LOGGER.debug( + "Created client `google.cloud.apihub_v1.ApiHubDiscoveryAsyncClient`.", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubDiscovery", + "universeDomain": getattr( + self._client._transport._credentials, "universe_domain", "" + ), + "credentialsType": f"{type(self._client._transport._credentials).__module__}.{type(self._client._transport._credentials).__qualname__}", + "credentialsInfo": getattr( + self.transport._credentials, "get_cred_info", lambda: None + )(), + } + if hasattr(self._client._transport, "_credentials") + else { + "serviceName": "google.cloud.apihub.v1.ApiHubDiscovery", + "credentialsType": None, + }, + ) + + async def list_discovered_api_observations( + self, + request: Optional[ + Union[discovery_service.ListDiscoveredApiObservationsRequest, dict] + ] = None, + *, + parent: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> pagers.ListDiscoveredApiObservationsAsyncPager: + r"""Lists all the DiscoveredAPIObservations in a given + project and location. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_list_discovered_api_observations(): + # Create a client + client = apihub_v1.ApiHubDiscoveryAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.ListDiscoveredApiObservationsRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_discovered_api_observations(request=request) + + # Handle the response + async for response in page_result: + print(response) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.ListDiscoveredApiObservationsRequest, dict]]): + The request object. Message for requesting list of + DiscoveredApiObservations + parent (:class:`str`): + Required. The parent, which owns this + collection of ApiObservations. Format: + + projects/{project}/locations/{location} + + This corresponds to the ``parent`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + + Returns: + google.cloud.apihub_v1.services.api_hub_discovery.pagers.ListDiscoveredApiObservationsAsyncPager: + Message for response to listing + DiscoveredApiObservations + Iterating over this object will yield + results and resolve additional pages + automatically. + + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [parent] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance( + request, discovery_service.ListDiscoveredApiObservationsRequest + ): + request = discovery_service.ListDiscoveredApiObservationsRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.list_discovered_api_observations + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # This method is paged; wrap the response in a pager, which provides + # an `__aiter__` convenience method. + response = pagers.ListDiscoveredApiObservationsAsyncPager( + method=rpc, + request=request, + response=response, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def get_discovered_api_observation( + self, + request: Optional[ + Union[discovery_service.GetDiscoveredApiObservationRequest, dict] + ] = None, + *, + name: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> common_fields.DiscoveredApiObservation: + r"""Gets a DiscoveredAPIObservation in a given project, + location and ApiObservation. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_get_discovered_api_observation(): + # Create a client + client = apihub_v1.ApiHubDiscoveryAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetDiscoveredApiObservationRequest( + name="name_value", + ) + + # Make the request + response = await client.get_discovered_api_observation(request=request) + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.GetDiscoveredApiObservationRequest, dict]]): + The request object. Message for requesting a + DiscoveredApiObservation + name (:class:`str`): + Required. The name of the DiscoveredApiObservation to + retrieve. Format: + projects/{project}/locations/{location}/discoveredApiObservations/{discovered_api_observation} + + This corresponds to the ``name`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + + Returns: + google.cloud.apihub_v1.types.DiscoveredApiObservation: + Respresents an API Observation + observed in one of the sources. + + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance( + request, discovery_service.GetDiscoveredApiObservationRequest + ): + request = discovery_service.GetDiscoveredApiObservationRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if name is not None: + request.name = name + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.get_discovered_api_observation + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def list_discovered_api_operations( + self, + request: Optional[ + Union[discovery_service.ListDiscoveredApiOperationsRequest, dict] + ] = None, + *, + parent: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> pagers.ListDiscoveredApiOperationsAsyncPager: + r"""Lists all the DiscoveredAPIOperations in a given + project, location and ApiObservation. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_list_discovered_api_operations(): + # Create a client + client = apihub_v1.ApiHubDiscoveryAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.ListDiscoveredApiOperationsRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_discovered_api_operations(request=request) + + # Handle the response + async for response in page_result: + print(response) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.ListDiscoveredApiOperationsRequest, dict]]): + The request object. Message for requesting list of + DiscoveredApiOperations + parent (:class:`str`): + Required. The parent, which owns this collection of + DiscoveredApiOperations. Format: + projects/{project}/locations/{location}/discoveredApiObservations/{discovered_api_observation} + + This corresponds to the ``parent`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + + Returns: + google.cloud.apihub_v1.services.api_hub_discovery.pagers.ListDiscoveredApiOperationsAsyncPager: + Message for response to listing + DiscoveredApiOperations + Iterating over this object will yield + results and resolve additional pages + automatically. + + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [parent] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance( + request, discovery_service.ListDiscoveredApiOperationsRequest + ): + request = discovery_service.ListDiscoveredApiOperationsRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.list_discovered_api_operations + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # This method is paged; wrap the response in a pager, which provides + # an `__aiter__` convenience method. + response = pagers.ListDiscoveredApiOperationsAsyncPager( + method=rpc, + request=request, + response=response, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def get_discovered_api_operation( + self, + request: Optional[ + Union[discovery_service.GetDiscoveredApiOperationRequest, dict] + ] = None, + *, + name: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> common_fields.DiscoveredApiOperation: + r"""Gets a DiscoveredAPIOperation in a given project, + location, ApiObservation and ApiOperation. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_get_discovered_api_operation(): + # Create a client + client = apihub_v1.ApiHubDiscoveryAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetDiscoveredApiOperationRequest( + name="name_value", + ) + + # Make the request + response = await client.get_discovered_api_operation(request=request) + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.GetDiscoveredApiOperationRequest, dict]]): + The request object. Message for requesting a + DiscoveredApiOperation + name (:class:`str`): + Required. The name of the DiscoveredApiOperation to + retrieve. Format: + projects/{project}/locations/{location}/discoveredApiObservations/{discovered_api_observation}/discoveredApiOperations/{discovered_api_operation} + + This corresponds to the ``name`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + + Returns: + google.cloud.apihub_v1.types.DiscoveredApiOperation: + DiscoveredApiOperation represents an + API Operation observed in one of the + sources. + + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, discovery_service.GetDiscoveredApiOperationRequest): + request = discovery_service.GetDiscoveredApiOperationRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if name is not None: + request.name = name + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.get_discovered_api_operation + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def list_operations( + self, + request: Optional[operations_pb2.ListOperationsRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> operations_pb2.ListOperationsResponse: + r"""Lists operations that match the specified filter in the request. + + Args: + request (:class:`~.operations_pb2.ListOperationsRequest`): + The request object. Request message for + `ListOperations` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + ~.operations_pb2.ListOperationsResponse: + Response message for ``ListOperations`` method. + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = operations_pb2.ListOperationsRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.list_operations] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def get_operation( + self, + request: Optional[operations_pb2.GetOperationRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> operations_pb2.Operation: + r"""Gets the latest state of a long-running operation. + + Args: + request (:class:`~.operations_pb2.GetOperationRequest`): + The request object. Request message for + `GetOperation` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + ~.operations_pb2.Operation: + An ``Operation`` object. + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = operations_pb2.GetOperationRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.get_operation] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def delete_operation( + self, + request: Optional[operations_pb2.DeleteOperationRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> None: + r"""Deletes a long-running operation. + + This method indicates that the client is no longer interested + in the operation result. It does not cancel the operation. + If the server doesn't support this method, it returns + `google.rpc.Code.UNIMPLEMENTED`. + + Args: + request (:class:`~.operations_pb2.DeleteOperationRequest`): + The request object. Request message for + `DeleteOperation` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + None + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = operations_pb2.DeleteOperationRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.delete_operation] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + async def cancel_operation( + self, + request: Optional[operations_pb2.CancelOperationRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> None: + r"""Starts asynchronous cancellation on a long-running operation. + + The server makes a best effort to cancel the operation, but success + is not guaranteed. If the server doesn't support this method, it returns + `google.rpc.Code.UNIMPLEMENTED`. + + Args: + request (:class:`~.operations_pb2.CancelOperationRequest`): + The request object. Request message for + `CancelOperation` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + None + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = operations_pb2.CancelOperationRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.cancel_operation] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + async def get_location( + self, + request: Optional[locations_pb2.GetLocationRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> locations_pb2.Location: + r"""Gets information about a location. + + Args: + request (:class:`~.location_pb2.GetLocationRequest`): + The request object. Request message for + `GetLocation` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + ~.location_pb2.Location: + Location object. + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = locations_pb2.GetLocationRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.get_location] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def list_locations( + self, + request: Optional[locations_pb2.ListLocationsRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> locations_pb2.ListLocationsResponse: + r"""Lists information about the supported locations for this service. + + Args: + request (:class:`~.location_pb2.ListLocationsRequest`): + The request object. Request message for + `ListLocations` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + ~.location_pb2.ListLocationsResponse: + Response message for ``ListLocations`` method. + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = locations_pb2.ListLocationsRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.list_locations] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def __aenter__(self) -> "ApiHubDiscoveryAsyncClient": + return self + + async def __aexit__(self, exc_type, exc, tb): + await self.transport.close() + + +DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=package_version.__version__ +) + +if hasattr(DEFAULT_CLIENT_INFO, "protobuf_runtime_version"): # pragma: NO COVER + DEFAULT_CLIENT_INFO.protobuf_runtime_version = google.protobuf.__version__ + + +__all__ = ("ApiHubDiscoveryAsyncClient",) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/client.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/client.py index 21a488fccba1..233b40b8e6f2 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/client.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/client.py @@ -69,6 +69,8 @@ from google.cloud.apihub_v1.types import common_fields, discovery_service from .transports.base import DEFAULT_CLIENT_INFO, ApiHubDiscoveryTransport +from .transports.grpc import ApiHubDiscoveryGrpcTransport +from .transports.grpc_asyncio import ApiHubDiscoveryGrpcAsyncIOTransport from .transports.rest import ApiHubDiscoveryRestTransport @@ -83,6 +85,8 @@ class ApiHubDiscoveryClientMeta(type): _transport_registry = ( OrderedDict() ) # type: Dict[str, Type[ApiHubDiscoveryTransport]] + _transport_registry["grpc"] = ApiHubDiscoveryGrpcTransport + _transport_registry["grpc_asyncio"] = ApiHubDiscoveryGrpcAsyncIOTransport _transport_registry["rest"] = ApiHubDiscoveryRestTransport def get_transport_class( diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/pagers.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/pagers.py index d795eaed3fa5..2c798ca38cfa 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/pagers.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/pagers.py @@ -119,6 +119,90 @@ def __repr__(self) -> str: return "{0}<{1!r}>".format(self.__class__.__name__, self._response) +class ListDiscoveredApiObservationsAsyncPager: + """A pager for iterating through ``list_discovered_api_observations`` requests. + + This class thinly wraps an initial + :class:`google.cloud.apihub_v1.types.ListDiscoveredApiObservationsResponse` object, and + provides an ``__aiter__`` method to iterate through its + ``discovered_api_observations`` field. + + If there are more pages, the ``__aiter__`` method will make additional + ``ListDiscoveredApiObservations`` requests and continue to iterate + through the ``discovered_api_observations`` field on the + corresponding responses. + + All the usual :class:`google.cloud.apihub_v1.types.ListDiscoveredApiObservationsResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[ + ..., Awaitable[discovery_service.ListDiscoveredApiObservationsResponse] + ], + request: discovery_service.ListDiscoveredApiObservationsRequest, + response: discovery_service.ListDiscoveredApiObservationsResponse, + *, + retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = () + ): + """Instantiates the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.apihub_v1.types.ListDiscoveredApiObservationsRequest): + The initial request object. + response (google.cloud.apihub_v1.types.ListDiscoveredApiObservationsResponse): + The initial response object. + retry (google.api_core.retry.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + """ + self._method = method + self._request = discovery_service.ListDiscoveredApiObservationsRequest(request) + self._response = response + self._retry = retry + self._timeout = timeout + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + async def pages( + self, + ) -> AsyncIterator[discovery_service.ListDiscoveredApiObservationsResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = await self._method( + self._request, + retry=self._retry, + timeout=self._timeout, + metadata=self._metadata, + ) + yield self._response + + def __aiter__(self) -> AsyncIterator[common_fields.DiscoveredApiObservation]: + async def async_generator(): + async for page in self.pages: + for response in page.discovered_api_observations: + yield response + + return async_generator() + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + class ListDiscoveredApiOperationsPager: """A pager for iterating through ``list_discovered_api_operations`` requests. @@ -193,3 +277,87 @@ def __iter__(self) -> Iterator[common_fields.DiscoveredApiOperation]: def __repr__(self) -> str: return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListDiscoveredApiOperationsAsyncPager: + """A pager for iterating through ``list_discovered_api_operations`` requests. + + This class thinly wraps an initial + :class:`google.cloud.apihub_v1.types.ListDiscoveredApiOperationsResponse` object, and + provides an ``__aiter__`` method to iterate through its + ``discovered_api_operations`` field. + + If there are more pages, the ``__aiter__`` method will make additional + ``ListDiscoveredApiOperations`` requests and continue to iterate + through the ``discovered_api_operations`` field on the + corresponding responses. + + All the usual :class:`google.cloud.apihub_v1.types.ListDiscoveredApiOperationsResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[ + ..., Awaitable[discovery_service.ListDiscoveredApiOperationsResponse] + ], + request: discovery_service.ListDiscoveredApiOperationsRequest, + response: discovery_service.ListDiscoveredApiOperationsResponse, + *, + retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = () + ): + """Instantiates the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.apihub_v1.types.ListDiscoveredApiOperationsRequest): + The initial request object. + response (google.cloud.apihub_v1.types.ListDiscoveredApiOperationsResponse): + The initial response object. + retry (google.api_core.retry.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + """ + self._method = method + self._request = discovery_service.ListDiscoveredApiOperationsRequest(request) + self._response = response + self._retry = retry + self._timeout = timeout + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + async def pages( + self, + ) -> AsyncIterator[discovery_service.ListDiscoveredApiOperationsResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = await self._method( + self._request, + retry=self._retry, + timeout=self._timeout, + metadata=self._metadata, + ) + yield self._response + + def __aiter__(self) -> AsyncIterator[common_fields.DiscoveredApiOperation]: + async def async_generator(): + async for page in self.pages: + for response in page.discovered_api_operations: + yield response + + return async_generator() + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/transports/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/transports/__init__.py index c3a66da818d3..90cc4e78b445 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/transports/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/transports/__init__.py @@ -17,14 +17,20 @@ from typing import Dict, Type from .base import ApiHubDiscoveryTransport +from .grpc import ApiHubDiscoveryGrpcTransport +from .grpc_asyncio import ApiHubDiscoveryGrpcAsyncIOTransport from .rest import ApiHubDiscoveryRestInterceptor, ApiHubDiscoveryRestTransport # Compile a registry of transports. _transport_registry = OrderedDict() # type: Dict[str, Type[ApiHubDiscoveryTransport]] +_transport_registry["grpc"] = ApiHubDiscoveryGrpcTransport +_transport_registry["grpc_asyncio"] = ApiHubDiscoveryGrpcAsyncIOTransport _transport_registry["rest"] = ApiHubDiscoveryRestTransport __all__ = ( "ApiHubDiscoveryTransport", + "ApiHubDiscoveryGrpcTransport", + "ApiHubDiscoveryGrpcAsyncIOTransport", "ApiHubDiscoveryRestTransport", "ApiHubDiscoveryRestInterceptor", ) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/transports/grpc.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/transports/grpc.py new file mode 100644 index 000000000000..0ce6a1979f67 --- /dev/null +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/transports/grpc.py @@ -0,0 +1,568 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +import json +import logging as std_logging +import pickle +from typing import Callable, Dict, Optional, Sequence, Tuple, Union +import warnings + +from google.api_core import gapic_v1, grpc_helpers +import google.auth # type: ignore +from google.auth import credentials as ga_credentials # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore +from google.cloud.location import locations_pb2 # type: ignore +from google.longrunning import operations_pb2 # type: ignore +from google.protobuf.json_format import MessageToJson +import google.protobuf.message +import grpc # type: ignore +import proto # type: ignore + +from google.cloud.apihub_v1.types import common_fields, discovery_service + +from .base import DEFAULT_CLIENT_INFO, ApiHubDiscoveryTransport + +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class _LoggingClientInterceptor(grpc.UnaryUnaryClientInterceptor): # pragma: NO COVER + def intercept_unary_unary(self, continuation, client_call_details, request): + logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ) + if logging_enabled: # pragma: NO COVER + request_metadata = client_call_details.metadata + if isinstance(request, proto.Message): + request_payload = type(request).to_json(request) + elif isinstance(request, google.protobuf.message.Message): + request_payload = MessageToJson(request) + else: + request_payload = f"{type(request).__name__}: {pickle.dumps(request)}" + + request_metadata = { + key: value.decode("utf-8") if isinstance(value, bytes) else value + for key, value in request_metadata + } + grpc_request = { + "payload": request_payload, + "requestMethod": "grpc", + "metadata": dict(request_metadata), + } + _LOGGER.debug( + f"Sending request for {client_call_details.method}", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubDiscovery", + "rpcName": str(client_call_details.method), + "request": grpc_request, + "metadata": grpc_request["metadata"], + }, + ) + response = continuation(client_call_details, request) + if logging_enabled: # pragma: NO COVER + response_metadata = response.trailing_metadata() + # Convert gRPC metadata `` to list of tuples + metadata = ( + dict([(k, str(v)) for k, v in response_metadata]) + if response_metadata + else None + ) + result = response.result() + if isinstance(result, proto.Message): + response_payload = type(result).to_json(result) + elif isinstance(result, google.protobuf.message.Message): + response_payload = MessageToJson(result) + else: + response_payload = f"{type(result).__name__}: {pickle.dumps(result)}" + grpc_response = { + "payload": response_payload, + "metadata": metadata, + "status": "OK", + } + _LOGGER.debug( + f"Received response for {client_call_details.method}.", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubDiscovery", + "rpcName": client_call_details.method, + "response": grpc_response, + "metadata": grpc_response["metadata"], + }, + ) + return response + + +class ApiHubDiscoveryGrpcTransport(ApiHubDiscoveryTransport): + """gRPC backend transport for ApiHubDiscovery. + + This service exposes methods used to manage + DiscoveredApiObservations and DiscoveredApiOperations. + + This class defines the same methods as the primary client, so the + primary client can load the underlying transport implementation + and call it. + + It sends protocol buffers over the wire using gRPC (which is built on + top of HTTP/2); the ``grpcio`` package must be installed. + """ + + _stubs: Dict[str, Callable] + + def __init__( + self, + *, + host: str = "apihub.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + channel: Optional[Union[grpc.Channel, Callable[..., grpc.Channel]]] = None, + api_mtls_endpoint: Optional[str] = None, + client_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + ssl_channel_credentials: Optional[grpc.ChannelCredentials] = None, + client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + quota_project_id: Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + always_use_jwt_access: Optional[bool] = False, + api_audience: Optional[str] = None, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): + The hostname to connect to (default: 'apihub.googleapis.com'). + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + This argument is ignored if a ``channel`` instance is provided. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is ignored if a ``channel`` instance is provided. + scopes (Optional(Sequence[str])): A list of scopes. This argument is + ignored if a ``channel`` instance is provided. + channel (Optional[Union[grpc.Channel, Callable[..., grpc.Channel]]]): + A ``Channel`` instance through which to make calls, or a Callable + that constructs and returns one. If set to None, ``self.create_channel`` + is used to create the channel. If a Callable is given, it will be called + with the same arguments as used in ``self.create_channel``. + 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 application default SSL credentials. + 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 the grpc channel. It is ignored if a ``channel`` instance is provided. + client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]): + A callback to provide client certificate bytes and private key bytes, + both in PEM format. It is used to configure a mutual TLS channel. It is + ignored if a ``channel`` instance or ``ssl_channel_credentials`` 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 + your own client library. + always_use_jwt_access (Optional[bool]): Whether self signed JWT should + be used for service account credentials. + + Raises: + google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport + creation failed for any reason. + google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` + and ``credentials_file`` are passed. + """ + self._grpc_channel = None + self._ssl_channel_credentials = ssl_channel_credentials + self._stubs: Dict[str, Callable] = {} + + if api_mtls_endpoint: + warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning) + if client_cert_source: + warnings.warn("client_cert_source is deprecated", DeprecationWarning) + + if isinstance(channel, grpc.Channel): + # Ignore credentials if a channel was passed. + credentials = None + self._ignore_credentials = True + # If a channel was explicitly provided, set it. + self._grpc_channel = channel + self._ssl_channel_credentials = None + + else: + if api_mtls_endpoint: + host = api_mtls_endpoint + + # Create SSL credentials with client_cert_source or application + # default SSL credentials. + if client_cert_source: + cert, key = client_cert_source() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + else: + self._ssl_channel_credentials = SslCredentials().ssl_credentials + + else: + if client_cert_source_for_mtls and not ssl_channel_credentials: + cert, key = client_cert_source_for_mtls() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + + # The base transport sets the host, credentials and scopes + super().__init__( + host=host, + credentials=credentials, + credentials_file=credentials_file, + scopes=scopes, + quota_project_id=quota_project_id, + client_info=client_info, + always_use_jwt_access=always_use_jwt_access, + api_audience=api_audience, + ) + + if not self._grpc_channel: + # initialize with the provided callable or the default channel + channel_init = channel or type(self).create_channel + self._grpc_channel = channel_init( + self._host, + # use the credentials which are saved + credentials=self._credentials, + # Set ``credentials_file`` to ``None`` here as + # the credentials that we saved earlier should be used. + credentials_file=None, + scopes=self._scopes, + ssl_credentials=self._ssl_channel_credentials, + quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + self._interceptor = _LoggingClientInterceptor() + self._logged_channel = grpc.intercept_channel( + self._grpc_channel, self._interceptor + ) + + # Wrap messages. This must be done after self._logged_channel exists + self._prep_wrapped_messages(client_info) + + @classmethod + def create_channel( + cls, + host: str = "apihub.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + quota_project_id: Optional[str] = None, + **kwargs, + ) -> grpc.Channel: + """Create and return a gRPC channel object. + Args: + host (Optional[str]): The host for the channel to use. + credentials (Optional[~.Credentials]): The + authorization credentials to attach to requests. These + credentials identify this application to the service. If + none are specified, the client will attempt to ascertain + the credentials from the environment. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is mutually exclusive with credentials. + scopes (Optional[Sequence[str]]): A optional list of scopes needed for this + service. These are only used when credentials are not specified and + are passed to :func:`google.auth.default`. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + kwargs (Optional[dict]): Keyword arguments, which are passed to the + channel creation. + Returns: + grpc.Channel: A gRPC channel object. + + Raises: + google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` + and ``credentials_file`` are passed. + """ + + return grpc_helpers.create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + quota_project_id=quota_project_id, + default_scopes=cls.AUTH_SCOPES, + scopes=scopes, + default_host=cls.DEFAULT_HOST, + **kwargs, + ) + + @property + def grpc_channel(self) -> grpc.Channel: + """Return the channel designed to connect to this service.""" + return self._grpc_channel + + @property + def list_discovered_api_observations( + self, + ) -> Callable[ + [discovery_service.ListDiscoveredApiObservationsRequest], + discovery_service.ListDiscoveredApiObservationsResponse, + ]: + r"""Return a callable for the list discovered api + observations method over gRPC. + + Lists all the DiscoveredAPIObservations in a given + project and location. + + Returns: + Callable[[~.ListDiscoveredApiObservationsRequest], + ~.ListDiscoveredApiObservationsResponse]: + 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 "list_discovered_api_observations" not in self._stubs: + self._stubs[ + "list_discovered_api_observations" + ] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubDiscovery/ListDiscoveredApiObservations", + request_serializer=discovery_service.ListDiscoveredApiObservationsRequest.serialize, + response_deserializer=discovery_service.ListDiscoveredApiObservationsResponse.deserialize, + ) + return self._stubs["list_discovered_api_observations"] + + @property + def get_discovered_api_observation( + self, + ) -> Callable[ + [discovery_service.GetDiscoveredApiObservationRequest], + common_fields.DiscoveredApiObservation, + ]: + r"""Return a callable for the get discovered api observation method over gRPC. + + Gets a DiscoveredAPIObservation in a given project, + location and ApiObservation. + + Returns: + Callable[[~.GetDiscoveredApiObservationRequest], + ~.DiscoveredApiObservation]: + 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 "get_discovered_api_observation" not in self._stubs: + self._stubs[ + "get_discovered_api_observation" + ] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubDiscovery/GetDiscoveredApiObservation", + request_serializer=discovery_service.GetDiscoveredApiObservationRequest.serialize, + response_deserializer=common_fields.DiscoveredApiObservation.deserialize, + ) + return self._stubs["get_discovered_api_observation"] + + @property + def list_discovered_api_operations( + self, + ) -> Callable[ + [discovery_service.ListDiscoveredApiOperationsRequest], + discovery_service.ListDiscoveredApiOperationsResponse, + ]: + r"""Return a callable for the list discovered api operations method over gRPC. + + Lists all the DiscoveredAPIOperations in a given + project, location and ApiObservation. + + Returns: + Callable[[~.ListDiscoveredApiOperationsRequest], + ~.ListDiscoveredApiOperationsResponse]: + 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 "list_discovered_api_operations" not in self._stubs: + self._stubs[ + "list_discovered_api_operations" + ] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubDiscovery/ListDiscoveredApiOperations", + request_serializer=discovery_service.ListDiscoveredApiOperationsRequest.serialize, + response_deserializer=discovery_service.ListDiscoveredApiOperationsResponse.deserialize, + ) + return self._stubs["list_discovered_api_operations"] + + @property + def get_discovered_api_operation( + self, + ) -> Callable[ + [discovery_service.GetDiscoveredApiOperationRequest], + common_fields.DiscoveredApiOperation, + ]: + r"""Return a callable for the get discovered api operation method over gRPC. + + Gets a DiscoveredAPIOperation in a given project, + location, ApiObservation and ApiOperation. + + Returns: + Callable[[~.GetDiscoveredApiOperationRequest], + ~.DiscoveredApiOperation]: + 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 "get_discovered_api_operation" not in self._stubs: + self._stubs[ + "get_discovered_api_operation" + ] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubDiscovery/GetDiscoveredApiOperation", + request_serializer=discovery_service.GetDiscoveredApiOperationRequest.serialize, + response_deserializer=common_fields.DiscoveredApiOperation.deserialize, + ) + return self._stubs["get_discovered_api_operation"] + + def close(self): + self._logged_channel.close() + + @property + def delete_operation( + self, + ) -> Callable[[operations_pb2.DeleteOperationRequest], None]: + r"""Return a callable for the delete_operation method over gRPC.""" + # 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 "delete_operation" not in self._stubs: + self._stubs["delete_operation"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/DeleteOperation", + request_serializer=operations_pb2.DeleteOperationRequest.SerializeToString, + response_deserializer=None, + ) + return self._stubs["delete_operation"] + + @property + def cancel_operation( + self, + ) -> Callable[[operations_pb2.CancelOperationRequest], None]: + r"""Return a callable for the cancel_operation method over gRPC.""" + # 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 "cancel_operation" not in self._stubs: + self._stubs["cancel_operation"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/CancelOperation", + request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, + response_deserializer=None, + ) + return self._stubs["cancel_operation"] + + @property + def get_operation( + self, + ) -> Callable[[operations_pb2.GetOperationRequest], operations_pb2.Operation]: + r"""Return a callable for the get_operation method over gRPC.""" + # 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 "get_operation" not in self._stubs: + self._stubs["get_operation"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/GetOperation", + request_serializer=operations_pb2.GetOperationRequest.SerializeToString, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["get_operation"] + + @property + def list_operations( + self, + ) -> Callable[ + [operations_pb2.ListOperationsRequest], operations_pb2.ListOperationsResponse + ]: + r"""Return a callable for the list_operations method over gRPC.""" + # 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 "list_operations" not in self._stubs: + self._stubs["list_operations"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/ListOperations", + request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, + response_deserializer=operations_pb2.ListOperationsResponse.FromString, + ) + return self._stubs["list_operations"] + + @property + def list_locations( + self, + ) -> Callable[ + [locations_pb2.ListLocationsRequest], locations_pb2.ListLocationsResponse + ]: + r"""Return a callable for the list locations method over gRPC.""" + # 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 "list_locations" not in self._stubs: + self._stubs["list_locations"] = self._logged_channel.unary_unary( + "/google.cloud.location.Locations/ListLocations", + request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, + response_deserializer=locations_pb2.ListLocationsResponse.FromString, + ) + return self._stubs["list_locations"] + + @property + def get_location( + self, + ) -> Callable[[locations_pb2.GetLocationRequest], locations_pb2.Location]: + r"""Return a callable for the list locations method over gRPC.""" + # 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 "get_location" not in self._stubs: + self._stubs["get_location"] = self._logged_channel.unary_unary( + "/google.cloud.location.Locations/GetLocation", + request_serializer=locations_pb2.GetLocationRequest.SerializeToString, + response_deserializer=locations_pb2.Location.FromString, + ) + return self._stubs["get_location"] + + @property + def kind(self) -> str: + return "grpc" + + +__all__ = ("ApiHubDiscoveryGrpcTransport",) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/transports/grpc_asyncio.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/transports/grpc_asyncio.py new file mode 100644 index 000000000000..7d11e2b4fe6f --- /dev/null +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_discovery/transports/grpc_asyncio.py @@ -0,0 +1,636 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +import inspect +import json +import logging as std_logging +import pickle +from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union +import warnings + +from google.api_core import exceptions as core_exceptions +from google.api_core import gapic_v1, grpc_helpers_async +from google.api_core import retry_async as retries +from google.auth import credentials as ga_credentials # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore +from google.cloud.location import locations_pb2 # type: ignore +from google.longrunning import operations_pb2 # type: ignore +from google.protobuf.json_format import MessageToJson +import google.protobuf.message +import grpc # type: ignore +from grpc.experimental import aio # type: ignore +import proto # type: ignore + +from google.cloud.apihub_v1.types import common_fields, discovery_service + +from .base import DEFAULT_CLIENT_INFO, ApiHubDiscoveryTransport +from .grpc import ApiHubDiscoveryGrpcTransport + +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class _LoggingClientAIOInterceptor( + grpc.aio.UnaryUnaryClientInterceptor +): # pragma: NO COVER + async def intercept_unary_unary(self, continuation, client_call_details, request): + logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ) + if logging_enabled: # pragma: NO COVER + request_metadata = client_call_details.metadata + if isinstance(request, proto.Message): + request_payload = type(request).to_json(request) + elif isinstance(request, google.protobuf.message.Message): + request_payload = MessageToJson(request) + else: + request_payload = f"{type(request).__name__}: {pickle.dumps(request)}" + + request_metadata = { + key: value.decode("utf-8") if isinstance(value, bytes) else value + for key, value in request_metadata + } + grpc_request = { + "payload": request_payload, + "requestMethod": "grpc", + "metadata": dict(request_metadata), + } + _LOGGER.debug( + f"Sending request for {client_call_details.method}", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubDiscovery", + "rpcName": str(client_call_details.method), + "request": grpc_request, + "metadata": grpc_request["metadata"], + }, + ) + response = await continuation(client_call_details, request) + if logging_enabled: # pragma: NO COVER + response_metadata = await response.trailing_metadata() + # Convert gRPC metadata `` to list of tuples + metadata = ( + dict([(k, str(v)) for k, v in response_metadata]) + if response_metadata + else None + ) + result = await response + if isinstance(result, proto.Message): + response_payload = type(result).to_json(result) + elif isinstance(result, google.protobuf.message.Message): + response_payload = MessageToJson(result) + else: + response_payload = f"{type(result).__name__}: {pickle.dumps(result)}" + grpc_response = { + "payload": response_payload, + "metadata": metadata, + "status": "OK", + } + _LOGGER.debug( + f"Received response to rpc {client_call_details.method}.", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubDiscovery", + "rpcName": str(client_call_details.method), + "response": grpc_response, + "metadata": grpc_response["metadata"], + }, + ) + return response + + +class ApiHubDiscoveryGrpcAsyncIOTransport(ApiHubDiscoveryTransport): + """gRPC AsyncIO backend transport for ApiHubDiscovery. + + This service exposes methods used to manage + DiscoveredApiObservations and DiscoveredApiOperations. + + This class defines the same methods as the primary client, so the + primary client can load the underlying transport implementation + and call it. + + It sends protocol buffers over the wire using gRPC (which is built on + top of HTTP/2); the ``grpcio`` package must be installed. + """ + + _grpc_channel: aio.Channel + _stubs: Dict[str, Callable] = {} + + @classmethod + def create_channel( + cls, + host: str = "apihub.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + quota_project_id: Optional[str] = None, + **kwargs, + ) -> aio.Channel: + """Create and return a gRPC AsyncIO channel object. + Args: + host (Optional[str]): The host for the channel to use. + credentials (Optional[~.Credentials]): The + authorization credentials to attach to requests. These + credentials identify this application to the service. If + none are specified, the client will attempt to ascertain + the credentials from the environment. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + scopes (Optional[Sequence[str]]): A optional list of scopes needed for this + service. These are only used when credentials are not specified and + are passed to :func:`google.auth.default`. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + kwargs (Optional[dict]): Keyword arguments, which are passed to the + channel creation. + Returns: + aio.Channel: A gRPC AsyncIO channel object. + """ + + return grpc_helpers_async.create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + quota_project_id=quota_project_id, + default_scopes=cls.AUTH_SCOPES, + scopes=scopes, + default_host=cls.DEFAULT_HOST, + **kwargs, + ) + + def __init__( + self, + *, + host: str = "apihub.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + channel: Optional[Union[aio.Channel, Callable[..., aio.Channel]]] = None, + api_mtls_endpoint: Optional[str] = None, + client_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + ssl_channel_credentials: Optional[grpc.ChannelCredentials] = None, + client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + quota_project_id: Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + always_use_jwt_access: Optional[bool] = False, + api_audience: Optional[str] = None, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): + The hostname to connect to (default: 'apihub.googleapis.com'). + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + This argument is ignored if a ``channel`` instance is provided. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is ignored if a ``channel`` instance is provided. + scopes (Optional[Sequence[str]]): A optional list of scopes needed for this + service. These are only used when credentials are not specified and + are passed to :func:`google.auth.default`. + channel (Optional[Union[aio.Channel, Callable[..., aio.Channel]]]): + A ``Channel`` instance through which to make calls, or a Callable + that constructs and returns one. If set to None, ``self.create_channel`` + is used to create the channel. If a Callable is given, it will be called + with the same arguments as used in ``self.create_channel``. + 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 application default SSL credentials. + 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 the grpc channel. It is ignored if a ``channel`` instance is provided. + client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]): + A callback to provide client certificate bytes and private key bytes, + both in PEM format. It is used to configure a mutual TLS channel. It is + ignored if a ``channel`` instance or ``ssl_channel_credentials`` 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 + your own client library. + always_use_jwt_access (Optional[bool]): Whether self signed JWT should + be used for service account credentials. + + Raises: + google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport + creation failed for any reason. + google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` + and ``credentials_file`` are passed. + """ + self._grpc_channel = None + self._ssl_channel_credentials = ssl_channel_credentials + self._stubs: Dict[str, Callable] = {} + + if api_mtls_endpoint: + warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning) + if client_cert_source: + warnings.warn("client_cert_source is deprecated", DeprecationWarning) + + if isinstance(channel, aio.Channel): + # Ignore credentials if a channel was passed. + credentials = None + self._ignore_credentials = True + # If a channel was explicitly provided, set it. + self._grpc_channel = channel + self._ssl_channel_credentials = None + else: + if api_mtls_endpoint: + host = api_mtls_endpoint + + # Create SSL credentials with client_cert_source or application + # default SSL credentials. + if client_cert_source: + cert, key = client_cert_source() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + else: + self._ssl_channel_credentials = SslCredentials().ssl_credentials + + else: + if client_cert_source_for_mtls and not ssl_channel_credentials: + cert, key = client_cert_source_for_mtls() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + + # The base transport sets the host, credentials and scopes + super().__init__( + host=host, + credentials=credentials, + credentials_file=credentials_file, + scopes=scopes, + quota_project_id=quota_project_id, + client_info=client_info, + always_use_jwt_access=always_use_jwt_access, + api_audience=api_audience, + ) + + if not self._grpc_channel: + # initialize with the provided callable or the default channel + channel_init = channel or type(self).create_channel + self._grpc_channel = channel_init( + self._host, + # use the credentials which are saved + credentials=self._credentials, + # Set ``credentials_file`` to ``None`` here as + # the credentials that we saved earlier should be used. + credentials_file=None, + scopes=self._scopes, + ssl_credentials=self._ssl_channel_credentials, + quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + self._interceptor = _LoggingClientAIOInterceptor() + self._grpc_channel._unary_unary_interceptors.append(self._interceptor) + self._logged_channel = self._grpc_channel + self._wrap_with_kind = ( + "kind" in inspect.signature(gapic_v1.method_async.wrap_method).parameters + ) + # Wrap messages. This must be done after self._logged_channel exists + self._prep_wrapped_messages(client_info) + + @property + def grpc_channel(self) -> aio.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 from cache. + return self._grpc_channel + + @property + def list_discovered_api_observations( + self, + ) -> Callable[ + [discovery_service.ListDiscoveredApiObservationsRequest], + Awaitable[discovery_service.ListDiscoveredApiObservationsResponse], + ]: + r"""Return a callable for the list discovered api + observations method over gRPC. + + Lists all the DiscoveredAPIObservations in a given + project and location. + + Returns: + Callable[[~.ListDiscoveredApiObservationsRequest], + Awaitable[~.ListDiscoveredApiObservationsResponse]]: + 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 "list_discovered_api_observations" not in self._stubs: + self._stubs[ + "list_discovered_api_observations" + ] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubDiscovery/ListDiscoveredApiObservations", + request_serializer=discovery_service.ListDiscoveredApiObservationsRequest.serialize, + response_deserializer=discovery_service.ListDiscoveredApiObservationsResponse.deserialize, + ) + return self._stubs["list_discovered_api_observations"] + + @property + def get_discovered_api_observation( + self, + ) -> Callable[ + [discovery_service.GetDiscoveredApiObservationRequest], + Awaitable[common_fields.DiscoveredApiObservation], + ]: + r"""Return a callable for the get discovered api observation method over gRPC. + + Gets a DiscoveredAPIObservation in a given project, + location and ApiObservation. + + Returns: + Callable[[~.GetDiscoveredApiObservationRequest], + Awaitable[~.DiscoveredApiObservation]]: + 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 "get_discovered_api_observation" not in self._stubs: + self._stubs[ + "get_discovered_api_observation" + ] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubDiscovery/GetDiscoveredApiObservation", + request_serializer=discovery_service.GetDiscoveredApiObservationRequest.serialize, + response_deserializer=common_fields.DiscoveredApiObservation.deserialize, + ) + return self._stubs["get_discovered_api_observation"] + + @property + def list_discovered_api_operations( + self, + ) -> Callable[ + [discovery_service.ListDiscoveredApiOperationsRequest], + Awaitable[discovery_service.ListDiscoveredApiOperationsResponse], + ]: + r"""Return a callable for the list discovered api operations method over gRPC. + + Lists all the DiscoveredAPIOperations in a given + project, location and ApiObservation. + + Returns: + Callable[[~.ListDiscoveredApiOperationsRequest], + Awaitable[~.ListDiscoveredApiOperationsResponse]]: + 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 "list_discovered_api_operations" not in self._stubs: + self._stubs[ + "list_discovered_api_operations" + ] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubDiscovery/ListDiscoveredApiOperations", + request_serializer=discovery_service.ListDiscoveredApiOperationsRequest.serialize, + response_deserializer=discovery_service.ListDiscoveredApiOperationsResponse.deserialize, + ) + return self._stubs["list_discovered_api_operations"] + + @property + def get_discovered_api_operation( + self, + ) -> Callable[ + [discovery_service.GetDiscoveredApiOperationRequest], + Awaitable[common_fields.DiscoveredApiOperation], + ]: + r"""Return a callable for the get discovered api operation method over gRPC. + + Gets a DiscoveredAPIOperation in a given project, + location, ApiObservation and ApiOperation. + + Returns: + Callable[[~.GetDiscoveredApiOperationRequest], + Awaitable[~.DiscoveredApiOperation]]: + 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 "get_discovered_api_operation" not in self._stubs: + self._stubs[ + "get_discovered_api_operation" + ] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubDiscovery/GetDiscoveredApiOperation", + request_serializer=discovery_service.GetDiscoveredApiOperationRequest.serialize, + response_deserializer=common_fields.DiscoveredApiOperation.deserialize, + ) + return self._stubs["get_discovered_api_operation"] + + def _prep_wrapped_messages(self, client_info): + """Precompute the wrapped methods, overriding the base class method to use async wrappers.""" + self._wrapped_methods = { + self.list_discovered_api_observations: self._wrap_method( + self.list_discovered_api_observations, + default_timeout=None, + client_info=client_info, + ), + self.get_discovered_api_observation: self._wrap_method( + self.get_discovered_api_observation, + default_timeout=None, + client_info=client_info, + ), + self.list_discovered_api_operations: self._wrap_method( + self.list_discovered_api_operations, + default_timeout=None, + client_info=client_info, + ), + self.get_discovered_api_operation: self._wrap_method( + self.get_discovered_api_operation, + default_timeout=None, + client_info=client_info, + ), + self.get_location: self._wrap_method( + self.get_location, + default_timeout=None, + client_info=client_info, + ), + self.list_locations: self._wrap_method( + self.list_locations, + default_timeout=None, + client_info=client_info, + ), + self.cancel_operation: self._wrap_method( + self.cancel_operation, + default_timeout=None, + client_info=client_info, + ), + self.delete_operation: self._wrap_method( + self.delete_operation, + default_timeout=None, + client_info=client_info, + ), + self.get_operation: self._wrap_method( + self.get_operation, + default_timeout=None, + client_info=client_info, + ), + self.list_operations: self._wrap_method( + self.list_operations, + default_timeout=None, + client_info=client_info, + ), + } + + def _wrap_method(self, func, *args, **kwargs): + if self._wrap_with_kind: # pragma: NO COVER + kwargs["kind"] = self.kind + return gapic_v1.method_async.wrap_method(func, *args, **kwargs) + + def close(self): + return self._logged_channel.close() + + @property + def kind(self) -> str: + return "grpc_asyncio" + + @property + def delete_operation( + self, + ) -> Callable[[operations_pb2.DeleteOperationRequest], None]: + r"""Return a callable for the delete_operation method over gRPC.""" + # 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 "delete_operation" not in self._stubs: + self._stubs["delete_operation"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/DeleteOperation", + request_serializer=operations_pb2.DeleteOperationRequest.SerializeToString, + response_deserializer=None, + ) + return self._stubs["delete_operation"] + + @property + def cancel_operation( + self, + ) -> Callable[[operations_pb2.CancelOperationRequest], None]: + r"""Return a callable for the cancel_operation method over gRPC.""" + # 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 "cancel_operation" not in self._stubs: + self._stubs["cancel_operation"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/CancelOperation", + request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, + response_deserializer=None, + ) + return self._stubs["cancel_operation"] + + @property + def get_operation( + self, + ) -> Callable[[operations_pb2.GetOperationRequest], operations_pb2.Operation]: + r"""Return a callable for the get_operation method over gRPC.""" + # 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 "get_operation" not in self._stubs: + self._stubs["get_operation"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/GetOperation", + request_serializer=operations_pb2.GetOperationRequest.SerializeToString, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["get_operation"] + + @property + def list_operations( + self, + ) -> Callable[ + [operations_pb2.ListOperationsRequest], operations_pb2.ListOperationsResponse + ]: + r"""Return a callable for the list_operations method over gRPC.""" + # 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 "list_operations" not in self._stubs: + self._stubs["list_operations"] = self._logged_channel.unary_unary( + "/google.longrunning.Operations/ListOperations", + request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, + response_deserializer=operations_pb2.ListOperationsResponse.FromString, + ) + return self._stubs["list_operations"] + + @property + def list_locations( + self, + ) -> Callable[ + [locations_pb2.ListLocationsRequest], locations_pb2.ListLocationsResponse + ]: + r"""Return a callable for the list locations method over gRPC.""" + # 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 "list_locations" not in self._stubs: + self._stubs["list_locations"] = self._logged_channel.unary_unary( + "/google.cloud.location.Locations/ListLocations", + request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, + response_deserializer=locations_pb2.ListLocationsResponse.FromString, + ) + return self._stubs["list_locations"] + + @property + def get_location( + self, + ) -> Callable[[locations_pb2.GetLocationRequest], locations_pb2.Location]: + r"""Return a callable for the list locations method over gRPC.""" + # 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 "get_location" not in self._stubs: + self._stubs["get_location"] = self._logged_channel.unary_unary( + "/google.cloud.location.Locations/GetLocation", + request_serializer=locations_pb2.GetLocationRequest.SerializeToString, + response_deserializer=locations_pb2.Location.FromString, + ) + return self._stubs["get_location"] + + +__all__ = ("ApiHubDiscoveryGrpcAsyncIOTransport",) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/__init__.py index 07e408161223..f3e42ab7b2d3 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/__init__.py @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from .async_client import ApiHubPluginAsyncClient from .client import ApiHubPluginClient -__all__ = ("ApiHubPluginClient",) +__all__ = ( + "ApiHubPluginClient", + "ApiHubPluginAsyncClient", +) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/async_client.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/async_client.py index 6f83920a990a..aced5f7275a9 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/async_client.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/async_client.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,6 +14,7 @@ # limitations under the License. # from collections import OrderedDict +import logging as std_logging import re from typing import ( Callable, @@ -34,6 +35,7 @@ from google.api_core.client_options import ClientOptions from google.auth import credentials as ga_credentials # type: ignore from google.oauth2 import service_account # type: ignore +import google.protobuf from google.cloud.apihub_v1 import gapic_version as package_version @@ -42,15 +44,30 @@ except AttributeError: # pragma: NO COVER OptionalRetry = Union[retries.AsyncRetry, object, None] # type: ignore +from google.api_core import operation # type: ignore +from google.api_core import operation_async # type: ignore from google.cloud.location import locations_pb2 # type: ignore from google.longrunning import operations_pb2 # type: ignore +from google.protobuf import empty_pb2 # type: ignore +from google.protobuf import field_mask_pb2 # type: ignore +from google.protobuf import timestamp_pb2 # type: ignore +from google.cloud.apihub_v1.services.api_hub_plugin import pagers from google.cloud.apihub_v1.types import common_fields, plugin_service from .client import ApiHubPluginClient from .transports.base import DEFAULT_CLIENT_INFO, ApiHubPluginTransport from .transports.grpc_asyncio import ApiHubPluginGrpcAsyncIOTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + class ApiHubPluginAsyncClient: """This service is used for managing plugins inside the API Hub.""" @@ -68,6 +85,14 @@ class ApiHubPluginAsyncClient: parse_attribute_path = staticmethod(ApiHubPluginClient.parse_attribute_path) plugin_path = staticmethod(ApiHubPluginClient.plugin_path) parse_plugin_path = staticmethod(ApiHubPluginClient.parse_plugin_path) + plugin_instance_path = staticmethod(ApiHubPluginClient.plugin_instance_path) + parse_plugin_instance_path = staticmethod( + ApiHubPluginClient.parse_plugin_instance_path + ) + service_account_path = staticmethod(ApiHubPluginClient.service_account_path) + parse_service_account_path = staticmethod( + ApiHubPluginClient.parse_service_account_path + ) common_billing_account_path = staticmethod( ApiHubPluginClient.common_billing_account_path ) @@ -254,6 +279,28 @@ def __init__( client_info=client_info, ) + if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ): # pragma: NO COVER + _LOGGER.debug( + "Created client `google.cloud.apihub_v1.ApiHubPluginAsyncClient`.", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubPlugin", + "universeDomain": getattr( + self._client._transport._credentials, "universe_domain", "" + ), + "credentialsType": f"{type(self._client._transport._credentials).__module__}.{type(self._client._transport._credentials).__qualname__}", + "credentialsInfo": getattr( + self.transport._credentials, "get_cred_info", lambda: None + )(), + } + if hasattr(self._client._transport, "_credentials") + else { + "serviceName": "google.cloud.apihub.v1.ApiHubPlugin", + "credentialsType": None, + }, + ) + async def get_plugin( self, request: Optional[Union[plugin_service.GetPluginRequest, dict]] = None, @@ -261,9 +308,9 @@ async def get_plugin( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> plugin_service.Plugin: - r"""Get details about an API Hub plugin. + r"""Get an API Hub plugin. .. code-block:: python @@ -306,8 +353,10 @@ async def sample_get_plugin(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Plugin: @@ -316,7 +365,10 @@ async def sample_get_plugin(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -366,7 +418,7 @@ async def enable_plugin( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> plugin_service.Plugin: r"""Enables a plugin. The ``state`` of the plugin after enabling is ``ENABLED`` @@ -412,8 +464,10 @@ async def sample_enable_plugin(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Plugin: @@ -422,7 +476,10 @@ async def sample_enable_plugin(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -472,7 +529,7 @@ async def disable_plugin( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> plugin_service.Plugin: r"""Disables a plugin. The ``state`` of the plugin after disabling is ``DISABLED`` @@ -518,8 +575,10 @@ async def sample_disable_plugin(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.Plugin: @@ -528,7 +587,10 @@ async def sample_disable_plugin(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -571,47 +633,137 @@ async def sample_disable_plugin(): # Done; return the response. return response - async def list_operations( + async def create_plugin( self, - request: Optional[operations_pb2.ListOperationsRequest] = None, + request: Optional[Union[plugin_service.CreatePluginRequest, dict]] = None, *, + parent: Optional[str] = None, + plugin: Optional[plugin_service.Plugin] = None, + plugin_id: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), - ) -> operations_pb2.ListOperationsResponse: - r"""Lists operations that match the specified filter in the request. + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> plugin_service.Plugin: + r"""Create an API Hub plugin resource in the API hub. + Once a plugin is created, it can be used to create + plugin instances. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_create_plugin(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + plugin = apihub_v1.Plugin() + plugin.display_name = "display_name_value" + + request = apihub_v1.CreatePluginRequest( + parent="parent_value", + plugin=plugin, + ) + + # Make the request + response = await client.create_plugin(request=request) + + # Handle the response + print(response) Args: - request (:class:`~.operations_pb2.ListOperationsRequest`): - The request object. Request message for - `ListOperations` method. - retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, - if any, should be retried. + request (Optional[Union[google.cloud.apihub_v1.types.CreatePluginRequest, dict]]): + The request object. The + [CreatePlugin][google.cloud.apihub.v1.ApiHubPlugin.CreatePlugin] + method's request. + parent (:class:`str`): + Required. The parent resource where this plugin will be + created. Format: + ``projects/{project}/locations/{location}``. + + This corresponds to the ``parent`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + plugin (:class:`google.cloud.apihub_v1.types.Plugin`): + Required. The plugin to create. + This corresponds to the ``plugin`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + plugin_id (:class:`str`): + Optional. The ID to use for the Plugin resource, which + will become the final component of the Plugin's resource + name. This field is optional. + + - If provided, the same will be used. The service will + throw an error if the specified id is already used by + another Plugin resource in the API hub instance. + - If not provided, a system generated id will be used. + + This value should be 4-63 characters, overall resource + name which will be of format + ``projects/{project}/locations/{location}/plugins/{plugin}``, + its length is limited to 1000 characters and valid + characters are /[a-z][A-Z][0-9]-\_/. + + This corresponds to the ``plugin_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: - ~.operations_pb2.ListOperationsResponse: - Response message for ``ListOperations`` method. + google.cloud.apihub_v1.types.Plugin: + A plugin resource in the API Hub. """ # Create or coerce a protobuf request object. - # The request isn't a proto-plus wrapped type, - # so it must be constructed via keyword expansion. - if isinstance(request, dict): - request = operations_pb2.ListOperationsRequest(**request) + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [parent, plugin, plugin_id] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, plugin_service.CreatePluginRequest): + request = plugin_service.CreatePluginRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent + if plugin is not None: + request.plugin = plugin + if plugin_id is not None: + request.plugin_id = plugin_id # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.list_operations, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self._client._transport._wrapped_methods[ + self._client._transport.create_plugin + ] # Certain fields should be provided within the metadata header; # add these here. metadata = tuple(metadata) + ( - gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), ) # Validate the universe domain. @@ -628,47 +780,107 @@ async def list_operations( # Done; return the response. return response - async def get_operation( + async def list_plugins( self, - request: Optional[operations_pb2.GetOperationRequest] = None, + request: Optional[Union[plugin_service.ListPluginsRequest, dict]] = None, *, + parent: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), - ) -> operations_pb2.Operation: - r"""Gets the latest state of a long-running operation. + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> pagers.ListPluginsAsyncPager: + r"""List all the plugins in a given project and location. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_list_plugins(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.ListPluginsRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_plugins(request=request) + + # Handle the response + async for response in page_result: + print(response) Args: - request (:class:`~.operations_pb2.GetOperationRequest`): - The request object. Request message for - `GetOperation` method. - retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, - if any, should be retried. + request (Optional[Union[google.cloud.apihub_v1.types.ListPluginsRequest, dict]]): + The request object. The + [ListPlugins][google.cloud.apihub.v1.ApiHubPlugin.ListPlugins] + method's request. + parent (:class:`str`): + Required. The parent resource where this plugin will be + created. Format: + ``projects/{project}/locations/{location}``. + + This corresponds to the ``parent`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: - ~.operations_pb2.Operation: - An ``Operation`` object. + google.cloud.apihub_v1.services.api_hub_plugin.pagers.ListPluginsAsyncPager: + The [ListPlugins][google.cloud.apihub.v1.ApiHubPlugin.ListPlugins] method's + response. + + Iterating over this object will yield results and + resolve additional pages automatically. + """ # Create or coerce a protobuf request object. - # The request isn't a proto-plus wrapped type, - # so it must be constructed via keyword expansion. - if isinstance(request, dict): - request = operations_pb2.GetOperationRequest(**request) + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [parent] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, plugin_service.ListPluginsRequest): + request = plugin_service.ListPluginsRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.get_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self._client._transport._wrapped_methods[ + self._client._transport.list_plugins + ] # Certain fields should be provided within the metadata header; # add these here. metadata = tuple(metadata) + ( - gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), ) # Validate the universe domain. @@ -682,49 +894,127 @@ async def get_operation( metadata=metadata, ) + # This method is paged; wrap the response in a pager, which provides + # an `__aiter__` convenience method. + response = pagers.ListPluginsAsyncPager( + method=rpc, + request=request, + response=response, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + # Done; return the response. return response - async def delete_operation( + async def delete_plugin( self, - request: Optional[operations_pb2.DeleteOperationRequest] = None, + request: Optional[Union[plugin_service.DeletePluginRequest, dict]] = None, *, + name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), - ) -> None: - r"""Deletes a long-running operation. + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> operation_async.AsyncOperation: + r"""Delete a Plugin in API hub. + Note, only user owned plugins can be deleted via this + method. - This method indicates that the client is no longer interested - in the operation result. It does not cancel the operation. - If the server doesn't support this method, it returns - `google.rpc.Code.UNIMPLEMENTED`. + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_delete_plugin(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DeletePluginRequest( + name="name_value", + ) + + # Make the request + operation = client.delete_plugin(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) Args: - request (:class:`~.operations_pb2.DeleteOperationRequest`): - The request object. Request message for - `DeleteOperation` method. - retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, - if any, should be retried. + request (Optional[Union[google.cloud.apihub_v1.types.DeletePluginRequest, dict]]): + The request object. The [DeletePlugin][ApiHub.DeletePlugin] method's + request. + name (:class:`str`): + Required. The name of the Plugin resource to delete. + Format: + ``projects/{project}/locations/{location}/plugins/{plugin}`` + + This corresponds to the ``name`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: - None + google.api_core.operation_async.AsyncOperation: + An object representing a long-running operation. + + The result type for the operation will be :class:`google.protobuf.empty_pb2.Empty` A generic empty message that you can re-use to avoid defining duplicated + empty messages in your APIs. A typical example is to + use it as the request or the response type of an API + method. For instance: + + service Foo { + rpc Bar(google.protobuf.Empty) returns + (google.protobuf.Empty); + + } + """ # Create or coerce a protobuf request object. - # The request isn't a proto-plus wrapped type, - # so it must be constructed via keyword expansion. - if isinstance(request, dict): - request = operations_pb2.DeleteOperationRequest(**request) + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, plugin_service.DeletePluginRequest): + request = plugin_service.DeletePluginRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if name is not None: + request.name = name # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.delete_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self._client._transport._wrapped_methods[ + self._client._transport.delete_plugin + ] # Certain fields should be provided within the metadata header; # add these here. @@ -736,53 +1026,1352 @@ async def delete_operation( self._client._validate_universe_domain() # Send the request. - await rpc( + response = await rpc( request, retry=retry, timeout=timeout, metadata=metadata, ) - async def cancel_operation( + # Wrap the response in an operation future. + response = operation_async.from_gapic( + response, + self._client._transport.operations_client, + empty_pb2.Empty, + metadata_type=common_fields.OperationMetadata, + ) + + # Done; return the response. + return response + + async def create_plugin_instance( self, - request: Optional[operations_pb2.CancelOperationRequest] = None, + request: Optional[ + Union[plugin_service.CreatePluginInstanceRequest, dict] + ] = None, *, + parent: Optional[str] = None, + plugin_instance: Optional[plugin_service.PluginInstance] = None, + plugin_instance_id: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), - ) -> None: - r"""Starts asynchronous cancellation on a long-running operation. - - The server makes a best effort to cancel the operation, but success - is not guaranteed. If the server doesn't support this method, it returns - `google.rpc.Code.UNIMPLEMENTED`. + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> operation_async.AsyncOperation: + r"""Creates a Plugin instance in the API hub. - Args: - request (:class:`~.operations_pb2.CancelOperationRequest`): - The request object. Request message for - `CancelOperation` method. - retry (google.api_core.retry_async.AsyncRetry): 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: - None - """ - # Create or coerce a protobuf request object. - # The request isn't a proto-plus wrapped type, - # so it must be constructed via keyword expansion. - if isinstance(request, dict): - request = operations_pb2.CancelOperationRequest(**request) + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_create_plugin_instance(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + plugin_instance = apihub_v1.PluginInstance() + plugin_instance.display_name = "display_name_value" + plugin_instance.actions.action_id = "action_id_value" + + request = apihub_v1.CreatePluginInstanceRequest( + parent="parent_value", + plugin_instance=plugin_instance, + ) + + # Make the request + operation = client.create_plugin_instance(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.CreatePluginInstanceRequest, dict]]): + The request object. The + [CreatePluginInstance][google.cloud.apihub.v1.ApiHubPlugin.CreatePluginInstance] + method's request. + parent (:class:`str`): + Required. The parent of the plugin instance resource. + Format: + ``projects/{project}/locations/{location}/plugins/{plugin}`` + + This corresponds to the ``parent`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + plugin_instance (:class:`google.cloud.apihub_v1.types.PluginInstance`): + Required. The plugin instance to + create. + + This corresponds to the ``plugin_instance`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + plugin_instance_id (:class:`str`): + Optional. The ID to use for the plugin instance, which + will become the final component of the plugin instance's + resource name. This field is optional. + + - If provided, the same will be used. The service will + throw an error if the specified id is already used by + another plugin instance in the plugin resource. + - If not provided, a system generated id will be used. + + This value should be 4-63 characters, and valid + characters are /[a-z][A-Z][0-9]-\_/. + + This corresponds to the ``plugin_instance_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + + Returns: + google.api_core.operation_async.AsyncOperation: + An object representing a long-running operation. + + The result type for the operation will be :class:`google.cloud.apihub_v1.types.PluginInstance` Represents a plugin instance resource in the API Hub. + A PluginInstance is a specific instance of a hub + plugin with its own configuration, state, and + execution details. + + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [parent, plugin_instance, plugin_instance_id] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, plugin_service.CreatePluginInstanceRequest): + request = plugin_service.CreatePluginInstanceRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent + if plugin_instance is not None: + request.plugin_instance = plugin_instance + if plugin_instance_id is not None: + request.plugin_instance_id = plugin_instance_id + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.create_plugin_instance + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Wrap the response in an operation future. + response = operation_async.from_gapic( + response, + self._client._transport.operations_client, + plugin_service.PluginInstance, + metadata_type=common_fields.OperationMetadata, + ) + + # Done; return the response. + return response + + async def execute_plugin_instance_action( + self, + request: Optional[ + Union[plugin_service.ExecutePluginInstanceActionRequest, dict] + ] = None, + *, + name: Optional[str] = None, + action_execution_detail: Optional[plugin_service.ActionExecutionDetail] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> operation_async.AsyncOperation: + r"""Executes a plugin instance in the API hub. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_execute_plugin_instance_action(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + action_execution_detail = apihub_v1.ActionExecutionDetail() + action_execution_detail.action_id = "action_id_value" + + request = apihub_v1.ExecutePluginInstanceActionRequest( + name="name_value", + action_execution_detail=action_execution_detail, + ) + + # Make the request + operation = client.execute_plugin_instance_action(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.ExecutePluginInstanceActionRequest, dict]]): + The request object. The + [ExecutePluginInstanceAction][google.cloud.apihub.v1.ApiHubPlugin.ExecutePluginInstanceAction] + method's request. + name (:class:`str`): + Required. The name of the plugin instance to execute. + Format: + ``projects/{project}/locations/{location}/plugins/{plugin}/instances/{instance}`` + + This corresponds to the ``name`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + action_execution_detail (:class:`google.cloud.apihub_v1.types.ActionExecutionDetail`): + Required. The execution details for + the action to execute. + + This corresponds to the ``action_execution_detail`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + + Returns: + google.api_core.operation_async.AsyncOperation: + An object representing a long-running operation. + + The result type for the operation will be :class:`google.cloud.apihub_v1.types.ExecutePluginInstanceActionResponse` The + [ExecutePluginInstanceAction][google.cloud.apihub.v1.ApiHubPlugin.ExecutePluginInstanceAction] + method's response. + + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [name, action_execution_detail] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, plugin_service.ExecutePluginInstanceActionRequest): + request = plugin_service.ExecutePluginInstanceActionRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if name is not None: + request.name = name + if action_execution_detail is not None: + request.action_execution_detail = action_execution_detail + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.execute_plugin_instance_action + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Wrap the response in an operation future. + response = operation_async.from_gapic( + response, + self._client._transport.operations_client, + plugin_service.ExecutePluginInstanceActionResponse, + metadata_type=common_fields.OperationMetadata, + ) + + # Done; return the response. + return response + + async def get_plugin_instance( + self, + request: Optional[Union[plugin_service.GetPluginInstanceRequest, dict]] = None, + *, + name: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> plugin_service.PluginInstance: + r"""Get an API Hub plugin instance. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_get_plugin_instance(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetPluginInstanceRequest( + name="name_value", + ) + + # Make the request + response = await client.get_plugin_instance(request=request) + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.GetPluginInstanceRequest, dict]]): + The request object. The + [GetPluginInstance][google.cloud.apihub.v1.ApiHubPlugin.GetPluginInstance] + method's request. + name (:class:`str`): + Required. The name of the plugin instance to retrieve. + Format: + ``projects/{project}/locations/{location}/plugins/{plugin}/instances/{instance}`` + + This corresponds to the ``name`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + + Returns: + google.cloud.apihub_v1.types.PluginInstance: + Represents a plugin instance resource + in the API Hub. A PluginInstance is a + specific instance of a hub plugin with + its own configuration, state, and + execution details. + + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, plugin_service.GetPluginInstanceRequest): + request = plugin_service.GetPluginInstanceRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if name is not None: + request.name = name + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.get_plugin_instance + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def list_plugin_instances( + self, + request: Optional[ + Union[plugin_service.ListPluginInstancesRequest, dict] + ] = None, + *, + parent: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> pagers.ListPluginInstancesAsyncPager: + r"""List all the plugins in a given project and location. ``-`` can + be used as wildcard value for {plugin_id} + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_list_plugin_instances(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.ListPluginInstancesRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_plugin_instances(request=request) + + # Handle the response + async for response in page_result: + print(response) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.ListPluginInstancesRequest, dict]]): + The request object. The + [ListPluginInstances][google.cloud.apihub.v1.ApiHubPlugin.ListPluginInstances] + method's request. + parent (:class:`str`): + Required. The parent resource where this plugin will be + created. Format: + ``projects/{project}/locations/{location}/plugins/{plugin}``. + To list plugin instances for multiple plugins, use the - + character instead of the plugin ID. + + This corresponds to the ``parent`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + + Returns: + google.cloud.apihub_v1.services.api_hub_plugin.pagers.ListPluginInstancesAsyncPager: + The + [ListPluginInstances][google.cloud.apihub.v1.ApiHubPlugin.ListPluginInstances] + method's response. + + Iterating over this object will yield results and + resolve additional pages automatically. + + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [parent] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, plugin_service.ListPluginInstancesRequest): + request = plugin_service.ListPluginInstancesRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.list_plugin_instances + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # This method is paged; wrap the response in a pager, which provides + # an `__aiter__` convenience method. + response = pagers.ListPluginInstancesAsyncPager( + method=rpc, + request=request, + response=response, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def enable_plugin_instance_action( + self, + request: Optional[ + Union[plugin_service.EnablePluginInstanceActionRequest, dict] + ] = None, + *, + name: Optional[str] = None, + action_id: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> operation_async.AsyncOperation: + r"""Enables a plugin instance in the API hub. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_enable_plugin_instance_action(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.EnablePluginInstanceActionRequest( + name="name_value", + action_id="action_id_value", + ) + + # Make the request + operation = client.enable_plugin_instance_action(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.EnablePluginInstanceActionRequest, dict]]): + The request object. The + [EnablePluginInstanceAction][google.cloud.apihub.v1.ApiHubPlugin.EnablePluginInstanceAction] + method's request. + name (:class:`str`): + Required. The name of the plugin instance to enable. + Format: + ``projects/{project}/locations/{location}/plugins/{plugin}/instances/{instance}`` + + This corresponds to the ``name`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + action_id (:class:`str`): + Required. The action id to enable. + This corresponds to the ``action_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + + Returns: + google.api_core.operation_async.AsyncOperation: + An object representing a long-running operation. + + The result type for the operation will be :class:`google.cloud.apihub_v1.types.EnablePluginInstanceActionResponse` The + [EnablePluginInstanceAction][google.cloud.apihub.v1.ApiHubPlugin.EnablePluginInstanceAction] + method's response. + + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [name, action_id] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, plugin_service.EnablePluginInstanceActionRequest): + request = plugin_service.EnablePluginInstanceActionRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if name is not None: + request.name = name + if action_id is not None: + request.action_id = action_id + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.enable_plugin_instance_action + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Wrap the response in an operation future. + response = operation_async.from_gapic( + response, + self._client._transport.operations_client, + plugin_service.EnablePluginInstanceActionResponse, + metadata_type=common_fields.OperationMetadata, + ) + + # Done; return the response. + return response + + async def disable_plugin_instance_action( + self, + request: Optional[ + Union[plugin_service.DisablePluginInstanceActionRequest, dict] + ] = None, + *, + name: Optional[str] = None, + action_id: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> operation_async.AsyncOperation: + r"""Disables a plugin instance in the API hub. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_disable_plugin_instance_action(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DisablePluginInstanceActionRequest( + name="name_value", + action_id="action_id_value", + ) + + # Make the request + operation = client.disable_plugin_instance_action(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.DisablePluginInstanceActionRequest, dict]]): + The request object. The + [DisablePluginInstanceAction][google.cloud.apihub.v1.ApiHubPlugin.DisablePluginInstanceAction] + method's request. + name (:class:`str`): + Required. The name of the plugin instance to disable. + Format: + ``projects/{project}/locations/{location}/plugins/{plugin}/instances/{instance}`` + + This corresponds to the ``name`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + action_id (:class:`str`): + Required. The action id to disable. + This corresponds to the ``action_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + + Returns: + google.api_core.operation_async.AsyncOperation: + An object representing a long-running operation. + + The result type for the operation will be :class:`google.cloud.apihub_v1.types.DisablePluginInstanceActionResponse` The + [DisablePluginInstanceAction][google.cloud.apihub.v1.ApiHubPlugin.DisablePluginInstanceAction] + method's response. + + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [name, action_id] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, plugin_service.DisablePluginInstanceActionRequest): + request = plugin_service.DisablePluginInstanceActionRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if name is not None: + request.name = name + if action_id is not None: + request.action_id = action_id + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.disable_plugin_instance_action + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Wrap the response in an operation future. + response = operation_async.from_gapic( + response, + self._client._transport.operations_client, + plugin_service.DisablePluginInstanceActionResponse, + metadata_type=common_fields.OperationMetadata, + ) + + # Done; return the response. + return response + + async def update_plugin_instance( + self, + request: Optional[ + Union[plugin_service.UpdatePluginInstanceRequest, dict] + ] = None, + *, + plugin_instance: Optional[plugin_service.PluginInstance] = None, + update_mask: Optional[field_mask_pb2.FieldMask] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> plugin_service.PluginInstance: + r"""Updates a plugin instance in the API hub. The following fields + in the [plugin_instance][google.cloud.apihub.v1.PluginInstance] + can be updated currently: + + - [display_name][google.cloud.apihub.v1.PluginInstance.display_name] + - [schedule_cron_expression][PluginInstance.actions.schedule_cron_expression] + + The + [update_mask][google.cloud.apihub.v1.UpdatePluginInstanceRequest.update_mask] + should be used to specify the fields being updated. + + To update the + [auth_config][google.cloud.apihub.v1.PluginInstance.auth_config] + and + [additional_config][google.cloud.apihub.v1.PluginInstance.additional_config] + of the plugin instance, use the + [ApplyPluginInstanceConfig][google.cloud.apihub.v1.ApiHubPlugin.ApplyPluginInstanceConfig] + method. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_update_plugin_instance(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + plugin_instance = apihub_v1.PluginInstance() + plugin_instance.display_name = "display_name_value" + plugin_instance.actions.action_id = "action_id_value" + + request = apihub_v1.UpdatePluginInstanceRequest( + plugin_instance=plugin_instance, + ) + + # Make the request + response = await client.update_plugin_instance(request=request) + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.UpdatePluginInstanceRequest, dict]]): + The request object. The + [UpdatePluginInstance][google.cloud.apihub.v1.ApiHubPlugin.UpdatePluginInstance] + method's request. + plugin_instance (:class:`google.cloud.apihub_v1.types.PluginInstance`): + Required. The plugin instance to + update. + + This corresponds to the ``plugin_instance`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + update_mask (:class:`google.protobuf.field_mask_pb2.FieldMask`): + Optional. The list of fields to + update. + + This corresponds to the ``update_mask`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + + Returns: + google.cloud.apihub_v1.types.PluginInstance: + Represents a plugin instance resource + in the API Hub. A PluginInstance is a + specific instance of a hub plugin with + its own configuration, state, and + execution details. + + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [plugin_instance, update_mask] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, plugin_service.UpdatePluginInstanceRequest): + request = plugin_service.UpdatePluginInstanceRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if plugin_instance is not None: + request.plugin_instance = plugin_instance + if update_mask is not None: + request.update_mask = update_mask + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.update_plugin_instance + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata( + (("plugin_instance.name", request.plugin_instance.name),) + ), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def delete_plugin_instance( + self, + request: Optional[ + Union[plugin_service.DeletePluginInstanceRequest, dict] + ] = None, + *, + name: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> operation_async.AsyncOperation: + r"""Deletes a plugin instance in the API hub. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_delete_plugin_instance(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DeletePluginInstanceRequest( + name="name_value", + ) + + # Make the request + operation = client.delete_plugin_instance(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.DeletePluginInstanceRequest, dict]]): + The request object. The + [DeletePluginInstance][google.cloud.apihub.v1.ApiHubPlugin.DeletePluginInstance] + method's request. + name (:class:`str`): + Required. The name of the plugin instance to delete. + Format: + ``projects/{project}/locations/{location}/plugins/{plugin}/instances/{instance}``. + + This corresponds to the ``name`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + + Returns: + google.api_core.operation_async.AsyncOperation: + An object representing a long-running operation. + + The result type for the operation will be :class:`google.protobuf.empty_pb2.Empty` A generic empty message that you can re-use to avoid defining duplicated + empty messages in your APIs. A typical example is to + use it as the request or the response type of an API + method. For instance: + + service Foo { + rpc Bar(google.protobuf.Empty) returns + (google.protobuf.Empty); + + } + + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, plugin_service.DeletePluginInstanceRequest): + request = plugin_service.DeletePluginInstanceRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if name is not None: + request.name = name + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.delete_plugin_instance + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Wrap the response in an operation future. + response = operation_async.from_gapic( + response, + self._client._transport.operations_client, + empty_pb2.Empty, + metadata_type=common_fields.OperationMetadata, + ) + + # Done; return the response. + return response + + async def list_operations( + self, + request: Optional[operations_pb2.ListOperationsRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> operations_pb2.ListOperationsResponse: + r"""Lists operations that match the specified filter in the request. + + Args: + request (:class:`~.operations_pb2.ListOperationsRequest`): + The request object. Request message for + `ListOperations` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + ~.operations_pb2.ListOperationsResponse: + Response message for ``ListOperations`` method. + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = operations_pb2.ListOperationsRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.list_operations] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def get_operation( + self, + request: Optional[operations_pb2.GetOperationRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> operations_pb2.Operation: + r"""Gets the latest state of a long-running operation. + + Args: + request (:class:`~.operations_pb2.GetOperationRequest`): + The request object. Request message for + `GetOperation` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + ~.operations_pb2.Operation: + An ``Operation`` object. + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = operations_pb2.GetOperationRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.get_operation] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def delete_operation( + self, + request: Optional[operations_pb2.DeleteOperationRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> None: + r"""Deletes a long-running operation. + + This method indicates that the client is no longer interested + in the operation result. It does not cancel the operation. + If the server doesn't support this method, it returns + `google.rpc.Code.UNIMPLEMENTED`. + + Args: + request (:class:`~.operations_pb2.DeleteOperationRequest`): + The request object. Request message for + `DeleteOperation` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + None + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = operations_pb2.DeleteOperationRequest(**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.cancel_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, + rpc = self.transport._wrapped_methods[self._client._transport.delete_operation] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, ) + async def cancel_operation( + self, + request: Optional[operations_pb2.CancelOperationRequest] = None, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> None: + r"""Starts asynchronous cancellation on a long-running operation. + + The server makes a best effort to cancel the operation, but success + is not guaranteed. If the server doesn't support this method, it returns + `google.rpc.Code.UNIMPLEMENTED`. + + Args: + request (:class:`~.operations_pb2.CancelOperationRequest`): + The request object. Request message for + `CancelOperation` method. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + Returns: + None + """ + # Create or coerce a protobuf request object. + # The request isn't a proto-plus wrapped type, + # so it must be constructed via keyword expansion. + if isinstance(request, dict): + request = operations_pb2.CancelOperationRequest(**request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self.transport._wrapped_methods[self._client._transport.cancel_operation] + # Certain fields should be provided within the metadata header; # add these here. metadata = tuple(metadata) + ( @@ -806,7 +2395,7 @@ async def get_location( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> locations_pb2.Location: r"""Gets information about a location. @@ -817,8 +2406,10 @@ async def get_location( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.location_pb2.Location: Location object. @@ -831,11 +2422,7 @@ async def get_location( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.get_location, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.get_location] # Certain fields should be provided within the metadata header; # add these here. @@ -863,7 +2450,7 @@ async def list_locations( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> locations_pb2.ListLocationsResponse: r"""Lists information about the supported locations for this service. @@ -874,8 +2461,10 @@ async def list_locations( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.location_pb2.ListLocationsResponse: Response message for ``ListLocations`` method. @@ -888,11 +2477,7 @@ async def list_locations( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.list_locations, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.list_locations] # Certain fields should be provided within the metadata header; # add these here. @@ -925,5 +2510,8 @@ async def __aexit__(self, exc_type, exc, tb): gapic_version=package_version.__version__ ) +if hasattr(DEFAULT_CLIENT_INFO, "protobuf_runtime_version"): # pragma: NO COVER + DEFAULT_CLIENT_INFO.protobuf_runtime_version = google.protobuf.__version__ + __all__ = ("ApiHubPluginAsyncClient",) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/client.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/client.py index 81945c27bea5..188c4983a164 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/client.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/client.py @@ -73,6 +73,8 @@ from google.cloud.apihub_v1.types import common_fields, plugin_service from .transports.base import DEFAULT_CLIENT_INFO, ApiHubPluginTransport +from .transports.grpc import ApiHubPluginGrpcTransport +from .transports.grpc_asyncio import ApiHubPluginGrpcAsyncIOTransport from .transports.rest import ApiHubPluginRestTransport @@ -85,6 +87,8 @@ class ApiHubPluginClientMeta(type): """ _transport_registry = OrderedDict() # type: Dict[str, Type[ApiHubPluginTransport]] + _transport_registry["grpc"] = ApiHubPluginGrpcTransport + _transport_registry["grpc_asyncio"] = ApiHubPluginGrpcAsyncIOTransport _transport_registry["rest"] = ApiHubPluginRestTransport def get_transport_class( diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/pagers.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/pagers.py index 4d94649795ef..92aa37d721d2 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/pagers.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/pagers.py @@ -117,6 +117,86 @@ def __repr__(self) -> str: return "{0}<{1!r}>".format(self.__class__.__name__, self._response) +class ListPluginsAsyncPager: + """A pager for iterating through ``list_plugins`` requests. + + This class thinly wraps an initial + :class:`google.cloud.apihub_v1.types.ListPluginsResponse` object, and + provides an ``__aiter__`` method to iterate through its + ``plugins`` field. + + If there are more pages, the ``__aiter__`` method will make additional + ``ListPlugins`` requests and continue to iterate + through the ``plugins`` field on the + corresponding responses. + + All the usual :class:`google.cloud.apihub_v1.types.ListPluginsResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., Awaitable[plugin_service.ListPluginsResponse]], + request: plugin_service.ListPluginsRequest, + response: plugin_service.ListPluginsResponse, + *, + retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = () + ): + """Instantiates the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.apihub_v1.types.ListPluginsRequest): + The initial request object. + response (google.cloud.apihub_v1.types.ListPluginsResponse): + The initial response object. + retry (google.api_core.retry.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + """ + self._method = method + self._request = plugin_service.ListPluginsRequest(request) + self._response = response + self._retry = retry + self._timeout = timeout + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + async def pages(self) -> AsyncIterator[plugin_service.ListPluginsResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = await self._method( + self._request, + retry=self._retry, + timeout=self._timeout, + metadata=self._metadata, + ) + yield self._response + + def __aiter__(self) -> AsyncIterator[plugin_service.Plugin]: + async def async_generator(): + async for page in self.pages: + for response in page.plugins: + yield response + + return async_generator() + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + class ListPluginInstancesPager: """A pager for iterating through ``list_plugin_instances`` requests. @@ -191,3 +271,83 @@ def __iter__(self) -> Iterator[plugin_service.PluginInstance]: def __repr__(self) -> str: return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPluginInstancesAsyncPager: + """A pager for iterating through ``list_plugin_instances`` requests. + + This class thinly wraps an initial + :class:`google.cloud.apihub_v1.types.ListPluginInstancesResponse` object, and + provides an ``__aiter__`` method to iterate through its + ``plugin_instances`` field. + + If there are more pages, the ``__aiter__`` method will make additional + ``ListPluginInstances`` requests and continue to iterate + through the ``plugin_instances`` field on the + corresponding responses. + + All the usual :class:`google.cloud.apihub_v1.types.ListPluginInstancesResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., Awaitable[plugin_service.ListPluginInstancesResponse]], + request: plugin_service.ListPluginInstancesRequest, + response: plugin_service.ListPluginInstancesResponse, + *, + retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = () + ): + """Instantiates the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.apihub_v1.types.ListPluginInstancesRequest): + The initial request object. + response (google.cloud.apihub_v1.types.ListPluginInstancesResponse): + The initial response object. + retry (google.api_core.retry.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + """ + self._method = method + self._request = plugin_service.ListPluginInstancesRequest(request) + self._response = response + self._retry = retry + self._timeout = timeout + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + async def pages(self) -> AsyncIterator[plugin_service.ListPluginInstancesResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = await self._method( + self._request, + retry=self._retry, + timeout=self._timeout, + metadata=self._metadata, + ) + yield self._response + + def __aiter__(self) -> AsyncIterator[plugin_service.PluginInstance]: + async def async_generator(): + async for page in self.pages: + for response in page.plugin_instances: + yield response + + return async_generator() + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/transports/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/transports/__init__.py index 13eccf927d99..b294f9575be5 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/transports/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/transports/__init__.py @@ -17,14 +17,20 @@ from typing import Dict, Type from .base import ApiHubPluginTransport +from .grpc import ApiHubPluginGrpcTransport +from .grpc_asyncio import ApiHubPluginGrpcAsyncIOTransport from .rest import ApiHubPluginRestInterceptor, ApiHubPluginRestTransport # Compile a registry of transports. _transport_registry = OrderedDict() # type: Dict[str, Type[ApiHubPluginTransport]] +_transport_registry["grpc"] = ApiHubPluginGrpcTransport +_transport_registry["grpc_asyncio"] = ApiHubPluginGrpcAsyncIOTransport _transport_registry["rest"] = ApiHubPluginRestTransport __all__ = ( "ApiHubPluginTransport", + "ApiHubPluginGrpcTransport", + "ApiHubPluginGrpcAsyncIOTransport", "ApiHubPluginRestTransport", "ApiHubPluginRestInterceptor", ) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/transports/grpc.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/transports/grpc.py index e40d93292b20..9f743a628714 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/transports/grpc.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/transports/grpc.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,21 +13,101 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import json +import logging as std_logging +import pickle from typing import Callable, Dict, Optional, Sequence, Tuple, Union import warnings -from google.api_core import gapic_v1, grpc_helpers +from google.api_core import gapic_v1, grpc_helpers, operations_v1 import google.auth # type: ignore from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore from google.cloud.location import locations_pb2 # type: ignore from google.longrunning import operations_pb2 # type: ignore +from google.protobuf.json_format import MessageToJson +import google.protobuf.message import grpc # type: ignore +import proto # type: ignore from google.cloud.apihub_v1.types import plugin_service from .base import DEFAULT_CLIENT_INFO, ApiHubPluginTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class _LoggingClientInterceptor(grpc.UnaryUnaryClientInterceptor): # pragma: NO COVER + def intercept_unary_unary(self, continuation, client_call_details, request): + logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ) + if logging_enabled: # pragma: NO COVER + request_metadata = client_call_details.metadata + if isinstance(request, proto.Message): + request_payload = type(request).to_json(request) + elif isinstance(request, google.protobuf.message.Message): + request_payload = MessageToJson(request) + else: + request_payload = f"{type(request).__name__}: {pickle.dumps(request)}" + + request_metadata = { + key: value.decode("utf-8") if isinstance(value, bytes) else value + for key, value in request_metadata + } + grpc_request = { + "payload": request_payload, + "requestMethod": "grpc", + "metadata": dict(request_metadata), + } + _LOGGER.debug( + f"Sending request for {client_call_details.method}", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubPlugin", + "rpcName": str(client_call_details.method), + "request": grpc_request, + "metadata": grpc_request["metadata"], + }, + ) + response = continuation(client_call_details, request) + if logging_enabled: # pragma: NO COVER + response_metadata = response.trailing_metadata() + # Convert gRPC metadata `` to list of tuples + metadata = ( + dict([(k, str(v)) for k, v in response_metadata]) + if response_metadata + else None + ) + result = response.result() + if isinstance(result, proto.Message): + response_payload = type(result).to_json(result) + elif isinstance(result, google.protobuf.message.Message): + response_payload = MessageToJson(result) + else: + response_payload = f"{type(result).__name__}: {pickle.dumps(result)}" + grpc_response = { + "payload": response_payload, + "metadata": metadata, + "status": "OK", + } + _LOGGER.debug( + f"Received response for {client_call_details.method}.", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubPlugin", + "rpcName": client_call_details.method, + "response": grpc_response, + "metadata": grpc_response["metadata"], + }, + ) + return response + class ApiHubPluginGrpcTransport(ApiHubPluginTransport): """gRPC backend transport for ApiHubPlugin. @@ -115,6 +195,7 @@ def __init__( self._grpc_channel = None self._ssl_channel_credentials = ssl_channel_credentials self._stubs: Dict[str, Callable] = {} + self._operations_client: Optional[operations_v1.OperationsClient] = None if api_mtls_endpoint: warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning) @@ -181,7 +262,12 @@ def __init__( ], ) - # Wrap messages. This must be done after self._grpc_channel exists + self._interceptor = _LoggingClientInterceptor() + self._logged_channel = grpc.intercept_channel( + self._grpc_channel, self._interceptor + ) + + # Wrap messages. This must be done after self._logged_channel exists self._prep_wrapped_messages(client_info) @classmethod @@ -236,13 +322,29 @@ def grpc_channel(self) -> grpc.Channel: """Return the channel designed to connect to this service.""" return self._grpc_channel + @property + def operations_client(self) -> operations_v1.OperationsClient: + """Create the client designed to process long-running operations. + + This property caches on the instance; repeated calls return the same + client. + """ + # Quick check: Only create a new client if we do not already have one. + if self._operations_client is None: + self._operations_client = operations_v1.OperationsClient( + self._logged_channel + ) + + # Return the client from cache. + return self._operations_client + @property def get_plugin( self, ) -> Callable[[plugin_service.GetPluginRequest], plugin_service.Plugin]: r"""Return a callable for the get plugin method over gRPC. - Get details about an API Hub plugin. + Get an API Hub plugin. Returns: Callable[[~.GetPluginRequest], @@ -255,7 +357,7 @@ def get_plugin( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_plugin" not in self._stubs: - self._stubs["get_plugin"] = self.grpc_channel.unary_unary( + self._stubs["get_plugin"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHubPlugin/GetPlugin", request_serializer=plugin_service.GetPluginRequest.serialize, response_deserializer=plugin_service.Plugin.deserialize, @@ -282,7 +384,7 @@ def enable_plugin( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "enable_plugin" not in self._stubs: - self._stubs["enable_plugin"] = self.grpc_channel.unary_unary( + self._stubs["enable_plugin"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHubPlugin/EnablePlugin", request_serializer=plugin_service.EnablePluginRequest.serialize, response_deserializer=plugin_service.Plugin.deserialize, @@ -309,15 +411,348 @@ def disable_plugin( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "disable_plugin" not in self._stubs: - self._stubs["disable_plugin"] = self.grpc_channel.unary_unary( + self._stubs["disable_plugin"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHubPlugin/DisablePlugin", request_serializer=plugin_service.DisablePluginRequest.serialize, response_deserializer=plugin_service.Plugin.deserialize, ) return self._stubs["disable_plugin"] + @property + def create_plugin( + self, + ) -> Callable[[plugin_service.CreatePluginRequest], plugin_service.Plugin]: + r"""Return a callable for the create plugin method over gRPC. + + Create an API Hub plugin resource in the API hub. + Once a plugin is created, it can be used to create + plugin instances. + + Returns: + Callable[[~.CreatePluginRequest], + ~.Plugin]: + 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 "create_plugin" not in self._stubs: + self._stubs["create_plugin"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/CreatePlugin", + request_serializer=plugin_service.CreatePluginRequest.serialize, + response_deserializer=plugin_service.Plugin.deserialize, + ) + return self._stubs["create_plugin"] + + @property + def list_plugins( + self, + ) -> Callable[ + [plugin_service.ListPluginsRequest], plugin_service.ListPluginsResponse + ]: + r"""Return a callable for the list plugins method over gRPC. + + List all the plugins in a given project and location. + + Returns: + Callable[[~.ListPluginsRequest], + ~.ListPluginsResponse]: + 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 "list_plugins" not in self._stubs: + self._stubs["list_plugins"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/ListPlugins", + request_serializer=plugin_service.ListPluginsRequest.serialize, + response_deserializer=plugin_service.ListPluginsResponse.deserialize, + ) + return self._stubs["list_plugins"] + + @property + def delete_plugin( + self, + ) -> Callable[[plugin_service.DeletePluginRequest], operations_pb2.Operation]: + r"""Return a callable for the delete plugin method over gRPC. + + Delete a Plugin in API hub. + Note, only user owned plugins can be deleted via this + method. + + Returns: + Callable[[~.DeletePluginRequest], + ~.Operation]: + 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 "delete_plugin" not in self._stubs: + self._stubs["delete_plugin"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/DeletePlugin", + request_serializer=plugin_service.DeletePluginRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["delete_plugin"] + + @property + def create_plugin_instance( + self, + ) -> Callable[ + [plugin_service.CreatePluginInstanceRequest], operations_pb2.Operation + ]: + r"""Return a callable for the create plugin instance method over gRPC. + + Creates a Plugin instance in the API hub. + + Returns: + Callable[[~.CreatePluginInstanceRequest], + ~.Operation]: + 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 "create_plugin_instance" not in self._stubs: + self._stubs["create_plugin_instance"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/CreatePluginInstance", + request_serializer=plugin_service.CreatePluginInstanceRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["create_plugin_instance"] + + @property + def execute_plugin_instance_action( + self, + ) -> Callable[ + [plugin_service.ExecutePluginInstanceActionRequest], operations_pb2.Operation + ]: + r"""Return a callable for the execute plugin instance action method over gRPC. + + Executes a plugin instance in the API hub. + + Returns: + Callable[[~.ExecutePluginInstanceActionRequest], + ~.Operation]: + 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 "execute_plugin_instance_action" not in self._stubs: + self._stubs[ + "execute_plugin_instance_action" + ] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/ExecutePluginInstanceAction", + request_serializer=plugin_service.ExecutePluginInstanceActionRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["execute_plugin_instance_action"] + + @property + def get_plugin_instance( + self, + ) -> Callable[ + [plugin_service.GetPluginInstanceRequest], plugin_service.PluginInstance + ]: + r"""Return a callable for the get plugin instance method over gRPC. + + Get an API Hub plugin instance. + + Returns: + Callable[[~.GetPluginInstanceRequest], + ~.PluginInstance]: + 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 "get_plugin_instance" not in self._stubs: + self._stubs["get_plugin_instance"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/GetPluginInstance", + request_serializer=plugin_service.GetPluginInstanceRequest.serialize, + response_deserializer=plugin_service.PluginInstance.deserialize, + ) + return self._stubs["get_plugin_instance"] + + @property + def list_plugin_instances( + self, + ) -> Callable[ + [plugin_service.ListPluginInstancesRequest], + plugin_service.ListPluginInstancesResponse, + ]: + r"""Return a callable for the list plugin instances method over gRPC. + + List all the plugins in a given project and location. ``-`` can + be used as wildcard value for {plugin_id} + + Returns: + Callable[[~.ListPluginInstancesRequest], + ~.ListPluginInstancesResponse]: + 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 "list_plugin_instances" not in self._stubs: + self._stubs["list_plugin_instances"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/ListPluginInstances", + request_serializer=plugin_service.ListPluginInstancesRequest.serialize, + response_deserializer=plugin_service.ListPluginInstancesResponse.deserialize, + ) + return self._stubs["list_plugin_instances"] + + @property + def enable_plugin_instance_action( + self, + ) -> Callable[ + [plugin_service.EnablePluginInstanceActionRequest], operations_pb2.Operation + ]: + r"""Return a callable for the enable plugin instance action method over gRPC. + + Enables a plugin instance in the API hub. + + Returns: + Callable[[~.EnablePluginInstanceActionRequest], + ~.Operation]: + 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 "enable_plugin_instance_action" not in self._stubs: + self._stubs[ + "enable_plugin_instance_action" + ] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/EnablePluginInstanceAction", + request_serializer=plugin_service.EnablePluginInstanceActionRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["enable_plugin_instance_action"] + + @property + def disable_plugin_instance_action( + self, + ) -> Callable[ + [plugin_service.DisablePluginInstanceActionRequest], operations_pb2.Operation + ]: + r"""Return a callable for the disable plugin instance action method over gRPC. + + Disables a plugin instance in the API hub. + + Returns: + Callable[[~.DisablePluginInstanceActionRequest], + ~.Operation]: + 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 "disable_plugin_instance_action" not in self._stubs: + self._stubs[ + "disable_plugin_instance_action" + ] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/DisablePluginInstanceAction", + request_serializer=plugin_service.DisablePluginInstanceActionRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["disable_plugin_instance_action"] + + @property + def update_plugin_instance( + self, + ) -> Callable[ + [plugin_service.UpdatePluginInstanceRequest], plugin_service.PluginInstance + ]: + r"""Return a callable for the update plugin instance method over gRPC. + + Updates a plugin instance in the API hub. The following fields + in the [plugin_instance][google.cloud.apihub.v1.PluginInstance] + can be updated currently: + + - [display_name][google.cloud.apihub.v1.PluginInstance.display_name] + - [schedule_cron_expression][PluginInstance.actions.schedule_cron_expression] + + The + [update_mask][google.cloud.apihub.v1.UpdatePluginInstanceRequest.update_mask] + should be used to specify the fields being updated. + + To update the + [auth_config][google.cloud.apihub.v1.PluginInstance.auth_config] + and + [additional_config][google.cloud.apihub.v1.PluginInstance.additional_config] + of the plugin instance, use the + [ApplyPluginInstanceConfig][google.cloud.apihub.v1.ApiHubPlugin.ApplyPluginInstanceConfig] + method. + + Returns: + Callable[[~.UpdatePluginInstanceRequest], + ~.PluginInstance]: + 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 "update_plugin_instance" not in self._stubs: + self._stubs["update_plugin_instance"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/UpdatePluginInstance", + request_serializer=plugin_service.UpdatePluginInstanceRequest.serialize, + response_deserializer=plugin_service.PluginInstance.deserialize, + ) + return self._stubs["update_plugin_instance"] + + @property + def delete_plugin_instance( + self, + ) -> Callable[ + [plugin_service.DeletePluginInstanceRequest], operations_pb2.Operation + ]: + r"""Return a callable for the delete plugin instance method over gRPC. + + Deletes a plugin instance in the API hub. + + Returns: + Callable[[~.DeletePluginInstanceRequest], + ~.Operation]: + 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 "delete_plugin_instance" not in self._stubs: + self._stubs["delete_plugin_instance"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/DeletePluginInstance", + request_serializer=plugin_service.DeletePluginInstanceRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["delete_plugin_instance"] + def close(self): - self.grpc_channel.close() + self._logged_channel.close() @property def delete_operation( @@ -329,7 +764,7 @@ def delete_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_operation" not in self._stubs: - self._stubs["delete_operation"] = self.grpc_channel.unary_unary( + self._stubs["delete_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/DeleteOperation", request_serializer=operations_pb2.DeleteOperationRequest.SerializeToString, response_deserializer=None, @@ -346,7 +781,7 @@ def cancel_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "cancel_operation" not in self._stubs: - self._stubs["cancel_operation"] = self.grpc_channel.unary_unary( + self._stubs["cancel_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/CancelOperation", request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, response_deserializer=None, @@ -363,7 +798,7 @@ def get_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_operation" not in self._stubs: - self._stubs["get_operation"] = self.grpc_channel.unary_unary( + self._stubs["get_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/GetOperation", request_serializer=operations_pb2.GetOperationRequest.SerializeToString, response_deserializer=operations_pb2.Operation.FromString, @@ -382,7 +817,7 @@ def list_operations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_operations" not in self._stubs: - self._stubs["list_operations"] = self.grpc_channel.unary_unary( + self._stubs["list_operations"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/ListOperations", request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, response_deserializer=operations_pb2.ListOperationsResponse.FromString, @@ -401,7 +836,7 @@ def list_locations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_locations" not in self._stubs: - self._stubs["list_locations"] = self.grpc_channel.unary_unary( + self._stubs["list_locations"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/ListLocations", request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, response_deserializer=locations_pb2.ListLocationsResponse.FromString, @@ -418,7 +853,7 @@ def get_location( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_location" not in self._stubs: - self._stubs["get_location"] = self.grpc_channel.unary_unary( + self._stubs["get_location"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/GetLocation", request_serializer=locations_pb2.GetLocationRequest.SerializeToString, response_deserializer=locations_pb2.Location.FromString, diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/transports/grpc_asyncio.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/transports/grpc_asyncio.py index 32ddaaecb024..beeafb8a0b72 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/transports/grpc_asyncio.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/api_hub_plugin/transports/grpc_asyncio.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,24 +13,107 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import inspect +import json +import logging as std_logging +import pickle from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union import warnings from google.api_core import exceptions as core_exceptions -from google.api_core import gapic_v1, grpc_helpers_async +from google.api_core import gapic_v1, grpc_helpers_async, operations_v1 from google.api_core import retry_async as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore from google.cloud.location import locations_pb2 # type: ignore from google.longrunning import operations_pb2 # type: ignore +from google.protobuf.json_format import MessageToJson +import google.protobuf.message import grpc # type: ignore from grpc.experimental import aio # type: ignore +import proto # type: ignore from google.cloud.apihub_v1.types import plugin_service from .base import DEFAULT_CLIENT_INFO, ApiHubPluginTransport from .grpc import ApiHubPluginGrpcTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class _LoggingClientAIOInterceptor( + grpc.aio.UnaryUnaryClientInterceptor +): # pragma: NO COVER + async def intercept_unary_unary(self, continuation, client_call_details, request): + logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ) + if logging_enabled: # pragma: NO COVER + request_metadata = client_call_details.metadata + if isinstance(request, proto.Message): + request_payload = type(request).to_json(request) + elif isinstance(request, google.protobuf.message.Message): + request_payload = MessageToJson(request) + else: + request_payload = f"{type(request).__name__}: {pickle.dumps(request)}" + + request_metadata = { + key: value.decode("utf-8") if isinstance(value, bytes) else value + for key, value in request_metadata + } + grpc_request = { + "payload": request_payload, + "requestMethod": "grpc", + "metadata": dict(request_metadata), + } + _LOGGER.debug( + f"Sending request for {client_call_details.method}", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubPlugin", + "rpcName": str(client_call_details.method), + "request": grpc_request, + "metadata": grpc_request["metadata"], + }, + ) + response = await continuation(client_call_details, request) + if logging_enabled: # pragma: NO COVER + response_metadata = await response.trailing_metadata() + # Convert gRPC metadata `` to list of tuples + metadata = ( + dict([(k, str(v)) for k, v in response_metadata]) + if response_metadata + else None + ) + result = await response + if isinstance(result, proto.Message): + response_payload = type(result).to_json(result) + elif isinstance(result, google.protobuf.message.Message): + response_payload = MessageToJson(result) + else: + response_payload = f"{type(result).__name__}: {pickle.dumps(result)}" + grpc_response = { + "payload": response_payload, + "metadata": metadata, + "status": "OK", + } + _LOGGER.debug( + f"Received response to rpc {client_call_details.method}.", + extra={ + "serviceName": "google.cloud.apihub.v1.ApiHubPlugin", + "rpcName": str(client_call_details.method), + "response": grpc_response, + "metadata": grpc_response["metadata"], + }, + ) + return response + class ApiHubPluginGrpcAsyncIOTransport(ApiHubPluginTransport): """gRPC AsyncIO backend transport for ApiHubPlugin. @@ -162,6 +245,7 @@ def __init__( self._grpc_channel = None self._ssl_channel_credentials = ssl_channel_credentials self._stubs: Dict[str, Callable] = {} + self._operations_client: Optional[operations_v1.OperationsAsyncClient] = None if api_mtls_endpoint: warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning) @@ -227,7 +311,13 @@ def __init__( ], ) - # Wrap messages. This must be done after self._grpc_channel exists + self._interceptor = _LoggingClientAIOInterceptor() + self._grpc_channel._unary_unary_interceptors.append(self._interceptor) + self._logged_channel = self._grpc_channel + self._wrap_with_kind = ( + "kind" in inspect.signature(gapic_v1.method_async.wrap_method).parameters + ) + # Wrap messages. This must be done after self._logged_channel exists self._prep_wrapped_messages(client_info) @property @@ -240,13 +330,29 @@ def grpc_channel(self) -> aio.Channel: # Return the channel from cache. return self._grpc_channel + @property + def operations_client(self) -> operations_v1.OperationsAsyncClient: + """Create the client designed to process long-running operations. + + This property caches on the instance; repeated calls return the same + client. + """ + # Quick check: Only create a new client if we do not already have one. + if self._operations_client is None: + self._operations_client = operations_v1.OperationsAsyncClient( + self._logged_channel + ) + + # Return the client from cache. + return self._operations_client + @property def get_plugin( self, ) -> Callable[[plugin_service.GetPluginRequest], Awaitable[plugin_service.Plugin]]: r"""Return a callable for the get plugin method over gRPC. - Get details about an API Hub plugin. + Get an API Hub plugin. Returns: Callable[[~.GetPluginRequest], @@ -259,7 +365,7 @@ def get_plugin( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_plugin" not in self._stubs: - self._stubs["get_plugin"] = self.grpc_channel.unary_unary( + self._stubs["get_plugin"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHubPlugin/GetPlugin", request_serializer=plugin_service.GetPluginRequest.serialize, response_deserializer=plugin_service.Plugin.deserialize, @@ -288,7 +394,7 @@ def enable_plugin( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "enable_plugin" not in self._stubs: - self._stubs["enable_plugin"] = self.grpc_channel.unary_unary( + self._stubs["enable_plugin"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHubPlugin/EnablePlugin", request_serializer=plugin_service.EnablePluginRequest.serialize, response_deserializer=plugin_service.Plugin.deserialize, @@ -317,17 +423,362 @@ def disable_plugin( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "disable_plugin" not in self._stubs: - self._stubs["disable_plugin"] = self.grpc_channel.unary_unary( + self._stubs["disable_plugin"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.ApiHubPlugin/DisablePlugin", request_serializer=plugin_service.DisablePluginRequest.serialize, response_deserializer=plugin_service.Plugin.deserialize, ) return self._stubs["disable_plugin"] + @property + def create_plugin( + self, + ) -> Callable[ + [plugin_service.CreatePluginRequest], Awaitable[plugin_service.Plugin] + ]: + r"""Return a callable for the create plugin method over gRPC. + + Create an API Hub plugin resource in the API hub. + Once a plugin is created, it can be used to create + plugin instances. + + Returns: + Callable[[~.CreatePluginRequest], + Awaitable[~.Plugin]]: + 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 "create_plugin" not in self._stubs: + self._stubs["create_plugin"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/CreatePlugin", + request_serializer=plugin_service.CreatePluginRequest.serialize, + response_deserializer=plugin_service.Plugin.deserialize, + ) + return self._stubs["create_plugin"] + + @property + def list_plugins( + self, + ) -> Callable[ + [plugin_service.ListPluginsRequest], + Awaitable[plugin_service.ListPluginsResponse], + ]: + r"""Return a callable for the list plugins method over gRPC. + + List all the plugins in a given project and location. + + Returns: + Callable[[~.ListPluginsRequest], + Awaitable[~.ListPluginsResponse]]: + 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 "list_plugins" not in self._stubs: + self._stubs["list_plugins"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/ListPlugins", + request_serializer=plugin_service.ListPluginsRequest.serialize, + response_deserializer=plugin_service.ListPluginsResponse.deserialize, + ) + return self._stubs["list_plugins"] + + @property + def delete_plugin( + self, + ) -> Callable[ + [plugin_service.DeletePluginRequest], Awaitable[operations_pb2.Operation] + ]: + r"""Return a callable for the delete plugin method over gRPC. + + Delete a Plugin in API hub. + Note, only user owned plugins can be deleted via this + method. + + Returns: + Callable[[~.DeletePluginRequest], + Awaitable[~.Operation]]: + 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 "delete_plugin" not in self._stubs: + self._stubs["delete_plugin"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/DeletePlugin", + request_serializer=plugin_service.DeletePluginRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["delete_plugin"] + + @property + def create_plugin_instance( + self, + ) -> Callable[ + [plugin_service.CreatePluginInstanceRequest], + Awaitable[operations_pb2.Operation], + ]: + r"""Return a callable for the create plugin instance method over gRPC. + + Creates a Plugin instance in the API hub. + + Returns: + Callable[[~.CreatePluginInstanceRequest], + Awaitable[~.Operation]]: + 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 "create_plugin_instance" not in self._stubs: + self._stubs["create_plugin_instance"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/CreatePluginInstance", + request_serializer=plugin_service.CreatePluginInstanceRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["create_plugin_instance"] + + @property + def execute_plugin_instance_action( + self, + ) -> Callable[ + [plugin_service.ExecutePluginInstanceActionRequest], + Awaitable[operations_pb2.Operation], + ]: + r"""Return a callable for the execute plugin instance action method over gRPC. + + Executes a plugin instance in the API hub. + + Returns: + Callable[[~.ExecutePluginInstanceActionRequest], + Awaitable[~.Operation]]: + 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 "execute_plugin_instance_action" not in self._stubs: + self._stubs[ + "execute_plugin_instance_action" + ] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/ExecutePluginInstanceAction", + request_serializer=plugin_service.ExecutePluginInstanceActionRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["execute_plugin_instance_action"] + + @property + def get_plugin_instance( + self, + ) -> Callable[ + [plugin_service.GetPluginInstanceRequest], + Awaitable[plugin_service.PluginInstance], + ]: + r"""Return a callable for the get plugin instance method over gRPC. + + Get an API Hub plugin instance. + + Returns: + Callable[[~.GetPluginInstanceRequest], + Awaitable[~.PluginInstance]]: + 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 "get_plugin_instance" not in self._stubs: + self._stubs["get_plugin_instance"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/GetPluginInstance", + request_serializer=plugin_service.GetPluginInstanceRequest.serialize, + response_deserializer=plugin_service.PluginInstance.deserialize, + ) + return self._stubs["get_plugin_instance"] + + @property + def list_plugin_instances( + self, + ) -> Callable[ + [plugin_service.ListPluginInstancesRequest], + Awaitable[plugin_service.ListPluginInstancesResponse], + ]: + r"""Return a callable for the list plugin instances method over gRPC. + + List all the plugins in a given project and location. ``-`` can + be used as wildcard value for {plugin_id} + + Returns: + Callable[[~.ListPluginInstancesRequest], + Awaitable[~.ListPluginInstancesResponse]]: + 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 "list_plugin_instances" not in self._stubs: + self._stubs["list_plugin_instances"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/ListPluginInstances", + request_serializer=plugin_service.ListPluginInstancesRequest.serialize, + response_deserializer=plugin_service.ListPluginInstancesResponse.deserialize, + ) + return self._stubs["list_plugin_instances"] + + @property + def enable_plugin_instance_action( + self, + ) -> Callable[ + [plugin_service.EnablePluginInstanceActionRequest], + Awaitable[operations_pb2.Operation], + ]: + r"""Return a callable for the enable plugin instance action method over gRPC. + + Enables a plugin instance in the API hub. + + Returns: + Callable[[~.EnablePluginInstanceActionRequest], + Awaitable[~.Operation]]: + 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 "enable_plugin_instance_action" not in self._stubs: + self._stubs[ + "enable_plugin_instance_action" + ] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/EnablePluginInstanceAction", + request_serializer=plugin_service.EnablePluginInstanceActionRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["enable_plugin_instance_action"] + + @property + def disable_plugin_instance_action( + self, + ) -> Callable[ + [plugin_service.DisablePluginInstanceActionRequest], + Awaitable[operations_pb2.Operation], + ]: + r"""Return a callable for the disable plugin instance action method over gRPC. + + Disables a plugin instance in the API hub. + + Returns: + Callable[[~.DisablePluginInstanceActionRequest], + Awaitable[~.Operation]]: + 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 "disable_plugin_instance_action" not in self._stubs: + self._stubs[ + "disable_plugin_instance_action" + ] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/DisablePluginInstanceAction", + request_serializer=plugin_service.DisablePluginInstanceActionRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["disable_plugin_instance_action"] + + @property + def update_plugin_instance( + self, + ) -> Callable[ + [plugin_service.UpdatePluginInstanceRequest], + Awaitable[plugin_service.PluginInstance], + ]: + r"""Return a callable for the update plugin instance method over gRPC. + + Updates a plugin instance in the API hub. The following fields + in the [plugin_instance][google.cloud.apihub.v1.PluginInstance] + can be updated currently: + + - [display_name][google.cloud.apihub.v1.PluginInstance.display_name] + - [schedule_cron_expression][PluginInstance.actions.schedule_cron_expression] + + The + [update_mask][google.cloud.apihub.v1.UpdatePluginInstanceRequest.update_mask] + should be used to specify the fields being updated. + + To update the + [auth_config][google.cloud.apihub.v1.PluginInstance.auth_config] + and + [additional_config][google.cloud.apihub.v1.PluginInstance.additional_config] + of the plugin instance, use the + [ApplyPluginInstanceConfig][google.cloud.apihub.v1.ApiHubPlugin.ApplyPluginInstanceConfig] + method. + + Returns: + Callable[[~.UpdatePluginInstanceRequest], + Awaitable[~.PluginInstance]]: + 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 "update_plugin_instance" not in self._stubs: + self._stubs["update_plugin_instance"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/UpdatePluginInstance", + request_serializer=plugin_service.UpdatePluginInstanceRequest.serialize, + response_deserializer=plugin_service.PluginInstance.deserialize, + ) + return self._stubs["update_plugin_instance"] + + @property + def delete_plugin_instance( + self, + ) -> Callable[ + [plugin_service.DeletePluginInstanceRequest], + Awaitable[operations_pb2.Operation], + ]: + r"""Return a callable for the delete plugin instance method over gRPC. + + Deletes a plugin instance in the API hub. + + Returns: + Callable[[~.DeletePluginInstanceRequest], + Awaitable[~.Operation]]: + 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 "delete_plugin_instance" not in self._stubs: + self._stubs["delete_plugin_instance"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.ApiHubPlugin/DeletePluginInstance", + request_serializer=plugin_service.DeletePluginInstanceRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["delete_plugin_instance"] + def _prep_wrapped_messages(self, client_info): """Precompute the wrapped methods, overriding the base class method to use async wrappers.""" self._wrapped_methods = { - self.get_plugin: gapic_v1.method_async.wrap_method( + self.get_plugin: self._wrap_method( self.get_plugin, default_retry=retries.AsyncRetry( initial=1.0, @@ -341,20 +792,114 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.enable_plugin: gapic_v1.method_async.wrap_method( + self.enable_plugin: self._wrap_method( self.enable_plugin, default_timeout=60.0, client_info=client_info, ), - self.disable_plugin: gapic_v1.method_async.wrap_method( + self.disable_plugin: self._wrap_method( self.disable_plugin, default_timeout=60.0, client_info=client_info, ), + self.create_plugin: self._wrap_method( + self.create_plugin, + default_timeout=None, + client_info=client_info, + ), + self.list_plugins: self._wrap_method( + self.list_plugins, + default_timeout=None, + client_info=client_info, + ), + self.delete_plugin: self._wrap_method( + self.delete_plugin, + default_timeout=None, + client_info=client_info, + ), + self.create_plugin_instance: self._wrap_method( + self.create_plugin_instance, + default_timeout=None, + client_info=client_info, + ), + self.execute_plugin_instance_action: self._wrap_method( + self.execute_plugin_instance_action, + default_timeout=None, + client_info=client_info, + ), + self.get_plugin_instance: self._wrap_method( + self.get_plugin_instance, + default_timeout=None, + client_info=client_info, + ), + self.list_plugin_instances: self._wrap_method( + self.list_plugin_instances, + default_timeout=None, + client_info=client_info, + ), + self.enable_plugin_instance_action: self._wrap_method( + self.enable_plugin_instance_action, + default_timeout=None, + client_info=client_info, + ), + self.disable_plugin_instance_action: self._wrap_method( + self.disable_plugin_instance_action, + default_timeout=None, + client_info=client_info, + ), + self.update_plugin_instance: self._wrap_method( + self.update_plugin_instance, + default_timeout=None, + client_info=client_info, + ), + self.delete_plugin_instance: self._wrap_method( + self.delete_plugin_instance, + default_timeout=None, + client_info=client_info, + ), + self.get_location: self._wrap_method( + self.get_location, + default_timeout=None, + client_info=client_info, + ), + self.list_locations: self._wrap_method( + self.list_locations, + default_timeout=None, + client_info=client_info, + ), + self.cancel_operation: self._wrap_method( + self.cancel_operation, + default_timeout=None, + client_info=client_info, + ), + self.delete_operation: self._wrap_method( + self.delete_operation, + default_timeout=None, + client_info=client_info, + ), + self.get_operation: self._wrap_method( + self.get_operation, + default_timeout=None, + client_info=client_info, + ), + self.list_operations: self._wrap_method( + self.list_operations, + default_timeout=None, + client_info=client_info, + ), } + def _wrap_method(self, func, *args, **kwargs): + if self._wrap_with_kind: # pragma: NO COVER + kwargs["kind"] = self.kind + return gapic_v1.method_async.wrap_method(func, *args, **kwargs) + def close(self): - return self.grpc_channel.close() + return self._logged_channel.close() + + @property + def kind(self) -> str: + return "grpc_asyncio" @property def delete_operation( @@ -366,7 +911,7 @@ def delete_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_operation" not in self._stubs: - self._stubs["delete_operation"] = self.grpc_channel.unary_unary( + self._stubs["delete_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/DeleteOperation", request_serializer=operations_pb2.DeleteOperationRequest.SerializeToString, response_deserializer=None, @@ -383,7 +928,7 @@ def cancel_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "cancel_operation" not in self._stubs: - self._stubs["cancel_operation"] = self.grpc_channel.unary_unary( + self._stubs["cancel_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/CancelOperation", request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, response_deserializer=None, @@ -400,7 +945,7 @@ def get_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_operation" not in self._stubs: - self._stubs["get_operation"] = self.grpc_channel.unary_unary( + self._stubs["get_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/GetOperation", request_serializer=operations_pb2.GetOperationRequest.SerializeToString, response_deserializer=operations_pb2.Operation.FromString, @@ -419,7 +964,7 @@ def list_operations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_operations" not in self._stubs: - self._stubs["list_operations"] = self.grpc_channel.unary_unary( + self._stubs["list_operations"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/ListOperations", request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, response_deserializer=operations_pb2.ListOperationsResponse.FromString, @@ -438,7 +983,7 @@ def list_locations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_locations" not in self._stubs: - self._stubs["list_locations"] = self.grpc_channel.unary_unary( + self._stubs["list_locations"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/ListLocations", request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, response_deserializer=locations_pb2.ListLocationsResponse.FromString, @@ -455,7 +1000,7 @@ def get_location( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_location" not in self._stubs: - self._stubs["get_location"] = self.grpc_channel.unary_unary( + self._stubs["get_location"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/GetLocation", request_serializer=locations_pb2.GetLocationRequest.SerializeToString, response_deserializer=locations_pb2.Location.FromString, diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/__init__.py index d73fc8a8fedd..6bb9671974f6 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/__init__.py @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from .async_client import HostProjectRegistrationServiceAsyncClient from .client import HostProjectRegistrationServiceClient -__all__ = ("HostProjectRegistrationServiceClient",) +__all__ = ( + "HostProjectRegistrationServiceClient", + "HostProjectRegistrationServiceAsyncClient", +) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/async_client.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/async_client.py index be906eaa63df..a0ed565e4d54 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/async_client.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/async_client.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,6 +14,7 @@ # limitations under the License. # from collections import OrderedDict +import logging as std_logging import re from typing import ( Callable, @@ -34,6 +35,7 @@ from google.api_core.client_options import ClientOptions from google.auth import credentials as ga_credentials # type: ignore from google.oauth2 import service_account # type: ignore +import google.protobuf from google.cloud.apihub_v1 import gapic_version as package_version @@ -56,6 +58,15 @@ ) from .transports.grpc_asyncio import HostProjectRegistrationServiceGrpcAsyncIOTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + class HostProjectRegistrationServiceAsyncClient: """This service is used for managing the host project @@ -279,6 +290,28 @@ def __init__( client_info=client_info, ) + if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ): # pragma: NO COVER + _LOGGER.debug( + "Created client `google.cloud.apihub_v1.HostProjectRegistrationServiceAsyncClient`.", + extra={ + "serviceName": "google.cloud.apihub.v1.HostProjectRegistrationService", + "universeDomain": getattr( + self._client._transport._credentials, "universe_domain", "" + ), + "credentialsType": f"{type(self._client._transport._credentials).__module__}.{type(self._client._transport._credentials).__qualname__}", + "credentialsInfo": getattr( + self.transport._credentials, "get_cred_info", lambda: None + )(), + } + if hasattr(self._client._transport, "_credentials") + else { + "serviceName": "google.cloud.apihub.v1.HostProjectRegistrationService", + "credentialsType": None, + }, + ) + async def create_host_project_registration( self, request: Optional[ @@ -295,7 +328,7 @@ async def create_host_project_registration( host_project_registration_id: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> host_project_registration_service.HostProjectRegistration: r"""Create a host project registration. A Google cloud project can be registered as a host @@ -367,8 +400,10 @@ async def sample_create_host_project_registration(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.HostProjectRegistration: @@ -387,8 +422,13 @@ async def sample_create_host_project_registration(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any( - [parent, host_project_registration, host_project_registration_id] + flattened_params = [ + parent, + host_project_registration, + host_project_registration_id, + ] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 ) if request is not None and has_flattened_params: raise ValueError( @@ -455,7 +495,7 @@ async def get_host_project_registration( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> host_project_registration_service.HostProjectRegistration: r"""Get a host project registration. @@ -500,8 +540,10 @@ async def sample_get_host_project_registration(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.HostProjectRegistration: @@ -520,7 +562,10 @@ async def sample_get_host_project_registration(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -581,7 +626,7 @@ async def list_host_project_registrations( parent: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> pagers.ListHostProjectRegistrationsAsyncPager: r"""Lists host project registrations. @@ -627,8 +672,10 @@ async def sample_list_host_project_registrations(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.services.host_project_registration_service.pagers.ListHostProjectRegistrationsAsyncPager: @@ -643,7 +690,10 @@ async def sample_list_host_project_registrations(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([parent]) + flattened_params = [parent] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -710,7 +760,7 @@ async def list_operations( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> operations_pb2.ListOperationsResponse: r"""Lists operations that match the specified filter in the request. @@ -721,8 +771,10 @@ async def list_operations( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.operations_pb2.ListOperationsResponse: Response message for ``ListOperations`` method. @@ -735,11 +787,7 @@ async def list_operations( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.list_operations, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.list_operations] # Certain fields should be provided within the metadata header; # add these here. @@ -767,7 +815,7 @@ async def get_operation( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> operations_pb2.Operation: r"""Gets the latest state of a long-running operation. @@ -778,8 +826,10 @@ async def get_operation( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.operations_pb2.Operation: An ``Operation`` object. @@ -792,11 +842,7 @@ async def get_operation( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.get_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.get_operation] # Certain fields should be provided within the metadata header; # add these here. @@ -824,7 +870,7 @@ async def delete_operation( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Deletes a long-running operation. @@ -840,8 +886,10 @@ async def delete_operation( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: None """ @@ -853,11 +901,7 @@ async def delete_operation( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.delete_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.delete_operation] # Certain fields should be provided within the metadata header; # add these here. @@ -882,7 +926,7 @@ async def cancel_operation( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Starts asynchronous cancellation on a long-running operation. @@ -897,8 +941,10 @@ async def cancel_operation( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: None """ @@ -910,11 +956,7 @@ async def cancel_operation( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.cancel_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.cancel_operation] # Certain fields should be provided within the metadata header; # add these here. @@ -939,7 +981,7 @@ async def get_location( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> locations_pb2.Location: r"""Gets information about a location. @@ -950,8 +992,10 @@ async def get_location( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.location_pb2.Location: Location object. @@ -964,11 +1008,7 @@ async def get_location( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.get_location, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.get_location] # Certain fields should be provided within the metadata header; # add these here. @@ -996,7 +1036,7 @@ async def list_locations( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> locations_pb2.ListLocationsResponse: r"""Lists information about the supported locations for this service. @@ -1007,8 +1047,10 @@ async def list_locations( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.location_pb2.ListLocationsResponse: Response message for ``ListLocations`` method. @@ -1021,11 +1063,7 @@ async def list_locations( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.list_locations, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.list_locations] # Certain fields should be provided within the metadata header; # add these here. @@ -1058,5 +1096,8 @@ async def __aexit__(self, exc_type, exc, tb): gapic_version=package_version.__version__ ) +if hasattr(DEFAULT_CLIENT_INFO, "protobuf_runtime_version"): # pragma: NO COVER + DEFAULT_CLIENT_INFO.protobuf_runtime_version = google.protobuf.__version__ + __all__ = ("HostProjectRegistrationServiceAsyncClient",) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/client.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/client.py index e4885c37d077..0bf1eac1d3e3 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/client.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/client.py @@ -72,6 +72,8 @@ DEFAULT_CLIENT_INFO, HostProjectRegistrationServiceTransport, ) +from .transports.grpc import HostProjectRegistrationServiceGrpcTransport +from .transports.grpc_asyncio import HostProjectRegistrationServiceGrpcAsyncIOTransport from .transports.rest import HostProjectRegistrationServiceRestTransport @@ -86,6 +88,10 @@ class HostProjectRegistrationServiceClientMeta(type): _transport_registry = ( OrderedDict() ) # type: Dict[str, Type[HostProjectRegistrationServiceTransport]] + _transport_registry["grpc"] = HostProjectRegistrationServiceGrpcTransport + _transport_registry[ + "grpc_asyncio" + ] = HostProjectRegistrationServiceGrpcAsyncIOTransport _transport_registry["rest"] = HostProjectRegistrationServiceRestTransport def get_transport_class( diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/pagers.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/pagers.py index 1addfa65fcb3..42a7a42b645f 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/pagers.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/pagers.py @@ -127,3 +127,98 @@ def __iter__( def __repr__(self) -> str: return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListHostProjectRegistrationsAsyncPager: + """A pager for iterating through ``list_host_project_registrations`` requests. + + This class thinly wraps an initial + :class:`google.cloud.apihub_v1.types.ListHostProjectRegistrationsResponse` object, and + provides an ``__aiter__`` method to iterate through its + ``host_project_registrations`` field. + + If there are more pages, the ``__aiter__`` method will make additional + ``ListHostProjectRegistrations`` requests and continue to iterate + through the ``host_project_registrations`` field on the + corresponding responses. + + All the usual :class:`google.cloud.apihub_v1.types.ListHostProjectRegistrationsResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[ + ..., + Awaitable[ + host_project_registration_service.ListHostProjectRegistrationsResponse + ], + ], + request: host_project_registration_service.ListHostProjectRegistrationsRequest, + response: host_project_registration_service.ListHostProjectRegistrationsResponse, + *, + retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = () + ): + """Instantiates the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.apihub_v1.types.ListHostProjectRegistrationsRequest): + The initial request object. + response (google.cloud.apihub_v1.types.ListHostProjectRegistrationsResponse): + The initial response object. + retry (google.api_core.retry.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + """ + self._method = method + self._request = ( + host_project_registration_service.ListHostProjectRegistrationsRequest( + request + ) + ) + self._response = response + self._retry = retry + self._timeout = timeout + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + async def pages( + self, + ) -> AsyncIterator[ + host_project_registration_service.ListHostProjectRegistrationsResponse + ]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = await self._method( + self._request, + retry=self._retry, + timeout=self._timeout, + metadata=self._metadata, + ) + yield self._response + + def __aiter__( + self, + ) -> AsyncIterator[host_project_registration_service.HostProjectRegistration]: + async def async_generator(): + async for page in self.pages: + for response in page.host_project_registrations: + yield response + + return async_generator() + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/transports/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/transports/__init__.py index 71137a7839d5..312bae57ee53 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/transports/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/transports/__init__.py @@ -17,6 +17,8 @@ from typing import Dict, Type from .base import HostProjectRegistrationServiceTransport +from .grpc import HostProjectRegistrationServiceGrpcTransport +from .grpc_asyncio import HostProjectRegistrationServiceGrpcAsyncIOTransport from .rest import ( HostProjectRegistrationServiceRestInterceptor, HostProjectRegistrationServiceRestTransport, @@ -26,10 +28,14 @@ _transport_registry = ( OrderedDict() ) # type: Dict[str, Type[HostProjectRegistrationServiceTransport]] +_transport_registry["grpc"] = HostProjectRegistrationServiceGrpcTransport +_transport_registry["grpc_asyncio"] = HostProjectRegistrationServiceGrpcAsyncIOTransport _transport_registry["rest"] = HostProjectRegistrationServiceRestTransport __all__ = ( "HostProjectRegistrationServiceTransport", + "HostProjectRegistrationServiceGrpcTransport", + "HostProjectRegistrationServiceGrpcAsyncIOTransport", "HostProjectRegistrationServiceRestTransport", "HostProjectRegistrationServiceRestInterceptor", ) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/transports/grpc.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/transports/grpc.py index 5ea890aefe8e..038f75386490 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/transports/grpc.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/transports/grpc.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,6 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import json +import logging as std_logging +import pickle from typing import Callable, Dict, Optional, Sequence, Tuple, Union import warnings @@ -22,12 +25,89 @@ from google.auth.transport.grpc import SslCredentials # type: ignore from google.cloud.location import locations_pb2 # type: ignore from google.longrunning import operations_pb2 # type: ignore +from google.protobuf.json_format import MessageToJson +import google.protobuf.message import grpc # type: ignore +import proto # type: ignore from google.cloud.apihub_v1.types import host_project_registration_service from .base import DEFAULT_CLIENT_INFO, HostProjectRegistrationServiceTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class _LoggingClientInterceptor(grpc.UnaryUnaryClientInterceptor): # pragma: NO COVER + def intercept_unary_unary(self, continuation, client_call_details, request): + logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ) + if logging_enabled: # pragma: NO COVER + request_metadata = client_call_details.metadata + if isinstance(request, proto.Message): + request_payload = type(request).to_json(request) + elif isinstance(request, google.protobuf.message.Message): + request_payload = MessageToJson(request) + else: + request_payload = f"{type(request).__name__}: {pickle.dumps(request)}" + + request_metadata = { + key: value.decode("utf-8") if isinstance(value, bytes) else value + for key, value in request_metadata + } + grpc_request = { + "payload": request_payload, + "requestMethod": "grpc", + "metadata": dict(request_metadata), + } + _LOGGER.debug( + f"Sending request for {client_call_details.method}", + extra={ + "serviceName": "google.cloud.apihub.v1.HostProjectRegistrationService", + "rpcName": str(client_call_details.method), + "request": grpc_request, + "metadata": grpc_request["metadata"], + }, + ) + response = continuation(client_call_details, request) + if logging_enabled: # pragma: NO COVER + response_metadata = response.trailing_metadata() + # Convert gRPC metadata `` to list of tuples + metadata = ( + dict([(k, str(v)) for k, v in response_metadata]) + if response_metadata + else None + ) + result = response.result() + if isinstance(result, proto.Message): + response_payload = type(result).to_json(result) + elif isinstance(result, google.protobuf.message.Message): + response_payload = MessageToJson(result) + else: + response_payload = f"{type(result).__name__}: {pickle.dumps(result)}" + grpc_response = { + "payload": response_payload, + "metadata": metadata, + "status": "OK", + } + _LOGGER.debug( + f"Received response for {client_call_details.method}.", + extra={ + "serviceName": "google.cloud.apihub.v1.HostProjectRegistrationService", + "rpcName": client_call_details.method, + "response": grpc_response, + "metadata": grpc_response["metadata"], + }, + ) + return response + class HostProjectRegistrationServiceGrpcTransport( HostProjectRegistrationServiceTransport @@ -184,7 +264,12 @@ def __init__( ], ) - # Wrap messages. This must be done after self._grpc_channel exists + self._interceptor = _LoggingClientInterceptor() + self._logged_channel = grpc.intercept_channel( + self._grpc_channel, self._interceptor + ) + + # Wrap messages. This must be done after self._logged_channel exists self._prep_wrapped_messages(client_info) @classmethod @@ -269,7 +354,7 @@ def create_host_project_registration( if "create_host_project_registration" not in self._stubs: self._stubs[ "create_host_project_registration" - ] = self.grpc_channel.unary_unary( + ] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.HostProjectRegistrationService/CreateHostProjectRegistration", request_serializer=host_project_registration_service.CreateHostProjectRegistrationRequest.serialize, response_deserializer=host_project_registration_service.HostProjectRegistration.deserialize, @@ -300,7 +385,7 @@ def get_host_project_registration( if "get_host_project_registration" not in self._stubs: self._stubs[ "get_host_project_registration" - ] = self.grpc_channel.unary_unary( + ] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.HostProjectRegistrationService/GetHostProjectRegistration", request_serializer=host_project_registration_service.GetHostProjectRegistrationRequest.serialize, response_deserializer=host_project_registration_service.HostProjectRegistration.deserialize, @@ -332,7 +417,7 @@ def list_host_project_registrations( if "list_host_project_registrations" not in self._stubs: self._stubs[ "list_host_project_registrations" - ] = self.grpc_channel.unary_unary( + ] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.HostProjectRegistrationService/ListHostProjectRegistrations", request_serializer=host_project_registration_service.ListHostProjectRegistrationsRequest.serialize, response_deserializer=host_project_registration_service.ListHostProjectRegistrationsResponse.deserialize, @@ -340,7 +425,7 @@ def list_host_project_registrations( return self._stubs["list_host_project_registrations"] def close(self): - self.grpc_channel.close() + self._logged_channel.close() @property def delete_operation( @@ -352,7 +437,7 @@ def delete_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_operation" not in self._stubs: - self._stubs["delete_operation"] = self.grpc_channel.unary_unary( + self._stubs["delete_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/DeleteOperation", request_serializer=operations_pb2.DeleteOperationRequest.SerializeToString, response_deserializer=None, @@ -369,7 +454,7 @@ def cancel_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "cancel_operation" not in self._stubs: - self._stubs["cancel_operation"] = self.grpc_channel.unary_unary( + self._stubs["cancel_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/CancelOperation", request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, response_deserializer=None, @@ -386,7 +471,7 @@ def get_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_operation" not in self._stubs: - self._stubs["get_operation"] = self.grpc_channel.unary_unary( + self._stubs["get_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/GetOperation", request_serializer=operations_pb2.GetOperationRequest.SerializeToString, response_deserializer=operations_pb2.Operation.FromString, @@ -405,7 +490,7 @@ def list_operations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_operations" not in self._stubs: - self._stubs["list_operations"] = self.grpc_channel.unary_unary( + self._stubs["list_operations"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/ListOperations", request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, response_deserializer=operations_pb2.ListOperationsResponse.FromString, @@ -424,7 +509,7 @@ def list_locations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_locations" not in self._stubs: - self._stubs["list_locations"] = self.grpc_channel.unary_unary( + self._stubs["list_locations"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/ListLocations", request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, response_deserializer=locations_pb2.ListLocationsResponse.FromString, @@ -441,7 +526,7 @@ def get_location( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_location" not in self._stubs: - self._stubs["get_location"] = self.grpc_channel.unary_unary( + self._stubs["get_location"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/GetLocation", request_serializer=locations_pb2.GetLocationRequest.SerializeToString, response_deserializer=locations_pb2.Location.FromString, diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/transports/grpc_asyncio.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/transports/grpc_asyncio.py index 31680c310ff8..997c9eaef06c 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/transports/grpc_asyncio.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/host_project_registration_service/transports/grpc_asyncio.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import inspect +import json +import logging as std_logging +import pickle from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union import warnings @@ -23,14 +27,93 @@ from google.auth.transport.grpc import SslCredentials # type: ignore from google.cloud.location import locations_pb2 # type: ignore from google.longrunning import operations_pb2 # type: ignore +from google.protobuf.json_format import MessageToJson +import google.protobuf.message import grpc # type: ignore from grpc.experimental import aio # type: ignore +import proto # type: ignore from google.cloud.apihub_v1.types import host_project_registration_service from .base import DEFAULT_CLIENT_INFO, HostProjectRegistrationServiceTransport from .grpc import HostProjectRegistrationServiceGrpcTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class _LoggingClientAIOInterceptor( + grpc.aio.UnaryUnaryClientInterceptor +): # pragma: NO COVER + async def intercept_unary_unary(self, continuation, client_call_details, request): + logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ) + if logging_enabled: # pragma: NO COVER + request_metadata = client_call_details.metadata + if isinstance(request, proto.Message): + request_payload = type(request).to_json(request) + elif isinstance(request, google.protobuf.message.Message): + request_payload = MessageToJson(request) + else: + request_payload = f"{type(request).__name__}: {pickle.dumps(request)}" + + request_metadata = { + key: value.decode("utf-8") if isinstance(value, bytes) else value + for key, value in request_metadata + } + grpc_request = { + "payload": request_payload, + "requestMethod": "grpc", + "metadata": dict(request_metadata), + } + _LOGGER.debug( + f"Sending request for {client_call_details.method}", + extra={ + "serviceName": "google.cloud.apihub.v1.HostProjectRegistrationService", + "rpcName": str(client_call_details.method), + "request": grpc_request, + "metadata": grpc_request["metadata"], + }, + ) + response = await continuation(client_call_details, request) + if logging_enabled: # pragma: NO COVER + response_metadata = await response.trailing_metadata() + # Convert gRPC metadata `` to list of tuples + metadata = ( + dict([(k, str(v)) for k, v in response_metadata]) + if response_metadata + else None + ) + result = await response + if isinstance(result, proto.Message): + response_payload = type(result).to_json(result) + elif isinstance(result, google.protobuf.message.Message): + response_payload = MessageToJson(result) + else: + response_payload = f"{type(result).__name__}: {pickle.dumps(result)}" + grpc_response = { + "payload": response_payload, + "metadata": metadata, + "status": "OK", + } + _LOGGER.debug( + f"Received response to rpc {client_call_details.method}.", + extra={ + "serviceName": "google.cloud.apihub.v1.HostProjectRegistrationService", + "rpcName": str(client_call_details.method), + "response": grpc_response, + "metadata": grpc_response["metadata"], + }, + ) + return response + class HostProjectRegistrationServiceGrpcAsyncIOTransport( HostProjectRegistrationServiceTransport @@ -230,7 +313,13 @@ def __init__( ], ) - # Wrap messages. This must be done after self._grpc_channel exists + self._interceptor = _LoggingClientAIOInterceptor() + self._grpc_channel._unary_unary_interceptors.append(self._interceptor) + self._logged_channel = self._grpc_channel + self._wrap_with_kind = ( + "kind" in inspect.signature(gapic_v1.method_async.wrap_method).parameters + ) + # Wrap messages. This must be done after self._logged_channel exists self._prep_wrapped_messages(client_info) @property @@ -273,7 +362,7 @@ def create_host_project_registration( if "create_host_project_registration" not in self._stubs: self._stubs[ "create_host_project_registration" - ] = self.grpc_channel.unary_unary( + ] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.HostProjectRegistrationService/CreateHostProjectRegistration", request_serializer=host_project_registration_service.CreateHostProjectRegistrationRequest.serialize, response_deserializer=host_project_registration_service.HostProjectRegistration.deserialize, @@ -304,7 +393,7 @@ def get_host_project_registration( if "get_host_project_registration" not in self._stubs: self._stubs[ "get_host_project_registration" - ] = self.grpc_channel.unary_unary( + ] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.HostProjectRegistrationService/GetHostProjectRegistration", request_serializer=host_project_registration_service.GetHostProjectRegistrationRequest.serialize, response_deserializer=host_project_registration_service.HostProjectRegistration.deserialize, @@ -338,7 +427,7 @@ def list_host_project_registrations( if "list_host_project_registrations" not in self._stubs: self._stubs[ "list_host_project_registrations" - ] = self.grpc_channel.unary_unary( + ] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.HostProjectRegistrationService/ListHostProjectRegistrations", request_serializer=host_project_registration_service.ListHostProjectRegistrationsRequest.serialize, response_deserializer=host_project_registration_service.ListHostProjectRegistrationsResponse.deserialize, @@ -348,12 +437,12 @@ def list_host_project_registrations( def _prep_wrapped_messages(self, client_info): """Precompute the wrapped methods, overriding the base class method to use async wrappers.""" self._wrapped_methods = { - self.create_host_project_registration: gapic_v1.method_async.wrap_method( + self.create_host_project_registration: self._wrap_method( self.create_host_project_registration, default_timeout=60.0, client_info=client_info, ), - self.get_host_project_registration: gapic_v1.method_async.wrap_method( + self.get_host_project_registration: self._wrap_method( self.get_host_project_registration, default_retry=retries.AsyncRetry( initial=1.0, @@ -367,7 +456,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.list_host_project_registrations: gapic_v1.method_async.wrap_method( + self.list_host_project_registrations: self._wrap_method( self.list_host_project_registrations, default_retry=retries.AsyncRetry( initial=1.0, @@ -381,10 +470,49 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), + self.get_location: self._wrap_method( + self.get_location, + default_timeout=None, + client_info=client_info, + ), + self.list_locations: self._wrap_method( + self.list_locations, + default_timeout=None, + client_info=client_info, + ), + self.cancel_operation: self._wrap_method( + self.cancel_operation, + default_timeout=None, + client_info=client_info, + ), + self.delete_operation: self._wrap_method( + self.delete_operation, + default_timeout=None, + client_info=client_info, + ), + self.get_operation: self._wrap_method( + self.get_operation, + default_timeout=None, + client_info=client_info, + ), + self.list_operations: self._wrap_method( + self.list_operations, + default_timeout=None, + client_info=client_info, + ), } + def _wrap_method(self, func, *args, **kwargs): + if self._wrap_with_kind: # pragma: NO COVER + kwargs["kind"] = self.kind + return gapic_v1.method_async.wrap_method(func, *args, **kwargs) + def close(self): - return self.grpc_channel.close() + return self._logged_channel.close() + + @property + def kind(self) -> str: + return "grpc_asyncio" @property def delete_operation( @@ -396,7 +524,7 @@ def delete_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_operation" not in self._stubs: - self._stubs["delete_operation"] = self.grpc_channel.unary_unary( + self._stubs["delete_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/DeleteOperation", request_serializer=operations_pb2.DeleteOperationRequest.SerializeToString, response_deserializer=None, @@ -413,7 +541,7 @@ def cancel_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "cancel_operation" not in self._stubs: - self._stubs["cancel_operation"] = self.grpc_channel.unary_unary( + self._stubs["cancel_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/CancelOperation", request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, response_deserializer=None, @@ -430,7 +558,7 @@ def get_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_operation" not in self._stubs: - self._stubs["get_operation"] = self.grpc_channel.unary_unary( + self._stubs["get_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/GetOperation", request_serializer=operations_pb2.GetOperationRequest.SerializeToString, response_deserializer=operations_pb2.Operation.FromString, @@ -449,7 +577,7 @@ def list_operations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_operations" not in self._stubs: - self._stubs["list_operations"] = self.grpc_channel.unary_unary( + self._stubs["list_operations"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/ListOperations", request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, response_deserializer=operations_pb2.ListOperationsResponse.FromString, @@ -468,7 +596,7 @@ def list_locations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_locations" not in self._stubs: - self._stubs["list_locations"] = self.grpc_channel.unary_unary( + self._stubs["list_locations"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/ListLocations", request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, response_deserializer=locations_pb2.ListLocationsResponse.FromString, @@ -485,7 +613,7 @@ def get_location( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_location" not in self._stubs: - self._stubs["get_location"] = self.grpc_channel.unary_unary( + self._stubs["get_location"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/GetLocation", request_serializer=locations_pb2.GetLocationRequest.SerializeToString, response_deserializer=locations_pb2.Location.FromString, diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/__init__.py index 5427eabcfdbe..d2e2dfc4bfa6 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/__init__.py @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from .async_client import LintingServiceAsyncClient from .client import LintingServiceClient -__all__ = ("LintingServiceClient",) +__all__ = ( + "LintingServiceClient", + "LintingServiceAsyncClient", +) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/async_client.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/async_client.py index e0a1abb6ea97..c5c17bc23f5d 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/async_client.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/async_client.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,6 +14,7 @@ # limitations under the License. # from collections import OrderedDict +import logging as std_logging import re from typing import ( Callable, @@ -34,6 +35,7 @@ from google.api_core.client_options import ClientOptions from google.auth import credentials as ga_credentials # type: ignore from google.oauth2 import service_account # type: ignore +import google.protobuf from google.cloud.apihub_v1 import gapic_version as package_version @@ -52,6 +54,15 @@ from .transports.base import DEFAULT_CLIENT_INFO, LintingServiceTransport from .transports.grpc_asyncio import LintingServiceGrpcAsyncIOTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + class LintingServiceAsyncClient: """This service provides all methods related to the 1p Linter.""" @@ -259,6 +270,28 @@ def __init__( client_info=client_info, ) + if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ): # pragma: NO COVER + _LOGGER.debug( + "Created client `google.cloud.apihub_v1.LintingServiceAsyncClient`.", + extra={ + "serviceName": "google.cloud.apihub.v1.LintingService", + "universeDomain": getattr( + self._client._transport._credentials, "universe_domain", "" + ), + "credentialsType": f"{type(self._client._transport._credentials).__module__}.{type(self._client._transport._credentials).__qualname__}", + "credentialsInfo": getattr( + self.transport._credentials, "get_cred_info", lambda: None + )(), + } + if hasattr(self._client._transport, "_credentials") + else { + "serviceName": "google.cloud.apihub.v1.LintingService", + "credentialsType": None, + }, + ) + async def get_style_guide( self, request: Optional[Union[linting_service.GetStyleGuideRequest, dict]] = None, @@ -266,7 +299,7 @@ async def get_style_guide( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> linting_service.StyleGuide: r"""Get the style guide being used for linting. @@ -298,8 +331,9 @@ async def sample_get_style_guide(): Args: request (Optional[Union[google.cloud.apihub_v1.types.GetStyleGuideRequest, dict]]): - The request object. The [GetStyleGuide][ApiHub.GetStyleGuide] method's - request. + The request object. The + [GetStyleGuide][google.cloud.apihub.v1.LintingService.GetStyleGuide] + method's request. name (:class:`str`): Required. The name of the spec to retrieve. Format: ``projects/{project}/locations/{location}/plugins/{plugin}/styleGuide``. @@ -310,8 +344,10 @@ async def sample_get_style_guide(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.StyleGuide: @@ -323,7 +359,10 @@ async def sample_get_style_guide(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -374,7 +413,7 @@ async def update_style_guide( update_mask: Optional[field_mask_pb2.FieldMask] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> linting_service.StyleGuide: r"""Update the styleGuide to be used for liniting in by API hub. @@ -412,8 +451,9 @@ async def sample_update_style_guide(): Args: request (Optional[Union[google.cloud.apihub_v1.types.UpdateStyleGuideRequest, dict]]): - The request object. The [UpdateStyleGuide][ApiHub.UpdateStyleGuide] method's - request. + The request object. The + [UpdateStyleGuide][google.cloud.apihub.v1.LintingService.UpdateStyleGuide] + method's request. style_guide (:class:`google.cloud.apihub_v1.types.StyleGuide`): Required. The Style guide resource to update. @@ -431,8 +471,10 @@ async def sample_update_style_guide(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.StyleGuide: @@ -444,7 +486,10 @@ async def sample_update_style_guide(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([style_guide, update_mask]) + flattened_params = [style_guide, update_mask] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -500,7 +545,7 @@ async def get_style_guide_contents( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> linting_service.StyleGuideContents: r"""Get the contents of the style guide. @@ -533,7 +578,7 @@ async def sample_get_style_guide_contents(): Args: request (Optional[Union[google.cloud.apihub_v1.types.GetStyleGuideContentsRequest, dict]]): The request object. The - [GetStyleGuideContents][ApiHub.GetStyleGuideContents] + [GetStyleGuideContents][google.cloud.apihub.v1.LintingService.GetStyleGuideContents] method's request. name (:class:`str`): Required. The name of the StyleGuide whose contents need @@ -548,8 +593,10 @@ async def sample_get_style_guide_contents(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.StyleGuideContents: @@ -558,7 +605,10 @@ async def sample_get_style_guide_contents(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -607,7 +657,7 @@ async def lint_spec( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Lints the requested spec and updates the corresponding API Spec with the lint response. This lint @@ -639,12 +689,16 @@ async def sample_lint_spec(): Args: request (Optional[Union[google.cloud.apihub_v1.types.LintSpecRequest, dict]]): - The request object. The [LintSpec][ApiHub.LintSpec] method's request. + The request object. The + [LintSpec][google.cloud.apihub.v1.LintingService.LintSpec] + method's request. retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. """ # Create or coerce a protobuf request object. # - Use the request object if provided (there's no risk of modifying the input as @@ -681,7 +735,7 @@ async def list_operations( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> operations_pb2.ListOperationsResponse: r"""Lists operations that match the specified filter in the request. @@ -692,8 +746,10 @@ async def list_operations( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.operations_pb2.ListOperationsResponse: Response message for ``ListOperations`` method. @@ -706,11 +762,7 @@ async def list_operations( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.list_operations, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.list_operations] # Certain fields should be provided within the metadata header; # add these here. @@ -738,7 +790,7 @@ async def get_operation( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> operations_pb2.Operation: r"""Gets the latest state of a long-running operation. @@ -749,8 +801,10 @@ async def get_operation( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.operations_pb2.Operation: An ``Operation`` object. @@ -763,11 +817,7 @@ async def get_operation( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.get_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.get_operation] # Certain fields should be provided within the metadata header; # add these here. @@ -795,7 +845,7 @@ async def delete_operation( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Deletes a long-running operation. @@ -811,8 +861,10 @@ async def delete_operation( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: None """ @@ -824,11 +876,7 @@ async def delete_operation( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.delete_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.delete_operation] # Certain fields should be provided within the metadata header; # add these here. @@ -853,7 +901,7 @@ async def cancel_operation( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Starts asynchronous cancellation on a long-running operation. @@ -868,8 +916,10 @@ async def cancel_operation( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: None """ @@ -881,11 +931,7 @@ async def cancel_operation( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.cancel_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.cancel_operation] # Certain fields should be provided within the metadata header; # add these here. @@ -910,7 +956,7 @@ async def get_location( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> locations_pb2.Location: r"""Gets information about a location. @@ -921,8 +967,10 @@ async def get_location( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.location_pb2.Location: Location object. @@ -935,11 +983,7 @@ async def get_location( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.get_location, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.get_location] # Certain fields should be provided within the metadata header; # add these here. @@ -967,7 +1011,7 @@ async def list_locations( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> locations_pb2.ListLocationsResponse: r"""Lists information about the supported locations for this service. @@ -978,8 +1022,10 @@ async def list_locations( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.location_pb2.ListLocationsResponse: Response message for ``ListLocations`` method. @@ -992,11 +1038,7 @@ async def list_locations( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.list_locations, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.list_locations] # Certain fields should be provided within the metadata header; # add these here. @@ -1029,5 +1071,8 @@ async def __aexit__(self, exc_type, exc, tb): gapic_version=package_version.__version__ ) +if hasattr(DEFAULT_CLIENT_INFO, "protobuf_runtime_version"): # pragma: NO COVER + DEFAULT_CLIENT_INFO.protobuf_runtime_version = google.protobuf.__version__ + __all__ = ("LintingServiceAsyncClient",) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/client.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/client.py index c20fcdb7012b..45ff13b8e303 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/client.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/client.py @@ -68,6 +68,8 @@ from google.cloud.apihub_v1.types import common_fields, linting_service from .transports.base import DEFAULT_CLIENT_INFO, LintingServiceTransport +from .transports.grpc import LintingServiceGrpcTransport +from .transports.grpc_asyncio import LintingServiceGrpcAsyncIOTransport from .transports.rest import LintingServiceRestTransport @@ -82,6 +84,8 @@ class LintingServiceClientMeta(type): _transport_registry = ( OrderedDict() ) # type: Dict[str, Type[LintingServiceTransport]] + _transport_registry["grpc"] = LintingServiceGrpcTransport + _transport_registry["grpc_asyncio"] = LintingServiceGrpcAsyncIOTransport _transport_registry["rest"] = LintingServiceRestTransport def get_transport_class( diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/transports/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/transports/__init__.py index 7f6fad921c3d..90ba0e94f13f 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/transports/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/transports/__init__.py @@ -17,14 +17,20 @@ from typing import Dict, Type from .base import LintingServiceTransport +from .grpc import LintingServiceGrpcTransport +from .grpc_asyncio import LintingServiceGrpcAsyncIOTransport from .rest import LintingServiceRestInterceptor, LintingServiceRestTransport # Compile a registry of transports. _transport_registry = OrderedDict() # type: Dict[str, Type[LintingServiceTransport]] +_transport_registry["grpc"] = LintingServiceGrpcTransport +_transport_registry["grpc_asyncio"] = LintingServiceGrpcAsyncIOTransport _transport_registry["rest"] = LintingServiceRestTransport __all__ = ( "LintingServiceTransport", + "LintingServiceGrpcTransport", + "LintingServiceGrpcAsyncIOTransport", "LintingServiceRestTransport", "LintingServiceRestInterceptor", ) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/transports/grpc.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/transports/grpc.py index 1096e78975f6..c89a762a743c 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/transports/grpc.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/transports/grpc.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,6 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import json +import logging as std_logging +import pickle from typing import Callable, Dict, Optional, Sequence, Tuple, Union import warnings @@ -23,12 +26,89 @@ from google.cloud.location import locations_pb2 # type: ignore from google.longrunning import operations_pb2 # type: ignore from google.protobuf import empty_pb2 # type: ignore +from google.protobuf.json_format import MessageToJson +import google.protobuf.message import grpc # type: ignore +import proto # type: ignore from google.cloud.apihub_v1.types import linting_service from .base import DEFAULT_CLIENT_INFO, LintingServiceTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class _LoggingClientInterceptor(grpc.UnaryUnaryClientInterceptor): # pragma: NO COVER + def intercept_unary_unary(self, continuation, client_call_details, request): + logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ) + if logging_enabled: # pragma: NO COVER + request_metadata = client_call_details.metadata + if isinstance(request, proto.Message): + request_payload = type(request).to_json(request) + elif isinstance(request, google.protobuf.message.Message): + request_payload = MessageToJson(request) + else: + request_payload = f"{type(request).__name__}: {pickle.dumps(request)}" + + request_metadata = { + key: value.decode("utf-8") if isinstance(value, bytes) else value + for key, value in request_metadata + } + grpc_request = { + "payload": request_payload, + "requestMethod": "grpc", + "metadata": dict(request_metadata), + } + _LOGGER.debug( + f"Sending request for {client_call_details.method}", + extra={ + "serviceName": "google.cloud.apihub.v1.LintingService", + "rpcName": str(client_call_details.method), + "request": grpc_request, + "metadata": grpc_request["metadata"], + }, + ) + response = continuation(client_call_details, request) + if logging_enabled: # pragma: NO COVER + response_metadata = response.trailing_metadata() + # Convert gRPC metadata `` to list of tuples + metadata = ( + dict([(k, str(v)) for k, v in response_metadata]) + if response_metadata + else None + ) + result = response.result() + if isinstance(result, proto.Message): + response_payload = type(result).to_json(result) + elif isinstance(result, google.protobuf.message.Message): + response_payload = MessageToJson(result) + else: + response_payload = f"{type(result).__name__}: {pickle.dumps(result)}" + grpc_response = { + "payload": response_payload, + "metadata": metadata, + "status": "OK", + } + _LOGGER.debug( + f"Received response for {client_call_details.method}.", + extra={ + "serviceName": "google.cloud.apihub.v1.LintingService", + "rpcName": client_call_details.method, + "response": grpc_response, + "metadata": grpc_response["metadata"], + }, + ) + return response + class LintingServiceGrpcTransport(LintingServiceTransport): """gRPC backend transport for LintingService. @@ -182,7 +262,12 @@ def __init__( ], ) - # Wrap messages. This must be done after self._grpc_channel exists + self._interceptor = _LoggingClientInterceptor() + self._logged_channel = grpc.intercept_channel( + self._grpc_channel, self._interceptor + ) + + # Wrap messages. This must be done after self._logged_channel exists self._prep_wrapped_messages(client_info) @classmethod @@ -256,7 +341,7 @@ def get_style_guide( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_style_guide" not in self._stubs: - self._stubs["get_style_guide"] = self.grpc_channel.unary_unary( + self._stubs["get_style_guide"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.LintingService/GetStyleGuide", request_serializer=linting_service.GetStyleGuideRequest.serialize, response_deserializer=linting_service.StyleGuide.deserialize, @@ -285,7 +370,7 @@ def update_style_guide( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "update_style_guide" not in self._stubs: - self._stubs["update_style_guide"] = self.grpc_channel.unary_unary( + self._stubs["update_style_guide"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.LintingService/UpdateStyleGuide", request_serializer=linting_service.UpdateStyleGuideRequest.serialize, response_deserializer=linting_service.StyleGuide.deserialize, @@ -314,7 +399,7 @@ def get_style_guide_contents( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_style_guide_contents" not in self._stubs: - self._stubs["get_style_guide_contents"] = self.grpc_channel.unary_unary( + self._stubs["get_style_guide_contents"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.LintingService/GetStyleGuideContents", request_serializer=linting_service.GetStyleGuideContentsRequest.serialize, response_deserializer=linting_service.StyleGuideContents.deserialize, @@ -341,7 +426,7 @@ def lint_spec(self) -> Callable[[linting_service.LintSpecRequest], empty_pb2.Emp # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "lint_spec" not in self._stubs: - self._stubs["lint_spec"] = self.grpc_channel.unary_unary( + self._stubs["lint_spec"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.LintingService/LintSpec", request_serializer=linting_service.LintSpecRequest.serialize, response_deserializer=empty_pb2.Empty.FromString, @@ -349,7 +434,7 @@ def lint_spec(self) -> Callable[[linting_service.LintSpecRequest], empty_pb2.Emp return self._stubs["lint_spec"] def close(self): - self.grpc_channel.close() + self._logged_channel.close() @property def delete_operation( @@ -361,7 +446,7 @@ def delete_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_operation" not in self._stubs: - self._stubs["delete_operation"] = self.grpc_channel.unary_unary( + self._stubs["delete_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/DeleteOperation", request_serializer=operations_pb2.DeleteOperationRequest.SerializeToString, response_deserializer=None, @@ -378,7 +463,7 @@ def cancel_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "cancel_operation" not in self._stubs: - self._stubs["cancel_operation"] = self.grpc_channel.unary_unary( + self._stubs["cancel_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/CancelOperation", request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, response_deserializer=None, @@ -395,7 +480,7 @@ def get_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_operation" not in self._stubs: - self._stubs["get_operation"] = self.grpc_channel.unary_unary( + self._stubs["get_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/GetOperation", request_serializer=operations_pb2.GetOperationRequest.SerializeToString, response_deserializer=operations_pb2.Operation.FromString, @@ -414,7 +499,7 @@ def list_operations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_operations" not in self._stubs: - self._stubs["list_operations"] = self.grpc_channel.unary_unary( + self._stubs["list_operations"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/ListOperations", request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, response_deserializer=operations_pb2.ListOperationsResponse.FromString, @@ -433,7 +518,7 @@ def list_locations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_locations" not in self._stubs: - self._stubs["list_locations"] = self.grpc_channel.unary_unary( + self._stubs["list_locations"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/ListLocations", request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, response_deserializer=locations_pb2.ListLocationsResponse.FromString, @@ -450,7 +535,7 @@ def get_location( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_location" not in self._stubs: - self._stubs["get_location"] = self.grpc_channel.unary_unary( + self._stubs["get_location"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/GetLocation", request_serializer=locations_pb2.GetLocationRequest.SerializeToString, response_deserializer=locations_pb2.Location.FromString, diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/transports/grpc_asyncio.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/transports/grpc_asyncio.py index a1753eed897d..2b9718241a19 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/transports/grpc_asyncio.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/linting_service/transports/grpc_asyncio.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import inspect +import json +import logging as std_logging +import pickle from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union import warnings @@ -24,14 +28,93 @@ from google.cloud.location import locations_pb2 # type: ignore from google.longrunning import operations_pb2 # type: ignore from google.protobuf import empty_pb2 # type: ignore +from google.protobuf.json_format import MessageToJson +import google.protobuf.message import grpc # type: ignore from grpc.experimental import aio # type: ignore +import proto # type: ignore from google.cloud.apihub_v1.types import linting_service from .base import DEFAULT_CLIENT_INFO, LintingServiceTransport from .grpc import LintingServiceGrpcTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class _LoggingClientAIOInterceptor( + grpc.aio.UnaryUnaryClientInterceptor +): # pragma: NO COVER + async def intercept_unary_unary(self, continuation, client_call_details, request): + logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ) + if logging_enabled: # pragma: NO COVER + request_metadata = client_call_details.metadata + if isinstance(request, proto.Message): + request_payload = type(request).to_json(request) + elif isinstance(request, google.protobuf.message.Message): + request_payload = MessageToJson(request) + else: + request_payload = f"{type(request).__name__}: {pickle.dumps(request)}" + + request_metadata = { + key: value.decode("utf-8") if isinstance(value, bytes) else value + for key, value in request_metadata + } + grpc_request = { + "payload": request_payload, + "requestMethod": "grpc", + "metadata": dict(request_metadata), + } + _LOGGER.debug( + f"Sending request for {client_call_details.method}", + extra={ + "serviceName": "google.cloud.apihub.v1.LintingService", + "rpcName": str(client_call_details.method), + "request": grpc_request, + "metadata": grpc_request["metadata"], + }, + ) + response = await continuation(client_call_details, request) + if logging_enabled: # pragma: NO COVER + response_metadata = await response.trailing_metadata() + # Convert gRPC metadata `` to list of tuples + metadata = ( + dict([(k, str(v)) for k, v in response_metadata]) + if response_metadata + else None + ) + result = await response + if isinstance(result, proto.Message): + response_payload = type(result).to_json(result) + elif isinstance(result, google.protobuf.message.Message): + response_payload = MessageToJson(result) + else: + response_payload = f"{type(result).__name__}: {pickle.dumps(result)}" + grpc_response = { + "payload": response_payload, + "metadata": metadata, + "status": "OK", + } + _LOGGER.debug( + f"Received response to rpc {client_call_details.method}.", + extra={ + "serviceName": "google.cloud.apihub.v1.LintingService", + "rpcName": str(client_call_details.method), + "response": grpc_response, + "metadata": grpc_response["metadata"], + }, + ) + return response + class LintingServiceGrpcAsyncIOTransport(LintingServiceTransport): """gRPC AsyncIO backend transport for LintingService. @@ -228,7 +311,13 @@ def __init__( ], ) - # Wrap messages. This must be done after self._grpc_channel exists + self._interceptor = _LoggingClientAIOInterceptor() + self._grpc_channel._unary_unary_interceptors.append(self._interceptor) + self._logged_channel = self._grpc_channel + self._wrap_with_kind = ( + "kind" in inspect.signature(gapic_v1.method_async.wrap_method).parameters + ) + # Wrap messages. This must be done after self._logged_channel exists self._prep_wrapped_messages(client_info) @property @@ -262,7 +351,7 @@ def get_style_guide( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_style_guide" not in self._stubs: - self._stubs["get_style_guide"] = self.grpc_channel.unary_unary( + self._stubs["get_style_guide"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.LintingService/GetStyleGuide", request_serializer=linting_service.GetStyleGuideRequest.serialize, response_deserializer=linting_service.StyleGuide.deserialize, @@ -291,7 +380,7 @@ def update_style_guide( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "update_style_guide" not in self._stubs: - self._stubs["update_style_guide"] = self.grpc_channel.unary_unary( + self._stubs["update_style_guide"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.LintingService/UpdateStyleGuide", request_serializer=linting_service.UpdateStyleGuideRequest.serialize, response_deserializer=linting_service.StyleGuide.deserialize, @@ -320,7 +409,7 @@ def get_style_guide_contents( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_style_guide_contents" not in self._stubs: - self._stubs["get_style_guide_contents"] = self.grpc_channel.unary_unary( + self._stubs["get_style_guide_contents"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.LintingService/GetStyleGuideContents", request_serializer=linting_service.GetStyleGuideContentsRequest.serialize, response_deserializer=linting_service.StyleGuideContents.deserialize, @@ -349,7 +438,7 @@ def lint_spec( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "lint_spec" not in self._stubs: - self._stubs["lint_spec"] = self.grpc_channel.unary_unary( + self._stubs["lint_spec"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.LintingService/LintSpec", request_serializer=linting_service.LintSpecRequest.serialize, response_deserializer=empty_pb2.Empty.FromString, @@ -359,7 +448,7 @@ def lint_spec( def _prep_wrapped_messages(self, client_info): """Precompute the wrapped methods, overriding the base class method to use async wrappers.""" self._wrapped_methods = { - self.get_style_guide: gapic_v1.method_async.wrap_method( + self.get_style_guide: self._wrap_method( self.get_style_guide, default_retry=retries.AsyncRetry( initial=1.0, @@ -373,12 +462,12 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.update_style_guide: gapic_v1.method_async.wrap_method( + self.update_style_guide: self._wrap_method( self.update_style_guide, default_timeout=60.0, client_info=client_info, ), - self.get_style_guide_contents: gapic_v1.method_async.wrap_method( + self.get_style_guide_contents: self._wrap_method( self.get_style_guide_contents, default_retry=retries.AsyncRetry( initial=1.0, @@ -392,15 +481,54 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.lint_spec: gapic_v1.method_async.wrap_method( + self.lint_spec: self._wrap_method( self.lint_spec, default_timeout=60.0, client_info=client_info, ), + self.get_location: self._wrap_method( + self.get_location, + default_timeout=None, + client_info=client_info, + ), + self.list_locations: self._wrap_method( + self.list_locations, + default_timeout=None, + client_info=client_info, + ), + self.cancel_operation: self._wrap_method( + self.cancel_operation, + default_timeout=None, + client_info=client_info, + ), + self.delete_operation: self._wrap_method( + self.delete_operation, + default_timeout=None, + client_info=client_info, + ), + self.get_operation: self._wrap_method( + self.get_operation, + default_timeout=None, + client_info=client_info, + ), + self.list_operations: self._wrap_method( + self.list_operations, + default_timeout=None, + client_info=client_info, + ), } + def _wrap_method(self, func, *args, **kwargs): + if self._wrap_with_kind: # pragma: NO COVER + kwargs["kind"] = self.kind + return gapic_v1.method_async.wrap_method(func, *args, **kwargs) + def close(self): - return self.grpc_channel.close() + return self._logged_channel.close() + + @property + def kind(self) -> str: + return "grpc_asyncio" @property def delete_operation( @@ -412,7 +540,7 @@ def delete_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_operation" not in self._stubs: - self._stubs["delete_operation"] = self.grpc_channel.unary_unary( + self._stubs["delete_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/DeleteOperation", request_serializer=operations_pb2.DeleteOperationRequest.SerializeToString, response_deserializer=None, @@ -429,7 +557,7 @@ def cancel_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "cancel_operation" not in self._stubs: - self._stubs["cancel_operation"] = self.grpc_channel.unary_unary( + self._stubs["cancel_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/CancelOperation", request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, response_deserializer=None, @@ -446,7 +574,7 @@ def get_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_operation" not in self._stubs: - self._stubs["get_operation"] = self.grpc_channel.unary_unary( + self._stubs["get_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/GetOperation", request_serializer=operations_pb2.GetOperationRequest.SerializeToString, response_deserializer=operations_pb2.Operation.FromString, @@ -465,7 +593,7 @@ def list_operations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_operations" not in self._stubs: - self._stubs["list_operations"] = self.grpc_channel.unary_unary( + self._stubs["list_operations"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/ListOperations", request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, response_deserializer=operations_pb2.ListOperationsResponse.FromString, @@ -484,7 +612,7 @@ def list_locations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_locations" not in self._stubs: - self._stubs["list_locations"] = self.grpc_channel.unary_unary( + self._stubs["list_locations"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/ListLocations", request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, response_deserializer=locations_pb2.ListLocationsResponse.FromString, @@ -501,7 +629,7 @@ def get_location( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_location" not in self._stubs: - self._stubs["get_location"] = self.grpc_channel.unary_unary( + self._stubs["get_location"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/GetLocation", request_serializer=locations_pb2.GetLocationRequest.SerializeToString, response_deserializer=locations_pb2.Location.FromString, diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/__init__.py index 52bde941dbce..c6ace02481a0 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/__init__.py @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from .async_client import ProvisioningAsyncClient from .client import ProvisioningClient -__all__ = ("ProvisioningClient",) +__all__ = ( + "ProvisioningClient", + "ProvisioningAsyncClient", +) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/async_client.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/async_client.py index 5f52e6bf1413..8e9a9c7fe8c3 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/async_client.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/async_client.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,6 +14,7 @@ # limitations under the License. # from collections import OrderedDict +import logging as std_logging import re from typing import ( Callable, @@ -34,6 +35,7 @@ from google.api_core.client_options import ClientOptions from google.auth import credentials as ga_credentials # type: ignore from google.oauth2 import service_account # type: ignore +import google.protobuf from google.cloud.apihub_v1 import gapic_version as package_version @@ -46,6 +48,7 @@ from google.api_core import operation_async # type: ignore from google.cloud.location import locations_pb2 # type: ignore from google.longrunning import operations_pb2 # type: ignore +from google.protobuf import empty_pb2 # type: ignore from google.protobuf import timestamp_pb2 # type: ignore from google.cloud.apihub_v1.types import common_fields, provisioning_service @@ -54,6 +57,15 @@ from .transports.base import DEFAULT_CLIENT_INFO, ProvisioningTransport from .transports.grpc_asyncio import ProvisioningGrpcAsyncIOTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + class ProvisioningAsyncClient: """This service is used for managing the data plane provisioning @@ -259,6 +271,28 @@ def __init__( client_info=client_info, ) + if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ): # pragma: NO COVER + _LOGGER.debug( + "Created client `google.cloud.apihub_v1.ProvisioningAsyncClient`.", + extra={ + "serviceName": "google.cloud.apihub.v1.Provisioning", + "universeDomain": getattr( + self._client._transport._credentials, "universe_domain", "" + ), + "credentialsType": f"{type(self._client._transport._credentials).__module__}.{type(self._client._transport._credentials).__qualname__}", + "credentialsInfo": getattr( + self.transport._credentials, "get_cred_info", lambda: None + )(), + } + if hasattr(self._client._transport, "_credentials") + else { + "serviceName": "google.cloud.apihub.v1.Provisioning", + "credentialsType": None, + }, + ) + async def create_api_hub_instance( self, request: Optional[ @@ -270,7 +304,7 @@ async def create_api_hub_instance( api_hub_instance_id: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> operation_async.AsyncOperation: r"""Provisions instance resources for the API Hub. @@ -290,12 +324,8 @@ async def sample_create_api_hub_instance(): client = apihub_v1.ProvisioningAsyncClient() # Initialize request argument(s) - api_hub_instance = apihub_v1.ApiHubInstance() - api_hub_instance.config.cmek_key_name = "cmek_key_name_value" - request = apihub_v1.CreateApiHubInstanceRequest( parent="parent_value", - api_hub_instance=api_hub_instance, ) # Make the request @@ -341,8 +371,10 @@ async def sample_create_api_hub_instance(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.api_core.operation_async.AsyncOperation: @@ -356,7 +388,10 @@ async def sample_create_api_hub_instance(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([parent, api_hub_instance, api_hub_instance_id]) + flattened_params = [parent, api_hub_instance, api_hub_instance_id] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -411,6 +446,143 @@ async def sample_create_api_hub_instance(): # Done; return the response. return response + async def delete_api_hub_instance( + self, + request: Optional[ + Union[provisioning_service.DeleteApiHubInstanceRequest, dict] + ] = None, + *, + name: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), + ) -> operation_async.AsyncOperation: + r"""Deletes the API hub instance. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import apihub_v1 + + async def sample_delete_api_hub_instance(): + # Create a client + client = apihub_v1.ProvisioningAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DeleteApiHubInstanceRequest( + name="name_value", + ) + + # Make the request + operation = client.delete_api_hub_instance(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.apihub_v1.types.DeleteApiHubInstanceRequest, dict]]): + The request object. The + [DeleteApiHubInstance][google.cloud.apihub.v1.Provisioning.DeleteApiHubInstance] + method's request. + name (:class:`str`): + Required. The name of the Api Hub instance to delete. + Format: + ``projects/{project}/locations/{location}/apiHubInstances/{apiHubInstance}``. + + This corresponds to the ``name`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + + Returns: + google.api_core.operation_async.AsyncOperation: + An object representing a long-running operation. + + The result type for the operation will be :class:`google.protobuf.empty_pb2.Empty` A generic empty message that you can re-use to avoid defining duplicated + empty messages in your APIs. A typical example is to + use it as the request or the response type of an API + method. For instance: + + service Foo { + rpc Bar(google.protobuf.Empty) returns + (google.protobuf.Empty); + + } + + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) + 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance(request, provisioning_service.DeleteApiHubInstanceRequest): + request = provisioning_service.DeleteApiHubInstanceRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if name is not None: + request.name = name + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.delete_api_hub_instance + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Wrap the response in an operation future. + response = operation_async.from_gapic( + response, + self._client._transport.operations_client, + empty_pb2.Empty, + metadata_type=common_fields.OperationMetadata, + ) + + # Done; return the response. + return response + async def get_api_hub_instance( self, request: Optional[ @@ -420,7 +592,7 @@ async def get_api_hub_instance( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> common_fields.ApiHubInstance: r"""Gets details of a single API Hub instance. @@ -466,8 +638,10 @@ async def sample_get_api_hub_instance(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.ApiHubInstance: @@ -480,7 +654,10 @@ async def sample_get_api_hub_instance(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -532,7 +709,7 @@ async def lookup_api_hub_instance( parent: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> provisioning_service.LookupApiHubInstanceResponse: r"""Looks up an Api Hub instance in a given GCP project. There will always be only one Api Hub instance for a GCP @@ -581,8 +758,10 @@ async def sample_lookup_api_hub_instance(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.LookupApiHubInstanceResponse: @@ -594,7 +773,10 @@ async def sample_lookup_api_hub_instance(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([parent]) + flattened_params = [parent] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -643,7 +825,7 @@ async def list_operations( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> operations_pb2.ListOperationsResponse: r"""Lists operations that match the specified filter in the request. @@ -654,8 +836,10 @@ async def list_operations( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.operations_pb2.ListOperationsResponse: Response message for ``ListOperations`` method. @@ -668,11 +852,7 @@ async def list_operations( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.list_operations, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.list_operations] # Certain fields should be provided within the metadata header; # add these here. @@ -700,7 +880,7 @@ async def get_operation( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> operations_pb2.Operation: r"""Gets the latest state of a long-running operation. @@ -711,8 +891,10 @@ async def get_operation( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.operations_pb2.Operation: An ``Operation`` object. @@ -725,11 +907,7 @@ async def get_operation( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.get_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.get_operation] # Certain fields should be provided within the metadata header; # add these here. @@ -757,7 +935,7 @@ async def delete_operation( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Deletes a long-running operation. @@ -773,8 +951,10 @@ async def delete_operation( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: None """ @@ -786,11 +966,7 @@ async def delete_operation( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.delete_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.delete_operation] # Certain fields should be provided within the metadata header; # add these here. @@ -815,7 +991,7 @@ async def cancel_operation( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Starts asynchronous cancellation on a long-running operation. @@ -830,8 +1006,10 @@ async def cancel_operation( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: None """ @@ -843,11 +1021,7 @@ async def cancel_operation( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.cancel_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.cancel_operation] # Certain fields should be provided within the metadata header; # add these here. @@ -872,7 +1046,7 @@ async def get_location( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> locations_pb2.Location: r"""Gets information about a location. @@ -883,8 +1057,10 @@ async def get_location( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.location_pb2.Location: Location object. @@ -897,11 +1073,7 @@ async def get_location( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.get_location, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.get_location] # Certain fields should be provided within the metadata header; # add these here. @@ -929,7 +1101,7 @@ async def list_locations( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> locations_pb2.ListLocationsResponse: r"""Lists information about the supported locations for this service. @@ -940,8 +1112,10 @@ async def list_locations( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.location_pb2.ListLocationsResponse: Response message for ``ListLocations`` method. @@ -954,11 +1128,7 @@ async def list_locations( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.list_locations, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.list_locations] # Certain fields should be provided within the metadata header; # add these here. @@ -991,5 +1161,8 @@ async def __aexit__(self, exc_type, exc, tb): gapic_version=package_version.__version__ ) +if hasattr(DEFAULT_CLIENT_INFO, "protobuf_runtime_version"): # pragma: NO COVER + DEFAULT_CLIENT_INFO.protobuf_runtime_version = google.protobuf.__version__ + __all__ = ("ProvisioningAsyncClient",) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/client.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/client.py index e837863071f2..d07ee03353a5 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/client.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/client.py @@ -71,6 +71,8 @@ from google.cloud.apihub_v1.types import common_fields, provisioning_service from .transports.base import DEFAULT_CLIENT_INFO, ProvisioningTransport +from .transports.grpc import ProvisioningGrpcTransport +from .transports.grpc_asyncio import ProvisioningGrpcAsyncIOTransport from .transports.rest import ProvisioningRestTransport @@ -83,6 +85,8 @@ class ProvisioningClientMeta(type): """ _transport_registry = OrderedDict() # type: Dict[str, Type[ProvisioningTransport]] + _transport_registry["grpc"] = ProvisioningGrpcTransport + _transport_registry["grpc_asyncio"] = ProvisioningGrpcAsyncIOTransport _transport_registry["rest"] = ProvisioningRestTransport def get_transport_class( diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/transports/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/transports/__init__.py index f8e50e2c9813..6f89d267b053 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/transports/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/transports/__init__.py @@ -17,14 +17,20 @@ from typing import Dict, Type from .base import ProvisioningTransport +from .grpc import ProvisioningGrpcTransport +from .grpc_asyncio import ProvisioningGrpcAsyncIOTransport from .rest import ProvisioningRestInterceptor, ProvisioningRestTransport # Compile a registry of transports. _transport_registry = OrderedDict() # type: Dict[str, Type[ProvisioningTransport]] +_transport_registry["grpc"] = ProvisioningGrpcTransport +_transport_registry["grpc_asyncio"] = ProvisioningGrpcAsyncIOTransport _transport_registry["rest"] = ProvisioningRestTransport __all__ = ( "ProvisioningTransport", + "ProvisioningGrpcTransport", + "ProvisioningGrpcAsyncIOTransport", "ProvisioningRestTransport", "ProvisioningRestInterceptor", ) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/transports/grpc.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/transports/grpc.py index 70851c33861e..82d829826312 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/transports/grpc.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/transports/grpc.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,6 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import json +import logging as std_logging +import pickle from typing import Callable, Dict, Optional, Sequence, Tuple, Union import warnings @@ -22,12 +25,89 @@ from google.auth.transport.grpc import SslCredentials # type: ignore from google.cloud.location import locations_pb2 # type: ignore from google.longrunning import operations_pb2 # type: ignore +from google.protobuf.json_format import MessageToJson +import google.protobuf.message import grpc # type: ignore +import proto # type: ignore from google.cloud.apihub_v1.types import common_fields, provisioning_service from .base import DEFAULT_CLIENT_INFO, ProvisioningTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class _LoggingClientInterceptor(grpc.UnaryUnaryClientInterceptor): # pragma: NO COVER + def intercept_unary_unary(self, continuation, client_call_details, request): + logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ) + if logging_enabled: # pragma: NO COVER + request_metadata = client_call_details.metadata + if isinstance(request, proto.Message): + request_payload = type(request).to_json(request) + elif isinstance(request, google.protobuf.message.Message): + request_payload = MessageToJson(request) + else: + request_payload = f"{type(request).__name__}: {pickle.dumps(request)}" + + request_metadata = { + key: value.decode("utf-8") if isinstance(value, bytes) else value + for key, value in request_metadata + } + grpc_request = { + "payload": request_payload, + "requestMethod": "grpc", + "metadata": dict(request_metadata), + } + _LOGGER.debug( + f"Sending request for {client_call_details.method}", + extra={ + "serviceName": "google.cloud.apihub.v1.Provisioning", + "rpcName": str(client_call_details.method), + "request": grpc_request, + "metadata": grpc_request["metadata"], + }, + ) + response = continuation(client_call_details, request) + if logging_enabled: # pragma: NO COVER + response_metadata = response.trailing_metadata() + # Convert gRPC metadata `` to list of tuples + metadata = ( + dict([(k, str(v)) for k, v in response_metadata]) + if response_metadata + else None + ) + result = response.result() + if isinstance(result, proto.Message): + response_payload = type(result).to_json(result) + elif isinstance(result, google.protobuf.message.Message): + response_payload = MessageToJson(result) + else: + response_payload = f"{type(result).__name__}: {pickle.dumps(result)}" + grpc_response = { + "payload": response_payload, + "metadata": metadata, + "status": "OK", + } + _LOGGER.debug( + f"Received response for {client_call_details.method}.", + extra={ + "serviceName": "google.cloud.apihub.v1.Provisioning", + "rpcName": client_call_details.method, + "response": grpc_response, + "metadata": grpc_response["metadata"], + }, + ) + return response + class ProvisioningGrpcTransport(ProvisioningTransport): """gRPC backend transport for Provisioning. @@ -183,7 +263,12 @@ def __init__( ], ) - # Wrap messages. This must be done after self._grpc_channel exists + self._interceptor = _LoggingClientInterceptor() + self._logged_channel = grpc.intercept_channel( + self._grpc_channel, self._interceptor + ) + + # Wrap messages. This must be done after self._logged_channel exists self._prep_wrapped_messages(client_info) @classmethod @@ -247,7 +332,9 @@ def operations_client(self) -> operations_v1.OperationsClient: """ # Quick check: Only create a new client if we do not already have one. if self._operations_client is None: - self._operations_client = operations_v1.OperationsClient(self.grpc_channel) + self._operations_client = operations_v1.OperationsClient( + self._logged_channel + ) # Return the client from cache. return self._operations_client @@ -273,13 +360,41 @@ def create_api_hub_instance( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "create_api_hub_instance" not in self._stubs: - self._stubs["create_api_hub_instance"] = self.grpc_channel.unary_unary( + self._stubs["create_api_hub_instance"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.Provisioning/CreateApiHubInstance", request_serializer=provisioning_service.CreateApiHubInstanceRequest.serialize, response_deserializer=operations_pb2.Operation.FromString, ) return self._stubs["create_api_hub_instance"] + @property + def delete_api_hub_instance( + self, + ) -> Callable[ + [provisioning_service.DeleteApiHubInstanceRequest], operations_pb2.Operation + ]: + r"""Return a callable for the delete api hub instance method over gRPC. + + Deletes the API hub instance. + + Returns: + Callable[[~.DeleteApiHubInstanceRequest], + ~.Operation]: + 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 "delete_api_hub_instance" not in self._stubs: + self._stubs["delete_api_hub_instance"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.Provisioning/DeleteApiHubInstance", + request_serializer=provisioning_service.DeleteApiHubInstanceRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["delete_api_hub_instance"] + @property def get_api_hub_instance( self, @@ -301,7 +416,7 @@ def get_api_hub_instance( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_api_hub_instance" not in self._stubs: - self._stubs["get_api_hub_instance"] = self.grpc_channel.unary_unary( + self._stubs["get_api_hub_instance"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.Provisioning/GetApiHubInstance", request_serializer=provisioning_service.GetApiHubInstanceRequest.serialize, response_deserializer=common_fields.ApiHubInstance.deserialize, @@ -332,7 +447,7 @@ def lookup_api_hub_instance( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "lookup_api_hub_instance" not in self._stubs: - self._stubs["lookup_api_hub_instance"] = self.grpc_channel.unary_unary( + self._stubs["lookup_api_hub_instance"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.Provisioning/LookupApiHubInstance", request_serializer=provisioning_service.LookupApiHubInstanceRequest.serialize, response_deserializer=provisioning_service.LookupApiHubInstanceResponse.deserialize, @@ -340,7 +455,7 @@ def lookup_api_hub_instance( return self._stubs["lookup_api_hub_instance"] def close(self): - self.grpc_channel.close() + self._logged_channel.close() @property def delete_operation( @@ -352,7 +467,7 @@ def delete_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_operation" not in self._stubs: - self._stubs["delete_operation"] = self.grpc_channel.unary_unary( + self._stubs["delete_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/DeleteOperation", request_serializer=operations_pb2.DeleteOperationRequest.SerializeToString, response_deserializer=None, @@ -369,7 +484,7 @@ def cancel_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "cancel_operation" not in self._stubs: - self._stubs["cancel_operation"] = self.grpc_channel.unary_unary( + self._stubs["cancel_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/CancelOperation", request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, response_deserializer=None, @@ -386,7 +501,7 @@ def get_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_operation" not in self._stubs: - self._stubs["get_operation"] = self.grpc_channel.unary_unary( + self._stubs["get_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/GetOperation", request_serializer=operations_pb2.GetOperationRequest.SerializeToString, response_deserializer=operations_pb2.Operation.FromString, @@ -405,7 +520,7 @@ def list_operations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_operations" not in self._stubs: - self._stubs["list_operations"] = self.grpc_channel.unary_unary( + self._stubs["list_operations"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/ListOperations", request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, response_deserializer=operations_pb2.ListOperationsResponse.FromString, @@ -424,7 +539,7 @@ def list_locations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_locations" not in self._stubs: - self._stubs["list_locations"] = self.grpc_channel.unary_unary( + self._stubs["list_locations"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/ListLocations", request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, response_deserializer=locations_pb2.ListLocationsResponse.FromString, @@ -441,7 +556,7 @@ def get_location( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_location" not in self._stubs: - self._stubs["get_location"] = self.grpc_channel.unary_unary( + self._stubs["get_location"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/GetLocation", request_serializer=locations_pb2.GetLocationRequest.SerializeToString, response_deserializer=locations_pb2.Location.FromString, diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/transports/grpc_asyncio.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/transports/grpc_asyncio.py index ad2087a81b75..8e4e84bd0c46 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/transports/grpc_asyncio.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/provisioning/transports/grpc_asyncio.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import inspect +import json +import logging as std_logging +import pickle from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union import warnings @@ -23,14 +27,93 @@ from google.auth.transport.grpc import SslCredentials # type: ignore from google.cloud.location import locations_pb2 # type: ignore from google.longrunning import operations_pb2 # type: ignore +from google.protobuf.json_format import MessageToJson +import google.protobuf.message import grpc # type: ignore from grpc.experimental import aio # type: ignore +import proto # type: ignore from google.cloud.apihub_v1.types import common_fields, provisioning_service from .base import DEFAULT_CLIENT_INFO, ProvisioningTransport from .grpc import ProvisioningGrpcTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class _LoggingClientAIOInterceptor( + grpc.aio.UnaryUnaryClientInterceptor +): # pragma: NO COVER + async def intercept_unary_unary(self, continuation, client_call_details, request): + logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ) + if logging_enabled: # pragma: NO COVER + request_metadata = client_call_details.metadata + if isinstance(request, proto.Message): + request_payload = type(request).to_json(request) + elif isinstance(request, google.protobuf.message.Message): + request_payload = MessageToJson(request) + else: + request_payload = f"{type(request).__name__}: {pickle.dumps(request)}" + + request_metadata = { + key: value.decode("utf-8") if isinstance(value, bytes) else value + for key, value in request_metadata + } + grpc_request = { + "payload": request_payload, + "requestMethod": "grpc", + "metadata": dict(request_metadata), + } + _LOGGER.debug( + f"Sending request for {client_call_details.method}", + extra={ + "serviceName": "google.cloud.apihub.v1.Provisioning", + "rpcName": str(client_call_details.method), + "request": grpc_request, + "metadata": grpc_request["metadata"], + }, + ) + response = await continuation(client_call_details, request) + if logging_enabled: # pragma: NO COVER + response_metadata = await response.trailing_metadata() + # Convert gRPC metadata `` to list of tuples + metadata = ( + dict([(k, str(v)) for k, v in response_metadata]) + if response_metadata + else None + ) + result = await response + if isinstance(result, proto.Message): + response_payload = type(result).to_json(result) + elif isinstance(result, google.protobuf.message.Message): + response_payload = MessageToJson(result) + else: + response_payload = f"{type(result).__name__}: {pickle.dumps(result)}" + grpc_response = { + "payload": response_payload, + "metadata": metadata, + "status": "OK", + } + _LOGGER.debug( + f"Received response to rpc {client_call_details.method}.", + extra={ + "serviceName": "google.cloud.apihub.v1.Provisioning", + "rpcName": str(client_call_details.method), + "response": grpc_response, + "metadata": grpc_response["metadata"], + }, + ) + return response + class ProvisioningGrpcAsyncIOTransport(ProvisioningTransport): """gRPC AsyncIO backend transport for Provisioning. @@ -229,7 +312,13 @@ def __init__( ], ) - # Wrap messages. This must be done after self._grpc_channel exists + self._interceptor = _LoggingClientAIOInterceptor() + self._grpc_channel._unary_unary_interceptors.append(self._interceptor) + self._logged_channel = self._grpc_channel + self._wrap_with_kind = ( + "kind" in inspect.signature(gapic_v1.method_async.wrap_method).parameters + ) + # Wrap messages. This must be done after self._logged_channel exists self._prep_wrapped_messages(client_info) @property @@ -252,7 +341,7 @@ def operations_client(self) -> operations_v1.OperationsAsyncClient: # Quick check: Only create a new client if we do not already have one. if self._operations_client is None: self._operations_client = operations_v1.OperationsAsyncClient( - self.grpc_channel + self._logged_channel ) # Return the client from cache. @@ -280,13 +369,42 @@ def create_api_hub_instance( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "create_api_hub_instance" not in self._stubs: - self._stubs["create_api_hub_instance"] = self.grpc_channel.unary_unary( + self._stubs["create_api_hub_instance"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.Provisioning/CreateApiHubInstance", request_serializer=provisioning_service.CreateApiHubInstanceRequest.serialize, response_deserializer=operations_pb2.Operation.FromString, ) return self._stubs["create_api_hub_instance"] + @property + def delete_api_hub_instance( + self, + ) -> Callable[ + [provisioning_service.DeleteApiHubInstanceRequest], + Awaitable[operations_pb2.Operation], + ]: + r"""Return a callable for the delete api hub instance method over gRPC. + + Deletes the API hub instance. + + Returns: + Callable[[~.DeleteApiHubInstanceRequest], + Awaitable[~.Operation]]: + 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 "delete_api_hub_instance" not in self._stubs: + self._stubs["delete_api_hub_instance"] = self._logged_channel.unary_unary( + "/google.cloud.apihub.v1.Provisioning/DeleteApiHubInstance", + request_serializer=provisioning_service.DeleteApiHubInstanceRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["delete_api_hub_instance"] + @property def get_api_hub_instance( self, @@ -309,7 +427,7 @@ def get_api_hub_instance( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_api_hub_instance" not in self._stubs: - self._stubs["get_api_hub_instance"] = self.grpc_channel.unary_unary( + self._stubs["get_api_hub_instance"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.Provisioning/GetApiHubInstance", request_serializer=provisioning_service.GetApiHubInstanceRequest.serialize, response_deserializer=common_fields.ApiHubInstance.deserialize, @@ -340,7 +458,7 @@ def lookup_api_hub_instance( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "lookup_api_hub_instance" not in self._stubs: - self._stubs["lookup_api_hub_instance"] = self.grpc_channel.unary_unary( + self._stubs["lookup_api_hub_instance"] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.Provisioning/LookupApiHubInstance", request_serializer=provisioning_service.LookupApiHubInstanceRequest.serialize, response_deserializer=provisioning_service.LookupApiHubInstanceResponse.deserialize, @@ -350,12 +468,17 @@ def lookup_api_hub_instance( def _prep_wrapped_messages(self, client_info): """Precompute the wrapped methods, overriding the base class method to use async wrappers.""" self._wrapped_methods = { - self.create_api_hub_instance: gapic_v1.method_async.wrap_method( + self.create_api_hub_instance: self._wrap_method( self.create_api_hub_instance, default_timeout=60.0, client_info=client_info, ), - self.get_api_hub_instance: gapic_v1.method_async.wrap_method( + self.delete_api_hub_instance: self._wrap_method( + self.delete_api_hub_instance, + default_timeout=None, + client_info=client_info, + ), + self.get_api_hub_instance: self._wrap_method( self.get_api_hub_instance, default_retry=retries.AsyncRetry( initial=1.0, @@ -369,7 +492,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.lookup_api_hub_instance: gapic_v1.method_async.wrap_method( + self.lookup_api_hub_instance: self._wrap_method( self.lookup_api_hub_instance, default_retry=retries.AsyncRetry( initial=1.0, @@ -383,10 +506,49 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), + self.get_location: self._wrap_method( + self.get_location, + default_timeout=None, + client_info=client_info, + ), + self.list_locations: self._wrap_method( + self.list_locations, + default_timeout=None, + client_info=client_info, + ), + self.cancel_operation: self._wrap_method( + self.cancel_operation, + default_timeout=None, + client_info=client_info, + ), + self.delete_operation: self._wrap_method( + self.delete_operation, + default_timeout=None, + client_info=client_info, + ), + self.get_operation: self._wrap_method( + self.get_operation, + default_timeout=None, + client_info=client_info, + ), + self.list_operations: self._wrap_method( + self.list_operations, + default_timeout=None, + client_info=client_info, + ), } + def _wrap_method(self, func, *args, **kwargs): + if self._wrap_with_kind: # pragma: NO COVER + kwargs["kind"] = self.kind + return gapic_v1.method_async.wrap_method(func, *args, **kwargs) + def close(self): - return self.grpc_channel.close() + return self._logged_channel.close() + + @property + def kind(self) -> str: + return "grpc_asyncio" @property def delete_operation( @@ -398,7 +560,7 @@ def delete_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_operation" not in self._stubs: - self._stubs["delete_operation"] = self.grpc_channel.unary_unary( + self._stubs["delete_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/DeleteOperation", request_serializer=operations_pb2.DeleteOperationRequest.SerializeToString, response_deserializer=None, @@ -415,7 +577,7 @@ def cancel_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "cancel_operation" not in self._stubs: - self._stubs["cancel_operation"] = self.grpc_channel.unary_unary( + self._stubs["cancel_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/CancelOperation", request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, response_deserializer=None, @@ -432,7 +594,7 @@ def get_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_operation" not in self._stubs: - self._stubs["get_operation"] = self.grpc_channel.unary_unary( + self._stubs["get_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/GetOperation", request_serializer=operations_pb2.GetOperationRequest.SerializeToString, response_deserializer=operations_pb2.Operation.FromString, @@ -451,7 +613,7 @@ def list_operations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_operations" not in self._stubs: - self._stubs["list_operations"] = self.grpc_channel.unary_unary( + self._stubs["list_operations"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/ListOperations", request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, response_deserializer=operations_pb2.ListOperationsResponse.FromString, @@ -470,7 +632,7 @@ def list_locations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_locations" not in self._stubs: - self._stubs["list_locations"] = self.grpc_channel.unary_unary( + self._stubs["list_locations"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/ListLocations", request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, response_deserializer=locations_pb2.ListLocationsResponse.FromString, @@ -487,7 +649,7 @@ def get_location( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_location" not in self._stubs: - self._stubs["get_location"] = self.grpc_channel.unary_unary( + self._stubs["get_location"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/GetLocation", request_serializer=locations_pb2.GetLocationRequest.SerializeToString, response_deserializer=locations_pb2.Location.FromString, diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/__init__.py index 84191a717b4d..33b4f7df6384 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/__init__.py @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from .async_client import RuntimeProjectAttachmentServiceAsyncClient from .client import RuntimeProjectAttachmentServiceClient -__all__ = ("RuntimeProjectAttachmentServiceClient",) +__all__ = ( + "RuntimeProjectAttachmentServiceClient", + "RuntimeProjectAttachmentServiceAsyncClient", +) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/async_client.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/async_client.py index aa2a3acd7dd3..6d74c71060d1 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/async_client.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/async_client.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,6 +14,7 @@ # limitations under the License. # from collections import OrderedDict +import logging as std_logging import re from typing import ( Callable, @@ -34,6 +35,7 @@ from google.api_core.client_options import ClientOptions from google.auth import credentials as ga_credentials # type: ignore from google.oauth2 import service_account # type: ignore +import google.protobuf from google.cloud.apihub_v1 import gapic_version as package_version @@ -56,6 +58,15 @@ ) from .transports.grpc_asyncio import RuntimeProjectAttachmentServiceGrpcAsyncIOTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + class RuntimeProjectAttachmentServiceAsyncClient: """This service is used for managing the runtime project @@ -279,6 +290,28 @@ def __init__( client_info=client_info, ) + if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ): # pragma: NO COVER + _LOGGER.debug( + "Created client `google.cloud.apihub_v1.RuntimeProjectAttachmentServiceAsyncClient`.", + extra={ + "serviceName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService", + "universeDomain": getattr( + self._client._transport._credentials, "universe_domain", "" + ), + "credentialsType": f"{type(self._client._transport._credentials).__module__}.{type(self._client._transport._credentials).__qualname__}", + "credentialsInfo": getattr( + self.transport._credentials, "get_cred_info", lambda: None + )(), + } + if hasattr(self._client._transport, "_credentials") + else { + "serviceName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService", + "credentialsType": None, + }, + ) + async def create_runtime_project_attachment( self, request: Optional[ @@ -295,7 +328,7 @@ async def create_runtime_project_attachment( runtime_project_attachment_id: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> runtime_project_attachment_service.RuntimeProjectAttachment: r"""Attaches a runtime project to the host project. @@ -364,8 +397,10 @@ async def sample_create_runtime_project_attachment(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.RuntimeProjectAttachment: @@ -381,8 +416,13 @@ async def sample_create_runtime_project_attachment(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any( - [parent, runtime_project_attachment, runtime_project_attachment_id] + flattened_params = [ + parent, + runtime_project_attachment, + runtime_project_attachment_id, + ] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 ) if request is not None and has_flattened_params: raise ValueError( @@ -447,7 +487,7 @@ async def get_runtime_project_attachment( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> runtime_project_attachment_service.RuntimeProjectAttachment: r"""Gets a runtime project attachment. @@ -493,8 +533,10 @@ async def sample_get_runtime_project_attachment(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.RuntimeProjectAttachment: @@ -510,7 +552,10 @@ async def sample_get_runtime_project_attachment(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -572,7 +617,7 @@ async def list_runtime_project_attachments( parent: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> pagers.ListRuntimeProjectAttachmentsAsyncPager: r"""List runtime projects attached to the host project. @@ -619,8 +664,10 @@ async def sample_list_runtime_project_attachments(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.services.runtime_project_attachment_service.pagers.ListRuntimeProjectAttachmentsAsyncPager: @@ -635,7 +682,10 @@ async def sample_list_runtime_project_attachments(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([parent]) + flattened_params = [parent] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -708,7 +758,7 @@ async def delete_runtime_project_attachment( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Delete a runtime project attachment in the API Hub. This call will detach the runtime project from the host @@ -753,13 +803,18 @@ async def sample_delete_runtime_project_attachment(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. """ # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -816,7 +871,7 @@ async def lookup_runtime_project_attachment( name: Optional[str] = None, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse: r"""Look up a runtime project attachment. This API can be called in the context of any project. @@ -864,8 +919,10 @@ async def sample_lookup_runtime_project_attachment(): retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: google.cloud.apihub_v1.types.LookupRuntimeProjectAttachmentResponse: @@ -877,7 +934,10 @@ async def sample_lookup_runtime_project_attachment(): # Create or coerce a protobuf request object. # - Quick check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - has_flattened_params = any([name]) + flattened_params = [name] + has_flattened_params = ( + len([param for param in flattened_params if param is not None]) > 0 + ) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " @@ -931,7 +991,7 @@ async def list_operations( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> operations_pb2.ListOperationsResponse: r"""Lists operations that match the specified filter in the request. @@ -942,8 +1002,10 @@ async def list_operations( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.operations_pb2.ListOperationsResponse: Response message for ``ListOperations`` method. @@ -956,11 +1018,7 @@ async def list_operations( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.list_operations, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.list_operations] # Certain fields should be provided within the metadata header; # add these here. @@ -988,7 +1046,7 @@ async def get_operation( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> operations_pb2.Operation: r"""Gets the latest state of a long-running operation. @@ -999,8 +1057,10 @@ async def get_operation( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.operations_pb2.Operation: An ``Operation`` object. @@ -1013,11 +1073,7 @@ async def get_operation( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.get_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.get_operation] # Certain fields should be provided within the metadata header; # add these here. @@ -1045,7 +1101,7 @@ async def delete_operation( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Deletes a long-running operation. @@ -1061,8 +1117,10 @@ async def delete_operation( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: None """ @@ -1074,11 +1132,7 @@ async def delete_operation( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.delete_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.delete_operation] # Certain fields should be provided within the metadata header; # add these here. @@ -1103,7 +1157,7 @@ async def cancel_operation( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> None: r"""Starts asynchronous cancellation on a long-running operation. @@ -1118,8 +1172,10 @@ async def cancel_operation( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: None """ @@ -1131,11 +1187,7 @@ async def cancel_operation( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.cancel_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.cancel_operation] # Certain fields should be provided within the metadata header; # add these here. @@ -1160,7 +1212,7 @@ async def get_location( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> locations_pb2.Location: r"""Gets information about a location. @@ -1171,8 +1223,10 @@ async def get_location( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.location_pb2.Location: Location object. @@ -1185,11 +1239,7 @@ async def get_location( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.get_location, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.get_location] # Certain fields should be provided within the metadata header; # add these here. @@ -1217,7 +1267,7 @@ async def list_locations( *, retry: OptionalRetry = gapic_v1.method.DEFAULT, timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), + metadata: Sequence[Tuple[str, Union[str, bytes]]] = (), ) -> locations_pb2.ListLocationsResponse: r"""Lists information about the supported locations for this service. @@ -1228,8 +1278,10 @@ async def list_locations( retry (google.api_core.retry_async.AsyncRetry): 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. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. Returns: ~.location_pb2.ListLocationsResponse: Response message for ``ListLocations`` method. @@ -1242,11 +1294,7 @@ async def list_locations( # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.list_locations, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) + rpc = self.transport._wrapped_methods[self._client._transport.list_locations] # Certain fields should be provided within the metadata header; # add these here. @@ -1279,5 +1327,8 @@ async def __aexit__(self, exc_type, exc, tb): gapic_version=package_version.__version__ ) +if hasattr(DEFAULT_CLIENT_INFO, "protobuf_runtime_version"): # pragma: NO COVER + DEFAULT_CLIENT_INFO.protobuf_runtime_version = google.protobuf.__version__ + __all__ = ("RuntimeProjectAttachmentServiceAsyncClient",) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/client.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/client.py index eb4715c795bc..7a8b9e4e228e 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/client.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/client.py @@ -72,6 +72,8 @@ DEFAULT_CLIENT_INFO, RuntimeProjectAttachmentServiceTransport, ) +from .transports.grpc import RuntimeProjectAttachmentServiceGrpcTransport +from .transports.grpc_asyncio import RuntimeProjectAttachmentServiceGrpcAsyncIOTransport from .transports.rest import RuntimeProjectAttachmentServiceRestTransport @@ -86,6 +88,10 @@ class RuntimeProjectAttachmentServiceClientMeta(type): _transport_registry = ( OrderedDict() ) # type: Dict[str, Type[RuntimeProjectAttachmentServiceTransport]] + _transport_registry["grpc"] = RuntimeProjectAttachmentServiceGrpcTransport + _transport_registry[ + "grpc_asyncio" + ] = RuntimeProjectAttachmentServiceGrpcAsyncIOTransport _transport_registry["rest"] = RuntimeProjectAttachmentServiceRestTransport def get_transport_class( diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/pagers.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/pagers.py index a99d31b8492c..7ec9855408df 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/pagers.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/pagers.py @@ -128,3 +128,98 @@ def __iter__( def __repr__(self) -> str: return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListRuntimeProjectAttachmentsAsyncPager: + """A pager for iterating through ``list_runtime_project_attachments`` requests. + + This class thinly wraps an initial + :class:`google.cloud.apihub_v1.types.ListRuntimeProjectAttachmentsResponse` object, and + provides an ``__aiter__`` method to iterate through its + ``runtime_project_attachments`` field. + + If there are more pages, the ``__aiter__`` method will make additional + ``ListRuntimeProjectAttachments`` requests and continue to iterate + through the ``runtime_project_attachments`` field on the + corresponding responses. + + All the usual :class:`google.cloud.apihub_v1.types.ListRuntimeProjectAttachmentsResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[ + ..., + Awaitable[ + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse + ], + ], + request: runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest, + response: runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse, + *, + retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, Union[str, bytes]]] = () + ): + """Instantiates the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.apihub_v1.types.ListRuntimeProjectAttachmentsRequest): + The initial request object. + response (google.cloud.apihub_v1.types.ListRuntimeProjectAttachmentsResponse): + The initial response object. + retry (google.api_core.retry.AsyncRetry): Designation of what errors, + if any, should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be + sent along with the request as metadata. Normally, each value must be of type `str`, + but for metadata keys ending with the suffix `-bin`, the corresponding values must + be of type `bytes`. + """ + self._method = method + self._request = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest( + request + ) + ) + self._response = response + self._retry = retry + self._timeout = timeout + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + async def pages( + self, + ) -> AsyncIterator[ + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse + ]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = await self._method( + self._request, + retry=self._retry, + timeout=self._timeout, + metadata=self._metadata, + ) + yield self._response + + def __aiter__( + self, + ) -> AsyncIterator[runtime_project_attachment_service.RuntimeProjectAttachment]: + async def async_generator(): + async for page in self.pages: + for response in page.runtime_project_attachments: + yield response + + return async_generator() + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/transports/__init__.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/transports/__init__.py index 0d61a196043e..fef6954cdf31 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/transports/__init__.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/transports/__init__.py @@ -17,6 +17,8 @@ from typing import Dict, Type from .base import RuntimeProjectAttachmentServiceTransport +from .grpc import RuntimeProjectAttachmentServiceGrpcTransport +from .grpc_asyncio import RuntimeProjectAttachmentServiceGrpcAsyncIOTransport from .rest import ( RuntimeProjectAttachmentServiceRestInterceptor, RuntimeProjectAttachmentServiceRestTransport, @@ -26,10 +28,16 @@ _transport_registry = ( OrderedDict() ) # type: Dict[str, Type[RuntimeProjectAttachmentServiceTransport]] +_transport_registry["grpc"] = RuntimeProjectAttachmentServiceGrpcTransport +_transport_registry[ + "grpc_asyncio" +] = RuntimeProjectAttachmentServiceGrpcAsyncIOTransport _transport_registry["rest"] = RuntimeProjectAttachmentServiceRestTransport __all__ = ( "RuntimeProjectAttachmentServiceTransport", + "RuntimeProjectAttachmentServiceGrpcTransport", + "RuntimeProjectAttachmentServiceGrpcAsyncIOTransport", "RuntimeProjectAttachmentServiceRestTransport", "RuntimeProjectAttachmentServiceRestInterceptor", ) diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/transports/grpc.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/transports/grpc.py index 5eaca16634d5..43a983e3e5cb 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/transports/grpc.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/transports/grpc.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,6 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import json +import logging as std_logging +import pickle from typing import Callable, Dict, Optional, Sequence, Tuple, Union import warnings @@ -23,12 +26,89 @@ from google.cloud.location import locations_pb2 # type: ignore from google.longrunning import operations_pb2 # type: ignore from google.protobuf import empty_pb2 # type: ignore +from google.protobuf.json_format import MessageToJson +import google.protobuf.message import grpc # type: ignore +import proto # type: ignore from google.cloud.apihub_v1.types import runtime_project_attachment_service from .base import DEFAULT_CLIENT_INFO, RuntimeProjectAttachmentServiceTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class _LoggingClientInterceptor(grpc.UnaryUnaryClientInterceptor): # pragma: NO COVER + def intercept_unary_unary(self, continuation, client_call_details, request): + logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ) + if logging_enabled: # pragma: NO COVER + request_metadata = client_call_details.metadata + if isinstance(request, proto.Message): + request_payload = type(request).to_json(request) + elif isinstance(request, google.protobuf.message.Message): + request_payload = MessageToJson(request) + else: + request_payload = f"{type(request).__name__}: {pickle.dumps(request)}" + + request_metadata = { + key: value.decode("utf-8") if isinstance(value, bytes) else value + for key, value in request_metadata + } + grpc_request = { + "payload": request_payload, + "requestMethod": "grpc", + "metadata": dict(request_metadata), + } + _LOGGER.debug( + f"Sending request for {client_call_details.method}", + extra={ + "serviceName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService", + "rpcName": str(client_call_details.method), + "request": grpc_request, + "metadata": grpc_request["metadata"], + }, + ) + response = continuation(client_call_details, request) + if logging_enabled: # pragma: NO COVER + response_metadata = response.trailing_metadata() + # Convert gRPC metadata `` to list of tuples + metadata = ( + dict([(k, str(v)) for k, v in response_metadata]) + if response_metadata + else None + ) + result = response.result() + if isinstance(result, proto.Message): + response_payload = type(result).to_json(result) + elif isinstance(result, google.protobuf.message.Message): + response_payload = MessageToJson(result) + else: + response_payload = f"{type(result).__name__}: {pickle.dumps(result)}" + grpc_response = { + "payload": response_payload, + "metadata": metadata, + "status": "OK", + } + _LOGGER.debug( + f"Received response for {client_call_details.method}.", + extra={ + "serviceName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService", + "rpcName": client_call_details.method, + "response": grpc_response, + "metadata": grpc_response["metadata"], + }, + ) + return response + class RuntimeProjectAttachmentServiceGrpcTransport( RuntimeProjectAttachmentServiceTransport @@ -185,7 +265,12 @@ def __init__( ], ) - # Wrap messages. This must be done after self._grpc_channel exists + self._interceptor = _LoggingClientInterceptor() + self._logged_channel = grpc.intercept_channel( + self._grpc_channel, self._interceptor + ) + + # Wrap messages. This must be done after self._logged_channel exists self._prep_wrapped_messages(client_info) @classmethod @@ -265,7 +350,7 @@ def create_runtime_project_attachment( if "create_runtime_project_attachment" not in self._stubs: self._stubs[ "create_runtime_project_attachment" - ] = self.grpc_channel.unary_unary( + ] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.RuntimeProjectAttachmentService/CreateRuntimeProjectAttachment", request_serializer=runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest.serialize, response_deserializer=runtime_project_attachment_service.RuntimeProjectAttachment.deserialize, @@ -296,7 +381,7 @@ def get_runtime_project_attachment( if "get_runtime_project_attachment" not in self._stubs: self._stubs[ "get_runtime_project_attachment" - ] = self.grpc_channel.unary_unary( + ] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.RuntimeProjectAttachmentService/GetRuntimeProjectAttachment", request_serializer=runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest.serialize, response_deserializer=runtime_project_attachment_service.RuntimeProjectAttachment.deserialize, @@ -328,7 +413,7 @@ def list_runtime_project_attachments( if "list_runtime_project_attachments" not in self._stubs: self._stubs[ "list_runtime_project_attachments" - ] = self.grpc_channel.unary_unary( + ] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.RuntimeProjectAttachmentService/ListRuntimeProjectAttachments", request_serializer=runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest.serialize, response_deserializer=runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse.deserialize, @@ -362,7 +447,7 @@ def delete_runtime_project_attachment( if "delete_runtime_project_attachment" not in self._stubs: self._stubs[ "delete_runtime_project_attachment" - ] = self.grpc_channel.unary_unary( + ] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.RuntimeProjectAttachmentService/DeleteRuntimeProjectAttachment", request_serializer=runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest.serialize, response_deserializer=empty_pb2.Empty.FromString, @@ -395,7 +480,7 @@ def lookup_runtime_project_attachment( if "lookup_runtime_project_attachment" not in self._stubs: self._stubs[ "lookup_runtime_project_attachment" - ] = self.grpc_channel.unary_unary( + ] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.RuntimeProjectAttachmentService/LookupRuntimeProjectAttachment", request_serializer=runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest.serialize, response_deserializer=runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse.deserialize, @@ -403,7 +488,7 @@ def lookup_runtime_project_attachment( return self._stubs["lookup_runtime_project_attachment"] def close(self): - self.grpc_channel.close() + self._logged_channel.close() @property def delete_operation( @@ -415,7 +500,7 @@ def delete_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_operation" not in self._stubs: - self._stubs["delete_operation"] = self.grpc_channel.unary_unary( + self._stubs["delete_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/DeleteOperation", request_serializer=operations_pb2.DeleteOperationRequest.SerializeToString, response_deserializer=None, @@ -432,7 +517,7 @@ def cancel_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "cancel_operation" not in self._stubs: - self._stubs["cancel_operation"] = self.grpc_channel.unary_unary( + self._stubs["cancel_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/CancelOperation", request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, response_deserializer=None, @@ -449,7 +534,7 @@ def get_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_operation" not in self._stubs: - self._stubs["get_operation"] = self.grpc_channel.unary_unary( + self._stubs["get_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/GetOperation", request_serializer=operations_pb2.GetOperationRequest.SerializeToString, response_deserializer=operations_pb2.Operation.FromString, @@ -468,7 +553,7 @@ def list_operations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_operations" not in self._stubs: - self._stubs["list_operations"] = self.grpc_channel.unary_unary( + self._stubs["list_operations"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/ListOperations", request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, response_deserializer=operations_pb2.ListOperationsResponse.FromString, @@ -487,7 +572,7 @@ def list_locations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_locations" not in self._stubs: - self._stubs["list_locations"] = self.grpc_channel.unary_unary( + self._stubs["list_locations"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/ListLocations", request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, response_deserializer=locations_pb2.ListLocationsResponse.FromString, @@ -504,7 +589,7 @@ def get_location( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_location" not in self._stubs: - self._stubs["get_location"] = self.grpc_channel.unary_unary( + self._stubs["get_location"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/GetLocation", request_serializer=locations_pb2.GetLocationRequest.SerializeToString, response_deserializer=locations_pb2.Location.FromString, diff --git a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/transports/grpc_asyncio.py b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/transports/grpc_asyncio.py index cf4121623217..5bcbd0531cad 100644 --- a/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/transports/grpc_asyncio.py +++ b/packages/google-cloud-apihub/google/cloud/apihub_v1/services/runtime_project_attachment_service/transports/grpc_asyncio.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import inspect +import json +import logging as std_logging +import pickle from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union import warnings @@ -24,14 +28,93 @@ from google.cloud.location import locations_pb2 # type: ignore from google.longrunning import operations_pb2 # type: ignore from google.protobuf import empty_pb2 # type: ignore +from google.protobuf.json_format import MessageToJson +import google.protobuf.message import grpc # type: ignore from grpc.experimental import aio # type: ignore +import proto # type: ignore from google.cloud.apihub_v1.types import runtime_project_attachment_service from .base import DEFAULT_CLIENT_INFO, RuntimeProjectAttachmentServiceTransport from .grpc import RuntimeProjectAttachmentServiceGrpcTransport +try: + from google.api_core import client_logging # type: ignore + + CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER +except ImportError: # pragma: NO COVER + CLIENT_LOGGING_SUPPORTED = False + +_LOGGER = std_logging.getLogger(__name__) + + +class _LoggingClientAIOInterceptor( + grpc.aio.UnaryUnaryClientInterceptor +): # pragma: NO COVER + async def intercept_unary_unary(self, continuation, client_call_details, request): + logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor( + std_logging.DEBUG + ) + if logging_enabled: # pragma: NO COVER + request_metadata = client_call_details.metadata + if isinstance(request, proto.Message): + request_payload = type(request).to_json(request) + elif isinstance(request, google.protobuf.message.Message): + request_payload = MessageToJson(request) + else: + request_payload = f"{type(request).__name__}: {pickle.dumps(request)}" + + request_metadata = { + key: value.decode("utf-8") if isinstance(value, bytes) else value + for key, value in request_metadata + } + grpc_request = { + "payload": request_payload, + "requestMethod": "grpc", + "metadata": dict(request_metadata), + } + _LOGGER.debug( + f"Sending request for {client_call_details.method}", + extra={ + "serviceName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService", + "rpcName": str(client_call_details.method), + "request": grpc_request, + "metadata": grpc_request["metadata"], + }, + ) + response = await continuation(client_call_details, request) + if logging_enabled: # pragma: NO COVER + response_metadata = await response.trailing_metadata() + # Convert gRPC metadata `` to list of tuples + metadata = ( + dict([(k, str(v)) for k, v in response_metadata]) + if response_metadata + else None + ) + result = await response + if isinstance(result, proto.Message): + response_payload = type(result).to_json(result) + elif isinstance(result, google.protobuf.message.Message): + response_payload = MessageToJson(result) + else: + response_payload = f"{type(result).__name__}: {pickle.dumps(result)}" + grpc_response = { + "payload": response_payload, + "metadata": metadata, + "status": "OK", + } + _LOGGER.debug( + f"Received response to rpc {client_call_details.method}.", + extra={ + "serviceName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService", + "rpcName": str(client_call_details.method), + "response": grpc_response, + "metadata": grpc_response["metadata"], + }, + ) + return response + class RuntimeProjectAttachmentServiceGrpcAsyncIOTransport( RuntimeProjectAttachmentServiceTransport @@ -231,7 +314,13 @@ def __init__( ], ) - # Wrap messages. This must be done after self._grpc_channel exists + self._interceptor = _LoggingClientAIOInterceptor() + self._grpc_channel._unary_unary_interceptors.append(self._interceptor) + self._logged_channel = self._grpc_channel + self._wrap_with_kind = ( + "kind" in inspect.signature(gapic_v1.method_async.wrap_method).parameters + ) + # Wrap messages. This must be done after self._logged_channel exists self._prep_wrapped_messages(client_info) @property @@ -269,7 +358,7 @@ def create_runtime_project_attachment( if "create_runtime_project_attachment" not in self._stubs: self._stubs[ "create_runtime_project_attachment" - ] = self.grpc_channel.unary_unary( + ] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.RuntimeProjectAttachmentService/CreateRuntimeProjectAttachment", request_serializer=runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest.serialize, response_deserializer=runtime_project_attachment_service.RuntimeProjectAttachment.deserialize, @@ -300,7 +389,7 @@ def get_runtime_project_attachment( if "get_runtime_project_attachment" not in self._stubs: self._stubs[ "get_runtime_project_attachment" - ] = self.grpc_channel.unary_unary( + ] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.RuntimeProjectAttachmentService/GetRuntimeProjectAttachment", request_serializer=runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest.serialize, response_deserializer=runtime_project_attachment_service.RuntimeProjectAttachment.deserialize, @@ -334,7 +423,7 @@ def list_runtime_project_attachments( if "list_runtime_project_attachments" not in self._stubs: self._stubs[ "list_runtime_project_attachments" - ] = self.grpc_channel.unary_unary( + ] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.RuntimeProjectAttachmentService/ListRuntimeProjectAttachments", request_serializer=runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest.serialize, response_deserializer=runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse.deserialize, @@ -368,7 +457,7 @@ def delete_runtime_project_attachment( if "delete_runtime_project_attachment" not in self._stubs: self._stubs[ "delete_runtime_project_attachment" - ] = self.grpc_channel.unary_unary( + ] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.RuntimeProjectAttachmentService/DeleteRuntimeProjectAttachment", request_serializer=runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest.serialize, response_deserializer=empty_pb2.Empty.FromString, @@ -403,7 +492,7 @@ def lookup_runtime_project_attachment( if "lookup_runtime_project_attachment" not in self._stubs: self._stubs[ "lookup_runtime_project_attachment" - ] = self.grpc_channel.unary_unary( + ] = self._logged_channel.unary_unary( "/google.cloud.apihub.v1.RuntimeProjectAttachmentService/LookupRuntimeProjectAttachment", request_serializer=runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest.serialize, response_deserializer=runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse.deserialize, @@ -413,12 +502,12 @@ def lookup_runtime_project_attachment( def _prep_wrapped_messages(self, client_info): """Precompute the wrapped methods, overriding the base class method to use async wrappers.""" self._wrapped_methods = { - self.create_runtime_project_attachment: gapic_v1.method_async.wrap_method( + self.create_runtime_project_attachment: self._wrap_method( self.create_runtime_project_attachment, default_timeout=60.0, client_info=client_info, ), - self.get_runtime_project_attachment: gapic_v1.method_async.wrap_method( + self.get_runtime_project_attachment: self._wrap_method( self.get_runtime_project_attachment, default_retry=retries.AsyncRetry( initial=1.0, @@ -432,7 +521,7 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.list_runtime_project_attachments: gapic_v1.method_async.wrap_method( + self.list_runtime_project_attachments: self._wrap_method( self.list_runtime_project_attachments, default_retry=retries.AsyncRetry( initial=1.0, @@ -446,12 +535,12 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), - self.delete_runtime_project_attachment: gapic_v1.method_async.wrap_method( + self.delete_runtime_project_attachment: self._wrap_method( self.delete_runtime_project_attachment, default_timeout=60.0, client_info=client_info, ), - self.lookup_runtime_project_attachment: gapic_v1.method_async.wrap_method( + self.lookup_runtime_project_attachment: self._wrap_method( self.lookup_runtime_project_attachment, default_retry=retries.AsyncRetry( initial=1.0, @@ -465,10 +554,49 @@ def _prep_wrapped_messages(self, client_info): default_timeout=60.0, client_info=client_info, ), + self.get_location: self._wrap_method( + self.get_location, + default_timeout=None, + client_info=client_info, + ), + self.list_locations: self._wrap_method( + self.list_locations, + default_timeout=None, + client_info=client_info, + ), + self.cancel_operation: self._wrap_method( + self.cancel_operation, + default_timeout=None, + client_info=client_info, + ), + self.delete_operation: self._wrap_method( + self.delete_operation, + default_timeout=None, + client_info=client_info, + ), + self.get_operation: self._wrap_method( + self.get_operation, + default_timeout=None, + client_info=client_info, + ), + self.list_operations: self._wrap_method( + self.list_operations, + default_timeout=None, + client_info=client_info, + ), } + def _wrap_method(self, func, *args, **kwargs): + if self._wrap_with_kind: # pragma: NO COVER + kwargs["kind"] = self.kind + return gapic_v1.method_async.wrap_method(func, *args, **kwargs) + def close(self): - return self.grpc_channel.close() + return self._logged_channel.close() + + @property + def kind(self) -> str: + return "grpc_asyncio" @property def delete_operation( @@ -480,7 +608,7 @@ def delete_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "delete_operation" not in self._stubs: - self._stubs["delete_operation"] = self.grpc_channel.unary_unary( + self._stubs["delete_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/DeleteOperation", request_serializer=operations_pb2.DeleteOperationRequest.SerializeToString, response_deserializer=None, @@ -497,7 +625,7 @@ def cancel_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "cancel_operation" not in self._stubs: - self._stubs["cancel_operation"] = self.grpc_channel.unary_unary( + self._stubs["cancel_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/CancelOperation", request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, response_deserializer=None, @@ -514,7 +642,7 @@ def get_operation( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_operation" not in self._stubs: - self._stubs["get_operation"] = self.grpc_channel.unary_unary( + self._stubs["get_operation"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/GetOperation", request_serializer=operations_pb2.GetOperationRequest.SerializeToString, response_deserializer=operations_pb2.Operation.FromString, @@ -533,7 +661,7 @@ def list_operations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_operations" not in self._stubs: - self._stubs["list_operations"] = self.grpc_channel.unary_unary( + self._stubs["list_operations"] = self._logged_channel.unary_unary( "/google.longrunning.Operations/ListOperations", request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, response_deserializer=operations_pb2.ListOperationsResponse.FromString, @@ -552,7 +680,7 @@ def list_locations( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "list_locations" not in self._stubs: - self._stubs["list_locations"] = self.grpc_channel.unary_unary( + self._stubs["list_locations"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/ListLocations", request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, response_deserializer=locations_pb2.ListLocationsResponse.FromString, @@ -569,7 +697,7 @@ def get_location( # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "get_location" not in self._stubs: - self._stubs["get_location"] = self.grpc_channel.unary_unary( + self._stubs["get_location"] = self._logged_channel.unary_unary( "/google.cloud.location.Locations/GetLocation", request_serializer=locations_pb2.GetLocationRequest.SerializeToString, response_deserializer=locations_pb2.Location.FromString, diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_collect_collect_api_data_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_collect_collect_api_data_async.py new file mode 100644 index 000000000000..972dbdbfc894 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_collect_collect_api_data_async.py @@ -0,0 +1,63 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for CollectApiData +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubCollect_CollectApiData_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_collect_api_data(): + # Create a client + client = apihub_v1.ApiHubCollectAsyncClient() + + # Initialize request argument(s) + api_data = apihub_v1.ApiData() + api_data.api_metadata_list.api_metadata.api.display_name = "display_name_value" + + request = apihub_v1.CollectApiDataRequest( + location="location_value", + collection_type="COLLECTION_TYPE_DELETE", + plugin_instance="plugin_instance_value", + action_id="action_id_value", + api_data=api_data, + ) + + # Make the request + operation = client.collect_api_data(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubCollect_CollectApiData_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_api_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_api_async.py new file mode 100644 index 000000000000..21b457011e7d --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_api_async.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for CreateApi +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_CreateApi_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_create_api(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + api = apihub_v1.Api() + api.display_name = "display_name_value" + + request = apihub_v1.CreateApiRequest( + parent="parent_value", + api=api, + ) + + # Make the request + response = await client.create_api(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_CreateApi_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_api_operation_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_api_operation_async.py new file mode 100644 index 000000000000..9ba548e43edc --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_api_operation_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for CreateApiOperation +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_CreateApiOperation_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_create_api_operation(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.CreateApiOperationRequest( + parent="parent_value", + ) + + # Make the request + response = await client.create_api_operation(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_CreateApiOperation_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_attribute_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_attribute_async.py new file mode 100644 index 000000000000..3de2f7a58e49 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_attribute_async.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for CreateAttribute +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_CreateAttribute_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_create_attribute(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + attribute = apihub_v1.Attribute() + attribute.display_name = "display_name_value" + attribute.scope = "PLUGIN" + attribute.data_type = "URI" + + request = apihub_v1.CreateAttributeRequest( + parent="parent_value", + attribute=attribute, + ) + + # Make the request + response = await client.create_attribute(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_CreateAttribute_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_deployment_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_deployment_async.py new file mode 100644 index 000000000000..1f11d1407560 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_deployment_async.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for CreateDeployment +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_CreateDeployment_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_create_deployment(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + deployment = apihub_v1.Deployment() + deployment.display_name = "display_name_value" + deployment.deployment_type.enum_values.values.id = "id_value" + deployment.deployment_type.enum_values.values.display_name = "display_name_value" + deployment.resource_uri = "resource_uri_value" + deployment.endpoints = ['endpoints_value1', 'endpoints_value2'] + + request = apihub_v1.CreateDeploymentRequest( + parent="parent_value", + deployment=deployment, + ) + + # Make the request + response = await client.create_deployment(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_CreateDeployment_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_external_api_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_external_api_async.py new file mode 100644 index 000000000000..ebafaf427d73 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_external_api_async.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for CreateExternalApi +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_CreateExternalApi_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_create_external_api(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + external_api = apihub_v1.ExternalApi() + external_api.display_name = "display_name_value" + + request = apihub_v1.CreateExternalApiRequest( + parent="parent_value", + external_api=external_api, + ) + + # Make the request + response = await client.create_external_api(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_CreateExternalApi_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_spec_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_spec_async.py new file mode 100644 index 000000000000..813c2c66053e --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_spec_async.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for CreateSpec +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_CreateSpec_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_create_spec(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + spec = apihub_v1.Spec() + spec.display_name = "display_name_value" + spec.spec_type.enum_values.values.id = "id_value" + spec.spec_type.enum_values.values.display_name = "display_name_value" + + request = apihub_v1.CreateSpecRequest( + parent="parent_value", + spec=spec, + ) + + # Make the request + response = await client.create_spec(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_CreateSpec_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_version_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_version_async.py new file mode 100644 index 000000000000..811e28e9a782 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_create_version_async.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for CreateVersion +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_CreateVersion_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_create_version(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + version = apihub_v1.Version() + version.display_name = "display_name_value" + + request = apihub_v1.CreateVersionRequest( + parent="parent_value", + version=version, + ) + + # Make the request + response = await client.create_version(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_CreateVersion_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_curate_create_curation_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_curate_create_curation_async.py new file mode 100644 index 000000000000..be6b5ec36436 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_curate_create_curation_async.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for CreateCuration +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubCurate_CreateCuration_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_create_curation(): + # Create a client + client = apihub_v1.ApiHubCurateAsyncClient() + + # Initialize request argument(s) + curation = apihub_v1.Curation() + curation.display_name = "display_name_value" + curation.endpoint.application_integration_endpoint_details.uri = "uri_value" + curation.endpoint.application_integration_endpoint_details.trigger_id = "trigger_id_value" + + request = apihub_v1.CreateCurationRequest( + parent="parent_value", + curation=curation, + ) + + # Make the request + response = await client.create_curation(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubCurate_CreateCuration_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_curate_delete_curation_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_curate_delete_curation_async.py new file mode 100644 index 000000000000..fe8be6827ee9 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_curate_delete_curation_async.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for DeleteCuration +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubCurate_DeleteCuration_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_delete_curation(): + # Create a client + client = apihub_v1.ApiHubCurateAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DeleteCurationRequest( + name="name_value", + ) + + # Make the request + await client.delete_curation(request=request) + + +# [END apihub_v1_generated_ApiHubCurate_DeleteCuration_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_curate_get_curation_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_curate_get_curation_async.py new file mode 100644 index 000000000000..a297a1e9aa11 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_curate_get_curation_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetCuration +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubCurate_GetCuration_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_get_curation(): + # Create a client + client = apihub_v1.ApiHubCurateAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetCurationRequest( + name="name_value", + ) + + # Make the request + response = await client.get_curation(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubCurate_GetCuration_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_curate_list_curations_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_curate_list_curations_async.py new file mode 100644 index 000000000000..a43764b31c47 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_curate_list_curations_async.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for ListCurations +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubCurate_ListCurations_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_list_curations(): + # Create a client + client = apihub_v1.ApiHubCurateAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.ListCurationsRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_curations(request=request) + + # Handle the response + async for response in page_result: + print(response) + +# [END apihub_v1_generated_ApiHubCurate_ListCurations_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_curate_update_curation_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_curate_update_curation_async.py new file mode 100644 index 000000000000..2b9af815ea42 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_curate_update_curation_async.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for UpdateCuration +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubCurate_UpdateCuration_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_update_curation(): + # Create a client + client = apihub_v1.ApiHubCurateAsyncClient() + + # Initialize request argument(s) + curation = apihub_v1.Curation() + curation.display_name = "display_name_value" + curation.endpoint.application_integration_endpoint_details.uri = "uri_value" + curation.endpoint.application_integration_endpoint_details.trigger_id = "trigger_id_value" + + request = apihub_v1.UpdateCurationRequest( + curation=curation, + ) + + # Make the request + response = await client.update_curation(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubCurate_UpdateCuration_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_api_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_api_async.py new file mode 100644 index 000000000000..a4ccf1729e8f --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_api_async.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for DeleteApi +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_DeleteApi_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_delete_api(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DeleteApiRequest( + name="name_value", + ) + + # Make the request + await client.delete_api(request=request) + + +# [END apihub_v1_generated_ApiHub_DeleteApi_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_api_operation_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_api_operation_async.py new file mode 100644 index 000000000000..412b86e7044f --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_api_operation_async.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for DeleteApiOperation +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_DeleteApiOperation_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_delete_api_operation(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DeleteApiOperationRequest( + name="name_value", + ) + + # Make the request + await client.delete_api_operation(request=request) + + +# [END apihub_v1_generated_ApiHub_DeleteApiOperation_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_attribute_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_attribute_async.py new file mode 100644 index 000000000000..6accc35dbf74 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_attribute_async.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for DeleteAttribute +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_DeleteAttribute_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_delete_attribute(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DeleteAttributeRequest( + name="name_value", + ) + + # Make the request + await client.delete_attribute(request=request) + + +# [END apihub_v1_generated_ApiHub_DeleteAttribute_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_deployment_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_deployment_async.py new file mode 100644 index 000000000000..2373a0ea8b71 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_deployment_async.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for DeleteDeployment +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_DeleteDeployment_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_delete_deployment(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DeleteDeploymentRequest( + name="name_value", + ) + + # Make the request + await client.delete_deployment(request=request) + + +# [END apihub_v1_generated_ApiHub_DeleteDeployment_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_external_api_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_external_api_async.py new file mode 100644 index 000000000000..4c2b2fef485e --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_external_api_async.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for DeleteExternalApi +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_DeleteExternalApi_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_delete_external_api(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DeleteExternalApiRequest( + name="name_value", + ) + + # Make the request + await client.delete_external_api(request=request) + + +# [END apihub_v1_generated_ApiHub_DeleteExternalApi_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_spec_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_spec_async.py new file mode 100644 index 000000000000..4ce8c88e5663 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_spec_async.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for DeleteSpec +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_DeleteSpec_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_delete_spec(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DeleteSpecRequest( + name="name_value", + ) + + # Make the request + await client.delete_spec(request=request) + + +# [END apihub_v1_generated_ApiHub_DeleteSpec_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_version_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_version_async.py new file mode 100644 index 000000000000..0e24644eb69d --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_delete_version_async.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for DeleteVersion +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_DeleteVersion_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_delete_version(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DeleteVersionRequest( + name="name_value", + ) + + # Make the request + await client.delete_version(request=request) + + +# [END apihub_v1_generated_ApiHub_DeleteVersion_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_dependencies_create_dependency_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_dependencies_create_dependency_async.py new file mode 100644 index 000000000000..1c3c5a6c5b4c --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_dependencies_create_dependency_async.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for CreateDependency +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubDependencies_CreateDependency_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_create_dependency(): + # Create a client + client = apihub_v1.ApiHubDependenciesAsyncClient() + + # Initialize request argument(s) + dependency = apihub_v1.Dependency() + dependency.consumer.operation_resource_name = "operation_resource_name_value" + dependency.supplier.operation_resource_name = "operation_resource_name_value" + + request = apihub_v1.CreateDependencyRequest( + parent="parent_value", + dependency=dependency, + ) + + # Make the request + response = await client.create_dependency(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubDependencies_CreateDependency_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_dependencies_delete_dependency_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_dependencies_delete_dependency_async.py new file mode 100644 index 000000000000..e50a42e02fc1 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_dependencies_delete_dependency_async.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for DeleteDependency +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubDependencies_DeleteDependency_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_delete_dependency(): + # Create a client + client = apihub_v1.ApiHubDependenciesAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DeleteDependencyRequest( + name="name_value", + ) + + # Make the request + await client.delete_dependency(request=request) + + +# [END apihub_v1_generated_ApiHubDependencies_DeleteDependency_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_dependencies_get_dependency_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_dependencies_get_dependency_async.py new file mode 100644 index 000000000000..1c3c9e0c460b --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_dependencies_get_dependency_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetDependency +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubDependencies_GetDependency_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_get_dependency(): + # Create a client + client = apihub_v1.ApiHubDependenciesAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetDependencyRequest( + name="name_value", + ) + + # Make the request + response = await client.get_dependency(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubDependencies_GetDependency_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_dependencies_list_dependencies_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_dependencies_list_dependencies_async.py new file mode 100644 index 000000000000..46977fd4c6f7 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_dependencies_list_dependencies_async.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for ListDependencies +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubDependencies_ListDependencies_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_list_dependencies(): + # Create a client + client = apihub_v1.ApiHubDependenciesAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.ListDependenciesRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_dependencies(request=request) + + # Handle the response + async for response in page_result: + print(response) + +# [END apihub_v1_generated_ApiHubDependencies_ListDependencies_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_dependencies_update_dependency_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_dependencies_update_dependency_async.py new file mode 100644 index 000000000000..e839f9a26de6 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_dependencies_update_dependency_async.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for UpdateDependency +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubDependencies_UpdateDependency_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_update_dependency(): + # Create a client + client = apihub_v1.ApiHubDependenciesAsyncClient() + + # Initialize request argument(s) + dependency = apihub_v1.Dependency() + dependency.consumer.operation_resource_name = "operation_resource_name_value" + dependency.supplier.operation_resource_name = "operation_resource_name_value" + + request = apihub_v1.UpdateDependencyRequest( + dependency=dependency, + ) + + # Make the request + response = await client.update_dependency(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubDependencies_UpdateDependency_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_discovery_get_discovered_api_observation_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_discovery_get_discovered_api_observation_async.py new file mode 100644 index 000000000000..27b6874952d3 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_discovery_get_discovered_api_observation_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetDiscoveredApiObservation +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubDiscovery_GetDiscoveredApiObservation_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_get_discovered_api_observation(): + # Create a client + client = apihub_v1.ApiHubDiscoveryAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetDiscoveredApiObservationRequest( + name="name_value", + ) + + # Make the request + response = await client.get_discovered_api_observation(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubDiscovery_GetDiscoveredApiObservation_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_discovery_get_discovered_api_operation_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_discovery_get_discovered_api_operation_async.py new file mode 100644 index 000000000000..0968a2205410 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_discovery_get_discovered_api_operation_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetDiscoveredApiOperation +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubDiscovery_GetDiscoveredApiOperation_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_get_discovered_api_operation(): + # Create a client + client = apihub_v1.ApiHubDiscoveryAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetDiscoveredApiOperationRequest( + name="name_value", + ) + + # Make the request + response = await client.get_discovered_api_operation(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubDiscovery_GetDiscoveredApiOperation_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_discovery_list_discovered_api_observations_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_discovery_list_discovered_api_observations_async.py new file mode 100644 index 000000000000..bc584906adae --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_discovery_list_discovered_api_observations_async.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for ListDiscoveredApiObservations +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubDiscovery_ListDiscoveredApiObservations_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_list_discovered_api_observations(): + # Create a client + client = apihub_v1.ApiHubDiscoveryAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.ListDiscoveredApiObservationsRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_discovered_api_observations(request=request) + + # Handle the response + async for response in page_result: + print(response) + +# [END apihub_v1_generated_ApiHubDiscovery_ListDiscoveredApiObservations_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_discovery_list_discovered_api_operations_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_discovery_list_discovered_api_operations_async.py new file mode 100644 index 000000000000..1fc5a3e38db4 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_discovery_list_discovered_api_operations_async.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for ListDiscoveredApiOperations +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubDiscovery_ListDiscoveredApiOperations_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_list_discovered_api_operations(): + # Create a client + client = apihub_v1.ApiHubDiscoveryAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.ListDiscoveredApiOperationsRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_discovered_api_operations(request=request) + + # Handle the response + async for response in page_result: + print(response) + +# [END apihub_v1_generated_ApiHubDiscovery_ListDiscoveredApiOperations_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_api_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_api_async.py new file mode 100644 index 000000000000..91d4548c2f1b --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_api_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetApi +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_GetApi_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_get_api(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetApiRequest( + name="name_value", + ) + + # Make the request + response = await client.get_api(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_GetApi_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_api_operation_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_api_operation_async.py new file mode 100644 index 000000000000..4c9e19b1849b --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_api_operation_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetApiOperation +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_GetApiOperation_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_get_api_operation(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetApiOperationRequest( + name="name_value", + ) + + # Make the request + response = await client.get_api_operation(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_GetApiOperation_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_attribute_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_attribute_async.py new file mode 100644 index 000000000000..0db02286be83 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_attribute_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetAttribute +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_GetAttribute_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_get_attribute(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetAttributeRequest( + name="name_value", + ) + + # Make the request + response = await client.get_attribute(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_GetAttribute_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_definition_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_definition_async.py new file mode 100644 index 000000000000..864ca4964069 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_definition_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetDefinition +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_GetDefinition_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_get_definition(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetDefinitionRequest( + name="name_value", + ) + + # Make the request + response = await client.get_definition(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_GetDefinition_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_deployment_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_deployment_async.py new file mode 100644 index 000000000000..d43455da6b26 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_deployment_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetDeployment +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_GetDeployment_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_get_deployment(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetDeploymentRequest( + name="name_value", + ) + + # Make the request + response = await client.get_deployment(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_GetDeployment_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_external_api_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_external_api_async.py new file mode 100644 index 000000000000..2c98cae36970 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_external_api_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetExternalApi +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_GetExternalApi_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_get_external_api(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetExternalApiRequest( + name="name_value", + ) + + # Make the request + response = await client.get_external_api(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_GetExternalApi_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_spec_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_spec_async.py new file mode 100644 index 000000000000..d1b816fcf9cb --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_spec_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetSpec +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_GetSpec_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_get_spec(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetSpecRequest( + name="name_value", + ) + + # Make the request + response = await client.get_spec(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_GetSpec_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_spec_contents_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_spec_contents_async.py new file mode 100644 index 000000000000..fa72eeef4c17 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_spec_contents_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetSpecContents +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_GetSpecContents_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_get_spec_contents(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetSpecContentsRequest( + name="name_value", + ) + + # Make the request + response = await client.get_spec_contents(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_GetSpecContents_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_version_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_version_async.py new file mode 100644 index 000000000000..e6f857435cf9 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_get_version_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetVersion +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_GetVersion_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_get_version(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetVersionRequest( + name="name_value", + ) + + # Make the request + response = await client.get_version(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_GetVersion_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_api_operations_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_api_operations_async.py new file mode 100644 index 000000000000..7e33f7e1ea49 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_api_operations_async.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for ListApiOperations +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_ListApiOperations_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_list_api_operations(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.ListApiOperationsRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_api_operations(request=request) + + # Handle the response + async for response in page_result: + print(response) + +# [END apihub_v1_generated_ApiHub_ListApiOperations_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_apis_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_apis_async.py new file mode 100644 index 000000000000..04fe1e46a215 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_apis_async.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for ListApis +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_ListApis_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_list_apis(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.ListApisRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_apis(request=request) + + # Handle the response + async for response in page_result: + print(response) + +# [END apihub_v1_generated_ApiHub_ListApis_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_attributes_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_attributes_async.py new file mode 100644 index 000000000000..474614cdea06 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_attributes_async.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for ListAttributes +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_ListAttributes_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_list_attributes(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.ListAttributesRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_attributes(request=request) + + # Handle the response + async for response in page_result: + print(response) + +# [END apihub_v1_generated_ApiHub_ListAttributes_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_deployments_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_deployments_async.py new file mode 100644 index 000000000000..8823958db5b1 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_deployments_async.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for ListDeployments +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_ListDeployments_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_list_deployments(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.ListDeploymentsRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_deployments(request=request) + + # Handle the response + async for response in page_result: + print(response) + +# [END apihub_v1_generated_ApiHub_ListDeployments_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_external_apis_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_external_apis_async.py new file mode 100644 index 000000000000..17f77e948cfe --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_external_apis_async.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for ListExternalApis +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_ListExternalApis_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_list_external_apis(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.ListExternalApisRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_external_apis(request=request) + + # Handle the response + async for response in page_result: + print(response) + +# [END apihub_v1_generated_ApiHub_ListExternalApis_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_specs_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_specs_async.py new file mode 100644 index 000000000000..fdd7388bc72a --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_specs_async.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for ListSpecs +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_ListSpecs_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_list_specs(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.ListSpecsRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_specs(request=request) + + # Handle the response + async for response in page_result: + print(response) + +# [END apihub_v1_generated_ApiHub_ListSpecs_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_versions_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_versions_async.py new file mode 100644 index 000000000000..39f13fe594f1 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_list_versions_async.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for ListVersions +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_ListVersions_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_list_versions(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.ListVersionsRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_versions(request=request) + + # Handle the response + async for response in page_result: + print(response) + +# [END apihub_v1_generated_ApiHub_ListVersions_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_create_plugin_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_create_plugin_async.py new file mode 100644 index 000000000000..bdb7cdd68db7 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_create_plugin_async.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for CreatePlugin +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubPlugin_CreatePlugin_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_create_plugin(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + plugin = apihub_v1.Plugin() + plugin.display_name = "display_name_value" + + request = apihub_v1.CreatePluginRequest( + parent="parent_value", + plugin=plugin, + ) + + # Make the request + response = await client.create_plugin(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubPlugin_CreatePlugin_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_create_plugin_instance_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_create_plugin_instance_async.py new file mode 100644 index 000000000000..0ba0f3488494 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_create_plugin_instance_async.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for CreatePluginInstance +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubPlugin_CreatePluginInstance_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_create_plugin_instance(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + plugin_instance = apihub_v1.PluginInstance() + plugin_instance.display_name = "display_name_value" + plugin_instance.actions.action_id = "action_id_value" + + request = apihub_v1.CreatePluginInstanceRequest( + parent="parent_value", + plugin_instance=plugin_instance, + ) + + # Make the request + operation = client.create_plugin_instance(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubPlugin_CreatePluginInstance_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_delete_plugin_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_delete_plugin_async.py new file mode 100644 index 000000000000..df0c34b72329 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_delete_plugin_async.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for DeletePlugin +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubPlugin_DeletePlugin_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_delete_plugin(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DeletePluginRequest( + name="name_value", + ) + + # Make the request + operation = client.delete_plugin(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubPlugin_DeletePlugin_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_delete_plugin_instance_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_delete_plugin_instance_async.py new file mode 100644 index 000000000000..9ca220cb1547 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_delete_plugin_instance_async.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for DeletePluginInstance +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubPlugin_DeletePluginInstance_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_delete_plugin_instance(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DeletePluginInstanceRequest( + name="name_value", + ) + + # Make the request + operation = client.delete_plugin_instance(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubPlugin_DeletePluginInstance_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_disable_plugin_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_disable_plugin_async.py new file mode 100644 index 000000000000..d1dc7f9cb394 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_disable_plugin_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for DisablePlugin +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubPlugin_DisablePlugin_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_disable_plugin(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DisablePluginRequest( + name="name_value", + ) + + # Make the request + response = await client.disable_plugin(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubPlugin_DisablePlugin_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_disable_plugin_instance_action_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_disable_plugin_instance_action_async.py new file mode 100644 index 000000000000..2ea787cac667 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_disable_plugin_instance_action_async.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for DisablePluginInstanceAction +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubPlugin_DisablePluginInstanceAction_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_disable_plugin_instance_action(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DisablePluginInstanceActionRequest( + name="name_value", + action_id="action_id_value", + ) + + # Make the request + operation = client.disable_plugin_instance_action(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubPlugin_DisablePluginInstanceAction_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_enable_plugin_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_enable_plugin_async.py new file mode 100644 index 000000000000..f5f29bb163be --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_enable_plugin_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for EnablePlugin +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubPlugin_EnablePlugin_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_enable_plugin(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.EnablePluginRequest( + name="name_value", + ) + + # Make the request + response = await client.enable_plugin(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubPlugin_EnablePlugin_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_enable_plugin_instance_action_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_enable_plugin_instance_action_async.py new file mode 100644 index 000000000000..841fc92db8af --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_enable_plugin_instance_action_async.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for EnablePluginInstanceAction +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubPlugin_EnablePluginInstanceAction_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_enable_plugin_instance_action(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.EnablePluginInstanceActionRequest( + name="name_value", + action_id="action_id_value", + ) + + # Make the request + operation = client.enable_plugin_instance_action(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubPlugin_EnablePluginInstanceAction_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_execute_plugin_instance_action_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_execute_plugin_instance_action_async.py new file mode 100644 index 000000000000..a3e2b8e8276f --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_execute_plugin_instance_action_async.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for ExecutePluginInstanceAction +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubPlugin_ExecutePluginInstanceAction_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_execute_plugin_instance_action(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + action_execution_detail = apihub_v1.ActionExecutionDetail() + action_execution_detail.action_id = "action_id_value" + + request = apihub_v1.ExecutePluginInstanceActionRequest( + name="name_value", + action_execution_detail=action_execution_detail, + ) + + # Make the request + operation = client.execute_plugin_instance_action(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubPlugin_ExecutePluginInstanceAction_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_get_plugin_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_get_plugin_async.py new file mode 100644 index 000000000000..9b24bb05131c --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_get_plugin_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetPlugin +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubPlugin_GetPlugin_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_get_plugin(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetPluginRequest( + name="name_value", + ) + + # Make the request + response = await client.get_plugin(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubPlugin_GetPlugin_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_get_plugin_instance_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_get_plugin_instance_async.py new file mode 100644 index 000000000000..a1390299ba7c --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_get_plugin_instance_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetPluginInstance +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubPlugin_GetPluginInstance_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_get_plugin_instance(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetPluginInstanceRequest( + name="name_value", + ) + + # Make the request + response = await client.get_plugin_instance(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubPlugin_GetPluginInstance_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_list_plugin_instances_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_list_plugin_instances_async.py new file mode 100644 index 000000000000..6c28c268f9a3 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_list_plugin_instances_async.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for ListPluginInstances +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubPlugin_ListPluginInstances_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_list_plugin_instances(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.ListPluginInstancesRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_plugin_instances(request=request) + + # Handle the response + async for response in page_result: + print(response) + +# [END apihub_v1_generated_ApiHubPlugin_ListPluginInstances_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_list_plugins_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_list_plugins_async.py new file mode 100644 index 000000000000..48dda7b3a385 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_list_plugins_async.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for ListPlugins +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubPlugin_ListPlugins_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_list_plugins(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.ListPluginsRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_plugins(request=request) + + # Handle the response + async for response in page_result: + print(response) + +# [END apihub_v1_generated_ApiHubPlugin_ListPlugins_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_update_plugin_instance_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_update_plugin_instance_async.py new file mode 100644 index 000000000000..f0861fccf312 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_plugin_update_plugin_instance_async.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for UpdatePluginInstance +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHubPlugin_UpdatePluginInstance_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_update_plugin_instance(): + # Create a client + client = apihub_v1.ApiHubPluginAsyncClient() + + # Initialize request argument(s) + plugin_instance = apihub_v1.PluginInstance() + plugin_instance.display_name = "display_name_value" + plugin_instance.actions.action_id = "action_id_value" + + request = apihub_v1.UpdatePluginInstanceRequest( + plugin_instance=plugin_instance, + ) + + # Make the request + response = await client.update_plugin_instance(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHubPlugin_UpdatePluginInstance_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_search_resources_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_search_resources_async.py new file mode 100644 index 000000000000..51a0ee03add7 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_search_resources_async.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for SearchResources +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_SearchResources_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_search_resources(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.SearchResourcesRequest( + location="location_value", + query="query_value", + ) + + # Make the request + page_result = client.search_resources(request=request) + + # Handle the response + async for response in page_result: + print(response) + +# [END apihub_v1_generated_ApiHub_SearchResources_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_api_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_api_async.py new file mode 100644 index 000000000000..76dd59259d2f --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_api_async.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for UpdateApi +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_UpdateApi_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_update_api(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + api = apihub_v1.Api() + api.display_name = "display_name_value" + + request = apihub_v1.UpdateApiRequest( + api=api, + ) + + # Make the request + response = await client.update_api(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_UpdateApi_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_api_operation_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_api_operation_async.py new file mode 100644 index 000000000000..6432b6c08717 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_api_operation_async.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for UpdateApiOperation +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_UpdateApiOperation_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_update_api_operation(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.UpdateApiOperationRequest( + ) + + # Make the request + response = await client.update_api_operation(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_UpdateApiOperation_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_attribute_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_attribute_async.py new file mode 100644 index 000000000000..0f8179e8a3b2 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_attribute_async.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for UpdateAttribute +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_UpdateAttribute_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_update_attribute(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + attribute = apihub_v1.Attribute() + attribute.display_name = "display_name_value" + attribute.scope = "PLUGIN" + attribute.data_type = "URI" + + request = apihub_v1.UpdateAttributeRequest( + attribute=attribute, + ) + + # Make the request + response = await client.update_attribute(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_UpdateAttribute_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_deployment_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_deployment_async.py new file mode 100644 index 000000000000..633e73516b88 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_deployment_async.py @@ -0,0 +1,59 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for UpdateDeployment +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_UpdateDeployment_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_update_deployment(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + deployment = apihub_v1.Deployment() + deployment.display_name = "display_name_value" + deployment.deployment_type.enum_values.values.id = "id_value" + deployment.deployment_type.enum_values.values.display_name = "display_name_value" + deployment.resource_uri = "resource_uri_value" + deployment.endpoints = ['endpoints_value1', 'endpoints_value2'] + + request = apihub_v1.UpdateDeploymentRequest( + deployment=deployment, + ) + + # Make the request + response = await client.update_deployment(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_UpdateDeployment_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_external_api_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_external_api_async.py new file mode 100644 index 000000000000..5c74d4cb1805 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_external_api_async.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for UpdateExternalApi +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_UpdateExternalApi_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_update_external_api(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + external_api = apihub_v1.ExternalApi() + external_api.display_name = "display_name_value" + + request = apihub_v1.UpdateExternalApiRequest( + external_api=external_api, + ) + + # Make the request + response = await client.update_external_api(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_UpdateExternalApi_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_spec_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_spec_async.py new file mode 100644 index 000000000000..dfc1f59ad2b4 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_spec_async.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for UpdateSpec +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_UpdateSpec_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_update_spec(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + spec = apihub_v1.Spec() + spec.display_name = "display_name_value" + spec.spec_type.enum_values.values.id = "id_value" + spec.spec_type.enum_values.values.display_name = "display_name_value" + + request = apihub_v1.UpdateSpecRequest( + spec=spec, + ) + + # Make the request + response = await client.update_spec(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_UpdateSpec_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_version_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_version_async.py new file mode 100644 index 000000000000..f2abf754e920 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_api_hub_update_version_async.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for UpdateVersion +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_ApiHub_UpdateVersion_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_update_version(): + # Create a client + client = apihub_v1.ApiHubAsyncClient() + + # Initialize request argument(s) + version = apihub_v1.Version() + version.display_name = "display_name_value" + + request = apihub_v1.UpdateVersionRequest( + version=version, + ) + + # Make the request + response = await client.update_version(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_ApiHub_UpdateVersion_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_host_project_registration_service_create_host_project_registration_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_host_project_registration_service_create_host_project_registration_async.py new file mode 100644 index 000000000000..c2cbf289fb8c --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_host_project_registration_service_create_host_project_registration_async.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for CreateHostProjectRegistration +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_HostProjectRegistrationService_CreateHostProjectRegistration_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_create_host_project_registration(): + # Create a client + client = apihub_v1.HostProjectRegistrationServiceAsyncClient() + + # Initialize request argument(s) + host_project_registration = apihub_v1.HostProjectRegistration() + host_project_registration.gcp_project = "gcp_project_value" + + request = apihub_v1.CreateHostProjectRegistrationRequest( + parent="parent_value", + host_project_registration_id="host_project_registration_id_value", + host_project_registration=host_project_registration, + ) + + # Make the request + response = await client.create_host_project_registration(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_HostProjectRegistrationService_CreateHostProjectRegistration_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_host_project_registration_service_get_host_project_registration_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_host_project_registration_service_get_host_project_registration_async.py new file mode 100644 index 000000000000..da872fa9df0f --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_host_project_registration_service_get_host_project_registration_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetHostProjectRegistration +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_HostProjectRegistrationService_GetHostProjectRegistration_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_get_host_project_registration(): + # Create a client + client = apihub_v1.HostProjectRegistrationServiceAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetHostProjectRegistrationRequest( + name="name_value", + ) + + # Make the request + response = await client.get_host_project_registration(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_HostProjectRegistrationService_GetHostProjectRegistration_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_host_project_registration_service_list_host_project_registrations_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_host_project_registration_service_list_host_project_registrations_async.py new file mode 100644 index 000000000000..771c3de784a4 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_host_project_registration_service_list_host_project_registrations_async.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for ListHostProjectRegistrations +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_HostProjectRegistrationService_ListHostProjectRegistrations_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_list_host_project_registrations(): + # Create a client + client = apihub_v1.HostProjectRegistrationServiceAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.ListHostProjectRegistrationsRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_host_project_registrations(request=request) + + # Handle the response + async for response in page_result: + print(response) + +# [END apihub_v1_generated_HostProjectRegistrationService_ListHostProjectRegistrations_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_linting_service_get_style_guide_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_linting_service_get_style_guide_async.py new file mode 100644 index 000000000000..f6d984456804 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_linting_service_get_style_guide_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetStyleGuide +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_LintingService_GetStyleGuide_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_get_style_guide(): + # Create a client + client = apihub_v1.LintingServiceAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetStyleGuideRequest( + name="name_value", + ) + + # Make the request + response = await client.get_style_guide(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_LintingService_GetStyleGuide_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_linting_service_get_style_guide_contents_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_linting_service_get_style_guide_contents_async.py new file mode 100644 index 000000000000..a02903615ee3 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_linting_service_get_style_guide_contents_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetStyleGuideContents +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_LintingService_GetStyleGuideContents_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_get_style_guide_contents(): + # Create a client + client = apihub_v1.LintingServiceAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetStyleGuideContentsRequest( + name="name_value", + ) + + # Make the request + response = await client.get_style_guide_contents(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_LintingService_GetStyleGuideContents_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_linting_service_lint_spec_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_linting_service_lint_spec_async.py new file mode 100644 index 000000000000..ac53b47b8bdf --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_linting_service_lint_spec_async.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for LintSpec +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_LintingService_LintSpec_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_lint_spec(): + # Create a client + client = apihub_v1.LintingServiceAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.LintSpecRequest( + name="name_value", + ) + + # Make the request + await client.lint_spec(request=request) + + +# [END apihub_v1_generated_LintingService_LintSpec_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_linting_service_update_style_guide_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_linting_service_update_style_guide_async.py new file mode 100644 index 000000000000..768803fe12c3 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_linting_service_update_style_guide_async.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for UpdateStyleGuide +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_LintingService_UpdateStyleGuide_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_update_style_guide(): + # Create a client + client = apihub_v1.LintingServiceAsyncClient() + + # Initialize request argument(s) + style_guide = apihub_v1.StyleGuide() + style_guide.linter = "OTHER" + style_guide.contents.contents = b'contents_blob' + style_guide.contents.mime_type = "mime_type_value" + + request = apihub_v1.UpdateStyleGuideRequest( + style_guide=style_guide, + ) + + # Make the request + response = await client.update_style_guide(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_LintingService_UpdateStyleGuide_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_provisioning_create_api_hub_instance_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_provisioning_create_api_hub_instance_async.py new file mode 100644 index 000000000000..a6af7953a6bb --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_provisioning_create_api_hub_instance_async.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for CreateApiHubInstance +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_Provisioning_CreateApiHubInstance_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_create_api_hub_instance(): + # Create a client + client = apihub_v1.ProvisioningAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.CreateApiHubInstanceRequest( + parent="parent_value", + ) + + # Make the request + operation = client.create_api_hub_instance(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) + +# [END apihub_v1_generated_Provisioning_CreateApiHubInstance_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_provisioning_delete_api_hub_instance_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_provisioning_delete_api_hub_instance_async.py new file mode 100644 index 000000000000..c43392242489 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_provisioning_delete_api_hub_instance_async.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for DeleteApiHubInstance +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_Provisioning_DeleteApiHubInstance_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_delete_api_hub_instance(): + # Create a client + client = apihub_v1.ProvisioningAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DeleteApiHubInstanceRequest( + name="name_value", + ) + + # Make the request + operation = client.delete_api_hub_instance(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) + +# [END apihub_v1_generated_Provisioning_DeleteApiHubInstance_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_provisioning_get_api_hub_instance_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_provisioning_get_api_hub_instance_async.py new file mode 100644 index 000000000000..4cf9ce6c9ff5 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_provisioning_get_api_hub_instance_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetApiHubInstance +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_Provisioning_GetApiHubInstance_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_get_api_hub_instance(): + # Create a client + client = apihub_v1.ProvisioningAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetApiHubInstanceRequest( + name="name_value", + ) + + # Make the request + response = await client.get_api_hub_instance(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_Provisioning_GetApiHubInstance_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_provisioning_lookup_api_hub_instance_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_provisioning_lookup_api_hub_instance_async.py new file mode 100644 index 000000000000..915605fceb00 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_provisioning_lookup_api_hub_instance_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for LookupApiHubInstance +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_Provisioning_LookupApiHubInstance_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_lookup_api_hub_instance(): + # Create a client + client = apihub_v1.ProvisioningAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.LookupApiHubInstanceRequest( + parent="parent_value", + ) + + # Make the request + response = await client.lookup_api_hub_instance(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_Provisioning_LookupApiHubInstance_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_runtime_project_attachment_service_create_runtime_project_attachment_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_runtime_project_attachment_service_create_runtime_project_attachment_async.py new file mode 100644 index 000000000000..79801a00cc53 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_runtime_project_attachment_service_create_runtime_project_attachment_async.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for CreateRuntimeProjectAttachment +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_RuntimeProjectAttachmentService_CreateRuntimeProjectAttachment_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_create_runtime_project_attachment(): + # Create a client + client = apihub_v1.RuntimeProjectAttachmentServiceAsyncClient() + + # Initialize request argument(s) + runtime_project_attachment = apihub_v1.RuntimeProjectAttachment() + runtime_project_attachment.runtime_project = "runtime_project_value" + + request = apihub_v1.CreateRuntimeProjectAttachmentRequest( + parent="parent_value", + runtime_project_attachment_id="runtime_project_attachment_id_value", + runtime_project_attachment=runtime_project_attachment, + ) + + # Make the request + response = await client.create_runtime_project_attachment(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_RuntimeProjectAttachmentService_CreateRuntimeProjectAttachment_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_runtime_project_attachment_service_delete_runtime_project_attachment_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_runtime_project_attachment_service_delete_runtime_project_attachment_async.py new file mode 100644 index 000000000000..22e09a1874bb --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_runtime_project_attachment_service_delete_runtime_project_attachment_async.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for DeleteRuntimeProjectAttachment +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_RuntimeProjectAttachmentService_DeleteRuntimeProjectAttachment_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_delete_runtime_project_attachment(): + # Create a client + client = apihub_v1.RuntimeProjectAttachmentServiceAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.DeleteRuntimeProjectAttachmentRequest( + name="name_value", + ) + + # Make the request + await client.delete_runtime_project_attachment(request=request) + + +# [END apihub_v1_generated_RuntimeProjectAttachmentService_DeleteRuntimeProjectAttachment_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_runtime_project_attachment_service_get_runtime_project_attachment_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_runtime_project_attachment_service_get_runtime_project_attachment_async.py new file mode 100644 index 000000000000..ab0aff798d1a --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_runtime_project_attachment_service_get_runtime_project_attachment_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetRuntimeProjectAttachment +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_RuntimeProjectAttachmentService_GetRuntimeProjectAttachment_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_get_runtime_project_attachment(): + # Create a client + client = apihub_v1.RuntimeProjectAttachmentServiceAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.GetRuntimeProjectAttachmentRequest( + name="name_value", + ) + + # Make the request + response = await client.get_runtime_project_attachment(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_RuntimeProjectAttachmentService_GetRuntimeProjectAttachment_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_runtime_project_attachment_service_list_runtime_project_attachments_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_runtime_project_attachment_service_list_runtime_project_attachments_async.py new file mode 100644 index 000000000000..84452129969d --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_runtime_project_attachment_service_list_runtime_project_attachments_async.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for ListRuntimeProjectAttachments +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_RuntimeProjectAttachmentService_ListRuntimeProjectAttachments_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_list_runtime_project_attachments(): + # Create a client + client = apihub_v1.RuntimeProjectAttachmentServiceAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.ListRuntimeProjectAttachmentsRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_runtime_project_attachments(request=request) + + # Handle the response + async for response in page_result: + print(response) + +# [END apihub_v1_generated_RuntimeProjectAttachmentService_ListRuntimeProjectAttachments_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_runtime_project_attachment_service_lookup_runtime_project_attachment_async.py b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_runtime_project_attachment_service_lookup_runtime_project_attachment_async.py new file mode 100644 index 000000000000..862f70429b33 --- /dev/null +++ b/packages/google-cloud-apihub/samples/generated_samples/apihub_v1_generated_runtime_project_attachment_service_lookup_runtime_project_attachment_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 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. +# +# Generated code. DO NOT EDIT! +# +# Snippet for LookupRuntimeProjectAttachment +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-apihub + + +# [START apihub_v1_generated_RuntimeProjectAttachmentService_LookupRuntimeProjectAttachment_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import apihub_v1 + + +async def sample_lookup_runtime_project_attachment(): + # Create a client + client = apihub_v1.RuntimeProjectAttachmentServiceAsyncClient() + + # Initialize request argument(s) + request = apihub_v1.LookupRuntimeProjectAttachmentRequest( + name="name_value", + ) + + # Make the request + response = await client.lookup_runtime_project_attachment(request=request) + + # Handle the response + print(response) + +# [END apihub_v1_generated_RuntimeProjectAttachmentService_LookupRuntimeProjectAttachment_async] diff --git a/packages/google-cloud-apihub/samples/generated_samples/snippet_metadata_google.cloud.apihub.v1.json b/packages/google-cloud-apihub/samples/generated_samples/snippet_metadata_google.cloud.apihub.v1.json index 543836af9558..3bd5c6e4bb59 100644 --- a/packages/google-cloud-apihub/samples/generated_samples/snippet_metadata_google.cloud.apihub.v1.json +++ b/packages/google-cloud-apihub/samples/generated_samples/snippet_metadata_google.cloud.apihub.v1.json @@ -8,9 +8,98 @@ ], "language": "PYTHON", "name": "google-cloud-apihub", - "version": "0.2.7" + "version": "0.1.0" }, "snippets": [ + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubCollectAsyncClient", + "shortName": "ApiHubCollectAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubCollectAsyncClient.collect_api_data", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHubCollect.CollectApiData", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHubCollect", + "shortName": "ApiHubCollect" + }, + "shortName": "CollectApiData" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.CollectApiDataRequest" + }, + { + "name": "location", + "type": "str" + }, + { + "name": "collection_type", + "type": "google.cloud.apihub_v1.types.CollectionType" + }, + { + "name": "api_data", + "type": "google.cloud.apihub_v1.types.ApiData" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.api_core.operation_async.AsyncOperation", + "shortName": "collect_api_data" + }, + "description": "Sample for CollectApiData", + "file": "apihub_v1_generated_api_hub_collect_collect_api_data_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHubCollect_CollectApiData_async", + "segments": [ + { + "end": 62, + "start": 27, + "type": "FULL" + }, + { + "end": 62, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 52, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 59, + "start": 53, + "type": "REQUEST_EXECUTION" + }, + { + "end": 63, + "start": 60, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_collect_collect_api_data_async.py" + }, { "canonical": true, "clientMethod": { @@ -102,11 +191,12 @@ { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubCurateClient", - "shortName": "ApiHubCurateClient" + "fullName": "google.cloud.apihub_v1.ApiHubCurateAsyncClient", + "shortName": "ApiHubCurateAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubCurateClient.create_curation", + "fullName": "google.cloud.apihub_v1.ApiHubCurateAsyncClient.create_curation", "method": { "fullName": "google.cloud.apihub.v1.ApiHubCurate.CreateCuration", "service": { @@ -149,10 +239,10 @@ "shortName": "create_curation" }, "description": "Sample for CreateCuration", - "file": "apihub_v1_generated_api_hub_curate_create_curation_sync.py", + "file": "apihub_v1_generated_api_hub_curate_create_curation_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubCurate_CreateCuration_sync", + "regionTag": "apihub_v1_generated_ApiHubCurate_CreateCuration_async", "segments": [ { "end": 57, @@ -185,7 +275,7 @@ "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_curate_create_curation_sync.py" + "title": "apihub_v1_generated_api_hub_curate_create_curation_async.py" }, { "canonical": true, @@ -194,22 +284,30 @@ "fullName": "google.cloud.apihub_v1.ApiHubCurateClient", "shortName": "ApiHubCurateClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubCurateClient.delete_curation", + "fullName": "google.cloud.apihub_v1.ApiHubCurateClient.create_curation", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubCurate.DeleteCuration", + "fullName": "google.cloud.apihub.v1.ApiHubCurate.CreateCuration", "service": { "fullName": "google.cloud.apihub.v1.ApiHubCurate", "shortName": "ApiHubCurate" }, - "shortName": "DeleteCuration" + "shortName": "CreateCuration" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.DeleteCurationRequest" + "type": "google.cloud.apihub_v1.types.CreateCurationRequest" }, { - "name": "name", + "name": "parent", + "type": "str" + }, + { + "name": "curation", + "type": "google.cloud.apihub_v1.types.Curation" + }, + { + "name": "curation_id", "type": "str" }, { @@ -225,21 +323,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "shortName": "delete_curation" + "resultType": "google.cloud.apihub_v1.types.Curation", + "shortName": "create_curation" }, - "description": "Sample for DeleteCuration", - "file": "apihub_v1_generated_api_hub_curate_delete_curation_sync.py", + "description": "Sample for CreateCuration", + "file": "apihub_v1_generated_api_hub_curate_create_curation_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubCurate_DeleteCuration_sync", + "regionTag": "apihub_v1_generated_ApiHubCurate_CreateCuration_sync", "segments": [ { - "end": 49, + "end": 57, "start": 27, "type": "FULL" }, { - "end": 49, + "end": 57, "start": 27, "type": "SHORT" }, @@ -249,41 +348,44 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 45, + "end": 51, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "start": 46, + "end": 54, + "start": 52, "type": "REQUEST_EXECUTION" }, { - "end": 50, + "end": 58, + "start": 55, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_curate_delete_curation_sync.py" + "title": "apihub_v1_generated_api_hub_curate_create_curation_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubCurateClient", - "shortName": "ApiHubCurateClient" + "fullName": "google.cloud.apihub_v1.ApiHubCurateAsyncClient", + "shortName": "ApiHubCurateAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubCurateClient.get_curation", + "fullName": "google.cloud.apihub_v1.ApiHubCurateAsyncClient.delete_curation", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubCurate.GetCuration", + "fullName": "google.cloud.apihub.v1.ApiHubCurate.DeleteCuration", "service": { "fullName": "google.cloud.apihub.v1.ApiHubCurate", "shortName": "ApiHubCurate" }, - "shortName": "GetCuration" + "shortName": "DeleteCuration" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.GetCurationRequest" + "type": "google.cloud.apihub_v1.types.DeleteCurationRequest" }, { "name": "name", @@ -302,22 +404,21 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Curation", - "shortName": "get_curation" + "shortName": "delete_curation" }, - "description": "Sample for GetCuration", - "file": "apihub_v1_generated_api_hub_curate_get_curation_sync.py", + "description": "Sample for DeleteCuration", + "file": "apihub_v1_generated_api_hub_curate_delete_curation_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubCurate_GetCuration_sync", + "regionTag": "apihub_v1_generated_ApiHubCurate_DeleteCuration_async", "segments": [ { - "end": 51, + "end": 49, "start": 27, "type": "FULL" }, { - "end": 51, + "end": 49, "start": 27, "type": "SHORT" }, @@ -332,17 +433,15 @@ "type": "REQUEST_INITIALIZATION" }, { - "end": 48, "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 52, - "start": 49, + "end": 50, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_curate_get_curation_sync.py" + "title": "apihub_v1_generated_api_hub_curate_delete_curation_async.py" }, { "canonical": true, @@ -351,22 +450,22 @@ "fullName": "google.cloud.apihub_v1.ApiHubCurateClient", "shortName": "ApiHubCurateClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubCurateClient.list_curations", + "fullName": "google.cloud.apihub_v1.ApiHubCurateClient.delete_curation", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubCurate.ListCurations", + "fullName": "google.cloud.apihub.v1.ApiHubCurate.DeleteCuration", "service": { "fullName": "google.cloud.apihub.v1.ApiHubCurate", "shortName": "ApiHubCurate" }, - "shortName": "ListCurations" + "shortName": "DeleteCuration" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.ListCurationsRequest" + "type": "google.cloud.apihub_v1.types.DeleteCurationRequest" }, { - "name": "parent", + "name": "name", "type": "str" }, { @@ -382,22 +481,21 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.services.api_hub_curate.pagers.ListCurationsPager", - "shortName": "list_curations" + "shortName": "delete_curation" }, - "description": "Sample for ListCurations", - "file": "apihub_v1_generated_api_hub_curate_list_curations_sync.py", + "description": "Sample for DeleteCuration", + "file": "apihub_v1_generated_api_hub_curate_delete_curation_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubCurate_ListCurations_sync", + "regionTag": "apihub_v1_generated_ApiHubCurate_DeleteCuration_sync", "segments": [ { - "end": 52, + "end": 49, "start": 27, "type": "FULL" }, { - "end": 52, + "end": 49, "start": 27, "type": "SHORT" }, @@ -412,46 +510,41 @@ "type": "REQUEST_INITIALIZATION" }, { - "end": 48, "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 53, - "start": 49, + "end": 50, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_curate_list_curations_sync.py" + "title": "apihub_v1_generated_api_hub_curate_delete_curation_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubCurateClient", - "shortName": "ApiHubCurateClient" + "fullName": "google.cloud.apihub_v1.ApiHubCurateAsyncClient", + "shortName": "ApiHubCurateAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubCurateClient.update_curation", + "fullName": "google.cloud.apihub_v1.ApiHubCurateAsyncClient.get_curation", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubCurate.UpdateCuration", + "fullName": "google.cloud.apihub.v1.ApiHubCurate.GetCuration", "service": { "fullName": "google.cloud.apihub.v1.ApiHubCurate", "shortName": "ApiHubCurate" }, - "shortName": "UpdateCuration" + "shortName": "GetCuration" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.UpdateCurationRequest" - }, - { - "name": "curation", - "type": "google.cloud.apihub_v1.types.Curation" + "type": "google.cloud.apihub_v1.types.GetCurationRequest" }, { - "name": "update_mask", - "type": "google.protobuf.field_mask_pb2.FieldMask" + "name": "name", + "type": "str" }, { "name": "retry", @@ -467,21 +560,21 @@ } ], "resultType": "google.cloud.apihub_v1.types.Curation", - "shortName": "update_curation" + "shortName": "get_curation" }, - "description": "Sample for UpdateCuration", - "file": "apihub_v1_generated_api_hub_curate_update_curation_sync.py", + "description": "Sample for GetCuration", + "file": "apihub_v1_generated_api_hub_curate_get_curation_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubCurate_UpdateCuration_sync", + "regionTag": "apihub_v1_generated_ApiHubCurate_GetCuration_async", "segments": [ { - "end": 56, + "end": 51, "start": 27, "type": "FULL" }, { - "end": 56, + "end": 51, "start": 27, "type": "SHORT" }, @@ -491,54 +584,46 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 50, + "end": 45, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 53, - "start": 51, + "end": 48, + "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 57, - "start": 54, + "end": 52, + "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_curate_update_curation_sync.py" + "title": "apihub_v1_generated_api_hub_curate_get_curation_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubDependenciesClient", - "shortName": "ApiHubDependenciesClient" + "fullName": "google.cloud.apihub_v1.ApiHubCurateClient", + "shortName": "ApiHubCurateClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubDependenciesClient.create_dependency", + "fullName": "google.cloud.apihub_v1.ApiHubCurateClient.get_curation", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubDependencies.CreateDependency", + "fullName": "google.cloud.apihub.v1.ApiHubCurate.GetCuration", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubDependencies", - "shortName": "ApiHubDependencies" + "fullName": "google.cloud.apihub.v1.ApiHubCurate", + "shortName": "ApiHubCurate" }, - "shortName": "CreateDependency" + "shortName": "GetCuration" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.CreateDependencyRequest" - }, - { - "name": "parent", - "type": "str" - }, - { - "name": "dependency", - "type": "google.cloud.apihub_v1.types.Dependency" + "type": "google.cloud.apihub_v1.types.GetCurationRequest" }, { - "name": "dependency_id", + "name": "name", "type": "str" }, { @@ -554,22 +639,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Dependency", - "shortName": "create_dependency" + "resultType": "google.cloud.apihub_v1.types.Curation", + "shortName": "get_curation" }, - "description": "Sample for CreateDependency", - "file": "apihub_v1_generated_api_hub_dependencies_create_dependency_sync.py", + "description": "Sample for GetCuration", + "file": "apihub_v1_generated_api_hub_curate_get_curation_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubDependencies_CreateDependency_sync", + "regionTag": "apihub_v1_generated_ApiHubCurate_GetCuration_sync", "segments": [ { - "end": 56, + "end": 51, "start": 27, "type": "FULL" }, { - "end": 56, + "end": 51, "start": 27, "type": "SHORT" }, @@ -579,46 +664,47 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 50, + "end": 45, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 53, - "start": 51, + "end": 48, + "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 57, - "start": 54, + "end": 52, + "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_dependencies_create_dependency_sync.py" + "title": "apihub_v1_generated_api_hub_curate_get_curation_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubDependenciesClient", - "shortName": "ApiHubDependenciesClient" + "fullName": "google.cloud.apihub_v1.ApiHubCurateAsyncClient", + "shortName": "ApiHubCurateAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubDependenciesClient.delete_dependency", + "fullName": "google.cloud.apihub_v1.ApiHubCurateAsyncClient.list_curations", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubDependencies.DeleteDependency", + "fullName": "google.cloud.apihub.v1.ApiHubCurate.ListCurations", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubDependencies", - "shortName": "ApiHubDependencies" + "fullName": "google.cloud.apihub.v1.ApiHubCurate", + "shortName": "ApiHubCurate" }, - "shortName": "DeleteDependency" + "shortName": "ListCurations" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.DeleteDependencyRequest" + "type": "google.cloud.apihub_v1.types.ListCurationsRequest" }, { - "name": "name", + "name": "parent", "type": "str" }, { @@ -634,21 +720,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "shortName": "delete_dependency" + "resultType": "google.cloud.apihub_v1.services.api_hub_curate.pagers.ListCurationsAsyncPager", + "shortName": "list_curations" }, - "description": "Sample for DeleteDependency", - "file": "apihub_v1_generated_api_hub_dependencies_delete_dependency_sync.py", + "description": "Sample for ListCurations", + "file": "apihub_v1_generated_api_hub_curate_list_curations_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubDependencies_DeleteDependency_sync", + "regionTag": "apihub_v1_generated_ApiHubCurate_ListCurations_async", "segments": [ { - "end": 49, + "end": 52, "start": 27, "type": "FULL" }, { - "end": 49, + "end": 52, "start": 27, "type": "SHORT" }, @@ -663,39 +750,41 @@ "type": "REQUEST_INITIALIZATION" }, { + "end": 48, "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 50, + "end": 53, + "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_dependencies_delete_dependency_sync.py" + "title": "apihub_v1_generated_api_hub_curate_list_curations_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubDependenciesClient", - "shortName": "ApiHubDependenciesClient" + "fullName": "google.cloud.apihub_v1.ApiHubCurateClient", + "shortName": "ApiHubCurateClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubDependenciesClient.get_dependency", + "fullName": "google.cloud.apihub_v1.ApiHubCurateClient.list_curations", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubDependencies.GetDependency", + "fullName": "google.cloud.apihub.v1.ApiHubCurate.ListCurations", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubDependencies", - "shortName": "ApiHubDependencies" + "fullName": "google.cloud.apihub.v1.ApiHubCurate", + "shortName": "ApiHubCurate" }, - "shortName": "GetDependency" + "shortName": "ListCurations" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.GetDependencyRequest" + "type": "google.cloud.apihub_v1.types.ListCurationsRequest" }, { - "name": "name", + "name": "parent", "type": "str" }, { @@ -711,22 +800,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Dependency", - "shortName": "get_dependency" + "resultType": "google.cloud.apihub_v1.services.api_hub_curate.pagers.ListCurationsPager", + "shortName": "list_curations" }, - "description": "Sample for GetDependency", - "file": "apihub_v1_generated_api_hub_dependencies_get_dependency_sync.py", + "description": "Sample for ListCurations", + "file": "apihub_v1_generated_api_hub_curate_list_curations_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubDependencies_GetDependency_sync", + "regionTag": "apihub_v1_generated_ApiHubCurate_ListCurations_sync", "segments": [ { - "end": 51, + "end": 52, "start": 27, "type": "FULL" }, { - "end": 51, + "end": 52, "start": 27, "type": "SHORT" }, @@ -746,37 +835,42 @@ "type": "REQUEST_EXECUTION" }, { - "end": 52, + "end": 53, "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_dependencies_get_dependency_sync.py" + "title": "apihub_v1_generated_api_hub_curate_list_curations_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubDependenciesClient", - "shortName": "ApiHubDependenciesClient" + "fullName": "google.cloud.apihub_v1.ApiHubCurateAsyncClient", + "shortName": "ApiHubCurateAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubDependenciesClient.list_dependencies", + "fullName": "google.cloud.apihub_v1.ApiHubCurateAsyncClient.update_curation", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubDependencies.ListDependencies", + "fullName": "google.cloud.apihub.v1.ApiHubCurate.UpdateCuration", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubDependencies", - "shortName": "ApiHubDependencies" + "fullName": "google.cloud.apihub.v1.ApiHubCurate", + "shortName": "ApiHubCurate" }, - "shortName": "ListDependencies" + "shortName": "UpdateCuration" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.ListDependenciesRequest" + "type": "google.cloud.apihub_v1.types.UpdateCurationRequest" }, { - "name": "parent", - "type": "str" + "name": "curation", + "type": "google.cloud.apihub_v1.types.Curation" + }, + { + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" }, { "name": "retry", @@ -791,22 +885,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.services.api_hub_dependencies.pagers.ListDependenciesPager", - "shortName": "list_dependencies" + "resultType": "google.cloud.apihub_v1.types.Curation", + "shortName": "update_curation" }, - "description": "Sample for ListDependencies", - "file": "apihub_v1_generated_api_hub_dependencies_list_dependencies_sync.py", + "description": "Sample for UpdateCuration", + "file": "apihub_v1_generated_api_hub_curate_update_curation_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubDependencies_ListDependencies_sync", + "regionTag": "apihub_v1_generated_ApiHubCurate_UpdateCuration_async", "segments": [ { - "end": 52, + "end": 56, "start": 27, "type": "FULL" }, { - "end": 52, + "end": 56, "start": 27, "type": "SHORT" }, @@ -816,47 +910,47 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 45, + "end": 50, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 48, - "start": 46, + "end": 53, + "start": 51, "type": "REQUEST_EXECUTION" }, { - "end": 53, - "start": 49, + "end": 57, + "start": 54, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_dependencies_list_dependencies_sync.py" + "title": "apihub_v1_generated_api_hub_curate_update_curation_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubDependenciesClient", - "shortName": "ApiHubDependenciesClient" + "fullName": "google.cloud.apihub_v1.ApiHubCurateClient", + "shortName": "ApiHubCurateClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubDependenciesClient.update_dependency", + "fullName": "google.cloud.apihub_v1.ApiHubCurateClient.update_curation", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubDependencies.UpdateDependency", + "fullName": "google.cloud.apihub.v1.ApiHubCurate.UpdateCuration", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubDependencies", - "shortName": "ApiHubDependencies" + "fullName": "google.cloud.apihub.v1.ApiHubCurate", + "shortName": "ApiHubCurate" }, - "shortName": "UpdateDependency" + "shortName": "UpdateCuration" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.UpdateDependencyRequest" + "type": "google.cloud.apihub_v1.types.UpdateCurationRequest" }, { - "name": "dependency", - "type": "google.cloud.apihub_v1.types.Dependency" + "name": "curation", + "type": "google.cloud.apihub_v1.types.Curation" }, { "name": "update_mask", @@ -875,22 +969,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Dependency", - "shortName": "update_dependency" + "resultType": "google.cloud.apihub_v1.types.Curation", + "shortName": "update_curation" }, - "description": "Sample for UpdateDependency", - "file": "apihub_v1_generated_api_hub_dependencies_update_dependency_sync.py", + "description": "Sample for UpdateCuration", + "file": "apihub_v1_generated_api_hub_curate_update_curation_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubDependencies_UpdateDependency_sync", + "regionTag": "apihub_v1_generated_ApiHubCurate_UpdateCuration_sync", "segments": [ { - "end": 55, + "end": 56, "start": 27, "type": "FULL" }, { - "end": 55, + "end": 56, "start": 27, "type": "SHORT" }, @@ -900,46 +994,55 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 49, + "end": 50, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 52, - "start": 50, + "end": 53, + "start": 51, "type": "REQUEST_EXECUTION" }, { - "end": 56, - "start": 53, + "end": 57, + "start": 54, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_dependencies_update_dependency_sync.py" + "title": "apihub_v1_generated_api_hub_curate_update_curation_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryClient", - "shortName": "ApiHubDiscoveryClient" + "fullName": "google.cloud.apihub_v1.ApiHubDependenciesAsyncClient", + "shortName": "ApiHubDependenciesAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryClient.get_discovered_api_observation", + "fullName": "google.cloud.apihub_v1.ApiHubDependenciesAsyncClient.create_dependency", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubDiscovery.GetDiscoveredApiObservation", + "fullName": "google.cloud.apihub.v1.ApiHubDependencies.CreateDependency", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubDiscovery", - "shortName": "ApiHubDiscovery" + "fullName": "google.cloud.apihub.v1.ApiHubDependencies", + "shortName": "ApiHubDependencies" }, - "shortName": "GetDiscoveredApiObservation" + "shortName": "CreateDependency" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.GetDiscoveredApiObservationRequest" + "type": "google.cloud.apihub_v1.types.CreateDependencyRequest" }, { - "name": "name", + "name": "parent", + "type": "str" + }, + { + "name": "dependency", + "type": "google.cloud.apihub_v1.types.Dependency" + }, + { + "name": "dependency_id", "type": "str" }, { @@ -955,22 +1058,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.DiscoveredApiObservation", - "shortName": "get_discovered_api_observation" + "resultType": "google.cloud.apihub_v1.types.Dependency", + "shortName": "create_dependency" }, - "description": "Sample for GetDiscoveredApiObservation", - "file": "apihub_v1_generated_api_hub_discovery_get_discovered_api_observation_sync.py", + "description": "Sample for CreateDependency", + "file": "apihub_v1_generated_api_hub_dependencies_create_dependency_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubDiscovery_GetDiscoveredApiObservation_sync", + "regionTag": "apihub_v1_generated_ApiHubDependencies_CreateDependency_async", "segments": [ { - "end": 51, + "end": 56, "start": 27, "type": "FULL" }, { - "end": 51, + "end": 56, "start": 27, "type": "SHORT" }, @@ -980,46 +1083,54 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 45, + "end": 50, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 48, - "start": 46, + "end": 53, + "start": 51, "type": "REQUEST_EXECUTION" }, { - "end": 52, - "start": 49, + "end": 57, + "start": 54, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_discovery_get_discovered_api_observation_sync.py" + "title": "apihub_v1_generated_api_hub_dependencies_create_dependency_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryClient", - "shortName": "ApiHubDiscoveryClient" + "fullName": "google.cloud.apihub_v1.ApiHubDependenciesClient", + "shortName": "ApiHubDependenciesClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryClient.get_discovered_api_operation", + "fullName": "google.cloud.apihub_v1.ApiHubDependenciesClient.create_dependency", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubDiscovery.GetDiscoveredApiOperation", + "fullName": "google.cloud.apihub.v1.ApiHubDependencies.CreateDependency", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubDiscovery", - "shortName": "ApiHubDiscovery" + "fullName": "google.cloud.apihub.v1.ApiHubDependencies", + "shortName": "ApiHubDependencies" }, - "shortName": "GetDiscoveredApiOperation" + "shortName": "CreateDependency" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.GetDiscoveredApiOperationRequest" + "type": "google.cloud.apihub_v1.types.CreateDependencyRequest" }, { - "name": "name", + "name": "parent", + "type": "str" + }, + { + "name": "dependency", + "type": "google.cloud.apihub_v1.types.Dependency" + }, + { + "name": "dependency_id", "type": "str" }, { @@ -1035,22 +1146,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.DiscoveredApiOperation", - "shortName": "get_discovered_api_operation" + "resultType": "google.cloud.apihub_v1.types.Dependency", + "shortName": "create_dependency" }, - "description": "Sample for GetDiscoveredApiOperation", - "file": "apihub_v1_generated_api_hub_discovery_get_discovered_api_operation_sync.py", + "description": "Sample for CreateDependency", + "file": "apihub_v1_generated_api_hub_dependencies_create_dependency_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubDiscovery_GetDiscoveredApiOperation_sync", + "regionTag": "apihub_v1_generated_ApiHubDependencies_CreateDependency_sync", "segments": [ { - "end": 51, + "end": 56, "start": 27, "type": "FULL" }, { - "end": 51, + "end": 56, "start": 27, "type": "SHORT" }, @@ -1060,46 +1171,47 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 45, + "end": 50, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 48, - "start": 46, + "end": 53, + "start": 51, "type": "REQUEST_EXECUTION" }, { - "end": 52, - "start": 49, + "end": 57, + "start": 54, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_discovery_get_discovered_api_operation_sync.py" + "title": "apihub_v1_generated_api_hub_dependencies_create_dependency_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryClient", - "shortName": "ApiHubDiscoveryClient" + "fullName": "google.cloud.apihub_v1.ApiHubDependenciesAsyncClient", + "shortName": "ApiHubDependenciesAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryClient.list_discovered_api_observations", + "fullName": "google.cloud.apihub_v1.ApiHubDependenciesAsyncClient.delete_dependency", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubDiscovery.ListDiscoveredApiObservations", + "fullName": "google.cloud.apihub.v1.ApiHubDependencies.DeleteDependency", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubDiscovery", - "shortName": "ApiHubDiscovery" + "fullName": "google.cloud.apihub.v1.ApiHubDependencies", + "shortName": "ApiHubDependencies" }, - "shortName": "ListDiscoveredApiObservations" + "shortName": "DeleteDependency" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.ListDiscoveredApiObservationsRequest" + "type": "google.cloud.apihub_v1.types.DeleteDependencyRequest" }, { - "name": "parent", + "name": "name", "type": "str" }, { @@ -1115,22 +1227,21 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.services.api_hub_discovery.pagers.ListDiscoveredApiObservationsPager", - "shortName": "list_discovered_api_observations" + "shortName": "delete_dependency" }, - "description": "Sample for ListDiscoveredApiObservations", - "file": "apihub_v1_generated_api_hub_discovery_list_discovered_api_observations_sync.py", + "description": "Sample for DeleteDependency", + "file": "apihub_v1_generated_api_hub_dependencies_delete_dependency_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubDiscovery_ListDiscoveredApiObservations_sync", + "regionTag": "apihub_v1_generated_ApiHubDependencies_DeleteDependency_async", "segments": [ { - "end": 52, + "end": 49, "start": 27, "type": "FULL" }, { - "end": 52, + "end": 49, "start": 27, "type": "SHORT" }, @@ -1145,41 +1256,39 @@ "type": "REQUEST_INITIALIZATION" }, { - "end": 48, "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 53, - "start": 49, + "end": 50, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_discovery_list_discovered_api_observations_sync.py" + "title": "apihub_v1_generated_api_hub_dependencies_delete_dependency_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryClient", - "shortName": "ApiHubDiscoveryClient" + "fullName": "google.cloud.apihub_v1.ApiHubDependenciesClient", + "shortName": "ApiHubDependenciesClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryClient.list_discovered_api_operations", + "fullName": "google.cloud.apihub_v1.ApiHubDependenciesClient.delete_dependency", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubDiscovery.ListDiscoveredApiOperations", + "fullName": "google.cloud.apihub.v1.ApiHubDependencies.DeleteDependency", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubDiscovery", - "shortName": "ApiHubDiscovery" + "fullName": "google.cloud.apihub.v1.ApiHubDependencies", + "shortName": "ApiHubDependencies" }, - "shortName": "ListDiscoveredApiOperations" + "shortName": "DeleteDependency" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.ListDiscoveredApiOperationsRequest" + "type": "google.cloud.apihub_v1.types.DeleteDependencyRequest" }, { - "name": "parent", + "name": "name", "type": "str" }, { @@ -1195,22 +1304,21 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.services.api_hub_discovery.pagers.ListDiscoveredApiOperationsPager", - "shortName": "list_discovered_api_operations" + "shortName": "delete_dependency" }, - "description": "Sample for ListDiscoveredApiOperations", - "file": "apihub_v1_generated_api_hub_discovery_list_discovered_api_operations_sync.py", + "description": "Sample for DeleteDependency", + "file": "apihub_v1_generated_api_hub_dependencies_delete_dependency_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubDiscovery_ListDiscoveredApiOperations_sync", + "regionTag": "apihub_v1_generated_ApiHubDependencies_DeleteDependency_sync", "segments": [ { - "end": 52, + "end": 49, "start": 27, "type": "FULL" }, { - "end": 52, + "end": 49, "start": 27, "type": "SHORT" }, @@ -1225,49 +1333,40 @@ "type": "REQUEST_INITIALIZATION" }, { - "end": 48, "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 53, - "start": 49, + "end": 50, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_discovery_list_discovered_api_operations_sync.py" + "title": "apihub_v1_generated_api_hub_dependencies_delete_dependency_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", - "shortName": "ApiHubPluginClient" + "fullName": "google.cloud.apihub_v1.ApiHubDependenciesAsyncClient", + "shortName": "ApiHubDependenciesAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.create_plugin_instance", + "fullName": "google.cloud.apihub_v1.ApiHubDependenciesAsyncClient.get_dependency", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin.CreatePluginInstance", + "fullName": "google.cloud.apihub.v1.ApiHubDependencies.GetDependency", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin", - "shortName": "ApiHubPlugin" + "fullName": "google.cloud.apihub.v1.ApiHubDependencies", + "shortName": "ApiHubDependencies" }, - "shortName": "CreatePluginInstance" + "shortName": "GetDependency" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.CreatePluginInstanceRequest" - }, - { - "name": "parent", - "type": "str" - }, - { - "name": "plugin_instance", - "type": "google.cloud.apihub_v1.types.PluginInstance" + "type": "google.cloud.apihub_v1.types.GetDependencyRequest" }, { - "name": "plugin_instance_id", + "name": "name", "type": "str" }, { @@ -1283,22 +1382,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.api_core.operation.Operation", - "shortName": "create_plugin_instance" + "resultType": "google.cloud.apihub_v1.types.Dependency", + "shortName": "get_dependency" }, - "description": "Sample for CreatePluginInstance", - "file": "apihub_v1_generated_api_hub_plugin_create_plugin_instance_sync.py", + "description": "Sample for GetDependency", + "file": "apihub_v1_generated_api_hub_dependencies_get_dependency_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubPlugin_CreatePluginInstance_sync", + "regionTag": "apihub_v1_generated_ApiHubDependencies_GetDependency_async", "segments": [ { - "end": 60, + "end": 51, "start": 27, "type": "FULL" }, { - "end": 60, + "end": 51, "start": 27, "type": "SHORT" }, @@ -1308,54 +1407,46 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 50, + "end": 45, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 57, - "start": 51, + "end": 48, + "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 61, - "start": 58, + "end": 52, + "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_plugin_create_plugin_instance_sync.py" + "title": "apihub_v1_generated_api_hub_dependencies_get_dependency_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", - "shortName": "ApiHubPluginClient" + "fullName": "google.cloud.apihub_v1.ApiHubDependenciesClient", + "shortName": "ApiHubDependenciesClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.create_plugin", + "fullName": "google.cloud.apihub_v1.ApiHubDependenciesClient.get_dependency", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin.CreatePlugin", + "fullName": "google.cloud.apihub.v1.ApiHubDependencies.GetDependency", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin", - "shortName": "ApiHubPlugin" + "fullName": "google.cloud.apihub.v1.ApiHubDependencies", + "shortName": "ApiHubDependencies" }, - "shortName": "CreatePlugin" + "shortName": "GetDependency" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.CreatePluginRequest" - }, - { - "name": "parent", - "type": "str" - }, - { - "name": "plugin", - "type": "google.cloud.apihub_v1.types.Plugin" + "type": "google.cloud.apihub_v1.types.GetDependencyRequest" }, { - "name": "plugin_id", + "name": "name", "type": "str" }, { @@ -1371,22 +1462,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Plugin", - "shortName": "create_plugin" + "resultType": "google.cloud.apihub_v1.types.Dependency", + "shortName": "get_dependency" }, - "description": "Sample for CreatePlugin", - "file": "apihub_v1_generated_api_hub_plugin_create_plugin_sync.py", + "description": "Sample for GetDependency", + "file": "apihub_v1_generated_api_hub_dependencies_get_dependency_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubPlugin_CreatePlugin_sync", + "regionTag": "apihub_v1_generated_ApiHubDependencies_GetDependency_sync", "segments": [ { - "end": 55, + "end": 51, "start": 27, "type": "FULL" }, { - "end": 55, + "end": 51, "start": 27, "type": "SHORT" }, @@ -1396,46 +1487,47 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 49, + "end": 45, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 52, - "start": 50, + "end": 48, + "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 56, - "start": 53, + "end": 52, + "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_plugin_create_plugin_sync.py" + "title": "apihub_v1_generated_api_hub_dependencies_get_dependency_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", - "shortName": "ApiHubPluginClient" + "fullName": "google.cloud.apihub_v1.ApiHubDependenciesAsyncClient", + "shortName": "ApiHubDependenciesAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.delete_plugin_instance", + "fullName": "google.cloud.apihub_v1.ApiHubDependenciesAsyncClient.list_dependencies", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin.DeletePluginInstance", + "fullName": "google.cloud.apihub.v1.ApiHubDependencies.ListDependencies", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin", - "shortName": "ApiHubPlugin" + "fullName": "google.cloud.apihub.v1.ApiHubDependencies", + "shortName": "ApiHubDependencies" }, - "shortName": "DeletePluginInstance" + "shortName": "ListDependencies" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.DeletePluginInstanceRequest" + "type": "google.cloud.apihub_v1.types.ListDependenciesRequest" }, { - "name": "name", + "name": "parent", "type": "str" }, { @@ -1451,22 +1543,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.api_core.operation.Operation", - "shortName": "delete_plugin_instance" + "resultType": "google.cloud.apihub_v1.services.api_hub_dependencies.pagers.ListDependenciesAsyncPager", + "shortName": "list_dependencies" }, - "description": "Sample for DeletePluginInstance", - "file": "apihub_v1_generated_api_hub_plugin_delete_plugin_instance_sync.py", + "description": "Sample for ListDependencies", + "file": "apihub_v1_generated_api_hub_dependencies_list_dependencies_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubPlugin_DeletePluginInstance_sync", + "regionTag": "apihub_v1_generated_ApiHubDependencies_ListDependencies_async", "segments": [ { - "end": 55, + "end": 52, "start": 27, "type": "FULL" }, { - "end": 55, + "end": 52, "start": 27, "type": "SHORT" }, @@ -1481,41 +1573,41 @@ "type": "REQUEST_INITIALIZATION" }, { - "end": 52, + "end": 48, "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 56, - "start": 53, + "end": 53, + "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_plugin_delete_plugin_instance_sync.py" + "title": "apihub_v1_generated_api_hub_dependencies_list_dependencies_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", - "shortName": "ApiHubPluginClient" + "fullName": "google.cloud.apihub_v1.ApiHubDependenciesClient", + "shortName": "ApiHubDependenciesClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.delete_plugin", + "fullName": "google.cloud.apihub_v1.ApiHubDependenciesClient.list_dependencies", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin.DeletePlugin", + "fullName": "google.cloud.apihub.v1.ApiHubDependencies.ListDependencies", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin", - "shortName": "ApiHubPlugin" + "fullName": "google.cloud.apihub.v1.ApiHubDependencies", + "shortName": "ApiHubDependencies" }, - "shortName": "DeletePlugin" + "shortName": "ListDependencies" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.DeletePluginRequest" + "type": "google.cloud.apihub_v1.types.ListDependenciesRequest" }, { - "name": "name", + "name": "parent", "type": "str" }, { @@ -1531,22 +1623,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.api_core.operation.Operation", - "shortName": "delete_plugin" + "resultType": "google.cloud.apihub_v1.services.api_hub_dependencies.pagers.ListDependenciesPager", + "shortName": "list_dependencies" }, - "description": "Sample for DeletePlugin", - "file": "apihub_v1_generated_api_hub_plugin_delete_plugin_sync.py", + "description": "Sample for ListDependencies", + "file": "apihub_v1_generated_api_hub_dependencies_list_dependencies_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubPlugin_DeletePlugin_sync", + "regionTag": "apihub_v1_generated_ApiHubDependencies_ListDependencies_sync", "segments": [ { - "end": 55, + "end": 52, "start": 27, "type": "FULL" }, { - "end": 55, + "end": 52, "start": 27, "type": "SHORT" }, @@ -1561,46 +1653,47 @@ "type": "REQUEST_INITIALIZATION" }, { - "end": 52, + "end": 48, "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 56, - "start": 53, + "end": 53, + "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_plugin_delete_plugin_sync.py" + "title": "apihub_v1_generated_api_hub_dependencies_list_dependencies_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", - "shortName": "ApiHubPluginClient" + "fullName": "google.cloud.apihub_v1.ApiHubDependenciesAsyncClient", + "shortName": "ApiHubDependenciesAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.disable_plugin_instance_action", + "fullName": "google.cloud.apihub_v1.ApiHubDependenciesAsyncClient.update_dependency", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin.DisablePluginInstanceAction", + "fullName": "google.cloud.apihub.v1.ApiHubDependencies.UpdateDependency", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin", - "shortName": "ApiHubPlugin" + "fullName": "google.cloud.apihub.v1.ApiHubDependencies", + "shortName": "ApiHubDependencies" }, - "shortName": "DisablePluginInstanceAction" + "shortName": "UpdateDependency" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.DisablePluginInstanceActionRequest" + "type": "google.cloud.apihub_v1.types.UpdateDependencyRequest" }, { - "name": "name", - "type": "str" + "name": "dependency", + "type": "google.cloud.apihub_v1.types.Dependency" }, { - "name": "action_id", - "type": "str" + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" }, { "name": "retry", @@ -1615,22 +1708,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.api_core.operation.Operation", - "shortName": "disable_plugin_instance_action" + "resultType": "google.cloud.apihub_v1.types.Dependency", + "shortName": "update_dependency" }, - "description": "Sample for DisablePluginInstanceAction", - "file": "apihub_v1_generated_api_hub_plugin_disable_plugin_instance_action_sync.py", + "description": "Sample for UpdateDependency", + "file": "apihub_v1_generated_api_hub_dependencies_update_dependency_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubPlugin_DisablePluginInstanceAction_sync", + "regionTag": "apihub_v1_generated_ApiHubDependencies_UpdateDependency_async", "segments": [ { - "end": 56, + "end": 55, "start": 27, "type": "FULL" }, { - "end": 56, + "end": 55, "start": 27, "type": "SHORT" }, @@ -1640,47 +1733,51 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 46, + "end": 49, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 53, - "start": 47, + "end": 52, + "start": 50, "type": "REQUEST_EXECUTION" }, { - "end": 57, - "start": 54, + "end": 56, + "start": 53, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_plugin_disable_plugin_instance_action_sync.py" + "title": "apihub_v1_generated_api_hub_dependencies_update_dependency_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", - "shortName": "ApiHubPluginClient" + "fullName": "google.cloud.apihub_v1.ApiHubDependenciesClient", + "shortName": "ApiHubDependenciesClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.disable_plugin", + "fullName": "google.cloud.apihub_v1.ApiHubDependenciesClient.update_dependency", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin.DisablePlugin", + "fullName": "google.cloud.apihub.v1.ApiHubDependencies.UpdateDependency", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin", - "shortName": "ApiHubPlugin" + "fullName": "google.cloud.apihub.v1.ApiHubDependencies", + "shortName": "ApiHubDependencies" }, - "shortName": "DisablePlugin" + "shortName": "UpdateDependency" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.DisablePluginRequest" + "type": "google.cloud.apihub_v1.types.UpdateDependencyRequest" }, { - "name": "name", - "type": "str" + "name": "dependency", + "type": "google.cloud.apihub_v1.types.Dependency" + }, + { + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" }, { "name": "retry", @@ -1695,22 +1792,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Plugin", - "shortName": "disable_plugin" + "resultType": "google.cloud.apihub_v1.types.Dependency", + "shortName": "update_dependency" }, - "description": "Sample for DisablePlugin", - "file": "apihub_v1_generated_api_hub_plugin_disable_plugin_sync.py", + "description": "Sample for UpdateDependency", + "file": "apihub_v1_generated_api_hub_dependencies_update_dependency_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubPlugin_DisablePlugin_sync", + "regionTag": "apihub_v1_generated_ApiHubDependencies_UpdateDependency_sync", "segments": [ { - "end": 51, + "end": 55, "start": 27, "type": "FULL" }, { - "end": 51, + "end": 55, "start": 27, "type": "SHORT" }, @@ -1720,52 +1817,49 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 45, + "end": 49, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 48, - "start": 46, + "end": 52, + "start": 50, "type": "REQUEST_EXECUTION" }, { - "end": 52, - "start": 49, + "end": 56, + "start": 53, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_plugin_disable_plugin_sync.py" + "title": "apihub_v1_generated_api_hub_dependencies_update_dependency_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", - "shortName": "ApiHubPluginClient" + "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryAsyncClient", + "shortName": "ApiHubDiscoveryAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.enable_plugin_instance_action", + "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryAsyncClient.get_discovered_api_observation", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin.EnablePluginInstanceAction", + "fullName": "google.cloud.apihub.v1.ApiHubDiscovery.GetDiscoveredApiObservation", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin", - "shortName": "ApiHubPlugin" + "fullName": "google.cloud.apihub.v1.ApiHubDiscovery", + "shortName": "ApiHubDiscovery" }, - "shortName": "EnablePluginInstanceAction" + "shortName": "GetDiscoveredApiObservation" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.EnablePluginInstanceActionRequest" + "type": "google.cloud.apihub_v1.types.GetDiscoveredApiObservationRequest" }, { "name": "name", "type": "str" }, - { - "name": "action_id", - "type": "str" - }, { "name": "retry", "type": "google.api_core.retry.Retry" @@ -1779,22 +1873,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.api_core.operation.Operation", - "shortName": "enable_plugin_instance_action" + "resultType": "google.cloud.apihub_v1.types.DiscoveredApiObservation", + "shortName": "get_discovered_api_observation" }, - "description": "Sample for EnablePluginInstanceAction", - "file": "apihub_v1_generated_api_hub_plugin_enable_plugin_instance_action_sync.py", + "description": "Sample for GetDiscoveredApiObservation", + "file": "apihub_v1_generated_api_hub_discovery_get_discovered_api_observation_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubPlugin_EnablePluginInstanceAction_sync", + "regionTag": "apihub_v1_generated_ApiHubDiscovery_GetDiscoveredApiObservation_async", "segments": [ { - "end": 56, + "end": 51, "start": 27, "type": "FULL" }, { - "end": 56, + "end": 51, "start": 27, "type": "SHORT" }, @@ -1804,43 +1898,43 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 46, + "end": 45, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 53, - "start": 47, + "end": 48, + "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 57, - "start": 54, + "end": 52, + "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_plugin_enable_plugin_instance_action_sync.py" + "title": "apihub_v1_generated_api_hub_discovery_get_discovered_api_observation_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", - "shortName": "ApiHubPluginClient" + "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryClient", + "shortName": "ApiHubDiscoveryClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.enable_plugin", + "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryClient.get_discovered_api_observation", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin.EnablePlugin", + "fullName": "google.cloud.apihub.v1.ApiHubDiscovery.GetDiscoveredApiObservation", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin", - "shortName": "ApiHubPlugin" + "fullName": "google.cloud.apihub.v1.ApiHubDiscovery", + "shortName": "ApiHubDiscovery" }, - "shortName": "EnablePlugin" + "shortName": "GetDiscoveredApiObservation" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.EnablePluginRequest" + "type": "google.cloud.apihub_v1.types.GetDiscoveredApiObservationRequest" }, { "name": "name", @@ -1859,14 +1953,14 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Plugin", - "shortName": "enable_plugin" + "resultType": "google.cloud.apihub_v1.types.DiscoveredApiObservation", + "shortName": "get_discovered_api_observation" }, - "description": "Sample for EnablePlugin", - "file": "apihub_v1_generated_api_hub_plugin_enable_plugin_sync.py", + "description": "Sample for GetDiscoveredApiObservation", + "file": "apihub_v1_generated_api_hub_discovery_get_discovered_api_observation_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubPlugin_EnablePlugin_sync", + "regionTag": "apihub_v1_generated_ApiHubDiscovery_GetDiscoveredApiObservation_sync", "segments": [ { "end": 51, @@ -1899,37 +1993,34 @@ "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_plugin_enable_plugin_sync.py" + "title": "apihub_v1_generated_api_hub_discovery_get_discovered_api_observation_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", - "shortName": "ApiHubPluginClient" + "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryAsyncClient", + "shortName": "ApiHubDiscoveryAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.execute_plugin_instance_action", + "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryAsyncClient.get_discovered_api_operation", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin.ExecutePluginInstanceAction", + "fullName": "google.cloud.apihub.v1.ApiHubDiscovery.GetDiscoveredApiOperation", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin", - "shortName": "ApiHubPlugin" + "fullName": "google.cloud.apihub.v1.ApiHubDiscovery", + "shortName": "ApiHubDiscovery" }, - "shortName": "ExecutePluginInstanceAction" + "shortName": "GetDiscoveredApiOperation" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.ExecutePluginInstanceActionRequest" + "type": "google.cloud.apihub_v1.types.GetDiscoveredApiOperationRequest" }, { "name": "name", "type": "str" }, - { - "name": "action_execution_detail", - "type": "google.cloud.apihub_v1.types.ActionExecutionDetail" - }, { "name": "retry", "type": "google.api_core.retry.Retry" @@ -1943,22 +2034,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.api_core.operation.Operation", - "shortName": "execute_plugin_instance_action" + "resultType": "google.cloud.apihub_v1.types.DiscoveredApiOperation", + "shortName": "get_discovered_api_operation" }, - "description": "Sample for ExecutePluginInstanceAction", - "file": "apihub_v1_generated_api_hub_plugin_execute_plugin_instance_action_sync.py", + "description": "Sample for GetDiscoveredApiOperation", + "file": "apihub_v1_generated_api_hub_discovery_get_discovered_api_operation_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubPlugin_ExecutePluginInstanceAction_sync", + "regionTag": "apihub_v1_generated_ApiHubDiscovery_GetDiscoveredApiOperation_async", "segments": [ { - "end": 59, + "end": 51, "start": 27, "type": "FULL" }, { - "end": 59, + "end": 51, "start": 27, "type": "SHORT" }, @@ -1968,43 +2059,43 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 49, + "end": 45, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 56, - "start": 50, + "end": 48, + "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 60, - "start": 57, + "end": 52, + "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_plugin_execute_plugin_instance_action_sync.py" + "title": "apihub_v1_generated_api_hub_discovery_get_discovered_api_operation_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", - "shortName": "ApiHubPluginClient" + "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryClient", + "shortName": "ApiHubDiscoveryClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.get_plugin_instance", + "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryClient.get_discovered_api_operation", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin.GetPluginInstance", + "fullName": "google.cloud.apihub.v1.ApiHubDiscovery.GetDiscoveredApiOperation", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin", - "shortName": "ApiHubPlugin" + "fullName": "google.cloud.apihub.v1.ApiHubDiscovery", + "shortName": "ApiHubDiscovery" }, - "shortName": "GetPluginInstance" + "shortName": "GetDiscoveredApiOperation" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.GetPluginInstanceRequest" + "type": "google.cloud.apihub_v1.types.GetDiscoveredApiOperationRequest" }, { "name": "name", @@ -2023,14 +2114,14 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.PluginInstance", - "shortName": "get_plugin_instance" + "resultType": "google.cloud.apihub_v1.types.DiscoveredApiOperation", + "shortName": "get_discovered_api_operation" }, - "description": "Sample for GetPluginInstance", - "file": "apihub_v1_generated_api_hub_plugin_get_plugin_instance_sync.py", + "description": "Sample for GetDiscoveredApiOperation", + "file": "apihub_v1_generated_api_hub_discovery_get_discovered_api_operation_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubPlugin_GetPluginInstance_sync", + "regionTag": "apihub_v1_generated_ApiHubDiscovery_GetDiscoveredApiOperation_sync", "segments": [ { "end": 51, @@ -2063,31 +2154,32 @@ "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_plugin_get_plugin_instance_sync.py" + "title": "apihub_v1_generated_api_hub_discovery_get_discovered_api_operation_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", - "shortName": "ApiHubPluginClient" + "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryAsyncClient", + "shortName": "ApiHubDiscoveryAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.get_plugin", + "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryAsyncClient.list_discovered_api_observations", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin.GetPlugin", + "fullName": "google.cloud.apihub.v1.ApiHubDiscovery.ListDiscoveredApiObservations", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin", - "shortName": "ApiHubPlugin" + "fullName": "google.cloud.apihub.v1.ApiHubDiscovery", + "shortName": "ApiHubDiscovery" }, - "shortName": "GetPlugin" + "shortName": "ListDiscoveredApiObservations" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.GetPluginRequest" + "type": "google.cloud.apihub_v1.types.ListDiscoveredApiObservationsRequest" }, { - "name": "name", + "name": "parent", "type": "str" }, { @@ -2103,22 +2195,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Plugin", - "shortName": "get_plugin" + "resultType": "google.cloud.apihub_v1.services.api_hub_discovery.pagers.ListDiscoveredApiObservationsAsyncPager", + "shortName": "list_discovered_api_observations" }, - "description": "Sample for GetPlugin", - "file": "apihub_v1_generated_api_hub_plugin_get_plugin_sync.py", + "description": "Sample for ListDiscoveredApiObservations", + "file": "apihub_v1_generated_api_hub_discovery_list_discovered_api_observations_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubPlugin_GetPlugin_sync", + "regionTag": "apihub_v1_generated_ApiHubDiscovery_ListDiscoveredApiObservations_async", "segments": [ { - "end": 51, + "end": 52, "start": 27, "type": "FULL" }, { - "end": 51, + "end": 52, "start": 27, "type": "SHORT" }, @@ -2138,33 +2230,33 @@ "type": "REQUEST_EXECUTION" }, { - "end": 52, + "end": 53, "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_plugin_get_plugin_sync.py" + "title": "apihub_v1_generated_api_hub_discovery_list_discovered_api_observations_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", - "shortName": "ApiHubPluginClient" + "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryClient", + "shortName": "ApiHubDiscoveryClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.list_plugin_instances", + "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryClient.list_discovered_api_observations", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin.ListPluginInstances", + "fullName": "google.cloud.apihub.v1.ApiHubDiscovery.ListDiscoveredApiObservations", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin", - "shortName": "ApiHubPlugin" + "fullName": "google.cloud.apihub.v1.ApiHubDiscovery", + "shortName": "ApiHubDiscovery" }, - "shortName": "ListPluginInstances" + "shortName": "ListDiscoveredApiObservations" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.ListPluginInstancesRequest" + "type": "google.cloud.apihub_v1.types.ListDiscoveredApiObservationsRequest" }, { "name": "parent", @@ -2183,14 +2275,14 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.services.api_hub_plugin.pagers.ListPluginInstancesPager", - "shortName": "list_plugin_instances" + "resultType": "google.cloud.apihub_v1.services.api_hub_discovery.pagers.ListDiscoveredApiObservationsPager", + "shortName": "list_discovered_api_observations" }, - "description": "Sample for ListPluginInstances", - "file": "apihub_v1_generated_api_hub_plugin_list_plugin_instances_sync.py", + "description": "Sample for ListDiscoveredApiObservations", + "file": "apihub_v1_generated_api_hub_discovery_list_discovered_api_observations_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubPlugin_ListPluginInstances_sync", + "regionTag": "apihub_v1_generated_ApiHubDiscovery_ListDiscoveredApiObservations_sync", "segments": [ { "end": 52, @@ -2223,28 +2315,29 @@ "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_plugin_list_plugin_instances_sync.py" + "title": "apihub_v1_generated_api_hub_discovery_list_discovered_api_observations_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", - "shortName": "ApiHubPluginClient" + "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryAsyncClient", + "shortName": "ApiHubDiscoveryAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.list_plugins", + "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryAsyncClient.list_discovered_api_operations", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin.ListPlugins", + "fullName": "google.cloud.apihub.v1.ApiHubDiscovery.ListDiscoveredApiOperations", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin", - "shortName": "ApiHubPlugin" + "fullName": "google.cloud.apihub.v1.ApiHubDiscovery", + "shortName": "ApiHubDiscovery" }, - "shortName": "ListPlugins" + "shortName": "ListDiscoveredApiOperations" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.ListPluginsRequest" + "type": "google.cloud.apihub_v1.types.ListDiscoveredApiOperationsRequest" }, { "name": "parent", @@ -2263,14 +2356,14 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.services.api_hub_plugin.pagers.ListPluginsPager", - "shortName": "list_plugins" + "resultType": "google.cloud.apihub_v1.services.api_hub_discovery.pagers.ListDiscoveredApiOperationsAsyncPager", + "shortName": "list_discovered_api_operations" }, - "description": "Sample for ListPlugins", - "file": "apihub_v1_generated_api_hub_plugin_list_plugins_sync.py", + "description": "Sample for ListDiscoveredApiOperations", + "file": "apihub_v1_generated_api_hub_discovery_list_discovered_api_operations_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubPlugin_ListPlugins_sync", + "regionTag": "apihub_v1_generated_ApiHubDiscovery_ListDiscoveredApiOperations_async", "segments": [ { "end": 52, @@ -2303,36 +2396,32 @@ "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_plugin_list_plugins_sync.py" + "title": "apihub_v1_generated_api_hub_discovery_list_discovered_api_operations_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", - "shortName": "ApiHubPluginClient" + "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryClient", + "shortName": "ApiHubDiscoveryClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.update_plugin_instance", + "fullName": "google.cloud.apihub_v1.ApiHubDiscoveryClient.list_discovered_api_operations", "method": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin.UpdatePluginInstance", + "fullName": "google.cloud.apihub.v1.ApiHubDiscovery.ListDiscoveredApiOperations", "service": { - "fullName": "google.cloud.apihub.v1.ApiHubPlugin", - "shortName": "ApiHubPlugin" + "fullName": "google.cloud.apihub.v1.ApiHubDiscovery", + "shortName": "ApiHubDiscovery" }, - "shortName": "UpdatePluginInstance" + "shortName": "ListDiscoveredApiOperations" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.UpdatePluginInstanceRequest" - }, - { - "name": "plugin_instance", - "type": "google.cloud.apihub_v1.types.PluginInstance" + "type": "google.cloud.apihub_v1.types.ListDiscoveredApiOperationsRequest" }, { - "name": "update_mask", - "type": "google.protobuf.field_mask_pb2.FieldMask" + "name": "parent", + "type": "str" }, { "name": "retry", @@ -2347,22 +2436,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.PluginInstance", - "shortName": "update_plugin_instance" + "resultType": "google.cloud.apihub_v1.services.api_hub_discovery.pagers.ListDiscoveredApiOperationsPager", + "shortName": "list_discovered_api_operations" }, - "description": "Sample for UpdatePluginInstance", - "file": "apihub_v1_generated_api_hub_plugin_update_plugin_instance_sync.py", + "description": "Sample for ListDiscoveredApiOperations", + "file": "apihub_v1_generated_api_hub_discovery_list_discovered_api_operations_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHubPlugin_UpdatePluginInstance_sync", + "regionTag": "apihub_v1_generated_ApiHubDiscovery_ListDiscoveredApiOperations_sync", "segments": [ { - "end": 55, + "end": 52, "start": 27, "type": "FULL" }, { - "end": 55, + "end": 52, "start": 27, "type": "SHORT" }, @@ -2372,54 +2461,55 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 49, + "end": 45, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 52, - "start": 50, + "end": 48, + "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 56, - "start": 53, + "end": 53, + "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_plugin_update_plugin_instance_sync.py" + "title": "apihub_v1_generated_api_hub_discovery_list_discovered_api_operations_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient", + "shortName": "ApiHubPluginAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.create_api_operation", + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient.create_plugin_instance", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.CreateApiOperation", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.CreatePluginInstance", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "CreateApiOperation" + "shortName": "CreatePluginInstance" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.CreateApiOperationRequest" + "type": "google.cloud.apihub_v1.types.CreatePluginInstanceRequest" }, { "name": "parent", "type": "str" }, { - "name": "api_operation", - "type": "google.cloud.apihub_v1.types.ApiOperation" + "name": "plugin_instance", + "type": "google.cloud.apihub_v1.types.PluginInstance" }, { - "name": "api_operation_id", + "name": "plugin_instance_id", "type": "str" }, { @@ -2435,22 +2525,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.ApiOperation", - "shortName": "create_api_operation" + "resultType": "google.api_core.operation_async.AsyncOperation", + "shortName": "create_plugin_instance" }, - "description": "Sample for CreateApiOperation", - "file": "apihub_v1_generated_api_hub_create_api_operation_sync.py", + "description": "Sample for CreatePluginInstance", + "file": "apihub_v1_generated_api_hub_plugin_create_plugin_instance_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_CreateApiOperation_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_CreatePluginInstance_async", "segments": [ { - "end": 51, + "end": 60, "start": 27, "type": "FULL" }, { - "end": 51, + "end": 60, "start": 27, "type": "SHORT" }, @@ -2460,54 +2550,54 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 45, + "end": 50, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 48, - "start": 46, + "end": 57, + "start": 51, "type": "REQUEST_EXECUTION" }, { - "end": 52, - "start": 49, + "end": 61, + "start": 58, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_create_api_operation_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_create_plugin_instance_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", + "shortName": "ApiHubPluginClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.create_api", + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.create_plugin_instance", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.CreateApi", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.CreatePluginInstance", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "CreateApi" + "shortName": "CreatePluginInstance" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.CreateApiRequest" + "type": "google.cloud.apihub_v1.types.CreatePluginInstanceRequest" }, { "name": "parent", "type": "str" }, { - "name": "api", - "type": "google.cloud.apihub_v1.types.Api" + "name": "plugin_instance", + "type": "google.cloud.apihub_v1.types.PluginInstance" }, { - "name": "api_id", + "name": "plugin_instance_id", "type": "str" }, { @@ -2523,22 +2613,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Api", - "shortName": "create_api" + "resultType": "google.api_core.operation.Operation", + "shortName": "create_plugin_instance" }, - "description": "Sample for CreateApi", - "file": "apihub_v1_generated_api_hub_create_api_sync.py", + "description": "Sample for CreatePluginInstance", + "file": "apihub_v1_generated_api_hub_plugin_create_plugin_instance_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_CreateApi_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_CreatePluginInstance_sync", "segments": [ { - "end": 55, + "end": 60, "start": 27, "type": "FULL" }, { - "end": 55, + "end": 60, "start": 27, "type": "SHORT" }, @@ -2548,54 +2638,55 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 49, + "end": 50, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 52, - "start": 50, + "end": 57, + "start": 51, "type": "REQUEST_EXECUTION" }, { - "end": 56, - "start": 53, + "end": 61, + "start": 58, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_create_api_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_create_plugin_instance_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient", + "shortName": "ApiHubPluginAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.create_attribute", + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient.create_plugin", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.CreateAttribute", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.CreatePlugin", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "CreateAttribute" + "shortName": "CreatePlugin" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.CreateAttributeRequest" + "type": "google.cloud.apihub_v1.types.CreatePluginRequest" }, { "name": "parent", "type": "str" }, { - "name": "attribute", - "type": "google.cloud.apihub_v1.types.Attribute" + "name": "plugin", + "type": "google.cloud.apihub_v1.types.Plugin" }, { - "name": "attribute_id", + "name": "plugin_id", "type": "str" }, { @@ -2611,22 +2702,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Attribute", - "shortName": "create_attribute" + "resultType": "google.cloud.apihub_v1.types.Plugin", + "shortName": "create_plugin" }, - "description": "Sample for CreateAttribute", - "file": "apihub_v1_generated_api_hub_create_attribute_sync.py", + "description": "Sample for CreatePlugin", + "file": "apihub_v1_generated_api_hub_plugin_create_plugin_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_CreateAttribute_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_CreatePlugin_async", "segments": [ { - "end": 57, + "end": 55, "start": 27, "type": "FULL" }, { - "end": 57, + "end": 55, "start": 27, "type": "SHORT" }, @@ -2636,54 +2727,54 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 51, + "end": 49, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 54, - "start": 52, + "end": 52, + "start": 50, "type": "REQUEST_EXECUTION" }, { - "end": 58, - "start": 55, + "end": 56, + "start": 53, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_create_attribute_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_create_plugin_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", + "shortName": "ApiHubPluginClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.create_deployment", + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.create_plugin", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.CreateDeployment", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.CreatePlugin", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "CreateDeployment" + "shortName": "CreatePlugin" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.CreateDeploymentRequest" + "type": "google.cloud.apihub_v1.types.CreatePluginRequest" }, { "name": "parent", "type": "str" }, { - "name": "deployment", - "type": "google.cloud.apihub_v1.types.Deployment" + "name": "plugin", + "type": "google.cloud.apihub_v1.types.Plugin" }, { - "name": "deployment_id", + "name": "plugin_id", "type": "str" }, { @@ -2699,22 +2790,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Deployment", - "shortName": "create_deployment" + "resultType": "google.cloud.apihub_v1.types.Plugin", + "shortName": "create_plugin" }, - "description": "Sample for CreateDeployment", - "file": "apihub_v1_generated_api_hub_create_deployment_sync.py", + "description": "Sample for CreatePlugin", + "file": "apihub_v1_generated_api_hub_plugin_create_plugin_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_CreateDeployment_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_CreatePlugin_sync", "segments": [ { - "end": 59, + "end": 55, "start": 27, "type": "FULL" }, { - "end": 59, + "end": 55, "start": 27, "type": "SHORT" }, @@ -2724,54 +2815,47 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 53, + "end": 49, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 56, - "start": 54, + "end": 52, + "start": 50, "type": "REQUEST_EXECUTION" }, { - "end": 60, - "start": 57, + "end": 56, + "start": 53, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_create_deployment_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_create_plugin_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient", + "shortName": "ApiHubPluginAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.create_external_api", + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient.delete_plugin_instance", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.CreateExternalApi", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.DeletePluginInstance", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "CreateExternalApi" + "shortName": "DeletePluginInstance" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.CreateExternalApiRequest" - }, - { - "name": "parent", - "type": "str" - }, - { - "name": "external_api", - "type": "google.cloud.apihub_v1.types.ExternalApi" + "type": "google.cloud.apihub_v1.types.DeletePluginInstanceRequest" }, { - "name": "external_api_id", + "name": "name", "type": "str" }, { @@ -2787,14 +2871,14 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.ExternalApi", - "shortName": "create_external_api" + "resultType": "google.api_core.operation_async.AsyncOperation", + "shortName": "delete_plugin_instance" }, - "description": "Sample for CreateExternalApi", - "file": "apihub_v1_generated_api_hub_create_external_api_sync.py", + "description": "Sample for DeletePluginInstance", + "file": "apihub_v1_generated_api_hub_plugin_delete_plugin_instance_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_CreateExternalApi_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_DeletePluginInstance_async", "segments": [ { "end": 55, @@ -2812,13 +2896,13 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 49, + "end": 45, "start": 41, "type": "REQUEST_INITIALIZATION" }, { "end": 52, - "start": 50, + "start": 46, "type": "REQUEST_EXECUTION" }, { @@ -2827,39 +2911,31 @@ "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_create_external_api_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_delete_plugin_instance_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", + "shortName": "ApiHubPluginClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.create_spec", + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.delete_plugin_instance", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.CreateSpec", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.DeletePluginInstance", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "CreateSpec" + "shortName": "DeletePluginInstance" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.CreateSpecRequest" - }, - { - "name": "parent", - "type": "str" - }, - { - "name": "spec", - "type": "google.cloud.apihub_v1.types.Spec" + "type": "google.cloud.apihub_v1.types.DeletePluginInstanceRequest" }, { - "name": "spec_id", + "name": "name", "type": "str" }, { @@ -2875,22 +2951,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Spec", - "shortName": "create_spec" + "resultType": "google.api_core.operation.Operation", + "shortName": "delete_plugin_instance" }, - "description": "Sample for CreateSpec", - "file": "apihub_v1_generated_api_hub_create_spec_sync.py", + "description": "Sample for DeletePluginInstance", + "file": "apihub_v1_generated_api_hub_plugin_delete_plugin_instance_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_CreateSpec_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_DeletePluginInstance_sync", "segments": [ { - "end": 57, + "end": 55, "start": 27, "type": "FULL" }, { - "end": 57, + "end": 55, "start": 27, "type": "SHORT" }, @@ -2900,54 +2976,47 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 51, + "end": 45, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 54, - "start": 52, + "end": 52, + "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 58, - "start": 55, + "end": 56, + "start": 53, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_create_spec_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_delete_plugin_instance_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient", + "shortName": "ApiHubPluginAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.create_version", + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient.delete_plugin", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.CreateVersion", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.DeletePlugin", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "CreateVersion" + "shortName": "DeletePlugin" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.CreateVersionRequest" - }, - { - "name": "parent", - "type": "str" - }, - { - "name": "version", - "type": "google.cloud.apihub_v1.types.Version" + "type": "google.cloud.apihub_v1.types.DeletePluginRequest" }, { - "name": "version_id", + "name": "name", "type": "str" }, { @@ -2963,14 +3032,14 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Version", - "shortName": "create_version" + "resultType": "google.api_core.operation_async.AsyncOperation", + "shortName": "delete_plugin" }, - "description": "Sample for CreateVersion", - "file": "apihub_v1_generated_api_hub_create_version_sync.py", + "description": "Sample for DeletePlugin", + "file": "apihub_v1_generated_api_hub_plugin_delete_plugin_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_CreateVersion_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_DeletePlugin_async", "segments": [ { "end": 55, @@ -2988,13 +3057,13 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 49, + "end": 45, "start": 41, "type": "REQUEST_INITIALIZATION" }, { "end": 52, - "start": 50, + "start": 46, "type": "REQUEST_EXECUTION" }, { @@ -3003,28 +3072,28 @@ "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_create_version_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_delete_plugin_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", + "shortName": "ApiHubPluginClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.delete_api_operation", + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.delete_plugin", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.DeleteApiOperation", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.DeletePlugin", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "DeleteApiOperation" + "shortName": "DeletePlugin" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.DeleteApiOperationRequest" + "type": "google.cloud.apihub_v1.types.DeletePluginRequest" }, { "name": "name", @@ -3043,21 +3112,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "shortName": "delete_api_operation" + "resultType": "google.api_core.operation.Operation", + "shortName": "delete_plugin" }, - "description": "Sample for DeleteApiOperation", - "file": "apihub_v1_generated_api_hub_delete_api_operation_sync.py", + "description": "Sample for DeletePlugin", + "file": "apihub_v1_generated_api_hub_plugin_delete_plugin_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_DeleteApiOperation_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_DeletePlugin_sync", "segments": [ { - "end": 49, + "end": 55, "start": 27, "type": "FULL" }, { - "end": 49, + "end": 55, "start": 27, "type": "SHORT" }, @@ -3072,41 +3142,48 @@ "type": "REQUEST_INITIALIZATION" }, { + "end": 52, "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 50, + "end": 56, + "start": 53, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_delete_api_operation_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_delete_plugin_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient", + "shortName": "ApiHubPluginAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.delete_api", + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient.disable_plugin_instance_action", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.DeleteApi", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.DisablePluginInstanceAction", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "DeleteApi" + "shortName": "DisablePluginInstanceAction" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.DeleteApiRequest" + "type": "google.cloud.apihub_v1.types.DisablePluginInstanceActionRequest" }, { "name": "name", "type": "str" }, + { + "name": "action_id", + "type": "str" + }, { "name": "retry", "type": "google.api_core.retry.Retry" @@ -3120,21 +3197,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "shortName": "delete_api" + "resultType": "google.api_core.operation_async.AsyncOperation", + "shortName": "disable_plugin_instance_action" }, - "description": "Sample for DeleteApi", - "file": "apihub_v1_generated_api_hub_delete_api_sync.py", + "description": "Sample for DisablePluginInstanceAction", + "file": "apihub_v1_generated_api_hub_plugin_disable_plugin_instance_action_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_DeleteApi_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_DisablePluginInstanceAction_async", "segments": [ { - "end": 49, + "end": 56, "start": 27, "type": "FULL" }, { - "end": 49, + "end": 56, "start": 27, "type": "SHORT" }, @@ -3144,46 +3222,52 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 45, + "end": 46, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "start": 46, + "end": 53, + "start": 47, "type": "REQUEST_EXECUTION" }, { - "end": 50, + "end": 57, + "start": 54, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_delete_api_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_disable_plugin_instance_action_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", + "shortName": "ApiHubPluginClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.delete_attribute", + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.disable_plugin_instance_action", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.DeleteAttribute", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.DisablePluginInstanceAction", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "DeleteAttribute" + "shortName": "DisablePluginInstanceAction" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.DeleteAttributeRequest" + "type": "google.cloud.apihub_v1.types.DisablePluginInstanceActionRequest" }, { "name": "name", "type": "str" }, + { + "name": "action_id", + "type": "str" + }, { "name": "retry", "type": "google.api_core.retry.Retry" @@ -3197,21 +3281,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "shortName": "delete_attribute" + "resultType": "google.api_core.operation.Operation", + "shortName": "disable_plugin_instance_action" }, - "description": "Sample for DeleteAttribute", - "file": "apihub_v1_generated_api_hub_delete_attribute_sync.py", + "description": "Sample for DisablePluginInstanceAction", + "file": "apihub_v1_generated_api_hub_plugin_disable_plugin_instance_action_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_DeleteAttribute_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_DisablePluginInstanceAction_sync", "segments": [ { - "end": 49, + "end": 56, "start": 27, "type": "FULL" }, { - "end": 49, + "end": 56, "start": 27, "type": "SHORT" }, @@ -3221,41 +3306,44 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 45, + "end": 46, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "start": 46, + "end": 53, + "start": 47, "type": "REQUEST_EXECUTION" }, { - "end": 50, + "end": 57, + "start": 54, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_delete_attribute_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_disable_plugin_instance_action_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient", + "shortName": "ApiHubPluginAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.delete_deployment", + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient.disable_plugin", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.DeleteDeployment", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.DisablePlugin", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "DeleteDeployment" + "shortName": "DisablePlugin" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.DeleteDeploymentRequest" + "type": "google.cloud.apihub_v1.types.DisablePluginRequest" }, { "name": "name", @@ -3274,21 +3362,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "shortName": "delete_deployment" + "resultType": "google.cloud.apihub_v1.types.Plugin", + "shortName": "disable_plugin" }, - "description": "Sample for DeleteDeployment", - "file": "apihub_v1_generated_api_hub_delete_deployment_sync.py", + "description": "Sample for DisablePlugin", + "file": "apihub_v1_generated_api_hub_plugin_disable_plugin_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_DeleteDeployment_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_DisablePlugin_async", "segments": [ { - "end": 49, + "end": 51, "start": 27, "type": "FULL" }, { - "end": 49, + "end": 51, "start": 27, "type": "SHORT" }, @@ -3303,36 +3392,38 @@ "type": "REQUEST_INITIALIZATION" }, { + "end": 48, "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 50, + "end": 52, + "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_delete_deployment_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_disable_plugin_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", + "shortName": "ApiHubPluginClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.delete_external_api", + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.disable_plugin", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.DeleteExternalApi", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.DisablePlugin", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "DeleteExternalApi" + "shortName": "DisablePlugin" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.DeleteExternalApiRequest" + "type": "google.cloud.apihub_v1.types.DisablePluginRequest" }, { "name": "name", @@ -3351,21 +3442,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "shortName": "delete_external_api" + "resultType": "google.cloud.apihub_v1.types.Plugin", + "shortName": "disable_plugin" }, - "description": "Sample for DeleteExternalApi", - "file": "apihub_v1_generated_api_hub_delete_external_api_sync.py", + "description": "Sample for DisablePlugin", + "file": "apihub_v1_generated_api_hub_plugin_disable_plugin_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_DeleteExternalApi_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_DisablePlugin_sync", "segments": [ { - "end": 49, + "end": 51, "start": 27, "type": "FULL" }, { - "end": 49, + "end": 51, "start": 27, "type": "SHORT" }, @@ -3380,41 +3472,48 @@ "type": "REQUEST_INITIALIZATION" }, { + "end": 48, "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 50, + "end": 52, + "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_delete_external_api_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_disable_plugin_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient", + "shortName": "ApiHubPluginAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.delete_spec", + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient.enable_plugin_instance_action", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.DeleteSpec", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.EnablePluginInstanceAction", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "DeleteSpec" + "shortName": "EnablePluginInstanceAction" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.DeleteSpecRequest" + "type": "google.cloud.apihub_v1.types.EnablePluginInstanceActionRequest" }, { "name": "name", "type": "str" }, + { + "name": "action_id", + "type": "str" + }, { "name": "retry", "type": "google.api_core.retry.Retry" @@ -3428,21 +3527,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "shortName": "delete_spec" + "resultType": "google.api_core.operation_async.AsyncOperation", + "shortName": "enable_plugin_instance_action" }, - "description": "Sample for DeleteSpec", - "file": "apihub_v1_generated_api_hub_delete_spec_sync.py", + "description": "Sample for EnablePluginInstanceAction", + "file": "apihub_v1_generated_api_hub_plugin_enable_plugin_instance_action_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_DeleteSpec_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_EnablePluginInstanceAction_async", "segments": [ { - "end": 49, + "end": 56, "start": 27, "type": "FULL" }, { - "end": 49, + "end": 56, "start": 27, "type": "SHORT" }, @@ -3452,46 +3552,52 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 45, + "end": 46, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "start": 46, + "end": 53, + "start": 47, "type": "REQUEST_EXECUTION" }, { - "end": 50, + "end": 57, + "start": 54, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_delete_spec_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_enable_plugin_instance_action_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", + "shortName": "ApiHubPluginClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.delete_version", + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.enable_plugin_instance_action", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.DeleteVersion", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.EnablePluginInstanceAction", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "DeleteVersion" + "shortName": "EnablePluginInstanceAction" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.DeleteVersionRequest" + "type": "google.cloud.apihub_v1.types.EnablePluginInstanceActionRequest" }, { "name": "name", "type": "str" }, + { + "name": "action_id", + "type": "str" + }, { "name": "retry", "type": "google.api_core.retry.Retry" @@ -3505,21 +3611,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "shortName": "delete_version" + "resultType": "google.api_core.operation.Operation", + "shortName": "enable_plugin_instance_action" }, - "description": "Sample for DeleteVersion", - "file": "apihub_v1_generated_api_hub_delete_version_sync.py", + "description": "Sample for EnablePluginInstanceAction", + "file": "apihub_v1_generated_api_hub_plugin_enable_plugin_instance_action_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_DeleteVersion_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_EnablePluginInstanceAction_sync", "segments": [ { - "end": 49, + "end": 56, "start": 27, "type": "FULL" }, { - "end": 49, + "end": 56, "start": 27, "type": "SHORT" }, @@ -3529,41 +3636,44 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 45, + "end": 46, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "start": 46, + "end": 53, + "start": 47, "type": "REQUEST_EXECUTION" }, { - "end": 50, + "end": 57, + "start": 54, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_delete_version_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_enable_plugin_instance_action_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient", + "shortName": "ApiHubPluginAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.get_api_operation", + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient.enable_plugin", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.GetApiOperation", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.EnablePlugin", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "GetApiOperation" + "shortName": "EnablePlugin" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.GetApiOperationRequest" + "type": "google.cloud.apihub_v1.types.EnablePluginRequest" }, { "name": "name", @@ -3582,14 +3692,14 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.ApiOperation", - "shortName": "get_api_operation" + "resultType": "google.cloud.apihub_v1.types.Plugin", + "shortName": "enable_plugin" }, - "description": "Sample for GetApiOperation", - "file": "apihub_v1_generated_api_hub_get_api_operation_sync.py", + "description": "Sample for EnablePlugin", + "file": "apihub_v1_generated_api_hub_plugin_enable_plugin_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_GetApiOperation_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_EnablePlugin_async", "segments": [ { "end": 51, @@ -3622,28 +3732,28 @@ "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_get_api_operation_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_enable_plugin_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", + "shortName": "ApiHubPluginClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.get_api", + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.enable_plugin", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.GetApi", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.EnablePlugin", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "GetApi" + "shortName": "EnablePlugin" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.GetApiRequest" + "type": "google.cloud.apihub_v1.types.EnablePluginRequest" }, { "name": "name", @@ -3662,14 +3772,14 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Api", - "shortName": "get_api" + "resultType": "google.cloud.apihub_v1.types.Plugin", + "shortName": "enable_plugin" }, - "description": "Sample for GetApi", - "file": "apihub_v1_generated_api_hub_get_api_sync.py", + "description": "Sample for EnablePlugin", + "file": "apihub_v1_generated_api_hub_plugin_enable_plugin_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_GetApi_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_EnablePlugin_sync", "segments": [ { "end": 51, @@ -3702,33 +3812,38 @@ "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_get_api_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_enable_plugin_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient", + "shortName": "ApiHubPluginAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.get_attribute", + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient.execute_plugin_instance_action", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.GetAttribute", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.ExecutePluginInstanceAction", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "GetAttribute" + "shortName": "ExecutePluginInstanceAction" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.GetAttributeRequest" + "type": "google.cloud.apihub_v1.types.ExecutePluginInstanceActionRequest" }, { "name": "name", "type": "str" }, + { + "name": "action_execution_detail", + "type": "google.cloud.apihub_v1.types.ActionExecutionDetail" + }, { "name": "retry", "type": "google.api_core.retry.Retry" @@ -3742,22 +3857,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Attribute", - "shortName": "get_attribute" + "resultType": "google.api_core.operation_async.AsyncOperation", + "shortName": "execute_plugin_instance_action" }, - "description": "Sample for GetAttribute", - "file": "apihub_v1_generated_api_hub_get_attribute_sync.py", + "description": "Sample for ExecutePluginInstanceAction", + "file": "apihub_v1_generated_api_hub_plugin_execute_plugin_instance_action_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_GetAttribute_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_ExecutePluginInstanceAction_async", "segments": [ { - "end": 51, + "end": 59, "start": 27, "type": "FULL" }, { - "end": 51, + "end": 59, "start": 27, "type": "SHORT" }, @@ -3767,48 +3882,52 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 45, + "end": 49, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 48, - "start": 46, + "end": 56, + "start": 50, "type": "REQUEST_EXECUTION" }, { - "end": 52, - "start": 49, + "end": 60, + "start": 57, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_get_attribute_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_execute_plugin_instance_action_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", + "shortName": "ApiHubPluginClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.get_definition", + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.execute_plugin_instance_action", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.GetDefinition", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.ExecutePluginInstanceAction", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "GetDefinition" + "shortName": "ExecutePluginInstanceAction" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.GetDefinitionRequest" + "type": "google.cloud.apihub_v1.types.ExecutePluginInstanceActionRequest" }, { "name": "name", "type": "str" }, + { + "name": "action_execution_detail", + "type": "google.cloud.apihub_v1.types.ActionExecutionDetail" + }, { "name": "retry", "type": "google.api_core.retry.Retry" @@ -3822,22 +3941,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Definition", - "shortName": "get_definition" + "resultType": "google.api_core.operation.Operation", + "shortName": "execute_plugin_instance_action" }, - "description": "Sample for GetDefinition", - "file": "apihub_v1_generated_api_hub_get_definition_sync.py", + "description": "Sample for ExecutePluginInstanceAction", + "file": "apihub_v1_generated_api_hub_plugin_execute_plugin_instance_action_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_GetDefinition_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_ExecutePluginInstanceAction_sync", "segments": [ { - "end": 51, + "end": 59, "start": 27, "type": "FULL" }, { - "end": 51, + "end": 59, "start": 27, "type": "SHORT" }, @@ -3847,43 +3966,44 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 45, + "end": 49, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 48, - "start": 46, + "end": 56, + "start": 50, "type": "REQUEST_EXECUTION" }, { - "end": 52, - "start": 49, + "end": 60, + "start": 57, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_get_definition_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_execute_plugin_instance_action_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient", + "shortName": "ApiHubPluginAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.get_deployment", + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient.get_plugin_instance", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.GetDeployment", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.GetPluginInstance", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "GetDeployment" + "shortName": "GetPluginInstance" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.GetDeploymentRequest" + "type": "google.cloud.apihub_v1.types.GetPluginInstanceRequest" }, { "name": "name", @@ -3902,14 +4022,14 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Deployment", - "shortName": "get_deployment" + "resultType": "google.cloud.apihub_v1.types.PluginInstance", + "shortName": "get_plugin_instance" }, - "description": "Sample for GetDeployment", - "file": "apihub_v1_generated_api_hub_get_deployment_sync.py", + "description": "Sample for GetPluginInstance", + "file": "apihub_v1_generated_api_hub_plugin_get_plugin_instance_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_GetDeployment_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_GetPluginInstance_async", "segments": [ { "end": 51, @@ -3942,28 +4062,28 @@ "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_get_deployment_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_get_plugin_instance_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", + "shortName": "ApiHubPluginClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.get_external_api", + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.get_plugin_instance", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.GetExternalApi", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.GetPluginInstance", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "GetExternalApi" + "shortName": "GetPluginInstance" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.GetExternalApiRequest" + "type": "google.cloud.apihub_v1.types.GetPluginInstanceRequest" }, { "name": "name", @@ -3982,14 +4102,14 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.ExternalApi", - "shortName": "get_external_api" + "resultType": "google.cloud.apihub_v1.types.PluginInstance", + "shortName": "get_plugin_instance" }, - "description": "Sample for GetExternalApi", - "file": "apihub_v1_generated_api_hub_get_external_api_sync.py", + "description": "Sample for GetPluginInstance", + "file": "apihub_v1_generated_api_hub_plugin_get_plugin_instance_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_GetExternalApi_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_GetPluginInstance_sync", "segments": [ { "end": 51, @@ -4022,28 +4142,29 @@ "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_get_external_api_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_get_plugin_instance_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient", + "shortName": "ApiHubPluginAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.get_spec_contents", + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient.get_plugin", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.GetSpecContents", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.GetPlugin", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "GetSpecContents" + "shortName": "GetPlugin" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.GetSpecContentsRequest" + "type": "google.cloud.apihub_v1.types.GetPluginRequest" }, { "name": "name", @@ -4062,14 +4183,14 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.SpecContents", - "shortName": "get_spec_contents" + "resultType": "google.cloud.apihub_v1.types.Plugin", + "shortName": "get_plugin" }, - "description": "Sample for GetSpecContents", - "file": "apihub_v1_generated_api_hub_get_spec_contents_sync.py", + "description": "Sample for GetPlugin", + "file": "apihub_v1_generated_api_hub_plugin_get_plugin_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_GetSpecContents_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_GetPlugin_async", "segments": [ { "end": 51, @@ -4102,28 +4223,28 @@ "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_get_spec_contents_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_get_plugin_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", + "shortName": "ApiHubPluginClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.get_spec", + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.get_plugin", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.GetSpec", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.GetPlugin", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "GetSpec" + "shortName": "GetPlugin" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.GetSpecRequest" + "type": "google.cloud.apihub_v1.types.GetPluginRequest" }, { "name": "name", @@ -4142,14 +4263,14 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Spec", - "shortName": "get_spec" + "resultType": "google.cloud.apihub_v1.types.Plugin", + "shortName": "get_plugin" }, - "description": "Sample for GetSpec", - "file": "apihub_v1_generated_api_hub_get_spec_sync.py", + "description": "Sample for GetPlugin", + "file": "apihub_v1_generated_api_hub_plugin_get_plugin_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_GetSpec_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_GetPlugin_sync", "segments": [ { "end": 51, @@ -4182,31 +4303,32 @@ "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_get_spec_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_get_plugin_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient", + "shortName": "ApiHubPluginAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.get_version", + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient.list_plugin_instances", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.GetVersion", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.ListPluginInstances", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "GetVersion" + "shortName": "ListPluginInstances" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.GetVersionRequest" + "type": "google.cloud.apihub_v1.types.ListPluginInstancesRequest" }, { - "name": "name", + "name": "parent", "type": "str" }, { @@ -4222,22 +4344,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Version", - "shortName": "get_version" + "resultType": "google.cloud.apihub_v1.services.api_hub_plugin.pagers.ListPluginInstancesAsyncPager", + "shortName": "list_plugin_instances" }, - "description": "Sample for GetVersion", - "file": "apihub_v1_generated_api_hub_get_version_sync.py", + "description": "Sample for ListPluginInstances", + "file": "apihub_v1_generated_api_hub_plugin_list_plugin_instances_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_GetVersion_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_ListPluginInstances_async", "segments": [ { - "end": 51, + "end": 52, "start": 27, "type": "FULL" }, { - "end": 51, + "end": 52, "start": 27, "type": "SHORT" }, @@ -4257,33 +4379,33 @@ "type": "REQUEST_EXECUTION" }, { - "end": 52, + "end": 53, "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_get_version_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_list_plugin_instances_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", + "shortName": "ApiHubPluginClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.list_api_operations", + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.list_plugin_instances", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.ListApiOperations", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.ListPluginInstances", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "ListApiOperations" + "shortName": "ListPluginInstances" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.ListApiOperationsRequest" + "type": "google.cloud.apihub_v1.types.ListPluginInstancesRequest" }, { "name": "parent", @@ -4302,14 +4424,14 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListApiOperationsPager", - "shortName": "list_api_operations" + "resultType": "google.cloud.apihub_v1.services.api_hub_plugin.pagers.ListPluginInstancesPager", + "shortName": "list_plugin_instances" }, - "description": "Sample for ListApiOperations", - "file": "apihub_v1_generated_api_hub_list_api_operations_sync.py", + "description": "Sample for ListPluginInstances", + "file": "apihub_v1_generated_api_hub_plugin_list_plugin_instances_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_ListApiOperations_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_ListPluginInstances_sync", "segments": [ { "end": 52, @@ -4342,28 +4464,29 @@ "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_list_api_operations_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_list_plugin_instances_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient", + "shortName": "ApiHubPluginAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.list_apis", + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient.list_plugins", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.ListApis", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.ListPlugins", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "ListApis" + "shortName": "ListPlugins" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.ListApisRequest" + "type": "google.cloud.apihub_v1.types.ListPluginsRequest" }, { "name": "parent", @@ -4382,14 +4505,14 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListApisPager", - "shortName": "list_apis" + "resultType": "google.cloud.apihub_v1.services.api_hub_plugin.pagers.ListPluginsAsyncPager", + "shortName": "list_plugins" }, - "description": "Sample for ListApis", - "file": "apihub_v1_generated_api_hub_list_apis_sync.py", + "description": "Sample for ListPlugins", + "file": "apihub_v1_generated_api_hub_plugin_list_plugins_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_ListApis_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_ListPlugins_async", "segments": [ { "end": 52, @@ -4422,28 +4545,28 @@ "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_list_apis_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_list_plugins_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", + "shortName": "ApiHubPluginClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.list_attributes", + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.list_plugins", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.ListAttributes", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.ListPlugins", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "ListAttributes" + "shortName": "ListPlugins" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.ListAttributesRequest" + "type": "google.cloud.apihub_v1.types.ListPluginsRequest" }, { "name": "parent", @@ -4462,14 +4585,14 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListAttributesPager", - "shortName": "list_attributes" + "resultType": "google.cloud.apihub_v1.services.api_hub_plugin.pagers.ListPluginsPager", + "shortName": "list_plugins" }, - "description": "Sample for ListAttributes", - "file": "apihub_v1_generated_api_hub_list_attributes_sync.py", + "description": "Sample for ListPlugins", + "file": "apihub_v1_generated_api_hub_plugin_list_plugins_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_ListAttributes_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_ListPlugins_sync", "segments": [ { "end": 52, @@ -4502,32 +4625,37 @@ "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_list_attributes_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_list_plugins_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient", + "shortName": "ApiHubPluginAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.list_deployments", + "fullName": "google.cloud.apihub_v1.ApiHubPluginAsyncClient.update_plugin_instance", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.ListDeployments", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.UpdatePluginInstance", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "ListDeployments" + "shortName": "UpdatePluginInstance" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.ListDeploymentsRequest" + "type": "google.cloud.apihub_v1.types.UpdatePluginInstanceRequest" }, { - "name": "parent", - "type": "str" + "name": "plugin_instance", + "type": "google.cloud.apihub_v1.types.PluginInstance" + }, + { + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" }, { "name": "retry", @@ -4542,22 +4670,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListDeploymentsPager", - "shortName": "list_deployments" + "resultType": "google.cloud.apihub_v1.types.PluginInstance", + "shortName": "update_plugin_instance" }, - "description": "Sample for ListDeployments", - "file": "apihub_v1_generated_api_hub_list_deployments_sync.py", + "description": "Sample for UpdatePluginInstance", + "file": "apihub_v1_generated_api_hub_plugin_update_plugin_instance_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_ListDeployments_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_UpdatePluginInstance_async", "segments": [ { - "end": 52, + "end": 55, "start": 27, "type": "FULL" }, { - "end": 52, + "end": 55, "start": 27, "type": "SHORT" }, @@ -4567,47 +4695,51 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 45, + "end": 49, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 48, - "start": 46, + "end": 52, + "start": 50, "type": "REQUEST_EXECUTION" }, { - "end": 53, - "start": 49, + "end": 56, + "start": 53, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_list_deployments_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_update_plugin_instance_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient", + "shortName": "ApiHubPluginClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.list_external_apis", + "fullName": "google.cloud.apihub_v1.ApiHubPluginClient.update_plugin_instance", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.ListExternalApis", + "fullName": "google.cloud.apihub.v1.ApiHubPlugin.UpdatePluginInstance", "service": { - "fullName": "google.cloud.apihub.v1.ApiHub", - "shortName": "ApiHub" + "fullName": "google.cloud.apihub.v1.ApiHubPlugin", + "shortName": "ApiHubPlugin" }, - "shortName": "ListExternalApis" + "shortName": "UpdatePluginInstance" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.ListExternalApisRequest" + "type": "google.cloud.apihub_v1.types.UpdatePluginInstanceRequest" }, { - "name": "parent", - "type": "str" + "name": "plugin_instance", + "type": "google.cloud.apihub_v1.types.PluginInstance" + }, + { + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" }, { "name": "retry", @@ -4622,22 +4754,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListExternalApisPager", - "shortName": "list_external_apis" + "resultType": "google.cloud.apihub_v1.types.PluginInstance", + "shortName": "update_plugin_instance" }, - "description": "Sample for ListExternalApis", - "file": "apihub_v1_generated_api_hub_list_external_apis_sync.py", + "description": "Sample for UpdatePluginInstance", + "file": "apihub_v1_generated_api_hub_plugin_update_plugin_instance_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_ListExternalApis_sync", + "regionTag": "apihub_v1_generated_ApiHubPlugin_UpdatePluginInstance_sync", "segments": [ { - "end": 52, + "end": 55, "start": 27, "type": "FULL" }, { - "end": 52, + "end": 55, "start": 27, "type": "SHORT" }, @@ -4647,48 +4779,57 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 45, + "end": 49, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 48, - "start": 46, + "end": 52, + "start": 50, "type": "REQUEST_EXECUTION" }, { - "end": 53, - "start": 49, + "end": 56, + "start": 53, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_list_external_apis_sync.py" + "title": "apihub_v1_generated_api_hub_plugin_update_plugin_instance_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.list_specs", + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.create_api_operation", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.ListSpecs", + "fullName": "google.cloud.apihub.v1.ApiHub.CreateApiOperation", "service": { "fullName": "google.cloud.apihub.v1.ApiHub", "shortName": "ApiHub" }, - "shortName": "ListSpecs" + "shortName": "CreateApiOperation" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.ListSpecsRequest" + "type": "google.cloud.apihub_v1.types.CreateApiOperationRequest" }, { "name": "parent", "type": "str" }, + { + "name": "api_operation", + "type": "google.cloud.apihub_v1.types.ApiOperation" + }, + { + "name": "api_operation_id", + "type": "str" + }, { "name": "retry", "type": "google.api_core.retry.Retry" @@ -4702,22 +4843,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListSpecsPager", - "shortName": "list_specs" + "resultType": "google.cloud.apihub_v1.types.ApiOperation", + "shortName": "create_api_operation" }, - "description": "Sample for ListSpecs", - "file": "apihub_v1_generated_api_hub_list_specs_sync.py", + "description": "Sample for CreateApiOperation", + "file": "apihub_v1_generated_api_hub_create_api_operation_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_ListSpecs_sync", + "regionTag": "apihub_v1_generated_ApiHub_CreateApiOperation_async", "segments": [ { - "end": 52, + "end": 51, "start": 27, "type": "FULL" }, { - "end": 52, + "end": 51, "start": 27, "type": "SHORT" }, @@ -4737,12 +4878,12 @@ "type": "REQUEST_EXECUTION" }, { - "end": 53, + "end": 52, "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_list_specs_sync.py" + "title": "apihub_v1_generated_api_hub_create_api_operation_async.py" }, { "canonical": true, @@ -4751,24 +4892,32 @@ "fullName": "google.cloud.apihub_v1.ApiHubClient", "shortName": "ApiHubClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.list_versions", + "fullName": "google.cloud.apihub_v1.ApiHubClient.create_api_operation", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.ListVersions", + "fullName": "google.cloud.apihub.v1.ApiHub.CreateApiOperation", "service": { "fullName": "google.cloud.apihub.v1.ApiHub", "shortName": "ApiHub" }, - "shortName": "ListVersions" + "shortName": "CreateApiOperation" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.ListVersionsRequest" + "type": "google.cloud.apihub_v1.types.CreateApiOperationRequest" }, { "name": "parent", "type": "str" }, + { + "name": "api_operation", + "type": "google.cloud.apihub_v1.types.ApiOperation" + }, + { + "name": "api_operation_id", + "type": "str" + }, { "name": "retry", "type": "google.api_core.retry.Retry" @@ -4782,22 +4931,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListVersionsPager", - "shortName": "list_versions" + "resultType": "google.cloud.apihub_v1.types.ApiOperation", + "shortName": "create_api_operation" }, - "description": "Sample for ListVersions", - "file": "apihub_v1_generated_api_hub_list_versions_sync.py", + "description": "Sample for CreateApiOperation", + "file": "apihub_v1_generated_api_hub_create_api_operation_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_ListVersions_sync", + "regionTag": "apihub_v1_generated_ApiHub_CreateApiOperation_sync", "segments": [ { - "end": 52, + "end": 51, "start": 27, "type": "FULL" }, { - "end": 52, + "end": 51, "start": 27, "type": "SHORT" }, @@ -4817,40 +4966,45 @@ "type": "REQUEST_EXECUTION" }, { - "end": 53, + "end": 52, "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_list_versions_sync.py" + "title": "apihub_v1_generated_api_hub_create_api_operation_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.search_resources", + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.create_api", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.SearchResources", + "fullName": "google.cloud.apihub.v1.ApiHub.CreateApi", "service": { "fullName": "google.cloud.apihub.v1.ApiHub", "shortName": "ApiHub" }, - "shortName": "SearchResources" + "shortName": "CreateApi" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.SearchResourcesRequest" + "type": "google.cloud.apihub_v1.types.CreateApiRequest" }, { - "name": "location", + "name": "parent", "type": "str" }, { - "name": "query", + "name": "api", + "type": "google.cloud.apihub_v1.types.Api" + }, + { + "name": "api_id", "type": "str" }, { @@ -4866,22 +5020,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.SearchResourcesPager", - "shortName": "search_resources" + "resultType": "google.cloud.apihub_v1.types.Api", + "shortName": "create_api" }, - "description": "Sample for SearchResources", - "file": "apihub_v1_generated_api_hub_search_resources_sync.py", + "description": "Sample for CreateApi", + "file": "apihub_v1_generated_api_hub_create_api_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_SearchResources_sync", + "regionTag": "apihub_v1_generated_ApiHub_CreateApi_async", "segments": [ { - "end": 53, + "end": 55, "start": 27, "type": "FULL" }, { - "end": 53, + "end": 55, "start": 27, "type": "SHORT" }, @@ -4891,22 +5045,22 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 46, + "end": 49, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 49, - "start": 47, + "end": 52, + "start": 50, "type": "REQUEST_EXECUTION" }, { - "end": 54, - "start": 50, + "end": 56, + "start": 53, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_search_resources_sync.py" + "title": "apihub_v1_generated_api_hub_create_api_async.py" }, { "canonical": true, @@ -4915,27 +5069,31 @@ "fullName": "google.cloud.apihub_v1.ApiHubClient", "shortName": "ApiHubClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.update_api_operation", + "fullName": "google.cloud.apihub_v1.ApiHubClient.create_api", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.UpdateApiOperation", + "fullName": "google.cloud.apihub.v1.ApiHub.CreateApi", "service": { "fullName": "google.cloud.apihub.v1.ApiHub", "shortName": "ApiHub" }, - "shortName": "UpdateApiOperation" + "shortName": "CreateApi" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.UpdateApiOperationRequest" + "type": "google.cloud.apihub_v1.types.CreateApiRequest" }, { - "name": "api_operation", - "type": "google.cloud.apihub_v1.types.ApiOperation" + "name": "parent", + "type": "str" }, { - "name": "update_mask", - "type": "google.protobuf.field_mask_pb2.FieldMask" + "name": "api", + "type": "google.cloud.apihub_v1.types.Api" + }, + { + "name": "api_id", + "type": "str" }, { "name": "retry", @@ -4950,22 +5108,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.ApiOperation", - "shortName": "update_api_operation" + "resultType": "google.cloud.apihub_v1.types.Api", + "shortName": "create_api" }, - "description": "Sample for UpdateApiOperation", - "file": "apihub_v1_generated_api_hub_update_api_operation_sync.py", + "description": "Sample for CreateApi", + "file": "apihub_v1_generated_api_hub_create_api_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_UpdateApiOperation_sync", + "regionTag": "apihub_v1_generated_ApiHub_CreateApi_sync", "segments": [ { - "end": 50, + "end": 55, "start": 27, "type": "FULL" }, { - "end": 50, + "end": 55, "start": 27, "type": "SHORT" }, @@ -4975,51 +5133,56 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 44, + "end": 49, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 47, - "start": 45, + "end": 52, + "start": 50, "type": "REQUEST_EXECUTION" }, { - "end": 51, - "start": 48, + "end": 56, + "start": 53, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_update_api_operation_sync.py" + "title": "apihub_v1_generated_api_hub_create_api_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.update_api", + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.create_attribute", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.UpdateApi", + "fullName": "google.cloud.apihub.v1.ApiHub.CreateAttribute", "service": { "fullName": "google.cloud.apihub.v1.ApiHub", "shortName": "ApiHub" }, - "shortName": "UpdateApi" + "shortName": "CreateAttribute" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.UpdateApiRequest" + "type": "google.cloud.apihub_v1.types.CreateAttributeRequest" }, { - "name": "api", - "type": "google.cloud.apihub_v1.types.Api" + "name": "parent", + "type": "str" }, { - "name": "update_mask", - "type": "google.protobuf.field_mask_pb2.FieldMask" + "name": "attribute", + "type": "google.cloud.apihub_v1.types.Attribute" + }, + { + "name": "attribute_id", + "type": "str" }, { "name": "retry", @@ -5034,22 +5197,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Api", - "shortName": "update_api" + "resultType": "google.cloud.apihub_v1.types.Attribute", + "shortName": "create_attribute" }, - "description": "Sample for UpdateApi", - "file": "apihub_v1_generated_api_hub_update_api_sync.py", + "description": "Sample for CreateAttribute", + "file": "apihub_v1_generated_api_hub_create_attribute_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_UpdateApi_sync", + "regionTag": "apihub_v1_generated_ApiHub_CreateAttribute_async", "segments": [ { - "end": 54, + "end": 57, "start": 27, "type": "FULL" }, { - "end": 54, + "end": 57, "start": 27, "type": "SHORT" }, @@ -5059,22 +5222,22 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 48, + "end": 51, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 51, - "start": 49, + "end": 54, + "start": 52, "type": "REQUEST_EXECUTION" }, { - "end": 55, - "start": 52, + "end": 58, + "start": 55, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_update_api_sync.py" + "title": "apihub_v1_generated_api_hub_create_attribute_async.py" }, { "canonical": true, @@ -5083,27 +5246,31 @@ "fullName": "google.cloud.apihub_v1.ApiHubClient", "shortName": "ApiHubClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.update_attribute", + "fullName": "google.cloud.apihub_v1.ApiHubClient.create_attribute", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.UpdateAttribute", + "fullName": "google.cloud.apihub.v1.ApiHub.CreateAttribute", "service": { "fullName": "google.cloud.apihub.v1.ApiHub", "shortName": "ApiHub" }, - "shortName": "UpdateAttribute" + "shortName": "CreateAttribute" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.UpdateAttributeRequest" + "type": "google.cloud.apihub_v1.types.CreateAttributeRequest" + }, + { + "name": "parent", + "type": "str" }, { "name": "attribute", "type": "google.cloud.apihub_v1.types.Attribute" }, { - "name": "update_mask", - "type": "google.protobuf.field_mask_pb2.FieldMask" + "name": "attribute_id", + "type": "str" }, { "name": "retry", @@ -5119,21 +5286,21 @@ } ], "resultType": "google.cloud.apihub_v1.types.Attribute", - "shortName": "update_attribute" + "shortName": "create_attribute" }, - "description": "Sample for UpdateAttribute", - "file": "apihub_v1_generated_api_hub_update_attribute_sync.py", + "description": "Sample for CreateAttribute", + "file": "apihub_v1_generated_api_hub_create_attribute_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_UpdateAttribute_sync", + "regionTag": "apihub_v1_generated_ApiHub_CreateAttribute_sync", "segments": [ { - "end": 56, + "end": 57, "start": 27, "type": "FULL" }, { - "end": 56, + "end": 57, "start": 27, "type": "SHORT" }, @@ -5143,51 +5310,56 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 50, + "end": 51, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 53, - "start": 51, + "end": 54, + "start": 52, "type": "REQUEST_EXECUTION" }, { - "end": 57, - "start": 54, + "end": 58, + "start": 55, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_update_attribute_sync.py" + "title": "apihub_v1_generated_api_hub_create_attribute_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.update_deployment", + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.create_deployment", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.UpdateDeployment", + "fullName": "google.cloud.apihub.v1.ApiHub.CreateDeployment", "service": { "fullName": "google.cloud.apihub.v1.ApiHub", "shortName": "ApiHub" }, - "shortName": "UpdateDeployment" + "shortName": "CreateDeployment" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.UpdateDeploymentRequest" + "type": "google.cloud.apihub_v1.types.CreateDeploymentRequest" + }, + { + "name": "parent", + "type": "str" }, { "name": "deployment", "type": "google.cloud.apihub_v1.types.Deployment" }, { - "name": "update_mask", - "type": "google.protobuf.field_mask_pb2.FieldMask" + "name": "deployment_id", + "type": "str" }, { "name": "retry", @@ -5203,21 +5375,21 @@ } ], "resultType": "google.cloud.apihub_v1.types.Deployment", - "shortName": "update_deployment" + "shortName": "create_deployment" }, - "description": "Sample for UpdateDeployment", - "file": "apihub_v1_generated_api_hub_update_deployment_sync.py", + "description": "Sample for CreateDeployment", + "file": "apihub_v1_generated_api_hub_create_deployment_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_UpdateDeployment_sync", + "regionTag": "apihub_v1_generated_ApiHub_CreateDeployment_async", "segments": [ { - "end": 58, + "end": 59, "start": 27, "type": "FULL" }, { - "end": 58, + "end": 59, "start": 27, "type": "SHORT" }, @@ -5227,22 +5399,22 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 52, + "end": 53, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 55, - "start": 53, + "end": 56, + "start": 54, "type": "REQUEST_EXECUTION" }, { - "end": 59, - "start": 56, + "end": 60, + "start": 57, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_update_deployment_sync.py" + "title": "apihub_v1_generated_api_hub_create_deployment_async.py" }, { "canonical": true, @@ -5251,27 +5423,31 @@ "fullName": "google.cloud.apihub_v1.ApiHubClient", "shortName": "ApiHubClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.update_external_api", + "fullName": "google.cloud.apihub_v1.ApiHubClient.create_deployment", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.UpdateExternalApi", + "fullName": "google.cloud.apihub.v1.ApiHub.CreateDeployment", "service": { "fullName": "google.cloud.apihub.v1.ApiHub", "shortName": "ApiHub" }, - "shortName": "UpdateExternalApi" + "shortName": "CreateDeployment" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.UpdateExternalApiRequest" + "type": "google.cloud.apihub_v1.types.CreateDeploymentRequest" }, { - "name": "external_api", - "type": "google.cloud.apihub_v1.types.ExternalApi" + "name": "parent", + "type": "str" }, { - "name": "update_mask", - "type": "google.protobuf.field_mask_pb2.FieldMask" + "name": "deployment", + "type": "google.cloud.apihub_v1.types.Deployment" + }, + { + "name": "deployment_id", + "type": "str" }, { "name": "retry", @@ -5286,22 +5462,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.ExternalApi", - "shortName": "update_external_api" + "resultType": "google.cloud.apihub_v1.types.Deployment", + "shortName": "create_deployment" }, - "description": "Sample for UpdateExternalApi", - "file": "apihub_v1_generated_api_hub_update_external_api_sync.py", + "description": "Sample for CreateDeployment", + "file": "apihub_v1_generated_api_hub_create_deployment_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_UpdateExternalApi_sync", + "regionTag": "apihub_v1_generated_ApiHub_CreateDeployment_sync", "segments": [ { - "end": 54, + "end": 59, "start": 27, "type": "FULL" }, { - "end": 54, + "end": 59, "start": 27, "type": "SHORT" }, @@ -5311,51 +5487,56 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 48, + "end": 53, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 51, - "start": 49, + "end": 56, + "start": 54, "type": "REQUEST_EXECUTION" }, { - "end": 55, - "start": 52, + "end": 60, + "start": 57, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_update_external_api_sync.py" + "title": "apihub_v1_generated_api_hub_create_deployment_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ApiHubClient", - "shortName": "ApiHubClient" + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.update_spec", + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.create_external_api", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.UpdateSpec", + "fullName": "google.cloud.apihub.v1.ApiHub.CreateExternalApi", "service": { "fullName": "google.cloud.apihub.v1.ApiHub", "shortName": "ApiHub" }, - "shortName": "UpdateSpec" + "shortName": "CreateExternalApi" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.UpdateSpecRequest" + "type": "google.cloud.apihub_v1.types.CreateExternalApiRequest" }, { - "name": "spec", - "type": "google.cloud.apihub_v1.types.Spec" + "name": "parent", + "type": "str" }, { - "name": "update_mask", - "type": "google.protobuf.field_mask_pb2.FieldMask" + "name": "external_api", + "type": "google.cloud.apihub_v1.types.ExternalApi" + }, + { + "name": "external_api_id", + "type": "str" }, { "name": "retry", @@ -5370,22 +5551,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Spec", - "shortName": "update_spec" + "resultType": "google.cloud.apihub_v1.types.ExternalApi", + "shortName": "create_external_api" }, - "description": "Sample for UpdateSpec", - "file": "apihub_v1_generated_api_hub_update_spec_sync.py", + "description": "Sample for CreateExternalApi", + "file": "apihub_v1_generated_api_hub_create_external_api_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_UpdateSpec_sync", + "regionTag": "apihub_v1_generated_ApiHub_CreateExternalApi_async", "segments": [ { - "end": 56, + "end": 55, "start": 27, "type": "FULL" }, { - "end": 56, + "end": 55, "start": 27, "type": "SHORT" }, @@ -5395,22 +5576,22 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 50, + "end": 49, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 53, - "start": 51, + "end": 52, + "start": 50, "type": "REQUEST_EXECUTION" }, { - "end": 57, - "start": 54, + "end": 56, + "start": 53, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_update_spec_sync.py" + "title": "apihub_v1_generated_api_hub_create_external_api_async.py" }, { "canonical": true, @@ -5419,27 +5600,31 @@ "fullName": "google.cloud.apihub_v1.ApiHubClient", "shortName": "ApiHubClient" }, - "fullName": "google.cloud.apihub_v1.ApiHubClient.update_version", + "fullName": "google.cloud.apihub_v1.ApiHubClient.create_external_api", "method": { - "fullName": "google.cloud.apihub.v1.ApiHub.UpdateVersion", + "fullName": "google.cloud.apihub.v1.ApiHub.CreateExternalApi", "service": { "fullName": "google.cloud.apihub.v1.ApiHub", "shortName": "ApiHub" }, - "shortName": "UpdateVersion" + "shortName": "CreateExternalApi" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.UpdateVersionRequest" + "type": "google.cloud.apihub_v1.types.CreateExternalApiRequest" }, { - "name": "version", - "type": "google.cloud.apihub_v1.types.Version" + "name": "parent", + "type": "str" }, { - "name": "update_mask", - "type": "google.protobuf.field_mask_pb2.FieldMask" + "name": "external_api", + "type": "google.cloud.apihub_v1.types.ExternalApi" + }, + { + "name": "external_api_id", + "type": "str" }, { "name": "retry", @@ -5454,22 +5639,6703 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.Version", - "shortName": "update_version" + "resultType": "google.cloud.apihub_v1.types.ExternalApi", + "shortName": "create_external_api" }, - "description": "Sample for UpdateVersion", - "file": "apihub_v1_generated_api_hub_update_version_sync.py", + "description": "Sample for CreateExternalApi", + "file": "apihub_v1_generated_api_hub_create_external_api_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_ApiHub_UpdateVersion_sync", + "regionTag": "apihub_v1_generated_ApiHub_CreateExternalApi_sync", + "segments": [ + { + "end": 55, + "start": 27, + "type": "FULL" + }, + { + "end": 55, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 49, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 52, + "start": 50, + "type": "REQUEST_EXECUTION" + }, + { + "end": 56, + "start": 53, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_create_external_api_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.create_spec", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.CreateSpec", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "CreateSpec" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.CreateSpecRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "spec", + "type": "google.cloud.apihub_v1.types.Spec" + }, + { + "name": "spec_id", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Spec", + "shortName": "create_spec" + }, + "description": "Sample for CreateSpec", + "file": "apihub_v1_generated_api_hub_create_spec_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_CreateSpec_async", + "segments": [ + { + "end": 57, + "start": 27, + "type": "FULL" + }, + { + "end": 57, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 51, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 54, + "start": 52, + "type": "REQUEST_EXECUTION" + }, + { + "end": 58, + "start": 55, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_create_spec_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.create_spec", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.CreateSpec", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "CreateSpec" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.CreateSpecRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "spec", + "type": "google.cloud.apihub_v1.types.Spec" + }, + { + "name": "spec_id", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Spec", + "shortName": "create_spec" + }, + "description": "Sample for CreateSpec", + "file": "apihub_v1_generated_api_hub_create_spec_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_CreateSpec_sync", + "segments": [ + { + "end": 57, + "start": 27, + "type": "FULL" + }, + { + "end": 57, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 51, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 54, + "start": 52, + "type": "REQUEST_EXECUTION" + }, + { + "end": 58, + "start": 55, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_create_spec_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.create_version", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.CreateVersion", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "CreateVersion" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.CreateVersionRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "version", + "type": "google.cloud.apihub_v1.types.Version" + }, + { + "name": "version_id", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Version", + "shortName": "create_version" + }, + "description": "Sample for CreateVersion", + "file": "apihub_v1_generated_api_hub_create_version_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_CreateVersion_async", + "segments": [ + { + "end": 55, + "start": 27, + "type": "FULL" + }, + { + "end": 55, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 49, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 52, + "start": 50, + "type": "REQUEST_EXECUTION" + }, + { + "end": 56, + "start": 53, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_create_version_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.create_version", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.CreateVersion", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "CreateVersion" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.CreateVersionRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "version", + "type": "google.cloud.apihub_v1.types.Version" + }, + { + "name": "version_id", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Version", + "shortName": "create_version" + }, + "description": "Sample for CreateVersion", + "file": "apihub_v1_generated_api_hub_create_version_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_CreateVersion_sync", + "segments": [ + { + "end": 55, + "start": 27, + "type": "FULL" + }, + { + "end": 55, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 49, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 52, + "start": 50, + "type": "REQUEST_EXECUTION" + }, + { + "end": 56, + "start": 53, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_create_version_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.delete_api_operation", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.DeleteApiOperation", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "DeleteApiOperation" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.DeleteApiOperationRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "shortName": "delete_api_operation" + }, + "description": "Sample for DeleteApiOperation", + "file": "apihub_v1_generated_api_hub_delete_api_operation_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_DeleteApiOperation_async", + "segments": [ + { + "end": 49, + "start": 27, + "type": "FULL" + }, + { + "end": 49, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 50, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_delete_api_operation_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.delete_api_operation", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.DeleteApiOperation", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "DeleteApiOperation" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.DeleteApiOperationRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "shortName": "delete_api_operation" + }, + "description": "Sample for DeleteApiOperation", + "file": "apihub_v1_generated_api_hub_delete_api_operation_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_DeleteApiOperation_sync", + "segments": [ + { + "end": 49, + "start": 27, + "type": "FULL" + }, + { + "end": 49, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 50, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_delete_api_operation_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.delete_api", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.DeleteApi", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "DeleteApi" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.DeleteApiRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "shortName": "delete_api" + }, + "description": "Sample for DeleteApi", + "file": "apihub_v1_generated_api_hub_delete_api_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_DeleteApi_async", + "segments": [ + { + "end": 49, + "start": 27, + "type": "FULL" + }, + { + "end": 49, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 50, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_delete_api_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.delete_api", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.DeleteApi", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "DeleteApi" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.DeleteApiRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "shortName": "delete_api" + }, + "description": "Sample for DeleteApi", + "file": "apihub_v1_generated_api_hub_delete_api_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_DeleteApi_sync", + "segments": [ + { + "end": 49, + "start": 27, + "type": "FULL" + }, + { + "end": 49, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 50, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_delete_api_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.delete_attribute", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.DeleteAttribute", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "DeleteAttribute" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.DeleteAttributeRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "shortName": "delete_attribute" + }, + "description": "Sample for DeleteAttribute", + "file": "apihub_v1_generated_api_hub_delete_attribute_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_DeleteAttribute_async", + "segments": [ + { + "end": 49, + "start": 27, + "type": "FULL" + }, + { + "end": 49, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 50, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_delete_attribute_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.delete_attribute", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.DeleteAttribute", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "DeleteAttribute" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.DeleteAttributeRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "shortName": "delete_attribute" + }, + "description": "Sample for DeleteAttribute", + "file": "apihub_v1_generated_api_hub_delete_attribute_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_DeleteAttribute_sync", + "segments": [ + { + "end": 49, + "start": 27, + "type": "FULL" + }, + { + "end": 49, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 50, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_delete_attribute_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.delete_deployment", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.DeleteDeployment", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "DeleteDeployment" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.DeleteDeploymentRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "shortName": "delete_deployment" + }, + "description": "Sample for DeleteDeployment", + "file": "apihub_v1_generated_api_hub_delete_deployment_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_DeleteDeployment_async", + "segments": [ + { + "end": 49, + "start": 27, + "type": "FULL" + }, + { + "end": 49, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 50, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_delete_deployment_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.delete_deployment", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.DeleteDeployment", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "DeleteDeployment" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.DeleteDeploymentRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "shortName": "delete_deployment" + }, + "description": "Sample for DeleteDeployment", + "file": "apihub_v1_generated_api_hub_delete_deployment_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_DeleteDeployment_sync", + "segments": [ + { + "end": 49, + "start": 27, + "type": "FULL" + }, + { + "end": 49, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 50, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_delete_deployment_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.delete_external_api", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.DeleteExternalApi", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "DeleteExternalApi" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.DeleteExternalApiRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "shortName": "delete_external_api" + }, + "description": "Sample for DeleteExternalApi", + "file": "apihub_v1_generated_api_hub_delete_external_api_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_DeleteExternalApi_async", + "segments": [ + { + "end": 49, + "start": 27, + "type": "FULL" + }, + { + "end": 49, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 50, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_delete_external_api_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.delete_external_api", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.DeleteExternalApi", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "DeleteExternalApi" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.DeleteExternalApiRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "shortName": "delete_external_api" + }, + "description": "Sample for DeleteExternalApi", + "file": "apihub_v1_generated_api_hub_delete_external_api_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_DeleteExternalApi_sync", + "segments": [ + { + "end": 49, + "start": 27, + "type": "FULL" + }, + { + "end": 49, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 50, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_delete_external_api_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.delete_spec", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.DeleteSpec", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "DeleteSpec" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.DeleteSpecRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "shortName": "delete_spec" + }, + "description": "Sample for DeleteSpec", + "file": "apihub_v1_generated_api_hub_delete_spec_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_DeleteSpec_async", + "segments": [ + { + "end": 49, + "start": 27, + "type": "FULL" + }, + { + "end": 49, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 50, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_delete_spec_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.delete_spec", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.DeleteSpec", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "DeleteSpec" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.DeleteSpecRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "shortName": "delete_spec" + }, + "description": "Sample for DeleteSpec", + "file": "apihub_v1_generated_api_hub_delete_spec_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_DeleteSpec_sync", + "segments": [ + { + "end": 49, + "start": 27, + "type": "FULL" + }, + { + "end": 49, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 50, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_delete_spec_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.delete_version", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.DeleteVersion", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "DeleteVersion" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.DeleteVersionRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "shortName": "delete_version" + }, + "description": "Sample for DeleteVersion", + "file": "apihub_v1_generated_api_hub_delete_version_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_DeleteVersion_async", + "segments": [ + { + "end": 49, + "start": 27, + "type": "FULL" + }, + { + "end": 49, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 50, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_delete_version_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.delete_version", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.DeleteVersion", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "DeleteVersion" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.DeleteVersionRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "shortName": "delete_version" + }, + "description": "Sample for DeleteVersion", + "file": "apihub_v1_generated_api_hub_delete_version_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_DeleteVersion_sync", + "segments": [ + { + "end": 49, + "start": 27, + "type": "FULL" + }, + { + "end": 49, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 50, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_delete_version_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.get_api_operation", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.GetApiOperation", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "GetApiOperation" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetApiOperationRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.ApiOperation", + "shortName": "get_api_operation" + }, + "description": "Sample for GetApiOperation", + "file": "apihub_v1_generated_api_hub_get_api_operation_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_GetApiOperation_async", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_get_api_operation_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.get_api_operation", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.GetApiOperation", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "GetApiOperation" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetApiOperationRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.ApiOperation", + "shortName": "get_api_operation" + }, + "description": "Sample for GetApiOperation", + "file": "apihub_v1_generated_api_hub_get_api_operation_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_GetApiOperation_sync", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_get_api_operation_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.get_api", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.GetApi", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "GetApi" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetApiRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Api", + "shortName": "get_api" + }, + "description": "Sample for GetApi", + "file": "apihub_v1_generated_api_hub_get_api_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_GetApi_async", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_get_api_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.get_api", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.GetApi", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "GetApi" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetApiRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Api", + "shortName": "get_api" + }, + "description": "Sample for GetApi", + "file": "apihub_v1_generated_api_hub_get_api_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_GetApi_sync", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_get_api_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.get_attribute", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.GetAttribute", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "GetAttribute" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetAttributeRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Attribute", + "shortName": "get_attribute" + }, + "description": "Sample for GetAttribute", + "file": "apihub_v1_generated_api_hub_get_attribute_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_GetAttribute_async", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_get_attribute_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.get_attribute", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.GetAttribute", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "GetAttribute" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetAttributeRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Attribute", + "shortName": "get_attribute" + }, + "description": "Sample for GetAttribute", + "file": "apihub_v1_generated_api_hub_get_attribute_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_GetAttribute_sync", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_get_attribute_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.get_definition", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.GetDefinition", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "GetDefinition" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetDefinitionRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Definition", + "shortName": "get_definition" + }, + "description": "Sample for GetDefinition", + "file": "apihub_v1_generated_api_hub_get_definition_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_GetDefinition_async", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_get_definition_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.get_definition", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.GetDefinition", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "GetDefinition" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetDefinitionRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Definition", + "shortName": "get_definition" + }, + "description": "Sample for GetDefinition", + "file": "apihub_v1_generated_api_hub_get_definition_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_GetDefinition_sync", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_get_definition_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.get_deployment", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.GetDeployment", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "GetDeployment" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetDeploymentRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Deployment", + "shortName": "get_deployment" + }, + "description": "Sample for GetDeployment", + "file": "apihub_v1_generated_api_hub_get_deployment_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_GetDeployment_async", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_get_deployment_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.get_deployment", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.GetDeployment", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "GetDeployment" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetDeploymentRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Deployment", + "shortName": "get_deployment" + }, + "description": "Sample for GetDeployment", + "file": "apihub_v1_generated_api_hub_get_deployment_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_GetDeployment_sync", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_get_deployment_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.get_external_api", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.GetExternalApi", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "GetExternalApi" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetExternalApiRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.ExternalApi", + "shortName": "get_external_api" + }, + "description": "Sample for GetExternalApi", + "file": "apihub_v1_generated_api_hub_get_external_api_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_GetExternalApi_async", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_get_external_api_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.get_external_api", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.GetExternalApi", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "GetExternalApi" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetExternalApiRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.ExternalApi", + "shortName": "get_external_api" + }, + "description": "Sample for GetExternalApi", + "file": "apihub_v1_generated_api_hub_get_external_api_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_GetExternalApi_sync", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_get_external_api_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.get_spec_contents", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.GetSpecContents", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "GetSpecContents" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetSpecContentsRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.SpecContents", + "shortName": "get_spec_contents" + }, + "description": "Sample for GetSpecContents", + "file": "apihub_v1_generated_api_hub_get_spec_contents_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_GetSpecContents_async", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_get_spec_contents_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.get_spec_contents", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.GetSpecContents", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "GetSpecContents" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetSpecContentsRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.SpecContents", + "shortName": "get_spec_contents" + }, + "description": "Sample for GetSpecContents", + "file": "apihub_v1_generated_api_hub_get_spec_contents_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_GetSpecContents_sync", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_get_spec_contents_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.get_spec", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.GetSpec", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "GetSpec" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetSpecRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Spec", + "shortName": "get_spec" + }, + "description": "Sample for GetSpec", + "file": "apihub_v1_generated_api_hub_get_spec_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_GetSpec_async", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_get_spec_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.get_spec", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.GetSpec", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "GetSpec" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetSpecRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Spec", + "shortName": "get_spec" + }, + "description": "Sample for GetSpec", + "file": "apihub_v1_generated_api_hub_get_spec_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_GetSpec_sync", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_get_spec_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.get_version", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.GetVersion", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "GetVersion" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetVersionRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Version", + "shortName": "get_version" + }, + "description": "Sample for GetVersion", + "file": "apihub_v1_generated_api_hub_get_version_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_GetVersion_async", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_get_version_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.get_version", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.GetVersion", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "GetVersion" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetVersionRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Version", + "shortName": "get_version" + }, + "description": "Sample for GetVersion", + "file": "apihub_v1_generated_api_hub_get_version_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_GetVersion_sync", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_get_version_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.list_api_operations", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.ListApiOperations", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "ListApiOperations" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.ListApiOperationsRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListApiOperationsAsyncPager", + "shortName": "list_api_operations" + }, + "description": "Sample for ListApiOperations", + "file": "apihub_v1_generated_api_hub_list_api_operations_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_ListApiOperations_async", + "segments": [ + { + "end": 52, + "start": 27, + "type": "FULL" + }, + { + "end": 52, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 53, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_list_api_operations_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.list_api_operations", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.ListApiOperations", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "ListApiOperations" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.ListApiOperationsRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListApiOperationsPager", + "shortName": "list_api_operations" + }, + "description": "Sample for ListApiOperations", + "file": "apihub_v1_generated_api_hub_list_api_operations_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_ListApiOperations_sync", + "segments": [ + { + "end": 52, + "start": 27, + "type": "FULL" + }, + { + "end": 52, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 53, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_list_api_operations_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.list_apis", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.ListApis", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "ListApis" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.ListApisRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListApisAsyncPager", + "shortName": "list_apis" + }, + "description": "Sample for ListApis", + "file": "apihub_v1_generated_api_hub_list_apis_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_ListApis_async", + "segments": [ + { + "end": 52, + "start": 27, + "type": "FULL" + }, + { + "end": 52, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 53, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_list_apis_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.list_apis", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.ListApis", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "ListApis" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.ListApisRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListApisPager", + "shortName": "list_apis" + }, + "description": "Sample for ListApis", + "file": "apihub_v1_generated_api_hub_list_apis_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_ListApis_sync", + "segments": [ + { + "end": 52, + "start": 27, + "type": "FULL" + }, + { + "end": 52, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 53, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_list_apis_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.list_attributes", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.ListAttributes", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "ListAttributes" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.ListAttributesRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListAttributesAsyncPager", + "shortName": "list_attributes" + }, + "description": "Sample for ListAttributes", + "file": "apihub_v1_generated_api_hub_list_attributes_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_ListAttributes_async", + "segments": [ + { + "end": 52, + "start": 27, + "type": "FULL" + }, + { + "end": 52, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 53, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_list_attributes_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.list_attributes", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.ListAttributes", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "ListAttributes" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.ListAttributesRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListAttributesPager", + "shortName": "list_attributes" + }, + "description": "Sample for ListAttributes", + "file": "apihub_v1_generated_api_hub_list_attributes_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_ListAttributes_sync", + "segments": [ + { + "end": 52, + "start": 27, + "type": "FULL" + }, + { + "end": 52, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 53, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_list_attributes_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.list_deployments", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.ListDeployments", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "ListDeployments" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.ListDeploymentsRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListDeploymentsAsyncPager", + "shortName": "list_deployments" + }, + "description": "Sample for ListDeployments", + "file": "apihub_v1_generated_api_hub_list_deployments_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_ListDeployments_async", + "segments": [ + { + "end": 52, + "start": 27, + "type": "FULL" + }, + { + "end": 52, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 53, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_list_deployments_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.list_deployments", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.ListDeployments", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "ListDeployments" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.ListDeploymentsRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListDeploymentsPager", + "shortName": "list_deployments" + }, + "description": "Sample for ListDeployments", + "file": "apihub_v1_generated_api_hub_list_deployments_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_ListDeployments_sync", + "segments": [ + { + "end": 52, + "start": 27, + "type": "FULL" + }, + { + "end": 52, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 53, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_list_deployments_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.list_external_apis", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.ListExternalApis", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "ListExternalApis" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.ListExternalApisRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListExternalApisAsyncPager", + "shortName": "list_external_apis" + }, + "description": "Sample for ListExternalApis", + "file": "apihub_v1_generated_api_hub_list_external_apis_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_ListExternalApis_async", + "segments": [ + { + "end": 52, + "start": 27, + "type": "FULL" + }, + { + "end": 52, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 53, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_list_external_apis_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.list_external_apis", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.ListExternalApis", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "ListExternalApis" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.ListExternalApisRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListExternalApisPager", + "shortName": "list_external_apis" + }, + "description": "Sample for ListExternalApis", + "file": "apihub_v1_generated_api_hub_list_external_apis_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_ListExternalApis_sync", + "segments": [ + { + "end": 52, + "start": 27, + "type": "FULL" + }, + { + "end": 52, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 53, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_list_external_apis_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.list_specs", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.ListSpecs", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "ListSpecs" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.ListSpecsRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListSpecsAsyncPager", + "shortName": "list_specs" + }, + "description": "Sample for ListSpecs", + "file": "apihub_v1_generated_api_hub_list_specs_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_ListSpecs_async", + "segments": [ + { + "end": 52, + "start": 27, + "type": "FULL" + }, + { + "end": 52, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 53, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_list_specs_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.list_specs", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.ListSpecs", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "ListSpecs" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.ListSpecsRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListSpecsPager", + "shortName": "list_specs" + }, + "description": "Sample for ListSpecs", + "file": "apihub_v1_generated_api_hub_list_specs_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_ListSpecs_sync", + "segments": [ + { + "end": 52, + "start": 27, + "type": "FULL" + }, + { + "end": 52, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 53, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_list_specs_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.list_versions", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.ListVersions", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "ListVersions" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.ListVersionsRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListVersionsAsyncPager", + "shortName": "list_versions" + }, + "description": "Sample for ListVersions", + "file": "apihub_v1_generated_api_hub_list_versions_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_ListVersions_async", + "segments": [ + { + "end": 52, + "start": 27, + "type": "FULL" + }, + { + "end": 52, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 53, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_list_versions_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.list_versions", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.ListVersions", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "ListVersions" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.ListVersionsRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.ListVersionsPager", + "shortName": "list_versions" + }, + "description": "Sample for ListVersions", + "file": "apihub_v1_generated_api_hub_list_versions_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_ListVersions_sync", + "segments": [ + { + "end": 52, + "start": 27, + "type": "FULL" + }, + { + "end": 52, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 53, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_list_versions_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.search_resources", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.SearchResources", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "SearchResources" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.SearchResourcesRequest" + }, + { + "name": "location", + "type": "str" + }, + { + "name": "query", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.SearchResourcesAsyncPager", + "shortName": "search_resources" + }, + "description": "Sample for SearchResources", + "file": "apihub_v1_generated_api_hub_search_resources_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_SearchResources_async", + "segments": [ + { + "end": 53, + "start": 27, + "type": "FULL" + }, + { + "end": 53, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 46, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 49, + "start": 47, + "type": "REQUEST_EXECUTION" + }, + { + "end": 54, + "start": 50, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_search_resources_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.search_resources", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.SearchResources", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "SearchResources" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.SearchResourcesRequest" + }, + { + "name": "location", + "type": "str" + }, + { + "name": "query", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.services.api_hub.pagers.SearchResourcesPager", + "shortName": "search_resources" + }, + "description": "Sample for SearchResources", + "file": "apihub_v1_generated_api_hub_search_resources_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_SearchResources_sync", + "segments": [ + { + "end": 53, + "start": 27, + "type": "FULL" + }, + { + "end": 53, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 46, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 49, + "start": 47, + "type": "REQUEST_EXECUTION" + }, + { + "end": 54, + "start": 50, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_search_resources_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.update_api_operation", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.UpdateApiOperation", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "UpdateApiOperation" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.UpdateApiOperationRequest" + }, + { + "name": "api_operation", + "type": "google.cloud.apihub_v1.types.ApiOperation" + }, + { + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.ApiOperation", + "shortName": "update_api_operation" + }, + "description": "Sample for UpdateApiOperation", + "file": "apihub_v1_generated_api_hub_update_api_operation_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_UpdateApiOperation_async", + "segments": [ + { + "end": 50, + "start": 27, + "type": "FULL" + }, + { + "end": 50, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 44, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 47, + "start": 45, + "type": "REQUEST_EXECUTION" + }, + { + "end": 51, + "start": 48, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_update_api_operation_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.update_api_operation", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.UpdateApiOperation", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "UpdateApiOperation" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.UpdateApiOperationRequest" + }, + { + "name": "api_operation", + "type": "google.cloud.apihub_v1.types.ApiOperation" + }, + { + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.ApiOperation", + "shortName": "update_api_operation" + }, + "description": "Sample for UpdateApiOperation", + "file": "apihub_v1_generated_api_hub_update_api_operation_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_UpdateApiOperation_sync", + "segments": [ + { + "end": 50, + "start": 27, + "type": "FULL" + }, + { + "end": 50, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 44, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 47, + "start": 45, + "type": "REQUEST_EXECUTION" + }, + { + "end": 51, + "start": 48, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_update_api_operation_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.update_api", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.UpdateApi", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "UpdateApi" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.UpdateApiRequest" + }, + { + "name": "api", + "type": "google.cloud.apihub_v1.types.Api" + }, + { + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Api", + "shortName": "update_api" + }, + "description": "Sample for UpdateApi", + "file": "apihub_v1_generated_api_hub_update_api_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_UpdateApi_async", + "segments": [ + { + "end": 54, + "start": 27, + "type": "FULL" + }, + { + "end": 54, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 48, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 51, + "start": 49, + "type": "REQUEST_EXECUTION" + }, + { + "end": 55, + "start": 52, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_update_api_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.update_api", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.UpdateApi", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "UpdateApi" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.UpdateApiRequest" + }, + { + "name": "api", + "type": "google.cloud.apihub_v1.types.Api" + }, + { + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Api", + "shortName": "update_api" + }, + "description": "Sample for UpdateApi", + "file": "apihub_v1_generated_api_hub_update_api_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_UpdateApi_sync", + "segments": [ + { + "end": 54, + "start": 27, + "type": "FULL" + }, + { + "end": 54, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 48, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 51, + "start": 49, + "type": "REQUEST_EXECUTION" + }, + { + "end": 55, + "start": 52, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_update_api_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.update_attribute", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.UpdateAttribute", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "UpdateAttribute" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.UpdateAttributeRequest" + }, + { + "name": "attribute", + "type": "google.cloud.apihub_v1.types.Attribute" + }, + { + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Attribute", + "shortName": "update_attribute" + }, + "description": "Sample for UpdateAttribute", + "file": "apihub_v1_generated_api_hub_update_attribute_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_UpdateAttribute_async", + "segments": [ + { + "end": 56, + "start": 27, + "type": "FULL" + }, + { + "end": 56, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 50, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 53, + "start": 51, + "type": "REQUEST_EXECUTION" + }, + { + "end": 57, + "start": 54, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_update_attribute_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.update_attribute", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.UpdateAttribute", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "UpdateAttribute" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.UpdateAttributeRequest" + }, + { + "name": "attribute", + "type": "google.cloud.apihub_v1.types.Attribute" + }, + { + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Attribute", + "shortName": "update_attribute" + }, + "description": "Sample for UpdateAttribute", + "file": "apihub_v1_generated_api_hub_update_attribute_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_UpdateAttribute_sync", + "segments": [ + { + "end": 56, + "start": 27, + "type": "FULL" + }, + { + "end": 56, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 50, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 53, + "start": 51, + "type": "REQUEST_EXECUTION" + }, + { + "end": 57, + "start": 54, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_update_attribute_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.update_deployment", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.UpdateDeployment", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "UpdateDeployment" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.UpdateDeploymentRequest" + }, + { + "name": "deployment", + "type": "google.cloud.apihub_v1.types.Deployment" + }, + { + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Deployment", + "shortName": "update_deployment" + }, + "description": "Sample for UpdateDeployment", + "file": "apihub_v1_generated_api_hub_update_deployment_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_UpdateDeployment_async", + "segments": [ + { + "end": 58, + "start": 27, + "type": "FULL" + }, + { + "end": 58, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 52, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 55, + "start": 53, + "type": "REQUEST_EXECUTION" + }, + { + "end": 59, + "start": 56, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_update_deployment_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.update_deployment", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.UpdateDeployment", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "UpdateDeployment" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.UpdateDeploymentRequest" + }, + { + "name": "deployment", + "type": "google.cloud.apihub_v1.types.Deployment" + }, + { + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Deployment", + "shortName": "update_deployment" + }, + "description": "Sample for UpdateDeployment", + "file": "apihub_v1_generated_api_hub_update_deployment_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_UpdateDeployment_sync", + "segments": [ + { + "end": 58, + "start": 27, + "type": "FULL" + }, + { + "end": 58, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 52, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 55, + "start": 53, + "type": "REQUEST_EXECUTION" + }, + { + "end": 59, + "start": 56, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_update_deployment_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.update_external_api", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.UpdateExternalApi", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "UpdateExternalApi" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.UpdateExternalApiRequest" + }, + { + "name": "external_api", + "type": "google.cloud.apihub_v1.types.ExternalApi" + }, + { + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.ExternalApi", + "shortName": "update_external_api" + }, + "description": "Sample for UpdateExternalApi", + "file": "apihub_v1_generated_api_hub_update_external_api_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_UpdateExternalApi_async", + "segments": [ + { + "end": 54, + "start": 27, + "type": "FULL" + }, + { + "end": 54, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 48, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 51, + "start": 49, + "type": "REQUEST_EXECUTION" + }, + { + "end": 55, + "start": 52, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_update_external_api_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.update_external_api", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.UpdateExternalApi", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "UpdateExternalApi" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.UpdateExternalApiRequest" + }, + { + "name": "external_api", + "type": "google.cloud.apihub_v1.types.ExternalApi" + }, + { + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.ExternalApi", + "shortName": "update_external_api" + }, + "description": "Sample for UpdateExternalApi", + "file": "apihub_v1_generated_api_hub_update_external_api_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_UpdateExternalApi_sync", + "segments": [ + { + "end": 54, + "start": 27, + "type": "FULL" + }, + { + "end": 54, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 48, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 51, + "start": 49, + "type": "REQUEST_EXECUTION" + }, + { + "end": 55, + "start": 52, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_update_external_api_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.update_spec", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.UpdateSpec", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "UpdateSpec" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.UpdateSpecRequest" + }, + { + "name": "spec", + "type": "google.cloud.apihub_v1.types.Spec" + }, + { + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Spec", + "shortName": "update_spec" + }, + "description": "Sample for UpdateSpec", + "file": "apihub_v1_generated_api_hub_update_spec_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_UpdateSpec_async", + "segments": [ + { + "end": 56, + "start": 27, + "type": "FULL" + }, + { + "end": 56, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 50, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 53, + "start": 51, + "type": "REQUEST_EXECUTION" + }, + { + "end": 57, + "start": 54, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_update_spec_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.update_spec", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.UpdateSpec", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "UpdateSpec" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.UpdateSpecRequest" + }, + { + "name": "spec", + "type": "google.cloud.apihub_v1.types.Spec" + }, + { + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Spec", + "shortName": "update_spec" + }, + "description": "Sample for UpdateSpec", + "file": "apihub_v1_generated_api_hub_update_spec_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_UpdateSpec_sync", + "segments": [ + { + "end": 56, + "start": 27, + "type": "FULL" + }, + { + "end": 56, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 50, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 53, + "start": 51, + "type": "REQUEST_EXECUTION" + }, + { + "end": 57, + "start": 54, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_update_spec_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient", + "shortName": "ApiHubAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubAsyncClient.update_version", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.UpdateVersion", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "UpdateVersion" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.UpdateVersionRequest" + }, + { + "name": "version", + "type": "google.cloud.apihub_v1.types.Version" + }, + { + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Version", + "shortName": "update_version" + }, + "description": "Sample for UpdateVersion", + "file": "apihub_v1_generated_api_hub_update_version_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_UpdateVersion_async", + "segments": [ + { + "end": 54, + "start": 27, + "type": "FULL" + }, + { + "end": 54, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 48, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 51, + "start": 49, + "type": "REQUEST_EXECUTION" + }, + { + "end": 55, + "start": 52, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_update_version_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ApiHubClient", + "shortName": "ApiHubClient" + }, + "fullName": "google.cloud.apihub_v1.ApiHubClient.update_version", + "method": { + "fullName": "google.cloud.apihub.v1.ApiHub.UpdateVersion", + "service": { + "fullName": "google.cloud.apihub.v1.ApiHub", + "shortName": "ApiHub" + }, + "shortName": "UpdateVersion" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.UpdateVersionRequest" + }, + { + "name": "version", + "type": "google.cloud.apihub_v1.types.Version" + }, + { + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.Version", + "shortName": "update_version" + }, + "description": "Sample for UpdateVersion", + "file": "apihub_v1_generated_api_hub_update_version_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_ApiHub_UpdateVersion_sync", + "segments": [ + { + "end": 54, + "start": 27, + "type": "FULL" + }, + { + "end": 54, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 48, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 51, + "start": 49, + "type": "REQUEST_EXECUTION" + }, + { + "end": 55, + "start": 52, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_api_hub_update_version_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.HostProjectRegistrationServiceAsyncClient", + "shortName": "HostProjectRegistrationServiceAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.HostProjectRegistrationServiceAsyncClient.create_host_project_registration", + "method": { + "fullName": "google.cloud.apihub.v1.HostProjectRegistrationService.CreateHostProjectRegistration", + "service": { + "fullName": "google.cloud.apihub.v1.HostProjectRegistrationService", + "shortName": "HostProjectRegistrationService" + }, + "shortName": "CreateHostProjectRegistration" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.CreateHostProjectRegistrationRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "host_project_registration", + "type": "google.cloud.apihub_v1.types.HostProjectRegistration" + }, + { + "name": "host_project_registration_id", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.HostProjectRegistration", + "shortName": "create_host_project_registration" + }, + "description": "Sample for CreateHostProjectRegistration", + "file": "apihub_v1_generated_host_project_registration_service_create_host_project_registration_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_HostProjectRegistrationService_CreateHostProjectRegistration_async", + "segments": [ + { + "end": 56, + "start": 27, + "type": "FULL" + }, + { + "end": 56, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 50, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 53, + "start": 51, + "type": "REQUEST_EXECUTION" + }, + { + "end": 57, + "start": 54, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_host_project_registration_service_create_host_project_registration_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.HostProjectRegistrationServiceClient", + "shortName": "HostProjectRegistrationServiceClient" + }, + "fullName": "google.cloud.apihub_v1.HostProjectRegistrationServiceClient.create_host_project_registration", + "method": { + "fullName": "google.cloud.apihub.v1.HostProjectRegistrationService.CreateHostProjectRegistration", + "service": { + "fullName": "google.cloud.apihub.v1.HostProjectRegistrationService", + "shortName": "HostProjectRegistrationService" + }, + "shortName": "CreateHostProjectRegistration" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.CreateHostProjectRegistrationRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "host_project_registration", + "type": "google.cloud.apihub_v1.types.HostProjectRegistration" + }, + { + "name": "host_project_registration_id", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.HostProjectRegistration", + "shortName": "create_host_project_registration" + }, + "description": "Sample for CreateHostProjectRegistration", + "file": "apihub_v1_generated_host_project_registration_service_create_host_project_registration_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_HostProjectRegistrationService_CreateHostProjectRegistration_sync", + "segments": [ + { + "end": 56, + "start": 27, + "type": "FULL" + }, + { + "end": 56, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 50, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 53, + "start": 51, + "type": "REQUEST_EXECUTION" + }, + { + "end": 57, + "start": 54, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_host_project_registration_service_create_host_project_registration_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.HostProjectRegistrationServiceAsyncClient", + "shortName": "HostProjectRegistrationServiceAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.HostProjectRegistrationServiceAsyncClient.get_host_project_registration", + "method": { + "fullName": "google.cloud.apihub.v1.HostProjectRegistrationService.GetHostProjectRegistration", + "service": { + "fullName": "google.cloud.apihub.v1.HostProjectRegistrationService", + "shortName": "HostProjectRegistrationService" + }, + "shortName": "GetHostProjectRegistration" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetHostProjectRegistrationRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.HostProjectRegistration", + "shortName": "get_host_project_registration" + }, + "description": "Sample for GetHostProjectRegistration", + "file": "apihub_v1_generated_host_project_registration_service_get_host_project_registration_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_HostProjectRegistrationService_GetHostProjectRegistration_async", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_host_project_registration_service_get_host_project_registration_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.HostProjectRegistrationServiceClient", + "shortName": "HostProjectRegistrationServiceClient" + }, + "fullName": "google.cloud.apihub_v1.HostProjectRegistrationServiceClient.get_host_project_registration", + "method": { + "fullName": "google.cloud.apihub.v1.HostProjectRegistrationService.GetHostProjectRegistration", + "service": { + "fullName": "google.cloud.apihub.v1.HostProjectRegistrationService", + "shortName": "HostProjectRegistrationService" + }, + "shortName": "GetHostProjectRegistration" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetHostProjectRegistrationRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.HostProjectRegistration", + "shortName": "get_host_project_registration" + }, + "description": "Sample for GetHostProjectRegistration", + "file": "apihub_v1_generated_host_project_registration_service_get_host_project_registration_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_HostProjectRegistrationService_GetHostProjectRegistration_sync", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_host_project_registration_service_get_host_project_registration_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.HostProjectRegistrationServiceAsyncClient", + "shortName": "HostProjectRegistrationServiceAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.HostProjectRegistrationServiceAsyncClient.list_host_project_registrations", + "method": { + "fullName": "google.cloud.apihub.v1.HostProjectRegistrationService.ListHostProjectRegistrations", + "service": { + "fullName": "google.cloud.apihub.v1.HostProjectRegistrationService", + "shortName": "HostProjectRegistrationService" + }, + "shortName": "ListHostProjectRegistrations" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.ListHostProjectRegistrationsRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.services.host_project_registration_service.pagers.ListHostProjectRegistrationsAsyncPager", + "shortName": "list_host_project_registrations" + }, + "description": "Sample for ListHostProjectRegistrations", + "file": "apihub_v1_generated_host_project_registration_service_list_host_project_registrations_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_HostProjectRegistrationService_ListHostProjectRegistrations_async", + "segments": [ + { + "end": 52, + "start": 27, + "type": "FULL" + }, + { + "end": 52, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 53, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_host_project_registration_service_list_host_project_registrations_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.HostProjectRegistrationServiceClient", + "shortName": "HostProjectRegistrationServiceClient" + }, + "fullName": "google.cloud.apihub_v1.HostProjectRegistrationServiceClient.list_host_project_registrations", + "method": { + "fullName": "google.cloud.apihub.v1.HostProjectRegistrationService.ListHostProjectRegistrations", + "service": { + "fullName": "google.cloud.apihub.v1.HostProjectRegistrationService", + "shortName": "HostProjectRegistrationService" + }, + "shortName": "ListHostProjectRegistrations" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.ListHostProjectRegistrationsRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.services.host_project_registration_service.pagers.ListHostProjectRegistrationsPager", + "shortName": "list_host_project_registrations" + }, + "description": "Sample for ListHostProjectRegistrations", + "file": "apihub_v1_generated_host_project_registration_service_list_host_project_registrations_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_HostProjectRegistrationService_ListHostProjectRegistrations_sync", + "segments": [ + { + "end": 52, + "start": 27, + "type": "FULL" + }, + { + "end": 52, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 53, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_host_project_registration_service_list_host_project_registrations_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.LintingServiceAsyncClient", + "shortName": "LintingServiceAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.LintingServiceAsyncClient.get_style_guide_contents", + "method": { + "fullName": "google.cloud.apihub.v1.LintingService.GetStyleGuideContents", + "service": { + "fullName": "google.cloud.apihub.v1.LintingService", + "shortName": "LintingService" + }, + "shortName": "GetStyleGuideContents" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetStyleGuideContentsRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.StyleGuideContents", + "shortName": "get_style_guide_contents" + }, + "description": "Sample for GetStyleGuideContents", + "file": "apihub_v1_generated_linting_service_get_style_guide_contents_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_LintingService_GetStyleGuideContents_async", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_linting_service_get_style_guide_contents_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.LintingServiceClient", + "shortName": "LintingServiceClient" + }, + "fullName": "google.cloud.apihub_v1.LintingServiceClient.get_style_guide_contents", + "method": { + "fullName": "google.cloud.apihub.v1.LintingService.GetStyleGuideContents", + "service": { + "fullName": "google.cloud.apihub.v1.LintingService", + "shortName": "LintingService" + }, + "shortName": "GetStyleGuideContents" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetStyleGuideContentsRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.StyleGuideContents", + "shortName": "get_style_guide_contents" + }, + "description": "Sample for GetStyleGuideContents", + "file": "apihub_v1_generated_linting_service_get_style_guide_contents_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_LintingService_GetStyleGuideContents_sync", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_linting_service_get_style_guide_contents_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.LintingServiceAsyncClient", + "shortName": "LintingServiceAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.LintingServiceAsyncClient.get_style_guide", + "method": { + "fullName": "google.cloud.apihub.v1.LintingService.GetStyleGuide", + "service": { + "fullName": "google.cloud.apihub.v1.LintingService", + "shortName": "LintingService" + }, + "shortName": "GetStyleGuide" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetStyleGuideRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.StyleGuide", + "shortName": "get_style_guide" + }, + "description": "Sample for GetStyleGuide", + "file": "apihub_v1_generated_linting_service_get_style_guide_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_LintingService_GetStyleGuide_async", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_linting_service_get_style_guide_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.LintingServiceClient", + "shortName": "LintingServiceClient" + }, + "fullName": "google.cloud.apihub_v1.LintingServiceClient.get_style_guide", + "method": { + "fullName": "google.cloud.apihub.v1.LintingService.GetStyleGuide", + "service": { + "fullName": "google.cloud.apihub.v1.LintingService", + "shortName": "LintingService" + }, + "shortName": "GetStyleGuide" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.GetStyleGuideRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.StyleGuide", + "shortName": "get_style_guide" + }, + "description": "Sample for GetStyleGuide", + "file": "apihub_v1_generated_linting_service_get_style_guide_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_LintingService_GetStyleGuide_sync", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_linting_service_get_style_guide_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.LintingServiceAsyncClient", + "shortName": "LintingServiceAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.LintingServiceAsyncClient.lint_spec", + "method": { + "fullName": "google.cloud.apihub.v1.LintingService.LintSpec", + "service": { + "fullName": "google.cloud.apihub.v1.LintingService", + "shortName": "LintingService" + }, + "shortName": "LintSpec" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.LintSpecRequest" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "shortName": "lint_spec" + }, + "description": "Sample for LintSpec", + "file": "apihub_v1_generated_linting_service_lint_spec_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_LintingService_LintSpec_async", + "segments": [ + { + "end": 49, + "start": 27, + "type": "FULL" + }, + { + "end": 49, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 50, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_linting_service_lint_spec_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.LintingServiceClient", + "shortName": "LintingServiceClient" + }, + "fullName": "google.cloud.apihub_v1.LintingServiceClient.lint_spec", + "method": { + "fullName": "google.cloud.apihub.v1.LintingService.LintSpec", + "service": { + "fullName": "google.cloud.apihub.v1.LintingService", + "shortName": "LintingService" + }, + "shortName": "LintSpec" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.LintSpecRequest" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "shortName": "lint_spec" + }, + "description": "Sample for LintSpec", + "file": "apihub_v1_generated_linting_service_lint_spec_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_LintingService_LintSpec_sync", + "segments": [ + { + "end": 49, + "start": 27, + "type": "FULL" + }, + { + "end": 49, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 50, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_linting_service_lint_spec_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.LintingServiceAsyncClient", + "shortName": "LintingServiceAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.LintingServiceAsyncClient.update_style_guide", + "method": { + "fullName": "google.cloud.apihub.v1.LintingService.UpdateStyleGuide", + "service": { + "fullName": "google.cloud.apihub.v1.LintingService", + "shortName": "LintingService" + }, + "shortName": "UpdateStyleGuide" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.UpdateStyleGuideRequest" + }, + { + "name": "style_guide", + "type": "google.cloud.apihub_v1.types.StyleGuide" + }, + { + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.StyleGuide", + "shortName": "update_style_guide" + }, + "description": "Sample for UpdateStyleGuide", + "file": "apihub_v1_generated_linting_service_update_style_guide_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_LintingService_UpdateStyleGuide_async", + "segments": [ + { + "end": 56, + "start": 27, + "type": "FULL" + }, + { + "end": 56, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 50, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 53, + "start": 51, + "type": "REQUEST_EXECUTION" + }, + { + "end": 57, + "start": 54, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_linting_service_update_style_guide_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.LintingServiceClient", + "shortName": "LintingServiceClient" + }, + "fullName": "google.cloud.apihub_v1.LintingServiceClient.update_style_guide", + "method": { + "fullName": "google.cloud.apihub.v1.LintingService.UpdateStyleGuide", + "service": { + "fullName": "google.cloud.apihub.v1.LintingService", + "shortName": "LintingService" + }, + "shortName": "UpdateStyleGuide" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.UpdateStyleGuideRequest" + }, + { + "name": "style_guide", + "type": "google.cloud.apihub_v1.types.StyleGuide" + }, + { + "name": "update_mask", + "type": "google.protobuf.field_mask_pb2.FieldMask" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.cloud.apihub_v1.types.StyleGuide", + "shortName": "update_style_guide" + }, + "description": "Sample for UpdateStyleGuide", + "file": "apihub_v1_generated_linting_service_update_style_guide_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_LintingService_UpdateStyleGuide_sync", + "segments": [ + { + "end": 56, + "start": 27, + "type": "FULL" + }, + { + "end": 56, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 50, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 53, + "start": 51, + "type": "REQUEST_EXECUTION" + }, + { + "end": 57, + "start": 54, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_linting_service_update_style_guide_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.apihub_v1.ProvisioningAsyncClient", + "shortName": "ProvisioningAsyncClient" + }, + "fullName": "google.cloud.apihub_v1.ProvisioningAsyncClient.create_api_hub_instance", + "method": { + "fullName": "google.cloud.apihub.v1.Provisioning.CreateApiHubInstance", + "service": { + "fullName": "google.cloud.apihub.v1.Provisioning", + "shortName": "Provisioning" + }, + "shortName": "CreateApiHubInstance" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.CreateApiHubInstanceRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "api_hub_instance", + "type": "google.cloud.apihub_v1.types.ApiHubInstance" + }, + { + "name": "api_hub_instance_id", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.api_core.operation_async.AsyncOperation", + "shortName": "create_api_hub_instance" + }, + "description": "Sample for CreateApiHubInstance", + "file": "apihub_v1_generated_provisioning_create_api_hub_instance_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_Provisioning_CreateApiHubInstance_async", + "segments": [ + { + "end": 55, + "start": 27, + "type": "FULL" + }, + { + "end": 55, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 52, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 56, + "start": 53, + "type": "RESPONSE_HANDLING" + } + ], + "title": "apihub_v1_generated_provisioning_create_api_hub_instance_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.apihub_v1.ProvisioningClient", + "shortName": "ProvisioningClient" + }, + "fullName": "google.cloud.apihub_v1.ProvisioningClient.create_api_hub_instance", + "method": { + "fullName": "google.cloud.apihub.v1.Provisioning.CreateApiHubInstance", + "service": { + "fullName": "google.cloud.apihub.v1.Provisioning", + "shortName": "Provisioning" + }, + "shortName": "CreateApiHubInstance" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.apihub_v1.types.CreateApiHubInstanceRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "api_hub_instance", + "type": "google.cloud.apihub_v1.types.ApiHubInstance" + }, + { + "name": "api_hub_instance_id", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, Union[str, bytes]]]" + } + ], + "resultType": "google.api_core.operation.Operation", + "shortName": "create_api_hub_instance" + }, + "description": "Sample for CreateApiHubInstance", + "file": "apihub_v1_generated_provisioning_create_api_hub_instance_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "apihub_v1_generated_Provisioning_CreateApiHubInstance_sync", "segments": [ { - "end": 54, + "end": 55, "start": 27, "type": "FULL" }, { - "end": 54, + "end": 55, "start": 27, "type": "SHORT" }, @@ -5479,54 +12345,47 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 48, + "end": 45, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 51, - "start": 49, + "end": 52, + "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 55, - "start": 52, + "end": 56, + "start": 53, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_api_hub_update_version_sync.py" + "title": "apihub_v1_generated_provisioning_create_api_hub_instance_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.HostProjectRegistrationServiceClient", - "shortName": "HostProjectRegistrationServiceClient" + "fullName": "google.cloud.apihub_v1.ProvisioningAsyncClient", + "shortName": "ProvisioningAsyncClient" }, - "fullName": "google.cloud.apihub_v1.HostProjectRegistrationServiceClient.create_host_project_registration", + "fullName": "google.cloud.apihub_v1.ProvisioningAsyncClient.delete_api_hub_instance", "method": { - "fullName": "google.cloud.apihub.v1.HostProjectRegistrationService.CreateHostProjectRegistration", + "fullName": "google.cloud.apihub.v1.Provisioning.DeleteApiHubInstance", "service": { - "fullName": "google.cloud.apihub.v1.HostProjectRegistrationService", - "shortName": "HostProjectRegistrationService" + "fullName": "google.cloud.apihub.v1.Provisioning", + "shortName": "Provisioning" }, - "shortName": "CreateHostProjectRegistration" + "shortName": "DeleteApiHubInstance" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.CreateHostProjectRegistrationRequest" - }, - { - "name": "parent", - "type": "str" - }, - { - "name": "host_project_registration", - "type": "google.cloud.apihub_v1.types.HostProjectRegistration" + "type": "google.cloud.apihub_v1.types.DeleteApiHubInstanceRequest" }, { - "name": "host_project_registration_id", + "name": "name", "type": "str" }, { @@ -5542,22 +12401,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.HostProjectRegistration", - "shortName": "create_host_project_registration" + "resultType": "google.api_core.operation_async.AsyncOperation", + "shortName": "delete_api_hub_instance" }, - "description": "Sample for CreateHostProjectRegistration", - "file": "apihub_v1_generated_host_project_registration_service_create_host_project_registration_sync.py", + "description": "Sample for DeleteApiHubInstance", + "file": "apihub_v1_generated_provisioning_delete_api_hub_instance_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_HostProjectRegistrationService_CreateHostProjectRegistration_sync", + "regionTag": "apihub_v1_generated_Provisioning_DeleteApiHubInstance_async", "segments": [ { - "end": 56, + "end": 55, "start": 27, "type": "FULL" }, { - "end": 56, + "end": 55, "start": 27, "type": "SHORT" }, @@ -5567,43 +12426,43 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 50, + "end": 45, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 53, - "start": 51, + "end": 52, + "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 57, - "start": 54, + "end": 56, + "start": 53, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_host_project_registration_service_create_host_project_registration_sync.py" + "title": "apihub_v1_generated_provisioning_delete_api_hub_instance_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.HostProjectRegistrationServiceClient", - "shortName": "HostProjectRegistrationServiceClient" + "fullName": "google.cloud.apihub_v1.ProvisioningClient", + "shortName": "ProvisioningClient" }, - "fullName": "google.cloud.apihub_v1.HostProjectRegistrationServiceClient.get_host_project_registration", + "fullName": "google.cloud.apihub_v1.ProvisioningClient.delete_api_hub_instance", "method": { - "fullName": "google.cloud.apihub.v1.HostProjectRegistrationService.GetHostProjectRegistration", + "fullName": "google.cloud.apihub.v1.Provisioning.DeleteApiHubInstance", "service": { - "fullName": "google.cloud.apihub.v1.HostProjectRegistrationService", - "shortName": "HostProjectRegistrationService" + "fullName": "google.cloud.apihub.v1.Provisioning", + "shortName": "Provisioning" }, - "shortName": "GetHostProjectRegistration" + "shortName": "DeleteApiHubInstance" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.GetHostProjectRegistrationRequest" + "type": "google.cloud.apihub_v1.types.DeleteApiHubInstanceRequest" }, { "name": "name", @@ -5622,22 +12481,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.HostProjectRegistration", - "shortName": "get_host_project_registration" + "resultType": "google.api_core.operation.Operation", + "shortName": "delete_api_hub_instance" }, - "description": "Sample for GetHostProjectRegistration", - "file": "apihub_v1_generated_host_project_registration_service_get_host_project_registration_sync.py", + "description": "Sample for DeleteApiHubInstance", + "file": "apihub_v1_generated_provisioning_delete_api_hub_instance_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_HostProjectRegistrationService_GetHostProjectRegistration_sync", + "regionTag": "apihub_v1_generated_Provisioning_DeleteApiHubInstance_sync", "segments": [ { - "end": 51, + "end": 55, "start": 27, "type": "FULL" }, { - "end": 51, + "end": 55, "start": 27, "type": "SHORT" }, @@ -5652,41 +12511,42 @@ "type": "REQUEST_INITIALIZATION" }, { - "end": 48, + "end": 52, "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 52, - "start": 49, + "end": 56, + "start": 53, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_host_project_registration_service_get_host_project_registration_sync.py" + "title": "apihub_v1_generated_provisioning_delete_api_hub_instance_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.HostProjectRegistrationServiceClient", - "shortName": "HostProjectRegistrationServiceClient" + "fullName": "google.cloud.apihub_v1.ProvisioningAsyncClient", + "shortName": "ProvisioningAsyncClient" }, - "fullName": "google.cloud.apihub_v1.HostProjectRegistrationServiceClient.list_host_project_registrations", + "fullName": "google.cloud.apihub_v1.ProvisioningAsyncClient.get_api_hub_instance", "method": { - "fullName": "google.cloud.apihub.v1.HostProjectRegistrationService.ListHostProjectRegistrations", + "fullName": "google.cloud.apihub.v1.Provisioning.GetApiHubInstance", "service": { - "fullName": "google.cloud.apihub.v1.HostProjectRegistrationService", - "shortName": "HostProjectRegistrationService" + "fullName": "google.cloud.apihub.v1.Provisioning", + "shortName": "Provisioning" }, - "shortName": "ListHostProjectRegistrations" + "shortName": "GetApiHubInstance" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.ListHostProjectRegistrationsRequest" + "type": "google.cloud.apihub_v1.types.GetApiHubInstanceRequest" }, { - "name": "parent", + "name": "name", "type": "str" }, { @@ -5702,22 +12562,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.services.host_project_registration_service.pagers.ListHostProjectRegistrationsPager", - "shortName": "list_host_project_registrations" + "resultType": "google.cloud.apihub_v1.types.ApiHubInstance", + "shortName": "get_api_hub_instance" }, - "description": "Sample for ListHostProjectRegistrations", - "file": "apihub_v1_generated_host_project_registration_service_list_host_project_registrations_sync.py", + "description": "Sample for GetApiHubInstance", + "file": "apihub_v1_generated_provisioning_get_api_hub_instance_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_HostProjectRegistrationService_ListHostProjectRegistrations_sync", + "regionTag": "apihub_v1_generated_Provisioning_GetApiHubInstance_async", "segments": [ { - "end": 52, + "end": 51, "start": 27, "type": "FULL" }, { - "end": 52, + "end": 51, "start": 27, "type": "SHORT" }, @@ -5737,33 +12597,33 @@ "type": "REQUEST_EXECUTION" }, { - "end": 53, + "end": 52, "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_host_project_registration_service_list_host_project_registrations_sync.py" + "title": "apihub_v1_generated_provisioning_get_api_hub_instance_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.LintingServiceClient", - "shortName": "LintingServiceClient" + "fullName": "google.cloud.apihub_v1.ProvisioningClient", + "shortName": "ProvisioningClient" }, - "fullName": "google.cloud.apihub_v1.LintingServiceClient.get_style_guide_contents", + "fullName": "google.cloud.apihub_v1.ProvisioningClient.get_api_hub_instance", "method": { - "fullName": "google.cloud.apihub.v1.LintingService.GetStyleGuideContents", + "fullName": "google.cloud.apihub.v1.Provisioning.GetApiHubInstance", "service": { - "fullName": "google.cloud.apihub.v1.LintingService", - "shortName": "LintingService" + "fullName": "google.cloud.apihub.v1.Provisioning", + "shortName": "Provisioning" }, - "shortName": "GetStyleGuideContents" + "shortName": "GetApiHubInstance" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.GetStyleGuideContentsRequest" + "type": "google.cloud.apihub_v1.types.GetApiHubInstanceRequest" }, { "name": "name", @@ -5782,14 +12642,14 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.StyleGuideContents", - "shortName": "get_style_guide_contents" + "resultType": "google.cloud.apihub_v1.types.ApiHubInstance", + "shortName": "get_api_hub_instance" }, - "description": "Sample for GetStyleGuideContents", - "file": "apihub_v1_generated_linting_service_get_style_guide_contents_sync.py", + "description": "Sample for GetApiHubInstance", + "file": "apihub_v1_generated_provisioning_get_api_hub_instance_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_LintingService_GetStyleGuideContents_sync", + "regionTag": "apihub_v1_generated_Provisioning_GetApiHubInstance_sync", "segments": [ { "end": 51, @@ -5822,31 +12682,32 @@ "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_linting_service_get_style_guide_contents_sync.py" + "title": "apihub_v1_generated_provisioning_get_api_hub_instance_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.LintingServiceClient", - "shortName": "LintingServiceClient" + "fullName": "google.cloud.apihub_v1.ProvisioningAsyncClient", + "shortName": "ProvisioningAsyncClient" }, - "fullName": "google.cloud.apihub_v1.LintingServiceClient.get_style_guide", + "fullName": "google.cloud.apihub_v1.ProvisioningAsyncClient.lookup_api_hub_instance", "method": { - "fullName": "google.cloud.apihub.v1.LintingService.GetStyleGuide", + "fullName": "google.cloud.apihub.v1.Provisioning.LookupApiHubInstance", "service": { - "fullName": "google.cloud.apihub.v1.LintingService", - "shortName": "LintingService" + "fullName": "google.cloud.apihub.v1.Provisioning", + "shortName": "Provisioning" }, - "shortName": "GetStyleGuide" + "shortName": "LookupApiHubInstance" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.GetStyleGuideRequest" + "type": "google.cloud.apihub_v1.types.LookupApiHubInstanceRequest" }, { - "name": "name", + "name": "parent", "type": "str" }, { @@ -5862,14 +12723,14 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.StyleGuide", - "shortName": "get_style_guide" + "resultType": "google.cloud.apihub_v1.types.LookupApiHubInstanceResponse", + "shortName": "lookup_api_hub_instance" }, - "description": "Sample for GetStyleGuide", - "file": "apihub_v1_generated_linting_service_get_style_guide_sync.py", + "description": "Sample for LookupApiHubInstance", + "file": "apihub_v1_generated_provisioning_lookup_api_hub_instance_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_LintingService_GetStyleGuide_sync", + "regionTag": "apihub_v1_generated_Provisioning_LookupApiHubInstance_async", "segments": [ { "end": 51, @@ -5902,28 +12763,32 @@ "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_linting_service_get_style_guide_sync.py" + "title": "apihub_v1_generated_provisioning_lookup_api_hub_instance_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.LintingServiceClient", - "shortName": "LintingServiceClient" + "fullName": "google.cloud.apihub_v1.ProvisioningClient", + "shortName": "ProvisioningClient" }, - "fullName": "google.cloud.apihub_v1.LintingServiceClient.lint_spec", + "fullName": "google.cloud.apihub_v1.ProvisioningClient.lookup_api_hub_instance", "method": { - "fullName": "google.cloud.apihub.v1.LintingService.LintSpec", + "fullName": "google.cloud.apihub.v1.Provisioning.LookupApiHubInstance", "service": { - "fullName": "google.cloud.apihub.v1.LintingService", - "shortName": "LintingService" + "fullName": "google.cloud.apihub.v1.Provisioning", + "shortName": "Provisioning" }, - "shortName": "LintSpec" + "shortName": "LookupApiHubInstance" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.LintSpecRequest" + "type": "google.cloud.apihub_v1.types.LookupApiHubInstanceRequest" + }, + { + "name": "parent", + "type": "str" }, { "name": "retry", @@ -5938,21 +12803,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "shortName": "lint_spec" + "resultType": "google.cloud.apihub_v1.types.LookupApiHubInstanceResponse", + "shortName": "lookup_api_hub_instance" }, - "description": "Sample for LintSpec", - "file": "apihub_v1_generated_linting_service_lint_spec_sync.py", + "description": "Sample for LookupApiHubInstance", + "file": "apihub_v1_generated_provisioning_lookup_api_hub_instance_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_LintingService_LintSpec_sync", + "regionTag": "apihub_v1_generated_Provisioning_LookupApiHubInstance_sync", "segments": [ { - "end": 49, + "end": 51, "start": 27, "type": "FULL" }, { - "end": 49, + "end": 51, "start": 27, "type": "SHORT" }, @@ -5967,44 +12833,51 @@ "type": "REQUEST_INITIALIZATION" }, { + "end": 48, "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 50, + "end": 52, + "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_linting_service_lint_spec_sync.py" + "title": "apihub_v1_generated_provisioning_lookup_api_hub_instance_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.LintingServiceClient", - "shortName": "LintingServiceClient" + "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceAsyncClient", + "shortName": "RuntimeProjectAttachmentServiceAsyncClient" }, - "fullName": "google.cloud.apihub_v1.LintingServiceClient.update_style_guide", + "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceAsyncClient.create_runtime_project_attachment", "method": { - "fullName": "google.cloud.apihub.v1.LintingService.UpdateStyleGuide", + "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService.CreateRuntimeProjectAttachment", "service": { - "fullName": "google.cloud.apihub.v1.LintingService", - "shortName": "LintingService" + "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService", + "shortName": "RuntimeProjectAttachmentService" }, - "shortName": "UpdateStyleGuide" + "shortName": "CreateRuntimeProjectAttachment" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.UpdateStyleGuideRequest" + "type": "google.cloud.apihub_v1.types.CreateRuntimeProjectAttachmentRequest" }, { - "name": "style_guide", - "type": "google.cloud.apihub_v1.types.StyleGuide" + "name": "parent", + "type": "str" }, { - "name": "update_mask", - "type": "google.protobuf.field_mask_pb2.FieldMask" + "name": "runtime_project_attachment", + "type": "google.cloud.apihub_v1.types.RuntimeProjectAttachment" + }, + { + "name": "runtime_project_attachment_id", + "type": "str" }, { "name": "retry", @@ -6019,14 +12892,14 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.StyleGuide", - "shortName": "update_style_guide" + "resultType": "google.cloud.apihub_v1.types.RuntimeProjectAttachment", + "shortName": "create_runtime_project_attachment" }, - "description": "Sample for UpdateStyleGuide", - "file": "apihub_v1_generated_linting_service_update_style_guide_sync.py", + "description": "Sample for CreateRuntimeProjectAttachment", + "file": "apihub_v1_generated_runtime_project_attachment_service_create_runtime_project_attachment_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_LintingService_UpdateStyleGuide_sync", + "regionTag": "apihub_v1_generated_RuntimeProjectAttachmentService_CreateRuntimeProjectAttachment_async", "segments": [ { "end": 56, @@ -6059,39 +12932,39 @@ "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_linting_service_update_style_guide_sync.py" + "title": "apihub_v1_generated_runtime_project_attachment_service_create_runtime_project_attachment_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ProvisioningClient", - "shortName": "ProvisioningClient" + "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceClient", + "shortName": "RuntimeProjectAttachmentServiceClient" }, - "fullName": "google.cloud.apihub_v1.ProvisioningClient.create_api_hub_instance", + "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceClient.create_runtime_project_attachment", "method": { - "fullName": "google.cloud.apihub.v1.Provisioning.CreateApiHubInstance", + "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService.CreateRuntimeProjectAttachment", "service": { - "fullName": "google.cloud.apihub.v1.Provisioning", - "shortName": "Provisioning" + "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService", + "shortName": "RuntimeProjectAttachmentService" }, - "shortName": "CreateApiHubInstance" + "shortName": "CreateRuntimeProjectAttachment" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.CreateApiHubInstanceRequest" + "type": "google.cloud.apihub_v1.types.CreateRuntimeProjectAttachmentRequest" }, { "name": "parent", "type": "str" }, { - "name": "api_hub_instance", - "type": "google.cloud.apihub_v1.types.ApiHubInstance" + "name": "runtime_project_attachment", + "type": "google.cloud.apihub_v1.types.RuntimeProjectAttachment" }, { - "name": "api_hub_instance_id", + "name": "runtime_project_attachment_id", "type": "str" }, { @@ -6107,22 +12980,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.api_core.operation.Operation", - "shortName": "create_api_hub_instance" + "resultType": "google.cloud.apihub_v1.types.RuntimeProjectAttachment", + "shortName": "create_runtime_project_attachment" }, - "description": "Sample for CreateApiHubInstance", - "file": "apihub_v1_generated_provisioning_create_api_hub_instance_sync.py", + "description": "Sample for CreateRuntimeProjectAttachment", + "file": "apihub_v1_generated_runtime_project_attachment_service_create_runtime_project_attachment_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_Provisioning_CreateApiHubInstance_sync", + "regionTag": "apihub_v1_generated_RuntimeProjectAttachmentService_CreateRuntimeProjectAttachment_sync", "segments": [ { - "end": 55, + "end": 56, "start": 27, "type": "FULL" }, { - "end": 55, + "end": 56, "start": 27, "type": "SHORT" }, @@ -6132,43 +13005,44 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 45, + "end": 50, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 52, - "start": 46, + "end": 53, + "start": 51, "type": "REQUEST_EXECUTION" }, { - "end": 56, - "start": 53, + "end": 57, + "start": 54, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_provisioning_create_api_hub_instance_sync.py" + "title": "apihub_v1_generated_runtime_project_attachment_service_create_runtime_project_attachment_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ProvisioningClient", - "shortName": "ProvisioningClient" + "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceAsyncClient", + "shortName": "RuntimeProjectAttachmentServiceAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ProvisioningClient.delete_api_hub_instance", + "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceAsyncClient.delete_runtime_project_attachment", "method": { - "fullName": "google.cloud.apihub.v1.Provisioning.DeleteApiHubInstance", + "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService.DeleteRuntimeProjectAttachment", "service": { - "fullName": "google.cloud.apihub.v1.Provisioning", - "shortName": "Provisioning" + "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService", + "shortName": "RuntimeProjectAttachmentService" }, - "shortName": "DeleteApiHubInstance" + "shortName": "DeleteRuntimeProjectAttachment" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.DeleteApiHubInstanceRequest" + "type": "google.cloud.apihub_v1.types.DeleteRuntimeProjectAttachmentRequest" }, { "name": "name", @@ -6187,22 +13061,21 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.api_core.operation.Operation", - "shortName": "delete_api_hub_instance" + "shortName": "delete_runtime_project_attachment" }, - "description": "Sample for DeleteApiHubInstance", - "file": "apihub_v1_generated_provisioning_delete_api_hub_instance_sync.py", + "description": "Sample for DeleteRuntimeProjectAttachment", + "file": "apihub_v1_generated_runtime_project_attachment_service_delete_runtime_project_attachment_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_Provisioning_DeleteApiHubInstance_sync", + "regionTag": "apihub_v1_generated_RuntimeProjectAttachmentService_DeleteRuntimeProjectAttachment_async", "segments": [ { - "end": 55, + "end": 49, "start": 27, "type": "FULL" }, { - "end": 55, + "end": 49, "start": 27, "type": "SHORT" }, @@ -6217,38 +13090,36 @@ "type": "REQUEST_INITIALIZATION" }, { - "end": 52, "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 56, - "start": 53, + "end": 50, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_provisioning_delete_api_hub_instance_sync.py" + "title": "apihub_v1_generated_runtime_project_attachment_service_delete_runtime_project_attachment_async.py" }, { "canonical": true, "clientMethod": { "client": { - "fullName": "google.cloud.apihub_v1.ProvisioningClient", - "shortName": "ProvisioningClient" + "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceClient", + "shortName": "RuntimeProjectAttachmentServiceClient" }, - "fullName": "google.cloud.apihub_v1.ProvisioningClient.get_api_hub_instance", + "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceClient.delete_runtime_project_attachment", "method": { - "fullName": "google.cloud.apihub.v1.Provisioning.GetApiHubInstance", + "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService.DeleteRuntimeProjectAttachment", "service": { - "fullName": "google.cloud.apihub.v1.Provisioning", - "shortName": "Provisioning" + "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService", + "shortName": "RuntimeProjectAttachmentService" }, - "shortName": "GetApiHubInstance" + "shortName": "DeleteRuntimeProjectAttachment" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.GetApiHubInstanceRequest" + "type": "google.cloud.apihub_v1.types.DeleteRuntimeProjectAttachmentRequest" }, { "name": "name", @@ -6267,22 +13138,21 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.ApiHubInstance", - "shortName": "get_api_hub_instance" + "shortName": "delete_runtime_project_attachment" }, - "description": "Sample for GetApiHubInstance", - "file": "apihub_v1_generated_provisioning_get_api_hub_instance_sync.py", + "description": "Sample for DeleteRuntimeProjectAttachment", + "file": "apihub_v1_generated_runtime_project_attachment_service_delete_runtime_project_attachment_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_Provisioning_GetApiHubInstance_sync", + "regionTag": "apihub_v1_generated_RuntimeProjectAttachmentService_DeleteRuntimeProjectAttachment_sync", "segments": [ { - "end": 51, + "end": 49, "start": 27, "type": "FULL" }, { - "end": 51, + "end": 49, "start": 27, "type": "SHORT" }, @@ -6297,41 +13167,40 @@ "type": "REQUEST_INITIALIZATION" }, { - "end": 48, "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 52, - "start": 49, + "end": 50, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_provisioning_get_api_hub_instance_sync.py" + "title": "apihub_v1_generated_runtime_project_attachment_service_delete_runtime_project_attachment_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.ProvisioningClient", - "shortName": "ProvisioningClient" + "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceAsyncClient", + "shortName": "RuntimeProjectAttachmentServiceAsyncClient" }, - "fullName": "google.cloud.apihub_v1.ProvisioningClient.lookup_api_hub_instance", + "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceAsyncClient.get_runtime_project_attachment", "method": { - "fullName": "google.cloud.apihub.v1.Provisioning.LookupApiHubInstance", + "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService.GetRuntimeProjectAttachment", "service": { - "fullName": "google.cloud.apihub.v1.Provisioning", - "shortName": "Provisioning" + "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService", + "shortName": "RuntimeProjectAttachmentService" }, - "shortName": "LookupApiHubInstance" + "shortName": "GetRuntimeProjectAttachment" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.LookupApiHubInstanceRequest" + "type": "google.cloud.apihub_v1.types.GetRuntimeProjectAttachmentRequest" }, { - "name": "parent", + "name": "name", "type": "str" }, { @@ -6347,14 +13216,14 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.LookupApiHubInstanceResponse", - "shortName": "lookup_api_hub_instance" + "resultType": "google.cloud.apihub_v1.types.RuntimeProjectAttachment", + "shortName": "get_runtime_project_attachment" }, - "description": "Sample for LookupApiHubInstance", - "file": "apihub_v1_generated_provisioning_lookup_api_hub_instance_sync.py", + "description": "Sample for GetRuntimeProjectAttachment", + "file": "apihub_v1_generated_runtime_project_attachment_service_get_runtime_project_attachment_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_Provisioning_LookupApiHubInstance_sync", + "regionTag": "apihub_v1_generated_RuntimeProjectAttachmentService_GetRuntimeProjectAttachment_async", "segments": [ { "end": 51, @@ -6387,7 +13256,7 @@ "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_provisioning_lookup_api_hub_instance_sync.py" + "title": "apihub_v1_generated_runtime_project_attachment_service_get_runtime_project_attachment_async.py" }, { "canonical": true, @@ -6396,30 +13265,22 @@ "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceClient", "shortName": "RuntimeProjectAttachmentServiceClient" }, - "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceClient.create_runtime_project_attachment", + "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceClient.get_runtime_project_attachment", "method": { - "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService.CreateRuntimeProjectAttachment", + "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService.GetRuntimeProjectAttachment", "service": { "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService", "shortName": "RuntimeProjectAttachmentService" }, - "shortName": "CreateRuntimeProjectAttachment" + "shortName": "GetRuntimeProjectAttachment" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.CreateRuntimeProjectAttachmentRequest" - }, - { - "name": "parent", - "type": "str" - }, - { - "name": "runtime_project_attachment", - "type": "google.cloud.apihub_v1.types.RuntimeProjectAttachment" + "type": "google.cloud.apihub_v1.types.GetRuntimeProjectAttachmentRequest" }, { - "name": "runtime_project_attachment_id", + "name": "name", "type": "str" }, { @@ -6436,21 +13297,21 @@ } ], "resultType": "google.cloud.apihub_v1.types.RuntimeProjectAttachment", - "shortName": "create_runtime_project_attachment" + "shortName": "get_runtime_project_attachment" }, - "description": "Sample for CreateRuntimeProjectAttachment", - "file": "apihub_v1_generated_runtime_project_attachment_service_create_runtime_project_attachment_sync.py", + "description": "Sample for GetRuntimeProjectAttachment", + "file": "apihub_v1_generated_runtime_project_attachment_service_get_runtime_project_attachment_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_RuntimeProjectAttachmentService_CreateRuntimeProjectAttachment_sync", + "regionTag": "apihub_v1_generated_RuntimeProjectAttachmentService_GetRuntimeProjectAttachment_sync", "segments": [ { - "end": 56, + "end": 51, "start": 27, "type": "FULL" }, { - "end": 56, + "end": 51, "start": 27, "type": "SHORT" }, @@ -6460,46 +13321,47 @@ "type": "CLIENT_INITIALIZATION" }, { - "end": 50, + "end": 45, "start": 41, "type": "REQUEST_INITIALIZATION" }, { - "end": 53, - "start": 51, + "end": 48, + "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 57, - "start": 54, + "end": 52, + "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_runtime_project_attachment_service_create_runtime_project_attachment_sync.py" + "title": "apihub_v1_generated_runtime_project_attachment_service_get_runtime_project_attachment_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceClient", - "shortName": "RuntimeProjectAttachmentServiceClient" + "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceAsyncClient", + "shortName": "RuntimeProjectAttachmentServiceAsyncClient" }, - "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceClient.delete_runtime_project_attachment", + "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceAsyncClient.list_runtime_project_attachments", "method": { - "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService.DeleteRuntimeProjectAttachment", + "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService.ListRuntimeProjectAttachments", "service": { "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService", "shortName": "RuntimeProjectAttachmentService" }, - "shortName": "DeleteRuntimeProjectAttachment" + "shortName": "ListRuntimeProjectAttachments" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.DeleteRuntimeProjectAttachmentRequest" + "type": "google.cloud.apihub_v1.types.ListRuntimeProjectAttachmentsRequest" }, { - "name": "name", + "name": "parent", "type": "str" }, { @@ -6515,21 +13377,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "shortName": "delete_runtime_project_attachment" + "resultType": "google.cloud.apihub_v1.services.runtime_project_attachment_service.pagers.ListRuntimeProjectAttachmentsAsyncPager", + "shortName": "list_runtime_project_attachments" }, - "description": "Sample for DeleteRuntimeProjectAttachment", - "file": "apihub_v1_generated_runtime_project_attachment_service_delete_runtime_project_attachment_sync.py", + "description": "Sample for ListRuntimeProjectAttachments", + "file": "apihub_v1_generated_runtime_project_attachment_service_list_runtime_project_attachments_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_RuntimeProjectAttachmentService_DeleteRuntimeProjectAttachment_sync", + "regionTag": "apihub_v1_generated_RuntimeProjectAttachmentService_ListRuntimeProjectAttachments_async", "segments": [ { - "end": 49, + "end": 52, "start": 27, "type": "FULL" }, { - "end": 49, + "end": 52, "start": 27, "type": "SHORT" }, @@ -6544,15 +13407,17 @@ "type": "REQUEST_INITIALIZATION" }, { + "end": 48, "start": 46, "type": "REQUEST_EXECUTION" }, { - "end": 50, + "end": 53, + "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_runtime_project_attachment_service_delete_runtime_project_attachment_sync.py" + "title": "apihub_v1_generated_runtime_project_attachment_service_list_runtime_project_attachments_async.py" }, { "canonical": true, @@ -6561,22 +13426,22 @@ "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceClient", "shortName": "RuntimeProjectAttachmentServiceClient" }, - "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceClient.get_runtime_project_attachment", + "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceClient.list_runtime_project_attachments", "method": { - "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService.GetRuntimeProjectAttachment", + "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService.ListRuntimeProjectAttachments", "service": { "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService", "shortName": "RuntimeProjectAttachmentService" }, - "shortName": "GetRuntimeProjectAttachment" + "shortName": "ListRuntimeProjectAttachments" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.GetRuntimeProjectAttachmentRequest" + "type": "google.cloud.apihub_v1.types.ListRuntimeProjectAttachmentsRequest" }, { - "name": "name", + "name": "parent", "type": "str" }, { @@ -6592,22 +13457,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.types.RuntimeProjectAttachment", - "shortName": "get_runtime_project_attachment" + "resultType": "google.cloud.apihub_v1.services.runtime_project_attachment_service.pagers.ListRuntimeProjectAttachmentsPager", + "shortName": "list_runtime_project_attachments" }, - "description": "Sample for GetRuntimeProjectAttachment", - "file": "apihub_v1_generated_runtime_project_attachment_service_get_runtime_project_attachment_sync.py", + "description": "Sample for ListRuntimeProjectAttachments", + "file": "apihub_v1_generated_runtime_project_attachment_service_list_runtime_project_attachments_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_RuntimeProjectAttachmentService_GetRuntimeProjectAttachment_sync", + "regionTag": "apihub_v1_generated_RuntimeProjectAttachmentService_ListRuntimeProjectAttachments_sync", "segments": [ { - "end": 51, + "end": 52, "start": 27, "type": "FULL" }, { - "end": 51, + "end": 52, "start": 27, "type": "SHORT" }, @@ -6627,36 +13492,37 @@ "type": "REQUEST_EXECUTION" }, { - "end": 52, + "end": 53, "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_runtime_project_attachment_service_get_runtime_project_attachment_sync.py" + "title": "apihub_v1_generated_runtime_project_attachment_service_list_runtime_project_attachments_sync.py" }, { "canonical": true, "clientMethod": { + "async": true, "client": { - "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceClient", - "shortName": "RuntimeProjectAttachmentServiceClient" + "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceAsyncClient", + "shortName": "RuntimeProjectAttachmentServiceAsyncClient" }, - "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceClient.list_runtime_project_attachments", + "fullName": "google.cloud.apihub_v1.RuntimeProjectAttachmentServiceAsyncClient.lookup_runtime_project_attachment", "method": { - "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService.ListRuntimeProjectAttachments", + "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService.LookupRuntimeProjectAttachment", "service": { "fullName": "google.cloud.apihub.v1.RuntimeProjectAttachmentService", "shortName": "RuntimeProjectAttachmentService" }, - "shortName": "ListRuntimeProjectAttachments" + "shortName": "LookupRuntimeProjectAttachment" }, "parameters": [ { "name": "request", - "type": "google.cloud.apihub_v1.types.ListRuntimeProjectAttachmentsRequest" + "type": "google.cloud.apihub_v1.types.LookupRuntimeProjectAttachmentRequest" }, { - "name": "parent", + "name": "name", "type": "str" }, { @@ -6672,22 +13538,22 @@ "type": "Sequence[Tuple[str, Union[str, bytes]]]" } ], - "resultType": "google.cloud.apihub_v1.services.runtime_project_attachment_service.pagers.ListRuntimeProjectAttachmentsPager", - "shortName": "list_runtime_project_attachments" + "resultType": "google.cloud.apihub_v1.types.LookupRuntimeProjectAttachmentResponse", + "shortName": "lookup_runtime_project_attachment" }, - "description": "Sample for ListRuntimeProjectAttachments", - "file": "apihub_v1_generated_runtime_project_attachment_service_list_runtime_project_attachments_sync.py", + "description": "Sample for LookupRuntimeProjectAttachment", + "file": "apihub_v1_generated_runtime_project_attachment_service_lookup_runtime_project_attachment_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "apihub_v1_generated_RuntimeProjectAttachmentService_ListRuntimeProjectAttachments_sync", + "regionTag": "apihub_v1_generated_RuntimeProjectAttachmentService_LookupRuntimeProjectAttachment_async", "segments": [ { - "end": 52, + "end": 51, "start": 27, "type": "FULL" }, { - "end": 52, + "end": 51, "start": 27, "type": "SHORT" }, @@ -6707,12 +13573,12 @@ "type": "REQUEST_EXECUTION" }, { - "end": 53, + "end": 52, "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "apihub_v1_generated_runtime_project_attachment_service_list_runtime_project_attachments_sync.py" + "title": "apihub_v1_generated_runtime_project_attachment_service_lookup_runtime_project_attachment_async.py" }, { "canonical": true, diff --git a/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub.py b/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub.py index ffedfcadf560..698bc0302452 100644 --- a/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub.py +++ b/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub.py @@ -56,7 +56,12 @@ from google.protobuf import field_mask_pb2 # type: ignore from google.protobuf import timestamp_pb2 # type: ignore -from google.cloud.apihub_v1.services.api_hub import ApiHubClient, pagers, transports +from google.cloud.apihub_v1.services.api_hub import ( + ApiHubAsyncClient, + ApiHubClient, + pagers, + transports, +) from google.cloud.apihub_v1.types import apihub_service, common_fields CRED_INFO_JSON = { @@ -205,6 +210,11 @@ def test__get_client_cert_source(): "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubClient), ) +@mock.patch.object( + ApiHubAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubAsyncClient), +) def test__get_api_endpoint(): api_override = "foo.com" mock_client_cert_source = mock.Mock() @@ -329,6 +339,8 @@ def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code): @pytest.mark.parametrize( "client_class,transport_name", [ + (ApiHubClient, "grpc"), + (ApiHubAsyncClient, "grpc_asyncio"), (ApiHubClient, "rest"), ], ) @@ -353,6 +365,8 @@ def test_api_hub_client_from_service_account_info(client_class, transport_name): @pytest.mark.parametrize( "transport_class,transport_name", [ + (transports.ApiHubGrpcTransport, "grpc"), + (transports.ApiHubGrpcAsyncIOTransport, "grpc_asyncio"), (transports.ApiHubRestTransport, "rest"), ], ) @@ -375,6 +389,8 @@ def test_api_hub_client_service_account_always_use_jwt(transport_class, transpor @pytest.mark.parametrize( "client_class,transport_name", [ + (ApiHubClient, "grpc"), + (ApiHubAsyncClient, "grpc_asyncio"), (ApiHubClient, "rest"), ], ) @@ -406,17 +422,20 @@ def test_api_hub_client_from_service_account_file(client_class, transport_name): def test_api_hub_client_get_transport_class(): transport = ApiHubClient.get_transport_class() available_transports = [ + transports.ApiHubGrpcTransport, transports.ApiHubRestTransport, ] assert transport in available_transports - transport = ApiHubClient.get_transport_class("rest") - assert transport == transports.ApiHubRestTransport + transport = ApiHubClient.get_transport_class("grpc") + assert transport == transports.ApiHubGrpcTransport @pytest.mark.parametrize( "client_class,transport_class,transport_name", [ + (ApiHubClient, transports.ApiHubGrpcTransport, "grpc"), + (ApiHubAsyncClient, transports.ApiHubGrpcAsyncIOTransport, "grpc_asyncio"), (ApiHubClient, transports.ApiHubRestTransport, "rest"), ], ) @@ -425,6 +444,11 @@ def test_api_hub_client_get_transport_class(): "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubClient), ) +@mock.patch.object( + ApiHubAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubAsyncClient), +) def test_api_hub_client_client_options(client_class, transport_class, transport_name): # Check that if channel is provided we won't create a new one. with mock.patch.object(ApiHubClient, "get_transport_class") as gtc: @@ -556,6 +580,20 @@ def test_api_hub_client_client_options(client_class, transport_class, transport_ @pytest.mark.parametrize( "client_class,transport_class,transport_name,use_client_cert_env", [ + (ApiHubClient, transports.ApiHubGrpcTransport, "grpc", "true"), + ( + ApiHubAsyncClient, + transports.ApiHubGrpcAsyncIOTransport, + "grpc_asyncio", + "true", + ), + (ApiHubClient, transports.ApiHubGrpcTransport, "grpc", "false"), + ( + ApiHubAsyncClient, + transports.ApiHubGrpcAsyncIOTransport, + "grpc_asyncio", + "false", + ), (ApiHubClient, transports.ApiHubRestTransport, "rest", "true"), (ApiHubClient, transports.ApiHubRestTransport, "rest", "false"), ], @@ -565,6 +603,11 @@ def test_api_hub_client_client_options(client_class, transport_class, transport_ "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubClient), ) +@mock.patch.object( + ApiHubAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubAsyncClient), +) @mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) def test_api_hub_client_mtls_env_auto( client_class, transport_class, transport_name, use_client_cert_env @@ -668,10 +711,13 @@ def test_api_hub_client_mtls_env_auto( ) -@pytest.mark.parametrize("client_class", [ApiHubClient]) +@pytest.mark.parametrize("client_class", [ApiHubClient, ApiHubAsyncClient]) @mock.patch.object( ApiHubClient, "DEFAULT_ENDPOINT", modify_default_endpoint(ApiHubClient) ) +@mock.patch.object( + ApiHubAsyncClient, "DEFAULT_ENDPOINT", modify_default_endpoint(ApiHubAsyncClient) +) def test_api_hub_client_get_mtls_endpoint_and_cert_source(client_class): mock_client_cert_source = mock.Mock() @@ -763,12 +809,17 @@ def test_api_hub_client_get_mtls_endpoint_and_cert_source(client_class): ) -@pytest.mark.parametrize("client_class", [ApiHubClient]) +@pytest.mark.parametrize("client_class", [ApiHubClient, ApiHubAsyncClient]) @mock.patch.object( ApiHubClient, "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubClient), ) +@mock.patch.object( + ApiHubAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubAsyncClient), +) def test_api_hub_client_client_api_endpoint(client_class): mock_client_cert_source = client_cert_source_callback api_override = "foo.com" @@ -845,6 +896,8 @@ def test_api_hub_client_client_api_endpoint(client_class): @pytest.mark.parametrize( "client_class,transport_class,transport_name", [ + (ApiHubClient, transports.ApiHubGrpcTransport, "grpc"), + (ApiHubAsyncClient, transports.ApiHubGrpcAsyncIOTransport, "grpc_asyncio"), (ApiHubClient, transports.ApiHubRestTransport, "rest"), ], ) @@ -876,6 +929,13 @@ def test_api_hub_client_client_options_scopes( @pytest.mark.parametrize( "client_class,transport_class,transport_name,grpc_helpers", [ + (ApiHubClient, transports.ApiHubGrpcTransport, "grpc", grpc_helpers), + ( + ApiHubAsyncClient, + transports.ApiHubGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), (ApiHubClient, transports.ApiHubRestTransport, "rest", None), ], ) @@ -903,13 +963,172 @@ def test_api_hub_client_client_options_credentials_file( ) -def test_create_api_rest_use_cached_wrapped_rpc(): +def test_api_hub_client_client_options_from_dict(): + with mock.patch( + "google.cloud.apihub_v1.services.api_hub.transports.ApiHubGrpcTransport.__init__" + ) as grpc_transport: + grpc_transport.return_value = None + client = ApiHubClient(client_options={"api_endpoint": "squid.clam.whelk"}) + grpc_transport.assert_called_once_with( + credentials=None, + credentials_file=None, + host="squid.clam.whelk", + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name,grpc_helpers", + [ + (ApiHubClient, transports.ApiHubGrpcTransport, "grpc", grpc_helpers), + ( + ApiHubAsyncClient, + transports.ApiHubGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), + ], +) +def test_api_hub_client_create_channel_credentials_file( + client_class, transport_class, transport_name, grpc_helpers +): + # Check the case credentials file is provided. + options = client_options.ClientOptions(credentials_file="credentials.json") + + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options, transport=transport_name) + patched.assert_called_once_with( + credentials=None, + credentials_file="credentials.json", + host=client._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE + ), + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + # test that the credentials from file are saved and used as the credentials. + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel" + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + file_creds = ga_credentials.AnonymousCredentials() + load_creds.return_value = (file_creds, None) + adc.return_value = (creds, None) + client = client_class(client_options=options, transport=transport_name) + create_channel.assert_called_with( + "apihub.googleapis.com:443", + credentials=file_creds, + credentials_file=None, + quota_project_id=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + scopes=None, + default_host="apihub.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.CreateApiRequest, + dict, + ], +) +def test_create_api(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.create_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Api( + name="name_value", + display_name="display_name_value", + description="description_value", + versions=["versions_value"], + selected_version="selected_version_value", + fingerprint="fingerprint_value", + ) + response = client.create_api(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.CreateApiRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Api) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.versions == ["versions_value"] + assert response.selected_version == "selected_version_value" + assert response.fingerprint == "fingerprint_value" + + +def test_create_api_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.CreateApiRequest( + parent="parent_value", + api_id="api_id_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_api), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.create_api(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.CreateApiRequest( + parent="parent_value", + api_id="api_id_value", + ) + + +def test_create_api_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -925,7 +1144,6 @@ def test_create_api_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.create_api] = mock_rpc - request = {} client.create_api(request) @@ -939,153 +1157,247 @@ def test_create_api_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_create_api_rest_required_fields(request_type=apihub_service.CreateApiRequest): - transport_class = transports.ApiHubRestTransport - - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) - - # verify fields with default values are dropped +@pytest.mark.asyncio +async def test_create_api_async_use_cached_wrapped_rpc(transport: str = "grpc_asyncio"): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_api._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify required fields with default values are now present + # Ensure method has been cached + assert ( + client._client._transport.create_api + in client._client._transport._wrapped_methods + ) - jsonified_request["parent"] = "parent_value" + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.create_api + ] = mock_rpc - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_api._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("api_id",)) - jsonified_request.update(unset_fields) + request = {} + await client.create_api(request) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type(**request_init) + await client.create_api(request) - # Designate an appropriate value for the returned response. - return_value = common_fields.Api() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Api.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) +@pytest.mark.asyncio +async def test_create_api_async( + transport: str = "grpc_asyncio", request_type=apihub_service.CreateApiRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # 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() - response = client.create_api(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Api( + name="name_value", + display_name="display_name_value", + description="description_value", + versions=["versions_value"], + selected_version="selected_version_value", + fingerprint="fingerprint_value", + ) + ) + response = await client.create_api(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.CreateApiRequest() + assert args[0] == request + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Api) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.versions == ["versions_value"] + assert response.selected_version == "selected_version_value" + assert response.fingerprint == "fingerprint_value" -def test_create_api_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials - ) - unset_fields = transport.create_api._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(("apiId",)) - & set( - ( - "parent", - "api", - ) - ) - ) +@pytest.mark.asyncio +async def test_create_api_async_from_dict(): + await test_create_api_async(request_type=dict) -def test_create_api_rest_flattened(): +def test_create_api_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Api() + # 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 = apihub_service.CreateApiRequest() - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + request.parent = "parent_value" - # get truthy value for each flattened field - mock_args = dict( - parent="parent_value", - api=common_fields.Api(name="name_value"), + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_api), "__call__") as call: + call.return_value = common_fields.Api() + client.create_api(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", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_create_api_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = apihub_service.CreateApiRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_api), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(common_fields.Api()) + await client.create_api(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", + "parent=parent_value", + ) in kw["metadata"] + + +def test_create_api_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Api() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.create_api( + parent="parent_value", + api=common_fields.Api(name="name_value"), api_id="api_id_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Api.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].api + mock_val = common_fields.Api(name="name_value") + assert arg == mock_val + arg = args[0].api_id + mock_val = "api_id_value" + assert arg == mock_val + + +def test_create_api_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) - client.create_api(**mock_args) + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_api( + apihub_service.CreateApiRequest(), + parent="parent_value", + api=common_fields.Api(name="name_value"), + api_id="api_id_value", + ) + + +@pytest.mark.asyncio +async def test_create_api_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Api() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(common_fields.Api()) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.create_api( + parent="parent_value", + api=common_fields.Api(name="name_value"), + api_id="api_id_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/apis" % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].api + mock_val = common_fields.Api(name="name_value") + assert arg == mock_val + arg = args[0].api_id + mock_val = "api_id_value" + assert arg == mock_val -def test_create_api_rest_flattened_error(transport: str = "rest"): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_create_api_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.create_api( + await client.create_api( apihub_service.CreateApiRequest(), parent="parent_value", api=common_fields.Api(name="name_value"), @@ -1093,13 +1405,87 @@ def test_create_api_rest_flattened_error(transport: str = "rest"): ) -def test_get_api_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.GetApiRequest, + dict, + ], +) +def test_get_api(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.get_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Api( + name="name_value", + display_name="display_name_value", + description="description_value", + versions=["versions_value"], + selected_version="selected_version_value", + fingerprint="fingerprint_value", + ) + response = client.get_api(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.GetApiRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Api) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.versions == ["versions_value"] + assert response.selected_version == "selected_version_value" + assert response.fingerprint == "fingerprint_value" + + +def test_get_api_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.GetApiRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_api), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_api(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.GetApiRequest( + name="name_value", + ) + + +def test_get_api_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1115,7 +1501,6 @@ def test_get_api_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.get_api] = mock_rpc - request = {} client.get_api(request) @@ -1129,134 +1514,181 @@ def test_get_api_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_get_api_rest_required_fields(request_type=apihub_service.GetApiRequest): - transport_class = transports.ApiHubRestTransport +@pytest.mark.asyncio +async def test_get_api_async_use_cached_wrapped_rpc(transport: str = "grpc_asyncio"): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.get_api + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.get_api + ] = mock_rpc + + request = {} + await client.get_api(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.get_api(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_get_api_async( + transport: str = "grpc_asyncio", request_type=apihub_service.GetApiRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # verify fields with default values are dropped + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_api._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Api( + name="name_value", + display_name="display_name_value", + description="description_value", + versions=["versions_value"], + selected_version="selected_version_value", + fingerprint="fingerprint_value", + ) + ) + response = await client.get_api(request) - # verify required fields with default values are now present + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.GetApiRequest() + assert args[0] == request - jsonified_request["name"] = "name_value" + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Api) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.versions == ["versions_value"] + assert response.selected_version == "selected_version_value" + assert response.fingerprint == "fingerprint_value" - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_api._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" +@pytest.mark.asyncio +async def test_get_api_async_from_dict(): + await test_get_api_async(request_type=dict) + +def test_get_api_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = common_fields.Api() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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 = apihub_service.GetApiRequest() - # Convert return value to protobuf type - return_value = common_fields.Api.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.name = "name_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_api), "__call__") as call: + call.return_value = common_fields.Api() + client.get_api(request) - response = client.get_api(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] -def test_get_api_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_get_api_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.get_api._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # 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 = apihub_service.GetApiRequest() + request.name = "name_value" -def test_get_api_rest_flattened(): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_api), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(common_fields.Api()) + await client.get_api(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", + "name=name_value", + ) in kw["metadata"] + + +def test_get_api_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Api() - - # get arguments that satisfy an http rule for this method - sample_request = {"name": "projects/sample1/locations/sample2/apis/sample3"} - - # get truthy value for each flattened field - mock_args = dict( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Api() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_api( name="name_value", ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Api.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - - client.get_api(**mock_args) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/apis/*}" % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_get_api_rest_flattened_error(transport: str = "rest"): +def test_get_api_flattened_error(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened @@ -1268,13 +1700,123 @@ def test_get_api_rest_flattened_error(transport: str = "rest"): ) -def test_list_apis_rest_use_cached_wrapped_rpc(): +@pytest.mark.asyncio +async def test_get_api_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Api() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(common_fields.Api()) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_api( + name="name_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_get_api_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.get_api( + apihub_service.GetApiRequest(), + name="name_value", + ) + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.ListApisRequest, + dict, + ], +) +def test_list_apis(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.list_apis), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListApisResponse( + next_page_token="next_page_token_value", + ) + response = client.list_apis(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.ListApisRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListApisPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_apis_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.ListApisRequest( + parent="parent_value", + filter="filter_value", + page_token="page_token_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_apis), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.list_apis(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.ListApisRequest( + parent="parent_value", + filter="filter_value", + page_token="page_token_value", + ) + + +def test_list_apis_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1290,7 +1832,6 @@ def test_list_apis_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.list_apis] = mock_rpc - request = {} client.list_apis(request) @@ -1304,151 +1845,173 @@ def test_list_apis_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_list_apis_rest_required_fields(request_type=apihub_service.ListApisRequest): - transport_class = transports.ApiHubRestTransport +@pytest.mark.asyncio +async def test_list_apis_async_use_cached_wrapped_rpc(transport: str = "grpc_asyncio"): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.list_apis + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_apis._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.list_apis + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.list_apis(request) - jsonified_request["parent"] = "parent_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_apis._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "filter", - "page_size", - "page_token", - ) + await client.list_apis(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_list_apis_async( + transport: str = "grpc_asyncio", request_type=apihub_service.ListApisRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + # 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.list_apis), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListApisResponse( + next_page_token="next_page_token_value", + ) + ) + response = await client.list_apis(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.ListApisRequest() + assert args[0] == request + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListApisAsyncPager) + assert response.next_page_token == "next_page_token_value" + + +@pytest.mark.asyncio +async def test_list_apis_async_from_dict(): + await test_list_apis_async(request_type=dict) + + +def test_list_apis_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = apihub_service.ListApisResponse() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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 = apihub_service.ListApisRequest() - # Convert return value to protobuf type - return_value = apihub_service.ListApisResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.parent = "parent_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_apis), "__call__") as call: + call.return_value = apihub_service.ListApisResponse() + client.list_apis(request) - response = client.list_apis(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] -def test_list_apis_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_list_apis_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.list_apis._get_unset_required_fields({}) - assert set(unset_fields) == ( - set( - ( - "filter", - "pageSize", - "pageToken", - ) + # 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 = apihub_service.ListApisRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_apis), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListApisResponse() ) - & set(("parent",)) - ) + await client.list_apis(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request -def test_list_apis_rest_flattened(): + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +def test_list_apis_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = apihub_service.ListApisResponse() - - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} - - # get truthy value for each flattened field - mock_args = dict( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_apis), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListApisResponse() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.list_apis( parent="parent_value", ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = apihub_service.ListApisResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - - client.list_apis(**mock_args) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/apis" % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val -def test_list_apis_rest_flattened_error(transport: str = "rest"): +def test_list_apis_flattened_error(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened @@ -1460,18 +2023,60 @@ def test_list_apis_rest_flattened_error(transport: str = "rest"): ) -def test_list_apis_rest_pager(transport: str = "rest"): +@pytest.mark.asyncio +async def test_list_apis_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_apis), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListApisResponse() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListApisResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.list_apis( + parent="parent_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_list_apis_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.list_apis( + apihub_service.ListApisRequest(), + parent="parent_value", + ) + + +def test_list_apis_pager(transport_name: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport=transport_name, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # TODO(kbandes): remove this mock unless there's a good reason for it. - # with mock.patch.object(path_template, 'transcode') as transcode: - # Set the response as a series of pages - response = ( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_apis), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( apihub_service.ListApisResponse( apis=[ common_fields.Api(), @@ -1496,38 +2101,243 @@ def test_list_apis_rest_pager(transport: str = "rest"): common_fields.Api(), ], ), + RuntimeError, ) - # Two responses for two calls - response = response + response - - # Wrap the values into proper Response objs - response = tuple(apihub_service.ListApisResponse.to_json(x) for x in response) - return_values = tuple(Response() for i in response) - for return_val, response_val in zip(return_values, response): - return_val._content = response_val.encode("UTF-8") - return_val.status_code = 200 - req.side_effect = return_values - sample_request = {"parent": "projects/sample1/locations/sample2"} + expected_metadata = () + retry = retries.Retry() + timeout = 5 + expected_metadata = tuple(expected_metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", ""),)), + ) + pager = client.list_apis(request={}, retry=retry, timeout=timeout) - pager = client.list_apis(request=sample_request) + assert pager._metadata == expected_metadata + assert pager._retry == retry + assert pager._timeout == timeout results = list(pager) assert len(results) == 6 assert all(isinstance(i, common_fields.Api) for i in results) - pages = list(client.list_apis(request=sample_request).pages) + +def test_list_apis_pages(transport_name: str = "grpc"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport_name, + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_apis), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListApisResponse( + apis=[ + common_fields.Api(), + common_fields.Api(), + common_fields.Api(), + ], + next_page_token="abc", + ), + apihub_service.ListApisResponse( + apis=[], + next_page_token="def", + ), + apihub_service.ListApisResponse( + apis=[ + common_fields.Api(), + ], + next_page_token="ghi", + ), + apihub_service.ListApisResponse( + apis=[ + common_fields.Api(), + common_fields.Api(), + ], + ), + RuntimeError, + ) + pages = list(client.list_apis(request={}).pages) for page_, token in zip(pages, ["abc", "def", "ghi", ""]): assert page_.raw_page.next_page_token == token -def test_update_api_rest_use_cached_wrapped_rpc(): +@pytest.mark.asyncio +async def test_list_apis_async_pager(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_apis), "__call__", new_callable=mock.AsyncMock + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListApisResponse( + apis=[ + common_fields.Api(), + common_fields.Api(), + common_fields.Api(), + ], + next_page_token="abc", + ), + apihub_service.ListApisResponse( + apis=[], + next_page_token="def", + ), + apihub_service.ListApisResponse( + apis=[ + common_fields.Api(), + ], + next_page_token="ghi", + ), + apihub_service.ListApisResponse( + apis=[ + common_fields.Api(), + common_fields.Api(), + ], + ), + RuntimeError, + ) + async_pager = await client.list_apis( + request={}, + ) + assert async_pager.next_page_token == "abc" + responses = [] + async for response in async_pager: # pragma: no branch + responses.append(response) + + assert len(responses) == 6 + assert all(isinstance(i, common_fields.Api) for i in responses) + + +@pytest.mark.asyncio +async def test_list_apis_async_pages(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_apis), "__call__", new_callable=mock.AsyncMock + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListApisResponse( + apis=[ + common_fields.Api(), + common_fields.Api(), + common_fields.Api(), + ], + next_page_token="abc", + ), + apihub_service.ListApisResponse( + apis=[], + next_page_token="def", + ), + apihub_service.ListApisResponse( + apis=[ + common_fields.Api(), + ], + next_page_token="ghi", + ), + apihub_service.ListApisResponse( + apis=[ + common_fields.Api(), + common_fields.Api(), + ], + ), + RuntimeError, + ) + pages = [] + # Workaround issue in python 3.9 related to code coverage by adding `# pragma: no branch` + # See https://github.com/googleapis/gapic-generator-python/pull/1174#issuecomment-1025132372 + async for page_ in ( # pragma: no branch + await client.list_apis(request={}) + ).pages: + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.UpdateApiRequest, + dict, + ], +) +def test_update_api(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.update_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Api( + name="name_value", + display_name="display_name_value", + description="description_value", + versions=["versions_value"], + selected_version="selected_version_value", + fingerprint="fingerprint_value", + ) + response = client.update_api(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.UpdateApiRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Api) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.versions == ["versions_value"] + assert response.selected_version == "selected_version_value" + assert response.fingerprint == "fingerprint_value" + + +def test_update_api_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.UpdateApiRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_api), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.update_api(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.UpdateApiRequest() + + +def test_update_api_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1543,7 +2353,6 @@ def test_update_api_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.update_api] = mock_rpc - request = {} client.update_api(request) @@ -1557,162 +2366,312 @@ def test_update_api_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_update_api_rest_required_fields(request_type=apihub_service.UpdateApiRequest): - transport_class = transports.ApiHubRestTransport +@pytest.mark.asyncio +async def test_update_api_async_use_cached_wrapped_rpc(transport: str = "grpc_asyncio"): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.update_api + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_api._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.update_api + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.update_api(request) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_api._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("update_mask",)) - jsonified_request.update(unset_fields) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - # verify required fields with non-default values are left alone + await client.update_api(request) - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_update_api_async( + transport: str = "grpc_asyncio", request_type=apihub_service.UpdateApiRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = common_fields.Api() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "patch", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # 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() - response_value = Response() - response_value.status_code = 200 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Api( + name="name_value", + display_name="display_name_value", + description="description_value", + versions=["versions_value"], + selected_version="selected_version_value", + fingerprint="fingerprint_value", + ) + ) + response = await client.update_api(request) - # Convert return value to protobuf type - return_value = common_fields.Api.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.UpdateApiRequest() + assert args[0] == request - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Api) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.versions == ["versions_value"] + assert response.selected_version == "selected_version_value" + assert response.fingerprint == "fingerprint_value" - response = client.update_api(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_update_api_async_from_dict(): + await test_update_api_async(request_type=dict) -def test_update_api_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +def test_update_api_field_headers(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = transport.update_api._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(("updateMask",)) - & set( - ( - "api", - "updateMask", - ) - ) + # 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 = apihub_service.UpdateApiRequest() + + request.api.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_api), "__call__") as call: + call.return_value = common_fields.Api() + client.update_api(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", + "api.name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_update_api_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) + # 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 = apihub_service.UpdateApiRequest() -def test_update_api_rest_flattened(): + request.api.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_api), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(common_fields.Api()) + await client.update_api(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", + "api.name=name_value", + ) in kw["metadata"] + + +def test_update_api_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Api() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Api() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.update_api( + api=common_fields.Api(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "api": {"name": "projects/sample1/locations/sample2/apis/sample3"} - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].api + mock_val = common_fields.Api(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_update_api_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_api( + apihub_service.UpdateApiRequest(), api=common_fields.Api(name="name_value"), update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Api.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.update_api(**mock_args) +@pytest.mark.asyncio +async def test_update_api_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Api() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(common_fields.Api()) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.update_api( + api=common_fields.Api(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{api.name=projects/*/locations/*/apis/*}" % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].api + mock_val = common_fields.Api(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val -def test_update_api_rest_flattened_error(transport: str = "rest"): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_update_api_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.update_api( + await client.update_api( apihub_service.UpdateApiRequest(), api=common_fields.Api(name="name_value"), update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) -def test_delete_api_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.DeleteApiRequest, + dict, + ], +) +def test_delete_api(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.delete_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.delete_api(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.DeleteApiRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert response is None + + +def test_delete_api_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.DeleteApiRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_api), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.delete_api(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.DeleteApiRequest( + name="name_value", + ) + + +def test_delete_api_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1728,7 +2687,6 @@ def test_delete_api_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.delete_api] = mock_rpc - request = {} client.delete_api(request) @@ -1742,149 +2700,306 @@ def test_delete_api_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_delete_api_rest_required_fields(request_type=apihub_service.DeleteApiRequest): - transport_class = transports.ApiHubRestTransport +@pytest.mark.asyncio +async def test_delete_api_async_use_cached_wrapped_rpc(transport: str = "grpc_asyncio"): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.delete_api + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.delete_api + ] = mock_rpc + + request = {} + await client.delete_api(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.delete_api(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_delete_api_async( + transport: str = "grpc_asyncio", request_type=apihub_service.DeleteApiRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # verify fields with default values are dropped + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_api._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_api(request) - # verify required fields with default values are now present + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.DeleteApiRequest() + assert args[0] == request - jsonified_request["name"] = "name_value" + # Establish that the response is the type that we expect. + assert response is None - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_api._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("force",)) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" +@pytest.mark.asyncio +async def test_delete_api_async_from_dict(): + await test_delete_api_async(request_type=dict) + +def test_delete_api_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = None - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "delete", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # 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 = apihub_service.DeleteApiRequest() - response_value = Response() - response_value.status_code = 200 - json_return_value = "" + request.name = "name_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_api), "__call__") as call: + call.return_value = None + client.delete_api(request) - response = client.delete_api(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] -def test_delete_api_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_delete_api_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.delete_api._get_unset_required_fields({}) - assert set(unset_fields) == (set(("force",)) & set(("name",))) + # 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 = apihub_service.DeleteApiRequest() + request.name = "name_value" -def test_delete_api_rest_flattened(): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_api), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_api(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", + "name=name_value", + ) in kw["metadata"] + + +def test_delete_api_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = None + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.delete_api( + name="name_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = {"name": "projects/sample1/locations/sample2/apis/sample3"} + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_delete_api_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_api( + apihub_service.DeleteApiRequest(), name="name_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - json_return_value = "" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_api(**mock_args) +@pytest.mark.asyncio +async def test_delete_api_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.delete_api( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/apis/*}" % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_delete_api_rest_flattened_error(transport: str = "rest"): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_delete_api_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.delete_api( + await client.delete_api( apihub_service.DeleteApiRequest(), name="name_value", ) -def test_create_version_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.CreateVersionRequest, + dict, + ], +) +def test_create_version(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.create_version), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Version( + name="name_value", + display_name="display_name_value", + description="description_value", + specs=["specs_value"], + api_operations=["api_operations_value"], + definitions=["definitions_value"], + deployments=["deployments_value"], + selected_deployment="selected_deployment_value", + ) + response = client.create_version(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.CreateVersionRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Version) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.specs == ["specs_value"] + assert response.api_operations == ["api_operations_value"] + assert response.definitions == ["definitions_value"] + assert response.deployments == ["deployments_value"] + assert response.selected_deployment == "selected_deployment_value" + + +def test_create_version_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.CreateVersionRequest( + parent="parent_value", + version_id="version_id_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_version), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.create_version(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.CreateVersionRequest( + parent="parent_value", + version_id="version_id_value", + ) + + +def test_create_version_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1900,7 +3015,6 @@ def test_create_version_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.create_version] = mock_rpc - request = {} client.create_version(request) @@ -1914,156 +3028,257 @@ def test_create_version_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_create_version_rest_required_fields( - request_type=apihub_service.CreateVersionRequest, +@pytest.mark.asyncio +async def test_create_version_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.create_version + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_version._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.create_version + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.create_version(request) - jsonified_request["parent"] = "parent_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_version._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("version_id",)) - jsonified_request.update(unset_fields) + await client.create_version(request) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + +@pytest.mark.asyncio +async def test_create_version_async( + transport: str = "grpc_asyncio", request_type=apihub_service.CreateVersionRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = common_fields.Version() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # 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() - response_value = Response() - response_value.status_code = 200 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_version), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Version( + name="name_value", + display_name="display_name_value", + description="description_value", + specs=["specs_value"], + api_operations=["api_operations_value"], + definitions=["definitions_value"], + deployments=["deployments_value"], + selected_deployment="selected_deployment_value", + ) + ) + response = await client.create_version(request) - # Convert return value to protobuf type - return_value = common_fields.Version.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.CreateVersionRequest() + assert args[0] == request - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Version) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.specs == ["specs_value"] + assert response.api_operations == ["api_operations_value"] + assert response.definitions == ["definitions_value"] + assert response.deployments == ["deployments_value"] + assert response.selected_deployment == "selected_deployment_value" - response = client.create_version(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_create_version_async_from_dict(): + await test_create_version_async(request_type=dict) -def test_create_version_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +def test_create_version_field_headers(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = transport.create_version._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(("versionId",)) - & set( - ( - "parent", - "version", - ) - ) - ) + # 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 = apihub_service.CreateVersionRequest() + request.parent = "parent_value" -def test_create_version_rest_flattened(): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_version), "__call__") as call: + call.return_value = common_fields.Version() + client.create_version(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", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_create_version_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = apihub_service.CreateVersionRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_version), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Version() + ) + await client.create_version(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", + "parent=parent_value", + ) in kw["metadata"] + + +def test_create_version_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Version() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_version), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Version() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.create_version( + parent="parent_value", + version=common_fields.Version(name="name_value"), + version_id="version_id_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2/apis/sample3"} + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].version + mock_val = common_fields.Version(name="name_value") + assert arg == mock_val + arg = args[0].version_id + mock_val = "version_id_value" + assert arg == mock_val + + +def test_create_version_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) - # get truthy value for each flattened field - mock_args = dict( + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_version( + apihub_service.CreateVersionRequest(), parent="parent_value", version=common_fields.Version(name="name_value"), version_id="version_id_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Version.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_version(**mock_args) +@pytest.mark.asyncio +async def test_create_version_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_version), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Version() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Version() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.create_version( + parent="parent_value", + version=common_fields.Version(name="name_value"), + version_id="version_id_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*/apis/*}/versions" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].version + mock_val = common_fields.Version(name="name_value") + assert arg == mock_val + arg = args[0].version_id + mock_val = "version_id_value" + assert arg == mock_val -def test_create_version_rest_flattened_error(transport: str = "rest"): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_create_version_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.create_version( + await client.create_version( apihub_service.CreateVersionRequest(), parent="parent_value", version=common_fields.Version(name="name_value"), @@ -2071,13 +3286,91 @@ def test_create_version_rest_flattened_error(transport: str = "rest"): ) -def test_get_version_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.GetVersionRequest, + dict, + ], +) +def test_get_version(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.get_version), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Version( + name="name_value", + display_name="display_name_value", + description="description_value", + specs=["specs_value"], + api_operations=["api_operations_value"], + definitions=["definitions_value"], + deployments=["deployments_value"], + selected_deployment="selected_deployment_value", + ) + response = client.get_version(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.GetVersionRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Version) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.specs == ["specs_value"] + assert response.api_operations == ["api_operations_value"] + assert response.definitions == ["definitions_value"] + assert response.deployments == ["deployments_value"] + assert response.selected_deployment == "selected_deployment_value" + + +def test_get_version_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.GetVersionRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_version), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_version(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.GetVersionRequest( + name="name_value", + ) + + +def test_get_version_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -2093,7 +3386,6 @@ def test_get_version_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.get_version] = mock_rpc - request = {} client.get_version(request) @@ -2107,139 +3399,189 @@ def test_get_version_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_get_version_rest_required_fields( - request_type=apihub_service.GetVersionRequest, +@pytest.mark.asyncio +async def test_get_version_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport - - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) - - # verify fields with default values are dropped + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_version._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify required fields with default values are now present + # Ensure method has been cached + assert ( + client._client._transport.get_version + in client._client._transport._wrapped_methods + ) - jsonified_request["name"] = "name_value" + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.get_version + ] = mock_rpc - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_version._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + request = {} + await client.get_version(request) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type(**request_init) + await client.get_version(request) - # Designate an appropriate value for the returned response. - return_value = common_fields.Version() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Version.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) +@pytest.mark.asyncio +async def test_get_version_async( + transport: str = "grpc_asyncio", request_type=apihub_service.GetVersionRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # 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() - response = client.get_version(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_version), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Version( + name="name_value", + display_name="display_name_value", + description="description_value", + specs=["specs_value"], + api_operations=["api_operations_value"], + definitions=["definitions_value"], + deployments=["deployments_value"], + selected_deployment="selected_deployment_value", + ) + ) + response = await client.get_version(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.GetVersionRequest() + assert args[0] == request + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Version) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.specs == ["specs_value"] + assert response.api_operations == ["api_operations_value"] + assert response.definitions == ["definitions_value"] + assert response.deployments == ["deployments_value"] + assert response.selected_deployment == "selected_deployment_value" -def test_get_version_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials - ) - unset_fields = transport.get_version._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) +@pytest.mark.asyncio +async def test_get_version_async_from_dict(): + await test_get_version_async(request_type=dict) -def test_get_version_rest_flattened(): +def test_get_version_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Version() + # 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 = apihub_service.GetVersionRequest() - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" - } + request.name = "name_value" - # get truthy value for each flattened field - mock_args = dict( - name="name_value", + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_version), "__call__") as call: + call.return_value = common_fields.Version() + client.get_version(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", + "name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_get_version_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = apihub_service.GetVersionRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_version), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Version() ) - mock_args.update(sample_request) + await client.get_version(request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Version.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request - client.get_version(**mock_args) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] + + +def test_get_version_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_version), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Version() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_version( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/apis/*/versions/*}" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_get_version_rest_flattened_error(transport: str = "rest"): +def test_get_version_flattened_error(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened @@ -2251,13 +3593,125 @@ def test_get_version_rest_flattened_error(transport: str = "rest"): ) -def test_list_versions_rest_use_cached_wrapped_rpc(): +@pytest.mark.asyncio +async def test_get_version_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_version), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Version() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Version() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_version( + name="name_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_get_version_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.get_version( + apihub_service.GetVersionRequest(), + name="name_value", + ) + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.ListVersionsRequest, + dict, + ], +) +def test_list_versions(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.list_versions), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListVersionsResponse( + next_page_token="next_page_token_value", + ) + response = client.list_versions(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.ListVersionsRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListVersionsPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_versions_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.ListVersionsRequest( + parent="parent_value", + filter="filter_value", + page_token="page_token_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_versions), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.list_versions(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.ListVersionsRequest( + parent="parent_value", + filter="filter_value", + page_token="page_token_value", + ) + + +def test_list_versions_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -2273,7 +3727,6 @@ def test_list_versions_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.list_versions] = mock_rpc - request = {} client.list_versions(request) @@ -2287,154 +3740,175 @@ def test_list_versions_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_list_versions_rest_required_fields( - request_type=apihub_service.ListVersionsRequest, +@pytest.mark.asyncio +async def test_list_versions_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.list_versions + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_versions._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.list_versions + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.list_versions(request) - jsonified_request["parent"] = "parent_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_versions._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "filter", - "page_size", - "page_token", - ) + await client.list_versions(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_list_versions_async( + transport: str = "grpc_asyncio", request_type=apihub_service.ListVersionsRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + # 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.list_versions), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListVersionsResponse( + next_page_token="next_page_token_value", + ) + ) + response = await client.list_versions(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.ListVersionsRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListVersionsAsyncPager) + assert response.next_page_token == "next_page_token_value" + + +@pytest.mark.asyncio +async def test_list_versions_async_from_dict(): + await test_list_versions_async(request_type=dict) + +def test_list_versions_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = apihub_service.ListVersionsResponse() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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 = apihub_service.ListVersionsRequest() - # Convert return value to protobuf type - return_value = apihub_service.ListVersionsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.parent = "parent_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_versions), "__call__") as call: + call.return_value = apihub_service.ListVersionsResponse() + client.list_versions(request) - response = client.list_versions(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] -def test_list_versions_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_list_versions_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.list_versions._get_unset_required_fields({}) - assert set(unset_fields) == ( - set( - ( - "filter", - "pageSize", - "pageToken", - ) + # 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 = apihub_service.ListVersionsRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_versions), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListVersionsResponse() ) - & set(("parent",)) - ) + await client.list_versions(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", + "parent=parent_value", + ) in kw["metadata"] -def test_list_versions_rest_flattened(): + +def test_list_versions_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = apihub_service.ListVersionsResponse() - - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2/apis/sample3"} - - # get truthy value for each flattened field - mock_args = dict( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_versions), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListVersionsResponse() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.list_versions( parent="parent_value", ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = apihub_service.ListVersionsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - - client.list_versions(**mock_args) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*/apis/*}/versions" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val -def test_list_versions_rest_flattened_error(transport: str = "rest"): +def test_list_versions_flattened_error(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened @@ -2446,18 +3920,60 @@ def test_list_versions_rest_flattened_error(transport: str = "rest"): ) -def test_list_versions_rest_pager(transport: str = "rest"): +@pytest.mark.asyncio +async def test_list_versions_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_versions), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListVersionsResponse() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListVersionsResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.list_versions( + parent="parent_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_list_versions_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.list_versions( + apihub_service.ListVersionsRequest(), + parent="parent_value", + ) + + +def test_list_versions_pager(transport_name: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport=transport_name, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # TODO(kbandes): remove this mock unless there's a good reason for it. - # with mock.patch.object(path_template, 'transcode') as transcode: - # Set the response as a series of pages - response = ( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_versions), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( apihub_service.ListVersionsResponse( versions=[ common_fields.Version(), @@ -2482,40 +3998,247 @@ def test_list_versions_rest_pager(transport: str = "rest"): common_fields.Version(), ], ), + RuntimeError, ) - # Two responses for two calls - response = response + response - # Wrap the values into proper Response objs - response = tuple( - apihub_service.ListVersionsResponse.to_json(x) for x in response + expected_metadata = () + retry = retries.Retry() + timeout = 5 + expected_metadata = tuple(expected_metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", ""),)), ) - return_values = tuple(Response() for i in response) - for return_val, response_val in zip(return_values, response): - return_val._content = response_val.encode("UTF-8") - return_val.status_code = 200 - req.side_effect = return_values - - sample_request = {"parent": "projects/sample1/locations/sample2/apis/sample3"} + pager = client.list_versions(request={}, retry=retry, timeout=timeout) - pager = client.list_versions(request=sample_request) + assert pager._metadata == expected_metadata + assert pager._retry == retry + assert pager._timeout == timeout results = list(pager) assert len(results) == 6 assert all(isinstance(i, common_fields.Version) for i in results) - pages = list(client.list_versions(request=sample_request).pages) + +def test_list_versions_pages(transport_name: str = "grpc"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport_name, + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_versions), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListVersionsResponse( + versions=[ + common_fields.Version(), + common_fields.Version(), + common_fields.Version(), + ], + next_page_token="abc", + ), + apihub_service.ListVersionsResponse( + versions=[], + next_page_token="def", + ), + apihub_service.ListVersionsResponse( + versions=[ + common_fields.Version(), + ], + next_page_token="ghi", + ), + apihub_service.ListVersionsResponse( + versions=[ + common_fields.Version(), + common_fields.Version(), + ], + ), + RuntimeError, + ) + pages = list(client.list_versions(request={}).pages) for page_, token in zip(pages, ["abc", "def", "ghi", ""]): assert page_.raw_page.next_page_token == token -def test_update_version_rest_use_cached_wrapped_rpc(): +@pytest.mark.asyncio +async def test_list_versions_async_pager(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_versions), "__call__", new_callable=mock.AsyncMock + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListVersionsResponse( + versions=[ + common_fields.Version(), + common_fields.Version(), + common_fields.Version(), + ], + next_page_token="abc", + ), + apihub_service.ListVersionsResponse( + versions=[], + next_page_token="def", + ), + apihub_service.ListVersionsResponse( + versions=[ + common_fields.Version(), + ], + next_page_token="ghi", + ), + apihub_service.ListVersionsResponse( + versions=[ + common_fields.Version(), + common_fields.Version(), + ], + ), + RuntimeError, + ) + async_pager = await client.list_versions( + request={}, + ) + assert async_pager.next_page_token == "abc" + responses = [] + async for response in async_pager: # pragma: no branch + responses.append(response) + + assert len(responses) == 6 + assert all(isinstance(i, common_fields.Version) for i in responses) + + +@pytest.mark.asyncio +async def test_list_versions_async_pages(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_versions), "__call__", new_callable=mock.AsyncMock + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListVersionsResponse( + versions=[ + common_fields.Version(), + common_fields.Version(), + common_fields.Version(), + ], + next_page_token="abc", + ), + apihub_service.ListVersionsResponse( + versions=[], + next_page_token="def", + ), + apihub_service.ListVersionsResponse( + versions=[ + common_fields.Version(), + ], + next_page_token="ghi", + ), + apihub_service.ListVersionsResponse( + versions=[ + common_fields.Version(), + common_fields.Version(), + ], + ), + RuntimeError, + ) + pages = [] + # Workaround issue in python 3.9 related to code coverage by adding `# pragma: no branch` + # See https://github.com/googleapis/gapic-generator-python/pull/1174#issuecomment-1025132372 + async for page_ in ( # pragma: no branch + await client.list_versions(request={}) + ).pages: + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.UpdateVersionRequest, + dict, + ], +) +def test_update_version(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.update_version), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Version( + name="name_value", + display_name="display_name_value", + description="description_value", + specs=["specs_value"], + api_operations=["api_operations_value"], + definitions=["definitions_value"], + deployments=["deployments_value"], + selected_deployment="selected_deployment_value", + ) + response = client.update_version(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.UpdateVersionRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Version) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.specs == ["specs_value"] + assert response.api_operations == ["api_operations_value"] + assert response.definitions == ["definitions_value"] + assert response.deployments == ["deployments_value"] + assert response.selected_deployment == "selected_deployment_value" + + +def test_update_version_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.UpdateVersionRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_version), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.update_version(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.UpdateVersionRequest() + + +def test_update_version_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -2531,7 +4254,6 @@ def test_update_version_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.update_version] = mock_rpc - request = {} client.update_version(request) @@ -2545,167 +4267,322 @@ def test_update_version_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_update_version_rest_required_fields( - request_type=apihub_service.UpdateVersionRequest, +@pytest.mark.asyncio +async def test_update_version_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport - - request_init = {} - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - # verify fields with default values are dropped + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_version._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Ensure method has been cached + assert ( + client._client._transport.update_version + in client._client._transport._wrapped_methods + ) - # verify required fields with default values are now present + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.update_version + ] = mock_rpc - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_version._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("update_mask",)) - jsonified_request.update(unset_fields) + request = {} + await client.update_version(request) - # verify required fields with non-default values are left alone + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.update_version(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_update_version_async( + transport: str = "grpc_asyncio", request_type=apihub_service.UpdateVersionRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + 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.update_version), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Version( + name="name_value", + display_name="display_name_value", + description="description_value", + specs=["specs_value"], + api_operations=["api_operations_value"], + definitions=["definitions_value"], + deployments=["deployments_value"], + selected_deployment="selected_deployment_value", + ) + ) + response = await client.update_version(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.UpdateVersionRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Version) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.specs == ["specs_value"] + assert response.api_operations == ["api_operations_value"] + assert response.definitions == ["definitions_value"] + assert response.deployments == ["deployments_value"] + assert response.selected_deployment == "selected_deployment_value" + +@pytest.mark.asyncio +async def test_update_version_async_from_dict(): + await test_update_version_async(request_type=dict) + + +def test_update_version_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = common_fields.Version() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "patch", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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 = apihub_service.UpdateVersionRequest() - # Convert return value to protobuf type - return_value = common_fields.Version.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.version.name = "name_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_version), "__call__") as call: + call.return_value = common_fields.Version() + client.update_version(request) - response = client.update_version(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "version.name=name_value", + ) in kw["metadata"] -def test_update_version_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_update_version_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.update_version._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(("updateMask",)) - & set( - ( - "version", - "updateMask", - ) + # 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 = apihub_service.UpdateVersionRequest() + + request.version.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_version), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Version() ) - ) + await client.update_version(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request -def test_update_version_rest_flattened(): + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "version.name=name_value", + ) in kw["metadata"] + + +def test_update_version_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Version() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_version), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Version() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.update_version( + version=common_fields.Version(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "version": { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" - } - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].version + mock_val = common_fields.Version(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_update_version_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_version( + apihub_service.UpdateVersionRequest(), version=common_fields.Version(name="name_value"), update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Version.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.update_version(**mock_args) +@pytest.mark.asyncio +async def test_update_version_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_version), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Version() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Version() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.update_version( + version=common_fields.Version(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{version.name=projects/*/locations/*/apis/*/versions/*}" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].version + mock_val = common_fields.Version(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val -def test_update_version_rest_flattened_error(transport: str = "rest"): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_update_version_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.update_version( + await client.update_version( apihub_service.UpdateVersionRequest(), version=common_fields.Version(name="name_value"), update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) -def test_delete_version_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.DeleteVersionRequest, + dict, + ], +) +def test_delete_version(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.delete_version), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.delete_version(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.DeleteVersionRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert response is None + + +def test_delete_version_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.DeleteVersionRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_version), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.delete_version(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.DeleteVersionRequest( + name="name_value", + ) + + +def test_delete_version_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -2721,7 +4598,6 @@ def test_delete_version_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.delete_version] = mock_rpc - request = {} client.delete_version(request) @@ -2735,154 +4611,300 @@ def test_delete_version_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_delete_version_rest_required_fields( - request_type=apihub_service.DeleteVersionRequest, +@pytest.mark.asyncio +async def test_delete_version_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.delete_version + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_version._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.delete_version + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.delete_version(request) - jsonified_request["name"] = "name_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_version._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("force",)) - jsonified_request.update(unset_fields) + await client.delete_version(request) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + +@pytest.mark.asyncio +async def test_delete_version_async( + transport: str = "grpc_asyncio", request_type=apihub_service.DeleteVersionRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + 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.delete_version), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_version(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.DeleteVersionRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert response is None + + +@pytest.mark.asyncio +async def test_delete_version_async_from_dict(): + await test_delete_version_async(request_type=dict) + + +def test_delete_version_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = None - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "delete", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # 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 = apihub_service.DeleteVersionRequest() - response_value = Response() - response_value.status_code = 200 - json_return_value = "" + request.name = "name_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_version), "__call__") as call: + call.return_value = None + client.delete_version(request) - response = client.delete_version(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] -def test_delete_version_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_delete_version_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.delete_version._get_unset_required_fields({}) - assert set(unset_fields) == (set(("force",)) & set(("name",))) + # 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 = apihub_service.DeleteVersionRequest() + request.name = "name_value" -def test_delete_version_rest_flattened(): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_version), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_version(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", + "name=name_value", + ) in kw["metadata"] + + +def test_delete_version_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = None + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_version), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.delete_version( + name="name_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_delete_version_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_version( + apihub_service.DeleteVersionRequest(), name="name_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - json_return_value = "" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_version(**mock_args) +@pytest.mark.asyncio +async def test_delete_version_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_version), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.delete_version( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/apis/*/versions/*}" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_delete_version_rest_flattened_error(transport: str = "rest"): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_delete_version_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.delete_version( + await client.delete_version( apihub_service.DeleteVersionRequest(), name="name_value", ) -def test_create_spec_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.CreateSpecRequest, + dict, + ], +) +def test_create_spec(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.create_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Spec( + name="name_value", + display_name="display_name_value", + source_uri="source_uri_value", + parsing_mode=common_fields.Spec.ParsingMode.RELAXED, + ) + response = client.create_spec(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.CreateSpecRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Spec) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.source_uri == "source_uri_value" + assert response.parsing_mode == common_fields.Spec.ParsingMode.RELAXED + + +def test_create_spec_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.CreateSpecRequest( + parent="parent_value", + spec_id="spec_id_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_spec), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.create_spec(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.CreateSpecRequest( + parent="parent_value", + spec_id="spec_id_value", + ) + + +def test_create_spec_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -2898,7 +4920,6 @@ def test_create_spec_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.create_spec] = mock_rpc - request = {} client.create_spec(request) @@ -2912,158 +4933,245 @@ def test_create_spec_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_create_spec_rest_required_fields( - request_type=apihub_service.CreateSpecRequest, +@pytest.mark.asyncio +async def test_create_spec_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.create_spec + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.create_spec + ] = mock_rpc + + request = {} + await client.create_spec(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.create_spec(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_create_spec_async( + transport: str = "grpc_asyncio", request_type=apihub_service.CreateSpecRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # verify fields with default values are dropped + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_spec._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Spec( + name="name_value", + display_name="display_name_value", + source_uri="source_uri_value", + parsing_mode=common_fields.Spec.ParsingMode.RELAXED, + ) + ) + response = await client.create_spec(request) - # verify required fields with default values are now present + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.CreateSpecRequest() + assert args[0] == request - jsonified_request["parent"] = "parent_value" + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Spec) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.source_uri == "source_uri_value" + assert response.parsing_mode == common_fields.Spec.ParsingMode.RELAXED - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_spec._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("spec_id",)) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" +@pytest.mark.asyncio +async def test_create_spec_async_from_dict(): + await test_create_spec_async(request_type=dict) + +def test_create_spec_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = common_fields.Spec() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result - - response_value = Response() - response_value.status_code = 200 + # 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 = apihub_service.CreateSpecRequest() - # Convert return value to protobuf type - return_value = common_fields.Spec.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.parent = "parent_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_spec), "__call__") as call: + call.return_value = common_fields.Spec() + client.create_spec(request) - response = client.create_spec(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] -def test_create_spec_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_create_spec_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.create_spec._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(("specId",)) - & set( - ( - "parent", - "spec", - ) - ) - ) + # 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 = apihub_service.CreateSpecRequest() + request.parent = "parent_value" -def test_create_spec_rest_flattened(): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_spec), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(common_fields.Spec()) + await client.create_spec(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", + "parent=parent_value", + ) in kw["metadata"] + + +def test_create_spec_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Spec() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Spec() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.create_spec( + parent="parent_value", + spec=common_fields.Spec(name="name_value"), + spec_id="spec_id_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].spec + mock_val = common_fields.Spec(name="name_value") + assert arg == mock_val + arg = args[0].spec_id + mock_val = "spec_id_value" + assert arg == mock_val + + +def test_create_spec_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) - # get truthy value for each flattened field - mock_args = dict( + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_spec( + apihub_service.CreateSpecRequest(), parent="parent_value", spec=common_fields.Spec(name="name_value"), spec_id="spec_id_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Spec.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_spec(**mock_args) +@pytest.mark.asyncio +async def test_create_spec_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Spec() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(common_fields.Spec()) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.create_spec( + parent="parent_value", + spec=common_fields.Spec(name="name_value"), + spec_id="spec_id_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*/apis/*/versions/*}/specs" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].spec + mock_val = common_fields.Spec(name="name_value") + assert arg == mock_val + arg = args[0].spec_id + mock_val = "spec_id_value" + assert arg == mock_val -def test_create_spec_rest_flattened_error(transport: str = "rest"): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_create_spec_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.create_spec( + await client.create_spec( apihub_service.CreateSpecRequest(), parent="parent_value", spec=common_fields.Spec(name="name_value"), @@ -3071,13 +5179,83 @@ def test_create_spec_rest_flattened_error(transport: str = "rest"): ) -def test_get_spec_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.GetSpecRequest, + dict, + ], +) +def test_get_spec(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.get_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Spec( + name="name_value", + display_name="display_name_value", + source_uri="source_uri_value", + parsing_mode=common_fields.Spec.ParsingMode.RELAXED, + ) + response = client.get_spec(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.GetSpecRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Spec) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.source_uri == "source_uri_value" + assert response.parsing_mode == common_fields.Spec.ParsingMode.RELAXED + + +def test_get_spec_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.GetSpecRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_spec), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_spec(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.GetSpecRequest( + name="name_value", + ) + + +def test_get_spec_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -3093,7 +5271,6 @@ def test_get_spec_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.get_spec] = mock_rpc - request = {} client.get_spec(request) @@ -3107,155 +5284,307 @@ def test_get_spec_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_get_spec_rest_required_fields(request_type=apihub_service.GetSpecRequest): - transport_class = transports.ApiHubRestTransport +@pytest.mark.asyncio +async def test_get_spec_async_use_cached_wrapped_rpc(transport: str = "grpc_asyncio"): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.get_spec + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_spec._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.get_spec + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.get_spec(request) - jsonified_request["name"] = "name_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_spec._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + await client.get_spec(request) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + +@pytest.mark.asyncio +async def test_get_spec_async( + transport: str = "grpc_asyncio", request_type=apihub_service.GetSpecRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = common_fields.Spec() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # 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() - response_value = Response() - response_value.status_code = 200 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Spec( + name="name_value", + display_name="display_name_value", + source_uri="source_uri_value", + parsing_mode=common_fields.Spec.ParsingMode.RELAXED, + ) + ) + response = await client.get_spec(request) - # Convert return value to protobuf type - return_value = common_fields.Spec.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.GetSpecRequest() + assert args[0] == request - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Spec) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.source_uri == "source_uri_value" + assert response.parsing_mode == common_fields.Spec.ParsingMode.RELAXED - response = client.get_spec(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_get_spec_async_from_dict(): + await test_get_spec_async(request_type=dict) -def test_get_spec_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +def test_get_spec_field_headers(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = transport.get_spec._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # 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 = apihub_service.GetSpecRequest() + request.name = "name_value" -def test_get_spec_rest_flattened(): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_spec), "__call__") as call: + call.return_value = common_fields.Spec() + client.get_spec(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", + "name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_get_spec_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = apihub_service.GetSpecRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_spec), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(common_fields.Spec()) + await client.get_spec(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", + "name=name_value", + ) in kw["metadata"] + + +def test_get_spec_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Spec() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Spec() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_spec( + name="name_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_get_spec_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_spec( + apihub_service.GetSpecRequest(), name="name_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Spec.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_spec(**mock_args) +@pytest.mark.asyncio +async def test_get_spec_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Spec() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(common_fields.Spec()) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_spec( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/apis/*/versions/*/specs/*}" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_get_spec_rest_flattened_error(transport: str = "rest"): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_get_spec_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.get_spec( + await client.get_spec( apihub_service.GetSpecRequest(), name="name_value", ) -def test_get_spec_contents_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.GetSpecContentsRequest, + dict, + ], +) +def test_get_spec_contents(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.get_spec_contents), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.SpecContents( + contents=b"contents_blob", + mime_type="mime_type_value", + ) + response = client.get_spec_contents(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.GetSpecContentsRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.SpecContents) + assert response.contents == b"contents_blob" + assert response.mime_type == "mime_type_value" + + +def test_get_spec_contents_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.GetSpecContentsRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_spec_contents), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_spec_contents(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.GetSpecContentsRequest( + name="name_value", + ) + + +def test_get_spec_contents_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -3273,7 +5602,6 @@ def test_get_spec_contents_rest_use_cached_wrapped_rpc(): client._transport._wrapped_methods[ client._transport.get_spec_contents ] = mock_rpc - request = {} client.get_spec_contents(request) @@ -3287,157 +5615,317 @@ def test_get_spec_contents_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_get_spec_contents_rest_required_fields( - request_type=apihub_service.GetSpecContentsRequest, +@pytest.mark.asyncio +async def test_get_spec_contents_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.get_spec_contents + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.get_spec_contents + ] = mock_rpc + + request = {} + await client.get_spec_contents(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.get_spec_contents(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_get_spec_contents_async( + transport: str = "grpc_asyncio", request_type=apihub_service.GetSpecContentsRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # verify fields with default values are dropped + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_spec_contents._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_spec_contents), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.SpecContents( + contents=b"contents_blob", + mime_type="mime_type_value", + ) + ) + response = await client.get_spec_contents(request) - # verify required fields with default values are now present + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.GetSpecContentsRequest() + assert args[0] == request - jsonified_request["name"] = "name_value" + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.SpecContents) + assert response.contents == b"contents_blob" + assert response.mime_type == "mime_type_value" - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_spec_contents._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" +@pytest.mark.asyncio +async def test_get_spec_contents_async_from_dict(): + await test_get_spec_contents_async(request_type=dict) + +def test_get_spec_contents_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = common_fields.SpecContents() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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 = apihub_service.GetSpecContentsRequest() - # Convert return value to protobuf type - return_value = common_fields.SpecContents.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.name = "name_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_spec_contents), "__call__" + ) as call: + call.return_value = common_fields.SpecContents() + client.get_spec_contents(request) - response = client.get_spec_contents(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] -def test_get_spec_contents_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_get_spec_contents_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.get_spec_contents._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # 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 = apihub_service.GetSpecContentsRequest() + request.name = "name_value" -def test_get_spec_contents_rest_flattened(): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_spec_contents), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.SpecContents() + ) + await client.get_spec_contents(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", + "name=name_value", + ) in kw["metadata"] + + +def test_get_spec_contents_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.SpecContents() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_spec_contents), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.SpecContents() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_spec_contents( + name="name_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_get_spec_contents_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_spec_contents( + apihub_service.GetSpecContentsRequest(), name="name_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.SpecContents.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_spec_contents(**mock_args) +@pytest.mark.asyncio +async def test_get_spec_contents_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_spec_contents), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.SpecContents() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.SpecContents() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_spec_contents( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/apis/*/versions/*/specs/*}:contents" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_get_spec_contents_rest_flattened_error(transport: str = "rest"): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_get_spec_contents_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.get_spec_contents( + await client.get_spec_contents( apihub_service.GetSpecContentsRequest(), name="name_value", ) -def test_list_specs_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.ListSpecsRequest, + dict, + ], +) +def test_list_specs(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.list_specs), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListSpecsResponse( + next_page_token="next_page_token_value", + ) + response = client.list_specs(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.ListSpecsRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListSpecsPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_specs_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.ListSpecsRequest( + parent="parent_value", + filter="filter_value", + page_token="page_token_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_specs), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.list_specs(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.ListSpecsRequest( + parent="parent_value", + filter="filter_value", + page_token="page_token_value", + ) + + +def test_list_specs_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -3453,7 +5941,6 @@ def test_list_specs_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.list_specs] = mock_rpc - request = {} client.list_specs(request) @@ -3467,154 +5954,173 @@ def test_list_specs_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_list_specs_rest_required_fields(request_type=apihub_service.ListSpecsRequest): - transport_class = transports.ApiHubRestTransport +@pytest.mark.asyncio +async def test_list_specs_async_use_cached_wrapped_rpc(transport: str = "grpc_asyncio"): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.list_specs + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_specs._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.list_specs + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.list_specs(request) - jsonified_request["parent"] = "parent_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_specs._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "filter", - "page_size", - "page_token", - ) + await client.list_specs(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_list_specs_async( + transport: str = "grpc_asyncio", request_type=apihub_service.ListSpecsRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + # 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.list_specs), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListSpecsResponse( + next_page_token="next_page_token_value", + ) + ) + response = await client.list_specs(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.ListSpecsRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListSpecsAsyncPager) + assert response.next_page_token == "next_page_token_value" + + +@pytest.mark.asyncio +async def test_list_specs_async_from_dict(): + await test_list_specs_async(request_type=dict) + + +def test_list_specs_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = apihub_service.ListSpecsResponse() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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 = apihub_service.ListSpecsRequest() - # Convert return value to protobuf type - return_value = apihub_service.ListSpecsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.parent = "parent_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_specs), "__call__") as call: + call.return_value = apihub_service.ListSpecsResponse() + client.list_specs(request) - response = client.list_specs(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] -def test_list_specs_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_list_specs_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.list_specs._get_unset_required_fields({}) - assert set(unset_fields) == ( - set( - ( - "filter", - "pageSize", - "pageToken", - ) + # 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 = apihub_service.ListSpecsRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_specs), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListSpecsResponse() ) - & set(("parent",)) - ) + await client.list_specs(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request -def test_list_specs_rest_flattened(): + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +def test_list_specs_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = apihub_service.ListSpecsResponse() - - # get arguments that satisfy an http rule for this method - sample_request = { - "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" - } - - # get truthy value for each flattened field - mock_args = dict( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_specs), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListSpecsResponse() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.list_specs( parent="parent_value", ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = apihub_service.ListSpecsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - - client.list_specs(**mock_args) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*/apis/*/versions/*}/specs" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val -def test_list_specs_rest_flattened_error(transport: str = "rest"): +def test_list_specs_flattened_error(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened @@ -3626,18 +6132,60 @@ def test_list_specs_rest_flattened_error(transport: str = "rest"): ) -def test_list_specs_rest_pager(transport: str = "rest"): +@pytest.mark.asyncio +async def test_list_specs_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_specs), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListSpecsResponse() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListSpecsResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.list_specs( + parent="parent_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_list_specs_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.list_specs( + apihub_service.ListSpecsRequest(), + parent="parent_value", + ) + + +def test_list_specs_pager(transport_name: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport=transport_name, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # TODO(kbandes): remove this mock unless there's a good reason for it. - # with mock.patch.object(path_template, 'transcode') as transcode: - # Set the response as a series of pages - response = ( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_specs), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( apihub_service.ListSpecsResponse( specs=[ common_fields.Spec(), @@ -3662,40 +6210,239 @@ def test_list_specs_rest_pager(transport: str = "rest"): common_fields.Spec(), ], ), + RuntimeError, ) - # Two responses for two calls - response = response + response - - # Wrap the values into proper Response objs - response = tuple(apihub_service.ListSpecsResponse.to_json(x) for x in response) - return_values = tuple(Response() for i in response) - for return_val, response_val in zip(return_values, response): - return_val._content = response_val.encode("UTF-8") - return_val.status_code = 200 - req.side_effect = return_values - sample_request = { - "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" - } + expected_metadata = () + retry = retries.Retry() + timeout = 5 + expected_metadata = tuple(expected_metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", ""),)), + ) + pager = client.list_specs(request={}, retry=retry, timeout=timeout) - pager = client.list_specs(request=sample_request) + assert pager._metadata == expected_metadata + assert pager._retry == retry + assert pager._timeout == timeout results = list(pager) assert len(results) == 6 assert all(isinstance(i, common_fields.Spec) for i in results) - pages = list(client.list_specs(request=sample_request).pages) + +def test_list_specs_pages(transport_name: str = "grpc"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport_name, + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_specs), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListSpecsResponse( + specs=[ + common_fields.Spec(), + common_fields.Spec(), + common_fields.Spec(), + ], + next_page_token="abc", + ), + apihub_service.ListSpecsResponse( + specs=[], + next_page_token="def", + ), + apihub_service.ListSpecsResponse( + specs=[ + common_fields.Spec(), + ], + next_page_token="ghi", + ), + apihub_service.ListSpecsResponse( + specs=[ + common_fields.Spec(), + common_fields.Spec(), + ], + ), + RuntimeError, + ) + pages = list(client.list_specs(request={}).pages) for page_, token in zip(pages, ["abc", "def", "ghi", ""]): assert page_.raw_page.next_page_token == token -def test_update_spec_rest_use_cached_wrapped_rpc(): +@pytest.mark.asyncio +async def test_list_specs_async_pager(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_specs), "__call__", new_callable=mock.AsyncMock + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListSpecsResponse( + specs=[ + common_fields.Spec(), + common_fields.Spec(), + common_fields.Spec(), + ], + next_page_token="abc", + ), + apihub_service.ListSpecsResponse( + specs=[], + next_page_token="def", + ), + apihub_service.ListSpecsResponse( + specs=[ + common_fields.Spec(), + ], + next_page_token="ghi", + ), + apihub_service.ListSpecsResponse( + specs=[ + common_fields.Spec(), + common_fields.Spec(), + ], + ), + RuntimeError, + ) + async_pager = await client.list_specs( + request={}, + ) + assert async_pager.next_page_token == "abc" + responses = [] + async for response in async_pager: # pragma: no branch + responses.append(response) + + assert len(responses) == 6 + assert all(isinstance(i, common_fields.Spec) for i in responses) + + +@pytest.mark.asyncio +async def test_list_specs_async_pages(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_specs), "__call__", new_callable=mock.AsyncMock + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListSpecsResponse( + specs=[ + common_fields.Spec(), + common_fields.Spec(), + common_fields.Spec(), + ], + next_page_token="abc", + ), + apihub_service.ListSpecsResponse( + specs=[], + next_page_token="def", + ), + apihub_service.ListSpecsResponse( + specs=[ + common_fields.Spec(), + ], + next_page_token="ghi", + ), + apihub_service.ListSpecsResponse( + specs=[ + common_fields.Spec(), + common_fields.Spec(), + ], + ), + RuntimeError, + ) + pages = [] + # Workaround issue in python 3.9 related to code coverage by adding `# pragma: no branch` + # See https://github.com/googleapis/gapic-generator-python/pull/1174#issuecomment-1025132372 + async for page_ in ( # pragma: no branch + await client.list_specs(request={}) + ).pages: + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.UpdateSpecRequest, + dict, + ], +) +def test_update_spec(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.update_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Spec( + name="name_value", + display_name="display_name_value", + source_uri="source_uri_value", + parsing_mode=common_fields.Spec.ParsingMode.RELAXED, + ) + response = client.update_spec(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.UpdateSpecRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Spec) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.source_uri == "source_uri_value" + assert response.parsing_mode == common_fields.Spec.ParsingMode.RELAXED + + +def test_update_spec_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.UpdateSpecRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_spec), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.update_spec(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.UpdateSpecRequest() + + +def test_update_spec_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -3711,7 +6458,6 @@ def test_update_spec_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.update_spec] = mock_rpc - request = {} client.update_spec(request) @@ -3725,167 +6471,310 @@ def test_update_spec_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_update_spec_rest_required_fields( - request_type=apihub_service.UpdateSpecRequest, +@pytest.mark.asyncio +async def test_update_spec_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport - - request_init = {} - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - # verify fields with default values are dropped + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_spec._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Ensure method has been cached + assert ( + client._client._transport.update_spec + in client._client._transport._wrapped_methods + ) - # verify required fields with default values are now present + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.update_spec + ] = mock_rpc - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_spec._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("update_mask",)) - jsonified_request.update(unset_fields) + request = {} + await client.update_spec(request) - # verify required fields with non-default values are left alone + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type(**request_init) + await client.update_spec(request) - # Designate an appropriate value for the returned response. - return_value = common_fields.Spec() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "patch", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Spec.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) +@pytest.mark.asyncio +async def test_update_spec_async( + transport: str = "grpc_asyncio", request_type=apihub_service.UpdateSpecRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # 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() - response = client.update_spec(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Spec( + name="name_value", + display_name="display_name_value", + source_uri="source_uri_value", + parsing_mode=common_fields.Spec.ParsingMode.RELAXED, + ) + ) + response = await client.update_spec(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.UpdateSpecRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Spec) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.source_uri == "source_uri_value" + assert response.parsing_mode == common_fields.Spec.ParsingMode.RELAXED -def test_update_spec_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_update_spec_async_from_dict(): + await test_update_spec_async(request_type=dict) + + +def test_update_spec_field_headers(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = transport.update_spec._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(("updateMask",)) - & set( - ( - "spec", - "updateMask", - ) - ) + # 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 = apihub_service.UpdateSpecRequest() + + request.spec.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_spec), "__call__") as call: + call.return_value = common_fields.Spec() + client.update_spec(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", + "spec.name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_update_spec_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) + # 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 = apihub_service.UpdateSpecRequest() -def test_update_spec_rest_flattened(): + request.spec.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_spec), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(common_fields.Spec()) + await client.update_spec(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", + "spec.name=name_value", + ) in kw["metadata"] + + +def test_update_spec_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Spec() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Spec() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.update_spec( + spec=common_fields.Spec(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "spec": { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" - } - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].spec + mock_val = common_fields.Spec(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_update_spec_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_spec( + apihub_service.UpdateSpecRequest(), spec=common_fields.Spec(name="name_value"), update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Spec.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.update_spec(**mock_args) +@pytest.mark.asyncio +async def test_update_spec_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Spec() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(common_fields.Spec()) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.update_spec( + spec=common_fields.Spec(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{spec.name=projects/*/locations/*/apis/*/versions/*/specs/*}" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].spec + mock_val = common_fields.Spec(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val -def test_update_spec_rest_flattened_error(transport: str = "rest"): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_update_spec_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.update_spec( + await client.update_spec( apihub_service.UpdateSpecRequest(), spec=common_fields.Spec(name="name_value"), update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) -def test_delete_spec_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.DeleteSpecRequest, + dict, + ], +) +def test_delete_spec(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.delete_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.delete_spec(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.DeleteSpecRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert response is None + + +def test_delete_spec_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.DeleteSpecRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_spec), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.delete_spec(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.DeleteSpecRequest( + name="name_value", + ) + + +def test_delete_spec_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -3901,7 +6790,6 @@ def test_delete_spec_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.delete_spec] = mock_rpc - request = {} client.delete_spec(request) @@ -3915,152 +6803,300 @@ def test_delete_spec_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_delete_spec_rest_required_fields( - request_type=apihub_service.DeleteSpecRequest, +@pytest.mark.asyncio +async def test_delete_spec_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.delete_spec + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.delete_spec + ] = mock_rpc + + request = {} + await client.delete_spec(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.delete_spec(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_delete_spec_async( + transport: str = "grpc_asyncio", request_type=apihub_service.DeleteSpecRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # verify fields with default values are dropped + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_spec._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_spec(request) - # verify required fields with default values are now present + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.DeleteSpecRequest() + assert args[0] == request - jsonified_request["name"] = "name_value" + # Establish that the response is the type that we expect. + assert response is None - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_spec._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" +@pytest.mark.asyncio +async def test_delete_spec_async_from_dict(): + await test_delete_spec_async(request_type=dict) + +def test_delete_spec_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = None - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "delete", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # 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 = apihub_service.DeleteSpecRequest() - response_value = Response() - response_value.status_code = 200 - json_return_value = "" + request.name = "name_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_spec), "__call__") as call: + call.return_value = None + client.delete_spec(request) - response = client.delete_spec(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] -def test_delete_spec_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_delete_spec_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.delete_spec._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # 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 = apihub_service.DeleteSpecRequest() + request.name = "name_value" -def test_delete_spec_rest_flattened(): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_spec), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_spec(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", + "name=name_value", + ) in kw["metadata"] + + +def test_delete_spec_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = None + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.delete_spec( + name="name_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_delete_spec_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_spec( + apihub_service.DeleteSpecRequest(), name="name_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - json_return_value = "" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_spec(**mock_args) +@pytest.mark.asyncio +async def test_delete_spec_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.delete_spec( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/apis/*/versions/*/specs/*}" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_delete_spec_rest_flattened_error(transport: str = "rest"): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_delete_spec_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.delete_spec( + await client.delete_spec( apihub_service.DeleteSpecRequest(), name="name_value", ) -def test_create_api_operation_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.CreateApiOperationRequest, + dict, + ], +) +def test_create_api_operation(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.create_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ApiOperation( + name="name_value", + spec="spec_value", + ) + response = client.create_api_operation(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.CreateApiOperationRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.ApiOperation) + assert response.name == "name_value" + assert response.spec == "spec_value" + + +def test_create_api_operation_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.CreateApiOperationRequest( + parent="parent_value", + api_operation_id="api_operation_id_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_api_operation), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.create_api_operation(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.CreateApiOperationRequest( + parent="parent_value", + api_operation_id="api_operation_id_value", + ) + + +def test_create_api_operation_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -4080,7 +7116,6 @@ def test_create_api_operation_rest_use_cached_wrapped_rpc(): client._transport._wrapped_methods[ client._transport.create_api_operation ] = mock_rpc - request = {} client.create_api_operation(request) @@ -4094,158 +7129,256 @@ def test_create_api_operation_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_create_api_operation_rest_required_fields( - request_type=apihub_service.CreateApiOperationRequest, +@pytest.mark.asyncio +async def test_create_api_operation_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.create_api_operation + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_api_operation._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.create_api_operation + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.create_api_operation(request) - jsonified_request["parent"] = "parent_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_api_operation._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("api_operation_id",)) - jsonified_request.update(unset_fields) + await client.create_api_operation(request) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + +@pytest.mark.asyncio +async def test_create_api_operation_async( + transport: str = "grpc_asyncio", + request_type=apihub_service.CreateApiOperationRequest, +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = common_fields.ApiOperation() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # 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() - response_value = Response() - response_value.status_code = 200 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ApiOperation( + name="name_value", + spec="spec_value", + ) + ) + response = await client.create_api_operation(request) - # Convert return value to protobuf type - return_value = common_fields.ApiOperation.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.CreateApiOperationRequest() + assert args[0] == request - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.ApiOperation) + assert response.name == "name_value" + assert response.spec == "spec_value" - response = client.create_api_operation(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_create_api_operation_async_from_dict(): + await test_create_api_operation_async(request_type=dict) -def test_create_api_operation_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +def test_create_api_operation_field_headers(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = transport.create_api_operation._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(("apiOperationId",)) - & set( - ( - "parent", - "apiOperation", - ) - ) + # 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 = apihub_service.CreateApiOperationRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_api_operation), "__call__" + ) as call: + call.return_value = common_fields.ApiOperation() + client.create_api_operation(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", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_create_api_operation_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) + # 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 = apihub_service.CreateApiOperationRequest() -def test_create_api_operation_rest_flattened(): + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_api_operation), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ApiOperation() + ) + await client.create_api_operation(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", + "parent=parent_value", + ) in kw["metadata"] + + +def test_create_api_operation_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.ApiOperation() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ApiOperation() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.create_api_operation( + parent="parent_value", + api_operation=common_fields.ApiOperation(name="name_value"), + api_operation_id="api_operation_id_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].api_operation + mock_val = common_fields.ApiOperation(name="name_value") + assert arg == mock_val + arg = args[0].api_operation_id + mock_val = "api_operation_id_value" + assert arg == mock_val + + +def test_create_api_operation_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) - # get truthy value for each flattened field - mock_args = dict( + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_api_operation( + apihub_service.CreateApiOperationRequest(), parent="parent_value", api_operation=common_fields.ApiOperation(name="name_value"), api_operation_id="api_operation_id_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.ApiOperation.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_api_operation(**mock_args) +@pytest.mark.asyncio +async def test_create_api_operation_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ApiOperation() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ApiOperation() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.create_api_operation( + parent="parent_value", + api_operation=common_fields.ApiOperation(name="name_value"), + api_operation_id="api_operation_id_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*/apis/*/versions/*}/operations" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].api_operation + mock_val = common_fields.ApiOperation(name="name_value") + assert arg == mock_val + arg = args[0].api_operation_id + mock_val = "api_operation_id_value" + assert arg == mock_val -def test_create_api_operation_rest_flattened_error(transport: str = "rest"): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_create_api_operation_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.create_api_operation( + await client.create_api_operation( apihub_service.CreateApiOperationRequest(), parent="parent_value", api_operation=common_fields.ApiOperation(name="name_value"), @@ -4253,13 +7386,83 @@ def test_create_api_operation_rest_flattened_error(transport: str = "rest"): ) -def test_get_api_operation_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.GetApiOperationRequest, + dict, + ], +) +def test_get_api_operation(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.get_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ApiOperation( + name="name_value", + spec="spec_value", + ) + response = client.get_api_operation(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.GetApiOperationRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.ApiOperation) + assert response.name == "name_value" + assert response.spec == "spec_value" + + +def test_get_api_operation_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.GetApiOperationRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_api_operation), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_api_operation(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.GetApiOperationRequest( + name="name_value", + ) + + +def test_get_api_operation_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -4277,7 +7480,6 @@ def test_get_api_operation_rest_use_cached_wrapped_rpc(): client._transport._wrapped_methods[ client._transport.get_api_operation ] = mock_rpc - request = {} client.get_api_operation(request) @@ -4291,139 +7493,185 @@ def test_get_api_operation_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_get_api_operation_rest_required_fields( - request_type=apihub_service.GetApiOperationRequest, +@pytest.mark.asyncio +async def test_get_api_operation_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport - - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) - - # verify fields with default values are dropped + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_api_operation._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify required fields with default values are now present + # Ensure method has been cached + assert ( + client._client._transport.get_api_operation + in client._client._transport._wrapped_methods + ) - jsonified_request["name"] = "name_value" + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.get_api_operation + ] = mock_rpc - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_api_operation._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + request = {} + await client.get_api_operation(request) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type(**request_init) + await client.get_api_operation(request) - # Designate an appropriate value for the returned response. - return_value = common_fields.ApiOperation() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.ApiOperation.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) +@pytest.mark.asyncio +async def test_get_api_operation_async( + transport: str = "grpc_asyncio", request_type=apihub_service.GetApiOperationRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # 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() - response = client.get_api_operation(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ApiOperation( + name="name_value", + spec="spec_value", + ) + ) + response = await client.get_api_operation(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.GetApiOperationRequest() + assert args[0] == request + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.ApiOperation) + assert response.name == "name_value" + assert response.spec == "spec_value" -def test_get_api_operation_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials - ) - unset_fields = transport.get_api_operation._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) +@pytest.mark.asyncio +async def test_get_api_operation_async_from_dict(): + await test_get_api_operation_async(request_type=dict) -def test_get_api_operation_rest_flattened(): +def test_get_api_operation_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.ApiOperation() + # 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 = apihub_service.GetApiOperationRequest() - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/operations/sample5" - } + request.name = "name_value" - # get truthy value for each flattened field - mock_args = dict( - name="name_value", + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_api_operation), "__call__" + ) as call: + call.return_value = common_fields.ApiOperation() + client.get_api_operation(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", + "name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_get_api_operation_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = apihub_service.GetApiOperationRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_api_operation), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ApiOperation() ) - mock_args.update(sample_request) + await client.get_api_operation(request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.ApiOperation.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request - client.get_api_operation(**mock_args) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] + + +def test_get_api_operation_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ApiOperation() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_api_operation( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/apis/*/versions/*/operations/*}" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_get_api_operation_rest_flattened_error(transport: str = "rest"): +def test_get_api_operation_flattened_error(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened @@ -4435,13 +7683,131 @@ def test_get_api_operation_rest_flattened_error(transport: str = "rest"): ) -def test_list_api_operations_rest_use_cached_wrapped_rpc(): +@pytest.mark.asyncio +async def test_get_api_operation_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ApiOperation() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ApiOperation() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_api_operation( + name="name_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_get_api_operation_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.get_api_operation( + apihub_service.GetApiOperationRequest(), + name="name_value", + ) + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.ListApiOperationsRequest, + dict, + ], +) +def test_list_api_operations(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.list_api_operations), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListApiOperationsResponse( + next_page_token="next_page_token_value", + ) + response = client.list_api_operations(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.ListApiOperationsRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListApiOperationsPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_api_operations_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.ListApiOperationsRequest( + parent="parent_value", + filter="filter_value", + page_token="page_token_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_api_operations), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.list_api_operations(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.ListApiOperationsRequest( + parent="parent_value", + filter="filter_value", + page_token="page_token_value", + ) + + +def test_list_api_operations_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -4461,7 +7827,6 @@ def test_list_api_operations_rest_use_cached_wrapped_rpc(): client._transport._wrapped_methods[ client._transport.list_api_operations ] = mock_rpc - request = {} client.list_api_operations(request) @@ -4475,156 +7840,184 @@ def test_list_api_operations_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_list_api_operations_rest_required_fields( - request_type=apihub_service.ListApiOperationsRequest, +@pytest.mark.asyncio +async def test_list_api_operations_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport - - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) - - # verify fields with default values are dropped + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_api_operations._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify required fields with default values are now present + # Ensure method has been cached + assert ( + client._client._transport.list_api_operations + in client._client._transport._wrapped_methods + ) - jsonified_request["parent"] = "parent_value" + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.list_api_operations + ] = mock_rpc - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_api_operations._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "filter", - "page_size", - "page_token", - ) - ) - jsonified_request.update(unset_fields) + request = {} + await client.list_api_operations(request) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type(**request_init) + await client.list_api_operations(request) - # Designate an appropriate value for the returned response. - return_value = apihub_service.ListApiOperationsResponse() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = apihub_service.ListApiOperationsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) +@pytest.mark.asyncio +async def test_list_api_operations_async( + transport: str = "grpc_asyncio", + request_type=apihub_service.ListApiOperationsRequest, +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # 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() - response = client.list_api_operations(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_api_operations), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListApiOperationsResponse( + next_page_token="next_page_token_value", + ) + ) + response = await client.list_api_operations(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.ListApiOperationsRequest() + assert args[0] == request + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListApiOperationsAsyncPager) + assert response.next_page_token == "next_page_token_value" -def test_list_api_operations_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials - ) - unset_fields = transport.list_api_operations._get_unset_required_fields({}) - assert set(unset_fields) == ( - set( - ( - "filter", - "pageSize", - "pageToken", - ) - ) - & set(("parent",)) - ) +@pytest.mark.asyncio +async def test_list_api_operations_async_from_dict(): + await test_list_api_operations_async(request_type=dict) -def test_list_api_operations_rest_flattened(): +def test_list_api_operations_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = apihub_service.ListApiOperationsResponse() + # 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 = apihub_service.ListApiOperationsRequest() - # get arguments that satisfy an http rule for this method - sample_request = { - "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" - } + request.parent = "parent_value" - # get truthy value for each flattened field - mock_args = dict( - parent="parent_value", + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_api_operations), "__call__" + ) as call: + call.return_value = apihub_service.ListApiOperationsResponse() + client.list_api_operations(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", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_list_api_operations_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = apihub_service.ListApiOperationsRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_api_operations), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListApiOperationsResponse() ) - mock_args.update(sample_request) + await client.list_api_operations(request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = apihub_service.ListApiOperationsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request - client.list_api_operations(**mock_args) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +def test_list_api_operations_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_api_operations), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListApiOperationsResponse() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.list_api_operations( + parent="parent_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*/apis/*/versions/*}/operations" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val -def test_list_api_operations_rest_flattened_error(transport: str = "rest"): +def test_list_api_operations_flattened_error(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened @@ -4636,18 +8029,64 @@ def test_list_api_operations_rest_flattened_error(transport: str = "rest"): ) -def test_list_api_operations_rest_pager(transport: str = "rest"): +@pytest.mark.asyncio +async def test_list_api_operations_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_api_operations), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListApiOperationsResponse() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListApiOperationsResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.list_api_operations( + parent="parent_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_list_api_operations_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.list_api_operations( + apihub_service.ListApiOperationsRequest(), + parent="parent_value", + ) + + +def test_list_api_operations_pager(transport_name: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport=transport_name, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # TODO(kbandes): remove this mock unless there's a good reason for it. - # with mock.patch.object(path_template, 'transcode') as transcode: - # Set the response as a series of pages - response = ( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_api_operations), "__call__" + ) as call: + # Set the response to a series of pages. + call.side_effect = ( apihub_service.ListApiOperationsResponse( api_operations=[ common_fields.ApiOperation(), @@ -4672,236 +8111,245 @@ def test_list_api_operations_rest_pager(transport: str = "rest"): common_fields.ApiOperation(), ], ), + RuntimeError, ) - # Two responses for two calls - response = response + response - # Wrap the values into proper Response objs - response = tuple( - apihub_service.ListApiOperationsResponse.to_json(x) for x in response + expected_metadata = () + retry = retries.Retry() + timeout = 5 + expected_metadata = tuple(expected_metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", ""),)), ) - return_values = tuple(Response() for i in response) - for return_val, response_val in zip(return_values, response): - return_val._content = response_val.encode("UTF-8") - return_val.status_code = 200 - req.side_effect = return_values - - sample_request = { - "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" - } + pager = client.list_api_operations(request={}, retry=retry, timeout=timeout) - pager = client.list_api_operations(request=sample_request) + assert pager._metadata == expected_metadata + assert pager._retry == retry + assert pager._timeout == timeout results = list(pager) assert len(results) == 6 assert all(isinstance(i, common_fields.ApiOperation) for i in results) - pages = list(client.list_api_operations(request=sample_request).pages) - for page_, token in zip(pages, ["abc", "def", "ghi", ""]): - assert page_.raw_page.next_page_token == token +def test_list_api_operations_pages(transport_name: str = "grpc"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport_name, + ) -def test_update_api_operation_rest_use_cached_wrapped_rpc(): - # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, - # instead of constructing them on each call - with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_api_operations), "__call__" + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListApiOperationsResponse( + api_operations=[ + common_fields.ApiOperation(), + common_fields.ApiOperation(), + common_fields.ApiOperation(), + ], + next_page_token="abc", + ), + apihub_service.ListApiOperationsResponse( + api_operations=[], + next_page_token="def", + ), + apihub_service.ListApiOperationsResponse( + api_operations=[ + common_fields.ApiOperation(), + ], + next_page_token="ghi", + ), + apihub_service.ListApiOperationsResponse( + api_operations=[ + common_fields.ApiOperation(), + common_fields.ApiOperation(), + ], + ), + RuntimeError, ) + pages = list(client.list_api_operations(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token - # Should wrap all calls on client creation - assert wrapper_fn.call_count > 0 - wrapper_fn.reset_mock() - # Ensure method has been cached - assert ( - client._transport.update_api_operation in client._transport._wrapped_methods - ) +@pytest.mark.asyncio +async def test_list_api_operations_async_pager(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) - # Replace cached wrapped function with mock - mock_rpc = mock.Mock() - mock_rpc.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_api_operations), + "__call__", + new_callable=mock.AsyncMock, + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListApiOperationsResponse( + api_operations=[ + common_fields.ApiOperation(), + common_fields.ApiOperation(), + common_fields.ApiOperation(), + ], + next_page_token="abc", + ), + apihub_service.ListApiOperationsResponse( + api_operations=[], + next_page_token="def", + ), + apihub_service.ListApiOperationsResponse( + api_operations=[ + common_fields.ApiOperation(), + ], + next_page_token="ghi", + ), + apihub_service.ListApiOperationsResponse( + api_operations=[ + common_fields.ApiOperation(), + common_fields.ApiOperation(), + ], + ), + RuntimeError, ) - client._transport._wrapped_methods[ - client._transport.update_api_operation - ] = mock_rpc - - request = {} - client.update_api_operation(request) - - # Establish that the underlying gRPC stub method was called. - assert mock_rpc.call_count == 1 - - client.update_api_operation(request) - - # Establish that a new wrapper was not created for this call - assert wrapper_fn.call_count == 0 - assert mock_rpc.call_count == 2 + async_pager = await client.list_api_operations( + request={}, + ) + assert async_pager.next_page_token == "abc" + responses = [] + async for response in async_pager: # pragma: no branch + responses.append(response) + assert len(responses) == 6 + assert all(isinstance(i, common_fields.ApiOperation) for i in responses) -def test_update_api_operation_rest_required_fields( - request_type=apihub_service.UpdateApiOperationRequest, -): - transport_class = transports.ApiHubRestTransport - request_init = {} - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) +@pytest.mark.asyncio +async def test_list_api_operations_async_pages(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - # verify fields with default values are dropped + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_api_operations), + "__call__", + new_callable=mock.AsyncMock, + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListApiOperationsResponse( + api_operations=[ + common_fields.ApiOperation(), + common_fields.ApiOperation(), + common_fields.ApiOperation(), + ], + next_page_token="abc", + ), + apihub_service.ListApiOperationsResponse( + api_operations=[], + next_page_token="def", + ), + apihub_service.ListApiOperationsResponse( + api_operations=[ + common_fields.ApiOperation(), + ], + next_page_token="ghi", + ), + apihub_service.ListApiOperationsResponse( + api_operations=[ + common_fields.ApiOperation(), + common_fields.ApiOperation(), + ], + ), + RuntimeError, + ) + pages = [] + # Workaround issue in python 3.9 related to code coverage by adding `# pragma: no branch` + # See https://github.com/googleapis/gapic-generator-python/pull/1174#issuecomment-1025132372 + async for page_ in ( # pragma: no branch + await client.list_api_operations(request={}) + ).pages: + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_api_operation._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - # verify required fields with default values are now present - - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_api_operation._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("update_mask",)) - jsonified_request.update(unset_fields) - - # verify required fields with non-default values are left alone - - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = common_fields.ApiOperation() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "patch", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result - - response_value = Response() - response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = common_fields.ApiOperation.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - - response = client.update_api_operation(request) - - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params - - -def test_update_api_operation_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials - ) - - unset_fields = transport.update_api_operation._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(("updateMask",)) - & set( - ( - "apiOperation", - "updateMask", - ) - ) - ) - - -def test_update_api_operation_rest_flattened(): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.UpdateApiOperationRequest, + dict, + ], +) +def test_update_api_operation(request_type, transport: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.ApiOperation() - - # get arguments that satisfy an http rule for this method - sample_request = { - "api_operation": { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/operations/sample5" - } - } + # 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() - # get truthy value for each flattened field - mock_args = dict( - api_operation=common_fields.ApiOperation(name="name_value"), - update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ApiOperation( + name="name_value", + spec="spec_value", ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.ApiOperation.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.update_api_operation(request) - client.update_api_operation(**mock_args) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.UpdateApiOperationRequest() + assert args[0] == request - # Establish that the underlying call was made with the expected - # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{api_operation.name=projects/*/locations/*/apis/*/versions/*/operations/*}" - % client.transport._host, - args[1], - ) + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.ApiOperation) + assert response.name == "name_value" + assert response.spec == "spec_value" -def test_update_api_operation_rest_flattened_error(transport: str = "rest"): +def test_update_api_operation_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport="grpc", ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.update_api_operation( - apihub_service.UpdateApiOperationRequest(), - api_operation=common_fields.ApiOperation(name="name_value"), - update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.UpdateApiOperationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_api_operation), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. ) + client.update_api_operation(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.UpdateApiOperationRequest() -def test_delete_api_operation_rest_use_cached_wrapped_rpc(): +def test_update_api_operation_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -4910,7 +8358,7 @@ def test_delete_api_operation_rest_use_cached_wrapped_rpc(): # Ensure method has been cached assert ( - client._transport.delete_api_operation in client._transport._wrapped_methods + client._transport.update_api_operation in client._transport._wrapped_methods ) # Replace cached wrapped function with mock @@ -4919,168 +8367,340 @@ def test_delete_api_operation_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[ - client._transport.delete_api_operation + client._transport.update_api_operation ] = mock_rpc - request = {} - client.delete_api_operation(request) + client.update_api_operation(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.delete_api_operation(request) + client.update_api_operation(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_delete_api_operation_rest_required_fields( - request_type=apihub_service.DeleteApiOperationRequest, +@pytest.mark.asyncio +async def test_update_api_operation_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.update_api_operation + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_api_operation._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.update_api_operation + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.update_api_operation(request) - jsonified_request["name"] = "name_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_api_operation._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + await client.update_api_operation(request) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + +@pytest.mark.asyncio +async def test_update_api_operation_async( + transport: str = "grpc_asyncio", + request_type=apihub_service.UpdateApiOperationRequest, +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = None - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "delete", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # 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() - response_value = Response() - response_value.status_code = 200 - json_return_value = "" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ApiOperation( + name="name_value", + spec="spec_value", + ) + ) + response = await client.update_api_operation(request) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.UpdateApiOperationRequest() + assert args[0] == request - response = client.delete_api_operation(request) + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.ApiOperation) + assert response.name == "name_value" + assert response.spec == "spec_value" - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_update_api_operation_async_from_dict(): + await test_update_api_operation_async(request_type=dict) -def test_delete_api_operation_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials + +def test_update_api_operation_field_headers(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = transport.delete_api_operation._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # 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 = apihub_service.UpdateApiOperationRequest() + request.api_operation.name = "name_value" -def test_delete_api_operation_rest_flattened(): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_api_operation), "__call__" + ) as call: + call.return_value = common_fields.ApiOperation() + client.update_api_operation(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", + "api_operation.name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_update_api_operation_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = apihub_service.UpdateApiOperationRequest() + + request.api_operation.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_api_operation), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ApiOperation() + ) + await client.update_api_operation(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", + "api_operation.name=name_value", + ) in kw["metadata"] + + +def test_update_api_operation_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = None + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ApiOperation() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.update_api_operation( + api_operation=common_fields.ApiOperation(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/operations/sample5" - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].api_operation + mock_val = common_fields.ApiOperation(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( - name="name_value", + +def test_update_api_operation_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_api_operation( + apihub_service.UpdateApiOperationRequest(), + api_operation=common_fields.ApiOperation(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - json_return_value = "" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_api_operation(**mock_args) +@pytest.mark.asyncio +async def test_update_api_operation_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ApiOperation() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ApiOperation() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.update_api_operation( + api_operation=common_fields.ApiOperation(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/apis/*/versions/*/operations/*}" - % client.transport._host, - args[1], + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].api_operation + mock_val = common_fields.ApiOperation(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_update_api_operation_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.update_api_operation( + apihub_service.UpdateApiOperationRequest(), + api_operation=common_fields.ApiOperation(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) -def test_delete_api_operation_rest_flattened_error(transport: str = "rest"): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.DeleteApiOperationRequest, + dict, + ], +) +def test_delete_api_operation(request_type, transport: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.delete_api_operation( - apihub_service.DeleteApiOperationRequest(), + # 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.delete_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.delete_api_operation(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.DeleteApiOperationRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert response is None + + +def test_delete_api_operation_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.DeleteApiOperationRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_api_operation), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.delete_api_operation(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.DeleteApiOperationRequest( name="name_value", ) -def test_get_definition_rest_use_cached_wrapped_rpc(): +def test_delete_api_operation_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -5088,179 +8708,332 @@ def test_get_definition_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.get_definition in client._transport._wrapped_methods + assert ( + client._transport.delete_api_operation in client._transport._wrapped_methods + ) # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[client._transport.get_definition] = mock_rpc - + client._transport._wrapped_methods[ + client._transport.delete_api_operation + ] = mock_rpc request = {} - client.get_definition(request) + client.delete_api_operation(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.get_definition(request) + client.delete_api_operation(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_get_definition_rest_required_fields( - request_type=apihub_service.GetDefinitionRequest, +@pytest.mark.asyncio +async def test_delete_api_operation_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport - - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) - - # verify fields with default values are dropped + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_definition._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify required fields with default values are now present + # Ensure method has been cached + assert ( + client._client._transport.delete_api_operation + in client._client._transport._wrapped_methods + ) - jsonified_request["name"] = "name_value" + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.delete_api_operation + ] = mock_rpc - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_definition._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + request = {} + await client.delete_api_operation(request) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type(**request_init) + await client.delete_api_operation(request) - # Designate an appropriate value for the returned response. - return_value = common_fields.Definition() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Definition.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) +@pytest.mark.asyncio +async def test_delete_api_operation_async( + transport: str = "grpc_asyncio", + request_type=apihub_service.DeleteApiOperationRequest, +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # 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() - response = client.get_definition(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_api_operation(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.DeleteApiOperationRequest() + assert args[0] == request + # Establish that the response is the type that we expect. + assert response is None -def test_get_definition_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials - ) - unset_fields = transport.get_definition._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) +@pytest.mark.asyncio +async def test_delete_api_operation_async_from_dict(): + await test_delete_api_operation_async(request_type=dict) -def test_get_definition_rest_flattened(): +def test_delete_api_operation_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Definition() + # 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 = apihub_service.DeleteApiOperationRequest() - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/definitions/sample5" - } + request.name = "name_value" - # get truthy value for each flattened field - mock_args = dict( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_api_operation), "__call__" + ) as call: + call.return_value = None + client.delete_api_operation(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", + "name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_delete_api_operation_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = apihub_service.DeleteApiOperationRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_api_operation), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_api_operation(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", + "name=name_value", + ) in kw["metadata"] + + +def test_delete_api_operation_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = None + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.delete_api_operation( name="name_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Definition.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - client.get_definition(**mock_args) + +def test_delete_api_operation_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_api_operation( + apihub_service.DeleteApiOperationRequest(), + name="name_value", + ) + + +@pytest.mark.asyncio +async def test_delete_api_operation_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = None + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.delete_api_operation( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/apis/*/versions/*/definitions/*}" - % client.transport._host, - args[1], + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_delete_api_operation_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.delete_api_operation( + apihub_service.DeleteApiOperationRequest(), + name="name_value", ) -def test_get_definition_rest_flattened_error(transport: str = "rest"): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.GetDefinitionRequest, + dict, + ], +) +def test_get_definition(request_type, transport: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.get_definition( - apihub_service.GetDefinitionRequest(), + # 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.get_definition), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Definition( name="name_value", + spec="spec_value", + type_=common_fields.Definition.Type.SCHEMA, ) + response = client.get_definition(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.GetDefinitionRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Definition) + assert response.name == "name_value" + assert response.spec == "spec_value" + assert response.type_ == common_fields.Definition.Type.SCHEMA -def test_create_deployment_rest_use_cached_wrapped_rpc(): +def test_get_definition_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.GetDefinitionRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_definition), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_definition(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.GetDefinitionRequest( + name="name_value", + ) + + +def test_get_definition_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -5268,194 +9041,346 @@ def test_create_deployment_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.create_deployment in client._transport._wrapped_methods + assert client._transport.get_definition in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[ - client._transport.create_deployment + client._transport._wrapped_methods[client._transport.get_definition] = mock_rpc + request = {} + client.get_definition(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_definition(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_get_definition_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.get_definition + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.get_definition ] = mock_rpc request = {} - client.create_deployment(request) + await client.get_definition(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.create_deployment(request) + await client.get_definition(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_create_deployment_rest_required_fields( - request_type=apihub_service.CreateDeploymentRequest, +@pytest.mark.asyncio +async def test_get_definition_async( + transport: str = "grpc_asyncio", request_type=apihub_service.GetDefinitionRequest ): - transport_class = transports.ApiHubRestTransport - - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # verify fields with default values are dropped + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_deployment._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_definition), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Definition( + name="name_value", + spec="spec_value", + type_=common_fields.Definition.Type.SCHEMA, + ) + ) + response = await client.get_definition(request) - # verify required fields with default values are now present + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.GetDefinitionRequest() + assert args[0] == request - jsonified_request["parent"] = "parent_value" + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Definition) + assert response.name == "name_value" + assert response.spec == "spec_value" + assert response.type_ == common_fields.Definition.Type.SCHEMA - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_deployment._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("deployment_id",)) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" +@pytest.mark.asyncio +async def test_get_definition_async_from_dict(): + await test_get_definition_async(request_type=dict) + +def test_get_definition_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = common_fields.Deployment() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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 = apihub_service.GetDefinitionRequest() - # Convert return value to protobuf type - return_value = common_fields.Deployment.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.name = "name_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_definition), "__call__") as call: + call.return_value = common_fields.Definition() + client.get_definition(request) - response = client.create_deployment(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] -def test_create_deployment_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_get_definition_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.create_deployment._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(("deploymentId",)) - & set( - ( - "parent", - "deployment", - ) + # 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 = apihub_service.GetDefinitionRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_definition), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Definition() ) - ) + await client.get_definition(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request -def test_create_deployment_rest_flattened(): + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] + + +def test_get_definition_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Deployment() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_definition), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Definition() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_definition( + name="name_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( - parent="parent_value", - deployment=common_fields.Deployment(name="name_value"), - deployment_id="deployment_id_value", + +def test_get_definition_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_definition( + apihub_service.GetDefinitionRequest(), + name="name_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Deployment.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_deployment(**mock_args) +@pytest.mark.asyncio +async def test_get_definition_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_definition), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Definition() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Definition() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_definition( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/deployments" - % client.transport._host, - args[1], + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_get_definition_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.get_definition( + apihub_service.GetDefinitionRequest(), + name="name_value", ) -def test_create_deployment_rest_flattened_error(transport: str = "rest"): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.CreateDeploymentRequest, + dict, + ], +) +def test_create_deployment(request_type, transport: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.create_deployment( - apihub_service.CreateDeploymentRequest(), + # 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.create_deployment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Deployment( + name="name_value", + display_name="display_name_value", + description="description_value", + resource_uri="resource_uri_value", + endpoints=["endpoints_value"], + api_versions=["api_versions_value"], + source_project="source_project_value", + source_environment="source_environment_value", + ) + response = client.create_deployment(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.CreateDeploymentRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Deployment) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.resource_uri == "resource_uri_value" + assert response.endpoints == ["endpoints_value"] + assert response.api_versions == ["api_versions_value"] + assert response.source_project == "source_project_value" + assert response.source_environment == "source_environment_value" + + +def test_create_deployment_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.CreateDeploymentRequest( + parent="parent_value", + deployment_id="deployment_id_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_deployment), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.create_deployment(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.CreateDeploymentRequest( parent="parent_value", - deployment=common_fields.Deployment(name="name_value"), deployment_id="deployment_id_value", ) -def test_get_deployment_rest_use_cached_wrapped_rpc(): +def test_create_deployment_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -5463,179 +9388,382 @@ def test_get_deployment_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.get_deployment in client._transport._wrapped_methods + assert client._transport.create_deployment in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[client._transport.get_deployment] = mock_rpc - + client._transport._wrapped_methods[ + client._transport.create_deployment + ] = mock_rpc request = {} - client.get_deployment(request) + client.create_deployment(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.get_deployment(request) + client.create_deployment(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_get_deployment_rest_required_fields( - request_type=apihub_service.GetDeploymentRequest, +@pytest.mark.asyncio +async def test_create_deployment_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.create_deployment + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_deployment._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.create_deployment + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.create_deployment(request) - jsonified_request["name"] = "name_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_deployment._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + await client.create_deployment(request) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + +@pytest.mark.asyncio +async def test_create_deployment_async( + transport: str = "grpc_asyncio", request_type=apihub_service.CreateDeploymentRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = common_fields.Deployment() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # 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() - response_value = Response() - response_value.status_code = 200 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_deployment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Deployment( + name="name_value", + display_name="display_name_value", + description="description_value", + resource_uri="resource_uri_value", + endpoints=["endpoints_value"], + api_versions=["api_versions_value"], + source_project="source_project_value", + source_environment="source_environment_value", + ) + ) + response = await client.create_deployment(request) - # Convert return value to protobuf type - return_value = common_fields.Deployment.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.CreateDeploymentRequest() + assert args[0] == request - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Deployment) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.resource_uri == "resource_uri_value" + assert response.endpoints == ["endpoints_value"] + assert response.api_versions == ["api_versions_value"] + assert response.source_project == "source_project_value" + assert response.source_environment == "source_environment_value" - response = client.get_deployment(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_create_deployment_async_from_dict(): + await test_create_deployment_async(request_type=dict) -def test_get_deployment_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +def test_create_deployment_field_headers(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = transport.get_deployment._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # 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 = apihub_service.CreateDeploymentRequest() + request.parent = "parent_value" -def test_get_deployment_rest_flattened(): - client = ApiHubClient( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_deployment), "__call__" + ) as call: + call.return_value = common_fields.Deployment() + client.create_deployment(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", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_create_deployment_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = apihub_service.CreateDeploymentRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_deployment), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Deployment() + ) + await client.create_deployment(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", + "parent=parent_value", + ) in kw["metadata"] + + +def test_create_deployment_flattened(): + client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Deployment() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_deployment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Deployment() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.create_deployment( + parent="parent_value", + deployment=common_fields.Deployment(name="name_value"), + deployment_id="deployment_id_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/deployments/sample3" - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].deployment + mock_val = common_fields.Deployment(name="name_value") + assert arg == mock_val + arg = args[0].deployment_id + mock_val = "deployment_id_value" + assert arg == mock_val + + +def test_create_deployment_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) - # get truthy value for each flattened field - mock_args = dict( - name="name_value", + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_deployment( + apihub_service.CreateDeploymentRequest(), + parent="parent_value", + deployment=common_fields.Deployment(name="name_value"), + deployment_id="deployment_id_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Deployment.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_deployment(**mock_args) +@pytest.mark.asyncio +async def test_create_deployment_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_deployment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Deployment() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Deployment() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.create_deployment( + parent="parent_value", + deployment=common_fields.Deployment(name="name_value"), + deployment_id="deployment_id_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/deployments/*}" - % client.transport._host, - args[1], + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].deployment + mock_val = common_fields.Deployment(name="name_value") + assert arg == mock_val + arg = args[0].deployment_id + mock_val = "deployment_id_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_create_deployment_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.create_deployment( + apihub_service.CreateDeploymentRequest(), + parent="parent_value", + deployment=common_fields.Deployment(name="name_value"), + deployment_id="deployment_id_value", ) -def test_get_deployment_rest_flattened_error(transport: str = "rest"): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.GetDeploymentRequest, + dict, + ], +) +def test_get_deployment(request_type, transport: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.get_deployment( - apihub_service.GetDeploymentRequest(), + # 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.get_deployment), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Deployment( + name="name_value", + display_name="display_name_value", + description="description_value", + resource_uri="resource_uri_value", + endpoints=["endpoints_value"], + api_versions=["api_versions_value"], + source_project="source_project_value", + source_environment="source_environment_value", + ) + response = client.get_deployment(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.GetDeploymentRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Deployment) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.resource_uri == "resource_uri_value" + assert response.endpoints == ["endpoints_value"] + assert response.api_versions == ["api_versions_value"] + assert response.source_project == "source_project_value" + assert response.source_environment == "source_environment_value" + + +def test_get_deployment_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.GetDeploymentRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_deployment), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_deployment(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.GetDeploymentRequest( name="name_value", ) -def test_list_deployments_rest_use_cached_wrapped_rpc(): +def test_get_deployment_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -5643,259 +9771,340 @@ def test_list_deployments_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.list_deployments in client._transport._wrapped_methods + assert client._transport.get_deployment in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[ - client._transport.list_deployments + client._transport._wrapped_methods[client._transport.get_deployment] = mock_rpc + request = {} + client.get_deployment(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_deployment(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_get_deployment_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.get_deployment + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.get_deployment ] = mock_rpc request = {} - client.list_deployments(request) + await client.get_deployment(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.list_deployments(request) + await client.get_deployment(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_list_deployments_rest_required_fields( - request_type=apihub_service.ListDeploymentsRequest, +@pytest.mark.asyncio +async def test_get_deployment_async( + transport: str = "grpc_asyncio", request_type=apihub_service.GetDeploymentRequest ): - transport_class = transports.ApiHubRestTransport - - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # verify fields with default values are dropped + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_deployments._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_deployment), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Deployment( + name="name_value", + display_name="display_name_value", + description="description_value", + resource_uri="resource_uri_value", + endpoints=["endpoints_value"], + api_versions=["api_versions_value"], + source_project="source_project_value", + source_environment="source_environment_value", + ) + ) + response = await client.get_deployment(request) - # verify required fields with default values are now present + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.GetDeploymentRequest() + assert args[0] == request - jsonified_request["parent"] = "parent_value" + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Deployment) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.resource_uri == "resource_uri_value" + assert response.endpoints == ["endpoints_value"] + assert response.api_versions == ["api_versions_value"] + assert response.source_project == "source_project_value" + assert response.source_environment == "source_environment_value" - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_deployments._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "filter", - "page_size", - "page_token", - ) - ) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" +@pytest.mark.asyncio +async def test_get_deployment_async_from_dict(): + await test_get_deployment_async(request_type=dict) + +def test_get_deployment_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = apihub_service.ListDeploymentsResponse() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - - response_value = Response() - response_value.status_code = 200 + # 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 = apihub_service.GetDeploymentRequest() - # Convert return value to protobuf type - return_value = apihub_service.ListDeploymentsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.name = "name_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_deployment), "__call__") as call: + call.return_value = common_fields.Deployment() + client.get_deployment(request) - response = client.list_deployments(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] -def test_list_deployments_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_get_deployment_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.list_deployments._get_unset_required_fields({}) - assert set(unset_fields) == ( - set( - ( - "filter", - "pageSize", - "pageToken", - ) + # 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 = apihub_service.GetDeploymentRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_deployment), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Deployment() ) - & set(("parent",)) - ) + await client.get_deployment(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request -def test_list_deployments_rest_flattened(): + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] + + +def test_get_deployment_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = apihub_service.ListDeploymentsResponse() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_deployment), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Deployment() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_deployment( + name="name_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( - parent="parent_value", + +def test_get_deployment_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_deployment( + apihub_service.GetDeploymentRequest(), + name="name_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = apihub_service.ListDeploymentsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_deployments(**mock_args) +@pytest.mark.asyncio +async def test_get_deployment_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_deployment), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Deployment() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Deployment() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_deployment( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/deployments" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_list_deployments_rest_flattened_error(transport: str = "rest"): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_get_deployment_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.list_deployments( - apihub_service.ListDeploymentsRequest(), - parent="parent_value", + await client.get_deployment( + apihub_service.GetDeploymentRequest(), + name="name_value", ) -def test_list_deployments_rest_pager(transport: str = "rest"): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.ListDeploymentsRequest, + dict, + ], +) +def test_list_deployments(request_type, transport: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # TODO(kbandes): remove this mock unless there's a good reason for it. - # with mock.patch.object(path_template, 'transcode') as transcode: - # Set the response as a series of pages - response = ( - apihub_service.ListDeploymentsResponse( - deployments=[ - common_fields.Deployment(), - common_fields.Deployment(), - common_fields.Deployment(), - ], - next_page_token="abc", - ), - apihub_service.ListDeploymentsResponse( - deployments=[], - next_page_token="def", - ), - apihub_service.ListDeploymentsResponse( - deployments=[ - common_fields.Deployment(), - ], - next_page_token="ghi", - ), - apihub_service.ListDeploymentsResponse( - deployments=[ - common_fields.Deployment(), - common_fields.Deployment(), - ], - ), - ) - # Two responses for two calls - response = response + response + # 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() - # Wrap the values into proper Response objs - response = tuple( - apihub_service.ListDeploymentsResponse.to_json(x) for x in response + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_deployments), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListDeploymentsResponse( + next_page_token="next_page_token_value", ) - return_values = tuple(Response() for i in response) - for return_val, response_val in zip(return_values, response): - return_val._content = response_val.encode("UTF-8") - return_val.status_code = 200 - req.side_effect = return_values + response = client.list_deployments(request) - sample_request = {"parent": "projects/sample1/locations/sample2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.ListDeploymentsRequest() + assert args[0] == request - pager = client.list_deployments(request=sample_request) + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListDeploymentsPager) + assert response.next_page_token == "next_page_token_value" - results = list(pager) - assert len(results) == 6 - assert all(isinstance(i, common_fields.Deployment) for i in results) - pages = list(client.list_deployments(request=sample_request).pages) - for page_, token in zip(pages, ["abc", "def", "ghi", ""]): - assert page_.raw_page.next_page_token == token +def test_list_deployments_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.ListDeploymentsRequest( + parent="parent_value", + filter="filter_value", + page_token="page_token_value", + ) -def test_update_deployment_rest_use_cached_wrapped_rpc(): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_deployments), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.list_deployments(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.ListDeploymentsRequest( + parent="parent_value", + filter="filter_value", + page_token="page_token_value", + ) + + +def test_list_deployments_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -5903,7 +10112,7 @@ def test_update_deployment_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.update_deployment in client._transport._wrapped_methods + assert client._transport.list_deployments in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() @@ -5911,360 +10120,524 @@ def test_update_deployment_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[ - client._transport.update_deployment + client._transport.list_deployments ] = mock_rpc - request = {} - client.update_deployment(request) + client.list_deployments(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.update_deployment(request) + client.list_deployments(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_update_deployment_rest_required_fields( - request_type=apihub_service.UpdateDeploymentRequest, +@pytest.mark.asyncio +async def test_list_deployments_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.list_deployments + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_deployment._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.list_deployments + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.list_deployments(request) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_deployment._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("update_mask",)) - jsonified_request.update(unset_fields) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - # verify required fields with non-default values are left alone + await client.list_deployments(request) - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_list_deployments_async( + transport: str = "grpc_asyncio", request_type=apihub_service.ListDeploymentsRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = common_fields.Deployment() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "patch", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # 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() - response_value = Response() - response_value.status_code = 200 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_deployments), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListDeploymentsResponse( + next_page_token="next_page_token_value", + ) + ) + response = await client.list_deployments(request) - # Convert return value to protobuf type - return_value = common_fields.Deployment.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.ListDeploymentsRequest() + assert args[0] == request - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListDeploymentsAsyncPager) + assert response.next_page_token == "next_page_token_value" - response = client.update_deployment(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_list_deployments_async_from_dict(): + await test_list_deployments_async(request_type=dict) -def test_update_deployment_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +def test_list_deployments_field_headers(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = transport.update_deployment._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(("updateMask",)) - & set( - ( - "deployment", - "updateMask", - ) - ) - ) + # 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 = apihub_service.ListDeploymentsRequest() + request.parent = "parent_value" -def test_update_deployment_rest_flattened(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_deployments), "__call__") as call: + call.return_value = apihub_service.ListDeploymentsResponse() + client.list_deployments(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", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_list_deployments_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Deployment() + # 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 = apihub_service.ListDeploymentsRequest() - # get arguments that satisfy an http rule for this method - sample_request = { - "deployment": { - "name": "projects/sample1/locations/sample2/deployments/sample3" - } - } + request.parent = "parent_value" - # get truthy value for each flattened field - mock_args = dict( - deployment=common_fields.Deployment(name="name_value"), - update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_deployments), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListDeploymentsResponse() ) - mock_args.update(sample_request) + await client.list_deployments(request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Deployment.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request - client.update_deployment(**mock_args) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +def test_list_deployments_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_deployments), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListDeploymentsResponse() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.list_deployments( + parent="parent_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{deployment.name=projects/*/locations/*/deployments/*}" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val -def test_update_deployment_rest_flattened_error(transport: str = "rest"): +def test_list_deployments_flattened_error(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.update_deployment( - apihub_service.UpdateDeploymentRequest(), - deployment=common_fields.Deployment(name="name_value"), - update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + client.list_deployments( + apihub_service.ListDeploymentsRequest(), + parent="parent_value", ) -def test_delete_deployment_rest_use_cached_wrapped_rpc(): - # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, - # instead of constructing them on each call - with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - - # Should wrap all calls on client creation - assert wrapper_fn.call_count > 0 - wrapper_fn.reset_mock() +@pytest.mark.asyncio +async def test_list_deployments_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) - # Ensure method has been cached - assert client._transport.delete_deployment in client._transport._wrapped_methods + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_deployments), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListDeploymentsResponse() - # Replace cached wrapped function with mock - mock_rpc = mock.Mock() - mock_rpc.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListDeploymentsResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.list_deployments( + parent="parent_value", ) - client._transport._wrapped_methods[ - client._transport.delete_deployment - ] = mock_rpc - - request = {} - client.delete_deployment(request) - - # Establish that the underlying gRPC stub method was called. - assert mock_rpc.call_count == 1 - - client.delete_deployment(request) - - # Establish that a new wrapper was not created for this call - assert wrapper_fn.call_count == 0 - assert mock_rpc.call_count == 2 + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val -def test_delete_deployment_rest_required_fields( - request_type=apihub_service.DeleteDeploymentRequest, -): - transport_class = transports.ApiHubRestTransport - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) +@pytest.mark.asyncio +async def test_list_deployments_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - # verify fields with default values are dropped - - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_deployment._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - - # verify required fields with default values are now present - - jsonified_request["name"] = "name_value" - - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_deployment._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.list_deployments( + apihub_service.ListDeploymentsRequest(), + parent="parent_value", + ) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" +def test_list_deployments_pager(transport_name: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport_name, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = None - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "delete", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_deployments), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListDeploymentsResponse( + deployments=[ + common_fields.Deployment(), + common_fields.Deployment(), + common_fields.Deployment(), + ], + next_page_token="abc", + ), + apihub_service.ListDeploymentsResponse( + deployments=[], + next_page_token="def", + ), + apihub_service.ListDeploymentsResponse( + deployments=[ + common_fields.Deployment(), + ], + next_page_token="ghi", + ), + apihub_service.ListDeploymentsResponse( + deployments=[ + common_fields.Deployment(), + common_fields.Deployment(), + ], + ), + RuntimeError, + ) - response_value = Response() - response_value.status_code = 200 - json_return_value = "" + expected_metadata = () + retry = retries.Retry() + timeout = 5 + expected_metadata = tuple(expected_metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", ""),)), + ) + pager = client.list_deployments(request={}, retry=retry, timeout=timeout) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + assert pager._metadata == expected_metadata + assert pager._retry == retry + assert pager._timeout == timeout - response = client.delete_deployment(request) + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, common_fields.Deployment) for i in results) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +def test_list_deployments_pages(transport_name: str = "grpc"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport_name, + ) -def test_delete_deployment_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_deployments), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListDeploymentsResponse( + deployments=[ + common_fields.Deployment(), + common_fields.Deployment(), + common_fields.Deployment(), + ], + next_page_token="abc", + ), + apihub_service.ListDeploymentsResponse( + deployments=[], + next_page_token="def", + ), + apihub_service.ListDeploymentsResponse( + deployments=[ + common_fields.Deployment(), + ], + next_page_token="ghi", + ), + apihub_service.ListDeploymentsResponse( + deployments=[ + common_fields.Deployment(), + common_fields.Deployment(), + ], + ), + RuntimeError, + ) + pages = list(client.list_deployments(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.asyncio +async def test_list_deployments_async_pager(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.delete_deployment._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_deployments), "__call__", new_callable=mock.AsyncMock + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListDeploymentsResponse( + deployments=[ + common_fields.Deployment(), + common_fields.Deployment(), + common_fields.Deployment(), + ], + next_page_token="abc", + ), + apihub_service.ListDeploymentsResponse( + deployments=[], + next_page_token="def", + ), + apihub_service.ListDeploymentsResponse( + deployments=[ + common_fields.Deployment(), + ], + next_page_token="ghi", + ), + apihub_service.ListDeploymentsResponse( + deployments=[ + common_fields.Deployment(), + common_fields.Deployment(), + ], + ), + RuntimeError, + ) + async_pager = await client.list_deployments( + request={}, + ) + assert async_pager.next_page_token == "abc" + responses = [] + async for response in async_pager: # pragma: no branch + responses.append(response) + assert len(responses) == 6 + assert all(isinstance(i, common_fields.Deployment) for i in responses) -def test_delete_deployment_rest_flattened(): + +@pytest.mark.asyncio +async def test_list_deployments_async_pages(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_deployments), "__call__", new_callable=mock.AsyncMock + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListDeploymentsResponse( + deployments=[ + common_fields.Deployment(), + common_fields.Deployment(), + common_fields.Deployment(), + ], + next_page_token="abc", + ), + apihub_service.ListDeploymentsResponse( + deployments=[], + next_page_token="def", + ), + apihub_service.ListDeploymentsResponse( + deployments=[ + common_fields.Deployment(), + ], + next_page_token="ghi", + ), + apihub_service.ListDeploymentsResponse( + deployments=[ + common_fields.Deployment(), + common_fields.Deployment(), + ], + ), + RuntimeError, + ) + pages = [] + # Workaround issue in python 3.9 related to code coverage by adding `# pragma: no branch` + # See https://github.com/googleapis/gapic-generator-python/pull/1174#issuecomment-1025132372 + async for page_ in ( # pragma: no branch + await client.list_deployments(request={}) + ).pages: + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.UpdateDeploymentRequest, + dict, + ], +) +def test_update_deployment(request_type, transport: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = None - - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/deployments/sample3" - } + # 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() - # get truthy value for each flattened field - mock_args = dict( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_deployment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Deployment( name="name_value", + display_name="display_name_value", + description="description_value", + resource_uri="resource_uri_value", + endpoints=["endpoints_value"], + api_versions=["api_versions_value"], + source_project="source_project_value", + source_environment="source_environment_value", ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - json_return_value = "" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.update_deployment(request) - client.delete_deployment(**mock_args) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.UpdateDeploymentRequest() + assert args[0] == request - # Establish that the underlying call was made with the expected - # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/deployments/*}" - % client.transport._host, - args[1], - ) + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Deployment) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.resource_uri == "resource_uri_value" + assert response.endpoints == ["endpoints_value"] + assert response.api_versions == ["api_versions_value"] + assert response.source_project == "source_project_value" + assert response.source_environment == "source_environment_value" -def test_delete_deployment_rest_flattened_error(transport: str = "rest"): +def test_update_deployment_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport="grpc", ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.delete_deployment( - apihub_service.DeleteDeploymentRequest(), - name="name_value", + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.UpdateDeploymentRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_deployment), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. ) + client.update_deployment(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.UpdateDeploymentRequest() -def test_create_attribute_rest_use_cached_wrapped_rpc(): +def test_update_deployment_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -6272,7 +10645,7 @@ def test_create_attribute_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.create_attribute in client._transport._wrapped_methods + assert client._transport.update_deployment in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() @@ -6280,185 +10653,351 @@ def test_create_attribute_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[ - client._transport.create_attribute + client._transport.update_deployment ] = mock_rpc - request = {} - client.create_attribute(request) + client.update_deployment(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.create_attribute(request) + client.update_deployment(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_create_attribute_rest_required_fields( - request_type=apihub_service.CreateAttributeRequest, +@pytest.mark.asyncio +async def test_update_deployment_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.update_deployment + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_attribute._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.update_deployment + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.update_deployment(request) - jsonified_request["parent"] = "parent_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_attribute._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("attribute_id",)) - jsonified_request.update(unset_fields) + await client.update_deployment(request) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + +@pytest.mark.asyncio +async def test_update_deployment_async( + transport: str = "grpc_asyncio", request_type=apihub_service.UpdateDeploymentRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = common_fields.Attribute() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # 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() - response_value = Response() - response_value.status_code = 200 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_deployment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Deployment( + name="name_value", + display_name="display_name_value", + description="description_value", + resource_uri="resource_uri_value", + endpoints=["endpoints_value"], + api_versions=["api_versions_value"], + source_project="source_project_value", + source_environment="source_environment_value", + ) + ) + response = await client.update_deployment(request) - # Convert return value to protobuf type - return_value = common_fields.Attribute.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.UpdateDeploymentRequest() + assert args[0] == request - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Deployment) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.resource_uri == "resource_uri_value" + assert response.endpoints == ["endpoints_value"] + assert response.api_versions == ["api_versions_value"] + assert response.source_project == "source_project_value" + assert response.source_environment == "source_environment_value" - response = client.create_attribute(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_update_deployment_async_from_dict(): + await test_update_deployment_async(request_type=dict) -def test_create_attribute_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +def test_update_deployment_field_headers(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = transport.create_attribute._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(("attributeId",)) - & set( - ( - "parent", - "attribute", - ) - ) + # 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 = apihub_service.UpdateDeploymentRequest() + + request.deployment.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_deployment), "__call__" + ) as call: + call.return_value = common_fields.Deployment() + client.update_deployment(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", + "deployment.name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_update_deployment_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) + # 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 = apihub_service.UpdateDeploymentRequest() -def test_create_attribute_rest_flattened(): + request.deployment.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_deployment), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Deployment() + ) + await client.update_deployment(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", + "deployment.name=name_value", + ) in kw["metadata"] + + +def test_update_deployment_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Attribute() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_deployment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Deployment() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.update_deployment( + deployment=common_fields.Deployment(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].deployment + mock_val = common_fields.Deployment(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( - parent="parent_value", - attribute=common_fields.Attribute(name="name_value"), - attribute_id="attribute_id_value", + +def test_update_deployment_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_deployment( + apihub_service.UpdateDeploymentRequest(), + deployment=common_fields.Deployment(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Attribute.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_attribute(**mock_args) +@pytest.mark.asyncio +async def test_update_deployment_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_deployment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Deployment() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Deployment() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.update_deployment( + deployment=common_fields.Deployment(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/attributes" % client.transport._host, - args[1], + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].deployment + mock_val = common_fields.Deployment(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_update_deployment_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.update_deployment( + apihub_service.UpdateDeploymentRequest(), + deployment=common_fields.Deployment(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) -def test_create_attribute_rest_flattened_error(transport: str = "rest"): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.DeleteDeploymentRequest, + dict, + ], +) +def test_delete_deployment(request_type, transport: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.create_attribute( - apihub_service.CreateAttributeRequest(), - parent="parent_value", - attribute=common_fields.Attribute(name="name_value"), - attribute_id="attribute_id_value", + # 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.delete_deployment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.delete_deployment(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.DeleteDeploymentRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert response is None + + +def test_delete_deployment_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.DeleteDeploymentRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_deployment), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.delete_deployment(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.DeleteDeploymentRequest( + name="name_value", ) -def test_get_attribute_rest_use_cached_wrapped_rpc(): +def test_delete_deployment_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -6466,178 +11005,344 @@ def test_get_attribute_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.get_attribute in client._transport._wrapped_methods + assert client._transport.delete_deployment in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[client._transport.get_attribute] = mock_rpc - + client._transport._wrapped_methods[ + client._transport.delete_deployment + ] = mock_rpc request = {} - client.get_attribute(request) + client.delete_deployment(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.get_attribute(request) + client.delete_deployment(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_get_attribute_rest_required_fields( - request_type=apihub_service.GetAttributeRequest, +@pytest.mark.asyncio +async def test_delete_deployment_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.delete_deployment + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_attribute._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.delete_deployment + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.delete_deployment(request) - jsonified_request["name"] = "name_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_attribute._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + await client.delete_deployment(request) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + +@pytest.mark.asyncio +async def test_delete_deployment_async( + transport: str = "grpc_asyncio", request_type=apihub_service.DeleteDeploymentRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = common_fields.Attribute() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # 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() - response_value = Response() - response_value.status_code = 200 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_deployment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_deployment(request) - # Convert return value to protobuf type - return_value = common_fields.Attribute.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.DeleteDeploymentRequest() + assert args[0] == request - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert response is None - response = client.get_attribute(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_delete_deployment_async_from_dict(): + await test_delete_deployment_async(request_type=dict) -def test_get_attribute_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +def test_delete_deployment_field_headers(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = transport.get_attribute._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # 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 = apihub_service.DeleteDeploymentRequest() + request.name = "name_value" -def test_get_attribute_rest_flattened(): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_deployment), "__call__" + ) as call: + call.return_value = None + client.delete_deployment(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", + "name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_delete_deployment_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = apihub_service.DeleteDeploymentRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_deployment), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_deployment(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", + "name=name_value", + ) in kw["metadata"] + + +def test_delete_deployment_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Attribute() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_deployment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = None + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.delete_deployment( + name="name_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/attributes/sample3" - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_delete_deployment_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_deployment( + apihub_service.DeleteDeploymentRequest(), name="name_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Attribute.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_attribute(**mock_args) +@pytest.mark.asyncio +async def test_delete_deployment_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_deployment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = None + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.delete_deployment( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/attributes/*}" % client.transport._host, - args[1], + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_delete_deployment_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.delete_deployment( + apihub_service.DeleteDeploymentRequest(), + name="name_value", ) -def test_get_attribute_rest_flattened_error(transport: str = "rest"): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.CreateAttributeRequest, + dict, + ], +) +def test_create_attribute(request_type, transport: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.get_attribute( - apihub_service.GetAttributeRequest(), + # 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.create_attribute), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Attribute( name="name_value", + display_name="display_name_value", + description="description_value", + definition_type=common_fields.Attribute.DefinitionType.SYSTEM_DEFINED, + scope=common_fields.Attribute.Scope.API, + data_type=common_fields.Attribute.DataType.ENUM, + cardinality=1172, + mandatory=True, ) + response = client.create_attribute(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.CreateAttributeRequest() + assert args[0] == request -def test_update_attribute_rest_use_cached_wrapped_rpc(): + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Attribute) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert ( + response.definition_type + == common_fields.Attribute.DefinitionType.SYSTEM_DEFINED + ) + assert response.scope == common_fields.Attribute.Scope.API + assert response.data_type == common_fields.Attribute.DataType.ENUM + assert response.cardinality == 1172 + assert response.mandatory is True + + +def test_create_attribute_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.CreateAttributeRequest( + parent="parent_value", + attribute_id="attribute_id_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_attribute), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.create_attribute(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.CreateAttributeRequest( + parent="parent_value", + attribute_id="attribute_id_value", + ) + + +def test_create_attribute_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -6645,7 +11350,7 @@ def test_update_attribute_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.update_attribute in client._transport._wrapped_methods + assert client._transport.create_attribute in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() @@ -6653,183 +11358,370 @@ def test_update_attribute_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[ - client._transport.update_attribute + client._transport.create_attribute ] = mock_rpc - request = {} - client.update_attribute(request) + client.create_attribute(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.update_attribute(request) + client.create_attribute(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_update_attribute_rest_required_fields( - request_type=apihub_service.UpdateAttributeRequest, +@pytest.mark.asyncio +async def test_create_attribute_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.create_attribute + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_attribute._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.create_attribute + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.create_attribute(request) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_attribute._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("update_mask",)) - jsonified_request.update(unset_fields) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - # verify required fields with non-default values are left alone + await client.create_attribute(request) - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_create_attribute_async( + transport: str = "grpc_asyncio", request_type=apihub_service.CreateAttributeRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = common_fields.Attribute() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "patch", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # 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() - response_value = Response() - response_value.status_code = 200 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_attribute), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Attribute( + name="name_value", + display_name="display_name_value", + description="description_value", + definition_type=common_fields.Attribute.DefinitionType.SYSTEM_DEFINED, + scope=common_fields.Attribute.Scope.API, + data_type=common_fields.Attribute.DataType.ENUM, + cardinality=1172, + mandatory=True, + ) + ) + response = await client.create_attribute(request) - # Convert return value to protobuf type - return_value = common_fields.Attribute.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.CreateAttributeRequest() + assert args[0] == request - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Attribute) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert ( + response.definition_type + == common_fields.Attribute.DefinitionType.SYSTEM_DEFINED + ) + assert response.scope == common_fields.Attribute.Scope.API + assert response.data_type == common_fields.Attribute.DataType.ENUM + assert response.cardinality == 1172 + assert response.mandatory is True - response = client.update_attribute(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_create_attribute_async_from_dict(): + await test_create_attribute_async(request_type=dict) -def test_update_attribute_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +def test_create_attribute_field_headers(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = transport.update_attribute._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(("updateMask",)) - & set( - ( - "attribute", - "updateMask", - ) - ) + # 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 = apihub_service.CreateAttributeRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_attribute), "__call__") as call: + call.return_value = common_fields.Attribute() + client.create_attribute(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", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_create_attribute_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) + # 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 = apihub_service.CreateAttributeRequest() -def test_update_attribute_rest_flattened(): + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_attribute), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Attribute() + ) + await client.create_attribute(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", + "parent=parent_value", + ) in kw["metadata"] + + +def test_create_attribute_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Attribute() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_attribute), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Attribute() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.create_attribute( + parent="parent_value", + attribute=common_fields.Attribute(name="name_value"), + attribute_id="attribute_id_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "attribute": { - "name": "projects/sample1/locations/sample2/attributes/sample3" - } - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].attribute + mock_val = common_fields.Attribute(name="name_value") + assert arg == mock_val + arg = args[0].attribute_id + mock_val = "attribute_id_value" + assert arg == mock_val + + +def test_create_attribute_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) - # get truthy value for each flattened field - mock_args = dict( + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_attribute( + apihub_service.CreateAttributeRequest(), + parent="parent_value", attribute=common_fields.Attribute(name="name_value"), - update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + attribute_id="attribute_id_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Attribute.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.update_attribute(**mock_args) +@pytest.mark.asyncio +async def test_create_attribute_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_attribute), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Attribute() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Attribute() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.create_attribute( + parent="parent_value", + attribute=common_fields.Attribute(name="name_value"), + attribute_id="attribute_id_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{attribute.name=projects/*/locations/*/attributes/*}" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].attribute + mock_val = common_fields.Attribute(name="name_value") + assert arg == mock_val + arg = args[0].attribute_id + mock_val = "attribute_id_value" + assert arg == mock_val -def test_update_attribute_rest_flattened_error(transport: str = "rest"): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_create_attribute_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.update_attribute( - apihub_service.UpdateAttributeRequest(), + await client.create_attribute( + apihub_service.CreateAttributeRequest(), + parent="parent_value", attribute=common_fields.Attribute(name="name_value"), - update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + attribute_id="attribute_id_value", ) -def test_delete_attribute_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.GetAttributeRequest, + dict, + ], +) +def test_get_attribute(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.get_attribute), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Attribute( + name="name_value", + display_name="display_name_value", + description="description_value", + definition_type=common_fields.Attribute.DefinitionType.SYSTEM_DEFINED, + scope=common_fields.Attribute.Scope.API, + data_type=common_fields.Attribute.DataType.ENUM, + cardinality=1172, + mandatory=True, + ) + response = client.get_attribute(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.GetAttributeRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Attribute) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert ( + response.definition_type + == common_fields.Attribute.DefinitionType.SYSTEM_DEFINED + ) + assert response.scope == common_fields.Attribute.Scope.API + assert response.data_type == common_fields.Attribute.DataType.ENUM + assert response.cardinality == 1172 + assert response.mandatory is True + + +def test_get_attribute_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.GetAttributeRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_attribute), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_attribute(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.GetAttributeRequest( + name="name_value", + ) + + +def test_get_attribute_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -6837,175 +11729,352 @@ def test_delete_attribute_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.delete_attribute in client._transport._wrapped_methods + assert client._transport.get_attribute in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[ - client._transport.delete_attribute - ] = mock_rpc - + client._transport._wrapped_methods[client._transport.get_attribute] = mock_rpc request = {} - client.delete_attribute(request) + client.get_attribute(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.delete_attribute(request) + client.get_attribute(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_delete_attribute_rest_required_fields( - request_type=apihub_service.DeleteAttributeRequest, +@pytest.mark.asyncio +async def test_get_attribute_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.get_attribute + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_attribute._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.get_attribute + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.get_attribute(request) - jsonified_request["name"] = "name_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_attribute._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + await client.get_attribute(request) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_get_attribute_async( + transport: str = "grpc_asyncio", request_type=apihub_service.GetAttributeRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + 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.get_attribute), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Attribute( + name="name_value", + display_name="display_name_value", + description="description_value", + definition_type=common_fields.Attribute.DefinitionType.SYSTEM_DEFINED, + scope=common_fields.Attribute.Scope.API, + data_type=common_fields.Attribute.DataType.ENUM, + cardinality=1172, + mandatory=True, + ) + ) + response = await client.get_attribute(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.GetAttributeRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Attribute) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert ( + response.definition_type + == common_fields.Attribute.DefinitionType.SYSTEM_DEFINED + ) + assert response.scope == common_fields.Attribute.Scope.API + assert response.data_type == common_fields.Attribute.DataType.ENUM + assert response.cardinality == 1172 + assert response.mandatory is True + + +@pytest.mark.asyncio +async def test_get_attribute_async_from_dict(): + await test_get_attribute_async(request_type=dict) + +def test_get_attribute_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = None - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "delete", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # 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 = apihub_service.GetAttributeRequest() - response_value = Response() - response_value.status_code = 200 - json_return_value = "" + request.name = "name_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_attribute), "__call__") as call: + call.return_value = common_fields.Attribute() + client.get_attribute(request) - response = client.delete_attribute(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] -def test_delete_attribute_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_get_attribute_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.delete_attribute._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # 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 = apihub_service.GetAttributeRequest() + request.name = "name_value" -def test_delete_attribute_rest_flattened(): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_attribute), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Attribute() + ) + await client.get_attribute(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", + "name=name_value", + ) in kw["metadata"] + + +def test_get_attribute_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = None + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_attribute), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Attribute() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_attribute( + name="name_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/attributes/sample3" - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_get_attribute_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_attribute( + apihub_service.GetAttributeRequest(), name="name_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - json_return_value = "" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_attribute(**mock_args) +@pytest.mark.asyncio +async def test_get_attribute_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_attribute), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Attribute() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Attribute() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_attribute( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/attributes/*}" % client.transport._host, - args[1], + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_get_attribute_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.get_attribute( + apihub_service.GetAttributeRequest(), + name="name_value", ) -def test_delete_attribute_rest_flattened_error(transport: str = "rest"): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.UpdateAttributeRequest, + dict, + ], +) +def test_update_attribute(request_type, transport: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.delete_attribute( - apihub_service.DeleteAttributeRequest(), + # 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.update_attribute), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Attribute( name="name_value", + display_name="display_name_value", + description="description_value", + definition_type=common_fields.Attribute.DefinitionType.SYSTEM_DEFINED, + scope=common_fields.Attribute.Scope.API, + data_type=common_fields.Attribute.DataType.ENUM, + cardinality=1172, + mandatory=True, ) + response = client.update_attribute(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.UpdateAttributeRequest() + assert args[0] == request -def test_list_attributes_rest_use_cached_wrapped_rpc(): + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Attribute) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert ( + response.definition_type + == common_fields.Attribute.DefinitionType.SYSTEM_DEFINED + ) + assert response.scope == common_fields.Attribute.Scope.API + assert response.data_type == common_fields.Attribute.DataType.ENUM + assert response.cardinality == 1172 + assert response.mandatory is True + + +def test_update_attribute_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.UpdateAttributeRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_attribute), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.update_attribute(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.UpdateAttributeRequest() + + +def test_update_attribute_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -7013,256 +12082,348 @@ def test_list_attributes_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.list_attributes in client._transport._wrapped_methods + assert client._transport.update_attribute in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[client._transport.list_attributes] = mock_rpc - + client._transport._wrapped_methods[ + client._transport.update_attribute + ] = mock_rpc request = {} - client.list_attributes(request) + client.update_attribute(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.list_attributes(request) + client.update_attribute(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_list_attributes_rest_required_fields( - request_type=apihub_service.ListAttributesRequest, +@pytest.mark.asyncio +async def test_update_attribute_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.update_attribute + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_attributes._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.update_attribute + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.update_attribute(request) - jsonified_request["parent"] = "parent_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_attributes._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "filter", - "page_size", - "page_token", - ) - ) - jsonified_request.update(unset_fields) + await client.update_attribute(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_update_attribute_async( + transport: str = "grpc_asyncio", request_type=apihub_service.UpdateAttributeRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + 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.update_attribute), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Attribute( + name="name_value", + display_name="display_name_value", + description="description_value", + definition_type=common_fields.Attribute.DefinitionType.SYSTEM_DEFINED, + scope=common_fields.Attribute.Scope.API, + data_type=common_fields.Attribute.DataType.ENUM, + cardinality=1172, + mandatory=True, + ) + ) + response = await client.update_attribute(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.UpdateAttributeRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Attribute) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert ( + response.definition_type + == common_fields.Attribute.DefinitionType.SYSTEM_DEFINED + ) + assert response.scope == common_fields.Attribute.Scope.API + assert response.data_type == common_fields.Attribute.DataType.ENUM + assert response.cardinality == 1172 + assert response.mandatory is True - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" +@pytest.mark.asyncio +async def test_update_attribute_async_from_dict(): + await test_update_attribute_async(request_type=dict) + + +def test_update_attribute_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = apihub_service.ListAttributesResponse() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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 = apihub_service.UpdateAttributeRequest() - # Convert return value to protobuf type - return_value = apihub_service.ListAttributesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.attribute.name = "name_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_attribute), "__call__") as call: + call.return_value = common_fields.Attribute() + client.update_attribute(request) - response = client.list_attributes(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "attribute.name=name_value", + ) in kw["metadata"] -def test_list_attributes_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_update_attribute_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.list_attributes._get_unset_required_fields({}) - assert set(unset_fields) == ( - set( - ( - "filter", - "pageSize", - "pageToken", - ) + # 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 = apihub_service.UpdateAttributeRequest() + + request.attribute.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_attribute), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Attribute() ) - & set(("parent",)) - ) + await client.update_attribute(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", + "attribute.name=name_value", + ) in kw["metadata"] -def test_list_attributes_rest_flattened(): + +def test_update_attribute_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = apihub_service.ListAttributesResponse() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_attribute), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Attribute() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.update_attribute( + attribute=common_fields.Attribute(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].attribute + mock_val = common_fields.Attribute(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( - parent="parent_value", + +def test_update_attribute_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_attribute( + apihub_service.UpdateAttributeRequest(), + attribute=common_fields.Attribute(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = apihub_service.ListAttributesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_attributes(**mock_args) +@pytest.mark.asyncio +async def test_update_attribute_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_attribute), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Attribute() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Attribute() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.update_attribute( + attribute=common_fields.Attribute(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/attributes" % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].attribute + mock_val = common_fields.Attribute(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val -def test_list_attributes_rest_flattened_error(transport: str = "rest"): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_update_attribute_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.list_attributes( - apihub_service.ListAttributesRequest(), - parent="parent_value", + await client.update_attribute( + apihub_service.UpdateAttributeRequest(), + attribute=common_fields.Attribute(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) -def test_list_attributes_rest_pager(transport: str = "rest"): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.DeleteAttributeRequest, + dict, + ], +) +def test_delete_attribute(request_type, transport: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # TODO(kbandes): remove this mock unless there's a good reason for it. - # with mock.patch.object(path_template, 'transcode') as transcode: - # Set the response as a series of pages - response = ( - apihub_service.ListAttributesResponse( - attributes=[ - common_fields.Attribute(), - common_fields.Attribute(), - common_fields.Attribute(), - ], - next_page_token="abc", - ), - apihub_service.ListAttributesResponse( - attributes=[], - next_page_token="def", - ), - apihub_service.ListAttributesResponse( - attributes=[ - common_fields.Attribute(), - ], - next_page_token="ghi", - ), - apihub_service.ListAttributesResponse( - attributes=[ - common_fields.Attribute(), - common_fields.Attribute(), - ], - ), - ) - # Two responses for two calls - response = response + response + # 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() - # Wrap the values into proper Response objs - response = tuple( - apihub_service.ListAttributesResponse.to_json(x) for x in response - ) - return_values = tuple(Response() for i in response) - for return_val, response_val in zip(return_values, response): - return_val._content = response_val.encode("UTF-8") - return_val.status_code = 200 - req.side_effect = return_values + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_attribute), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.delete_attribute(request) - sample_request = {"parent": "projects/sample1/locations/sample2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.DeleteAttributeRequest() + assert args[0] == request - pager = client.list_attributes(request=sample_request) + # Establish that the response is the type that we expect. + assert response is None - results = list(pager) - assert len(results) == 6 - assert all(isinstance(i, common_fields.Attribute) for i in results) - pages = list(client.list_attributes(request=sample_request).pages) - for page_, token in zip(pages, ["abc", "def", "ghi", ""]): - assert page_.raw_page.next_page_token == token +def test_delete_attribute_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.DeleteAttributeRequest( + name="name_value", + ) -def test_search_resources_rest_use_cached_wrapped_rpc(): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_attribute), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.delete_attribute(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.DeleteAttributeRequest( + name="name_value", + ) + + +def test_delete_attribute_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -7270,7 +12431,7 @@ def test_search_resources_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.search_resources in client._transport._wrapped_methods + assert client._transport.delete_attribute in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() @@ -7278,249 +12439,311 @@ def test_search_resources_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[ - client._transport.search_resources + client._transport.delete_attribute ] = mock_rpc - request = {} - client.search_resources(request) + client.delete_attribute(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.search_resources(request) + client.delete_attribute(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_search_resources_rest_required_fields( - request_type=apihub_service.SearchResourcesRequest, +@pytest.mark.asyncio +async def test_delete_attribute_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["location"] = "" - request_init["query"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.delete_attribute + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.delete_attribute + ] = mock_rpc + + request = {} + await client.delete_attribute(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.delete_attribute(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_delete_attribute_async( + transport: str = "grpc_asyncio", request_type=apihub_service.DeleteAttributeRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # verify fields with default values are dropped + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).search_resources._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_attribute), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_attribute(request) - # verify required fields with default values are now present + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.DeleteAttributeRequest() + assert args[0] == request - jsonified_request["location"] = "location_value" - jsonified_request["query"] = "query_value" + # Establish that the response is the type that we expect. + assert response is None - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).search_resources._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "location" in jsonified_request - assert jsonified_request["location"] == "location_value" - assert "query" in jsonified_request - assert jsonified_request["query"] == "query_value" +@pytest.mark.asyncio +async def test_delete_attribute_async_from_dict(): + await test_delete_attribute_async(request_type=dict) + +def test_delete_attribute_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = apihub_service.SearchResourcesResponse() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # 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 = apihub_service.DeleteAttributeRequest() - response_value = Response() - response_value.status_code = 200 + request.name = "name_value" - # Convert return value to protobuf type - return_value = apihub_service.SearchResourcesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_attribute), "__call__") as call: + call.return_value = None + client.delete_attribute(request) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request - response = client.search_resources(request) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_delete_attribute_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) -def test_search_resources_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_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 = apihub_service.DeleteAttributeRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_attribute), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_attribute(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", + "name=name_value", + ) in kw["metadata"] + + +def test_delete_attribute_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = transport.search_resources._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(()) - & set( - ( - "location", - "query", - ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_attribute), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.delete_attribute( + name="name_value", ) - ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_search_resources_rest_flattened(): +def test_delete_attribute_flattened_error(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = apihub_service.SearchResourcesResponse() + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_attribute( + apihub_service.DeleteAttributeRequest(), + name="name_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = {"location": "projects/sample1/locations/sample2"} - # get truthy value for each flattened field - mock_args = dict( - location="location_value", - query="query_value", - ) - mock_args.update(sample_request) +@pytest.mark.asyncio +async def test_delete_attribute_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = apihub_service.SearchResourcesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_attribute), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None - client.search_resources(**mock_args) + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.delete_attribute( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{location=projects/*/locations/*}:searchResources" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_search_resources_rest_flattened_error(transport: str = "rest"): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_delete_attribute_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.search_resources( - apihub_service.SearchResourcesRequest(), - location="location_value", - query="query_value", + await client.delete_attribute( + apihub_service.DeleteAttributeRequest(), + name="name_value", ) -def test_search_resources_rest_pager(transport: str = "rest"): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.ListAttributesRequest, + dict, + ], +) +def test_list_attributes(request_type, transport: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # TODO(kbandes): remove this mock unless there's a good reason for it. - # with mock.patch.object(path_template, 'transcode') as transcode: - # Set the response as a series of pages - response = ( - apihub_service.SearchResourcesResponse( - search_results=[ - apihub_service.SearchResult(), - apihub_service.SearchResult(), - apihub_service.SearchResult(), - ], - next_page_token="abc", - ), - apihub_service.SearchResourcesResponse( - search_results=[], - next_page_token="def", - ), - apihub_service.SearchResourcesResponse( - search_results=[ - apihub_service.SearchResult(), - ], - next_page_token="ghi", - ), - apihub_service.SearchResourcesResponse( - search_results=[ - apihub_service.SearchResult(), - apihub_service.SearchResult(), - ], - ), - ) - # Two responses for two calls - response = response + response + # 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() - # Wrap the values into proper Response objs - response = tuple( - apihub_service.SearchResourcesResponse.to_json(x) for x in response + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_attributes), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListAttributesResponse( + next_page_token="next_page_token_value", ) - return_values = tuple(Response() for i in response) - for return_val, response_val in zip(return_values, response): - return_val._content = response_val.encode("UTF-8") - return_val.status_code = 200 - req.side_effect = return_values + response = client.list_attributes(request) - sample_request = {"location": "projects/sample1/locations/sample2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.ListAttributesRequest() + assert args[0] == request - pager = client.search_resources(request=sample_request) + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListAttributesPager) + assert response.next_page_token == "next_page_token_value" - results = list(pager) - assert len(results) == 6 - assert all(isinstance(i, apihub_service.SearchResult) for i in results) - pages = list(client.search_resources(request=sample_request).pages) - for page_, token in zip(pages, ["abc", "def", "ghi", ""]): - assert page_.raw_page.next_page_token == token +def test_list_attributes_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.ListAttributesRequest( + parent="parent_value", + filter="filter_value", + page_token="page_token_value", + ) -def test_create_external_api_rest_use_cached_wrapped_rpc(): - # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, - # instead of constructing them on each call + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_attributes), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.list_attributes(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.ListAttributesRequest( + parent="parent_value", + filter="filter_value", + page_token="page_token_value", + ) + + +def test_list_attributes_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -7528,378 +12751,522 @@ def test_create_external_api_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert ( - client._transport.create_external_api in client._transport._wrapped_methods - ) + assert client._transport.list_attributes in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[ - client._transport.create_external_api + client._transport._wrapped_methods[client._transport.list_attributes] = mock_rpc + request = {} + client.list_attributes(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.list_attributes(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_list_attributes_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.list_attributes + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.list_attributes ] = mock_rpc request = {} - client.create_external_api(request) + await client.list_attributes(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.create_external_api(request) + await client.list_attributes(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_create_external_api_rest_required_fields( - request_type=apihub_service.CreateExternalApiRequest, +@pytest.mark.asyncio +async def test_list_attributes_async( + transport: str = "grpc_asyncio", request_type=apihub_service.ListAttributesRequest ): - transport_class = transports.ApiHubRestTransport - - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # verify fields with default values are dropped + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_external_api._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_attributes), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListAttributesResponse( + next_page_token="next_page_token_value", + ) + ) + response = await client.list_attributes(request) - # verify required fields with default values are now present + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.ListAttributesRequest() + assert args[0] == request - jsonified_request["parent"] = "parent_value" + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListAttributesAsyncPager) + assert response.next_page_token == "next_page_token_value" - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_external_api._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("external_api_id",)) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" +@pytest.mark.asyncio +async def test_list_attributes_async_from_dict(): + await test_list_attributes_async(request_type=dict) + +def test_list_attributes_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = common_fields.ExternalApi() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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 = apihub_service.ListAttributesRequest() - # Convert return value to protobuf type - return_value = common_fields.ExternalApi.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.parent = "parent_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_attributes), "__call__") as call: + call.return_value = apihub_service.ListAttributesResponse() + client.list_attributes(request) - response = client.create_external_api(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] -def test_create_external_api_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_list_attributes_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.create_external_api._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(("externalApiId",)) - & set( - ( - "parent", - "externalApi", - ) + # 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 = apihub_service.ListAttributesRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_attributes), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListAttributesResponse() ) - ) + await client.list_attributes(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request -def test_create_external_api_rest_flattened(): + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +def test_list_attributes_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.ExternalApi() - - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} - - # get truthy value for each flattened field - mock_args = dict( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_attributes), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListAttributesResponse() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.list_attributes( parent="parent_value", - external_api=common_fields.ExternalApi(name="name_value"), - external_api_id="external_api_id_value", ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.ExternalApi.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - - client.create_external_api(**mock_args) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/externalApis" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val -def test_create_external_api_rest_flattened_error(transport: str = "rest"): +def test_list_attributes_flattened_error(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.create_external_api( - apihub_service.CreateExternalApiRequest(), + client.list_attributes( + apihub_service.ListAttributesRequest(), parent="parent_value", - external_api=common_fields.ExternalApi(name="name_value"), - external_api_id="external_api_id_value", ) -def test_get_external_api_rest_use_cached_wrapped_rpc(): - # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, - # instead of constructing them on each call - with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - - # Should wrap all calls on client creation - assert wrapper_fn.call_count > 0 - wrapper_fn.reset_mock() +@pytest.mark.asyncio +async def test_list_attributes_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) - # Ensure method has been cached - assert client._transport.get_external_api in client._transport._wrapped_methods + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_attributes), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListAttributesResponse() - # Replace cached wrapped function with mock - mock_rpc = mock.Mock() - mock_rpc.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListAttributesResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.list_attributes( + parent="parent_value", ) - client._transport._wrapped_methods[ - client._transport.get_external_api - ] = mock_rpc - - request = {} - client.get_external_api(request) - # Establish that the underlying gRPC stub method was called. - assert mock_rpc.call_count == 1 + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val - client.get_external_api(request) - # Establish that a new wrapper was not created for this call - assert wrapper_fn.call_count == 0 - assert mock_rpc.call_count == 2 +@pytest.mark.asyncio +async def test_list_attributes_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.list_attributes( + apihub_service.ListAttributesRequest(), + parent="parent_value", + ) -def test_get_external_api_rest_required_fields( - request_type=apihub_service.GetExternalApiRequest, -): - transport_class = transports.ApiHubRestTransport - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) +def test_list_attributes_pager(transport_name: str = "grpc"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport_name, ) - # verify fields with default values are dropped - - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_external_api._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - - # verify required fields with default values are now present + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_attributes), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListAttributesResponse( + attributes=[ + common_fields.Attribute(), + common_fields.Attribute(), + common_fields.Attribute(), + ], + next_page_token="abc", + ), + apihub_service.ListAttributesResponse( + attributes=[], + next_page_token="def", + ), + apihub_service.ListAttributesResponse( + attributes=[ + common_fields.Attribute(), + ], + next_page_token="ghi", + ), + apihub_service.ListAttributesResponse( + attributes=[ + common_fields.Attribute(), + common_fields.Attribute(), + ], + ), + RuntimeError, + ) - jsonified_request["name"] = "name_value" + expected_metadata = () + retry = retries.Retry() + timeout = 5 + expected_metadata = tuple(expected_metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", ""),)), + ) + pager = client.list_attributes(request={}, retry=retry, timeout=timeout) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_external_api._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + assert pager._metadata == expected_metadata + assert pager._retry == retry + assert pager._timeout == timeout + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, common_fields.Attribute) for i in results) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" +def test_list_attributes_pages(transport_name: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport_name, ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = common_fields.ExternalApi() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_attributes), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListAttributesResponse( + attributes=[ + common_fields.Attribute(), + common_fields.Attribute(), + common_fields.Attribute(), + ], + next_page_token="abc", + ), + apihub_service.ListAttributesResponse( + attributes=[], + next_page_token="def", + ), + apihub_service.ListAttributesResponse( + attributes=[ + common_fields.Attribute(), + ], + next_page_token="ghi", + ), + apihub_service.ListAttributesResponse( + attributes=[ + common_fields.Attribute(), + common_fields.Attribute(), + ], + ), + RuntimeError, + ) + pages = list(client.list_attributes(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token - # Convert return value to protobuf type - return_value = common_fields.ExternalApi.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +@pytest.mark.asyncio +async def test_list_attributes_async_pager(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) - response = client.get_external_api(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_attributes), "__call__", new_callable=mock.AsyncMock + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListAttributesResponse( + attributes=[ + common_fields.Attribute(), + common_fields.Attribute(), + common_fields.Attribute(), + ], + next_page_token="abc", + ), + apihub_service.ListAttributesResponse( + attributes=[], + next_page_token="def", + ), + apihub_service.ListAttributesResponse( + attributes=[ + common_fields.Attribute(), + ], + next_page_token="ghi", + ), + apihub_service.ListAttributesResponse( + attributes=[ + common_fields.Attribute(), + common_fields.Attribute(), + ], + ), + RuntimeError, + ) + async_pager = await client.list_attributes( + request={}, + ) + assert async_pager.next_page_token == "abc" + responses = [] + async for response in async_pager: # pragma: no branch + responses.append(response) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + assert len(responses) == 6 + assert all(isinstance(i, common_fields.Attribute) for i in responses) -def test_get_external_api_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_list_attributes_async_pages(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.get_external_api._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_attributes), "__call__", new_callable=mock.AsyncMock + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListAttributesResponse( + attributes=[ + common_fields.Attribute(), + common_fields.Attribute(), + common_fields.Attribute(), + ], + next_page_token="abc", + ), + apihub_service.ListAttributesResponse( + attributes=[], + next_page_token="def", + ), + apihub_service.ListAttributesResponse( + attributes=[ + common_fields.Attribute(), + ], + next_page_token="ghi", + ), + apihub_service.ListAttributesResponse( + attributes=[ + common_fields.Attribute(), + common_fields.Attribute(), + ], + ), + RuntimeError, + ) + pages = [] + # Workaround issue in python 3.9 related to code coverage by adding `# pragma: no branch` + # See https://github.com/googleapis/gapic-generator-python/pull/1174#issuecomment-1025132372 + async for page_ in ( # pragma: no branch + await client.list_attributes(request={}) + ).pages: + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token -def test_get_external_api_rest_flattened(): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.SearchResourcesRequest, + dict, + ], +) +def test_search_resources(request_type, transport: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.ExternalApi() - - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/externalApis/sample3" - } + # 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() - # get truthy value for each flattened field - mock_args = dict( - name="name_value", + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.search_resources), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.SearchResourcesResponse( + next_page_token="next_page_token_value", ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.ExternalApi.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.search_resources(request) - client.get_external_api(**mock_args) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.SearchResourcesRequest() + assert args[0] == request - # Establish that the underlying call was made with the expected - # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/externalApis/*}" - % client.transport._host, - args[1], - ) + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.SearchResourcesPager) + assert response.next_page_token == "next_page_token_value" -def test_get_external_api_rest_flattened_error(transport: str = "rest"): +def test_search_resources_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport="grpc", ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.get_external_api( - apihub_service.GetExternalApiRequest(), - name="name_value", + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.SearchResourcesRequest( + location="location_value", + query="query_value", + filter="filter_value", + page_token="page_token_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.search_resources), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.search_resources(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.SearchResourcesRequest( + location="location_value", + query="query_value", + filter="filter_value", + page_token="page_token_value", ) -def test_update_external_api_rest_use_cached_wrapped_rpc(): +def test_search_resources_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -7907,9 +13274,7 @@ def test_update_external_api_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert ( - client._transport.update_external_api in client._transport._wrapped_methods - ) + assert client._transport.search_resources in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() @@ -7917,362 +13282,534 @@ def test_update_external_api_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[ - client._transport.update_external_api + client._transport.search_resources ] = mock_rpc - request = {} - client.update_external_api(request) + client.search_resources(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.update_external_api(request) + client.search_resources(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_update_external_api_rest_required_fields( - request_type=apihub_service.UpdateExternalApiRequest, +@pytest.mark.asyncio +async def test_search_resources_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.search_resources + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.search_resources + ] = mock_rpc + + request = {} + await client.search_resources(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.search_resources(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_search_resources_async( + transport: str = "grpc_asyncio", request_type=apihub_service.SearchResourcesRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # verify fields with default values are dropped + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_external_api._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.search_resources), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.SearchResourcesResponse( + next_page_token="next_page_token_value", + ) + ) + response = await client.search_resources(request) - # verify required fields with default values are now present + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.SearchResourcesRequest() + assert args[0] == request - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_external_api._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("update_mask",)) - jsonified_request.update(unset_fields) + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.SearchResourcesAsyncPager) + assert response.next_page_token == "next_page_token_value" - # verify required fields with non-default values are left alone +@pytest.mark.asyncio +async def test_search_resources_async_from_dict(): + await test_search_resources_async(request_type=dict) + + +def test_search_resources_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = common_fields.ExternalApi() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "patch", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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 = apihub_service.SearchResourcesRequest() - # Convert return value to protobuf type - return_value = common_fields.ExternalApi.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.location = "location_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.search_resources), "__call__") as call: + call.return_value = apihub_service.SearchResourcesResponse() + client.search_resources(request) - response = client.update_external_api(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "location=location_value", + ) in kw["metadata"] -def test_update_external_api_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_search_resources_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.update_external_api._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(("updateMask",)) - & set( - ( - "externalApi", - "updateMask", - ) + # 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 = apihub_service.SearchResourcesRequest() + + request.location = "location_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.search_resources), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.SearchResourcesResponse() ) - ) + await client.search_resources(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request -def test_update_external_api_rest_flattened(): + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "location=location_value", + ) in kw["metadata"] + + +def test_search_resources_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.ExternalApi() - - # get arguments that satisfy an http rule for this method - sample_request = { - "external_api": { - "name": "projects/sample1/locations/sample2/externalApis/sample3" - } - } - - # get truthy value for each flattened field - mock_args = dict( - external_api=common_fields.ExternalApi(name="name_value"), - update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.search_resources), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.SearchResourcesResponse() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.search_resources( + location="location_value", + query="query_value", ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.ExternalApi.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - - client.update_external_api(**mock_args) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{external_api.name=projects/*/locations/*/externalApis/*}" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].location + mock_val = "location_value" + assert arg == mock_val + arg = args[0].query + mock_val = "query_value" + assert arg == mock_val -def test_update_external_api_rest_flattened_error(transport: str = "rest"): +def test_search_resources_flattened_error(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.update_external_api( - apihub_service.UpdateExternalApiRequest(), - external_api=common_fields.ExternalApi(name="name_value"), - update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + client.search_resources( + apihub_service.SearchResourcesRequest(), + location="location_value", + query="query_value", ) -def test_delete_external_api_rest_use_cached_wrapped_rpc(): - # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, - # instead of constructing them on each call - with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) +@pytest.mark.asyncio +async def test_search_resources_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) - # Should wrap all calls on client creation - assert wrapper_fn.call_count > 0 - wrapper_fn.reset_mock() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.search_resources), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.SearchResourcesResponse() - # Ensure method has been cached - assert ( - client._transport.delete_external_api in client._transport._wrapped_methods + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.SearchResourcesResponse() ) - - # Replace cached wrapped function with mock - mock_rpc = mock.Mock() - mock_rpc.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.search_resources( + location="location_value", + query="query_value", ) - client._transport._wrapped_methods[ - client._transport.delete_external_api - ] = mock_rpc - - request = {} - client.delete_external_api(request) - - # Establish that the underlying gRPC stub method was called. - assert mock_rpc.call_count == 1 - - client.delete_external_api(request) - - # Establish that a new wrapper was not created for this call - assert wrapper_fn.call_count == 0 - assert mock_rpc.call_count == 2 + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].location + mock_val = "location_value" + assert arg == mock_val + arg = args[0].query + mock_val = "query_value" + assert arg == mock_val -def test_delete_external_api_rest_required_fields( - request_type=apihub_service.DeleteExternalApiRequest, -): - transport_class = transports.ApiHubRestTransport - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) +@pytest.mark.asyncio +async def test_search_resources_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - # verify fields with default values are dropped - - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_external_api._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - - # verify required fields with default values are now present - - jsonified_request["name"] = "name_value" - - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_external_api._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.search_resources( + apihub_service.SearchResourcesRequest(), + location="location_value", + query="query_value", + ) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" +def test_search_resources_pager(transport_name: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport_name, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = None - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "delete", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.search_resources), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.SearchResourcesResponse( + search_results=[ + apihub_service.SearchResult(), + apihub_service.SearchResult(), + apihub_service.SearchResult(), + ], + next_page_token="abc", + ), + apihub_service.SearchResourcesResponse( + search_results=[], + next_page_token="def", + ), + apihub_service.SearchResourcesResponse( + search_results=[ + apihub_service.SearchResult(), + ], + next_page_token="ghi", + ), + apihub_service.SearchResourcesResponse( + search_results=[ + apihub_service.SearchResult(), + apihub_service.SearchResult(), + ], + ), + RuntimeError, + ) - response_value = Response() - response_value.status_code = 200 - json_return_value = "" + expected_metadata = () + retry = retries.Retry() + timeout = 5 + expected_metadata = tuple(expected_metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("location", ""),)), + ) + pager = client.search_resources(request={}, retry=retry, timeout=timeout) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + assert pager._metadata == expected_metadata + assert pager._retry == retry + assert pager._timeout == timeout - response = client.delete_external_api(request) + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, apihub_service.SearchResult) for i in results) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +def test_search_resources_pages(transport_name: str = "grpc"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport_name, + ) -def test_delete_external_api_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.search_resources), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.SearchResourcesResponse( + search_results=[ + apihub_service.SearchResult(), + apihub_service.SearchResult(), + apihub_service.SearchResult(), + ], + next_page_token="abc", + ), + apihub_service.SearchResourcesResponse( + search_results=[], + next_page_token="def", + ), + apihub_service.SearchResourcesResponse( + search_results=[ + apihub_service.SearchResult(), + ], + next_page_token="ghi", + ), + apihub_service.SearchResourcesResponse( + search_results=[ + apihub_service.SearchResult(), + apihub_service.SearchResult(), + ], + ), + RuntimeError, + ) + pages = list(client.search_resources(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.asyncio +async def test_search_resources_async_pager(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.delete_external_api._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.search_resources), "__call__", new_callable=mock.AsyncMock + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.SearchResourcesResponse( + search_results=[ + apihub_service.SearchResult(), + apihub_service.SearchResult(), + apihub_service.SearchResult(), + ], + next_page_token="abc", + ), + apihub_service.SearchResourcesResponse( + search_results=[], + next_page_token="def", + ), + apihub_service.SearchResourcesResponse( + search_results=[ + apihub_service.SearchResult(), + ], + next_page_token="ghi", + ), + apihub_service.SearchResourcesResponse( + search_results=[ + apihub_service.SearchResult(), + apihub_service.SearchResult(), + ], + ), + RuntimeError, + ) + async_pager = await client.search_resources( + request={}, + ) + assert async_pager.next_page_token == "abc" + responses = [] + async for response in async_pager: # pragma: no branch + responses.append(response) + assert len(responses) == 6 + assert all(isinstance(i, apihub_service.SearchResult) for i in responses) -def test_delete_external_api_rest_flattened(): + +@pytest.mark.asyncio +async def test_search_resources_async_pages(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.search_resources), "__call__", new_callable=mock.AsyncMock + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.SearchResourcesResponse( + search_results=[ + apihub_service.SearchResult(), + apihub_service.SearchResult(), + apihub_service.SearchResult(), + ], + next_page_token="abc", + ), + apihub_service.SearchResourcesResponse( + search_results=[], + next_page_token="def", + ), + apihub_service.SearchResourcesResponse( + search_results=[ + apihub_service.SearchResult(), + ], + next_page_token="ghi", + ), + apihub_service.SearchResourcesResponse( + search_results=[ + apihub_service.SearchResult(), + apihub_service.SearchResult(), + ], + ), + RuntimeError, + ) + pages = [] + # Workaround issue in python 3.9 related to code coverage by adding `# pragma: no branch` + # See https://github.com/googleapis/gapic-generator-python/pull/1174#issuecomment-1025132372 + async for page_ in ( # pragma: no branch + await client.search_resources(request={}) + ).pages: + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.CreateExternalApiRequest, + dict, + ], +) +def test_create_external_api(request_type, transport: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = None - - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/externalApis/sample3" - } + # 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() - # get truthy value for each flattened field - mock_args = dict( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_external_api), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ExternalApi( name="name_value", + display_name="display_name_value", + description="description_value", + endpoints=["endpoints_value"], + paths=["paths_value"], ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - json_return_value = "" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.create_external_api(request) - client.delete_external_api(**mock_args) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.CreateExternalApiRequest() + assert args[0] == request - # Establish that the underlying call was made with the expected - # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/externalApis/*}" - % client.transport._host, - args[1], - ) + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.ExternalApi) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.endpoints == ["endpoints_value"] + assert response.paths == ["paths_value"] -def test_delete_external_api_rest_flattened_error(transport: str = "rest"): +def test_create_external_api_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport="grpc", ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.delete_external_api( - apihub_service.DeleteExternalApiRequest(), - name="name_value", + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.CreateExternalApiRequest( + parent="parent_value", + external_api_id="external_api_id_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_external_api), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.create_external_api(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.CreateExternalApiRequest( + parent="parent_value", + external_api_id="external_api_id_value", ) -def test_list_external_apis_rest_use_cached_wrapped_rpc(): +def test_create_external_api_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -8281,7 +13818,7 @@ def test_list_external_apis_rest_use_cached_wrapped_rpc(): # Ensure method has been cached assert ( - client._transport.list_external_apis in client._transport._wrapped_methods + client._transport.create_external_api in client._transport._wrapped_methods ) # Replace cached wrapped function with mock @@ -8290,789 +13827,12393 @@ def test_list_external_apis_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[ - client._transport.list_external_apis + client._transport.create_external_api ] = mock_rpc - request = {} - client.list_external_apis(request) + client.create_external_api(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.list_external_apis(request) + client.create_external_api(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_list_external_apis_rest_required_fields( - request_type=apihub_service.ListExternalApisRequest, +@pytest.mark.asyncio +async def test_create_external_api_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.create_external_api + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_external_apis._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.create_external_api + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.create_external_api(request) - jsonified_request["parent"] = "parent_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_external_apis._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "page_size", - "page_token", - ) + await client.create_external_api(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_create_external_api_async( + transport: str = "grpc_asyncio", + request_type=apihub_service.CreateExternalApiRequest, +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + # 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.create_external_api), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ExternalApi( + name="name_value", + display_name="display_name_value", + description="description_value", + endpoints=["endpoints_value"], + paths=["paths_value"], + ) + ) + response = await client.create_external_api(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.CreateExternalApiRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.ExternalApi) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.endpoints == ["endpoints_value"] + assert response.paths == ["paths_value"] + + +@pytest.mark.asyncio +async def test_create_external_api_async_from_dict(): + await test_create_external_api_async(request_type=dict) + + +def test_create_external_api_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = apihub_service.ListExternalApisResponse() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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 = apihub_service.CreateExternalApiRequest() - # Convert return value to protobuf type - return_value = apihub_service.ListExternalApisResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.parent = "parent_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_external_api), "__call__" + ) as call: + call.return_value = common_fields.ExternalApi() + client.create_external_api(request) - response = client.list_external_apis(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] -def test_list_external_apis_rest_unset_required_fields(): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_create_external_api_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.list_external_apis._get_unset_required_fields({}) - assert set(unset_fields) == ( - set( - ( - "pageSize", - "pageToken", - ) + # 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 = apihub_service.CreateExternalApiRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_external_api), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ExternalApi() ) - & set(("parent",)) - ) + await client.create_external_api(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request -def test_list_external_apis_rest_flattened(): + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +def test_create_external_api_flattened(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = apihub_service.ListExternalApisResponse() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_external_api), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ExternalApi() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.create_external_api( + parent="parent_value", + external_api=common_fields.ExternalApi(name="name_value"), + external_api_id="external_api_id_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].external_api + mock_val = common_fields.ExternalApi(name="name_value") + assert arg == mock_val + arg = args[0].external_api_id + mock_val = "external_api_id_value" + assert arg == mock_val + + +def test_create_external_api_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) - # get truthy value for each flattened field - mock_args = dict( + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_external_api( + apihub_service.CreateExternalApiRequest(), parent="parent_value", + external_api=common_fields.ExternalApi(name="name_value"), + external_api_id="external_api_id_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = apihub_service.ListExternalApisResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_external_apis(**mock_args) +@pytest.mark.asyncio +async def test_create_external_api_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_external_api), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ExternalApi() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ExternalApi() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.create_external_api( + parent="parent_value", + external_api=common_fields.ExternalApi(name="name_value"), + external_api_id="external_api_id_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/externalApis" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].external_api + mock_val = common_fields.ExternalApi(name="name_value") + assert arg == mock_val + arg = args[0].external_api_id + mock_val = "external_api_id_value" + assert arg == mock_val -def test_list_external_apis_rest_flattened_error(transport: str = "rest"): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_create_external_api_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.list_external_apis( - apihub_service.ListExternalApisRequest(), + await client.create_external_api( + apihub_service.CreateExternalApiRequest(), parent="parent_value", + external_api=common_fields.ExternalApi(name="name_value"), + external_api_id="external_api_id_value", ) -def test_list_external_apis_rest_pager(transport: str = "rest"): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.GetExternalApiRequest, + dict, + ], +) +def test_get_external_api(request_type, transport: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # TODO(kbandes): remove this mock unless there's a good reason for it. - # with mock.patch.object(path_template, 'transcode') as transcode: - # Set the response as a series of pages - response = ( - apihub_service.ListExternalApisResponse( - external_apis=[ - common_fields.ExternalApi(), - common_fields.ExternalApi(), - common_fields.ExternalApi(), - ], - next_page_token="abc", - ), - apihub_service.ListExternalApisResponse( - external_apis=[], - next_page_token="def", - ), - apihub_service.ListExternalApisResponse( - external_apis=[ - common_fields.ExternalApi(), - ], - next_page_token="ghi", - ), - apihub_service.ListExternalApisResponse( - external_apis=[ - common_fields.ExternalApi(), - common_fields.ExternalApi(), - ], - ), - ) - # Two responses for two calls - response = response + response + # 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() - # Wrap the values into proper Response objs - response = tuple( - apihub_service.ListExternalApisResponse.to_json(x) for x in response + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_external_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ExternalApi( + name="name_value", + display_name="display_name_value", + description="description_value", + endpoints=["endpoints_value"], + paths=["paths_value"], ) - return_values = tuple(Response() for i in response) - for return_val, response_val in zip(return_values, response): - return_val._content = response_val.encode("UTF-8") - return_val.status_code = 200 - req.side_effect = return_values + response = client.get_external_api(request) - sample_request = {"parent": "projects/sample1/locations/sample2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.GetExternalApiRequest() + assert args[0] == request - pager = client.list_external_apis(request=sample_request) + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.ExternalApi) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.endpoints == ["endpoints_value"] + assert response.paths == ["paths_value"] - results = list(pager) - assert len(results) == 6 - assert all(isinstance(i, common_fields.ExternalApi) for i in results) - pages = list(client.list_external_apis(request=sample_request).pages) - for page_, token in zip(pages, ["abc", "def", "ghi", ""]): - assert page_.raw_page.next_page_token == token - - -def test_credentials_transport_error(): - # It is an error to provide credentials and a transport instance. - transport = transports.ApiHubRestTransport( +def test_get_external_api_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", ) - with pytest.raises(ValueError): + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.GetExternalApiRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_external_api), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_external_api(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.GetExternalApiRequest( + name="name_value", + ) + + +def test_get_external_api_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.get_external_api in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.get_external_api + ] = mock_rpc + request = {} + client.get_external_api(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_external_api(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_get_external_api_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), transport=transport, ) - # It is an error to provide a credentials file and a transport instance. - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials(), + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.get_external_api + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.get_external_api + ] = mock_rpc + + request = {} + await client.get_external_api(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.get_external_api(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_get_external_api_async( + transport: str = "grpc_asyncio", request_type=apihub_service.GetExternalApiRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - with pytest.raises(ValueError): - client = ApiHubClient( - client_options={"credentials_file": "credentials.json"}, - 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.get_external_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ExternalApi( + name="name_value", + display_name="display_name_value", + description="description_value", + endpoints=["endpoints_value"], + paths=["paths_value"], + ) ) + response = await client.get_external_api(request) - # It is an error to provide an api_key and a transport instance. - transport = transports.ApiHubRestTransport( + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.GetExternalApiRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.ExternalApi) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.endpoints == ["endpoints_value"] + assert response.paths == ["paths_value"] + + +@pytest.mark.asyncio +async def test_get_external_api_async_from_dict(): + await test_get_external_api_async(request_type=dict) + + +def test_get_external_api_field_headers(): + client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), ) - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = ApiHubClient( - client_options=options, - transport=transport, - ) - # It is an error to provide an api_key and a credential. - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = ApiHubClient( - client_options=options, credentials=ga_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 = apihub_service.GetExternalApiRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_external_api), "__call__") as call: + call.return_value = common_fields.ExternalApi() + client.get_external_api(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", + "name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_get_external_api_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = apihub_service.GetExternalApiRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_external_api), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ExternalApi() ) + await client.get_external_api(request) - # It is an error to provide scopes and a transport instance. - transport = transports.ApiHubRestTransport( + # 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", + "name=name_value", + ) in kw["metadata"] + + +def test_get_external_api_flattened(): + client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), ) - with pytest.raises(ValueError): - client = ApiHubClient( - client_options={"scopes": ["1", "2"]}, - transport=transport, + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_external_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ExternalApi() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_external_api( + name="name_value", ) + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + -def test_transport_instance(): - # A client may be instantiated with a custom transport instance. - transport = transports.ApiHubRestTransport( +def test_get_external_api_flattened_error(): + client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), ) - client = ApiHubClient(transport=transport) - assert client.transport is transport - -@pytest.mark.parametrize( - "transport_class", - [ - transports.ApiHubRestTransport, - ], -) -def test_transport_adc(transport_class): - # Test default credentials are used if not provided. - with mock.patch.object(google.auth, "default") as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport_class() - adc.assert_called_once() + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_external_api( + apihub_service.GetExternalApiRequest(), + name="name_value", + ) -def test_transport_kind_rest(): - transport = ApiHubClient.get_transport_class("rest")( - credentials=ga_credentials.AnonymousCredentials() +@pytest.mark.asyncio +async def test_get_external_api_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - assert transport.kind == "rest" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_external_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ExternalApi() -def test_create_api_rest_bad_request(request_type=apihub_service.CreateApiRequest): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ExternalApi() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_external_api( + name="name_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_get_external_api_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_api(request) + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.get_external_api( + apihub_service.GetExternalApiRequest(), + name="name_value", + ) @pytest.mark.parametrize( "request_type", [ - apihub_service.CreateApiRequest, + apihub_service.UpdateExternalApiRequest, dict, ], ) -def test_create_api_rest_call_success(request_type): +def test_update_external_api(request_type, transport: str = "grpc"): client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request_init["api"] = { - "name": "name_value", - "display_name": "display_name_value", - "description": "description_value", - "documentation": {"external_uri": "external_uri_value"}, - "owner": {"display_name": "display_name_value", "email": "email_value"}, - "versions": ["versions_value1", "versions_value2"], - "create_time": {"seconds": 751, "nanos": 543}, - "update_time": {}, - "target_user": { - "enum_values": { - "values": [ - { - "id": "id_value", - "display_name": "display_name_value", - "description": "description_value", - "immutable": True, - } - ] - }, - "string_values": {"values": ["values_value1", "values_value2"]}, - "json_values": {}, - "uri_values": {}, - "attribute": "attribute_value", - }, - "team": {}, - "business_unit": {}, - "maturity_level": {}, - "attributes": {}, - "api_style": {}, - "selected_version": "selected_version_value", - "api_requirements": {}, - "fingerprint": "fingerprint_value", - "source_metadata": [ - { - "plugin_instance_action_source": { - "plugin_instance": "plugin_instance_value", - "action_id": "action_id_value", - }, - "source_type": 1, - "original_resource_id": "original_resource_id_value", - "original_resource_create_time": {}, - "original_resource_update_time": {}, - } - ], - "api_functional_requirements": {}, - "api_technical_requirements": {}, - } - # The version of a generated dependency at test runtime may differ from the version used during generation. - # Delete any fields which are not present in the current runtime dependency - # See https://github.com/googleapis/gapic-generator-python/issues/1748 - - # Determine if the message type is proto-plus or protobuf - test_field = apihub_service.CreateApiRequest.meta.fields["api"] - - def get_message_fields(field): - # Given a field which is a message (composite type), return a list with - # all the fields of the message. - # If the field is not a composite type, return an empty list. - message_fields = [] - - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") - - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields - - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] - - subfields_not_in_runtime = [] - - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["api"].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value - - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) - - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range(0, len(request_init["api"][field])): - del request_init["api"][field][i][subfield] - else: - del request_init["api"][field][subfield] - request = request_type(**request_init) + # 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 http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Api( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_external_api), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ExternalApi( name="name_value", display_name="display_name_value", description="description_value", - versions=["versions_value"], - selected_version="selected_version_value", - fingerprint="fingerprint_value", + endpoints=["endpoints_value"], + paths=["paths_value"], ) + response = client.update_external_api(request) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = common_fields.Api.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.create_api(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.UpdateExternalApiRequest() + assert args[0] == request # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.Api) + assert isinstance(response, common_fields.ExternalApi) assert response.name == "name_value" assert response.display_name == "display_name_value" assert response.description == "description_value" - assert response.versions == ["versions_value"] - assert response.selected_version == "selected_version_value" - assert response.fingerprint == "fingerprint_value" + assert response.endpoints == ["endpoints_value"] + assert response.paths == ["paths_value"] -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_create_api_rest_interceptors(null_interceptor): - transport = transports.ApiHubRestTransport( +def test_update_external_api_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), + transport="grpc", ) - client = ApiHubClient(transport=transport) + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.UpdateExternalApiRequest() + + # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_create_api" - ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_create_api_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_create_api" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = apihub_service.CreateApiRequest.pb( - apihub_service.CreateApiRequest() + type(client.transport.update_external_api), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } + client.update_external_api(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.UpdateExternalApiRequest() - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.Api.to_json(common_fields.Api()) - req.return_value.content = return_value - request = apihub_service.CreateApiRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = common_fields.Api() - post_with_metadata.return_value = common_fields.Api(), metadata +def test_update_external_api_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) - client.create_api( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.update_external_api in client._transport._wrapped_methods ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.update_external_api + ] = mock_rpc + request = {} + client.update_external_api(request) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 -def test_get_api_rest_bad_request(request_type=apihub_service.GetApiRequest): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/apis/sample3"} - request = request_type(**request_init) + client.update_external_api(request) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_api(request) + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 -@pytest.mark.parametrize( - "request_type", - [ - apihub_service.GetApiRequest, - dict, - ], -) -def test_get_api_rest_call_success(request_type): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) +@pytest.mark.asyncio +async def test_update_external_api_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/apis/sample3"} - request = request_type(**request_init) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Api( - name="name_value", - display_name="display_name_value", - description="description_value", - versions=["versions_value"], - selected_version="selected_version_value", - fingerprint="fingerprint_value", + # Ensure method has been cached + assert ( + client._client._transport.update_external_api + in client._client._transport._wrapped_methods ) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.update_external_api + ] = mock_rpc - # Convert return value to protobuf type - return_value = common_fields.Api.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_api(request) + request = {} + await client.update_external_api(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.update_external_api(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_update_external_api_async( + transport: str = "grpc_asyncio", + request_type=apihub_service.UpdateExternalApiRequest, +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + 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.update_external_api), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ExternalApi( + name="name_value", + display_name="display_name_value", + description="description_value", + endpoints=["endpoints_value"], + paths=["paths_value"], + ) + ) + response = await client.update_external_api(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.UpdateExternalApiRequest() + assert args[0] == request # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.Api) + assert isinstance(response, common_fields.ExternalApi) assert response.name == "name_value" assert response.display_name == "display_name_value" assert response.description == "description_value" - assert response.versions == ["versions_value"] - assert response.selected_version == "selected_version_value" - assert response.fingerprint == "fingerprint_value" + assert response.endpoints == ["endpoints_value"] + assert response.paths == ["paths_value"] -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_api_rest_interceptors(null_interceptor): - transport = transports.ApiHubRestTransport( +@pytest.mark.asyncio +async def test_update_external_api_async_from_dict(): + await test_update_external_api_async(request_type=dict) + + +def test_update_external_api_field_headers(): + client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), ) - client = ApiHubClient(transport=transport) + # 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 = apihub_service.UpdateExternalApiRequest() + + request.external_api.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_get_api" - ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_get_api_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_get_api" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = apihub_service.GetApiRequest.pb(apihub_service.GetApiRequest()) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } + type(client.transport.update_external_api), "__call__" + ) as call: + call.return_value = common_fields.ExternalApi() + client.update_external_api(request) - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.Api.to_json(common_fields.Api()) - req.return_value.content = return_value + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request - request = apihub_service.GetApiRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = common_fields.Api() - post_with_metadata.return_value = common_fields.Api(), metadata + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "external_api.name=name_value", + ) in kw["metadata"] - client.get_api( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + +@pytest.mark.asyncio +async def test_update_external_api_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = apihub_service.UpdateExternalApiRequest() + + request.external_api.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_external_api), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ExternalApi() ) + await client.update_external_api(request) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # 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", + "external_api.name=name_value", + ) in kw["metadata"] -def test_list_apis_rest_bad_request(request_type=apihub_service.ListApisRequest): + +def test_update_external_api_flattened(): client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_apis(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_external_api), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ExternalApi() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.update_external_api( + external_api=common_fields.ExternalApi(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].external_api + mock_val = common_fields.ExternalApi(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val + + +def test_update_external_api_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_external_api( + apihub_service.UpdateExternalApiRequest(), + external_api=common_fields.ExternalApi(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +@pytest.mark.asyncio +async def test_update_external_api_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_external_api), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ExternalApi() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ExternalApi() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.update_external_api( + external_api=common_fields.ExternalApi(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].external_api + mock_val = common_fields.ExternalApi(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_update_external_api_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.update_external_api( + apihub_service.UpdateExternalApiRequest(), + external_api=common_fields.ExternalApi(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) @pytest.mark.parametrize( "request_type", [ - apihub_service.ListApisRequest, + apihub_service.DeleteExternalApiRequest, dict, ], ) -def test_list_apis_rest_call_success(request_type): +def test_delete_external_api(request_type, transport: str = "grpc"): client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = apihub_service.ListApisResponse( - next_page_token="next_page_token_value", - ) + # 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() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_external_api), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.delete_external_api(request) - # Convert return value to protobuf type - return_value = apihub_service.ListApisResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.list_apis(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.DeleteExternalApiRequest() + assert args[0] == request # Establish that the response is the type that we expect. - assert isinstance(response, pagers.ListApisPager) - assert response.next_page_token == "next_page_token_value" + assert response is None -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_list_apis_rest_interceptors(null_interceptor): - transport = transports.ApiHubRestTransport( +def test_delete_external_api_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.DeleteExternalApiRequest( + name="name_value", ) - client = ApiHubClient(transport=transport) + # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_list_apis" - ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_list_apis_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_list_apis" - ) as pre: - pre.assert_not_called() - post.assert_not_called() + type(client.transport.delete_external_api), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.delete_external_api(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.DeleteExternalApiRequest( + name="name_value", + ) + + +def test_delete_external_api_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.delete_external_api in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.delete_external_api + ] = mock_rpc + request = {} + client.delete_external_api(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.delete_external_api(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_delete_external_api_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.delete_external_api + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.delete_external_api + ] = mock_rpc + + request = {} + await client.delete_external_api(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.delete_external_api(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_delete_external_api_async( + transport: str = "grpc_asyncio", + request_type=apihub_service.DeleteExternalApiRequest, +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + 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.delete_external_api), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_external_api(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.DeleteExternalApiRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert response is None + + +@pytest.mark.asyncio +async def test_delete_external_api_async_from_dict(): + await test_delete_external_api_async(request_type=dict) + + +def test_delete_external_api_field_headers(): + client = ApiHubClient( + credentials=ga_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 = apihub_service.DeleteExternalApiRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_external_api), "__call__" + ) as call: + call.return_value = None + client.delete_external_api(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", + "name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_delete_external_api_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = apihub_service.DeleteExternalApiRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_external_api), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_external_api(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", + "name=name_value", + ) in kw["metadata"] + + +def test_delete_external_api_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_external_api), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = None + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.delete_external_api( + name="name_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +def test_delete_external_api_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_external_api( + apihub_service.DeleteExternalApiRequest(), + name="name_value", + ) + + +@pytest.mark.asyncio +async def test_delete_external_api_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_external_api), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = None + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.delete_external_api( + name="name_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_delete_external_api_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.delete_external_api( + apihub_service.DeleteExternalApiRequest(), + name="name_value", + ) + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.ListExternalApisRequest, + dict, + ], +) +def test_list_external_apis(request_type, transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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.list_external_apis), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListExternalApisResponse( + next_page_token="next_page_token_value", + ) + response = client.list_external_apis(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.ListExternalApisRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListExternalApisPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_external_apis_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.ListExternalApisRequest( + parent="parent_value", + page_token="page_token_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_external_apis), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.list_external_apis(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.ListExternalApisRequest( + parent="parent_value", + page_token="page_token_value", + ) + + +def test_list_external_apis_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.list_external_apis in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.list_external_apis + ] = mock_rpc + request = {} + client.list_external_apis(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.list_external_apis(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_list_external_apis_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.list_external_apis + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.list_external_apis + ] = mock_rpc + + request = {} + await client.list_external_apis(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.list_external_apis(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_list_external_apis_async( + transport: str = "grpc_asyncio", request_type=apihub_service.ListExternalApisRequest +): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + 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.list_external_apis), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListExternalApisResponse( + next_page_token="next_page_token_value", + ) + ) + response = await client.list_external_apis(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.ListExternalApisRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListExternalApisAsyncPager) + assert response.next_page_token == "next_page_token_value" + + +@pytest.mark.asyncio +async def test_list_external_apis_async_from_dict(): + await test_list_external_apis_async(request_type=dict) + + +def test_list_external_apis_field_headers(): + client = ApiHubClient( + credentials=ga_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 = apihub_service.ListExternalApisRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_external_apis), "__call__" + ) as call: + call.return_value = apihub_service.ListExternalApisResponse() + client.list_external_apis(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", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_list_external_apis_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = apihub_service.ListExternalApisRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_external_apis), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListExternalApisResponse() + ) + await client.list_external_apis(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", + "parent=parent_value", + ) in kw["metadata"] + + +def test_list_external_apis_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_external_apis), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListExternalApisResponse() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.list_external_apis( + parent="parent_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + + +def test_list_external_apis_flattened_error(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_external_apis( + apihub_service.ListExternalApisRequest(), + parent="parent_value", + ) + + +@pytest.mark.asyncio +async def test_list_external_apis_flattened_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_external_apis), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListExternalApisResponse() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListExternalApisResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.list_external_apis( + parent="parent_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_list_external_apis_flattened_error_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.list_external_apis( + apihub_service.ListExternalApisRequest(), + parent="parent_value", + ) + + +def test_list_external_apis_pager(transport_name: str = "grpc"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport_name, + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_external_apis), "__call__" + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListExternalApisResponse( + external_apis=[ + common_fields.ExternalApi(), + common_fields.ExternalApi(), + common_fields.ExternalApi(), + ], + next_page_token="abc", + ), + apihub_service.ListExternalApisResponse( + external_apis=[], + next_page_token="def", + ), + apihub_service.ListExternalApisResponse( + external_apis=[ + common_fields.ExternalApi(), + ], + next_page_token="ghi", + ), + apihub_service.ListExternalApisResponse( + external_apis=[ + common_fields.ExternalApi(), + common_fields.ExternalApi(), + ], + ), + RuntimeError, + ) + + expected_metadata = () + retry = retries.Retry() + timeout = 5 + expected_metadata = tuple(expected_metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", ""),)), + ) + pager = client.list_external_apis(request={}, retry=retry, timeout=timeout) + + assert pager._metadata == expected_metadata + assert pager._retry == retry + assert pager._timeout == timeout + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, common_fields.ExternalApi) for i in results) + + +def test_list_external_apis_pages(transport_name: str = "grpc"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport_name, + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_external_apis), "__call__" + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListExternalApisResponse( + external_apis=[ + common_fields.ExternalApi(), + common_fields.ExternalApi(), + common_fields.ExternalApi(), + ], + next_page_token="abc", + ), + apihub_service.ListExternalApisResponse( + external_apis=[], + next_page_token="def", + ), + apihub_service.ListExternalApisResponse( + external_apis=[ + common_fields.ExternalApi(), + ], + next_page_token="ghi", + ), + apihub_service.ListExternalApisResponse( + external_apis=[ + common_fields.ExternalApi(), + common_fields.ExternalApi(), + ], + ), + RuntimeError, + ) + pages = list(client.list_external_apis(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.asyncio +async def test_list_external_apis_async_pager(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_external_apis), + "__call__", + new_callable=mock.AsyncMock, + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListExternalApisResponse( + external_apis=[ + common_fields.ExternalApi(), + common_fields.ExternalApi(), + common_fields.ExternalApi(), + ], + next_page_token="abc", + ), + apihub_service.ListExternalApisResponse( + external_apis=[], + next_page_token="def", + ), + apihub_service.ListExternalApisResponse( + external_apis=[ + common_fields.ExternalApi(), + ], + next_page_token="ghi", + ), + apihub_service.ListExternalApisResponse( + external_apis=[ + common_fields.ExternalApi(), + common_fields.ExternalApi(), + ], + ), + RuntimeError, + ) + async_pager = await client.list_external_apis( + request={}, + ) + assert async_pager.next_page_token == "abc" + responses = [] + async for response in async_pager: # pragma: no branch + responses.append(response) + + assert len(responses) == 6 + assert all(isinstance(i, common_fields.ExternalApi) for i in responses) + + +@pytest.mark.asyncio +async def test_list_external_apis_async_pages(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_external_apis), + "__call__", + new_callable=mock.AsyncMock, + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListExternalApisResponse( + external_apis=[ + common_fields.ExternalApi(), + common_fields.ExternalApi(), + common_fields.ExternalApi(), + ], + next_page_token="abc", + ), + apihub_service.ListExternalApisResponse( + external_apis=[], + next_page_token="def", + ), + apihub_service.ListExternalApisResponse( + external_apis=[ + common_fields.ExternalApi(), + ], + next_page_token="ghi", + ), + apihub_service.ListExternalApisResponse( + external_apis=[ + common_fields.ExternalApi(), + common_fields.ExternalApi(), + ], + ), + RuntimeError, + ) + pages = [] + # Workaround issue in python 3.9 related to code coverage by adding `# pragma: no branch` + # See https://github.com/googleapis/gapic-generator-python/pull/1174#issuecomment-1025132372 + async for page_ in ( # pragma: no branch + await client.list_external_apis(request={}) + ).pages: + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +def test_create_api_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.create_api in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.create_api] = mock_rpc + + request = {} + client.create_api(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.create_api(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_create_api_rest_required_fields(request_type=apihub_service.CreateApiRequest): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_api._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_api._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("api_id",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.Api() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Api.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.create_api(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_api_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_api._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("apiId",)) + & set( + ( + "parent", + "api", + ) + ) + ) + + +def test_create_api_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Api() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + api=common_fields.Api(name="name_value"), + api_id="api_id_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.Api.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.create_api(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/apis" % client.transport._host, + args[1], + ) + + +def test_create_api_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_api( + apihub_service.CreateApiRequest(), + parent="parent_value", + api=common_fields.Api(name="name_value"), + api_id="api_id_value", + ) + + +def test_get_api_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.get_api in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.get_api] = mock_rpc + + request = {} + client.get_api(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_api(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_get_api_rest_required_fields(request_type=apihub_service.GetApiRequest): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_api._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_api._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.Api() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Api.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_api(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_api_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_api._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_get_api_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Api() + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/apis/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.Api.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.get_api(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/apis/*}" % client.transport._host, + args[1], + ) + + +def test_get_api_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_api( + apihub_service.GetApiRequest(), + name="name_value", + ) + + +def test_list_apis_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.list_apis in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.list_apis] = mock_rpc + + request = {} + client.list_apis(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.list_apis(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_list_apis_rest_required_fields(request_type=apihub_service.ListApisRequest): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_apis._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_apis._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = apihub_service.ListApisResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = apihub_service.ListApisResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_apis(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_apis_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_apis._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +def test_list_apis_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = apihub_service.ListApisResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = apihub_service.ListApisResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.list_apis(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/apis" % client.transport._host, + args[1], + ) + + +def test_list_apis_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_apis( + apihub_service.ListApisRequest(), + parent="parent_value", + ) + + +def test_list_apis_rest_pager(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + apihub_service.ListApisResponse( + apis=[ + common_fields.Api(), + common_fields.Api(), + common_fields.Api(), + ], + next_page_token="abc", + ), + apihub_service.ListApisResponse( + apis=[], + next_page_token="def", + ), + apihub_service.ListApisResponse( + apis=[ + common_fields.Api(), + ], + next_page_token="ghi", + ), + apihub_service.ListApisResponse( + apis=[ + common_fields.Api(), + common_fields.Api(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(apihub_service.ListApisResponse.to_json(x) for x in response) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2"} + + pager = client.list_apis(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, common_fields.Api) for i in results) + + pages = list(client.list_apis(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +def test_update_api_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.update_api in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.update_api] = mock_rpc + + request = {} + client.update_api(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.update_api(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_update_api_rest_required_fields(request_type=apihub_service.UpdateApiRequest): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_api._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_api._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("update_mask",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.Api() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Api.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.update_api(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_api_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.update_api._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("updateMask",)) + & set( + ( + "api", + "updateMask", + ) + ) + ) + + +def test_update_api_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Api() + + # get arguments that satisfy an http rule for this method + sample_request = { + "api": {"name": "projects/sample1/locations/sample2/apis/sample3"} + } + + # get truthy value for each flattened field + mock_args = dict( + api=common_fields.Api(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.Api.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.update_api(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{api.name=projects/*/locations/*/apis/*}" % client.transport._host, + args[1], + ) + + +def test_update_api_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_api( + apihub_service.UpdateApiRequest(), + api=common_fields.Api(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +def test_delete_api_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.delete_api in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.delete_api] = mock_rpc + + request = {} + client.delete_api(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.delete_api(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_delete_api_rest_required_fields(request_type=apihub_service.DeleteApiRequest): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_api._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_api._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("force",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = None + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_api(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_api_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_api._get_unset_required_fields({}) + assert set(unset_fields) == (set(("force",)) & set(("name",))) + + +def test_delete_api_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/apis/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.delete_api(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/apis/*}" % client.transport._host, + args[1], + ) + + +def test_delete_api_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_api( + apihub_service.DeleteApiRequest(), + name="name_value", + ) + + +def test_create_version_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.create_version in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.create_version] = mock_rpc + + request = {} + client.create_version(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.create_version(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_create_version_rest_required_fields( + request_type=apihub_service.CreateVersionRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_version._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_version._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("version_id",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.Version() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Version.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.create_version(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_version_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_version._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("versionId",)) + & set( + ( + "parent", + "version", + ) + ) + ) + + +def test_create_version_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Version() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2/apis/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + version=common_fields.Version(name="name_value"), + version_id="version_id_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.Version.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.create_version(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/apis/*}/versions" + % client.transport._host, + args[1], + ) + + +def test_create_version_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_version( + apihub_service.CreateVersionRequest(), + parent="parent_value", + version=common_fields.Version(name="name_value"), + version_id="version_id_value", + ) + + +def test_get_version_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.get_version in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.get_version] = mock_rpc + + request = {} + client.get_version(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_version(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_get_version_rest_required_fields( + request_type=apihub_service.GetVersionRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_version._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_version._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.Version() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Version.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_version(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_version_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_version._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_get_version_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Version() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.Version.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.get_version(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/apis/*/versions/*}" + % client.transport._host, + args[1], + ) + + +def test_get_version_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_version( + apihub_service.GetVersionRequest(), + name="name_value", + ) + + +def test_list_versions_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.list_versions in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.list_versions] = mock_rpc + + request = {} + client.list_versions(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.list_versions(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_list_versions_rest_required_fields( + request_type=apihub_service.ListVersionsRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_versions._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_versions._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = apihub_service.ListVersionsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = apihub_service.ListVersionsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_versions(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_versions_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_versions._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +def test_list_versions_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = apihub_service.ListVersionsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2/apis/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = apihub_service.ListVersionsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.list_versions(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/apis/*}/versions" + % client.transport._host, + args[1], + ) + + +def test_list_versions_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_versions( + apihub_service.ListVersionsRequest(), + parent="parent_value", + ) + + +def test_list_versions_rest_pager(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + apihub_service.ListVersionsResponse( + versions=[ + common_fields.Version(), + common_fields.Version(), + common_fields.Version(), + ], + next_page_token="abc", + ), + apihub_service.ListVersionsResponse( + versions=[], + next_page_token="def", + ), + apihub_service.ListVersionsResponse( + versions=[ + common_fields.Version(), + ], + next_page_token="ghi", + ), + apihub_service.ListVersionsResponse( + versions=[ + common_fields.Version(), + common_fields.Version(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + apihub_service.ListVersionsResponse.to_json(x) for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2/apis/sample3"} + + pager = client.list_versions(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, common_fields.Version) for i in results) + + pages = list(client.list_versions(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +def test_update_version_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.update_version in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.update_version] = mock_rpc + + request = {} + client.update_version(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.update_version(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_update_version_rest_required_fields( + request_type=apihub_service.UpdateVersionRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_version._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_version._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("update_mask",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.Version() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Version.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.update_version(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_version_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.update_version._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("updateMask",)) + & set( + ( + "version", + "updateMask", + ) + ) + ) + + +def test_update_version_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Version() + + # get arguments that satisfy an http rule for this method + sample_request = { + "version": { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + } + } + + # get truthy value for each flattened field + mock_args = dict( + version=common_fields.Version(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.Version.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.update_version(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{version.name=projects/*/locations/*/apis/*/versions/*}" + % client.transport._host, + args[1], + ) + + +def test_update_version_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_version( + apihub_service.UpdateVersionRequest(), + version=common_fields.Version(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +def test_delete_version_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.delete_version in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.delete_version] = mock_rpc + + request = {} + client.delete_version(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.delete_version(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_delete_version_rest_required_fields( + request_type=apihub_service.DeleteVersionRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_version._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_version._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("force",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = None + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_version(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_version_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_version._get_unset_required_fields({}) + assert set(unset_fields) == (set(("force",)) & set(("name",))) + + +def test_delete_version_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.delete_version(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/apis/*/versions/*}" + % client.transport._host, + args[1], + ) + + +def test_delete_version_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_version( + apihub_service.DeleteVersionRequest(), + name="name_value", + ) + + +def test_create_spec_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.create_spec in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.create_spec] = mock_rpc + + request = {} + client.create_spec(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.create_spec(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_create_spec_rest_required_fields( + request_type=apihub_service.CreateSpecRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_spec._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_spec._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("spec_id",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.Spec() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Spec.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.create_spec(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_spec_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_spec._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("specId",)) + & set( + ( + "parent", + "spec", + ) + ) + ) + + +def test_create_spec_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Spec() + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + spec=common_fields.Spec(name="name_value"), + spec_id="spec_id_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.Spec.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.create_spec(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/apis/*/versions/*}/specs" + % client.transport._host, + args[1], + ) + + +def test_create_spec_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_spec( + apihub_service.CreateSpecRequest(), + parent="parent_value", + spec=common_fields.Spec(name="name_value"), + spec_id="spec_id_value", + ) + + +def test_get_spec_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.get_spec in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.get_spec] = mock_rpc + + request = {} + client.get_spec(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_spec(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_get_spec_rest_required_fields(request_type=apihub_service.GetSpecRequest): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_spec._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_spec._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.Spec() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Spec.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_spec(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_spec_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_spec._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_get_spec_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Spec() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.Spec.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.get_spec(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/apis/*/versions/*/specs/*}" + % client.transport._host, + args[1], + ) + + +def test_get_spec_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_spec( + apihub_service.GetSpecRequest(), + name="name_value", + ) + + +def test_get_spec_contents_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.get_spec_contents in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.get_spec_contents + ] = mock_rpc + + request = {} + client.get_spec_contents(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_spec_contents(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_get_spec_contents_rest_required_fields( + request_type=apihub_service.GetSpecContentsRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_spec_contents._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_spec_contents._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.SpecContents() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.SpecContents.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_spec_contents(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_spec_contents_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_spec_contents._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_get_spec_contents_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.SpecContents() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.SpecContents.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.get_spec_contents(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/apis/*/versions/*/specs/*}:contents" + % client.transport._host, + args[1], + ) + + +def test_get_spec_contents_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_spec_contents( + apihub_service.GetSpecContentsRequest(), + name="name_value", + ) + + +def test_list_specs_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.list_specs in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.list_specs] = mock_rpc + + request = {} + client.list_specs(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.list_specs(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_list_specs_rest_required_fields(request_type=apihub_service.ListSpecsRequest): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_specs._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_specs._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = apihub_service.ListSpecsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = apihub_service.ListSpecsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_specs(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_specs_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_specs._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +def test_list_specs_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = apihub_service.ListSpecsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = apihub_service.ListSpecsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.list_specs(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/apis/*/versions/*}/specs" + % client.transport._host, + args[1], + ) + + +def test_list_specs_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_specs( + apihub_service.ListSpecsRequest(), + parent="parent_value", + ) + + +def test_list_specs_rest_pager(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + apihub_service.ListSpecsResponse( + specs=[ + common_fields.Spec(), + common_fields.Spec(), + common_fields.Spec(), + ], + next_page_token="abc", + ), + apihub_service.ListSpecsResponse( + specs=[], + next_page_token="def", + ), + apihub_service.ListSpecsResponse( + specs=[ + common_fields.Spec(), + ], + next_page_token="ghi", + ), + apihub_service.ListSpecsResponse( + specs=[ + common_fields.Spec(), + common_fields.Spec(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(apihub_service.ListSpecsResponse.to_json(x) for x in response) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = { + "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + } + + pager = client.list_specs(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, common_fields.Spec) for i in results) + + pages = list(client.list_specs(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +def test_update_spec_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.update_spec in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.update_spec] = mock_rpc + + request = {} + client.update_spec(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.update_spec(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_update_spec_rest_required_fields( + request_type=apihub_service.UpdateSpecRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_spec._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_spec._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("update_mask",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.Spec() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Spec.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.update_spec(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_spec_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.update_spec._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("updateMask",)) + & set( + ( + "spec", + "updateMask", + ) + ) + ) + + +def test_update_spec_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Spec() + + # get arguments that satisfy an http rule for this method + sample_request = { + "spec": { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" + } + } + + # get truthy value for each flattened field + mock_args = dict( + spec=common_fields.Spec(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.Spec.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.update_spec(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{spec.name=projects/*/locations/*/apis/*/versions/*/specs/*}" + % client.transport._host, + args[1], + ) + + +def test_update_spec_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_spec( + apihub_service.UpdateSpecRequest(), + spec=common_fields.Spec(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +def test_delete_spec_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.delete_spec in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.delete_spec] = mock_rpc + + request = {} + client.delete_spec(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.delete_spec(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_delete_spec_rest_required_fields( + request_type=apihub_service.DeleteSpecRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_spec._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_spec._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = None + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_spec(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_spec_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_spec._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_delete_spec_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.delete_spec(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/apis/*/versions/*/specs/*}" + % client.transport._host, + args[1], + ) + + +def test_delete_spec_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_spec( + apihub_service.DeleteSpecRequest(), + name="name_value", + ) + + +def test_create_api_operation_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.create_api_operation in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.create_api_operation + ] = mock_rpc + + request = {} + client.create_api_operation(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.create_api_operation(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_create_api_operation_rest_required_fields( + request_type=apihub_service.CreateApiOperationRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_api_operation._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_api_operation._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("api_operation_id",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.ApiOperation() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.ApiOperation.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.create_api_operation(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_api_operation_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_api_operation._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("apiOperationId",)) + & set( + ( + "parent", + "apiOperation", + ) + ) + ) + + +def test_create_api_operation_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.ApiOperation() + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + api_operation=common_fields.ApiOperation(name="name_value"), + api_operation_id="api_operation_id_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.ApiOperation.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.create_api_operation(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/apis/*/versions/*}/operations" + % client.transport._host, + args[1], + ) + + +def test_create_api_operation_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_api_operation( + apihub_service.CreateApiOperationRequest(), + parent="parent_value", + api_operation=common_fields.ApiOperation(name="name_value"), + api_operation_id="api_operation_id_value", + ) + + +def test_get_api_operation_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.get_api_operation in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.get_api_operation + ] = mock_rpc + + request = {} + client.get_api_operation(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_api_operation(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_get_api_operation_rest_required_fields( + request_type=apihub_service.GetApiOperationRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_api_operation._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_api_operation._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.ApiOperation() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.ApiOperation.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_api_operation(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_api_operation_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_api_operation._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_get_api_operation_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.ApiOperation() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/operations/sample5" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.ApiOperation.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.get_api_operation(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/apis/*/versions/*/operations/*}" + % client.transport._host, + args[1], + ) + + +def test_get_api_operation_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_api_operation( + apihub_service.GetApiOperationRequest(), + name="name_value", + ) + + +def test_list_api_operations_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.list_api_operations in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.list_api_operations + ] = mock_rpc + + request = {} + client.list_api_operations(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.list_api_operations(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_list_api_operations_rest_required_fields( + request_type=apihub_service.ListApiOperationsRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_api_operations._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_api_operations._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = apihub_service.ListApiOperationsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = apihub_service.ListApiOperationsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_api_operations(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_api_operations_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_api_operations._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +def test_list_api_operations_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = apihub_service.ListApiOperationsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = apihub_service.ListApiOperationsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.list_api_operations(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/apis/*/versions/*}/operations" + % client.transport._host, + args[1], + ) + + +def test_list_api_operations_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_api_operations( + apihub_service.ListApiOperationsRequest(), + parent="parent_value", + ) + + +def test_list_api_operations_rest_pager(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + apihub_service.ListApiOperationsResponse( + api_operations=[ + common_fields.ApiOperation(), + common_fields.ApiOperation(), + common_fields.ApiOperation(), + ], + next_page_token="abc", + ), + apihub_service.ListApiOperationsResponse( + api_operations=[], + next_page_token="def", + ), + apihub_service.ListApiOperationsResponse( + api_operations=[ + common_fields.ApiOperation(), + ], + next_page_token="ghi", + ), + apihub_service.ListApiOperationsResponse( + api_operations=[ + common_fields.ApiOperation(), + common_fields.ApiOperation(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + apihub_service.ListApiOperationsResponse.to_json(x) for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = { + "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + } + + pager = client.list_api_operations(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, common_fields.ApiOperation) for i in results) + + pages = list(client.list_api_operations(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +def test_update_api_operation_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.update_api_operation in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.update_api_operation + ] = mock_rpc + + request = {} + client.update_api_operation(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.update_api_operation(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_update_api_operation_rest_required_fields( + request_type=apihub_service.UpdateApiOperationRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_api_operation._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_api_operation._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("update_mask",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.ApiOperation() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.ApiOperation.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.update_api_operation(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_api_operation_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.update_api_operation._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("updateMask",)) + & set( + ( + "apiOperation", + "updateMask", + ) + ) + ) + + +def test_update_api_operation_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.ApiOperation() + + # get arguments that satisfy an http rule for this method + sample_request = { + "api_operation": { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/operations/sample5" + } + } + + # get truthy value for each flattened field + mock_args = dict( + api_operation=common_fields.ApiOperation(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.ApiOperation.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.update_api_operation(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{api_operation.name=projects/*/locations/*/apis/*/versions/*/operations/*}" + % client.transport._host, + args[1], + ) + + +def test_update_api_operation_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_api_operation( + apihub_service.UpdateApiOperationRequest(), + api_operation=common_fields.ApiOperation(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +def test_delete_api_operation_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.delete_api_operation in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.delete_api_operation + ] = mock_rpc + + request = {} + client.delete_api_operation(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.delete_api_operation(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_delete_api_operation_rest_required_fields( + request_type=apihub_service.DeleteApiOperationRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_api_operation._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_api_operation._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = None + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_api_operation(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_api_operation_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_api_operation._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_delete_api_operation_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/operations/sample5" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.delete_api_operation(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/apis/*/versions/*/operations/*}" + % client.transport._host, + args[1], + ) + + +def test_delete_api_operation_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_api_operation( + apihub_service.DeleteApiOperationRequest(), + name="name_value", + ) + + +def test_get_definition_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.get_definition in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.get_definition] = mock_rpc + + request = {} + client.get_definition(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_definition(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_get_definition_rest_required_fields( + request_type=apihub_service.GetDefinitionRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_definition._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_definition._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.Definition() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Definition.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_definition(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_definition_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_definition._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_get_definition_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Definition() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/definitions/sample5" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.Definition.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.get_definition(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/apis/*/versions/*/definitions/*}" + % client.transport._host, + args[1], + ) + + +def test_get_definition_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_definition( + apihub_service.GetDefinitionRequest(), + name="name_value", + ) + + +def test_create_deployment_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.create_deployment in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.create_deployment + ] = mock_rpc + + request = {} + client.create_deployment(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.create_deployment(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_create_deployment_rest_required_fields( + request_type=apihub_service.CreateDeploymentRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_deployment._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_deployment._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("deployment_id",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.Deployment() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Deployment.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.create_deployment(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_deployment_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_deployment._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("deploymentId",)) + & set( + ( + "parent", + "deployment", + ) + ) + ) + + +def test_create_deployment_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Deployment() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + deployment=common_fields.Deployment(name="name_value"), + deployment_id="deployment_id_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.Deployment.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.create_deployment(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/deployments" + % client.transport._host, + args[1], + ) + + +def test_create_deployment_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_deployment( + apihub_service.CreateDeploymentRequest(), + parent="parent_value", + deployment=common_fields.Deployment(name="name_value"), + deployment_id="deployment_id_value", + ) + + +def test_get_deployment_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.get_deployment in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.get_deployment] = mock_rpc + + request = {} + client.get_deployment(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_deployment(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_get_deployment_rest_required_fields( + request_type=apihub_service.GetDeploymentRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_deployment._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_deployment._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.Deployment() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Deployment.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_deployment(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_deployment_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_deployment._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_get_deployment_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Deployment() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/deployments/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.Deployment.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.get_deployment(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/deployments/*}" + % client.transport._host, + args[1], + ) + + +def test_get_deployment_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_deployment( + apihub_service.GetDeploymentRequest(), + name="name_value", + ) + + +def test_list_deployments_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.list_deployments in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.list_deployments + ] = mock_rpc + + request = {} + client.list_deployments(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.list_deployments(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_list_deployments_rest_required_fields( + request_type=apihub_service.ListDeploymentsRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_deployments._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_deployments._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = apihub_service.ListDeploymentsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = apihub_service.ListDeploymentsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_deployments(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_deployments_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_deployments._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +def test_list_deployments_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = apihub_service.ListDeploymentsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = apihub_service.ListDeploymentsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.list_deployments(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/deployments" + % client.transport._host, + args[1], + ) + + +def test_list_deployments_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_deployments( + apihub_service.ListDeploymentsRequest(), + parent="parent_value", + ) + + +def test_list_deployments_rest_pager(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + apihub_service.ListDeploymentsResponse( + deployments=[ + common_fields.Deployment(), + common_fields.Deployment(), + common_fields.Deployment(), + ], + next_page_token="abc", + ), + apihub_service.ListDeploymentsResponse( + deployments=[], + next_page_token="def", + ), + apihub_service.ListDeploymentsResponse( + deployments=[ + common_fields.Deployment(), + ], + next_page_token="ghi", + ), + apihub_service.ListDeploymentsResponse( + deployments=[ + common_fields.Deployment(), + common_fields.Deployment(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + apihub_service.ListDeploymentsResponse.to_json(x) for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2"} + + pager = client.list_deployments(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, common_fields.Deployment) for i in results) + + pages = list(client.list_deployments(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +def test_update_deployment_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.update_deployment in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.update_deployment + ] = mock_rpc + + request = {} + client.update_deployment(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.update_deployment(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_update_deployment_rest_required_fields( + request_type=apihub_service.UpdateDeploymentRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_deployment._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_deployment._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("update_mask",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.Deployment() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Deployment.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.update_deployment(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_deployment_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.update_deployment._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("updateMask",)) + & set( + ( + "deployment", + "updateMask", + ) + ) + ) + + +def test_update_deployment_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Deployment() + + # get arguments that satisfy an http rule for this method + sample_request = { + "deployment": { + "name": "projects/sample1/locations/sample2/deployments/sample3" + } + } + + # get truthy value for each flattened field + mock_args = dict( + deployment=common_fields.Deployment(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.Deployment.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.update_deployment(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{deployment.name=projects/*/locations/*/deployments/*}" + % client.transport._host, + args[1], + ) + + +def test_update_deployment_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_deployment( + apihub_service.UpdateDeploymentRequest(), + deployment=common_fields.Deployment(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +def test_delete_deployment_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.delete_deployment in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.delete_deployment + ] = mock_rpc + + request = {} + client.delete_deployment(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.delete_deployment(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_delete_deployment_rest_required_fields( + request_type=apihub_service.DeleteDeploymentRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_deployment._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_deployment._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = None + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_deployment(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_deployment_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_deployment._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_delete_deployment_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/deployments/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.delete_deployment(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/deployments/*}" + % client.transport._host, + args[1], + ) + + +def test_delete_deployment_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_deployment( + apihub_service.DeleteDeploymentRequest(), + name="name_value", + ) + + +def test_create_attribute_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.create_attribute in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.create_attribute + ] = mock_rpc + + request = {} + client.create_attribute(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.create_attribute(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_create_attribute_rest_required_fields( + request_type=apihub_service.CreateAttributeRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_attribute._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_attribute._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("attribute_id",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.Attribute() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Attribute.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.create_attribute(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_attribute_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_attribute._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("attributeId",)) + & set( + ( + "parent", + "attribute", + ) + ) + ) + + +def test_create_attribute_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Attribute() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + attribute=common_fields.Attribute(name="name_value"), + attribute_id="attribute_id_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.Attribute.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.create_attribute(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/attributes" % client.transport._host, + args[1], + ) + + +def test_create_attribute_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_attribute( + apihub_service.CreateAttributeRequest(), + parent="parent_value", + attribute=common_fields.Attribute(name="name_value"), + attribute_id="attribute_id_value", + ) + + +def test_get_attribute_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.get_attribute in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.get_attribute] = mock_rpc + + request = {} + client.get_attribute(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_attribute(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_get_attribute_rest_required_fields( + request_type=apihub_service.GetAttributeRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_attribute._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_attribute._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.Attribute() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Attribute.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_attribute(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_attribute_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_attribute._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_get_attribute_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Attribute() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/attributes/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.Attribute.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.get_attribute(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/attributes/*}" % client.transport._host, + args[1], + ) + + +def test_get_attribute_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_attribute( + apihub_service.GetAttributeRequest(), + name="name_value", + ) + + +def test_update_attribute_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.update_attribute in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.update_attribute + ] = mock_rpc + + request = {} + client.update_attribute(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.update_attribute(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_update_attribute_rest_required_fields( + request_type=apihub_service.UpdateAttributeRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_attribute._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_attribute._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("update_mask",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.Attribute() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Attribute.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.update_attribute(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_attribute_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.update_attribute._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("updateMask",)) + & set( + ( + "attribute", + "updateMask", + ) + ) + ) + + +def test_update_attribute_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Attribute() + + # get arguments that satisfy an http rule for this method + sample_request = { + "attribute": { + "name": "projects/sample1/locations/sample2/attributes/sample3" + } + } + + # get truthy value for each flattened field + mock_args = dict( + attribute=common_fields.Attribute(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.Attribute.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.update_attribute(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{attribute.name=projects/*/locations/*/attributes/*}" + % client.transport._host, + args[1], + ) + + +def test_update_attribute_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_attribute( + apihub_service.UpdateAttributeRequest(), + attribute=common_fields.Attribute(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +def test_delete_attribute_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.delete_attribute in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.delete_attribute + ] = mock_rpc + + request = {} + client.delete_attribute(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.delete_attribute(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_delete_attribute_rest_required_fields( + request_type=apihub_service.DeleteAttributeRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_attribute._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_attribute._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = None + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_attribute(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_attribute_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_attribute._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_delete_attribute_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/attributes/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.delete_attribute(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/attributes/*}" % client.transport._host, + args[1], + ) + + +def test_delete_attribute_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_attribute( + apihub_service.DeleteAttributeRequest(), + name="name_value", + ) + + +def test_list_attributes_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.list_attributes in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.list_attributes] = mock_rpc + + request = {} + client.list_attributes(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.list_attributes(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_list_attributes_rest_required_fields( + request_type=apihub_service.ListAttributesRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_attributes._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_attributes._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = apihub_service.ListAttributesResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = apihub_service.ListAttributesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_attributes(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_attributes_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_attributes._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +def test_list_attributes_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = apihub_service.ListAttributesResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = apihub_service.ListAttributesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.list_attributes(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/attributes" % client.transport._host, + args[1], + ) + + +def test_list_attributes_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_attributes( + apihub_service.ListAttributesRequest(), + parent="parent_value", + ) + + +def test_list_attributes_rest_pager(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + apihub_service.ListAttributesResponse( + attributes=[ + common_fields.Attribute(), + common_fields.Attribute(), + common_fields.Attribute(), + ], + next_page_token="abc", + ), + apihub_service.ListAttributesResponse( + attributes=[], + next_page_token="def", + ), + apihub_service.ListAttributesResponse( + attributes=[ + common_fields.Attribute(), + ], + next_page_token="ghi", + ), + apihub_service.ListAttributesResponse( + attributes=[ + common_fields.Attribute(), + common_fields.Attribute(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + apihub_service.ListAttributesResponse.to_json(x) for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2"} + + pager = client.list_attributes(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, common_fields.Attribute) for i in results) + + pages = list(client.list_attributes(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +def test_search_resources_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.search_resources in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.search_resources + ] = mock_rpc + + request = {} + client.search_resources(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.search_resources(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_search_resources_rest_required_fields( + request_type=apihub_service.SearchResourcesRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["location"] = "" + request_init["query"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).search_resources._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["location"] = "location_value" + jsonified_request["query"] = "query_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).search_resources._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "location" in jsonified_request + assert jsonified_request["location"] == "location_value" + assert "query" in jsonified_request + assert jsonified_request["query"] == "query_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = apihub_service.SearchResourcesResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = apihub_service.SearchResourcesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.search_resources(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_search_resources_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.search_resources._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(()) + & set( + ( + "location", + "query", + ) + ) + ) + + +def test_search_resources_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = apihub_service.SearchResourcesResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"location": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + location="location_value", + query="query_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = apihub_service.SearchResourcesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.search_resources(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{location=projects/*/locations/*}:searchResources" + % client.transport._host, + args[1], + ) + + +def test_search_resources_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.search_resources( + apihub_service.SearchResourcesRequest(), + location="location_value", + query="query_value", + ) + + +def test_search_resources_rest_pager(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + apihub_service.SearchResourcesResponse( + search_results=[ + apihub_service.SearchResult(), + apihub_service.SearchResult(), + apihub_service.SearchResult(), + ], + next_page_token="abc", + ), + apihub_service.SearchResourcesResponse( + search_results=[], + next_page_token="def", + ), + apihub_service.SearchResourcesResponse( + search_results=[ + apihub_service.SearchResult(), + ], + next_page_token="ghi", + ), + apihub_service.SearchResourcesResponse( + search_results=[ + apihub_service.SearchResult(), + apihub_service.SearchResult(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + apihub_service.SearchResourcesResponse.to_json(x) for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"location": "projects/sample1/locations/sample2"} + + pager = client.search_resources(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, apihub_service.SearchResult) for i in results) + + pages = list(client.search_resources(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +def test_create_external_api_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.create_external_api in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.create_external_api + ] = mock_rpc + + request = {} + client.create_external_api(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.create_external_api(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_create_external_api_rest_required_fields( + request_type=apihub_service.CreateExternalApiRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_external_api._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_external_api._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("external_api_id",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.ExternalApi() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.ExternalApi.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.create_external_api(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_external_api_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_external_api._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("externalApiId",)) + & set( + ( + "parent", + "externalApi", + ) + ) + ) + + +def test_create_external_api_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.ExternalApi() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + external_api=common_fields.ExternalApi(name="name_value"), + external_api_id="external_api_id_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.ExternalApi.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.create_external_api(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/externalApis" + % client.transport._host, + args[1], + ) + + +def test_create_external_api_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_external_api( + apihub_service.CreateExternalApiRequest(), + parent="parent_value", + external_api=common_fields.ExternalApi(name="name_value"), + external_api_id="external_api_id_value", + ) + + +def test_get_external_api_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.get_external_api in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.get_external_api + ] = mock_rpc + + request = {} + client.get_external_api(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_external_api(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_get_external_api_rest_required_fields( + request_type=apihub_service.GetExternalApiRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_external_api._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_external_api._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.ExternalApi() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.ExternalApi.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_external_api(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_external_api_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_external_api._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_get_external_api_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.ExternalApi() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/externalApis/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.ExternalApi.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.get_external_api(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/externalApis/*}" + % client.transport._host, + args[1], + ) + + +def test_get_external_api_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_external_api( + apihub_service.GetExternalApiRequest(), + name="name_value", + ) + + +def test_update_external_api_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.update_external_api in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.update_external_api + ] = mock_rpc + + request = {} + client.update_external_api(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.update_external_api(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_update_external_api_rest_required_fields( + request_type=apihub_service.UpdateExternalApiRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_external_api._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_external_api._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("update_mask",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.ExternalApi() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.ExternalApi.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.update_external_api(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_external_api_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.update_external_api._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("updateMask",)) + & set( + ( + "externalApi", + "updateMask", + ) + ) + ) + + +def test_update_external_api_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.ExternalApi() + + # get arguments that satisfy an http rule for this method + sample_request = { + "external_api": { + "name": "projects/sample1/locations/sample2/externalApis/sample3" + } + } + + # get truthy value for each flattened field + mock_args = dict( + external_api=common_fields.ExternalApi(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.ExternalApi.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.update_external_api(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{external_api.name=projects/*/locations/*/externalApis/*}" + % client.transport._host, + args[1], + ) + + +def test_update_external_api_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_external_api( + apihub_service.UpdateExternalApiRequest(), + external_api=common_fields.ExternalApi(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +def test_delete_external_api_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.delete_external_api in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.delete_external_api + ] = mock_rpc + + request = {} + client.delete_external_api(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.delete_external_api(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_delete_external_api_rest_required_fields( + request_type=apihub_service.DeleteExternalApiRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_external_api._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_external_api._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = None + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_external_api(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_external_api_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_external_api._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_delete_external_api_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/externalApis/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.delete_external_api(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/externalApis/*}" + % client.transport._host, + args[1], + ) + + +def test_delete_external_api_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_external_api( + apihub_service.DeleteExternalApiRequest(), + name="name_value", + ) + + +def test_list_external_apis_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.list_external_apis in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.list_external_apis + ] = mock_rpc + + request = {} + client.list_external_apis(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.list_external_apis(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_list_external_apis_rest_required_fields( + request_type=apihub_service.ListExternalApisRequest, +): + transport_class = transports.ApiHubRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_external_apis._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_external_apis._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = apihub_service.ListExternalApisResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = apihub_service.ListExternalApisResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_external_apis(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_external_apis_rest_unset_required_fields(): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_external_apis._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +def test_list_external_apis_rest_flattened(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = apihub_service.ListExternalApisResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = apihub_service.ListExternalApisResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.list_external_apis(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/externalApis" + % client.transport._host, + args[1], + ) + + +def test_list_external_apis_rest_flattened_error(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_external_apis( + apihub_service.ListExternalApisRequest(), + parent="parent_value", + ) + + +def test_list_external_apis_rest_pager(transport: str = "rest"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + apihub_service.ListExternalApisResponse( + external_apis=[ + common_fields.ExternalApi(), + common_fields.ExternalApi(), + common_fields.ExternalApi(), + ], + next_page_token="abc", + ), + apihub_service.ListExternalApisResponse( + external_apis=[], + next_page_token="def", + ), + apihub_service.ListExternalApisResponse( + external_apis=[ + common_fields.ExternalApi(), + ], + next_page_token="ghi", + ), + apihub_service.ListExternalApisResponse( + external_apis=[ + common_fields.ExternalApi(), + common_fields.ExternalApi(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + apihub_service.ListExternalApisResponse.to_json(x) for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2"} + + pager = client.list_external_apis(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, common_fields.ExternalApi) for i in results) + + pages = list(client.list_external_apis(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +def test_credentials_transport_error(): + # It is an error to provide credentials and a transport instance. + transport = transports.ApiHubGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # It is an error to provide a credentials file and a transport instance. + transport = transports.ApiHubGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = ApiHubClient( + client_options={"credentials_file": "credentials.json"}, + transport=transport, + ) + + # It is an error to provide an api_key and a transport instance. + transport = transports.ApiHubGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = ApiHubClient( + client_options=options, + transport=transport, + ) + + # It is an error to provide an api_key and a credential. + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = ApiHubClient( + client_options=options, credentials=ga_credentials.AnonymousCredentials() + ) + + # It is an error to provide scopes and a transport instance. + transport = transports.ApiHubGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = ApiHubClient( + client_options={"scopes": ["1", "2"]}, + transport=transport, + ) + + +def test_transport_instance(): + # A client may be instantiated with a custom transport instance. + transport = transports.ApiHubGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + client = ApiHubClient(transport=transport) + assert client.transport is transport + + +def test_transport_get_channel(): + # A client may be instantiated with a custom transport instance. + transport = transports.ApiHubGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + transport = transports.ApiHubGrpcAsyncIOTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubGrpcTransport, + transports.ApiHubGrpcAsyncIOTransport, + transports.ApiHubRestTransport, + ], +) +def test_transport_adc(transport_class): + # Test default credentials are used if not provided. + with mock.patch.object(google.auth, "default") as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class() + adc.assert_called_once() + + +def test_transport_kind_grpc(): + transport = ApiHubClient.get_transport_class("grpc")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "grpc" + + +def test_initialize_client_w_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_api_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.create_api), "__call__") as call: + call.return_value = common_fields.Api() + client.create_api(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateApiRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_api_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_api), "__call__") as call: + call.return_value = common_fields.Api() + client.get_api(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetApiRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_apis_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.list_apis), "__call__") as call: + call.return_value = apihub_service.ListApisResponse() + client.list_apis(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListApisRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_update_api_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.update_api), "__call__") as call: + call.return_value = common_fields.Api() + client.update_api(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateApiRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_api_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.delete_api), "__call__") as call: + call.return_value = None + client.delete_api(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteApiRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_version_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.create_version), "__call__") as call: + call.return_value = common_fields.Version() + client.create_version(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateVersionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_version_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_version), "__call__") as call: + call.return_value = common_fields.Version() + client.get_version(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetVersionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_versions_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.list_versions), "__call__") as call: + call.return_value = apihub_service.ListVersionsResponse() + client.list_versions(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListVersionsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_update_version_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.update_version), "__call__") as call: + call.return_value = common_fields.Version() + client.update_version(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateVersionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_version_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.delete_version), "__call__") as call: + call.return_value = None + client.delete_version(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteVersionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_spec_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.create_spec), "__call__") as call: + call.return_value = common_fields.Spec() + client.create_spec(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateSpecRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_spec_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_spec), "__call__") as call: + call.return_value = common_fields.Spec() + client.get_spec(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetSpecRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_spec_contents_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_spec_contents), "__call__" + ) as call: + call.return_value = common_fields.SpecContents() + client.get_spec_contents(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetSpecContentsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_specs_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.list_specs), "__call__") as call: + call.return_value = apihub_service.ListSpecsResponse() + client.list_specs(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListSpecsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_update_spec_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.update_spec), "__call__") as call: + call.return_value = common_fields.Spec() + client.update_spec(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateSpecRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_spec_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.delete_spec), "__call__") as call: + call.return_value = None + client.delete_spec(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteSpecRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_api_operation_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_api_operation), "__call__" + ) as call: + call.return_value = common_fields.ApiOperation() + client.create_api_operation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateApiOperationRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_api_operation_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_api_operation), "__call__" + ) as call: + call.return_value = common_fields.ApiOperation() + client.get_api_operation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetApiOperationRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_api_operations_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_api_operations), "__call__" + ) as call: + call.return_value = apihub_service.ListApiOperationsResponse() + client.list_api_operations(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListApiOperationsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_update_api_operation_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.update_api_operation), "__call__" + ) as call: + call.return_value = common_fields.ApiOperation() + client.update_api_operation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateApiOperationRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_api_operation_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.delete_api_operation), "__call__" + ) as call: + call.return_value = None + client.delete_api_operation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteApiOperationRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_definition_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_definition), "__call__") as call: + call.return_value = common_fields.Definition() + client.get_definition(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetDefinitionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_deployment_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_deployment), "__call__" + ) as call: + call.return_value = common_fields.Deployment() + client.create_deployment(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateDeploymentRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_deployment_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_deployment), "__call__") as call: + call.return_value = common_fields.Deployment() + client.get_deployment(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetDeploymentRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_deployments_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.list_deployments), "__call__") as call: + call.return_value = apihub_service.ListDeploymentsResponse() + client.list_deployments(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListDeploymentsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_update_deployment_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.update_deployment), "__call__" + ) as call: + call.return_value = common_fields.Deployment() + client.update_deployment(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateDeploymentRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_deployment_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.delete_deployment), "__call__" + ) as call: + call.return_value = None + client.delete_deployment(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteDeploymentRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_attribute_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.create_attribute), "__call__") as call: + call.return_value = common_fields.Attribute() + client.create_attribute(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateAttributeRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_attribute_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_attribute), "__call__") as call: + call.return_value = common_fields.Attribute() + client.get_attribute(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetAttributeRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_update_attribute_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.update_attribute), "__call__") as call: + call.return_value = common_fields.Attribute() + client.update_attribute(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateAttributeRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_attribute_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.delete_attribute), "__call__") as call: + call.return_value = None + client.delete_attribute(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteAttributeRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_attributes_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.list_attributes), "__call__") as call: + call.return_value = apihub_service.ListAttributesResponse() + client.list_attributes(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListAttributesRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_search_resources_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.search_resources), "__call__") as call: + call.return_value = apihub_service.SearchResourcesResponse() + client.search_resources(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.SearchResourcesRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_external_api_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_external_api), "__call__" + ) as call: + call.return_value = common_fields.ExternalApi() + client.create_external_api(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateExternalApiRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_external_api_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_external_api), "__call__") as call: + call.return_value = common_fields.ExternalApi() + client.get_external_api(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetExternalApiRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_update_external_api_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.update_external_api), "__call__" + ) as call: + call.return_value = common_fields.ExternalApi() + client.update_external_api(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateExternalApiRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_external_api_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.delete_external_api), "__call__" + ) as call: + call.return_value = None + client.delete_external_api(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteExternalApiRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_external_apis_empty_call_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_external_apis), "__call__" + ) as call: + call.return_value = apihub_service.ListExternalApisResponse() + client.list_external_apis(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListExternalApisRequest() + + assert args[0] == request_msg + + +def test_transport_kind_grpc_asyncio(): + transport = ApiHubAsyncClient.get_transport_class("grpc_asyncio")( + credentials=async_anonymous_credentials() + ) + assert transport.kind == "grpc_asyncio" + + +def test_initialize_client_w_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_create_api_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.create_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Api( + name="name_value", + display_name="display_name_value", + description="description_value", + versions=["versions_value"], + selected_version="selected_version_value", + fingerprint="fingerprint_value", + ) + ) + await client.create_api(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateApiRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_api_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Api( + name="name_value", + display_name="display_name_value", + description="description_value", + versions=["versions_value"], + selected_version="selected_version_value", + fingerprint="fingerprint_value", + ) + ) + await client.get_api(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetApiRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_list_apis_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.list_apis), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListApisResponse( + next_page_token="next_page_token_value", + ) + ) + await client.list_apis(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListApisRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_update_api_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.update_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Api( + name="name_value", + display_name="display_name_value", + description="description_value", + versions=["versions_value"], + selected_version="selected_version_value", + fingerprint="fingerprint_value", + ) + ) + await client.update_api(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateApiRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_delete_api_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.delete_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_api(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteApiRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_create_version_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.create_version), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Version( + name="name_value", + display_name="display_name_value", + description="description_value", + specs=["specs_value"], + api_operations=["api_operations_value"], + definitions=["definitions_value"], + deployments=["deployments_value"], + selected_deployment="selected_deployment_value", + ) + ) + await client.create_version(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateVersionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_version_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_version), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Version( + name="name_value", + display_name="display_name_value", + description="description_value", + specs=["specs_value"], + api_operations=["api_operations_value"], + definitions=["definitions_value"], + deployments=["deployments_value"], + selected_deployment="selected_deployment_value", + ) + ) + await client.get_version(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetVersionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_list_versions_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.list_versions), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListVersionsResponse( + next_page_token="next_page_token_value", + ) + ) + await client.list_versions(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListVersionsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_update_version_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.update_version), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Version( + name="name_value", + display_name="display_name_value", + description="description_value", + specs=["specs_value"], + api_operations=["api_operations_value"], + definitions=["definitions_value"], + deployments=["deployments_value"], + selected_deployment="selected_deployment_value", + ) + ) + await client.update_version(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateVersionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_delete_version_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.delete_version), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_version(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteVersionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_create_spec_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.create_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Spec( + name="name_value", + display_name="display_name_value", + source_uri="source_uri_value", + parsing_mode=common_fields.Spec.ParsingMode.RELAXED, + ) + ) + await client.create_spec(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateSpecRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_spec_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Spec( + name="name_value", + display_name="display_name_value", + source_uri="source_uri_value", + parsing_mode=common_fields.Spec.ParsingMode.RELAXED, + ) + ) + await client.get_spec(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetSpecRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_spec_contents_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_spec_contents), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.SpecContents( + contents=b"contents_blob", + mime_type="mime_type_value", + ) + ) + await client.get_spec_contents(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetSpecContentsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_list_specs_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.list_specs), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListSpecsResponse( + next_page_token="next_page_token_value", + ) + ) + await client.list_specs(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListSpecsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_update_spec_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.update_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Spec( + name="name_value", + display_name="display_name_value", + source_uri="source_uri_value", + parsing_mode=common_fields.Spec.ParsingMode.RELAXED, + ) + ) + await client.update_spec(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateSpecRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_delete_spec_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.delete_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_spec(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteSpecRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_create_api_operation_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ApiOperation( + name="name_value", + spec="spec_value", + ) + ) + await client.create_api_operation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateApiOperationRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_api_operation_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ApiOperation( + name="name_value", + spec="spec_value", + ) + ) + await client.get_api_operation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetApiOperationRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_list_api_operations_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_api_operations), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListApiOperationsResponse( + next_page_token="next_page_token_value", + ) + ) + await client.list_api_operations(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListApiOperationsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_update_api_operation_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.update_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ApiOperation( + name="name_value", + spec="spec_value", + ) + ) + await client.update_api_operation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateApiOperationRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_delete_api_operation_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.delete_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_api_operation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteApiOperationRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_definition_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_definition), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Definition( + name="name_value", + spec="spec_value", + type_=common_fields.Definition.Type.SCHEMA, + ) + ) + await client.get_definition(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetDefinitionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_create_deployment_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_deployment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Deployment( + name="name_value", + display_name="display_name_value", + description="description_value", + resource_uri="resource_uri_value", + endpoints=["endpoints_value"], + api_versions=["api_versions_value"], + source_project="source_project_value", + source_environment="source_environment_value", + ) + ) + await client.create_deployment(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateDeploymentRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_deployment_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_deployment), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Deployment( + name="name_value", + display_name="display_name_value", + description="description_value", + resource_uri="resource_uri_value", + endpoints=["endpoints_value"], + api_versions=["api_versions_value"], + source_project="source_project_value", + source_environment="source_environment_value", + ) + ) + await client.get_deployment(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetDeploymentRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_list_deployments_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.list_deployments), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListDeploymentsResponse( + next_page_token="next_page_token_value", + ) + ) + await client.list_deployments(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListDeploymentsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_update_deployment_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.update_deployment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Deployment( + name="name_value", + display_name="display_name_value", + description="description_value", + resource_uri="resource_uri_value", + endpoints=["endpoints_value"], + api_versions=["api_versions_value"], + source_project="source_project_value", + source_environment="source_environment_value", + ) + ) + await client.update_deployment(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateDeploymentRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_delete_deployment_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.delete_deployment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_deployment(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteDeploymentRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_create_attribute_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.create_attribute), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Attribute( + name="name_value", + display_name="display_name_value", + description="description_value", + definition_type=common_fields.Attribute.DefinitionType.SYSTEM_DEFINED, + scope=common_fields.Attribute.Scope.API, + data_type=common_fields.Attribute.DataType.ENUM, + cardinality=1172, + mandatory=True, + ) + ) + await client.create_attribute(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateAttributeRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_attribute_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_attribute), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Attribute( + name="name_value", + display_name="display_name_value", + description="description_value", + definition_type=common_fields.Attribute.DefinitionType.SYSTEM_DEFINED, + scope=common_fields.Attribute.Scope.API, + data_type=common_fields.Attribute.DataType.ENUM, + cardinality=1172, + mandatory=True, + ) + ) + await client.get_attribute(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetAttributeRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_update_attribute_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.update_attribute), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Attribute( + name="name_value", + display_name="display_name_value", + description="description_value", + definition_type=common_fields.Attribute.DefinitionType.SYSTEM_DEFINED, + scope=common_fields.Attribute.Scope.API, + data_type=common_fields.Attribute.DataType.ENUM, + cardinality=1172, + mandatory=True, + ) + ) + await client.update_attribute(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateAttributeRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_delete_attribute_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.delete_attribute), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_attribute(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteAttributeRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_list_attributes_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.list_attributes), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListAttributesResponse( + next_page_token="next_page_token_value", + ) + ) + await client.list_attributes(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListAttributesRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_search_resources_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.search_resources), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.SearchResourcesResponse( + next_page_token="next_page_token_value", + ) + ) + await client.search_resources(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.SearchResourcesRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_create_external_api_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_external_api), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ExternalApi( + name="name_value", + display_name="display_name_value", + description="description_value", + endpoints=["endpoints_value"], + paths=["paths_value"], + ) + ) + await client.create_external_api(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateExternalApiRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_external_api_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_external_api), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ExternalApi( + name="name_value", + display_name="display_name_value", + description="description_value", + endpoints=["endpoints_value"], + paths=["paths_value"], + ) + ) + await client.get_external_api(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetExternalApiRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_update_external_api_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.update_external_api), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ExternalApi( + name="name_value", + display_name="display_name_value", + description="description_value", + endpoints=["endpoints_value"], + paths=["paths_value"], + ) + ) + await client.update_external_api(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateExternalApiRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_delete_external_api_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.delete_external_api), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_external_api(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteExternalApiRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_list_external_apis_empty_call_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_external_apis), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListExternalApisResponse( + next_page_token="next_page_token_value", + ) + ) + await client.list_external_apis(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListExternalApisRequest() + + assert args[0] == request_msg + + +def test_transport_kind_rest(): + transport = ApiHubClient.get_transport_class("rest")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "rest" + + +def test_create_api_rest_bad_request(request_type=apihub_service.CreateApiRequest): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.create_api(request) + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.CreateApiRequest, + dict, + ], +) +def test_create_api_rest_call_success(request_type): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["api"] = { + "name": "name_value", + "display_name": "display_name_value", + "description": "description_value", + "documentation": {"external_uri": "external_uri_value"}, + "owner": {"display_name": "display_name_value", "email": "email_value"}, + "versions": ["versions_value1", "versions_value2"], + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "target_user": { + "enum_values": { + "values": [ + { + "id": "id_value", + "display_name": "display_name_value", + "description": "description_value", + "immutable": True, + } + ] + }, + "string_values": {"values": ["values_value1", "values_value2"]}, + "json_values": {}, + "uri_values": {}, + "attribute": "attribute_value", + }, + "team": {}, + "business_unit": {}, + "maturity_level": {}, + "attributes": {}, + "api_style": {}, + "selected_version": "selected_version_value", + "api_requirements": {}, + "fingerprint": "fingerprint_value", + "source_metadata": [ + { + "plugin_instance_action_source": { + "plugin_instance": "plugin_instance_value", + "action_id": "action_id_value", + }, + "source_type": 1, + "original_resource_id": "original_resource_id_value", + "original_resource_create_time": {}, + "original_resource_update_time": {}, + } + ], + "api_functional_requirements": {}, + "api_technical_requirements": {}, + } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = apihub_service.CreateApiRequest.meta.fields["api"] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["api"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["api"][field])): + del request_init["api"][field][i][subfield] + else: + del request_init["api"][field][subfield] + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Api( + name="name_value", + display_name="display_name_value", + description="description_value", + versions=["versions_value"], + selected_version="selected_version_value", + fingerprint="fingerprint_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Api.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.create_api(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Api) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.versions == ["versions_value"] + assert response.selected_version == "selected_version_value" + assert response.fingerprint == "fingerprint_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_api_rest_interceptors(null_interceptor): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), + ) + client = ApiHubClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubRestInterceptor, "post_create_api" + ) as post, mock.patch.object( + transports.ApiHubRestInterceptor, "post_create_api_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubRestInterceptor, "pre_create_api" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = apihub_service.CreateApiRequest.pb( + apihub_service.CreateApiRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = common_fields.Api.to_json(common_fields.Api()) + req.return_value.content = return_value + + request = apihub_service.CreateApiRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = common_fields.Api() + post_with_metadata.return_value = common_fields.Api(), metadata + + client.create_api( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_get_api_rest_bad_request(request_type=apihub_service.GetApiRequest): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/apis/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_api(request) + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.GetApiRequest, + dict, + ], +) +def test_get_api_rest_call_success(request_type): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/apis/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Api( + name="name_value", + display_name="display_name_value", + description="description_value", + versions=["versions_value"], + selected_version="selected_version_value", + fingerprint="fingerprint_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Api.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.get_api(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Api) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.versions == ["versions_value"] + assert response.selected_version == "selected_version_value" + assert response.fingerprint == "fingerprint_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_api_rest_interceptors(null_interceptor): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), + ) + client = ApiHubClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubRestInterceptor, "post_get_api" + ) as post, mock.patch.object( + transports.ApiHubRestInterceptor, "post_get_api_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubRestInterceptor, "pre_get_api" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = apihub_service.GetApiRequest.pb(apihub_service.GetApiRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = common_fields.Api.to_json(common_fields.Api()) + req.return_value.content = return_value + + request = apihub_service.GetApiRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = common_fields.Api() + post_with_metadata.return_value = common_fields.Api(), metadata + + client.get_api( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_list_apis_rest_bad_request(request_type=apihub_service.ListApisRequest): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_apis(request) + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.ListApisRequest, + dict, + ], +) +def test_list_apis_rest_call_success(request_type): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = apihub_service.ListApisResponse( + next_page_token="next_page_token_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = apihub_service.ListApisResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.list_apis(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListApisPager) + assert response.next_page_token == "next_page_token_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_apis_rest_interceptors(null_interceptor): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), + ) + client = ApiHubClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubRestInterceptor, "post_list_apis" + ) as post, mock.patch.object( + transports.ApiHubRestInterceptor, "post_list_apis_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubRestInterceptor, "pre_list_apis" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = apihub_service.ListApisRequest.pb(apihub_service.ListApisRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = apihub_service.ListApisResponse.to_json( + apihub_service.ListApisResponse() + ) + req.return_value.content = return_value + + request = apihub_service.ListApisRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = apihub_service.ListApisResponse() + post_with_metadata.return_value = apihub_service.ListApisResponse(), metadata + + client.list_apis( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_update_api_rest_bad_request(request_type=apihub_service.UpdateApiRequest): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"api": {"name": "projects/sample1/locations/sample2/apis/sample3"}} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.update_api(request) + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.UpdateApiRequest, + dict, + ], +) +def test_update_api_rest_call_success(request_type): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"api": {"name": "projects/sample1/locations/sample2/apis/sample3"}} + request_init["api"] = { + "name": "projects/sample1/locations/sample2/apis/sample3", + "display_name": "display_name_value", + "description": "description_value", + "documentation": {"external_uri": "external_uri_value"}, + "owner": {"display_name": "display_name_value", "email": "email_value"}, + "versions": ["versions_value1", "versions_value2"], + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "target_user": { + "enum_values": { + "values": [ + { + "id": "id_value", + "display_name": "display_name_value", + "description": "description_value", + "immutable": True, + } + ] + }, + "string_values": {"values": ["values_value1", "values_value2"]}, + "json_values": {}, + "uri_values": {}, + "attribute": "attribute_value", + }, + "team": {}, + "business_unit": {}, + "maturity_level": {}, + "attributes": {}, + "api_style": {}, + "selected_version": "selected_version_value", + "api_requirements": {}, + "fingerprint": "fingerprint_value", + "source_metadata": [ + { + "plugin_instance_action_source": { + "plugin_instance": "plugin_instance_value", + "action_id": "action_id_value", + }, + "source_type": 1, + "original_resource_id": "original_resource_id_value", + "original_resource_create_time": {}, + "original_resource_update_time": {}, + } + ], + "api_functional_requirements": {}, + "api_technical_requirements": {}, + } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = apihub_service.UpdateApiRequest.meta.fields["api"] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["api"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["api"][field])): + del request_init["api"][field][i][subfield] + else: + del request_init["api"][field][subfield] + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Api( + name="name_value", + display_name="display_name_value", + description="description_value", + versions=["versions_value"], + selected_version="selected_version_value", + fingerprint="fingerprint_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Api.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.update_api(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Api) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.versions == ["versions_value"] + assert response.selected_version == "selected_version_value" + assert response.fingerprint == "fingerprint_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_update_api_rest_interceptors(null_interceptor): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), + ) + client = ApiHubClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubRestInterceptor, "post_update_api" + ) as post, mock.patch.object( + transports.ApiHubRestInterceptor, "post_update_api_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubRestInterceptor, "pre_update_api" + ) as pre: + pre.assert_not_called() + post.assert_not_called() post_with_metadata.assert_not_called() - pb_message = apihub_service.ListApisRequest.pb(apihub_service.ListApisRequest()) + pb_message = apihub_service.UpdateApiRequest.pb( + apihub_service.UpdateApiRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = common_fields.Api.to_json(common_fields.Api()) + req.return_value.content = return_value + + request = apihub_service.UpdateApiRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = common_fields.Api() + post_with_metadata.return_value = common_fields.Api(), metadata + + client.update_api( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_delete_api_rest_bad_request(request_type=apihub_service.DeleteApiRequest): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/apis/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.delete_api(request) + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.DeleteApiRequest, + dict, + ], +) +def test_delete_api_rest_call_success(request_type): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/apis/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "" + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.delete_api(request) + + # Establish that the response is the type that we expect. + assert response is None + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_api_rest_interceptors(null_interceptor): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), + ) + client = ApiHubClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubRestInterceptor, "pre_delete_api" + ) as pre: + pre.assert_not_called() + pb_message = apihub_service.DeleteApiRequest.pb( + apihub_service.DeleteApiRequest() + ) transcode.return_value = { "method": "post", "uri": "my_uri", @@ -9083,21 +26224,15 @@ def test_list_apis_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = apihub_service.ListApisResponse.to_json( - apihub_service.ListApisResponse() - ) - req.return_value.content = return_value - request = apihub_service.ListApisRequest() + request = apihub_service.DeleteApiRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = apihub_service.ListApisResponse() - post_with_metadata.return_value = apihub_service.ListApisResponse(), metadata - client.list_apis( + client.delete_api( request, metadata=[ ("key", "val"), @@ -9106,16 +26241,16 @@ def test_list_apis_rest_interceptors(null_interceptor): ) pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() -def test_update_api_rest_bad_request(request_type=apihub_service.UpdateApiRequest): +def test_create_version_rest_bad_request( + request_type=apihub_service.CreateVersionRequest, +): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"api": {"name": "projects/sample1/locations/sample2/apis/sample3"}} + request_init = {"parent": "projects/sample1/locations/sample2/apis/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -9130,33 +26265,35 @@ def test_update_api_rest_bad_request(request_type=apihub_service.UpdateApiReques response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.update_api(request) + client.create_version(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.UpdateApiRequest, + apihub_service.CreateVersionRequest, dict, ], ) -def test_update_api_rest_call_success(request_type): +def test_create_version_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"api": {"name": "projects/sample1/locations/sample2/apis/sample3"}} - request_init["api"] = { - "name": "projects/sample1/locations/sample2/apis/sample3", + request_init = {"parent": "projects/sample1/locations/sample2/apis/sample3"} + request_init["version"] = { + "name": "name_value", "display_name": "display_name_value", "description": "description_value", "documentation": {"external_uri": "external_uri_value"}, - "owner": {"display_name": "display_name_value", "email": "email_value"}, - "versions": ["versions_value1", "versions_value2"], + "specs": ["specs_value1", "specs_value2"], + "api_operations": ["api_operations_value1", "api_operations_value2"], + "definitions": ["definitions_value1", "definitions_value2"], + "deployments": ["deployments_value1", "deployments_value2"], "create_time": {"seconds": 751, "nanos": 543}, "update_time": {}, - "target_user": { + "lifecycle": { "enum_values": { "values": [ { @@ -9172,14 +26309,10 @@ def test_update_api_rest_call_success(request_type): "uri_values": {}, "attribute": "attribute_value", }, - "team": {}, - "business_unit": {}, - "maturity_level": {}, + "compliance": {}, + "accreditation": {}, "attributes": {}, - "api_style": {}, - "selected_version": "selected_version_value", - "api_requirements": {}, - "fingerprint": "fingerprint_value", + "selected_deployment": "selected_deployment_value", "source_metadata": [ { "plugin_instance_action_source": { @@ -9192,15 +26325,13 @@ def test_update_api_rest_call_success(request_type): "original_resource_update_time": {}, } ], - "api_functional_requirements": {}, - "api_technical_requirements": {}, } # The version of a generated dependency at test runtime may differ from the version used during generation. # Delete any fields which are not present in the current runtime dependency # See https://github.com/googleapis/gapic-generator-python/issues/1748 # Determine if the message type is proto-plus or protobuf - test_field = apihub_service.UpdateApiRequest.meta.fields["api"] + test_field = apihub_service.CreateVersionRequest.meta.fields["version"] def get_message_fields(field): # Given a field which is a message (composite type), return a list with @@ -9228,7 +26359,7 @@ def get_message_fields(field): # For each item in the sample request, create a list of sub fields which are not present at runtime # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["api"].items(): # pragma: NO COVER + for field, value in request_init["version"].items(): # pragma: NO COVER result = None is_repeated = False # For repeated fields @@ -9258,22 +26389,24 @@ def get_message_fields(field): subfield = subfield_to_delete.get("subfield") if subfield: if field_repeated: - for i in range(0, len(request_init["api"][field])): - del request_init["api"][field][i][subfield] + for i in range(0, len(request_init["version"][field])): + del request_init["version"][field][i][subfield] else: - del request_init["api"][field][subfield] + del request_init["version"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = common_fields.Api( + return_value = common_fields.Version( name="name_value", display_name="display_name_value", description="description_value", - versions=["versions_value"], - selected_version="selected_version_value", - fingerprint="fingerprint_value", + specs=["specs_value"], + api_operations=["api_operations_value"], + definitions=["definitions_value"], + deployments=["deployments_value"], + selected_deployment="selected_deployment_value", ) # Wrap the value into a proper Response obj @@ -9281,25 +26414,27 @@ def get_message_fields(field): response_value.status_code = 200 # Convert return value to protobuf type - return_value = common_fields.Api.pb(return_value) + return_value = common_fields.Version.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.update_api(request) + response = client.create_version(request) # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.Api) + assert isinstance(response, common_fields.Version) assert response.name == "name_value" assert response.display_name == "display_name_value" assert response.description == "description_value" - assert response.versions == ["versions_value"] - assert response.selected_version == "selected_version_value" - assert response.fingerprint == "fingerprint_value" + assert response.specs == ["specs_value"] + assert response.api_operations == ["api_operations_value"] + assert response.definitions == ["definitions_value"] + assert response.deployments == ["deployments_value"] + assert response.selected_deployment == "selected_deployment_value" @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_update_api_rest_interceptors(null_interceptor): +def test_create_version_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -9311,17 +26446,158 @@ def test_update_api_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_update_api" + transports.ApiHubRestInterceptor, "post_create_version" + ) as post, mock.patch.object( + transports.ApiHubRestInterceptor, "post_create_version_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubRestInterceptor, "pre_create_version" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = apihub_service.CreateVersionRequest.pb( + apihub_service.CreateVersionRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = common_fields.Version.to_json(common_fields.Version()) + req.return_value.content = return_value + + request = apihub_service.CreateVersionRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = common_fields.Version() + post_with_metadata.return_value = common_fields.Version(), metadata + + client.create_version( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_get_version_rest_bad_request(request_type=apihub_service.GetVersionRequest): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_version(request) + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.GetVersionRequest, + dict, + ], +) +def test_get_version_rest_call_success(request_type): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Version( + name="name_value", + display_name="display_name_value", + description="description_value", + specs=["specs_value"], + api_operations=["api_operations_value"], + definitions=["definitions_value"], + deployments=["deployments_value"], + selected_deployment="selected_deployment_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Version.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.get_version(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Version) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.specs == ["specs_value"] + assert response.api_operations == ["api_operations_value"] + assert response.definitions == ["definitions_value"] + assert response.deployments == ["deployments_value"] + assert response.selected_deployment == "selected_deployment_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_version_rest_interceptors(null_interceptor): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), + ) + client = ApiHubClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubRestInterceptor, "post_get_version" ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_update_api_with_metadata" + transports.ApiHubRestInterceptor, "post_get_version_with_metadata" ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_update_api" + transports.ApiHubRestInterceptor, "pre_get_version" ) as pre: pre.assert_not_called() post.assert_not_called() post_with_metadata.assert_not_called() - pb_message = apihub_service.UpdateApiRequest.pb( - apihub_service.UpdateApiRequest() + pb_message = apihub_service.GetVersionRequest.pb( + apihub_service.GetVersionRequest() ) transcode.return_value = { "method": "post", @@ -9333,19 +26609,19 @@ def test_update_api_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.Api.to_json(common_fields.Api()) + return_value = common_fields.Version.to_json(common_fields.Version()) req.return_value.content = return_value - request = apihub_service.UpdateApiRequest() + request = apihub_service.GetVersionRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = common_fields.Api() - post_with_metadata.return_value = common_fields.Api(), metadata + post.return_value = common_fields.Version() + post_with_metadata.return_value = common_fields.Version(), metadata - client.update_api( + client.get_version( request, metadata=[ ("key", "val"), @@ -9358,12 +26634,14 @@ def test_update_api_rest_interceptors(null_interceptor): post_with_metadata.assert_called_once() -def test_delete_api_rest_bad_request(request_type=apihub_service.DeleteApiRequest): +def test_list_versions_rest_bad_request( + request_type=apihub_service.ListVersionsRequest, +): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/apis/sample3"} + request_init = {"parent": "projects/sample1/locations/sample2/apis/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -9378,45 +26656,51 @@ def test_delete_api_rest_bad_request(request_type=apihub_service.DeleteApiReques response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_api(request) + client.list_versions(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.DeleteApiRequest, + apihub_service.ListVersionsRequest, dict, ], ) -def test_delete_api_rest_call_success(request_type): +def test_list_versions_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/apis/sample3"} + request_init = {"parent": "projects/sample1/locations/sample2/apis/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = None + return_value = apihub_service.ListVersionsResponse( + next_page_token="next_page_token_value", + ) # Wrap the value into a proper Response obj response_value = mock.Mock() response_value.status_code = 200 - json_return_value = "" + + # Convert return value to protobuf type + return_value = apihub_service.ListVersionsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.delete_api(request) + response = client.list_versions(request) # Establish that the response is the type that we expect. - assert response is None + assert isinstance(response, pagers.ListVersionsPager) + assert response.next_page_token == "next_page_token_value" @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_delete_api_rest_interceptors(null_interceptor): +def test_list_versions_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -9428,11 +26712,17 @@ def test_delete_api_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_delete_api" + transports.ApiHubRestInterceptor, "post_list_versions" + ) as post, mock.patch.object( + transports.ApiHubRestInterceptor, "post_list_versions_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubRestInterceptor, "pre_list_versions" ) as pre: pre.assert_not_called() - pb_message = apihub_service.DeleteApiRequest.pb( - apihub_service.DeleteApiRequest() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = apihub_service.ListVersionsRequest.pb( + apihub_service.ListVersionsRequest() ) transcode.return_value = { "method": "post", @@ -9444,15 +26734,24 @@ def test_delete_api_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = apihub_service.ListVersionsResponse.to_json( + apihub_service.ListVersionsResponse() + ) + req.return_value.content = return_value - request = apihub_service.DeleteApiRequest() + request = apihub_service.ListVersionsRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata + post.return_value = apihub_service.ListVersionsResponse() + post_with_metadata.return_value = ( + apihub_service.ListVersionsResponse(), + metadata, + ) - client.delete_api( + client.list_versions( request, metadata=[ ("key", "val"), @@ -9461,16 +26760,22 @@ def test_delete_api_rest_interceptors(null_interceptor): ) pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() -def test_create_version_rest_bad_request( - request_type=apihub_service.CreateVersionRequest, +def test_update_version_rest_bad_request( + request_type=apihub_service.UpdateVersionRequest, ): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2/apis/sample3"} + request_init = { + "version": { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + } + } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -9485,25 +26790,29 @@ def test_create_version_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_version(request) + client.update_version(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.CreateVersionRequest, + apihub_service.UpdateVersionRequest, dict, ], ) -def test_create_version_rest_call_success(request_type): +def test_update_version_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2/apis/sample3"} + request_init = { + "version": { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + } + } request_init["version"] = { - "name": "name_value", + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4", "display_name": "display_name_value", "description": "description_value", "documentation": {"external_uri": "external_uri_value"}, @@ -9551,209 +26860,68 @@ def test_create_version_rest_call_success(request_type): # See https://github.com/googleapis/gapic-generator-python/issues/1748 # Determine if the message type is proto-plus or protobuf - test_field = apihub_service.CreateVersionRequest.meta.fields["version"] + test_field = apihub_service.UpdateVersionRequest.meta.fields["version"] def get_message_fields(field): # Given a field which is a message (composite type), return a list with # all the fields of the message. # If the field is not a composite type, return an empty list. - message_fields = [] - - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") - - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields - - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] - - subfields_not_in_runtime = [] - - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["version"].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value - - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) - - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range(0, len(request_init["version"][field])): - del request_init["version"][field][i][subfield] - else: - del request_init["version"][field][subfield] - request = request_type(**request_init) - - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Version( - name="name_value", - display_name="display_name_value", - description="description_value", - specs=["specs_value"], - api_operations=["api_operations_value"], - definitions=["definitions_value"], - deployments=["deployments_value"], - selected_deployment="selected_deployment_value", - ) - - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = common_fields.Version.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.create_version(request) - - # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.Version) - assert response.name == "name_value" - assert response.display_name == "display_name_value" - assert response.description == "description_value" - assert response.specs == ["specs_value"] - assert response.api_operations == ["api_operations_value"] - assert response.definitions == ["definitions_value"] - assert response.deployments == ["deployments_value"] - assert response.selected_deployment == "selected_deployment_value" - - -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_create_version_rest_interceptors(null_interceptor): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), - ) - client = ApiHubClient(transport=transport) - - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_create_version" - ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_create_version_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_create_version" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = apihub_service.CreateVersionRequest.pb( - apihub_service.CreateVersionRequest() - ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.Version.to_json(common_fields.Version()) - req.return_value.content = return_value - - request = apihub_service.CreateVersionRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = common_fields.Version() - post_with_metadata.return_value = common_fields.Version(), metadata - - client.create_version( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], - ) - - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + message_fields = [] + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") -def test_get_version_rest_bad_request(request_type=apihub_service.GetVersionRequest): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" - } - request = request_type(**request_init) + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_version(request) + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + subfields_not_in_runtime = [] -@pytest.mark.parametrize( - "request_type", - [ - apihub_service.GetVersionRequest, - dict, - ], -) -def test_get_version_rest_call_success(request_type): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["version"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" - } + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["version"][field])): + del request_init["version"][field][i][subfield] + else: + del request_init["version"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -9780,7 +26948,7 @@ def test_get_version_rest_call_success(request_type): response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_version(request) + response = client.update_version(request) # Establish that the response is the type that we expect. assert isinstance(response, common_fields.Version) @@ -9795,7 +26963,7 @@ def test_get_version_rest_call_success(request_type): @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_version_rest_interceptors(null_interceptor): +def test_update_version_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -9807,17 +26975,17 @@ def test_get_version_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_get_version" + transports.ApiHubRestInterceptor, "post_update_version" ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_get_version_with_metadata" + transports.ApiHubRestInterceptor, "post_update_version_with_metadata" ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_get_version" + transports.ApiHubRestInterceptor, "pre_update_version" ) as pre: pre.assert_not_called() post.assert_not_called() post_with_metadata.assert_not_called() - pb_message = apihub_service.GetVersionRequest.pb( - apihub_service.GetVersionRequest() + pb_message = apihub_service.UpdateVersionRequest.pb( + apihub_service.UpdateVersionRequest() ) transcode.return_value = { "method": "post", @@ -9832,7 +27000,7 @@ def test_get_version_rest_interceptors(null_interceptor): return_value = common_fields.Version.to_json(common_fields.Version()) req.return_value.content = return_value - request = apihub_service.GetVersionRequest() + request = apihub_service.UpdateVersionRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), @@ -9841,7 +27009,7 @@ def test_get_version_rest_interceptors(null_interceptor): post.return_value = common_fields.Version() post_with_metadata.return_value = common_fields.Version(), metadata - client.get_version( + client.update_version( request, metadata=[ ("key", "val"), @@ -9854,14 +27022,16 @@ def test_get_version_rest_interceptors(null_interceptor): post_with_metadata.assert_called_once() -def test_list_versions_rest_bad_request( - request_type=apihub_service.ListVersionsRequest, +def test_delete_version_rest_bad_request( + request_type=apihub_service.DeleteVersionRequest, ): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2/apis/sample3"} + request_init = { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -9876,51 +27046,47 @@ def test_list_versions_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_versions(request) + client.delete_version(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.ListVersionsRequest, + apihub_service.DeleteVersionRequest, dict, ], ) -def test_list_versions_rest_call_success(request_type): +def test_delete_version_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2/apis/sample3"} + request_init = { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + } request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = apihub_service.ListVersionsResponse( - next_page_token="next_page_token_value", - ) + return_value = None # Wrap the value into a proper Response obj response_value = mock.Mock() response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = apihub_service.ListVersionsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + json_return_value = "" response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.list_versions(request) + response = client.delete_version(request) # Establish that the response is the type that we expect. - assert isinstance(response, pagers.ListVersionsPager) - assert response.next_page_token == "next_page_token_value" + assert response is None @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_list_versions_rest_interceptors(null_interceptor): +def test_delete_version_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -9932,17 +27098,11 @@ def test_list_versions_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_list_versions" - ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_list_versions_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_list_versions" + transports.ApiHubRestInterceptor, "pre_delete_version" ) as pre: pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = apihub_service.ListVersionsRequest.pb( - apihub_service.ListVersionsRequest() + pb_message = apihub_service.DeleteVersionRequest.pb( + apihub_service.DeleteVersionRequest() ) transcode.return_value = { "method": "post", @@ -9954,24 +27114,15 @@ def test_list_versions_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = apihub_service.ListVersionsResponse.to_json( - apihub_service.ListVersionsResponse() - ) - req.return_value.content = return_value - request = apihub_service.ListVersionsRequest() + request = apihub_service.DeleteVersionRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = apihub_service.ListVersionsResponse() - post_with_metadata.return_value = ( - apihub_service.ListVersionsResponse(), - metadata, - ) - client.list_versions( + client.delete_version( request, metadata=[ ("key", "val"), @@ -9980,21 +27131,15 @@ def test_list_versions_rest_interceptors(null_interceptor): ) pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() -def test_update_version_rest_bad_request( - request_type=apihub_service.UpdateVersionRequest, -): +def test_create_spec_rest_bad_request(request_type=apihub_service.CreateSpecRequest): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = { - "version": { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" - } + "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" } request = request_type(**request_init) @@ -10010,39 +27155,29 @@ def test_update_version_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.update_version(request) + client.create_spec(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.UpdateVersionRequest, + apihub_service.CreateSpecRequest, dict, ], ) -def test_update_version_rest_call_success(request_type): +def test_create_spec_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = { - "version": { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" - } + "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" } - request_init["version"] = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4", + request_init["spec"] = { + "name": "name_value", "display_name": "display_name_value", - "description": "description_value", - "documentation": {"external_uri": "external_uri_value"}, - "specs": ["specs_value1", "specs_value2"], - "api_operations": ["api_operations_value1", "api_operations_value2"], - "definitions": ["definitions_value1", "definitions_value2"], - "deployments": ["deployments_value1", "deployments_value2"], - "create_time": {"seconds": 751, "nanos": 543}, - "update_time": {}, - "lifecycle": { + "spec_type": { "enum_values": { "values": [ { @@ -10058,10 +27193,37 @@ def test_update_version_rest_call_success(request_type): "uri_values": {}, "attribute": "attribute_value", }, - "compliance": {}, - "accreditation": {}, + "contents": {"contents": b"contents_blob", "mime_type": "mime_type_value"}, + "details": { + "open_api_spec_details": { + "format_": 1, + "version": "version_value", + "owner": {"display_name": "display_name_value", "email": "email_value"}, + }, + "description": "description_value", + }, + "source_uri": "source_uri_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "lint_response": { + "issues": [ + { + "code": "code_value", + "path": ["path_value1", "path_value2"], + "message": "message_value", + "severity": 1, + "range_": {"start": {"line": 424, "character": 941}, "end": {}}, + } + ], + "summary": [{"severity": 1, "count": 553}], + "state": 1, + "source": "source_value", + "linter": 1, + "create_time": {}, + }, "attributes": {}, - "selected_deployment": "selected_deployment_value", + "documentation": {"external_uri": "external_uri_value"}, + "parsing_mode": 1, "source_metadata": [ { "plugin_instance_action_source": { @@ -10080,7 +27242,7 @@ def test_update_version_rest_call_success(request_type): # See https://github.com/googleapis/gapic-generator-python/issues/1748 # Determine if the message type is proto-plus or protobuf - test_field = apihub_service.UpdateVersionRequest.meta.fields["version"] + test_field = apihub_service.CreateSpecRequest.meta.fields["spec"] def get_message_fields(field): # Given a field which is a message (composite type), return a list with @@ -10108,7 +27270,7 @@ def get_message_fields(field): # For each item in the sample request, create a list of sub fields which are not present at runtime # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["version"].items(): # pragma: NO COVER + for field, value in request_init["spec"].items(): # pragma: NO COVER result = None is_repeated = False # For repeated fields @@ -10138,24 +27300,20 @@ def get_message_fields(field): subfield = subfield_to_delete.get("subfield") if subfield: if field_repeated: - for i in range(0, len(request_init["version"][field])): - del request_init["version"][field][i][subfield] + for i in range(0, len(request_init["spec"][field])): + del request_init["spec"][field][i][subfield] else: - del request_init["version"][field][subfield] + del request_init["spec"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = common_fields.Version( + return_value = common_fields.Spec( name="name_value", display_name="display_name_value", - description="description_value", - specs=["specs_value"], - api_operations=["api_operations_value"], - definitions=["definitions_value"], - deployments=["deployments_value"], - selected_deployment="selected_deployment_value", + source_uri="source_uri_value", + parsing_mode=common_fields.Spec.ParsingMode.RELAXED, ) # Wrap the value into a proper Response obj @@ -10163,27 +27321,23 @@ def get_message_fields(field): response_value.status_code = 200 # Convert return value to protobuf type - return_value = common_fields.Version.pb(return_value) + return_value = common_fields.Spec.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.update_version(request) + response = client.create_spec(request) # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.Version) + assert isinstance(response, common_fields.Spec) assert response.name == "name_value" assert response.display_name == "display_name_value" - assert response.description == "description_value" - assert response.specs == ["specs_value"] - assert response.api_operations == ["api_operations_value"] - assert response.definitions == ["definitions_value"] - assert response.deployments == ["deployments_value"] - assert response.selected_deployment == "selected_deployment_value" + assert response.source_uri == "source_uri_value" + assert response.parsing_mode == common_fields.Spec.ParsingMode.RELAXED @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_update_version_rest_interceptors(null_interceptor): +def test_create_spec_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -10195,17 +27349,17 @@ def test_update_version_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_update_version" + transports.ApiHubRestInterceptor, "post_create_spec" ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_update_version_with_metadata" + transports.ApiHubRestInterceptor, "post_create_spec_with_metadata" ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_update_version" + transports.ApiHubRestInterceptor, "pre_create_spec" ) as pre: pre.assert_not_called() post.assert_not_called() post_with_metadata.assert_not_called() - pb_message = apihub_service.UpdateVersionRequest.pb( - apihub_service.UpdateVersionRequest() + pb_message = apihub_service.CreateSpecRequest.pb( + apihub_service.CreateSpecRequest() ) transcode.return_value = { "method": "post", @@ -10217,19 +27371,19 @@ def test_update_version_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.Version.to_json(common_fields.Version()) + return_value = common_fields.Spec.to_json(common_fields.Spec()) req.return_value.content = return_value - request = apihub_service.UpdateVersionRequest() + request = apihub_service.CreateSpecRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = common_fields.Version() - post_with_metadata.return_value = common_fields.Version(), metadata + post.return_value = common_fields.Spec() + post_with_metadata.return_value = common_fields.Spec(), metadata - client.update_version( + client.create_spec( request, metadata=[ ("key", "val"), @@ -10242,15 +27396,13 @@ def test_update_version_rest_interceptors(null_interceptor): post_with_metadata.assert_called_once() -def test_delete_version_rest_bad_request( - request_type=apihub_service.DeleteVersionRequest, -): +def test_get_spec_rest_bad_request(request_type=apihub_service.GetSpecRequest): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" } request = request_type(**request_init) @@ -10266,47 +27418,59 @@ def test_delete_version_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_version(request) + client.get_spec(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.DeleteVersionRequest, + apihub_service.GetSpecRequest, dict, ], ) -def test_delete_version_rest_call_success(request_type): +def test_get_spec_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" } request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = None + return_value = common_fields.Spec( + name="name_value", + display_name="display_name_value", + source_uri="source_uri_value", + parsing_mode=common_fields.Spec.ParsingMode.RELAXED, + ) # Wrap the value into a proper Response obj response_value = mock.Mock() response_value.status_code = 200 - json_return_value = "" + + # Convert return value to protobuf type + return_value = common_fields.Spec.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.delete_version(request) + response = client.get_spec(request) # Establish that the response is the type that we expect. - assert response is None + assert isinstance(response, common_fields.Spec) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.source_uri == "source_uri_value" + assert response.parsing_mode == common_fields.Spec.ParsingMode.RELAXED @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_delete_version_rest_interceptors(null_interceptor): +def test_get_spec_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -10318,12 +27482,16 @@ def test_delete_version_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_delete_version" + transports.ApiHubRestInterceptor, "post_get_spec" + ) as post, mock.patch.object( + transports.ApiHubRestInterceptor, "post_get_spec_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubRestInterceptor, "pre_get_spec" ) as pre: pre.assert_not_called() - pb_message = apihub_service.DeleteVersionRequest.pb( - apihub_service.DeleteVersionRequest() - ) + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = apihub_service.GetSpecRequest.pb(apihub_service.GetSpecRequest()) transcode.return_value = { "method": "post", "uri": "my_uri", @@ -10334,15 +27502,19 @@ def test_delete_version_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = common_fields.Spec.to_json(common_fields.Spec()) + req.return_value.content = return_value - request = apihub_service.DeleteVersionRequest() + request = apihub_service.GetSpecRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata + post.return_value = common_fields.Spec() + post_with_metadata.return_value = common_fields.Spec(), metadata - client.delete_version( + client.get_spec( request, metadata=[ ("key", "val"), @@ -10351,15 +27523,19 @@ def test_delete_version_rest_interceptors(null_interceptor): ) pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() -def test_create_spec_rest_bad_request(request_type=apihub_service.CreateSpecRequest): +def test_get_spec_contents_rest_bad_request( + request_type=apihub_service.GetSpecContentsRequest, +): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = { - "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" } request = request_type(**request_init) @@ -10375,165 +27551,33 @@ def test_create_spec_rest_bad_request(request_type=apihub_service.CreateSpecRequ response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_spec(request) + client.get_spec_contents(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.CreateSpecRequest, + apihub_service.GetSpecContentsRequest, dict, ], ) -def test_create_spec_rest_call_success(request_type): +def test_get_spec_contents_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = { - "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" - } - request_init["spec"] = { - "name": "name_value", - "display_name": "display_name_value", - "spec_type": { - "enum_values": { - "values": [ - { - "id": "id_value", - "display_name": "display_name_value", - "description": "description_value", - "immutable": True, - } - ] - }, - "string_values": {"values": ["values_value1", "values_value2"]}, - "json_values": {}, - "uri_values": {}, - "attribute": "attribute_value", - }, - "contents": {"contents": b"contents_blob", "mime_type": "mime_type_value"}, - "details": { - "open_api_spec_details": { - "format_": 1, - "version": "version_value", - "owner": {"display_name": "display_name_value", "email": "email_value"}, - }, - "description": "description_value", - }, - "source_uri": "source_uri_value", - "create_time": {"seconds": 751, "nanos": 543}, - "update_time": {}, - "lint_response": { - "issues": [ - { - "code": "code_value", - "path": ["path_value1", "path_value2"], - "message": "message_value", - "severity": 1, - "range_": {"start": {"line": 424, "character": 941}, "end": {}}, - } - ], - "summary": [{"severity": 1, "count": 553}], - "state": 1, - "source": "source_value", - "linter": 1, - "create_time": {}, - }, - "attributes": {}, - "documentation": {"external_uri": "external_uri_value"}, - "parsing_mode": 1, - "source_metadata": [ - { - "plugin_instance_action_source": { - "plugin_instance": "plugin_instance_value", - "action_id": "action_id_value", - }, - "source_type": 1, - "original_resource_id": "original_resource_id_value", - "original_resource_create_time": {}, - "original_resource_update_time": {}, - } - ], + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" } - # The version of a generated dependency at test runtime may differ from the version used during generation. - # Delete any fields which are not present in the current runtime dependency - # See https://github.com/googleapis/gapic-generator-python/issues/1748 - - # Determine if the message type is proto-plus or protobuf - test_field = apihub_service.CreateSpecRequest.meta.fields["spec"] - - def get_message_fields(field): - # Given a field which is a message (composite type), return a list with - # all the fields of the message. - # If the field is not a composite type, return an empty list. - message_fields = [] - - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") - - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields - - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] - - subfields_not_in_runtime = [] - - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["spec"].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value - - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) - - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range(0, len(request_init["spec"][field])): - del request_init["spec"][field][i][subfield] - else: - del request_init["spec"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = common_fields.Spec( - name="name_value", - display_name="display_name_value", - source_uri="source_uri_value", - parsing_mode=common_fields.Spec.ParsingMode.RELAXED, + return_value = common_fields.SpecContents( + contents=b"contents_blob", + mime_type="mime_type_value", ) # Wrap the value into a proper Response obj @@ -10541,23 +27585,21 @@ def get_message_fields(field): response_value.status_code = 200 # Convert return value to protobuf type - return_value = common_fields.Spec.pb(return_value) + return_value = common_fields.SpecContents.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.create_spec(request) + response = client.get_spec_contents(request) # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.Spec) - assert response.name == "name_value" - assert response.display_name == "display_name_value" - assert response.source_uri == "source_uri_value" - assert response.parsing_mode == common_fields.Spec.ParsingMode.RELAXED + assert isinstance(response, common_fields.SpecContents) + assert response.contents == b"contents_blob" + assert response.mime_type == "mime_type_value" @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_create_spec_rest_interceptors(null_interceptor): +def test_get_spec_contents_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -10569,17 +27611,17 @@ def test_create_spec_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_create_spec" + transports.ApiHubRestInterceptor, "post_get_spec_contents" ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_create_spec_with_metadata" + transports.ApiHubRestInterceptor, "post_get_spec_contents_with_metadata" ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_create_spec" + transports.ApiHubRestInterceptor, "pre_get_spec_contents" ) as pre: pre.assert_not_called() post.assert_not_called() post_with_metadata.assert_not_called() - pb_message = apihub_service.CreateSpecRequest.pb( - apihub_service.CreateSpecRequest() + pb_message = apihub_service.GetSpecContentsRequest.pb( + apihub_service.GetSpecContentsRequest() ) transcode.return_value = { "method": "post", @@ -10591,19 +27633,19 @@ def test_create_spec_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.Spec.to_json(common_fields.Spec()) + return_value = common_fields.SpecContents.to_json(common_fields.SpecContents()) req.return_value.content = return_value - request = apihub_service.CreateSpecRequest() + request = apihub_service.GetSpecContentsRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = common_fields.Spec() - post_with_metadata.return_value = common_fields.Spec(), metadata + post.return_value = common_fields.SpecContents() + post_with_metadata.return_value = common_fields.SpecContents(), metadata - client.create_spec( + client.get_spec_contents( request, metadata=[ ("key", "val"), @@ -10616,13 +27658,13 @@ def test_create_spec_rest_interceptors(null_interceptor): post_with_metadata.assert_called_once() -def test_get_spec_rest_bad_request(request_type=apihub_service.GetSpecRequest): +def test_list_specs_rest_bad_request(request_type=apihub_service.ListSpecsRequest): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" + "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" } request = request_type(**request_init) @@ -10638,35 +27680,32 @@ def test_get_spec_rest_bad_request(request_type=apihub_service.GetSpecRequest): response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_spec(request) + client.list_specs(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.GetSpecRequest, + apihub_service.ListSpecsRequest, dict, ], ) -def test_get_spec_rest_call_success(request_type): +def test_list_specs_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" + "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" } request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = common_fields.Spec( - name="name_value", - display_name="display_name_value", - source_uri="source_uri_value", - parsing_mode=common_fields.Spec.ParsingMode.RELAXED, + return_value = apihub_service.ListSpecsResponse( + next_page_token="next_page_token_value", ) # Wrap the value into a proper Response obj @@ -10674,23 +27713,20 @@ def test_get_spec_rest_call_success(request_type): response_value.status_code = 200 # Convert return value to protobuf type - return_value = common_fields.Spec.pb(return_value) + return_value = apihub_service.ListSpecsResponse.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_spec(request) + response = client.list_specs(request) # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.Spec) - assert response.name == "name_value" - assert response.display_name == "display_name_value" - assert response.source_uri == "source_uri_value" - assert response.parsing_mode == common_fields.Spec.ParsingMode.RELAXED + assert isinstance(response, pagers.ListSpecsPager) + assert response.next_page_token == "next_page_token_value" @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_spec_rest_interceptors(null_interceptor): +def test_list_specs_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -10702,16 +27738,18 @@ def test_get_spec_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_get_spec" + transports.ApiHubRestInterceptor, "post_list_specs" ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_get_spec_with_metadata" + transports.ApiHubRestInterceptor, "post_list_specs_with_metadata" ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_get_spec" + transports.ApiHubRestInterceptor, "pre_list_specs" ) as pre: pre.assert_not_called() post.assert_not_called() post_with_metadata.assert_not_called() - pb_message = apihub_service.GetSpecRequest.pb(apihub_service.GetSpecRequest()) + pb_message = apihub_service.ListSpecsRequest.pb( + apihub_service.ListSpecsRequest() + ) transcode.return_value = { "method": "post", "uri": "my_uri", @@ -10722,19 +27760,21 @@ def test_get_spec_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.Spec.to_json(common_fields.Spec()) + return_value = apihub_service.ListSpecsResponse.to_json( + apihub_service.ListSpecsResponse() + ) req.return_value.content = return_value - request = apihub_service.GetSpecRequest() + request = apihub_service.ListSpecsRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = common_fields.Spec() - post_with_metadata.return_value = common_fields.Spec(), metadata + post.return_value = apihub_service.ListSpecsResponse() + post_with_metadata.return_value = apihub_service.ListSpecsResponse(), metadata - client.get_spec( + client.list_specs( request, metadata=[ ("key", "val"), @@ -10747,15 +27787,15 @@ def test_get_spec_rest_interceptors(null_interceptor): post_with_metadata.assert_called_once() -def test_get_spec_contents_rest_bad_request( - request_type=apihub_service.GetSpecContentsRequest, -): +def test_update_spec_rest_bad_request(request_type=apihub_service.UpdateSpecRequest): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" + "spec": { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" + } } request = request_type(**request_init) @@ -10771,33 +27811,167 @@ def test_get_spec_contents_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_spec_contents(request) + client.update_spec(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.GetSpecContentsRequest, + apihub_service.UpdateSpecRequest, dict, ], ) -def test_get_spec_contents_rest_call_success(request_type): +def test_update_spec_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" + "spec": { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" + } + } + request_init["spec"] = { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5", + "display_name": "display_name_value", + "spec_type": { + "enum_values": { + "values": [ + { + "id": "id_value", + "display_name": "display_name_value", + "description": "description_value", + "immutable": True, + } + ] + }, + "string_values": {"values": ["values_value1", "values_value2"]}, + "json_values": {}, + "uri_values": {}, + "attribute": "attribute_value", + }, + "contents": {"contents": b"contents_blob", "mime_type": "mime_type_value"}, + "details": { + "open_api_spec_details": { + "format_": 1, + "version": "version_value", + "owner": {"display_name": "display_name_value", "email": "email_value"}, + }, + "description": "description_value", + }, + "source_uri": "source_uri_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "lint_response": { + "issues": [ + { + "code": "code_value", + "path": ["path_value1", "path_value2"], + "message": "message_value", + "severity": 1, + "range_": {"start": {"line": 424, "character": 941}, "end": {}}, + } + ], + "summary": [{"severity": 1, "count": 553}], + "state": 1, + "source": "source_value", + "linter": 1, + "create_time": {}, + }, + "attributes": {}, + "documentation": {"external_uri": "external_uri_value"}, + "parsing_mode": 1, + "source_metadata": [ + { + "plugin_instance_action_source": { + "plugin_instance": "plugin_instance_value", + "action_id": "action_id_value", + }, + "source_type": 1, + "original_resource_id": "original_resource_id_value", + "original_resource_create_time": {}, + "original_resource_update_time": {}, + } + ], } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = apihub_service.UpdateSpecRequest.meta.fields["spec"] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["spec"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["spec"][field])): + del request_init["spec"][field][i][subfield] + else: + del request_init["spec"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = common_fields.SpecContents( - contents=b"contents_blob", - mime_type="mime_type_value", + return_value = common_fields.Spec( + name="name_value", + display_name="display_name_value", + source_uri="source_uri_value", + parsing_mode=common_fields.Spec.ParsingMode.RELAXED, ) # Wrap the value into a proper Response obj @@ -10805,21 +27979,23 @@ def test_get_spec_contents_rest_call_success(request_type): response_value.status_code = 200 # Convert return value to protobuf type - return_value = common_fields.SpecContents.pb(return_value) + return_value = common_fields.Spec.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_spec_contents(request) + response = client.update_spec(request) # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.SpecContents) - assert response.contents == b"contents_blob" - assert response.mime_type == "mime_type_value" + assert isinstance(response, common_fields.Spec) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.source_uri == "source_uri_value" + assert response.parsing_mode == common_fields.Spec.ParsingMode.RELAXED @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_spec_contents_rest_interceptors(null_interceptor): +def test_update_spec_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -10831,17 +28007,17 @@ def test_get_spec_contents_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_get_spec_contents" + transports.ApiHubRestInterceptor, "post_update_spec" ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_get_spec_contents_with_metadata" + transports.ApiHubRestInterceptor, "post_update_spec_with_metadata" ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_get_spec_contents" + transports.ApiHubRestInterceptor, "pre_update_spec" ) as pre: pre.assert_not_called() post.assert_not_called() post_with_metadata.assert_not_called() - pb_message = apihub_service.GetSpecContentsRequest.pb( - apihub_service.GetSpecContentsRequest() + pb_message = apihub_service.UpdateSpecRequest.pb( + apihub_service.UpdateSpecRequest() ) transcode.return_value = { "method": "post", @@ -10853,19 +28029,19 @@ def test_get_spec_contents_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.SpecContents.to_json(common_fields.SpecContents()) + return_value = common_fields.Spec.to_json(common_fields.Spec()) req.return_value.content = return_value - request = apihub_service.GetSpecContentsRequest() + request = apihub_service.UpdateSpecRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = common_fields.SpecContents() - post_with_metadata.return_value = common_fields.SpecContents(), metadata + post.return_value = common_fields.Spec() + post_with_metadata.return_value = common_fields.Spec(), metadata - client.get_spec_contents( + client.update_spec( request, metadata=[ ("key", "val"), @@ -10878,13 +28054,13 @@ def test_get_spec_contents_rest_interceptors(null_interceptor): post_with_metadata.assert_called_once() -def test_list_specs_rest_bad_request(request_type=apihub_service.ListSpecsRequest): +def test_delete_spec_rest_bad_request(request_type=apihub_service.DeleteSpecRequest): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = { - "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" } request = request_type(**request_init) @@ -10900,53 +28076,47 @@ def test_list_specs_rest_bad_request(request_type=apihub_service.ListSpecsReques response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_specs(request) + client.delete_spec(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.ListSpecsRequest, + apihub_service.DeleteSpecRequest, dict, ], ) -def test_list_specs_rest_call_success(request_type): +def test_delete_spec_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = { - "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" } request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = apihub_service.ListSpecsResponse( - next_page_token="next_page_token_value", - ) + return_value = None # Wrap the value into a proper Response obj response_value = mock.Mock() response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = apihub_service.ListSpecsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + json_return_value = "" response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.list_specs(request) + response = client.delete_spec(request) # Establish that the response is the type that we expect. - assert isinstance(response, pagers.ListSpecsPager) - assert response.next_page_token == "next_page_token_value" + assert response is None @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_list_specs_rest_interceptors(null_interceptor): +def test_delete_spec_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -10958,17 +28128,11 @@ def test_list_specs_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_list_specs" - ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_list_specs_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_list_specs" + transports.ApiHubRestInterceptor, "pre_delete_spec" ) as pre: pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = apihub_service.ListSpecsRequest.pb( - apihub_service.ListSpecsRequest() + pb_message = apihub_service.DeleteSpecRequest.pb( + apihub_service.DeleteSpecRequest() ) transcode.return_value = { "method": "post", @@ -10980,21 +28144,15 @@ def test_list_specs_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = apihub_service.ListSpecsResponse.to_json( - apihub_service.ListSpecsResponse() - ) - req.return_value.content = return_value - request = apihub_service.ListSpecsRequest() + request = apihub_service.DeleteSpecRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = apihub_service.ListSpecsResponse() - post_with_metadata.return_value = apihub_service.ListSpecsResponse(), metadata - client.list_specs( + client.delete_spec( request, metadata=[ ("key", "val"), @@ -11003,19 +28161,17 @@ def test_list_specs_rest_interceptors(null_interceptor): ) pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() -def test_update_spec_rest_bad_request(request_type=apihub_service.UpdateSpecRequest): +def test_create_api_operation_rest_bad_request( + request_type=apihub_service.CreateApiOperationRequest, +): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = { - "spec": { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" - } + "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" } request = request_type(**request_init) @@ -11031,77 +28187,40 @@ def test_update_spec_rest_bad_request(request_type=apihub_service.UpdateSpecRequ response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.update_spec(request) + client.create_api_operation(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.UpdateSpecRequest, + apihub_service.CreateApiOperationRequest, dict, ], ) -def test_update_spec_rest_call_success(request_type): +def test_create_api_operation_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = { - "spec": { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" - } + "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" } - request_init["spec"] = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5", - "display_name": "display_name_value", - "spec_type": { - "enum_values": { - "values": [ - { - "id": "id_value", - "display_name": "display_name_value", - "description": "description_value", - "immutable": True, - } - ] - }, - "string_values": {"values": ["values_value1", "values_value2"]}, - "json_values": {}, - "uri_values": {}, - "attribute": "attribute_value", - }, - "contents": {"contents": b"contents_blob", "mime_type": "mime_type_value"}, + request_init["api_operation"] = { + "name": "name_value", + "spec": "spec_value", "details": { - "open_api_spec_details": { - "format_": 1, - "version": "version_value", - "owner": {"display_name": "display_name_value", "email": "email_value"}, + "http_operation": { + "path": {"path": "path_value", "description": "description_value"}, + "method": 1, }, "description": "description_value", + "documentation": {"external_uri": "external_uri_value"}, + "deprecated": True, }, - "source_uri": "source_uri_value", "create_time": {"seconds": 751, "nanos": 543}, "update_time": {}, - "lint_response": { - "issues": [ - { - "code": "code_value", - "path": ["path_value1", "path_value2"], - "message": "message_value", - "severity": 1, - "range_": {"start": {"line": 424, "character": 941}, "end": {}}, - } - ], - "summary": [{"severity": 1, "count": 553}], - "state": 1, - "source": "source_value", - "linter": 1, - "create_time": {}, - }, "attributes": {}, - "documentation": {"external_uri": "external_uri_value"}, - "parsing_mode": 1, "source_metadata": [ { "plugin_instance_action_source": { @@ -11120,7 +28239,7 @@ def test_update_spec_rest_call_success(request_type): # See https://github.com/googleapis/gapic-generator-python/issues/1748 # Determine if the message type is proto-plus or protobuf - test_field = apihub_service.UpdateSpecRequest.meta.fields["spec"] + test_field = apihub_service.CreateApiOperationRequest.meta.fields["api_operation"] def get_message_fields(field): # Given a field which is a message (composite type), return a list with @@ -11128,70 +28247,199 @@ def get_message_fields(field): # If the field is not a composite type, return an empty list. message_fields = [] - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["api_operation"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["api_operation"][field])): + del request_init["api_operation"][field][i][subfield] + else: + del request_init["api_operation"][field][subfield] + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.ApiOperation( + name="name_value", + spec="spec_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.ApiOperation.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.create_api_operation(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.ApiOperation) + assert response.name == "name_value" + assert response.spec == "spec_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_api_operation_rest_interceptors(null_interceptor): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), + ) + client = ApiHubClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubRestInterceptor, "post_create_api_operation" + ) as post, mock.patch.object( + transports.ApiHubRestInterceptor, "post_create_api_operation_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubRestInterceptor, "pre_create_api_operation" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = apihub_service.CreateApiOperationRequest.pb( + apihub_service.CreateApiOperationRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = common_fields.ApiOperation.to_json(common_fields.ApiOperation()) + req.return_value.content = return_value + + request = apihub_service.CreateApiOperationRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = common_fields.ApiOperation() + post_with_metadata.return_value = common_fields.ApiOperation(), metadata + + client.create_api_operation( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] - subfields_not_in_runtime = [] +def test_get_api_operation_rest_bad_request( + request_type=apihub_service.GetApiOperationRequest, +): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/operations/sample5" + } + request = request_type(**request_init) - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["spec"].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_api_operation(request) - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range(0, len(request_init["spec"][field])): - del request_init["spec"][field][i][subfield] - else: - del request_init["spec"][field][subfield] +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.GetApiOperationRequest, + dict, + ], +) +def test_get_api_operation_rest_call_success(request_type): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/operations/sample5" + } request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = common_fields.Spec( + return_value = common_fields.ApiOperation( name="name_value", - display_name="display_name_value", - source_uri="source_uri_value", - parsing_mode=common_fields.Spec.ParsingMode.RELAXED, + spec="spec_value", ) # Wrap the value into a proper Response obj @@ -11199,23 +28447,21 @@ def get_message_fields(field): response_value.status_code = 200 # Convert return value to protobuf type - return_value = common_fields.Spec.pb(return_value) + return_value = common_fields.ApiOperation.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.update_spec(request) + response = client.get_api_operation(request) # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.Spec) + assert isinstance(response, common_fields.ApiOperation) assert response.name == "name_value" - assert response.display_name == "display_name_value" - assert response.source_uri == "source_uri_value" - assert response.parsing_mode == common_fields.Spec.ParsingMode.RELAXED + assert response.spec == "spec_value" @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_update_spec_rest_interceptors(null_interceptor): +def test_get_api_operation_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -11227,17 +28473,17 @@ def test_update_spec_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_update_spec" + transports.ApiHubRestInterceptor, "post_get_api_operation" ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_update_spec_with_metadata" + transports.ApiHubRestInterceptor, "post_get_api_operation_with_metadata" ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_update_spec" + transports.ApiHubRestInterceptor, "pre_get_api_operation" ) as pre: pre.assert_not_called() post.assert_not_called() post_with_metadata.assert_not_called() - pb_message = apihub_service.UpdateSpecRequest.pb( - apihub_service.UpdateSpecRequest() + pb_message = apihub_service.GetApiOperationRequest.pb( + apihub_service.GetApiOperationRequest() ) transcode.return_value = { "method": "post", @@ -11249,19 +28495,19 @@ def test_update_spec_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.Spec.to_json(common_fields.Spec()) + return_value = common_fields.ApiOperation.to_json(common_fields.ApiOperation()) req.return_value.content = return_value - request = apihub_service.UpdateSpecRequest() + request = apihub_service.GetApiOperationRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = common_fields.Spec() - post_with_metadata.return_value = common_fields.Spec(), metadata + post.return_value = common_fields.ApiOperation() + post_with_metadata.return_value = common_fields.ApiOperation(), metadata - client.update_spec( + client.get_api_operation( request, metadata=[ ("key", "val"), @@ -11274,13 +28520,15 @@ def test_update_spec_rest_interceptors(null_interceptor): post_with_metadata.assert_called_once() -def test_delete_spec_rest_bad_request(request_type=apihub_service.DeleteSpecRequest): +def test_list_api_operations_rest_bad_request( + request_type=apihub_service.ListApiOperationsRequest, +): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" + "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" } request = request_type(**request_init) @@ -11296,47 +28544,53 @@ def test_delete_spec_rest_bad_request(request_type=apihub_service.DeleteSpecRequ response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_spec(request) + client.list_api_operations(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.DeleteSpecRequest, + apihub_service.ListApiOperationsRequest, dict, ], ) -def test_delete_spec_rest_call_success(request_type): +def test_list_api_operations_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" + "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" } request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = None + return_value = apihub_service.ListApiOperationsResponse( + next_page_token="next_page_token_value", + ) # Wrap the value into a proper Response obj response_value = mock.Mock() response_value.status_code = 200 - json_return_value = "" + + # Convert return value to protobuf type + return_value = apihub_service.ListApiOperationsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.delete_spec(request) + response = client.list_api_operations(request) # Establish that the response is the type that we expect. - assert response is None + assert isinstance(response, pagers.ListApiOperationsPager) + assert response.next_page_token == "next_page_token_value" @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_delete_spec_rest_interceptors(null_interceptor): +def test_list_api_operations_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -11348,11 +28602,17 @@ def test_delete_spec_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_delete_spec" + transports.ApiHubRestInterceptor, "post_list_api_operations" + ) as post, mock.patch.object( + transports.ApiHubRestInterceptor, "post_list_api_operations_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubRestInterceptor, "pre_list_api_operations" ) as pre: pre.assert_not_called() - pb_message = apihub_service.DeleteSpecRequest.pb( - apihub_service.DeleteSpecRequest() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = apihub_service.ListApiOperationsRequest.pb( + apihub_service.ListApiOperationsRequest() ) transcode.return_value = { "method": "post", @@ -11364,15 +28624,24 @@ def test_delete_spec_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = apihub_service.ListApiOperationsResponse.to_json( + apihub_service.ListApiOperationsResponse() + ) + req.return_value.content = return_value - request = apihub_service.DeleteSpecRequest() + request = apihub_service.ListApiOperationsRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata + post.return_value = apihub_service.ListApiOperationsResponse() + post_with_metadata.return_value = ( + apihub_service.ListApiOperationsResponse(), + metadata, + ) - client.delete_spec( + client.list_api_operations( request, metadata=[ ("key", "val"), @@ -11381,17 +28650,21 @@ def test_delete_spec_rest_interceptors(null_interceptor): ) pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() -def test_create_api_operation_rest_bad_request( - request_type=apihub_service.CreateApiOperationRequest, +def test_update_api_operation_rest_bad_request( + request_type=apihub_service.UpdateApiOperationRequest, ): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = { - "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + "api_operation": { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/operations/sample5" + } } request = request_type(**request_init) @@ -11407,27 +28680,29 @@ def test_create_api_operation_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_api_operation(request) + client.update_api_operation(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.CreateApiOperationRequest, + apihub_service.UpdateApiOperationRequest, dict, ], ) -def test_create_api_operation_rest_call_success(request_type): +def test_update_api_operation_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = { - "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + "api_operation": { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/operations/sample5" + } } request_init["api_operation"] = { - "name": "name_value", + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/operations/sample5", "spec": "spec_value", "details": { "http_operation": { @@ -11459,7 +28734,7 @@ def test_create_api_operation_rest_call_success(request_type): # See https://github.com/googleapis/gapic-generator-python/issues/1748 # Determine if the message type is proto-plus or protobuf - test_field = apihub_service.CreateApiOperationRequest.meta.fields["api_operation"] + test_field = apihub_service.UpdateApiOperationRequest.meta.fields["api_operation"] def get_message_fields(field): # Given a field which is a message (composite type), return a list with @@ -11541,7 +28816,7 @@ def get_message_fields(field): response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.create_api_operation(request) + response = client.update_api_operation(request) # Establish that the response is the type that we expect. assert isinstance(response, common_fields.ApiOperation) @@ -11550,7 +28825,7 @@ def get_message_fields(field): @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_create_api_operation_rest_interceptors(null_interceptor): +def test_update_api_operation_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -11562,17 +28837,17 @@ def test_create_api_operation_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_create_api_operation" + transports.ApiHubRestInterceptor, "post_update_api_operation" ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_create_api_operation_with_metadata" + transports.ApiHubRestInterceptor, "post_update_api_operation_with_metadata" ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_create_api_operation" + transports.ApiHubRestInterceptor, "pre_update_api_operation" ) as pre: pre.assert_not_called() post.assert_not_called() post_with_metadata.assert_not_called() - pb_message = apihub_service.CreateApiOperationRequest.pb( - apihub_service.CreateApiOperationRequest() + pb_message = apihub_service.UpdateApiOperationRequest.pb( + apihub_service.UpdateApiOperationRequest() ) transcode.return_value = { "method": "post", @@ -11587,7 +28862,7 @@ def test_create_api_operation_rest_interceptors(null_interceptor): return_value = common_fields.ApiOperation.to_json(common_fields.ApiOperation()) req.return_value.content = return_value - request = apihub_service.CreateApiOperationRequest() + request = apihub_service.UpdateApiOperationRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), @@ -11596,7 +28871,7 @@ def test_create_api_operation_rest_interceptors(null_interceptor): post.return_value = common_fields.ApiOperation() post_with_metadata.return_value = common_fields.ApiOperation(), metadata - client.create_api_operation( + client.update_api_operation( request, metadata=[ ("key", "val"), @@ -11609,8 +28884,8 @@ def test_create_api_operation_rest_interceptors(null_interceptor): post_with_metadata.assert_called_once() -def test_get_api_operation_rest_bad_request( - request_type=apihub_service.GetApiOperationRequest, +def test_delete_api_operation_rest_bad_request( + request_type=apihub_service.DeleteApiOperationRequest, ): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" @@ -11633,17 +28908,17 @@ def test_get_api_operation_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_api_operation(request) + client.delete_api_operation(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.GetApiOperationRequest, + apihub_service.DeleteApiOperationRequest, dict, ], ) -def test_get_api_operation_rest_call_success(request_type): +def test_delete_api_operation_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -11657,31 +28932,23 @@ def test_get_api_operation_rest_call_success(request_type): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = common_fields.ApiOperation( - name="name_value", - spec="spec_value", - ) + return_value = None # Wrap the value into a proper Response obj response_value = mock.Mock() response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = common_fields.ApiOperation.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + json_return_value = "" response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_api_operation(request) + response = client.delete_api_operation(request) # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.ApiOperation) - assert response.name == "name_value" - assert response.spec == "spec_value" + assert response is None @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_api_operation_rest_interceptors(null_interceptor): +def test_delete_api_operation_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -11693,17 +28960,11 @@ def test_get_api_operation_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_get_api_operation" - ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_get_api_operation_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_get_api_operation" + transports.ApiHubRestInterceptor, "pre_delete_api_operation" ) as pre: pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = apihub_service.GetApiOperationRequest.pb( - apihub_service.GetApiOperationRequest() + pb_message = apihub_service.DeleteApiOperationRequest.pb( + apihub_service.DeleteApiOperationRequest() ) transcode.return_value = { "method": "post", @@ -11715,19 +28976,15 @@ def test_get_api_operation_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.ApiOperation.to_json(common_fields.ApiOperation()) - req.return_value.content = return_value - request = apihub_service.GetApiOperationRequest() + request = apihub_service.DeleteApiOperationRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = common_fields.ApiOperation() - post_with_metadata.return_value = common_fields.ApiOperation(), metadata - client.get_api_operation( + client.delete_api_operation( request, metadata=[ ("key", "val"), @@ -11736,19 +28993,17 @@ def test_get_api_operation_rest_interceptors(null_interceptor): ) pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() -def test_list_api_operations_rest_bad_request( - request_type=apihub_service.ListApiOperationsRequest, +def test_get_definition_rest_bad_request( + request_type=apihub_service.GetDefinitionRequest, ): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = { - "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/definitions/sample5" } request = request_type(**request_init) @@ -11764,32 +29019,34 @@ def test_list_api_operations_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_api_operations(request) + client.get_definition(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.ListApiOperationsRequest, + apihub_service.GetDefinitionRequest, dict, ], ) -def test_list_api_operations_rest_call_success(request_type): +def test_get_definition_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = { - "parent": "projects/sample1/locations/sample2/apis/sample3/versions/sample4" + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/definitions/sample5" } request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = apihub_service.ListApiOperationsResponse( - next_page_token="next_page_token_value", + return_value = common_fields.Definition( + name="name_value", + spec="spec_value", + type_=common_fields.Definition.Type.SCHEMA, ) # Wrap the value into a proper Response obj @@ -11797,20 +29054,22 @@ def test_list_api_operations_rest_call_success(request_type): response_value.status_code = 200 # Convert return value to protobuf type - return_value = apihub_service.ListApiOperationsResponse.pb(return_value) + return_value = common_fields.Definition.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.list_api_operations(request) + response = client.get_definition(request) # Establish that the response is the type that we expect. - assert isinstance(response, pagers.ListApiOperationsPager) - assert response.next_page_token == "next_page_token_value" + assert isinstance(response, common_fields.Definition) + assert response.name == "name_value" + assert response.spec == "spec_value" + assert response.type_ == common_fields.Definition.Type.SCHEMA @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_list_api_operations_rest_interceptors(null_interceptor): +def test_get_definition_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -11822,17 +29081,17 @@ def test_list_api_operations_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_list_api_operations" + transports.ApiHubRestInterceptor, "post_get_definition" ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_list_api_operations_with_metadata" + transports.ApiHubRestInterceptor, "post_get_definition_with_metadata" ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_list_api_operations" + transports.ApiHubRestInterceptor, "pre_get_definition" ) as pre: pre.assert_not_called() post.assert_not_called() post_with_metadata.assert_not_called() - pb_message = apihub_service.ListApiOperationsRequest.pb( - apihub_service.ListApiOperationsRequest() + pb_message = apihub_service.GetDefinitionRequest.pb( + apihub_service.GetDefinitionRequest() ) transcode.return_value = { "method": "post", @@ -11844,24 +29103,19 @@ def test_list_api_operations_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = apihub_service.ListApiOperationsResponse.to_json( - apihub_service.ListApiOperationsResponse() - ) + return_value = common_fields.Definition.to_json(common_fields.Definition()) req.return_value.content = return_value - request = apihub_service.ListApiOperationsRequest() + request = apihub_service.GetDefinitionRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = apihub_service.ListApiOperationsResponse() - post_with_metadata.return_value = ( - apihub_service.ListApiOperationsResponse(), - metadata, - ) + post.return_value = common_fields.Definition() + post_with_metadata.return_value = common_fields.Definition(), metadata - client.list_api_operations( + client.get_definition( request, metadata=[ ("key", "val"), @@ -11874,18 +29128,14 @@ def test_list_api_operations_rest_interceptors(null_interceptor): post_with_metadata.assert_called_once() -def test_update_api_operation_rest_bad_request( - request_type=apihub_service.UpdateApiOperationRequest, +def test_create_deployment_rest_bad_request( + request_type=apihub_service.CreateDeploymentRequest, ): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = { - "api_operation": { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/operations/sample5" - } - } + request_init = {"parent": "projects/sample1/locations/sample2"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -11900,41 +29150,51 @@ def test_update_api_operation_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.update_api_operation(request) + client.create_deployment(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.UpdateApiOperationRequest, + apihub_service.CreateDeploymentRequest, dict, ], ) -def test_update_api_operation_rest_call_success(request_type): +def test_create_deployment_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = { - "api_operation": { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/operations/sample5" - } - } - request_init["api_operation"] = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/operations/sample5", - "spec": "spec_value", - "details": { - "http_operation": { - "path": {"path": "path_value", "description": "description_value"}, - "method": 1, + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["deployment"] = { + "name": "name_value", + "display_name": "display_name_value", + "description": "description_value", + "documentation": {"external_uri": "external_uri_value"}, + "deployment_type": { + "enum_values": { + "values": [ + { + "id": "id_value", + "display_name": "display_name_value", + "description": "description_value", + "immutable": True, + } + ] }, - "description": "description_value", - "documentation": {"external_uri": "external_uri_value"}, - "deprecated": True, + "string_values": {"values": ["values_value1", "values_value2"]}, + "json_values": {}, + "uri_values": {}, + "attribute": "attribute_value", }, + "resource_uri": "resource_uri_value", + "endpoints": ["endpoints_value1", "endpoints_value2"], + "api_versions": ["api_versions_value1", "api_versions_value2"], "create_time": {"seconds": 751, "nanos": 543}, "update_time": {}, + "slo": {}, + "environment": {}, "attributes": {}, "source_metadata": [ { @@ -11948,13 +29208,17 @@ def test_update_api_operation_rest_call_success(request_type): "original_resource_update_time": {}, } ], + "management_url": {}, + "source_uri": {}, + "source_project": "source_project_value", + "source_environment": "source_environment_value", } # The version of a generated dependency at test runtime may differ from the version used during generation. # Delete any fields which are not present in the current runtime dependency # See https://github.com/googleapis/gapic-generator-python/issues/1748 # Determine if the message type is proto-plus or protobuf - test_field = apihub_service.UpdateApiOperationRequest.meta.fields["api_operation"] + test_field = apihub_service.CreateDeploymentRequest.meta.fields["deployment"] def get_message_fields(field): # Given a field which is a message (composite type), return a list with @@ -11982,7 +29246,7 @@ def get_message_fields(field): # For each item in the sample request, create a list of sub fields which are not present at runtime # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["api_operation"].items(): # pragma: NO COVER + for field, value in request_init["deployment"].items(): # pragma: NO COVER result = None is_repeated = False # For repeated fields @@ -12012,18 +29276,24 @@ def get_message_fields(field): subfield = subfield_to_delete.get("subfield") if subfield: if field_repeated: - for i in range(0, len(request_init["api_operation"][field])): - del request_init["api_operation"][field][i][subfield] + for i in range(0, len(request_init["deployment"][field])): + del request_init["deployment"][field][i][subfield] else: - del request_init["api_operation"][field][subfield] + del request_init["deployment"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = common_fields.ApiOperation( + return_value = common_fields.Deployment( name="name_value", - spec="spec_value", + display_name="display_name_value", + description="description_value", + resource_uri="resource_uri_value", + endpoints=["endpoints_value"], + api_versions=["api_versions_value"], + source_project="source_project_value", + source_environment="source_environment_value", ) # Wrap the value into a proper Response obj @@ -12031,21 +29301,27 @@ def get_message_fields(field): response_value.status_code = 200 # Convert return value to protobuf type - return_value = common_fields.ApiOperation.pb(return_value) + return_value = common_fields.Deployment.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.update_api_operation(request) + response = client.create_deployment(request) # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.ApiOperation) + assert isinstance(response, common_fields.Deployment) assert response.name == "name_value" - assert response.spec == "spec_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.resource_uri == "resource_uri_value" + assert response.endpoints == ["endpoints_value"] + assert response.api_versions == ["api_versions_value"] + assert response.source_project == "source_project_value" + assert response.source_environment == "source_environment_value" @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_update_api_operation_rest_interceptors(null_interceptor): +def test_create_deployment_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -12057,17 +29333,17 @@ def test_update_api_operation_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_update_api_operation" + transports.ApiHubRestInterceptor, "post_create_deployment" ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_update_api_operation_with_metadata" + transports.ApiHubRestInterceptor, "post_create_deployment_with_metadata" ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_update_api_operation" + transports.ApiHubRestInterceptor, "pre_create_deployment" ) as pre: pre.assert_not_called() post.assert_not_called() post_with_metadata.assert_not_called() - pb_message = apihub_service.UpdateApiOperationRequest.pb( - apihub_service.UpdateApiOperationRequest() + pb_message = apihub_service.CreateDeploymentRequest.pb( + apihub_service.CreateDeploymentRequest() ) transcode.return_value = { "method": "post", @@ -12079,19 +29355,19 @@ def test_update_api_operation_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.ApiOperation.to_json(common_fields.ApiOperation()) + return_value = common_fields.Deployment.to_json(common_fields.Deployment()) req.return_value.content = return_value - request = apihub_service.UpdateApiOperationRequest() + request = apihub_service.CreateDeploymentRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = common_fields.ApiOperation() - post_with_metadata.return_value = common_fields.ApiOperation(), metadata + post.return_value = common_fields.Deployment() + post_with_metadata.return_value = common_fields.Deployment(), metadata - client.update_api_operation( + client.create_deployment( request, metadata=[ ("key", "val"), @@ -12104,16 +29380,14 @@ def test_update_api_operation_rest_interceptors(null_interceptor): post_with_metadata.assert_called_once() -def test_delete_api_operation_rest_bad_request( - request_type=apihub_service.DeleteApiOperationRequest, +def test_get_deployment_rest_bad_request( + request_type=apihub_service.GetDeploymentRequest, ): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/operations/sample5" - } + request_init = {"name": "projects/sample1/locations/sample2/deployments/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -12128,47 +29402,65 @@ def test_delete_api_operation_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_api_operation(request) + client.get_deployment(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.DeleteApiOperationRequest, + apihub_service.GetDeploymentRequest, dict, ], ) -def test_delete_api_operation_rest_call_success(request_type): +def test_get_deployment_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/operations/sample5" - } + request_init = {"name": "projects/sample1/locations/sample2/deployments/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = None + return_value = common_fields.Deployment( + name="name_value", + display_name="display_name_value", + description="description_value", + resource_uri="resource_uri_value", + endpoints=["endpoints_value"], + api_versions=["api_versions_value"], + source_project="source_project_value", + source_environment="source_environment_value", + ) # Wrap the value into a proper Response obj response_value = mock.Mock() response_value.status_code = 200 - json_return_value = "" + + # Convert return value to protobuf type + return_value = common_fields.Deployment.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.delete_api_operation(request) + response = client.get_deployment(request) # Establish that the response is the type that we expect. - assert response is None + assert isinstance(response, common_fields.Deployment) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.resource_uri == "resource_uri_value" + assert response.endpoints == ["endpoints_value"] + assert response.api_versions == ["api_versions_value"] + assert response.source_project == "source_project_value" + assert response.source_environment == "source_environment_value" @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_delete_api_operation_rest_interceptors(null_interceptor): +def test_get_deployment_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -12180,11 +29472,17 @@ def test_delete_api_operation_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_delete_api_operation" + transports.ApiHubRestInterceptor, "post_get_deployment" + ) as post, mock.patch.object( + transports.ApiHubRestInterceptor, "post_get_deployment_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubRestInterceptor, "pre_get_deployment" ) as pre: pre.assert_not_called() - pb_message = apihub_service.DeleteApiOperationRequest.pb( - apihub_service.DeleteApiOperationRequest() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = apihub_service.GetDeploymentRequest.pb( + apihub_service.GetDeploymentRequest() ) transcode.return_value = { "method": "post", @@ -12196,15 +29494,19 @@ def test_delete_api_operation_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = common_fields.Deployment.to_json(common_fields.Deployment()) + req.return_value.content = return_value - request = apihub_service.DeleteApiOperationRequest() + request = apihub_service.GetDeploymentRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata + post.return_value = common_fields.Deployment() + post_with_metadata.return_value = common_fields.Deployment(), metadata - client.delete_api_operation( + client.get_deployment( request, metadata=[ ("key", "val"), @@ -12213,18 +29515,18 @@ def test_delete_api_operation_rest_interceptors(null_interceptor): ) pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() -def test_get_definition_rest_bad_request( - request_type=apihub_service.GetDefinitionRequest, +def test_list_deployments_rest_bad_request( + request_type=apihub_service.ListDeploymentsRequest, ): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/definitions/sample5" - } + request_init = {"parent": "projects/sample1/locations/sample2"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -12239,34 +29541,30 @@ def test_get_definition_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_definition(request) + client.list_deployments(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.GetDefinitionRequest, + apihub_service.ListDeploymentsRequest, dict, ], ) -def test_get_definition_rest_call_success(request_type): +def test_list_deployments_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/definitions/sample5" - } + request_init = {"parent": "projects/sample1/locations/sample2"} request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = common_fields.Definition( - name="name_value", - spec="spec_value", - type_=common_fields.Definition.Type.SCHEMA, + return_value = apihub_service.ListDeploymentsResponse( + next_page_token="next_page_token_value", ) # Wrap the value into a proper Response obj @@ -12274,22 +29572,20 @@ def test_get_definition_rest_call_success(request_type): response_value.status_code = 200 # Convert return value to protobuf type - return_value = common_fields.Definition.pb(return_value) + return_value = apihub_service.ListDeploymentsResponse.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_definition(request) + response = client.list_deployments(request) # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.Definition) - assert response.name == "name_value" - assert response.spec == "spec_value" - assert response.type_ == common_fields.Definition.Type.SCHEMA + assert isinstance(response, pagers.ListDeploymentsPager) + assert response.next_page_token == "next_page_token_value" @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_definition_rest_interceptors(null_interceptor): +def test_list_deployments_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -12301,17 +29597,17 @@ def test_get_definition_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_get_definition" + transports.ApiHubRestInterceptor, "post_list_deployments" ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_get_definition_with_metadata" + transports.ApiHubRestInterceptor, "post_list_deployments_with_metadata" ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_get_definition" + transports.ApiHubRestInterceptor, "pre_list_deployments" ) as pre: pre.assert_not_called() post.assert_not_called() post_with_metadata.assert_not_called() - pb_message = apihub_service.GetDefinitionRequest.pb( - apihub_service.GetDefinitionRequest() + pb_message = apihub_service.ListDeploymentsRequest.pb( + apihub_service.ListDeploymentsRequest() ) transcode.return_value = { "method": "post", @@ -12323,19 +29619,24 @@ def test_get_definition_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.Definition.to_json(common_fields.Definition()) + return_value = apihub_service.ListDeploymentsResponse.to_json( + apihub_service.ListDeploymentsResponse() + ) req.return_value.content = return_value - request = apihub_service.GetDefinitionRequest() + request = apihub_service.ListDeploymentsRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = common_fields.Definition() - post_with_metadata.return_value = common_fields.Definition(), metadata + post.return_value = apihub_service.ListDeploymentsResponse() + post_with_metadata.return_value = ( + apihub_service.ListDeploymentsResponse(), + metadata, + ) - client.get_definition( + client.list_deployments( request, metadata=[ ("key", "val"), @@ -12348,14 +29649,16 @@ def test_get_definition_rest_interceptors(null_interceptor): post_with_metadata.assert_called_once() -def test_create_deployment_rest_bad_request( - request_type=apihub_service.CreateDeploymentRequest, +def test_update_deployment_rest_bad_request( + request_type=apihub_service.UpdateDeploymentRequest, ): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = { + "deployment": {"name": "projects/sample1/locations/sample2/deployments/sample3"} + } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -12370,25 +29673,27 @@ def test_create_deployment_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_deployment(request) + client.update_deployment(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.CreateDeploymentRequest, + apihub_service.UpdateDeploymentRequest, dict, ], ) -def test_create_deployment_rest_call_success(request_type): +def test_update_deployment_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = { + "deployment": {"name": "projects/sample1/locations/sample2/deployments/sample3"} + } request_init["deployment"] = { - "name": "name_value", + "name": "projects/sample1/locations/sample2/deployments/sample3", "display_name": "display_name_value", "description": "description_value", "documentation": {"external_uri": "external_uri_value"}, @@ -12438,7 +29743,7 @@ def test_create_deployment_rest_call_success(request_type): # See https://github.com/googleapis/gapic-generator-python/issues/1748 # Determine if the message type is proto-plus or protobuf - test_field = apihub_service.CreateDeploymentRequest.meta.fields["deployment"] + test_field = apihub_service.UpdateDeploymentRequest.meta.fields["deployment"] def get_message_fields(field): # Given a field which is a message (composite type), return a list with @@ -12468,177 +29773,38 @@ def get_message_fields(field): # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime for field, value in request_init["deployment"].items(): # pragma: NO COVER result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value - - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) - - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range(0, len(request_init["deployment"][field])): - del request_init["deployment"][field][i][subfield] - else: - del request_init["deployment"][field][subfield] - request = request_type(**request_init) - - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Deployment( - name="name_value", - display_name="display_name_value", - description="description_value", - resource_uri="resource_uri_value", - endpoints=["endpoints_value"], - api_versions=["api_versions_value"], - source_project="source_project_value", - source_environment="source_environment_value", - ) - - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = common_fields.Deployment.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.create_deployment(request) - - # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.Deployment) - assert response.name == "name_value" - assert response.display_name == "display_name_value" - assert response.description == "description_value" - assert response.resource_uri == "resource_uri_value" - assert response.endpoints == ["endpoints_value"] - assert response.api_versions == ["api_versions_value"] - assert response.source_project == "source_project_value" - assert response.source_environment == "source_environment_value" - - -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_create_deployment_rest_interceptors(null_interceptor): - transport = transports.ApiHubRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), - ) - client = ApiHubClient(transport=transport) - - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_create_deployment" - ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_create_deployment_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_create_deployment" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = apihub_service.CreateDeploymentRequest.pb( - apihub_service.CreateDeploymentRequest() - ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.Deployment.to_json(common_fields.Deployment()) - req.return_value.content = return_value - - request = apihub_service.CreateDeploymentRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = common_fields.Deployment() - post_with_metadata.return_value = common_fields.Deployment(), metadata - - client.create_deployment( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], - ) - - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() - - -def test_get_deployment_rest_bad_request( - request_type=apihub_service.GetDeploymentRequest, -): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/deployments/sample3"} - request = request_type(**request_init) - - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_deployment(request) - + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value -@pytest.mark.parametrize( - "request_type", - [ - apihub_service.GetDeploymentRequest, - dict, - ], -) -def test_get_deployment_rest_call_success(request_type): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/deployments/sample3"} + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["deployment"][field])): + del request_init["deployment"][field][i][subfield] + else: + del request_init["deployment"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -12665,7 +29831,7 @@ def test_get_deployment_rest_call_success(request_type): response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_deployment(request) + response = client.update_deployment(request) # Establish that the response is the type that we expect. assert isinstance(response, common_fields.Deployment) @@ -12680,7 +29846,7 @@ def test_get_deployment_rest_call_success(request_type): @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_deployment_rest_interceptors(null_interceptor): +def test_update_deployment_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -12692,17 +29858,17 @@ def test_get_deployment_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_get_deployment" + transports.ApiHubRestInterceptor, "post_update_deployment" ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_get_deployment_with_metadata" + transports.ApiHubRestInterceptor, "post_update_deployment_with_metadata" ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_get_deployment" + transports.ApiHubRestInterceptor, "pre_update_deployment" ) as pre: pre.assert_not_called() post.assert_not_called() post_with_metadata.assert_not_called() - pb_message = apihub_service.GetDeploymentRequest.pb( - apihub_service.GetDeploymentRequest() + pb_message = apihub_service.UpdateDeploymentRequest.pb( + apihub_service.UpdateDeploymentRequest() ) transcode.return_value = { "method": "post", @@ -12717,7 +29883,7 @@ def test_get_deployment_rest_interceptors(null_interceptor): return_value = common_fields.Deployment.to_json(common_fields.Deployment()) req.return_value.content = return_value - request = apihub_service.GetDeploymentRequest() + request = apihub_service.UpdateDeploymentRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), @@ -12726,7 +29892,7 @@ def test_get_deployment_rest_interceptors(null_interceptor): post.return_value = common_fields.Deployment() post_with_metadata.return_value = common_fields.Deployment(), metadata - client.get_deployment( + client.update_deployment( request, metadata=[ ("key", "val"), @@ -12739,14 +29905,14 @@ def test_get_deployment_rest_interceptors(null_interceptor): post_with_metadata.assert_called_once() -def test_list_deployments_rest_bad_request( - request_type=apihub_service.ListDeploymentsRequest, +def test_delete_deployment_rest_bad_request( + request_type=apihub_service.DeleteDeploymentRequest, ): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = {"name": "projects/sample1/locations/sample2/deployments/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -12761,51 +29927,45 @@ def test_list_deployments_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_deployments(request) + client.delete_deployment(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.ListDeploymentsRequest, + apihub_service.DeleteDeploymentRequest, dict, ], ) -def test_list_deployments_rest_call_success(request_type): +def test_delete_deployment_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = {"name": "projects/sample1/locations/sample2/deployments/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = apihub_service.ListDeploymentsResponse( - next_page_token="next_page_token_value", - ) + return_value = None # Wrap the value into a proper Response obj response_value = mock.Mock() response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = apihub_service.ListDeploymentsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + json_return_value = "" response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.list_deployments(request) + response = client.delete_deployment(request) # Establish that the response is the type that we expect. - assert isinstance(response, pagers.ListDeploymentsPager) - assert response.next_page_token == "next_page_token_value" + assert response is None @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_list_deployments_rest_interceptors(null_interceptor): +def test_delete_deployment_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -12817,17 +29977,11 @@ def test_list_deployments_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_list_deployments" - ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_list_deployments_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_list_deployments" + transports.ApiHubRestInterceptor, "pre_delete_deployment" ) as pre: pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = apihub_service.ListDeploymentsRequest.pb( - apihub_service.ListDeploymentsRequest() + pb_message = apihub_service.DeleteDeploymentRequest.pb( + apihub_service.DeleteDeploymentRequest() ) transcode.return_value = { "method": "post", @@ -12839,24 +29993,15 @@ def test_list_deployments_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = apihub_service.ListDeploymentsResponse.to_json( - apihub_service.ListDeploymentsResponse() - ) - req.return_value.content = return_value - request = apihub_service.ListDeploymentsRequest() + request = apihub_service.DeleteDeploymentRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = apihub_service.ListDeploymentsResponse() - post_with_metadata.return_value = ( - apihub_service.ListDeploymentsResponse(), - metadata, - ) - client.list_deployments( + client.delete_deployment( request, metadata=[ ("key", "val"), @@ -12865,20 +30010,16 @@ def test_list_deployments_rest_interceptors(null_interceptor): ) pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() -def test_update_deployment_rest_bad_request( - request_type=apihub_service.UpdateDeploymentRequest, +def test_create_attribute_rest_bad_request( + request_type=apihub_service.CreateAttributeRequest, ): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = { - "deployment": {"name": "projects/sample1/locations/sample2/deployments/sample3"} - } + request_init = {"parent": "projects/sample1/locations/sample2"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -12893,77 +30034,49 @@ def test_update_deployment_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.update_deployment(request) + client.create_attribute(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.UpdateDeploymentRequest, + apihub_service.CreateAttributeRequest, dict, ], ) -def test_update_deployment_rest_call_success(request_type): +def test_create_attribute_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = { - "deployment": {"name": "projects/sample1/locations/sample2/deployments/sample3"} - } - request_init["deployment"] = { - "name": "projects/sample1/locations/sample2/deployments/sample3", + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["attribute"] = { + "name": "name_value", "display_name": "display_name_value", "description": "description_value", - "documentation": {"external_uri": "external_uri_value"}, - "deployment_type": { - "enum_values": { - "values": [ - { - "id": "id_value", - "display_name": "display_name_value", - "description": "description_value", - "immutable": True, - } - ] - }, - "string_values": {"values": ["values_value1", "values_value2"]}, - "json_values": {}, - "uri_values": {}, - "attribute": "attribute_value", - }, - "resource_uri": "resource_uri_value", - "endpoints": ["endpoints_value1", "endpoints_value2"], - "api_versions": ["api_versions_value1", "api_versions_value2"], - "create_time": {"seconds": 751, "nanos": 543}, - "update_time": {}, - "slo": {}, - "environment": {}, - "attributes": {}, - "source_metadata": [ + "definition_type": 1, + "scope": 1, + "data_type": 1, + "allowed_values": [ { - "plugin_instance_action_source": { - "plugin_instance": "plugin_instance_value", - "action_id": "action_id_value", - }, - "source_type": 1, - "original_resource_id": "original_resource_id_value", - "original_resource_create_time": {}, - "original_resource_update_time": {}, + "id": "id_value", + "display_name": "display_name_value", + "description": "description_value", + "immutable": True, } ], - "management_url": {}, - "source_uri": {}, - "source_project": "source_project_value", - "source_environment": "source_environment_value", + "cardinality": 1172, + "mandatory": True, + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, } # The version of a generated dependency at test runtime may differ from the version used during generation. # Delete any fields which are not present in the current runtime dependency # See https://github.com/googleapis/gapic-generator-python/issues/1748 # Determine if the message type is proto-plus or protobuf - test_field = apihub_service.UpdateDeploymentRequest.meta.fields["deployment"] + test_field = apihub_service.CreateAttributeRequest.meta.fields["attribute"] def get_message_fields(field): # Given a field which is a message (composite type), return a list with @@ -12991,7 +30104,7 @@ def get_message_fields(field): # For each item in the sample request, create a list of sub fields which are not present at runtime # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["deployment"].items(): # pragma: NO COVER + for field, value in request_init["attribute"].items(): # pragma: NO COVER result = None is_repeated = False # For repeated fields @@ -13021,24 +30134,24 @@ def get_message_fields(field): subfield = subfield_to_delete.get("subfield") if subfield: if field_repeated: - for i in range(0, len(request_init["deployment"][field])): - del request_init["deployment"][field][i][subfield] + for i in range(0, len(request_init["attribute"][field])): + del request_init["attribute"][field][i][subfield] else: - del request_init["deployment"][field][subfield] + del request_init["attribute"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = common_fields.Deployment( + return_value = common_fields.Attribute( name="name_value", display_name="display_name_value", description="description_value", - resource_uri="resource_uri_value", - endpoints=["endpoints_value"], - api_versions=["api_versions_value"], - source_project="source_project_value", - source_environment="source_environment_value", + definition_type=common_fields.Attribute.DefinitionType.SYSTEM_DEFINED, + scope=common_fields.Attribute.Scope.API, + data_type=common_fields.Attribute.DataType.ENUM, + cardinality=1172, + mandatory=True, ) # Wrap the value into a proper Response obj @@ -13046,27 +30159,30 @@ def get_message_fields(field): response_value.status_code = 200 # Convert return value to protobuf type - return_value = common_fields.Deployment.pb(return_value) + return_value = common_fields.Attribute.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.update_deployment(request) + response = client.create_attribute(request) # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.Deployment) + assert isinstance(response, common_fields.Attribute) assert response.name == "name_value" assert response.display_name == "display_name_value" assert response.description == "description_value" - assert response.resource_uri == "resource_uri_value" - assert response.endpoints == ["endpoints_value"] - assert response.api_versions == ["api_versions_value"] - assert response.source_project == "source_project_value" - assert response.source_environment == "source_environment_value" + assert ( + response.definition_type + == common_fields.Attribute.DefinitionType.SYSTEM_DEFINED + ) + assert response.scope == common_fields.Attribute.Scope.API + assert response.data_type == common_fields.Attribute.DataType.ENUM + assert response.cardinality == 1172 + assert response.mandatory is True @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_update_deployment_rest_interceptors(null_interceptor): +def test_create_attribute_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -13078,17 +30194,17 @@ def test_update_deployment_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_update_deployment" + transports.ApiHubRestInterceptor, "post_create_attribute" ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_update_deployment_with_metadata" + transports.ApiHubRestInterceptor, "post_create_attribute_with_metadata" ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_update_deployment" + transports.ApiHubRestInterceptor, "pre_create_attribute" ) as pre: pre.assert_not_called() post.assert_not_called() post_with_metadata.assert_not_called() - pb_message = apihub_service.UpdateDeploymentRequest.pb( - apihub_service.UpdateDeploymentRequest() + pb_message = apihub_service.CreateAttributeRequest.pb( + apihub_service.CreateAttributeRequest() ) transcode.return_value = { "method": "post", @@ -13100,19 +30216,19 @@ def test_update_deployment_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.Deployment.to_json(common_fields.Deployment()) + return_value = common_fields.Attribute.to_json(common_fields.Attribute()) req.return_value.content = return_value - request = apihub_service.UpdateDeploymentRequest() + request = apihub_service.CreateAttributeRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = common_fields.Deployment() - post_with_metadata.return_value = common_fields.Deployment(), metadata + post.return_value = common_fields.Attribute() + post_with_metadata.return_value = common_fields.Attribute(), metadata - client.update_deployment( + client.create_attribute( request, metadata=[ ("key", "val"), @@ -13125,14 +30241,14 @@ def test_update_deployment_rest_interceptors(null_interceptor): post_with_metadata.assert_called_once() -def test_delete_deployment_rest_bad_request( - request_type=apihub_service.DeleteDeploymentRequest, +def test_get_attribute_rest_bad_request( + request_type=apihub_service.GetAttributeRequest, ): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/deployments/sample3"} + request_init = {"name": "projects/sample1/locations/sample2/attributes/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -13147,45 +30263,68 @@ def test_delete_deployment_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_deployment(request) + client.get_attribute(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.DeleteDeploymentRequest, + apihub_service.GetAttributeRequest, dict, ], ) -def test_delete_deployment_rest_call_success(request_type): +def test_get_attribute_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/deployments/sample3"} + request_init = {"name": "projects/sample1/locations/sample2/attributes/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = None + return_value = common_fields.Attribute( + name="name_value", + display_name="display_name_value", + description="description_value", + definition_type=common_fields.Attribute.DefinitionType.SYSTEM_DEFINED, + scope=common_fields.Attribute.Scope.API, + data_type=common_fields.Attribute.DataType.ENUM, + cardinality=1172, + mandatory=True, + ) # Wrap the value into a proper Response obj response_value = mock.Mock() response_value.status_code = 200 - json_return_value = "" + + # Convert return value to protobuf type + return_value = common_fields.Attribute.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.delete_deployment(request) + response = client.get_attribute(request) # Establish that the response is the type that we expect. - assert response is None + assert isinstance(response, common_fields.Attribute) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert ( + response.definition_type + == common_fields.Attribute.DefinitionType.SYSTEM_DEFINED + ) + assert response.scope == common_fields.Attribute.Scope.API + assert response.data_type == common_fields.Attribute.DataType.ENUM + assert response.cardinality == 1172 + assert response.mandatory is True @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_delete_deployment_rest_interceptors(null_interceptor): +def test_get_attribute_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -13197,11 +30336,17 @@ def test_delete_deployment_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_delete_deployment" + transports.ApiHubRestInterceptor, "post_get_attribute" + ) as post, mock.patch.object( + transports.ApiHubRestInterceptor, "post_get_attribute_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubRestInterceptor, "pre_get_attribute" ) as pre: pre.assert_not_called() - pb_message = apihub_service.DeleteDeploymentRequest.pb( - apihub_service.DeleteDeploymentRequest() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = apihub_service.GetAttributeRequest.pb( + apihub_service.GetAttributeRequest() ) transcode.return_value = { "method": "post", @@ -13213,15 +30358,19 @@ def test_delete_deployment_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = common_fields.Attribute.to_json(common_fields.Attribute()) + req.return_value.content = return_value - request = apihub_service.DeleteDeploymentRequest() + request = apihub_service.GetAttributeRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata + post.return_value = common_fields.Attribute() + post_with_metadata.return_value = common_fields.Attribute(), metadata - client.delete_deployment( + client.get_attribute( request, metadata=[ ("key", "val"), @@ -13230,16 +30379,20 @@ def test_delete_deployment_rest_interceptors(null_interceptor): ) pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() -def test_create_attribute_rest_bad_request( - request_type=apihub_service.CreateAttributeRequest, +def test_update_attribute_rest_bad_request( + request_type=apihub_service.UpdateAttributeRequest, ): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = { + "attribute": {"name": "projects/sample1/locations/sample2/attributes/sample3"} + } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -13254,25 +30407,27 @@ def test_create_attribute_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_attribute(request) + client.update_attribute(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.CreateAttributeRequest, + apihub_service.UpdateAttributeRequest, dict, ], ) -def test_create_attribute_rest_call_success(request_type): +def test_update_attribute_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = { + "attribute": {"name": "projects/sample1/locations/sample2/attributes/sample3"} + } request_init["attribute"] = { - "name": "name_value", + "name": "projects/sample1/locations/sample2/attributes/sample3", "display_name": "display_name_value", "description": "description_value", "definition_type": 1, @@ -13296,7 +30451,7 @@ def test_create_attribute_rest_call_success(request_type): # See https://github.com/googleapis/gapic-generator-python/issues/1748 # Determine if the message type is proto-plus or protobuf - test_field = apihub_service.CreateAttributeRequest.meta.fields["attribute"] + test_field = apihub_service.UpdateAttributeRequest.meta.fields["attribute"] def get_message_fields(field): # Given a field which is a message (composite type), return a list with @@ -13384,7 +30539,7 @@ def get_message_fields(field): response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.create_attribute(request) + response = client.update_attribute(request) # Establish that the response is the type that we expect. assert isinstance(response, common_fields.Attribute) @@ -13402,7 +30557,7 @@ def get_message_fields(field): @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_create_attribute_rest_interceptors(null_interceptor): +def test_update_attribute_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -13414,17 +30569,17 @@ def test_create_attribute_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_create_attribute" + transports.ApiHubRestInterceptor, "post_update_attribute" ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_create_attribute_with_metadata" + transports.ApiHubRestInterceptor, "post_update_attribute_with_metadata" ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_create_attribute" + transports.ApiHubRestInterceptor, "pre_update_attribute" ) as pre: pre.assert_not_called() post.assert_not_called() post_with_metadata.assert_not_called() - pb_message = apihub_service.CreateAttributeRequest.pb( - apihub_service.CreateAttributeRequest() + pb_message = apihub_service.UpdateAttributeRequest.pb( + apihub_service.UpdateAttributeRequest() ) transcode.return_value = { "method": "post", @@ -13439,7 +30594,7 @@ def test_create_attribute_rest_interceptors(null_interceptor): return_value = common_fields.Attribute.to_json(common_fields.Attribute()) req.return_value.content = return_value - request = apihub_service.CreateAttributeRequest() + request = apihub_service.UpdateAttributeRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), @@ -13448,7 +30603,7 @@ def test_create_attribute_rest_interceptors(null_interceptor): post.return_value = common_fields.Attribute() post_with_metadata.return_value = common_fields.Attribute(), metadata - client.create_attribute( + client.update_attribute( request, metadata=[ ("key", "val"), @@ -13461,8 +30616,8 @@ def test_create_attribute_rest_interceptors(null_interceptor): post_with_metadata.assert_called_once() -def test_get_attribute_rest_bad_request( - request_type=apihub_service.GetAttributeRequest, +def test_delete_attribute_rest_bad_request( + request_type=apihub_service.DeleteAttributeRequest, ): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" @@ -13483,17 +30638,17 @@ def test_get_attribute_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_attribute(request) + client.delete_attribute(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.GetAttributeRequest, + apihub_service.DeleteAttributeRequest, dict, ], ) -def test_get_attribute_rest_call_success(request_type): +def test_delete_attribute_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -13505,46 +30660,23 @@ def test_get_attribute_rest_call_success(request_type): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = common_fields.Attribute( - name="name_value", - display_name="display_name_value", - description="description_value", - definition_type=common_fields.Attribute.DefinitionType.SYSTEM_DEFINED, - scope=common_fields.Attribute.Scope.API, - data_type=common_fields.Attribute.DataType.ENUM, - cardinality=1172, - mandatory=True, - ) + return_value = None # Wrap the value into a proper Response obj response_value = mock.Mock() response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = common_fields.Attribute.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + json_return_value = "" response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_attribute(request) + response = client.delete_attribute(request) # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.Attribute) - assert response.name == "name_value" - assert response.display_name == "display_name_value" - assert response.description == "description_value" - assert ( - response.definition_type - == common_fields.Attribute.DefinitionType.SYSTEM_DEFINED - ) - assert response.scope == common_fields.Attribute.Scope.API - assert response.data_type == common_fields.Attribute.DataType.ENUM - assert response.cardinality == 1172 - assert response.mandatory is True + assert response is None @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_attribute_rest_interceptors(null_interceptor): +def test_delete_attribute_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -13556,17 +30688,11 @@ def test_get_attribute_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_get_attribute" - ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_get_attribute_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_get_attribute" + transports.ApiHubRestInterceptor, "pre_delete_attribute" ) as pre: pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = apihub_service.GetAttributeRequest.pb( - apihub_service.GetAttributeRequest() + pb_message = apihub_service.DeleteAttributeRequest.pb( + apihub_service.DeleteAttributeRequest() ) transcode.return_value = { "method": "post", @@ -13578,19 +30704,15 @@ def test_get_attribute_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.Attribute.to_json(common_fields.Attribute()) - req.return_value.content = return_value - request = apihub_service.GetAttributeRequest() + request = apihub_service.DeleteAttributeRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = common_fields.Attribute() - post_with_metadata.return_value = common_fields.Attribute(), metadata - client.get_attribute( + client.delete_attribute( request, metadata=[ ("key", "val"), @@ -13599,154 +30721,54 @@ def test_get_attribute_rest_interceptors(null_interceptor): ) pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() -def test_update_attribute_rest_bad_request( - request_type=apihub_service.UpdateAttributeRequest, +def test_list_attributes_rest_bad_request( + request_type=apihub_service.ListAttributesRequest, ): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = { - "attribute": {"name": "projects/sample1/locations/sample2/attributes/sample3"} - } + request_init = {"parent": "projects/sample1/locations/sample2"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. with mock.patch.object(Session, "request") as req, pytest.raises( core_exceptions.BadRequest ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.update_attribute(request) - - -@pytest.mark.parametrize( - "request_type", - [ - apihub_service.UpdateAttributeRequest, - dict, - ], -) -def test_update_attribute_rest_call_success(request_type): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - - # send a request that will satisfy transcoding - request_init = { - "attribute": {"name": "projects/sample1/locations/sample2/attributes/sample3"} - } - request_init["attribute"] = { - "name": "projects/sample1/locations/sample2/attributes/sample3", - "display_name": "display_name_value", - "description": "description_value", - "definition_type": 1, - "scope": 1, - "data_type": 1, - "allowed_values": [ - { - "id": "id_value", - "display_name": "display_name_value", - "description": "description_value", - "immutable": True, - } - ], - "cardinality": 1172, - "mandatory": True, - "create_time": {"seconds": 751, "nanos": 543}, - "update_time": {}, - } - # The version of a generated dependency at test runtime may differ from the version used during generation. - # Delete any fields which are not present in the current runtime dependency - # See https://github.com/googleapis/gapic-generator-python/issues/1748 - - # Determine if the message type is proto-plus or protobuf - test_field = apihub_service.UpdateAttributeRequest.meta.fields["attribute"] - - def get_message_fields(field): - # Given a field which is a message (composite type), return a list with - # all the fields of the message. - # If the field is not a composite type, return an empty list. - message_fields = [] - - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") - - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields - - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] - - subfields_not_in_runtime = [] + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_attributes(request) - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["attribute"].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.ListAttributesRequest, + dict, + ], +) +def test_list_attributes_rest_call_success(request_type): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range(0, len(request_init["attribute"][field])): - del request_init["attribute"][field][i][subfield] - else: - del request_init["attribute"][field][subfield] + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = common_fields.Attribute( - name="name_value", - display_name="display_name_value", - description="description_value", - definition_type=common_fields.Attribute.DefinitionType.SYSTEM_DEFINED, - scope=common_fields.Attribute.Scope.API, - data_type=common_fields.Attribute.DataType.ENUM, - cardinality=1172, - mandatory=True, + return_value = apihub_service.ListAttributesResponse( + next_page_token="next_page_token_value", ) # Wrap the value into a proper Response obj @@ -13754,30 +30776,20 @@ def get_message_fields(field): response_value.status_code = 200 # Convert return value to protobuf type - return_value = common_fields.Attribute.pb(return_value) + return_value = apihub_service.ListAttributesResponse.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.update_attribute(request) + response = client.list_attributes(request) # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.Attribute) - assert response.name == "name_value" - assert response.display_name == "display_name_value" - assert response.description == "description_value" - assert ( - response.definition_type - == common_fields.Attribute.DefinitionType.SYSTEM_DEFINED - ) - assert response.scope == common_fields.Attribute.Scope.API - assert response.data_type == common_fields.Attribute.DataType.ENUM - assert response.cardinality == 1172 - assert response.mandatory is True + assert isinstance(response, pagers.ListAttributesPager) + assert response.next_page_token == "next_page_token_value" @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_update_attribute_rest_interceptors(null_interceptor): +def test_list_attributes_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -13789,17 +30801,17 @@ def test_update_attribute_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_update_attribute" + transports.ApiHubRestInterceptor, "post_list_attributes" ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_update_attribute_with_metadata" + transports.ApiHubRestInterceptor, "post_list_attributes_with_metadata" ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_update_attribute" + transports.ApiHubRestInterceptor, "pre_list_attributes" ) as pre: pre.assert_not_called() post.assert_not_called() post_with_metadata.assert_not_called() - pb_message = apihub_service.UpdateAttributeRequest.pb( - apihub_service.UpdateAttributeRequest() + pb_message = apihub_service.ListAttributesRequest.pb( + apihub_service.ListAttributesRequest() ) transcode.return_value = { "method": "post", @@ -13811,19 +30823,24 @@ def test_update_attribute_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.Attribute.to_json(common_fields.Attribute()) + return_value = apihub_service.ListAttributesResponse.to_json( + apihub_service.ListAttributesResponse() + ) req.return_value.content = return_value - request = apihub_service.UpdateAttributeRequest() + request = apihub_service.ListAttributesRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = common_fields.Attribute() - post_with_metadata.return_value = common_fields.Attribute(), metadata + post.return_value = apihub_service.ListAttributesResponse() + post_with_metadata.return_value = ( + apihub_service.ListAttributesResponse(), + metadata, + ) - client.update_attribute( + client.list_attributes( request, metadata=[ ("key", "val"), @@ -13836,14 +30853,14 @@ def test_update_attribute_rest_interceptors(null_interceptor): post_with_metadata.assert_called_once() -def test_delete_attribute_rest_bad_request( - request_type=apihub_service.DeleteAttributeRequest, +def test_search_resources_rest_bad_request( + request_type=apihub_service.SearchResourcesRequest, ): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/attributes/sample3"} + request_init = {"location": "projects/sample1/locations/sample2"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -13858,45 +30875,51 @@ def test_delete_attribute_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_attribute(request) + client.search_resources(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.DeleteAttributeRequest, + apihub_service.SearchResourcesRequest, dict, ], ) -def test_delete_attribute_rest_call_success(request_type): +def test_search_resources_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/attributes/sample3"} + request_init = {"location": "projects/sample1/locations/sample2"} request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = None + return_value = apihub_service.SearchResourcesResponse( + next_page_token="next_page_token_value", + ) # Wrap the value into a proper Response obj response_value = mock.Mock() response_value.status_code = 200 - json_return_value = "" + + # Convert return value to protobuf type + return_value = apihub_service.SearchResourcesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.delete_attribute(request) + response = client.search_resources(request) # Establish that the response is the type that we expect. - assert response is None + assert isinstance(response, pagers.SearchResourcesPager) + assert response.next_page_token == "next_page_token_value" @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_delete_attribute_rest_interceptors(null_interceptor): +def test_search_resources_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -13908,11 +30931,17 @@ def test_delete_attribute_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_delete_attribute" + transports.ApiHubRestInterceptor, "post_search_resources" + ) as post, mock.patch.object( + transports.ApiHubRestInterceptor, "post_search_resources_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubRestInterceptor, "pre_search_resources" ) as pre: pre.assert_not_called() - pb_message = apihub_service.DeleteAttributeRequest.pb( - apihub_service.DeleteAttributeRequest() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = apihub_service.SearchResourcesRequest.pb( + apihub_service.SearchResourcesRequest() ) transcode.return_value = { "method": "post", @@ -13924,15 +30953,24 @@ def test_delete_attribute_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = apihub_service.SearchResourcesResponse.to_json( + apihub_service.SearchResourcesResponse() + ) + req.return_value.content = return_value - request = apihub_service.DeleteAttributeRequest() + request = apihub_service.SearchResourcesRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata + post.return_value = apihub_service.SearchResourcesResponse() + post_with_metadata.return_value = ( + apihub_service.SearchResourcesResponse(), + metadata, + ) - client.delete_attribute( + client.search_resources( request, metadata=[ ("key", "val"), @@ -13941,10 +30979,12 @@ def test_delete_attribute_rest_interceptors(null_interceptor): ) pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() -def test_list_attributes_rest_bad_request( - request_type=apihub_service.ListAttributesRequest, +def test_create_external_api_rest_bad_request( + request_type=apihub_service.CreateExternalApiRequest, ): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" @@ -13965,30 +31005,112 @@ def test_list_attributes_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_attributes(request) + client.create_external_api(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.ListAttributesRequest, + apihub_service.CreateExternalApiRequest, dict, ], ) -def test_list_attributes_rest_call_success(request_type): +def test_create_external_api_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["external_api"] = { + "name": "name_value", + "display_name": "display_name_value", + "description": "description_value", + "endpoints": ["endpoints_value1", "endpoints_value2"], + "paths": ["paths_value1", "paths_value2"], + "documentation": {"external_uri": "external_uri_value"}, + "attributes": {}, + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = apihub_service.CreateExternalApiRequest.meta.fields["external_api"] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["external_api"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["external_api"][field])): + del request_init["external_api"][field][i][subfield] + else: + del request_init["external_api"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = apihub_service.ListAttributesResponse( - next_page_token="next_page_token_value", + return_value = common_fields.ExternalApi( + name="name_value", + display_name="display_name_value", + description="description_value", + endpoints=["endpoints_value"], + paths=["paths_value"], ) # Wrap the value into a proper Response obj @@ -13996,20 +31118,24 @@ def test_list_attributes_rest_call_success(request_type): response_value.status_code = 200 # Convert return value to protobuf type - return_value = apihub_service.ListAttributesResponse.pb(return_value) + return_value = common_fields.ExternalApi.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.list_attributes(request) + response = client.create_external_api(request) # Establish that the response is the type that we expect. - assert isinstance(response, pagers.ListAttributesPager) - assert response.next_page_token == "next_page_token_value" + assert isinstance(response, common_fields.ExternalApi) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.endpoints == ["endpoints_value"] + assert response.paths == ["paths_value"] @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_list_attributes_rest_interceptors(null_interceptor): +def test_create_external_api_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -14021,17 +31147,17 @@ def test_list_attributes_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_list_attributes" + transports.ApiHubRestInterceptor, "post_create_external_api" ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_list_attributes_with_metadata" + transports.ApiHubRestInterceptor, "post_create_external_api_with_metadata" ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_list_attributes" + transports.ApiHubRestInterceptor, "pre_create_external_api" ) as pre: pre.assert_not_called() post.assert_not_called() post_with_metadata.assert_not_called() - pb_message = apihub_service.ListAttributesRequest.pb( - apihub_service.ListAttributesRequest() + pb_message = apihub_service.CreateExternalApiRequest.pb( + apihub_service.CreateExternalApiRequest() ) transcode.return_value = { "method": "post", @@ -14043,24 +31169,19 @@ def test_list_attributes_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = apihub_service.ListAttributesResponse.to_json( - apihub_service.ListAttributesResponse() - ) + return_value = common_fields.ExternalApi.to_json(common_fields.ExternalApi()) req.return_value.content = return_value - request = apihub_service.ListAttributesRequest() + request = apihub_service.CreateExternalApiRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = apihub_service.ListAttributesResponse() - post_with_metadata.return_value = ( - apihub_service.ListAttributesResponse(), - metadata, - ) + post.return_value = common_fields.ExternalApi() + post_with_metadata.return_value = common_fields.ExternalApi(), metadata - client.list_attributes( + client.create_external_api( request, metadata=[ ("key", "val"), @@ -14073,14 +31194,14 @@ def test_list_attributes_rest_interceptors(null_interceptor): post_with_metadata.assert_called_once() -def test_search_resources_rest_bad_request( - request_type=apihub_service.SearchResourcesRequest, +def test_get_external_api_rest_bad_request( + request_type=apihub_service.GetExternalApiRequest, ): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"location": "projects/sample1/locations/sample2"} + request_init = {"name": "projects/sample1/locations/sample2/externalApis/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -14095,30 +31216,34 @@ def test_search_resources_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.search_resources(request) + client.get_external_api(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.SearchResourcesRequest, + apihub_service.GetExternalApiRequest, dict, ], ) -def test_search_resources_rest_call_success(request_type): +def test_get_external_api_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"location": "projects/sample1/locations/sample2"} + request_init = {"name": "projects/sample1/locations/sample2/externalApis/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = apihub_service.SearchResourcesResponse( - next_page_token="next_page_token_value", + return_value = common_fields.ExternalApi( + name="name_value", + display_name="display_name_value", + description="description_value", + endpoints=["endpoints_value"], + paths=["paths_value"], ) # Wrap the value into a proper Response obj @@ -14126,20 +31251,24 @@ def test_search_resources_rest_call_success(request_type): response_value.status_code = 200 # Convert return value to protobuf type - return_value = apihub_service.SearchResourcesResponse.pb(return_value) + return_value = common_fields.ExternalApi.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.search_resources(request) + response = client.get_external_api(request) # Establish that the response is the type that we expect. - assert isinstance(response, pagers.SearchResourcesPager) - assert response.next_page_token == "next_page_token_value" + assert isinstance(response, common_fields.ExternalApi) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.endpoints == ["endpoints_value"] + assert response.paths == ["paths_value"] @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_search_resources_rest_interceptors(null_interceptor): +def test_get_external_api_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -14151,17 +31280,17 @@ def test_search_resources_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_search_resources" + transports.ApiHubRestInterceptor, "post_get_external_api" ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_search_resources_with_metadata" + transports.ApiHubRestInterceptor, "post_get_external_api_with_metadata" ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_search_resources" + transports.ApiHubRestInterceptor, "pre_get_external_api" ) as pre: pre.assert_not_called() post.assert_not_called() post_with_metadata.assert_not_called() - pb_message = apihub_service.SearchResourcesRequest.pb( - apihub_service.SearchResourcesRequest() + pb_message = apihub_service.GetExternalApiRequest.pb( + apihub_service.GetExternalApiRequest() ) transcode.return_value = { "method": "post", @@ -14173,24 +31302,19 @@ def test_search_resources_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = apihub_service.SearchResourcesResponse.to_json( - apihub_service.SearchResourcesResponse() - ) + return_value = common_fields.ExternalApi.to_json(common_fields.ExternalApi()) req.return_value.content = return_value - request = apihub_service.SearchResourcesRequest() + request = apihub_service.GetExternalApiRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = apihub_service.SearchResourcesResponse() - post_with_metadata.return_value = ( - apihub_service.SearchResourcesResponse(), - metadata, - ) + post.return_value = common_fields.ExternalApi() + post_with_metadata.return_value = common_fields.ExternalApi(), metadata - client.search_resources( + client.get_external_api( request, metadata=[ ("key", "val"), @@ -14203,14 +31327,18 @@ def test_search_resources_rest_interceptors(null_interceptor): post_with_metadata.assert_called_once() -def test_create_external_api_rest_bad_request( - request_type=apihub_service.CreateExternalApiRequest, +def test_update_external_api_rest_bad_request( + request_type=apihub_service.UpdateExternalApiRequest, ): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = { + "external_api": { + "name": "projects/sample1/locations/sample2/externalApis/sample3" + } + } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -14225,25 +31353,29 @@ def test_create_external_api_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_external_api(request) + client.update_external_api(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.CreateExternalApiRequest, + apihub_service.UpdateExternalApiRequest, dict, ], ) -def test_create_external_api_rest_call_success(request_type): +def test_update_external_api_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = { + "external_api": { + "name": "projects/sample1/locations/sample2/externalApis/sample3" + } + } request_init["external_api"] = { - "name": "name_value", + "name": "projects/sample1/locations/sample2/externalApis/sample3", "display_name": "display_name_value", "description": "description_value", "endpoints": ["endpoints_value1", "endpoints_value2"], @@ -14258,7 +31390,7 @@ def test_create_external_api_rest_call_success(request_type): # See https://github.com/googleapis/gapic-generator-python/issues/1748 # Determine if the message type is proto-plus or protobuf - test_field = apihub_service.CreateExternalApiRequest.meta.fields["external_api"] + test_field = apihub_service.UpdateExternalApiRequest.meta.fields["external_api"] def get_message_fields(field): # Given a field which is a message (composite type), return a list with @@ -14325,37 +31457,156 @@ def get_message_fields(field): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = common_fields.ExternalApi( - name="name_value", - display_name="display_name_value", - description="description_value", - endpoints=["endpoints_value"], - paths=["paths_value"], - ) + return_value = common_fields.ExternalApi( + name="name_value", + display_name="display_name_value", + description="description_value", + endpoints=["endpoints_value"], + paths=["paths_value"], + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.ExternalApi.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.update_external_api(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.ExternalApi) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.endpoints == ["endpoints_value"] + assert response.paths == ["paths_value"] + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_update_external_api_rest_interceptors(null_interceptor): + transport = transports.ApiHubRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), + ) + client = ApiHubClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubRestInterceptor, "post_update_external_api" + ) as post, mock.patch.object( + transports.ApiHubRestInterceptor, "post_update_external_api_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubRestInterceptor, "pre_update_external_api" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = apihub_service.UpdateExternalApiRequest.pb( + apihub_service.UpdateExternalApiRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = common_fields.ExternalApi.to_json(common_fields.ExternalApi()) + req.return_value.content = return_value + + request = apihub_service.UpdateExternalApiRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = common_fields.ExternalApi() + post_with_metadata.return_value = common_fields.ExternalApi(), metadata + + client.update_external_api( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_delete_external_api_rest_bad_request( + request_type=apihub_service.DeleteExternalApiRequest, +): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/externalApis/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.delete_external_api(request) + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.DeleteExternalApiRequest, + dict, + ], +) +def test_delete_external_api_rest_call_success(request_type): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/externalApis/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = None # Wrap the value into a proper Response obj response_value = mock.Mock() response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = common_fields.ExternalApi.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + json_return_value = "" response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.create_external_api(request) + response = client.delete_external_api(request) # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.ExternalApi) - assert response.name == "name_value" - assert response.display_name == "display_name_value" - assert response.description == "description_value" - assert response.endpoints == ["endpoints_value"] - assert response.paths == ["paths_value"] + assert response is None @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_create_external_api_rest_interceptors(null_interceptor): +def test_delete_external_api_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -14367,17 +31618,11 @@ def test_create_external_api_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_create_external_api" - ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_create_external_api_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_create_external_api" + transports.ApiHubRestInterceptor, "pre_delete_external_api" ) as pre: pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = apihub_service.CreateExternalApiRequest.pb( - apihub_service.CreateExternalApiRequest() + pb_message = apihub_service.DeleteExternalApiRequest.pb( + apihub_service.DeleteExternalApiRequest() ) transcode.return_value = { "method": "post", @@ -14389,19 +31634,15 @@ def test_create_external_api_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.ExternalApi.to_json(common_fields.ExternalApi()) - req.return_value.content = return_value - request = apihub_service.CreateExternalApiRequest() + request = apihub_service.DeleteExternalApiRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = common_fields.ExternalApi() - post_with_metadata.return_value = common_fields.ExternalApi(), metadata - client.create_external_api( + client.delete_external_api( request, metadata=[ ("key", "val"), @@ -14410,18 +31651,16 @@ def test_create_external_api_rest_interceptors(null_interceptor): ) pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() -def test_get_external_api_rest_bad_request( - request_type=apihub_service.GetExternalApiRequest, +def test_list_external_apis_rest_bad_request( + request_type=apihub_service.ListExternalApisRequest, ): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/externalApis/sample3"} + request_init = {"parent": "projects/sample1/locations/sample2"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -14436,34 +31675,30 @@ def test_get_external_api_rest_bad_request( response_value.request = mock.Mock() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_external_api(request) + client.list_external_apis(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.GetExternalApiRequest, + apihub_service.ListExternalApisRequest, dict, ], ) -def test_get_external_api_rest_call_success(request_type): +def test_list_external_apis_rest_call_success(request_type): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/externalApis/sample3"} + request_init = {"parent": "projects/sample1/locations/sample2"} request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = common_fields.ExternalApi( - name="name_value", - display_name="display_name_value", - description="description_value", - endpoints=["endpoints_value"], - paths=["paths_value"], + return_value = apihub_service.ListExternalApisResponse( + next_page_token="next_page_token_value", ) # Wrap the value into a proper Response obj @@ -14471,24 +31706,20 @@ def test_get_external_api_rest_call_success(request_type): response_value.status_code = 200 # Convert return value to protobuf type - return_value = common_fields.ExternalApi.pb(return_value) + return_value = apihub_service.ListExternalApisResponse.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_external_api(request) + response = client.list_external_apis(request) # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.ExternalApi) - assert response.name == "name_value" - assert response.display_name == "display_name_value" - assert response.description == "description_value" - assert response.endpoints == ["endpoints_value"] - assert response.paths == ["paths_value"] + assert isinstance(response, pagers.ListExternalApisPager) + assert response.next_page_token == "next_page_token_value" @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_external_api_rest_interceptors(null_interceptor): +def test_list_external_apis_rest_interceptors(null_interceptor): transport = transports.ApiHubRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), @@ -14500,17 +31731,17 @@ def test_get_external_api_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_get_external_api" + transports.ApiHubRestInterceptor, "post_list_external_apis" ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_get_external_api_with_metadata" + transports.ApiHubRestInterceptor, "post_list_external_apis_with_metadata" ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_get_external_api" + transports.ApiHubRestInterceptor, "pre_list_external_apis" ) as pre: pre.assert_not_called() post.assert_not_called() post_with_metadata.assert_not_called() - pb_message = apihub_service.GetExternalApiRequest.pb( - apihub_service.GetExternalApiRequest() + pb_message = apihub_service.ListExternalApisRequest.pb( + apihub_service.ListExternalApisRequest() ) transcode.return_value = { "method": "post", @@ -14522,19 +31753,24 @@ def test_get_external_api_rest_interceptors(null_interceptor): req.return_value = mock.Mock() req.return_value.status_code = 200 req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.ExternalApi.to_json(common_fields.ExternalApi()) + return_value = apihub_service.ListExternalApisResponse.to_json( + apihub_service.ListExternalApisResponse() + ) req.return_value.content = return_value - request = apihub_service.GetExternalApiRequest() + request = apihub_service.ListExternalApisRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = common_fields.ExternalApi() - post_with_metadata.return_value = common_fields.ExternalApi(), metadata + post.return_value = apihub_service.ListExternalApisResponse() + post_with_metadata.return_value = ( + apihub_service.ListExternalApisResponse(), + metadata, + ) - client.get_external_api( + client.list_external_apis( request, metadata=[ ("key", "val"), @@ -14547,1080 +31783,1148 @@ def test_get_external_api_rest_interceptors(null_interceptor): post_with_metadata.assert_called_once() -def test_update_external_api_rest_bad_request( - request_type=apihub_service.UpdateExternalApiRequest, -): +def test_get_location_rest_bad_request(request_type=locations_pb2.GetLocationRequest): client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request ) - # send a request that will satisfy transcoding - request_init = { - "external_api": { - "name": "projects/sample1/locations/sample2/externalApis/sample3" - } - } - request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. with mock.patch.object(Session, "request") as req, pytest.raises( core_exceptions.BadRequest ): # Wrap the value into a proper Response obj - response_value = mock.Mock() + response_value = Response() json_return_value = "" response_value.json = mock.Mock(return_value={}) response_value.status_code = 400 - response_value.request = mock.Mock() + response_value.request = Request() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.update_external_api(request) + client.get_location(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.UpdateExternalApiRequest, + locations_pb2.GetLocationRequest, dict, ], ) -def test_update_external_api_rest_call_success(request_type): +def test_get_location_rest(request_type): client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - # send a request that will satisfy transcoding - request_init = { - "external_api": { - "name": "projects/sample1/locations/sample2/externalApis/sample3" - } - } - request_init["external_api"] = { - "name": "projects/sample1/locations/sample2/externalApis/sample3", - "display_name": "display_name_value", - "description": "description_value", - "endpoints": ["endpoints_value1", "endpoints_value2"], - "paths": ["paths_value1", "paths_value2"], - "documentation": {"external_uri": "external_uri_value"}, - "attributes": {}, - "create_time": {"seconds": 751, "nanos": 543}, - "update_time": {}, - } - # The version of a generated dependency at test runtime may differ from the version used during generation. - # Delete any fields which are not present in the current runtime dependency - # See https://github.com/googleapis/gapic-generator-python/issues/1748 + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = locations_pb2.Location() - # Determine if the message type is proto-plus or protobuf - test_field = apihub_service.UpdateExternalApiRequest.meta.fields["external_api"] + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") - def get_message_fields(field): - # Given a field which is a message (composite type), return a list with - # all the fields of the message. - # If the field is not a composite type, return an empty list. - message_fields = [] + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + response = client.get_location(request) - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] - subfields_not_in_runtime = [] +def test_list_locations_rest_bad_request( + request_type=locations_pb2.ListLocationsRequest, +): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict({"name": "projects/sample1"}, request) - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["external_api"].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_locations(request) - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range(0, len(request_init["external_api"][field])): - del request_init["external_api"][field][i][subfield] - else: - del request_init["external_api"][field][subfield] +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.ListLocationsRequest, + dict, + ], +) +def test_list_locations_rest(request_type): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1"} request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: + with mock.patch.object(Session, "request") as req: # Designate an appropriate value for the returned response. - return_value = common_fields.ExternalApi( - name="name_value", - display_name="display_name_value", - description="description_value", - endpoints=["endpoints_value"], - paths=["paths_value"], - ) + return_value = locations_pb2.ListLocationsResponse() # Wrap the value into a proper Response obj response_value = mock.Mock() response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = common_fields.ExternalApi.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.update_external_api(request) + + response = client.list_locations(request) # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.ExternalApi) - assert response.name == "name_value" - assert response.display_name == "display_name_value" - assert response.description == "description_value" - assert response.endpoints == ["endpoints_value"] - assert response.paths == ["paths_value"] + assert isinstance(response, locations_pb2.ListLocationsResponse) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_update_external_api_rest_interceptors(null_interceptor): - transport = transports.ApiHubRestTransport( +def test_cancel_operation_rest_bad_request( + request_type=operations_pb2.CancelOperationRequest, +): + client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request ) - client = ApiHubClient(transport=transport) - - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_update_external_api" - ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_update_external_api_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_update_external_api" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = apihub_service.UpdateExternalApiRequest.pb( - apihub_service.UpdateExternalApiRequest() - ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.ExternalApi.to_json(common_fields.ExternalApi()) - req.return_value.content = return_value + client.cancel_operation(request) - request = apihub_service.UpdateExternalApiRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = common_fields.ExternalApi() - post_with_metadata.return_value = common_fields.ExternalApi(), metadata - client.update_external_api( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], - ) +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.CancelOperationRequest, + dict, + ], +) +def test_cancel_operation_rest(request_type): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "{}" + response_value.content = json_return_value.encode("UTF-8") -def test_delete_external_api_rest_bad_request( - request_type=apihub_service.DeleteExternalApiRequest, + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.cancel_operation(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_delete_operation_rest_bad_request( + request_type=operations_pb2.DeleteOperationRequest, ): client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/externalApis/sample3"} - request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. with mock.patch.object(Session, "request") as req, pytest.raises( core_exceptions.BadRequest ): # Wrap the value into a proper Response obj - response_value = mock.Mock() + response_value = Response() json_return_value = "" response_value.json = mock.Mock(return_value={}) response_value.status_code = 400 - response_value.request = mock.Mock() + response_value.request = Request() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_external_api(request) + client.delete_operation(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.DeleteExternalApiRequest, + operations_pb2.DeleteOperationRequest, dict, ], ) -def test_delete_external_api_rest_call_success(request_type): +def test_delete_operation_rest(request_type): client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/externalApis/sample3"} + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: + with mock.patch.object(Session, "request") as req: # Designate an appropriate value for the returned response. return_value = None # Wrap the value into a proper Response obj response_value = mock.Mock() response_value.status_code = 200 - json_return_value = "" + json_return_value = "{}" response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.delete_external_api(request) + + response = client.delete_operation(request) # Establish that the response is the type that we expect. assert response is None -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_delete_external_api_rest_interceptors(null_interceptor): - transport = transports.ApiHubRestTransport( +def test_get_operation_rest_bad_request( + request_type=operations_pb2.GetOperationRequest, +): + client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request ) - client = ApiHubClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_delete_external_api" - ) as pre: - pre.assert_not_called() - pb_message = apihub_service.DeleteExternalApiRequest.pb( - apihub_service.DeleteExternalApiRequest() - ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.GetOperationRequest, + dict, + ], +) +def test_get_operation_rest(request_type): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") - req.return_value = mock.Mock() - req.return_value.status_code = 200 + req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - request = apihub_service.DeleteExternalApiRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - - client.delete_external_api( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], - ) + response = client.get_operation(request) - pre.assert_called_once() + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) -def test_list_external_apis_rest_bad_request( - request_type=apihub_service.ListExternalApisRequest, +def test_list_operations_rest_bad_request( + request_type=operations_pb2.ListOperationsRequest, ): client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. with mock.patch.object(Session, "request") as req, pytest.raises( core_exceptions.BadRequest ): # Wrap the value into a proper Response obj - response_value = mock.Mock() + response_value = Response() json_return_value = "" response_value.json = mock.Mock(return_value={}) response_value.status_code = 400 - response_value.request = mock.Mock() + response_value.request = Request() req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_external_apis(request) + client.list_operations(request) @pytest.mark.parametrize( "request_type", [ - apihub_service.ListExternalApisRequest, + operations_pb2.ListOperationsRequest, dict, ], ) -def test_list_external_apis_rest_call_success(request_type): +def test_list_operations_rest(request_type): client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = {"name": "projects/sample1/locations/sample2"} request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: + with mock.patch.object(Session, "request") as req: # Designate an appropriate value for the returned response. - return_value = apihub_service.ListExternalApisResponse( - next_page_token="next_page_token_value", - ) + return_value = operations_pb2.ListOperationsResponse() # Wrap the value into a proper Response obj response_value = mock.Mock() response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = apihub_service.ListExternalApisResponse.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.list_external_apis(request) + + response = client.list_operations(request) # Establish that the response is the type that we expect. - assert isinstance(response, pagers.ListExternalApisPager) - assert response.next_page_token == "next_page_token_value" + assert isinstance(response, operations_pb2.ListOperationsResponse) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_list_external_apis_rest_interceptors(null_interceptor): - transport = transports.ApiHubRestTransport( +def test_initialize_client_w_rest(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_api_empty_call_rest(): + client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None if null_interceptor else transports.ApiHubRestInterceptor(), + transport="rest", ) - client = ApiHubClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubRestInterceptor, "post_list_external_apis" - ) as post, mock.patch.object( - transports.ApiHubRestInterceptor, "post_list_external_apis_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubRestInterceptor, "pre_list_external_apis" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = apihub_service.ListExternalApisRequest.pb( - apihub_service.ListExternalApisRequest() - ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.create_api), "__call__") as call: + client.create_api(request=None) - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = apihub_service.ListExternalApisResponse.to_json( - apihub_service.ListExternalApisResponse() - ) - req.return_value.content = return_value + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateApiRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_api_empty_call_rest(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_api), "__call__") as call: + client.get_api(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetApiRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_apis_empty_call_rest(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.list_apis), "__call__") as call: + client.list_apis(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListApisRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_update_api_empty_call_rest(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.update_api), "__call__") as call: + client.update_api(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateApiRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_api_empty_call_rest(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.delete_api), "__call__") as call: + client.delete_api(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteApiRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_version_empty_call_rest(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.create_version), "__call__") as call: + client.create_version(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateVersionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_version_empty_call_rest(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_version), "__call__") as call: + client.get_version(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetVersionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_versions_empty_call_rest(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) - request = apihub_service.ListExternalApisRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = apihub_service.ListExternalApisResponse() - post_with_metadata.return_value = ( - apihub_service.ListExternalApisResponse(), - metadata, - ) + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.list_versions), "__call__") as call: + client.list_versions(request=None) - client.list_external_apis( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], - ) + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListVersionsRequest() - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + assert args[0] == request_msg -def test_get_location_rest_bad_request(request_type=locations_pb2.GetLocationRequest): +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_update_version_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2"}, request - ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_location(request) + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.update_version), "__call__") as call: + client.update_version(request=None) + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateVersionRequest() -@pytest.mark.parametrize( - "request_type", - [ - locations_pb2.GetLocationRequest, - dict, - ], -) -def test_get_location_rest(request_type): + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_version_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) - request_init = {"name": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = locations_pb2.Location() + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.delete_version), "__call__") as call: + client.delete_version(request=None) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteVersionRequest() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + assert args[0] == request_msg - response = client.get_location(request) - # Establish that the response is the type that we expect. - assert isinstance(response, locations_pb2.Location) +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_spec_empty_call_rest(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.create_spec), "__call__") as call: + client.create_spec(request=None) -def test_list_locations_rest_bad_request( - request_type=locations_pb2.ListLocationsRequest, -): + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateSpecRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_spec_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) - request = request_type() - request = json_format.ParseDict({"name": "projects/sample1"}, request) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_locations(request) + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_spec), "__call__") as call: + client.get_spec(request=None) + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetSpecRequest() -@pytest.mark.parametrize( - "request_type", - [ - locations_pb2.ListLocationsRequest, - dict, - ], -) -def test_list_locations_rest(request_type): + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_spec_contents_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) - request_init = {"name": "projects/sample1"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = locations_pb2.ListLocationsResponse() + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_spec_contents), "__call__" + ) as call: + client.get_spec_contents(request=None) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetSpecContentsRequest() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + assert args[0] == request_msg - response = client.list_locations(request) - # Establish that the response is the type that we expect. - assert isinstance(response, locations_pb2.ListLocationsResponse) +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_specs_empty_call_rest(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.list_specs), "__call__") as call: + client.list_specs(request=None) + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListSpecsRequest() -def test_cancel_operation_rest_bad_request( - request_type=operations_pb2.CancelOperationRequest, -): + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_update_spec_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request - ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.cancel_operation(request) + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.update_spec), "__call__") as call: + client.update_spec(request=None) + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateSpecRequest() -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.CancelOperationRequest, - dict, - ], -) -def test_cancel_operation_rest(request_type): + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_spec_empty_call_rest(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.delete_spec), "__call__") as call: + client.delete_spec(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteSpecRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_api_operation_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = None - - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "{}" - response_value.content = json_return_value.encode("UTF-8") - - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_api_operation), "__call__" + ) as call: + client.create_api_operation(request=None) - response = client.cancel_operation(request) + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateApiOperationRequest() - # Establish that the response is the type that we expect. - assert response is None + assert args[0] == request_msg -def test_delete_operation_rest_bad_request( - request_type=operations_pb2.DeleteOperationRequest, -): +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_api_operation_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request - ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_operation(request) + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_api_operation), "__call__" + ) as call: + client.get_api_operation(request=None) + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetApiOperationRequest() -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.DeleteOperationRequest, - dict, - ], -) -def test_delete_operation_rest(request_type): + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_api_operations_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = None - - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "{}" - response_value.content = json_return_value.encode("UTF-8") - - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_api_operations), "__call__" + ) as call: + client.list_api_operations(request=None) - response = client.delete_operation(request) + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListApiOperationsRequest() - # Establish that the response is the type that we expect. - assert response is None + assert args[0] == request_msg -def test_get_operation_rest_bad_request( - request_type=operations_pb2.GetOperationRequest, -): +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_update_api_operation_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request - ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_operation(request) + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.update_api_operation), "__call__" + ) as call: + client.update_api_operation(request=None) + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateApiOperationRequest() -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.GetOperationRequest, - dict, - ], -) -def test_get_operation_rest(request_type): + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_api_operation_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation() - - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.delete_api_operation), "__call__" + ) as call: + client.delete_api_operation(request=None) - response = client.get_operation(request) + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteApiOperationRequest() - # Establish that the response is the type that we expect. - assert isinstance(response, operations_pb2.Operation) + assert args[0] == request_msg -def test_list_operations_rest_bad_request( - request_type=operations_pb2.ListOperationsRequest, -): +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_definition_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2"}, request - ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_operations(request) + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_definition), "__call__") as call: + client.get_definition(request=None) + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetDefinitionRequest() -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.ListOperationsRequest, - dict, - ], -) -def test_list_operations_rest(request_type): + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_deployment_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) - request_init = {"name": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.ListOperationsResponse() + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_deployment), "__call__" + ) as call: + client.create_deployment(request=None) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateDeploymentRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_deployment_empty_call_rest(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_deployment), "__call__") as call: + client.get_deployment(request=None) - response = client.list_operations(request) + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetDeploymentRequest() - # Establish that the response is the type that we expect. - assert isinstance(response, operations_pb2.ListOperationsResponse) + assert args[0] == request_msg -def test_initialize_client_w_rest(): +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_deployments_empty_call_rest(): client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - assert client is not None + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.list_deployments), "__call__") as call: + client.list_deployments(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListDeploymentsRequest() + + assert args[0] == request_msg # This test is a coverage failsafe to make sure that totally empty calls, # i.e. request == None and no flattened fields passed, work. -def test_create_api_empty_call_rest(): +def test_update_deployment_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.create_api), "__call__") as call: - client.create_api(request=None) + with mock.patch.object( + type(client.transport.update_deployment), "__call__" + ) as call: + client.update_deployment(request=None) # Establish that the underlying stub method was called. call.assert_called() _, args, _ = call.mock_calls[0] - request_msg = apihub_service.CreateApiRequest() + request_msg = apihub_service.UpdateDeploymentRequest() assert args[0] == request_msg # This test is a coverage failsafe to make sure that totally empty calls, # i.e. request == None and no flattened fields passed, work. -def test_get_api_empty_call_rest(): +def test_delete_deployment_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.get_api), "__call__") as call: - client.get_api(request=None) + with mock.patch.object( + type(client.transport.delete_deployment), "__call__" + ) as call: + client.delete_deployment(request=None) # Establish that the underlying stub method was called. call.assert_called() _, args, _ = call.mock_calls[0] - request_msg = apihub_service.GetApiRequest() + request_msg = apihub_service.DeleteDeploymentRequest() assert args[0] == request_msg # This test is a coverage failsafe to make sure that totally empty calls, # i.e. request == None and no flattened fields passed, work. -def test_list_apis_empty_call_rest(): +def test_create_attribute_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.list_apis), "__call__") as call: - client.list_apis(request=None) + with mock.patch.object(type(client.transport.create_attribute), "__call__") as call: + client.create_attribute(request=None) # Establish that the underlying stub method was called. call.assert_called() _, args, _ = call.mock_calls[0] - request_msg = apihub_service.ListApisRequest() + request_msg = apihub_service.CreateAttributeRequest() assert args[0] == request_msg # This test is a coverage failsafe to make sure that totally empty calls, # i.e. request == None and no flattened fields passed, work. -def test_update_api_empty_call_rest(): +def test_get_attribute_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.update_api), "__call__") as call: - client.update_api(request=None) + with mock.patch.object(type(client.transport.get_attribute), "__call__") as call: + client.get_attribute(request=None) # Establish that the underlying stub method was called. call.assert_called() _, args, _ = call.mock_calls[0] - request_msg = apihub_service.UpdateApiRequest() + request_msg = apihub_service.GetAttributeRequest() assert args[0] == request_msg # This test is a coverage failsafe to make sure that totally empty calls, # i.e. request == None and no flattened fields passed, work. -def test_delete_api_empty_call_rest(): +def test_update_attribute_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.delete_api), "__call__") as call: - client.delete_api(request=None) + with mock.patch.object(type(client.transport.update_attribute), "__call__") as call: + client.update_attribute(request=None) # Establish that the underlying stub method was called. call.assert_called() _, args, _ = call.mock_calls[0] - request_msg = apihub_service.DeleteApiRequest() + request_msg = apihub_service.UpdateAttributeRequest() assert args[0] == request_msg # This test is a coverage failsafe to make sure that totally empty calls, # i.e. request == None and no flattened fields passed, work. -def test_create_version_empty_call_rest(): +def test_delete_attribute_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.create_version), "__call__") as call: - client.create_version(request=None) + with mock.patch.object(type(client.transport.delete_attribute), "__call__") as call: + client.delete_attribute(request=None) # Establish that the underlying stub method was called. call.assert_called() _, args, _ = call.mock_calls[0] - request_msg = apihub_service.CreateVersionRequest() + request_msg = apihub_service.DeleteAttributeRequest() assert args[0] == request_msg # This test is a coverage failsafe to make sure that totally empty calls, # i.e. request == None and no flattened fields passed, work. -def test_get_version_empty_call_rest(): +def test_list_attributes_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.get_version), "__call__") as call: - client.get_version(request=None) + with mock.patch.object(type(client.transport.list_attributes), "__call__") as call: + client.list_attributes(request=None) # Establish that the underlying stub method was called. call.assert_called() _, args, _ = call.mock_calls[0] - request_msg = apihub_service.GetVersionRequest() + request_msg = apihub_service.ListAttributesRequest() assert args[0] == request_msg # This test is a coverage failsafe to make sure that totally empty calls, # i.e. request == None and no flattened fields passed, work. -def test_list_versions_empty_call_rest(): +def test_search_resources_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.list_versions), "__call__") as call: - client.list_versions(request=None) + with mock.patch.object(type(client.transport.search_resources), "__call__") as call: + client.search_resources(request=None) # Establish that the underlying stub method was called. call.assert_called() _, args, _ = call.mock_calls[0] - request_msg = apihub_service.ListVersionsRequest() + request_msg = apihub_service.SearchResourcesRequest() assert args[0] == request_msg # This test is a coverage failsafe to make sure that totally empty calls, # i.e. request == None and no flattened fields passed, work. -def test_update_version_empty_call_rest(): +def test_create_external_api_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.update_version), "__call__") as call: - client.update_version(request=None) + with mock.patch.object( + type(client.transport.create_external_api), "__call__" + ) as call: + client.create_external_api(request=None) # Establish that the underlying stub method was called. call.assert_called() _, args, _ = call.mock_calls[0] - request_msg = apihub_service.UpdateVersionRequest() + request_msg = apihub_service.CreateExternalApiRequest() assert args[0] == request_msg # This test is a coverage failsafe to make sure that totally empty calls, # i.e. request == None and no flattened fields passed, work. -def test_delete_version_empty_call_rest(): +def test_get_external_api_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.delete_version), "__call__") as call: - client.delete_version(request=None) + with mock.patch.object(type(client.transport.get_external_api), "__call__") as call: + client.get_external_api(request=None) # Establish that the underlying stub method was called. call.assert_called() _, args, _ = call.mock_calls[0] - request_msg = apihub_service.DeleteVersionRequest() + request_msg = apihub_service.GetExternalApiRequest() assert args[0] == request_msg # This test is a coverage failsafe to make sure that totally empty calls, # i.e. request == None and no flattened fields passed, work. -def test_create_spec_empty_call_rest(): +def test_update_external_api_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.create_spec), "__call__") as call: - client.create_spec(request=None) + with mock.patch.object( + type(client.transport.update_external_api), "__call__" + ) as call: + client.update_external_api(request=None) # Establish that the underlying stub method was called. call.assert_called() _, args, _ = call.mock_calls[0] - request_msg = apihub_service.CreateSpecRequest() + request_msg = apihub_service.UpdateExternalApiRequest() assert args[0] == request_msg # This test is a coverage failsafe to make sure that totally empty calls, # i.e. request == None and no flattened fields passed, work. -def test_get_spec_empty_call_rest(): +def test_delete_external_api_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.get_spec), "__call__") as call: - client.get_spec(request=None) + with mock.patch.object( + type(client.transport.delete_external_api), "__call__" + ) as call: + client.delete_external_api(request=None) # Establish that the underlying stub method was called. call.assert_called() _, args, _ = call.mock_calls[0] - request_msg = apihub_service.GetSpecRequest() + request_msg = apihub_service.DeleteExternalApiRequest() assert args[0] == request_msg # This test is a coverage failsafe to make sure that totally empty calls, # i.e. request == None and no flattened fields passed, work. -def test_get_spec_contents_empty_call_rest(): +def test_list_external_apis_empty_call_rest(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -15628,1243 +32932,1851 @@ def test_get_spec_contents_empty_call_rest(): # Mock the actual call, and fake the request. with mock.patch.object( - type(client.transport.get_spec_contents), "__call__" + type(client.transport.list_external_apis), "__call__" ) as call: - client.get_spec_contents(request=None) + client.list_external_apis(request=None) # Establish that the underlying stub method was called. call.assert_called() _, args, _ = call.mock_calls[0] - request_msg = apihub_service.GetSpecContentsRequest() + request_msg = apihub_service.ListExternalApisRequest() + + assert args[0] == request_msg + + +def test_transport_grpc_default(): + # A client should use the gRPC transport by default. + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert isinstance( + client.transport, + transports.ApiHubGrpcTransport, + ) + + +def test_api_hub_base_transport_error(): + # Passing both a credentials object and credentials_file should raise an error + with pytest.raises(core_exceptions.DuplicateCredentialArgs): + transport = transports.ApiHubTransport( + credentials=ga_credentials.AnonymousCredentials(), + credentials_file="credentials.json", + ) + + +def test_api_hub_base_transport(): + # Instantiate the base transport. + with mock.patch( + "google.cloud.apihub_v1.services.api_hub.transports.ApiHubTransport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.ApiHubTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Every method on the transport should just blindly + # raise NotImplementedError. + methods = ( + "create_api", + "get_api", + "list_apis", + "update_api", + "delete_api", + "create_version", + "get_version", + "list_versions", + "update_version", + "delete_version", + "create_spec", + "get_spec", + "get_spec_contents", + "list_specs", + "update_spec", + "delete_spec", + "create_api_operation", + "get_api_operation", + "list_api_operations", + "update_api_operation", + "delete_api_operation", + "get_definition", + "create_deployment", + "get_deployment", + "list_deployments", + "update_deployment", + "delete_deployment", + "create_attribute", + "get_attribute", + "update_attribute", + "delete_attribute", + "list_attributes", + "search_resources", + "create_external_api", + "get_external_api", + "update_external_api", + "delete_external_api", + "list_external_apis", + "get_location", + "list_locations", + "get_operation", + "cancel_operation", + "delete_operation", + "list_operations", + ) + for method in methods: + with pytest.raises(NotImplementedError): + getattr(transport, method)(request=object()) + + with pytest.raises(NotImplementedError): + transport.close() + + # Catch all for all remaining methods and properties + remainder = [ + "kind", + ] + for r in remainder: + with pytest.raises(NotImplementedError): + getattr(transport, r)() + + +def test_api_hub_base_transport_with_credentials_file(): + # Instantiate the base transport with a credentials file + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch( + "google.cloud.apihub_v1.services.api_hub.transports.ApiHubTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) + transport = transports.ApiHubTransport( + credentials_file="credentials.json", + quota_project_id="octopus", + ) + load_creds.assert_called_once_with( + "credentials.json", + scopes=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", + ) - assert args[0] == request_msg +def test_api_hub_base_transport_with_adc(): + # Test the default credentials are used if credentials and credentials_file are None. + with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch( + "google.cloud.apihub_v1.services.api_hub.transports.ApiHubTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport = transports.ApiHubTransport() + adc.assert_called_once() -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_list_specs_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.list_specs), "__call__") as call: - client.list_specs(request=None) +def test_api_hub_auth_adc(): + # If no credentials are provided, we should use ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + ApiHubClient() + adc.assert_called_once_with( + scopes=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id=None, + ) - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.ListSpecsRequest() - assert args[0] == request_msg +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubGrpcTransport, + transports.ApiHubGrpcAsyncIOTransport, + ], +) +def test_api_hub_transport_auth_adc(transport_class): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + adc.assert_called_once_with( + scopes=["1", "2"], + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", + ) -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_update_spec_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubGrpcTransport, + transports.ApiHubGrpcAsyncIOTransport, + transports.ApiHubRestTransport, + ], +) +def test_api_hub_transport_auth_gdch_credentials(transport_class): + host = "https://language.com" + api_audience_tests = [None, "https://language2.com"] + api_audience_expect = [host, "https://language2.com"] + for t, e in zip(api_audience_tests, api_audience_expect): + with mock.patch.object(google.auth, "default", autospec=True) as adc: + gdch_mock = mock.MagicMock() + type(gdch_mock).with_gdch_audience = mock.PropertyMock( + return_value=gdch_mock + ) + adc.return_value = (gdch_mock, None) + transport_class(host=host, api_audience=t) + gdch_mock.with_gdch_audience.assert_called_once_with(e) - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.update_spec), "__call__") as call: - client.update_spec(request=None) - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.UpdateSpecRequest() +@pytest.mark.parametrize( + "transport_class,grpc_helpers", + [ + (transports.ApiHubGrpcTransport, grpc_helpers), + (transports.ApiHubGrpcAsyncIOTransport, grpc_helpers_async), + ], +) +def test_api_hub_transport_create_channel(transport_class, grpc_helpers): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel", autospec=True + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + adc.return_value = (creds, None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + + create_channel.assert_called_with( + "apihub.googleapis.com:443", + credentials=creds, + credentials_file=None, + quota_project_id="octopus", + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + scopes=["1", "2"], + default_host="apihub.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) - assert args[0] == request_msg +@pytest.mark.parametrize( + "transport_class", + [transports.ApiHubGrpcTransport, transports.ApiHubGrpcAsyncIOTransport], +) +def test_api_hub_grpc_transport_client_cert_source_for_mtls(transport_class): + cred = ga_credentials.AnonymousCredentials() -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_delete_spec_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + # Check ssl_channel_credentials is used if provided. + with mock.patch.object(transport_class, "create_channel") as mock_create_channel: + mock_ssl_channel_creds = mock.Mock() + transport_class( + host="squid.clam.whelk", + credentials=cred, + ssl_channel_credentials=mock_ssl_channel_creds, + ) + mock_create_channel.assert_called_once_with( + "squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_channel_creds, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.delete_spec), "__call__") as call: - client.delete_spec(request=None) + # Check if ssl_channel_credentials is not provided, then client_cert_source_for_mtls + # is used. + with mock.patch.object(transport_class, "create_channel", return_value=mock.Mock()): + with mock.patch("grpc.ssl_channel_credentials") as mock_ssl_cred: + transport_class( + credentials=cred, + client_cert_source_for_mtls=client_cert_source_callback, + ) + expected_cert, expected_key = client_cert_source_callback() + mock_ssl_cred.assert_called_once_with( + certificate_chain=expected_cert, private_key=expected_key + ) - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.DeleteSpecRequest() - assert args[0] == request_msg +def test_api_hub_http_transport_client_cert_source_for_mtls(): + cred = ga_credentials.AnonymousCredentials() + with mock.patch( + "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" + ) as mock_configure_mtls_channel: + transports.ApiHubRestTransport( + credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + ) + mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_create_api_operation_empty_call_rest(): +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "grpc_asyncio", + "rest", + ], +) +def test_api_hub_host_no_port(transport_name): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + client_options=client_options.ClientOptions( + api_endpoint="apihub.googleapis.com" + ), + transport=transport_name, + ) + assert client.transport._host == ( + "apihub.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://apihub.googleapis.com" ) - - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.create_api_operation), "__call__" - ) as call: - client.create_api_operation(request=None) - - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.CreateApiOperationRequest() - - assert args[0] == request_msg -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_get_api_operation_empty_call_rest(): +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "grpc_asyncio", + "rest", + ], +) +def test_api_hub_host_with_port(transport_name): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + client_options=client_options.ClientOptions( + api_endpoint="apihub.googleapis.com:8000" + ), + transport=transport_name, + ) + assert client.transport._host == ( + "apihub.googleapis.com:8000" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://apihub.googleapis.com:8000" ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.get_api_operation), "__call__" - ) as call: - client.get_api_operation(request=None) - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.GetApiOperationRequest() +@pytest.mark.parametrize( + "transport_name", + [ + "rest", + ], +) +def test_api_hub_client_transport_session_collision(transport_name): + creds1 = ga_credentials.AnonymousCredentials() + creds2 = ga_credentials.AnonymousCredentials() + client1 = ApiHubClient( + credentials=creds1, + transport=transport_name, + ) + client2 = ApiHubClient( + credentials=creds2, + transport=transport_name, + ) + session1 = client1.transport.create_api._session + session2 = client2.transport.create_api._session + assert session1 != session2 + session1 = client1.transport.get_api._session + session2 = client2.transport.get_api._session + assert session1 != session2 + session1 = client1.transport.list_apis._session + session2 = client2.transport.list_apis._session + assert session1 != session2 + session1 = client1.transport.update_api._session + session2 = client2.transport.update_api._session + assert session1 != session2 + session1 = client1.transport.delete_api._session + session2 = client2.transport.delete_api._session + assert session1 != session2 + session1 = client1.transport.create_version._session + session2 = client2.transport.create_version._session + assert session1 != session2 + session1 = client1.transport.get_version._session + session2 = client2.transport.get_version._session + assert session1 != session2 + session1 = client1.transport.list_versions._session + session2 = client2.transport.list_versions._session + assert session1 != session2 + session1 = client1.transport.update_version._session + session2 = client2.transport.update_version._session + assert session1 != session2 + session1 = client1.transport.delete_version._session + session2 = client2.transport.delete_version._session + assert session1 != session2 + session1 = client1.transport.create_spec._session + session2 = client2.transport.create_spec._session + assert session1 != session2 + session1 = client1.transport.get_spec._session + session2 = client2.transport.get_spec._session + assert session1 != session2 + session1 = client1.transport.get_spec_contents._session + session2 = client2.transport.get_spec_contents._session + assert session1 != session2 + session1 = client1.transport.list_specs._session + session2 = client2.transport.list_specs._session + assert session1 != session2 + session1 = client1.transport.update_spec._session + session2 = client2.transport.update_spec._session + assert session1 != session2 + session1 = client1.transport.delete_spec._session + session2 = client2.transport.delete_spec._session + assert session1 != session2 + session1 = client1.transport.create_api_operation._session + session2 = client2.transport.create_api_operation._session + assert session1 != session2 + session1 = client1.transport.get_api_operation._session + session2 = client2.transport.get_api_operation._session + assert session1 != session2 + session1 = client1.transport.list_api_operations._session + session2 = client2.transport.list_api_operations._session + assert session1 != session2 + session1 = client1.transport.update_api_operation._session + session2 = client2.transport.update_api_operation._session + assert session1 != session2 + session1 = client1.transport.delete_api_operation._session + session2 = client2.transport.delete_api_operation._session + assert session1 != session2 + session1 = client1.transport.get_definition._session + session2 = client2.transport.get_definition._session + assert session1 != session2 + session1 = client1.transport.create_deployment._session + session2 = client2.transport.create_deployment._session + assert session1 != session2 + session1 = client1.transport.get_deployment._session + session2 = client2.transport.get_deployment._session + assert session1 != session2 + session1 = client1.transport.list_deployments._session + session2 = client2.transport.list_deployments._session + assert session1 != session2 + session1 = client1.transport.update_deployment._session + session2 = client2.transport.update_deployment._session + assert session1 != session2 + session1 = client1.transport.delete_deployment._session + session2 = client2.transport.delete_deployment._session + assert session1 != session2 + session1 = client1.transport.create_attribute._session + session2 = client2.transport.create_attribute._session + assert session1 != session2 + session1 = client1.transport.get_attribute._session + session2 = client2.transport.get_attribute._session + assert session1 != session2 + session1 = client1.transport.update_attribute._session + session2 = client2.transport.update_attribute._session + assert session1 != session2 + session1 = client1.transport.delete_attribute._session + session2 = client2.transport.delete_attribute._session + assert session1 != session2 + session1 = client1.transport.list_attributes._session + session2 = client2.transport.list_attributes._session + assert session1 != session2 + session1 = client1.transport.search_resources._session + session2 = client2.transport.search_resources._session + assert session1 != session2 + session1 = client1.transport.create_external_api._session + session2 = client2.transport.create_external_api._session + assert session1 != session2 + session1 = client1.transport.get_external_api._session + session2 = client2.transport.get_external_api._session + assert session1 != session2 + session1 = client1.transport.update_external_api._session + session2 = client2.transport.update_external_api._session + assert session1 != session2 + session1 = client1.transport.delete_external_api._session + session2 = client2.transport.delete_external_api._session + assert session1 != session2 + session1 = client1.transport.list_external_apis._session + session2 = client2.transport.list_external_apis._session + assert session1 != session2 - assert args[0] == request_msg +def test_api_hub_grpc_transport_channel(): + channel = grpc.secure_channel("http://localhost/", grpc.local_channel_credentials()) -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_list_api_operations_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + # Check that channel is used if provided. + transport = transports.ApiHubGrpcTransport( + host="squid.clam.whelk", + channel=channel, ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.list_api_operations), "__call__" - ) as call: - client.list_api_operations(request=None) - - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.ListApiOperationsRequest() - - assert args[0] == request_msg +def test_api_hub_grpc_asyncio_transport_channel(): + channel = aio.secure_channel("http://localhost/", grpc.local_channel_credentials()) -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_update_api_operation_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + # Check that channel is used if provided. + transport = transports.ApiHubGrpcAsyncIOTransport( + host="squid.clam.whelk", + channel=channel, ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.update_api_operation), "__call__" - ) as call: - client.update_api_operation(request=None) - - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.UpdateApiOperationRequest() - - assert args[0] == request_msg +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. +@pytest.mark.parametrize( + "transport_class", + [transports.ApiHubGrpcTransport, transports.ApiHubGrpcAsyncIOTransport], +) +def test_api_hub_transport_channel_mtls_with_client_cert_source(transport_class): + with mock.patch( + "grpc.ssl_channel_credentials", autospec=True + ) as grpc_ssl_channel_cred: + with mock.patch.object( + transport_class, "create_channel" + ) 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 = ga_credentials.AnonymousCredentials() + with pytest.warns(DeprecationWarning): + with mock.patch.object(google.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() -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_delete_api_operation_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + 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=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + assert transport._ssl_channel_credentials == mock_ssl_cred - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.delete_api_operation), "__call__" - ) as call: - client.delete_api_operation(request=None) - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.DeleteApiOperationRequest() +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. +@pytest.mark.parametrize( + "transport_class", + [transports.ApiHubGrpcTransport, transports.ApiHubGrpcAsyncIOTransport], +) +def test_api_hub_transport_channel_mtls_with_adc(transport_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), + ): + with mock.patch.object( + transport_class, "create_channel" + ) 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, + ) - assert args[0] == request_msg + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=mock_cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_get_definition_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +def test_api_path(): + project = "squid" + location = "clam" + api = "whelk" + expected = "projects/{project}/locations/{location}/apis/{api}".format( + project=project, + location=location, + api=api, ) + actual = ApiHubClient.api_path(project, location, api) + assert expected == actual - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.get_definition), "__call__") as call: - client.get_definition(request=None) - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.GetDefinitionRequest() +def test_parse_api_path(): + expected = { + "project": "octopus", + "location": "oyster", + "api": "nudibranch", + } + path = ApiHubClient.api_path(**expected) - assert args[0] == request_msg + # Check that the path construction is reversible. + actual = ApiHubClient.parse_api_path(path) + assert expected == actual -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_create_deployment_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +def test_api_operation_path(): + project = "cuttlefish" + location = "mussel" + api = "winkle" + version = "nautilus" + operation = "scallop" + expected = "projects/{project}/locations/{location}/apis/{api}/versions/{version}/operations/{operation}".format( + project=project, + location=location, + api=api, + version=version, + operation=operation, ) + actual = ApiHubClient.api_operation_path(project, location, api, version, operation) + assert expected == actual - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.create_deployment), "__call__" - ) as call: - client.create_deployment(request=None) - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.CreateDeploymentRequest() +def test_parse_api_operation_path(): + expected = { + "project": "abalone", + "location": "squid", + "api": "clam", + "version": "whelk", + "operation": "octopus", + } + path = ApiHubClient.api_operation_path(**expected) - assert args[0] == request_msg + # Check that the path construction is reversible. + actual = ApiHubClient.parse_api_operation_path(path) + assert expected == actual -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_get_deployment_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +def test_attribute_path(): + project = "oyster" + location = "nudibranch" + attribute = "cuttlefish" + expected = "projects/{project}/locations/{location}/attributes/{attribute}".format( + project=project, + location=location, + attribute=attribute, ) + actual = ApiHubClient.attribute_path(project, location, attribute) + assert expected == actual - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.get_deployment), "__call__") as call: - client.get_deployment(request=None) - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.GetDeploymentRequest() +def test_parse_attribute_path(): + expected = { + "project": "mussel", + "location": "winkle", + "attribute": "nautilus", + } + path = ApiHubClient.attribute_path(**expected) - assert args[0] == request_msg + # Check that the path construction is reversible. + actual = ApiHubClient.parse_attribute_path(path) + assert expected == actual -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_list_deployments_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +def test_definition_path(): + project = "scallop" + location = "abalone" + api = "squid" + version = "clam" + definition = "whelk" + expected = "projects/{project}/locations/{location}/apis/{api}/versions/{version}/definitions/{definition}".format( + project=project, + location=location, + api=api, + version=version, + definition=definition, ) + actual = ApiHubClient.definition_path(project, location, api, version, definition) + assert expected == actual - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.list_deployments), "__call__") as call: - client.list_deployments(request=None) - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.ListDeploymentsRequest() +def test_parse_definition_path(): + expected = { + "project": "octopus", + "location": "oyster", + "api": "nudibranch", + "version": "cuttlefish", + "definition": "mussel", + } + path = ApiHubClient.definition_path(**expected) - assert args[0] == request_msg + # Check that the path construction is reversible. + actual = ApiHubClient.parse_definition_path(path) + assert expected == actual -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_update_deployment_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +def test_deployment_path(): + project = "winkle" + location = "nautilus" + deployment = "scallop" + expected = ( + "projects/{project}/locations/{location}/deployments/{deployment}".format( + project=project, + location=location, + deployment=deployment, + ) ) + actual = ApiHubClient.deployment_path(project, location, deployment) + assert expected == actual - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.update_deployment), "__call__" - ) as call: - client.update_deployment(request=None) - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.UpdateDeploymentRequest() +def test_parse_deployment_path(): + expected = { + "project": "abalone", + "location": "squid", + "deployment": "clam", + } + path = ApiHubClient.deployment_path(**expected) - assert args[0] == request_msg + # Check that the path construction is reversible. + actual = ApiHubClient.parse_deployment_path(path) + assert expected == actual -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_delete_deployment_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +def test_external_api_path(): + project = "whelk" + location = "octopus" + external_api = "oyster" + expected = ( + "projects/{project}/locations/{location}/externalApis/{external_api}".format( + project=project, + location=location, + external_api=external_api, + ) ) + actual = ApiHubClient.external_api_path(project, location, external_api) + assert expected == actual - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.delete_deployment), "__call__" - ) as call: - client.delete_deployment(request=None) - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.DeleteDeploymentRequest() +def test_parse_external_api_path(): + expected = { + "project": "nudibranch", + "location": "cuttlefish", + "external_api": "mussel", + } + path = ApiHubClient.external_api_path(**expected) - assert args[0] == request_msg + # Check that the path construction is reversible. + actual = ApiHubClient.parse_external_api_path(path) + assert expected == actual -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_create_attribute_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +def test_plugin_instance_path(): + project = "winkle" + location = "nautilus" + plugin = "scallop" + instance = "abalone" + expected = "projects/{project}/locations/{location}/plugins/{plugin}/instances/{instance}".format( + project=project, + location=location, + plugin=plugin, + instance=instance, ) + actual = ApiHubClient.plugin_instance_path(project, location, plugin, instance) + assert expected == actual - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.create_attribute), "__call__") as call: - client.create_attribute(request=None) - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.CreateAttributeRequest() +def test_parse_plugin_instance_path(): + expected = { + "project": "squid", + "location": "clam", + "plugin": "whelk", + "instance": "octopus", + } + path = ApiHubClient.plugin_instance_path(**expected) - assert args[0] == request_msg + # Check that the path construction is reversible. + actual = ApiHubClient.parse_plugin_instance_path(path) + assert expected == actual -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_get_attribute_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +def test_spec_path(): + project = "oyster" + location = "nudibranch" + api = "cuttlefish" + version = "mussel" + spec = "winkle" + expected = "projects/{project}/locations/{location}/apis/{api}/versions/{version}/specs/{spec}".format( + project=project, + location=location, + api=api, + version=version, + spec=spec, ) + actual = ApiHubClient.spec_path(project, location, api, version, spec) + assert expected == actual - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.get_attribute), "__call__") as call: - client.get_attribute(request=None) - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.GetAttributeRequest() +def test_parse_spec_path(): + expected = { + "project": "nautilus", + "location": "scallop", + "api": "abalone", + "version": "squid", + "spec": "clam", + } + path = ApiHubClient.spec_path(**expected) - assert args[0] == request_msg + # Check that the path construction is reversible. + actual = ApiHubClient.parse_spec_path(path) + assert expected == actual -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_update_attribute_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +def test_version_path(): + project = "whelk" + location = "octopus" + api = "oyster" + version = "nudibranch" + expected = ( + "projects/{project}/locations/{location}/apis/{api}/versions/{version}".format( + project=project, + location=location, + api=api, + version=version, + ) ) + actual = ApiHubClient.version_path(project, location, api, version) + assert expected == actual - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.update_attribute), "__call__") as call: - client.update_attribute(request=None) - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.UpdateAttributeRequest() +def test_parse_version_path(): + expected = { + "project": "cuttlefish", + "location": "mussel", + "api": "winkle", + "version": "nautilus", + } + path = ApiHubClient.version_path(**expected) - assert args[0] == request_msg + # Check that the path construction is reversible. + actual = ApiHubClient.parse_version_path(path) + assert expected == actual -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_delete_attribute_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +def test_common_billing_account_path(): + billing_account = "scallop" + expected = "billingAccounts/{billing_account}".format( + billing_account=billing_account, ) + actual = ApiHubClient.common_billing_account_path(billing_account) + assert expected == actual - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.delete_attribute), "__call__") as call: - client.delete_attribute(request=None) - - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.DeleteAttributeRequest() - assert args[0] == request_msg +def test_parse_common_billing_account_path(): + expected = { + "billing_account": "abalone", + } + path = ApiHubClient.common_billing_account_path(**expected) + # Check that the path construction is reversible. + actual = ApiHubClient.parse_common_billing_account_path(path) + assert expected == actual -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_list_attributes_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + +def test_common_folder_path(): + folder = "squid" + expected = "folders/{folder}".format( + folder=folder, ) + actual = ApiHubClient.common_folder_path(folder) + assert expected == actual - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.list_attributes), "__call__") as call: - client.list_attributes(request=None) - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.ListAttributesRequest() +def test_parse_common_folder_path(): + expected = { + "folder": "clam", + } + path = ApiHubClient.common_folder_path(**expected) - assert args[0] == request_msg + # Check that the path construction is reversible. + actual = ApiHubClient.parse_common_folder_path(path) + assert expected == actual -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_search_resources_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +def test_common_organization_path(): + organization = "whelk" + expected = "organizations/{organization}".format( + organization=organization, ) + actual = ApiHubClient.common_organization_path(organization) + assert expected == actual - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.search_resources), "__call__") as call: - client.search_resources(request=None) - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.SearchResourcesRequest() +def test_parse_common_organization_path(): + expected = { + "organization": "octopus", + } + path = ApiHubClient.common_organization_path(**expected) - assert args[0] == request_msg + # Check that the path construction is reversible. + actual = ApiHubClient.parse_common_organization_path(path) + assert expected == actual -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_create_external_api_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +def test_common_project_path(): + project = "oyster" + expected = "projects/{project}".format( + project=project, ) + actual = ApiHubClient.common_project_path(project) + assert expected == actual - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.create_external_api), "__call__" - ) as call: - client.create_external_api(request=None) - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.CreateExternalApiRequest() +def test_parse_common_project_path(): + expected = { + "project": "nudibranch", + } + path = ApiHubClient.common_project_path(**expected) - assert args[0] == request_msg + # Check that the path construction is reversible. + actual = ApiHubClient.parse_common_project_path(path) + assert expected == actual -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_get_external_api_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +def test_common_location_path(): + project = "cuttlefish" + location = "mussel" + expected = "projects/{project}/locations/{location}".format( + project=project, + location=location, ) + actual = ApiHubClient.common_location_path(project, location) + assert expected == actual - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.get_external_api), "__call__") as call: - client.get_external_api(request=None) - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.GetExternalApiRequest() +def test_parse_common_location_path(): + expected = { + "project": "winkle", + "location": "nautilus", + } + path = ApiHubClient.common_location_path(**expected) - assert args[0] == request_msg + # Check that the path construction is reversible. + actual = ApiHubClient.parse_common_location_path(path) + assert expected == actual -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_update_external_api_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) +def test_client_with_default_client_info(): + client_info = gapic_v1.client_info.ClientInfo() - # Mock the actual call, and fake the request. with mock.patch.object( - type(client.transport.update_external_api), "__call__" - ) as call: - client.update_external_api(request=None) - - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = apihub_service.UpdateExternalApiRequest() + transports.ApiHubTransport, "_prep_wrapped_messages" + ) as prep: + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + client_info=client_info, + ) + prep.assert_called_once_with(client_info) - assert args[0] == request_msg + with mock.patch.object( + transports.ApiHubTransport, "_prep_wrapped_messages" + ) as prep: + transport_class = ApiHubClient.get_transport_class() + transport = transport_class( + credentials=ga_credentials.AnonymousCredentials(), + client_info=client_info, + ) + prep.assert_called_once_with(client_info) -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_delete_external_api_empty_call_rest(): +def test_delete_operation(transport: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.delete_external_api), "__call__" - ) as call: - client.delete_external_api(request=None) + # 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 = operations_pb2.DeleteOperationRequest() - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.delete_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = apihub_service.DeleteExternalApiRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the response is the type that we expect. + assert response is None -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_list_external_apis_empty_call_rest(): - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +@pytest.mark.asyncio +async def test_delete_operation_async(transport: str = "grpc_asyncio"): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.list_external_apis), "__call__" - ) as call: - client.list_external_apis(request=None) + # 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 = operations_pb2.DeleteOperationRequest() - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = apihub_service.ListExternalApisRequest() - - assert args[0] == request_msg - - -def test_api_hub_base_transport_error(): - # Passing both a credentials object and credentials_file should raise an error - with pytest.raises(core_exceptions.DuplicateCredentialArgs): - transport = transports.ApiHubTransport( - credentials=ga_credentials.AnonymousCredentials(), - credentials_file="credentials.json", - ) + assert args[0] == request + # Establish that the response is the type that we expect. + assert response is None -def test_api_hub_base_transport(): - # Instantiate the base transport. - with mock.patch( - "google.cloud.apihub_v1.services.api_hub.transports.ApiHubTransport.__init__" - ) as Transport: - Transport.return_value = None - transport = transports.ApiHubTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - # Every method on the transport should just blindly - # raise NotImplementedError. - methods = ( - "create_api", - "get_api", - "list_apis", - "update_api", - "delete_api", - "create_version", - "get_version", - "list_versions", - "update_version", - "delete_version", - "create_spec", - "get_spec", - "get_spec_contents", - "list_specs", - "update_spec", - "delete_spec", - "create_api_operation", - "get_api_operation", - "list_api_operations", - "update_api_operation", - "delete_api_operation", - "get_definition", - "create_deployment", - "get_deployment", - "list_deployments", - "update_deployment", - "delete_deployment", - "create_attribute", - "get_attribute", - "update_attribute", - "delete_attribute", - "list_attributes", - "search_resources", - "create_external_api", - "get_external_api", - "update_external_api", - "delete_external_api", - "list_external_apis", - "get_location", - "list_locations", - "get_operation", - "cancel_operation", - "delete_operation", - "list_operations", +def test_delete_operation_field_headers(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), ) - for method in methods: - with pytest.raises(NotImplementedError): - getattr(transport, method)(request=object()) - with pytest.raises(NotImplementedError): - transport.close() + # 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 = operations_pb2.DeleteOperationRequest() + request.name = "locations" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + call.return_value = None - # Catch all for all remaining methods and properties - remainder = [ - "kind", - ] - for r in remainder: - with pytest.raises(NotImplementedError): - getattr(transport, r)() + client.delete_operation(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", + "name=locations", + ) in kw["metadata"] -def test_api_hub_base_transport_with_credentials_file(): - # Instantiate the base transport with a credentials file - with mock.patch.object( - google.auth, "load_credentials_from_file", autospec=True - ) as load_creds, mock.patch( - "google.cloud.apihub_v1.services.api_hub.transports.ApiHubTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None - load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) - transport = transports.ApiHubTransport( - credentials_file="credentials.json", - quota_project_id="octopus", - ) - load_creds.assert_called_once_with( - "credentials.json", - scopes=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id="octopus", - ) +@pytest.mark.asyncio +async def test_delete_operation_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) -def test_api_hub_base_transport_with_adc(): - # Test the default credentials are used if credentials and credentials_file are None. - with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch( - "google.cloud.apihub_v1.services.api_hub.transports.ApiHubTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport = transports.ApiHubTransport() - adc.assert_called_once() + # 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 = operations_pb2.DeleteOperationRequest() + request.name = "locations" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_operation(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 -def test_api_hub_auth_adc(): - # If no credentials are provided, we should use ADC credentials. - with mock.patch.object(google.auth, "default", autospec=True) as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - ApiHubClient() - adc.assert_called_once_with( - scopes=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id=None, + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] + + +def test_delete_operation_from_dict(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + + response = client.delete_operation( + request={ + "name": "locations", + } ) + call.assert_called() -def test_api_hub_http_transport_client_cert_source_for_mtls(): - cred = ga_credentials.AnonymousCredentials() - with mock.patch( - "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" - ) as mock_configure_mtls_channel: - transports.ApiHubRestTransport( - credentials=cred, client_cert_source_for_mtls=client_cert_source_callback +@pytest.mark.asyncio +async def test_delete_operation_from_dict_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_operation( + request={ + "name": "locations", + } ) - mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + call.assert_called() -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_api_hub_host_no_port(transport_name): +def test_cancel_operation(transport: str = "grpc"): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - client_options=client_options.ClientOptions( - api_endpoint="apihub.googleapis.com" - ), - transport=transport_name, + transport=transport, ) - assert client.transport._host == ( - "apihub.googleapis.com:443" - if transport_name in ["grpc", "grpc_asyncio"] - else "https://apihub.googleapis.com" + + # 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 = operations_pb2.CancelOperationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.cancel_operation(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 response is the type that we expect. + assert response is None + + +@pytest.mark.asyncio +async def test_cancel_operation_async(transport: str = "grpc_asyncio"): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.CancelOperationRequest() -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_api_hub_host_with_port(transport_name): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.cancel_operation(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 response is the type that we expect. + assert response is None + + +def test_cancel_operation_field_headers(): client = ApiHubClient( credentials=ga_credentials.AnonymousCredentials(), - client_options=client_options.ClientOptions( - api_endpoint="apihub.googleapis.com:8000" - ), - transport=transport_name, - ) - assert client.transport._host == ( - "apihub.googleapis.com:8000" - if transport_name in ["grpc", "grpc_asyncio"] - else "https://apihub.googleapis.com:8000" ) + # 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 = operations_pb2.CancelOperationRequest() + request.name = "locations" -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_api_hub_client_transport_session_collision(transport_name): - creds1 = ga_credentials.AnonymousCredentials() - creds2 = ga_credentials.AnonymousCredentials() - client1 = ApiHubClient( - credentials=creds1, - transport=transport_name, - ) - client2 = ApiHubClient( - credentials=creds2, - transport=transport_name, - ) - session1 = client1.transport.create_api._session - session2 = client2.transport.create_api._session - assert session1 != session2 - session1 = client1.transport.get_api._session - session2 = client2.transport.get_api._session - assert session1 != session2 - session1 = client1.transport.list_apis._session - session2 = client2.transport.list_apis._session - assert session1 != session2 - session1 = client1.transport.update_api._session - session2 = client2.transport.update_api._session - assert session1 != session2 - session1 = client1.transport.delete_api._session - session2 = client2.transport.delete_api._session - assert session1 != session2 - session1 = client1.transport.create_version._session - session2 = client2.transport.create_version._session - assert session1 != session2 - session1 = client1.transport.get_version._session - session2 = client2.transport.get_version._session - assert session1 != session2 - session1 = client1.transport.list_versions._session - session2 = client2.transport.list_versions._session - assert session1 != session2 - session1 = client1.transport.update_version._session - session2 = client2.transport.update_version._session - assert session1 != session2 - session1 = client1.transport.delete_version._session - session2 = client2.transport.delete_version._session - assert session1 != session2 - session1 = client1.transport.create_spec._session - session2 = client2.transport.create_spec._session - assert session1 != session2 - session1 = client1.transport.get_spec._session - session2 = client2.transport.get_spec._session - assert session1 != session2 - session1 = client1.transport.get_spec_contents._session - session2 = client2.transport.get_spec_contents._session - assert session1 != session2 - session1 = client1.transport.list_specs._session - session2 = client2.transport.list_specs._session - assert session1 != session2 - session1 = client1.transport.update_spec._session - session2 = client2.transport.update_spec._session - assert session1 != session2 - session1 = client1.transport.delete_spec._session - session2 = client2.transport.delete_spec._session - assert session1 != session2 - session1 = client1.transport.create_api_operation._session - session2 = client2.transport.create_api_operation._session - assert session1 != session2 - session1 = client1.transport.get_api_operation._session - session2 = client2.transport.get_api_operation._session - assert session1 != session2 - session1 = client1.transport.list_api_operations._session - session2 = client2.transport.list_api_operations._session - assert session1 != session2 - session1 = client1.transport.update_api_operation._session - session2 = client2.transport.update_api_operation._session - assert session1 != session2 - session1 = client1.transport.delete_api_operation._session - session2 = client2.transport.delete_api_operation._session - assert session1 != session2 - session1 = client1.transport.get_definition._session - session2 = client2.transport.get_definition._session - assert session1 != session2 - session1 = client1.transport.create_deployment._session - session2 = client2.transport.create_deployment._session - assert session1 != session2 - session1 = client1.transport.get_deployment._session - session2 = client2.transport.get_deployment._session - assert session1 != session2 - session1 = client1.transport.list_deployments._session - session2 = client2.transport.list_deployments._session - assert session1 != session2 - session1 = client1.transport.update_deployment._session - session2 = client2.transport.update_deployment._session - assert session1 != session2 - session1 = client1.transport.delete_deployment._session - session2 = client2.transport.delete_deployment._session - assert session1 != session2 - session1 = client1.transport.create_attribute._session - session2 = client2.transport.create_attribute._session - assert session1 != session2 - session1 = client1.transport.get_attribute._session - session2 = client2.transport.get_attribute._session - assert session1 != session2 - session1 = client1.transport.update_attribute._session - session2 = client2.transport.update_attribute._session - assert session1 != session2 - session1 = client1.transport.delete_attribute._session - session2 = client2.transport.delete_attribute._session - assert session1 != session2 - session1 = client1.transport.list_attributes._session - session2 = client2.transport.list_attributes._session - assert session1 != session2 - session1 = client1.transport.search_resources._session - session2 = client2.transport.search_resources._session - assert session1 != session2 - session1 = client1.transport.create_external_api._session - session2 = client2.transport.create_external_api._session - assert session1 != session2 - session1 = client1.transport.get_external_api._session - session2 = client2.transport.get_external_api._session - assert session1 != session2 - session1 = client1.transport.update_external_api._session - session2 = client2.transport.update_external_api._session - assert session1 != session2 - session1 = client1.transport.delete_external_api._session - session2 = client2.transport.delete_external_api._session - assert session1 != session2 - session1 = client1.transport.list_external_apis._session - session2 = client2.transport.list_external_apis._session - assert session1 != session2 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + call.return_value = None + + client.cancel_operation(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", + "name=locations", + ) in kw["metadata"] -def test_api_path(): - project = "squid" - location = "clam" - api = "whelk" - expected = "projects/{project}/locations/{location}/apis/{api}".format( - project=project, - location=location, - api=api, + +@pytest.mark.asyncio +async def test_cancel_operation_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - actual = ApiHubClient.api_path(project, location, api) - assert expected == actual + # 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 = operations_pb2.CancelOperationRequest() + request.name = "locations" -def test_parse_api_path(): - expected = { - "project": "octopus", - "location": "oyster", - "api": "nudibranch", - } - path = ApiHubClient.api_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.cancel_operation(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 - # Check that the path construction is reversible. - actual = ApiHubClient.parse_api_path(path) - assert expected == actual + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -def test_api_operation_path(): - project = "cuttlefish" - location = "mussel" - api = "winkle" - version = "nautilus" - operation = "scallop" - expected = "projects/{project}/locations/{location}/apis/{api}/versions/{version}/operations/{operation}".format( - project=project, - location=location, - api=api, - version=version, - operation=operation, +def test_cancel_operation_from_dict(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), ) - actual = ApiHubClient.api_operation_path(project, location, api, version, operation) - assert expected == actual + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.cancel_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_parse_api_operation_path(): - expected = { - "project": "abalone", - "location": "squid", - "api": "clam", - "version": "whelk", - "operation": "octopus", - } - path = ApiHubClient.api_operation_path(**expected) - # Check that the path construction is reversible. - actual = ApiHubClient.parse_api_operation_path(path) - assert expected == actual +@pytest.mark.asyncio +async def test_cancel_operation_from_dict_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.cancel_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_attribute_path(): - project = "oyster" - location = "nudibranch" - attribute = "cuttlefish" - expected = "projects/{project}/locations/{location}/attributes/{attribute}".format( - project=project, - location=location, - attribute=attribute, +def test_get_operation(transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - actual = ApiHubClient.attribute_path(project, location, attribute) - assert expected == actual + # 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 = operations_pb2.GetOperationRequest() -def test_parse_attribute_path(): - expected = { - "project": "mussel", - "location": "winkle", - "attribute": "nautilus", - } - path = ApiHubClient.attribute_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation() + response = client.get_operation(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 - # Check that the path construction is reversible. - actual = ApiHubClient.parse_attribute_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) -def test_definition_path(): - project = "scallop" - location = "abalone" - api = "squid" - version = "clam" - definition = "whelk" - expected = "projects/{project}/locations/{location}/apis/{api}/versions/{version}/definitions/{definition}".format( - project=project, - location=location, - api=api, - version=version, - definition=definition, +@pytest.mark.asyncio +async def test_get_operation_async(transport: str = "grpc_asyncio"): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - actual = ApiHubClient.definition_path(project, location, api, version, definition) - assert expected == actual + # 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 = operations_pb2.GetOperationRequest() -def test_parse_definition_path(): - expected = { - "project": "octopus", - "location": "oyster", - "api": "nudibranch", - "version": "cuttlefish", - "definition": "mussel", - } - path = ApiHubClient.definition_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + response = await client.get_operation(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 - # Check that the path construction is reversible. - actual = ApiHubClient.parse_definition_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) -def test_deployment_path(): - project = "winkle" - location = "nautilus" - deployment = "scallop" - expected = ( - "projects/{project}/locations/{location}/deployments/{deployment}".format( - project=project, - location=location, - deployment=deployment, +def test_get_operation_field_headers(): + client = ApiHubClient( + credentials=ga_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 = operations_pb2.GetOperationRequest() + request.name = "locations" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + call.return_value = operations_pb2.Operation() + + client.get_operation(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", + "name=locations", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_get_operation_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = operations_pb2.GetOperationRequest() + request.name = "locations" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() ) + await client.get_operation(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", + "name=locations", + ) in kw["metadata"] + + +def test_get_operation_from_dict(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), ) - actual = ApiHubClient.deployment_path(project, location, deployment) - assert expected == actual + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation() + + response = client.get_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_parse_deployment_path(): - expected = { - "project": "abalone", - "location": "squid", - "deployment": "clam", - } - path = ApiHubClient.deployment_path(**expected) +@pytest.mark.asyncio +async def test_get_operation_from_dict_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + response = await client.get_operation( + request={ + "name": "locations", + } + ) + call.assert_called() - # Check that the path construction is reversible. - actual = ApiHubClient.parse_deployment_path(path) - assert expected == actual +def test_list_operations(transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) -def test_external_api_path(): - project = "whelk" - location = "octopus" - external_api = "oyster" - expected = ( - "projects/{project}/locations/{location}/externalApis/{external_api}".format( - project=project, - location=location, - external_api=external_api, + # 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 = operations_pb2.ListOperationsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.ListOperationsResponse() + response = client.list_operations(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 response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) + + +@pytest.mark.asyncio +async def test_list_operations_async(transport: str = "grpc_asyncio"): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.ListOperationsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() + ) + response = await client.list_operations(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 response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) + + +def test_list_operations_field_headers(): + client = ApiHubClient( + credentials=ga_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 = operations_pb2.ListOperationsRequest() + request.name = "locations" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + call.return_value = operations_pb2.ListOperationsResponse() + + client.list_operations(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", + "name=locations", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_list_operations_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = operations_pb2.ListOperationsRequest() + request.name = "locations" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() + ) + await client.list_operations(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", + "name=locations", + ) in kw["metadata"] + + +def test_list_operations_from_dict(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.ListOperationsResponse() + + response = client.list_operations( + request={ + "name": "locations", + } + ) + call.assert_called() + + +@pytest.mark.asyncio +async def test_list_operations_from_dict_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() + ) + response = await client.list_operations( + request={ + "name": "locations", + } ) + call.assert_called() + + +def test_list_locations(transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_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 = locations_pb2.ListLocationsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.ListLocationsResponse() + response = client.list_locations(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 response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + +@pytest.mark.asyncio +async def test_list_locations_async(transport: str = "grpc_asyncio"): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - actual = ApiHubClient.external_api_path(project, location, external_api) - assert expected == actual + # 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 = locations_pb2.ListLocationsRequest() -def test_parse_external_api_path(): - expected = { - "project": "nudibranch", - "location": "cuttlefish", - "external_api": "mussel", - } - path = ApiHubClient.external_api_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + response = await client.list_locations(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 - # Check that the path construction is reversible. - actual = ApiHubClient.parse_external_api_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) -def test_plugin_instance_path(): - project = "winkle" - location = "nautilus" - plugin = "scallop" - instance = "abalone" - expected = "projects/{project}/locations/{location}/plugins/{plugin}/instances/{instance}".format( - project=project, - location=location, - plugin=plugin, - instance=instance, +def test_list_locations_field_headers(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), ) - actual = ApiHubClient.plugin_instance_path(project, location, plugin, instance) - assert expected == actual + # 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 = locations_pb2.ListLocationsRequest() + request.name = "locations" -def test_parse_plugin_instance_path(): - expected = { - "project": "squid", - "location": "clam", - "plugin": "whelk", - "instance": "octopus", - } - path = ApiHubClient.plugin_instance_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + call.return_value = locations_pb2.ListLocationsResponse() - # Check that the path construction is reversible. - actual = ApiHubClient.parse_plugin_instance_path(path) - assert expected == actual + client.list_locations(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", + "name=locations", + ) in kw["metadata"] -def test_spec_path(): - project = "oyster" - location = "nudibranch" - api = "cuttlefish" - version = "mussel" - spec = "winkle" - expected = "projects/{project}/locations/{location}/apis/{api}/versions/{version}/specs/{spec}".format( - project=project, - location=location, - api=api, - version=version, - spec=spec, + +@pytest.mark.asyncio +async def test_list_locations_field_headers_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), ) - actual = ApiHubClient.spec_path(project, location, api, version, spec) - assert expected == actual + # 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 = locations_pb2.ListLocationsRequest() + request.name = "locations" -def test_parse_spec_path(): - expected = { - "project": "nautilus", - "location": "scallop", - "api": "abalone", - "version": "squid", - "spec": "clam", - } - path = ApiHubClient.spec_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + await client.list_locations(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 - # Check that the path construction is reversible. - actual = ApiHubClient.parse_spec_path(path) - assert expected == actual + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -def test_version_path(): - project = "whelk" - location = "octopus" - api = "oyster" - version = "nudibranch" - expected = ( - "projects/{project}/locations/{location}/apis/{api}/versions/{version}".format( - project=project, - location=location, - api=api, - version=version, - ) +def test_list_locations_from_dict(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), ) - actual = ApiHubClient.version_path(project, location, api, version) - assert expected == actual + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.ListLocationsResponse() + response = client.list_locations( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_parse_version_path(): - expected = { - "project": "cuttlefish", - "location": "mussel", - "api": "winkle", - "version": "nautilus", - } - path = ApiHubClient.version_path(**expected) - # Check that the path construction is reversible. - actual = ApiHubClient.parse_version_path(path) - assert expected == actual +@pytest.mark.asyncio +async def test_list_locations_from_dict_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + response = await client.list_locations( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_common_billing_account_path(): - billing_account = "scallop" - expected = "billingAccounts/{billing_account}".format( - billing_account=billing_account, +def test_get_location(transport: str = "grpc"): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - actual = ApiHubClient.common_billing_account_path(billing_account) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() -def test_parse_common_billing_account_path(): - expected = { - "billing_account": "abalone", - } - path = ApiHubClient.common_billing_account_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.Location() + response = client.get_location(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 - # Check that the path construction is reversible. - actual = ApiHubClient.parse_common_billing_account_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) -def test_common_folder_path(): - folder = "squid" - expected = "folders/{folder}".format( - folder=folder, +@pytest.mark.asyncio +async def test_get_location_async(transport: str = "grpc_asyncio"): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - actual = ApiHubClient.common_folder_path(folder) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() -def test_parse_common_folder_path(): - expected = { - "folder": "clam", - } - path = ApiHubClient.common_folder_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + response = await client.get_location(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 - # Check that the path construction is reversible. - actual = ApiHubClient.parse_common_folder_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) -def test_common_organization_path(): - organization = "whelk" - expected = "organizations/{organization}".format( - organization=organization, - ) - actual = ApiHubClient.common_organization_path(organization) - assert expected == actual +def test_get_location_field_headers(): + client = ApiHubClient(credentials=ga_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 = locations_pb2.GetLocationRequest() + request.name = "locations/abc" -def test_parse_common_organization_path(): - expected = { - "organization": "octopus", - } - path = ApiHubClient.common_organization_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + call.return_value = locations_pb2.Location() - # Check that the path construction is reversible. - actual = ApiHubClient.parse_common_organization_path(path) - assert expected == actual + client.get_location(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", + "name=locations/abc", + ) in kw["metadata"] -def test_common_project_path(): - project = "oyster" - expected = "projects/{project}".format( - project=project, - ) - actual = ApiHubClient.common_project_path(project) - assert expected == actual +@pytest.mark.asyncio +async def test_get_location_field_headers_async(): + client = ApiHubAsyncClient(credentials=async_anonymous_credentials()) -def test_parse_common_project_path(): - expected = { - "project": "nudibranch", - } - path = ApiHubClient.common_project_path(**expected) + # 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 = locations_pb2.GetLocationRequest() + request.name = "locations/abc" - # Check that the path construction is reversible. - actual = ApiHubClient.parse_common_project_path(path) - assert expected == actual + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + await client.get_location(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", + "name=locations/abc", + ) in kw["metadata"] -def test_common_location_path(): - project = "cuttlefish" - location = "mussel" - expected = "projects/{project}/locations/{location}".format( - project=project, - location=location, - ) - actual = ApiHubClient.common_location_path(project, location) - assert expected == actual +def test_get_location_from_dict(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.Location() -def test_parse_common_location_path(): - expected = { - "project": "winkle", - "location": "nautilus", - } - path = ApiHubClient.common_location_path(**expected) + response = client.get_location( + request={ + "name": "locations/abc", + } + ) + call.assert_called() - # Check that the path construction is reversible. - actual = ApiHubClient.parse_common_location_path(path) - assert expected == actual +@pytest.mark.asyncio +async def test_get_location_from_dict_async(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + response = await client.get_location( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_client_with_default_client_info(): - client_info = gapic_v1.client_info.ClientInfo() +def test_transport_close_grpc(): + client = ApiHubClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) with mock.patch.object( - transports.ApiHubTransport, "_prep_wrapped_messages" - ) as prep: - client = ApiHubClient( - credentials=ga_credentials.AnonymousCredentials(), - client_info=client_info, - ) - prep.assert_called_once_with(client_info) + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + +@pytest.mark.asyncio +async def test_transport_close_grpc_asyncio(): + client = ApiHubAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) with mock.patch.object( - transports.ApiHubTransport, "_prep_wrapped_messages" - ) as prep: - transport_class = ApiHubClient.get_transport_class() - transport = transport_class( - credentials=ga_credentials.AnonymousCredentials(), - client_info=client_info, - ) - prep.assert_called_once_with(client_info) + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + async with client: + close.assert_not_called() + close.assert_called_once() def test_transport_close_rest(): @@ -16882,6 +34794,7 @@ def test_transport_close_rest(): def test_client_ctx(): transports = [ "rest", + "grpc", ] for transport in transports: client = ApiHubClient( @@ -16898,7 +34811,8 @@ def test_client_ctx(): @pytest.mark.parametrize( "client_class,transport_class", [ - (ApiHubClient, transports.ApiHubRestTransport), + (ApiHubClient, transports.ApiHubGrpcTransport), + (ApiHubAsyncClient, transports.ApiHubGrpcAsyncIOTransport), ], ) def test_api_key_credentials(client_class, transport_class): diff --git a/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub_collect.py b/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub_collect.py index c11ea7f92b96..1c01a6810f68 100644 --- a/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub_collect.py +++ b/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub_collect.py @@ -65,6 +65,7 @@ from google.protobuf import timestamp_pb2 # type: ignore from google.cloud.apihub_v1.services.api_hub_collect import ( + ApiHubCollectAsyncClient, ApiHubCollectClient, transports, ) @@ -245,6 +246,11 @@ def test__get_client_cert_source(): "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubCollectClient), ) +@mock.patch.object( + ApiHubCollectAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubCollectAsyncClient), +) def test__get_api_endpoint(): api_override = "foo.com" mock_client_cert_source = mock.Mock() @@ -372,6 +378,8 @@ def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code): @pytest.mark.parametrize( "client_class,transport_name", [ + (ApiHubCollectClient, "grpc"), + (ApiHubCollectAsyncClient, "grpc_asyncio"), (ApiHubCollectClient, "rest"), ], ) @@ -396,6 +404,8 @@ def test_api_hub_collect_client_from_service_account_info(client_class, transpor @pytest.mark.parametrize( "transport_class,transport_name", [ + (transports.ApiHubCollectGrpcTransport, "grpc"), + (transports.ApiHubCollectGrpcAsyncIOTransport, "grpc_asyncio"), (transports.ApiHubCollectRestTransport, "rest"), ], ) @@ -420,6 +430,8 @@ def test_api_hub_collect_client_service_account_always_use_jwt( @pytest.mark.parametrize( "client_class,transport_name", [ + (ApiHubCollectClient, "grpc"), + (ApiHubCollectAsyncClient, "grpc_asyncio"), (ApiHubCollectClient, "rest"), ], ) @@ -451,17 +463,24 @@ def test_api_hub_collect_client_from_service_account_file(client_class, transpor def test_api_hub_collect_client_get_transport_class(): transport = ApiHubCollectClient.get_transport_class() available_transports = [ + transports.ApiHubCollectGrpcTransport, transports.ApiHubCollectRestTransport, ] assert transport in available_transports - transport = ApiHubCollectClient.get_transport_class("rest") - assert transport == transports.ApiHubCollectRestTransport + transport = ApiHubCollectClient.get_transport_class("grpc") + assert transport == transports.ApiHubCollectGrpcTransport @pytest.mark.parametrize( "client_class,transport_class,transport_name", [ + (ApiHubCollectClient, transports.ApiHubCollectGrpcTransport, "grpc"), + ( + ApiHubCollectAsyncClient, + transports.ApiHubCollectGrpcAsyncIOTransport, + "grpc_asyncio", + ), (ApiHubCollectClient, transports.ApiHubCollectRestTransport, "rest"), ], ) @@ -470,6 +489,11 @@ def test_api_hub_collect_client_get_transport_class(): "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubCollectClient), ) +@mock.patch.object( + ApiHubCollectAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubCollectAsyncClient), +) def test_api_hub_collect_client_client_options( client_class, transport_class, transport_name ): @@ -603,6 +627,20 @@ def test_api_hub_collect_client_client_options( @pytest.mark.parametrize( "client_class,transport_class,transport_name,use_client_cert_env", [ + (ApiHubCollectClient, transports.ApiHubCollectGrpcTransport, "grpc", "true"), + ( + ApiHubCollectAsyncClient, + transports.ApiHubCollectGrpcAsyncIOTransport, + "grpc_asyncio", + "true", + ), + (ApiHubCollectClient, transports.ApiHubCollectGrpcTransport, "grpc", "false"), + ( + ApiHubCollectAsyncClient, + transports.ApiHubCollectGrpcAsyncIOTransport, + "grpc_asyncio", + "false", + ), (ApiHubCollectClient, transports.ApiHubCollectRestTransport, "rest", "true"), (ApiHubCollectClient, transports.ApiHubCollectRestTransport, "rest", "false"), ], @@ -612,6 +650,11 @@ def test_api_hub_collect_client_client_options( "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubCollectClient), ) +@mock.patch.object( + ApiHubCollectAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubCollectAsyncClient), +) @mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) def test_api_hub_collect_client_mtls_env_auto( client_class, transport_class, transport_name, use_client_cert_env @@ -715,12 +758,19 @@ def test_api_hub_collect_client_mtls_env_auto( ) -@pytest.mark.parametrize("client_class", [ApiHubCollectClient]) +@pytest.mark.parametrize( + "client_class", [ApiHubCollectClient, ApiHubCollectAsyncClient] +) @mock.patch.object( ApiHubCollectClient, "DEFAULT_ENDPOINT", modify_default_endpoint(ApiHubCollectClient), ) +@mock.patch.object( + ApiHubCollectAsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(ApiHubCollectAsyncClient), +) def test_api_hub_collect_client_get_mtls_endpoint_and_cert_source(client_class): mock_client_cert_source = mock.Mock() @@ -812,12 +862,19 @@ def test_api_hub_collect_client_get_mtls_endpoint_and_cert_source(client_class): ) -@pytest.mark.parametrize("client_class", [ApiHubCollectClient]) +@pytest.mark.parametrize( + "client_class", [ApiHubCollectClient, ApiHubCollectAsyncClient] +) @mock.patch.object( ApiHubCollectClient, "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubCollectClient), ) +@mock.patch.object( + ApiHubCollectAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubCollectAsyncClient), +) def test_api_hub_collect_client_client_api_endpoint(client_class): mock_client_cert_source = client_cert_source_callback api_override = "foo.com" @@ -894,6 +951,12 @@ def test_api_hub_collect_client_client_api_endpoint(client_class): @pytest.mark.parametrize( "client_class,transport_class,transport_name", [ + (ApiHubCollectClient, transports.ApiHubCollectGrpcTransport, "grpc"), + ( + ApiHubCollectAsyncClient, + transports.ApiHubCollectGrpcAsyncIOTransport, + "grpc_asyncio", + ), (ApiHubCollectClient, transports.ApiHubCollectRestTransport, "rest"), ], ) @@ -925,6 +988,18 @@ def test_api_hub_collect_client_client_options_scopes( @pytest.mark.parametrize( "client_class,transport_class,transport_name,grpc_helpers", [ + ( + ApiHubCollectClient, + transports.ApiHubCollectGrpcTransport, + "grpc", + grpc_helpers, + ), + ( + ApiHubCollectAsyncClient, + transports.ApiHubCollectGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), (ApiHubCollectClient, transports.ApiHubCollectRestTransport, "rest", None), ], ) @@ -952,13 +1027,168 @@ def test_api_hub_collect_client_client_options_credentials_file( ) -def test_collect_api_data_rest_use_cached_wrapped_rpc(): +def test_api_hub_collect_client_client_options_from_dict(): + with mock.patch( + "google.cloud.apihub_v1.services.api_hub_collect.transports.ApiHubCollectGrpcTransport.__init__" + ) as grpc_transport: + grpc_transport.return_value = None + client = ApiHubCollectClient( + client_options={"api_endpoint": "squid.clam.whelk"} + ) + grpc_transport.assert_called_once_with( + credentials=None, + credentials_file=None, + host="squid.clam.whelk", + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name,grpc_helpers", + [ + ( + ApiHubCollectClient, + transports.ApiHubCollectGrpcTransport, + "grpc", + grpc_helpers, + ), + ( + ApiHubCollectAsyncClient, + transports.ApiHubCollectGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), + ], +) +def test_api_hub_collect_client_create_channel_credentials_file( + client_class, transport_class, transport_name, grpc_helpers +): + # Check the case credentials file is provided. + options = client_options.ClientOptions(credentials_file="credentials.json") + + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options, transport=transport_name) + patched.assert_called_once_with( + credentials=None, + credentials_file="credentials.json", + host=client._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE + ), + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + # test that the credentials from file are saved and used as the credentials. + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel" + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + file_creds = ga_credentials.AnonymousCredentials() + load_creds.return_value = (file_creds, None) + adc.return_value = (creds, None) + client = client_class(client_options=options, transport=transport_name) + create_channel.assert_called_with( + "apihub.googleapis.com:443", + credentials=file_creds, + credentials_file=None, + quota_project_id=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + scopes=None, + default_host="apihub.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + +@pytest.mark.parametrize( + "request_type", + [ + collect_service.CollectApiDataRequest, + dict, + ], +) +def test_collect_api_data(request_type, transport: str = "grpc"): + client = ApiHubCollectClient( + credentials=ga_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.collect_api_data), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/spam") + response = client.collect_api_data(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = collect_service.CollectApiDataRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) + + +def test_collect_api_data_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubCollectClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = collect_service.CollectApiDataRequest( + location="location_value", + plugin_instance="plugin_instance_value", + action_id="action_id_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.collect_api_data), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.collect_api_data(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == collect_service.CollectApiDataRequest( + location="location_value", + plugin_instance="plugin_instance_value", + action_id="action_id_value", + ) + + +def test_collect_api_data_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubCollectClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -976,15 +1206,15 @@ def test_collect_api_data_rest_use_cached_wrapped_rpc(): client._transport._wrapped_methods[ client._transport.collect_api_data ] = mock_rpc - request = {} client.collect_api_data(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - # Operation methods build a cached wrapper on first rpc call - # subsequent calls should use the cached wrapper + # Operation methods call wrapper_fn to build a cached + # client._transport.operations_client instance on first rpc call. + # Subsequent calls should use the cached wrapper wrapper_fn.reset_mock() client.collect_api_data(request) @@ -994,116 +1224,452 @@ def test_collect_api_data_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_collect_api_data_rest_required_fields( - request_type=collect_service.CollectApiDataRequest, +@pytest.mark.asyncio +async def test_collect_api_data_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubCollectRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["location"] = "" - request_init["plugin_instance"] = "" - request_init["action_id"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.collect_api_data + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.collect_api_data + ] = mock_rpc + + request = {} + await client.collect_api_data(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + # Operation methods call wrapper_fn to build a cached + # client._transport.operations_client instance on first rpc call. + # Subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() + + await client.collect_api_data(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_collect_api_data_async( + transport: str = "grpc_asyncio", request_type=collect_service.CollectApiDataRequest +): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # verify fields with default values are dropped + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).collect_api_data._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.collect_api_data), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + response = await client.collect_api_data(request) - # verify required fields with default values are now present + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = collect_service.CollectApiDataRequest() + assert args[0] == request - jsonified_request["location"] = "location_value" - jsonified_request["pluginInstance"] = "plugin_instance_value" - jsonified_request["actionId"] = "action_id_value" + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).collect_api_data._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "location" in jsonified_request - assert jsonified_request["location"] == "location_value" - assert "pluginInstance" in jsonified_request - assert jsonified_request["pluginInstance"] == "plugin_instance_value" - assert "actionId" in jsonified_request - assert jsonified_request["actionId"] == "action_id_value" +@pytest.mark.asyncio +async def test_collect_api_data_async_from_dict(): + await test_collect_api_data_async(request_type=dict) + +def test_collect_api_data_field_headers(): client = ApiHubCollectClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # 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 = collect_service.CollectApiDataRequest() - response_value = Response() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) + request.location = "location_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.collect_api_data), "__call__") as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.collect_api_data(request) - response = client.collect_api_data(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "location=location_value", + ) in kw["metadata"] -def test_collect_api_data_rest_unset_required_fields(): - transport = transports.ApiHubCollectRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_collect_api_data_field_headers_async(): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.collect_api_data._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(()) - & set( - ( - "location", - "collectionType", - "pluginInstance", - "actionId", - "apiData", - ) + # 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 = collect_service.CollectApiDataRequest() + + request.location = "location_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.collect_api_data), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/op") ) - ) + await client.collect_api_data(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", + "location=location_value", + ) in kw["metadata"] -def test_collect_api_data_rest_flattened(): +def test_collect_api_data_flattened(): client = ApiHubCollectClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.collect_api_data), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.collect_api_data( + location="location_value", + collection_type=collect_service.CollectionType.COLLECTION_TYPE_UPSERT, + api_data=collect_service.ApiData( + api_metadata_list=collect_service.ApiMetadataList( + api_metadata=[ + collect_service.APIMetadata( + api=common_fields.Api(name="name_value") + ) + ] + ) + ), + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].location + mock_val = "location_value" + assert arg == mock_val + arg = args[0].collection_type + mock_val = collect_service.CollectionType.COLLECTION_TYPE_UPSERT + assert arg == mock_val + arg = args[0].api_data + mock_val = collect_service.ApiData( + api_metadata_list=collect_service.ApiMetadataList( + api_metadata=[ + collect_service.APIMetadata( + api=common_fields.Api(name="name_value") + ) + ] + ) + ) + assert arg == mock_val + + +def test_collect_api_data_flattened_error(): + client = ApiHubCollectClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.collect_api_data( + collect_service.CollectApiDataRequest(), + location="location_value", + collection_type=collect_service.CollectionType.COLLECTION_TYPE_UPSERT, + api_data=collect_service.ApiData( + api_metadata_list=collect_service.ApiMetadataList( + api_metadata=[ + collect_service.APIMetadata( + api=common_fields.Api(name="name_value") + ) + ] + ) + ), + ) + + +@pytest.mark.asyncio +async def test_collect_api_data_flattened_async(): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.collect_api_data), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.collect_api_data( + location="location_value", + collection_type=collect_service.CollectionType.COLLECTION_TYPE_UPSERT, + api_data=collect_service.ApiData( + api_metadata_list=collect_service.ApiMetadataList( + api_metadata=[ + collect_service.APIMetadata( + api=common_fields.Api(name="name_value") + ) + ] + ) + ), + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].location + mock_val = "location_value" + assert arg == mock_val + arg = args[0].collection_type + mock_val = collect_service.CollectionType.COLLECTION_TYPE_UPSERT + assert arg == mock_val + arg = args[0].api_data + mock_val = collect_service.ApiData( + api_metadata_list=collect_service.ApiMetadataList( + api_metadata=[ + collect_service.APIMetadata( + api=common_fields.Api(name="name_value") + ) + ] + ) + ) + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_collect_api_data_flattened_error_async(): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.collect_api_data( + collect_service.CollectApiDataRequest(), + location="location_value", + collection_type=collect_service.CollectionType.COLLECTION_TYPE_UPSERT, + api_data=collect_service.ApiData( + api_metadata_list=collect_service.ApiMetadataList( + api_metadata=[ + collect_service.APIMetadata( + api=common_fields.Api(name="name_value") + ) + ] + ) + ), + ) + + +def test_collect_api_data_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubCollectClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.collect_api_data in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.collect_api_data + ] = mock_rpc + + request = {} + client.collect_api_data(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + # Operation methods build a cached wrapper on first rpc call + # subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() + + client.collect_api_data(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_collect_api_data_rest_required_fields( + request_type=collect_service.CollectApiDataRequest, +): + transport_class = transports.ApiHubCollectRestTransport + + request_init = {} + request_init["location"] = "" + request_init["plugin_instance"] = "" + request_init["action_id"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).collect_api_data._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["location"] = "location_value" + jsonified_request["pluginInstance"] = "plugin_instance_value" + jsonified_request["actionId"] = "action_id_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).collect_api_data._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "location" in jsonified_request + assert jsonified_request["location"] == "location_value" + assert "pluginInstance" in jsonified_request + assert jsonified_request["pluginInstance"] == "plugin_instance_value" + assert "actionId" in jsonified_request + assert jsonified_request["actionId"] == "action_id_value" + + client = ApiHubCollectClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.collect_api_data(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_collect_api_data_rest_unset_required_fields(): + transport = transports.ApiHubCollectRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.collect_api_data._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(()) + & set( + ( + "location", + "collectionType", + "pluginInstance", + "actionId", + "apiData", + ) + ) + ) + + +def test_collect_api_data_rest_flattened(): + client = ApiHubCollectClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. return_value = operations_pb2.Operation(name="operations/spam") # get arguments that satisfy an http rule for this method @@ -1173,7 +1739,7 @@ def test_collect_api_data_rest_flattened_error(transport: str = "rest"): def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. - transport = transports.ApiHubCollectRestTransport( + transport = transports.ApiHubCollectGrpcTransport( credentials=ga_credentials.AnonymousCredentials(), ) with pytest.raises(ValueError): @@ -1183,7 +1749,7 @@ def test_credentials_transport_error(): ) # It is an error to provide a credentials file and a transport instance. - transport = transports.ApiHubCollectRestTransport( + transport = transports.ApiHubCollectGrpcTransport( credentials=ga_credentials.AnonymousCredentials(), ) with pytest.raises(ValueError): @@ -1193,7 +1759,7 @@ def test_credentials_transport_error(): ) # It is an error to provide an api_key and a transport instance. - transport = transports.ApiHubCollectRestTransport( + transport = transports.ApiHubCollectGrpcTransport( credentials=ga_credentials.AnonymousCredentials(), ) options = client_options.ClientOptions() @@ -1213,7 +1779,7 @@ def test_credentials_transport_error(): ) # It is an error to provide scopes and a transport instance. - transport = transports.ApiHubCollectRestTransport( + transport = transports.ApiHubCollectGrpcTransport( credentials=ga_credentials.AnonymousCredentials(), ) with pytest.raises(ValueError): @@ -1225,20 +1791,37 @@ def test_credentials_transport_error(): def test_transport_instance(): # A client may be instantiated with a custom transport instance. - transport = transports.ApiHubCollectRestTransport( + transport = transports.ApiHubCollectGrpcTransport( credentials=ga_credentials.AnonymousCredentials(), ) client = ApiHubCollectClient(transport=transport) assert client.transport is transport -@pytest.mark.parametrize( - "transport_class", - [ - transports.ApiHubCollectRestTransport, - ], -) -def test_transport_adc(transport_class): +def test_transport_get_channel(): + # A client may be instantiated with a custom transport instance. + transport = transports.ApiHubCollectGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + transport = transports.ApiHubCollectGrpcAsyncIOTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubCollectGrpcTransport, + transports.ApiHubCollectGrpcAsyncIOTransport, + transports.ApiHubCollectRestTransport, + ], +) +def test_transport_adc(transport_class): # Test default credentials are used if not provided. with mock.patch.object(google.auth, "default") as adc: adc.return_value = (ga_credentials.AnonymousCredentials(), None) @@ -1246,6 +1829,80 @@ def test_transport_adc(transport_class): adc.assert_called_once() +def test_transport_kind_grpc(): + transport = ApiHubCollectClient.get_transport_class("grpc")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "grpc" + + +def test_initialize_client_w_grpc(): + client = ApiHubCollectClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_collect_api_data_empty_call_grpc(): + client = ApiHubCollectClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.collect_api_data), "__call__") as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.collect_api_data(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = collect_service.CollectApiDataRequest() + + assert args[0] == request_msg + + +def test_transport_kind_grpc_asyncio(): + transport = ApiHubCollectAsyncClient.get_transport_class("grpc_asyncio")( + credentials=async_anonymous_credentials() + ) + assert transport.kind == "grpc_asyncio" + + +def test_initialize_client_w_grpc_asyncio(): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_collect_api_data_empty_call_grpc_asyncio(): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.collect_api_data), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + await client.collect_api_data(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = collect_service.CollectApiDataRequest() + + assert args[0] == request_msg + + def test_transport_kind_rest(): transport = ApiHubCollectClient.get_transport_class("rest")( credentials=ga_credentials.AnonymousCredentials() @@ -1788,6 +2445,17 @@ def test_api_hub_collect_rest_lro_client(): assert transport.operations_client is transport.operations_client +def test_transport_grpc_default(): + # A client should use the gRPC transport by default. + client = ApiHubCollectClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert isinstance( + client.transport, + transports.ApiHubCollectGrpcTransport, + ) + + def test_api_hub_collect_base_transport_error(): # Passing both a credentials object and credentials_file should raise an error with pytest.raises(core_exceptions.DuplicateCredentialArgs): @@ -1883,6 +2551,129 @@ def test_api_hub_collect_auth_adc(): ) +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubCollectGrpcTransport, + transports.ApiHubCollectGrpcAsyncIOTransport, + ], +) +def test_api_hub_collect_transport_auth_adc(transport_class): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + adc.assert_called_once_with( + scopes=["1", "2"], + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubCollectGrpcTransport, + transports.ApiHubCollectGrpcAsyncIOTransport, + transports.ApiHubCollectRestTransport, + ], +) +def test_api_hub_collect_transport_auth_gdch_credentials(transport_class): + host = "https://language.com" + api_audience_tests = [None, "https://language2.com"] + api_audience_expect = [host, "https://language2.com"] + for t, e in zip(api_audience_tests, api_audience_expect): + with mock.patch.object(google.auth, "default", autospec=True) as adc: + gdch_mock = mock.MagicMock() + type(gdch_mock).with_gdch_audience = mock.PropertyMock( + return_value=gdch_mock + ) + adc.return_value = (gdch_mock, None) + transport_class(host=host, api_audience=t) + gdch_mock.with_gdch_audience.assert_called_once_with(e) + + +@pytest.mark.parametrize( + "transport_class,grpc_helpers", + [ + (transports.ApiHubCollectGrpcTransport, grpc_helpers), + (transports.ApiHubCollectGrpcAsyncIOTransport, grpc_helpers_async), + ], +) +def test_api_hub_collect_transport_create_channel(transport_class, grpc_helpers): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel", autospec=True + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + adc.return_value = (creds, None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + + create_channel.assert_called_with( + "apihub.googleapis.com:443", + credentials=creds, + credentials_file=None, + quota_project_id="octopus", + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + scopes=["1", "2"], + default_host="apihub.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubCollectGrpcTransport, + transports.ApiHubCollectGrpcAsyncIOTransport, + ], +) +def test_api_hub_collect_grpc_transport_client_cert_source_for_mtls(transport_class): + cred = ga_credentials.AnonymousCredentials() + + # Check ssl_channel_credentials is used if provided. + with mock.patch.object(transport_class, "create_channel") as mock_create_channel: + mock_ssl_channel_creds = mock.Mock() + transport_class( + host="squid.clam.whelk", + credentials=cred, + ssl_channel_credentials=mock_ssl_channel_creds, + ) + mock_create_channel.assert_called_once_with( + "squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_channel_creds, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + # Check if ssl_channel_credentials is not provided, then client_cert_source_for_mtls + # is used. + with mock.patch.object(transport_class, "create_channel", return_value=mock.Mock()): + with mock.patch("grpc.ssl_channel_credentials") as mock_ssl_cred: + transport_class( + credentials=cred, + client_cert_source_for_mtls=client_cert_source_callback, + ) + expected_cert, expected_key = client_cert_source_callback() + mock_ssl_cred.assert_called_once_with( + certificate_chain=expected_cert, private_key=expected_key + ) + + def test_api_hub_collect_http_transport_client_cert_source_for_mtls(): cred = ga_credentials.AnonymousCredentials() with mock.patch( @@ -1897,6 +2688,8 @@ def test_api_hub_collect_http_transport_client_cert_source_for_mtls(): @pytest.mark.parametrize( "transport_name", [ + "grpc", + "grpc_asyncio", "rest", ], ) @@ -1918,6 +2711,8 @@ def test_api_hub_collect_host_no_port(transport_name): @pytest.mark.parametrize( "transport_name", [ + "grpc", + "grpc_asyncio", "rest", ], ) @@ -1958,6 +2753,166 @@ def test_api_hub_collect_client_transport_session_collision(transport_name): assert session1 != session2 +def test_api_hub_collect_grpc_transport_channel(): + channel = grpc.secure_channel("http://localhost/", grpc.local_channel_credentials()) + + # Check that channel is used if provided. + transport = transports.ApiHubCollectGrpcTransport( + host="squid.clam.whelk", + channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None + + +def test_api_hub_collect_grpc_asyncio_transport_channel(): + channel = aio.secure_channel("http://localhost/", grpc.local_channel_credentials()) + + # Check that channel is used if provided. + transport = transports.ApiHubCollectGrpcAsyncIOTransport( + host="squid.clam.whelk", + channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None + + +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubCollectGrpcTransport, + transports.ApiHubCollectGrpcAsyncIOTransport, + ], +) +def test_api_hub_collect_transport_channel_mtls_with_client_cert_source( + transport_class, +): + with mock.patch( + "grpc.ssl_channel_credentials", autospec=True + ) as grpc_ssl_channel_cred: + with mock.patch.object( + transport_class, "create_channel" + ) 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 = ga_credentials.AnonymousCredentials() + with pytest.warns(DeprecationWarning): + with mock.patch.object(google.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=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + assert transport._ssl_channel_credentials == mock_ssl_cred + + +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubCollectGrpcTransport, + transports.ApiHubCollectGrpcAsyncIOTransport, + ], +) +def test_api_hub_collect_transport_channel_mtls_with_adc(transport_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), + ): + with mock.patch.object( + transport_class, "create_channel" + ) 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, + ) + + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=mock_cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + + +def test_api_hub_collect_grpc_lro_client(): + client = ApiHubCollectClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + transport = client.transport + + # Ensure that we have a api-core operations client. + assert isinstance( + transport.operations_client, + operations_v1.OperationsClient, + ) + + # Ensure that subsequent calls to the property send the exact same object. + assert transport.operations_client is transport.operations_client + + +def test_api_hub_collect_grpc_lro_async_client(): + client = ApiHubCollectAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc_asyncio", + ) + transport = client.transport + + # Ensure that we have a api-core operations client. + assert isinstance( + transport.operations_client, + operations_v1.OperationsAsyncClient, + ) + + # Ensure that subsequent calls to the property send the exact same object. + assert transport.operations_client is transport.operations_client + + def test_api_path(): project = "squid" location = "clam" @@ -2326,6 +3281,885 @@ def test_client_with_default_client_info(): prep.assert_called_once_with(client_info) +def test_delete_operation(transport: str = "grpc"): + client = ApiHubCollectClient( + credentials=ga_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 = operations_pb2.DeleteOperationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.delete_operation(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 response is the type that we expect. + assert response is None + + +@pytest.mark.asyncio +async def test_delete_operation_async(transport: str = "grpc_asyncio"): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.DeleteOperationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_operation(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 response is the type that we expect. + assert response is None + + +def test_delete_operation_field_headers(): + client = ApiHubCollectClient( + credentials=ga_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 = operations_pb2.DeleteOperationRequest() + request.name = "locations" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + call.return_value = None + + client.delete_operation(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", + "name=locations", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_delete_operation_field_headers_async(): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = operations_pb2.DeleteOperationRequest() + request.name = "locations" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_operation(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", + "name=locations", + ) in kw["metadata"] + + +def test_delete_operation_from_dict(): + client = ApiHubCollectClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + + response = client.delete_operation( + request={ + "name": "locations", + } + ) + call.assert_called() + + +@pytest.mark.asyncio +async def test_delete_operation_from_dict_async(): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_operation( + request={ + "name": "locations", + } + ) + call.assert_called() + + +def test_cancel_operation(transport: str = "grpc"): + client = ApiHubCollectClient( + credentials=ga_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 = operations_pb2.CancelOperationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.cancel_operation(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 response is the type that we expect. + assert response is None + + +@pytest.mark.asyncio +async def test_cancel_operation_async(transport: str = "grpc_asyncio"): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.CancelOperationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.cancel_operation(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 response is the type that we expect. + assert response is None + + +def test_cancel_operation_field_headers(): + client = ApiHubCollectClient( + credentials=ga_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 = operations_pb2.CancelOperationRequest() + request.name = "locations" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + call.return_value = None + + client.cancel_operation(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", + "name=locations", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_cancel_operation_field_headers_async(): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = operations_pb2.CancelOperationRequest() + request.name = "locations" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.cancel_operation(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", + "name=locations", + ) in kw["metadata"] + + +def test_cancel_operation_from_dict(): + client = ApiHubCollectClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + + response = client.cancel_operation( + request={ + "name": "locations", + } + ) + call.assert_called() + + +@pytest.mark.asyncio +async def test_cancel_operation_from_dict_async(): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.cancel_operation( + request={ + "name": "locations", + } + ) + call.assert_called() + + +def test_get_operation(transport: str = "grpc"): + client = ApiHubCollectClient( + credentials=ga_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 = operations_pb2.GetOperationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation() + response = client.get_operation(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 response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) + + +@pytest.mark.asyncio +async def test_get_operation_async(transport: str = "grpc_asyncio"): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.GetOperationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + response = await client.get_operation(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 response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) + + +def test_get_operation_field_headers(): + client = ApiHubCollectClient( + credentials=ga_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 = operations_pb2.GetOperationRequest() + request.name = "locations" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + call.return_value = operations_pb2.Operation() + + client.get_operation(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", + "name=locations", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_get_operation_field_headers_async(): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = operations_pb2.GetOperationRequest() + request.name = "locations" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + await client.get_operation(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", + "name=locations", + ) in kw["metadata"] + + +def test_get_operation_from_dict(): + client = ApiHubCollectClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation() + + response = client.get_operation( + request={ + "name": "locations", + } + ) + call.assert_called() + + +@pytest.mark.asyncio +async def test_get_operation_from_dict_async(): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + response = await client.get_operation( + request={ + "name": "locations", + } + ) + call.assert_called() + + +def test_list_operations(transport: str = "grpc"): + client = ApiHubCollectClient( + credentials=ga_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 = operations_pb2.ListOperationsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.ListOperationsResponse() + response = client.list_operations(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 response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) + + +@pytest.mark.asyncio +async def test_list_operations_async(transport: str = "grpc_asyncio"): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.ListOperationsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() + ) + response = await client.list_operations(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 response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) + + +def test_list_operations_field_headers(): + client = ApiHubCollectClient( + credentials=ga_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 = operations_pb2.ListOperationsRequest() + request.name = "locations" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + call.return_value = operations_pb2.ListOperationsResponse() + + client.list_operations(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", + "name=locations", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_list_operations_field_headers_async(): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = operations_pb2.ListOperationsRequest() + request.name = "locations" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() + ) + await client.list_operations(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", + "name=locations", + ) in kw["metadata"] + + +def test_list_operations_from_dict(): + client = ApiHubCollectClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.ListOperationsResponse() + + response = client.list_operations( + request={ + "name": "locations", + } + ) + call.assert_called() + + +@pytest.mark.asyncio +async def test_list_operations_from_dict_async(): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() + ) + response = await client.list_operations( + request={ + "name": "locations", + } + ) + call.assert_called() + + +def test_list_locations(transport: str = "grpc"): + client = ApiHubCollectClient( + credentials=ga_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 = locations_pb2.ListLocationsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.ListLocationsResponse() + response = client.list_locations(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 response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + +@pytest.mark.asyncio +async def test_list_locations_async(transport: str = "grpc_asyncio"): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + 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 = locations_pb2.ListLocationsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + response = await client.list_locations(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 response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + +def test_list_locations_field_headers(): + client = ApiHubCollectClient( + credentials=ga_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 = locations_pb2.ListLocationsRequest() + request.name = "locations" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + call.return_value = locations_pb2.ListLocationsResponse() + + client.list_locations(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", + "name=locations", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_list_locations_field_headers_async(): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = locations_pb2.ListLocationsRequest() + request.name = "locations" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + await client.list_locations(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", + "name=locations", + ) in kw["metadata"] + + +def test_list_locations_from_dict(): + client = ApiHubCollectClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.ListLocationsResponse() + + response = client.list_locations( + request={ + "name": "locations", + } + ) + call.assert_called() + + +@pytest.mark.asyncio +async def test_list_locations_from_dict_async(): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + response = await client.list_locations( + request={ + "name": "locations", + } + ) + call.assert_called() + + +def test_get_location(transport: str = "grpc"): + client = ApiHubCollectClient( + credentials=ga_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 = locations_pb2.GetLocationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.Location() + response = client.get_location(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 response is the type that we expect. + assert isinstance(response, locations_pb2.Location) + + +@pytest.mark.asyncio +async def test_get_location_async(transport: str = "grpc_asyncio"): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + 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 = locations_pb2.GetLocationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + response = await client.get_location(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 response is the type that we expect. + assert isinstance(response, locations_pb2.Location) + + +def test_get_location_field_headers(): + client = ApiHubCollectClient(credentials=ga_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 = locations_pb2.GetLocationRequest() + request.name = "locations/abc" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + call.return_value = locations_pb2.Location() + + client.get_location(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", + "name=locations/abc", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_get_location_field_headers_async(): + client = ApiHubCollectAsyncClient(credentials=async_anonymous_credentials()) + + # 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 = locations_pb2.GetLocationRequest() + request.name = "locations/abc" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + await client.get_location(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", + "name=locations/abc", + ) in kw["metadata"] + + +def test_get_location_from_dict(): + client = ApiHubCollectClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.Location() + + response = client.get_location( + request={ + "name": "locations/abc", + } + ) + call.assert_called() + + +@pytest.mark.asyncio +async def test_get_location_from_dict_async(): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + response = await client.get_location( + request={ + "name": "locations", + } + ) + call.assert_called() + + +def test_transport_close_grpc(): + client = ApiHubCollectClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + with mock.patch.object( + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + +@pytest.mark.asyncio +async def test_transport_close_grpc_asyncio(): + client = ApiHubCollectAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) + with mock.patch.object( + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + async with client: + close.assert_not_called() + close.assert_called_once() + + def test_transport_close_rest(): client = ApiHubCollectClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" @@ -2341,6 +4175,7 @@ def test_transport_close_rest(): def test_client_ctx(): transports = [ "rest", + "grpc", ] for transport in transports: client = ApiHubCollectClient( @@ -2357,7 +4192,8 @@ def test_client_ctx(): @pytest.mark.parametrize( "client_class,transport_class", [ - (ApiHubCollectClient, transports.ApiHubCollectRestTransport), + (ApiHubCollectClient, transports.ApiHubCollectGrpcTransport), + (ApiHubCollectAsyncClient, transports.ApiHubCollectGrpcAsyncIOTransport), ], ) def test_api_key_credentials(client_class, transport_class): diff --git a/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub_curate.py b/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub_curate.py index 7d56fe385b88..7ab9a1c8057c 100644 --- a/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub_curate.py +++ b/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub_curate.py @@ -57,6 +57,7 @@ from google.protobuf import timestamp_pb2 # type: ignore from google.cloud.apihub_v1.services.api_hub_curate import ( + ApiHubCurateAsyncClient, ApiHubCurateClient, pagers, transports, @@ -227,6 +228,11 @@ def test__get_client_cert_source(): "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubCurateClient), ) +@mock.patch.object( + ApiHubCurateAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubCurateAsyncClient), +) def test__get_api_endpoint(): api_override = "foo.com" mock_client_cert_source = mock.Mock() @@ -354,6 +360,8 @@ def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code): @pytest.mark.parametrize( "client_class,transport_name", [ + (ApiHubCurateClient, "grpc"), + (ApiHubCurateAsyncClient, "grpc_asyncio"), (ApiHubCurateClient, "rest"), ], ) @@ -378,6 +386,8 @@ def test_api_hub_curate_client_from_service_account_info(client_class, transport @pytest.mark.parametrize( "transport_class,transport_name", [ + (transports.ApiHubCurateGrpcTransport, "grpc"), + (transports.ApiHubCurateGrpcAsyncIOTransport, "grpc_asyncio"), (transports.ApiHubCurateRestTransport, "rest"), ], ) @@ -402,6 +412,8 @@ def test_api_hub_curate_client_service_account_always_use_jwt( @pytest.mark.parametrize( "client_class,transport_name", [ + (ApiHubCurateClient, "grpc"), + (ApiHubCurateAsyncClient, "grpc_asyncio"), (ApiHubCurateClient, "rest"), ], ) @@ -433,17 +445,24 @@ def test_api_hub_curate_client_from_service_account_file(client_class, transport def test_api_hub_curate_client_get_transport_class(): transport = ApiHubCurateClient.get_transport_class() available_transports = [ + transports.ApiHubCurateGrpcTransport, transports.ApiHubCurateRestTransport, ] assert transport in available_transports - transport = ApiHubCurateClient.get_transport_class("rest") - assert transport == transports.ApiHubCurateRestTransport + transport = ApiHubCurateClient.get_transport_class("grpc") + assert transport == transports.ApiHubCurateGrpcTransport @pytest.mark.parametrize( "client_class,transport_class,transport_name", [ + (ApiHubCurateClient, transports.ApiHubCurateGrpcTransport, "grpc"), + ( + ApiHubCurateAsyncClient, + transports.ApiHubCurateGrpcAsyncIOTransport, + "grpc_asyncio", + ), (ApiHubCurateClient, transports.ApiHubCurateRestTransport, "rest"), ], ) @@ -452,6 +471,11 @@ def test_api_hub_curate_client_get_transport_class(): "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubCurateClient), ) +@mock.patch.object( + ApiHubCurateAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubCurateAsyncClient), +) def test_api_hub_curate_client_client_options( client_class, transport_class, transport_name ): @@ -585,6 +609,20 @@ def test_api_hub_curate_client_client_options( @pytest.mark.parametrize( "client_class,transport_class,transport_name,use_client_cert_env", [ + (ApiHubCurateClient, transports.ApiHubCurateGrpcTransport, "grpc", "true"), + ( + ApiHubCurateAsyncClient, + transports.ApiHubCurateGrpcAsyncIOTransport, + "grpc_asyncio", + "true", + ), + (ApiHubCurateClient, transports.ApiHubCurateGrpcTransport, "grpc", "false"), + ( + ApiHubCurateAsyncClient, + transports.ApiHubCurateGrpcAsyncIOTransport, + "grpc_asyncio", + "false", + ), (ApiHubCurateClient, transports.ApiHubCurateRestTransport, "rest", "true"), (ApiHubCurateClient, transports.ApiHubCurateRestTransport, "rest", "false"), ], @@ -594,6 +632,11 @@ def test_api_hub_curate_client_client_options( "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubCurateClient), ) +@mock.patch.object( + ApiHubCurateAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubCurateAsyncClient), +) @mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) def test_api_hub_curate_client_mtls_env_auto( client_class, transport_class, transport_name, use_client_cert_env @@ -697,10 +740,15 @@ def test_api_hub_curate_client_mtls_env_auto( ) -@pytest.mark.parametrize("client_class", [ApiHubCurateClient]) +@pytest.mark.parametrize("client_class", [ApiHubCurateClient, ApiHubCurateAsyncClient]) @mock.patch.object( ApiHubCurateClient, "DEFAULT_ENDPOINT", modify_default_endpoint(ApiHubCurateClient) ) +@mock.patch.object( + ApiHubCurateAsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(ApiHubCurateAsyncClient), +) def test_api_hub_curate_client_get_mtls_endpoint_and_cert_source(client_class): mock_client_cert_source = mock.Mock() @@ -792,12 +840,17 @@ def test_api_hub_curate_client_get_mtls_endpoint_and_cert_source(client_class): ) -@pytest.mark.parametrize("client_class", [ApiHubCurateClient]) +@pytest.mark.parametrize("client_class", [ApiHubCurateClient, ApiHubCurateAsyncClient]) @mock.patch.object( ApiHubCurateClient, "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubCurateClient), ) +@mock.patch.object( + ApiHubCurateAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubCurateAsyncClient), +) def test_api_hub_curate_client_client_api_endpoint(client_class): mock_client_cert_source = client_cert_source_callback api_override = "foo.com" @@ -874,6 +927,12 @@ def test_api_hub_curate_client_client_api_endpoint(client_class): @pytest.mark.parametrize( "client_class,transport_class,transport_name", [ + (ApiHubCurateClient, transports.ApiHubCurateGrpcTransport, "grpc"), + ( + ApiHubCurateAsyncClient, + transports.ApiHubCurateGrpcAsyncIOTransport, + "grpc_asyncio", + ), (ApiHubCurateClient, transports.ApiHubCurateRestTransport, "rest"), ], ) @@ -905,6 +964,18 @@ def test_api_hub_curate_client_client_options_scopes( @pytest.mark.parametrize( "client_class,transport_class,transport_name,grpc_helpers", [ + ( + ApiHubCurateClient, + transports.ApiHubCurateGrpcTransport, + "grpc", + grpc_helpers, + ), + ( + ApiHubCurateAsyncClient, + transports.ApiHubCurateGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), (ApiHubCurateClient, transports.ApiHubCurateRestTransport, "rest", None), ], ) @@ -932,13 +1003,183 @@ def test_api_hub_curate_client_client_options_credentials_file( ) -def test_create_curation_rest_use_cached_wrapped_rpc(): +def test_api_hub_curate_client_client_options_from_dict(): + with mock.patch( + "google.cloud.apihub_v1.services.api_hub_curate.transports.ApiHubCurateGrpcTransport.__init__" + ) as grpc_transport: + grpc_transport.return_value = None + client = ApiHubCurateClient(client_options={"api_endpoint": "squid.clam.whelk"}) + grpc_transport.assert_called_once_with( + credentials=None, + credentials_file=None, + host="squid.clam.whelk", + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name,grpc_helpers", + [ + ( + ApiHubCurateClient, + transports.ApiHubCurateGrpcTransport, + "grpc", + grpc_helpers, + ), + ( + ApiHubCurateAsyncClient, + transports.ApiHubCurateGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), + ], +) +def test_api_hub_curate_client_create_channel_credentials_file( + client_class, transport_class, transport_name, grpc_helpers +): + # Check the case credentials file is provided. + options = client_options.ClientOptions(credentials_file="credentials.json") + + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options, transport=transport_name) + patched.assert_called_once_with( + credentials=None, + credentials_file="credentials.json", + host=client._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE + ), + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + # test that the credentials from file are saved and used as the credentials. + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel" + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + file_creds = ga_credentials.AnonymousCredentials() + load_creds.return_value = (file_creds, None) + adc.return_value = (creds, None) + client = client_class(client_options=options, transport=transport_name) + create_channel.assert_called_with( + "apihub.googleapis.com:443", + credentials=file_creds, + credentials_file=None, + quota_project_id=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + scopes=None, + default_host="apihub.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + +@pytest.mark.parametrize( + "request_type", + [ + curate_service.CreateCurationRequest, + dict, + ], +) +def test_create_curation(request_type, transport: str = "grpc"): + client = ApiHubCurateClient( + credentials=ga_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.create_curation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = curate_service.Curation( + name="name_value", + display_name="display_name_value", + description="description_value", + last_execution_state=curate_service.Curation.LastExecutionState.SUCCEEDED, + last_execution_error_code=curate_service.Curation.ErrorCode.INTERNAL_ERROR, + last_execution_error_message="last_execution_error_message_value", + ) + response = client.create_curation(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = curate_service.CreateCurationRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, curate_service.Curation) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert ( + response.last_execution_state + == curate_service.Curation.LastExecutionState.SUCCEEDED + ) + assert ( + response.last_execution_error_code + == curate_service.Curation.ErrorCode.INTERNAL_ERROR + ) + assert response.last_execution_error_message == "last_execution_error_message_value" + + +def test_create_curation_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = curate_service.CreateCurationRequest( + parent="parent_value", + curation_id="curation_id_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_curation), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.create_curation(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == curate_service.CreateCurationRequest( + parent="parent_value", + curation_id="curation_id_value", + ) + + +def test_create_curation_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -954,7 +1195,6 @@ def test_create_curation_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.create_curation] = mock_rpc - request = {} client.create_curation(request) @@ -968,155 +1208,259 @@ def test_create_curation_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_create_curation_rest_required_fields( - request_type=curate_service.CreateCurationRequest, +@pytest.mark.asyncio +async def test_create_curation_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubCurateRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.create_curation + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.create_curation + ] = mock_rpc + + request = {} + await client.create_curation(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.create_curation(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_create_curation_async( + transport: str = "grpc_asyncio", request_type=curate_service.CreateCurationRequest +): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # verify fields with default values are dropped + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_curation._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_curation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + curate_service.Curation( + name="name_value", + display_name="display_name_value", + description="description_value", + last_execution_state=curate_service.Curation.LastExecutionState.SUCCEEDED, + last_execution_error_code=curate_service.Curation.ErrorCode.INTERNAL_ERROR, + last_execution_error_message="last_execution_error_message_value", + ) + ) + response = await client.create_curation(request) - # verify required fields with default values are now present + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = curate_service.CreateCurationRequest() + assert args[0] == request - jsonified_request["parent"] = "parent_value" + # Establish that the response is the type that we expect. + assert isinstance(response, curate_service.Curation) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert ( + response.last_execution_state + == curate_service.Curation.LastExecutionState.SUCCEEDED + ) + assert ( + response.last_execution_error_code + == curate_service.Curation.ErrorCode.INTERNAL_ERROR + ) + assert response.last_execution_error_message == "last_execution_error_message_value" - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_curation._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("curation_id",)) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" +@pytest.mark.asyncio +async def test_create_curation_async_from_dict(): + await test_create_curation_async(request_type=dict) + +def test_create_curation_field_headers(): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = curate_service.Curation() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # 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 = curate_service.CreateCurationRequest() - response_value = Response() - response_value.status_code = 200 + request.parent = "parent_value" - # Convert return value to protobuf type - return_value = curate_service.Curation.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_curation), "__call__") as call: + call.return_value = curate_service.Curation() + client.create_curation(request) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request - response = client.create_curation(request) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_create_curation_field_headers_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) -def test_create_curation_rest_unset_required_fields(): - transport = transports.ApiHubCurateRestTransport( - credentials=ga_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 = curate_service.CreateCurationRequest() - unset_fields = transport.create_curation._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(("curationId",)) - & set( - ( - "parent", - "curation", - ) + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_curation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + curate_service.Curation() ) - ) + await client.create_curation(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", + "parent=parent_value", + ) in kw["metadata"] -def test_create_curation_rest_flattened(): + +def test_create_curation_flattened(): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = curate_service.Curation() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_curation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = curate_service.Curation() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.create_curation( + parent="parent_value", + curation=curate_service.Curation(name="name_value"), + curation_id="curation_id_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].curation + mock_val = curate_service.Curation(name="name_value") + assert arg == mock_val + arg = args[0].curation_id + mock_val = "curation_id_value" + assert arg == mock_val + + +def test_create_curation_flattened_error(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + ) - # get truthy value for each flattened field - mock_args = dict( + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_curation( + curate_service.CreateCurationRequest(), parent="parent_value", curation=curate_service.Curation(name="name_value"), curation_id="curation_id_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = curate_service.Curation.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_curation(**mock_args) +@pytest.mark.asyncio +async def test_create_curation_flattened_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) - # Establish that the underlying call was made with the expected - # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/curations" % client.transport._host, - args[1], - ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_curation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = curate_service.Curation() + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + curate_service.Curation() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.create_curation( + parent="parent_value", + curation=curate_service.Curation(name="name_value"), + curation_id="curation_id_value", + ) -def test_create_curation_rest_flattened_error(transport: str = "rest"): - client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].curation + mock_val = curate_service.Curation(name="name_value") + assert arg == mock_val + arg = args[0].curation_id + mock_val = "curation_id_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_create_curation_flattened_error_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.create_curation( + await client.create_curation( curate_service.CreateCurationRequest(), parent="parent_value", curation=curate_service.Curation(name="name_value"), @@ -1124,13 +1468,93 @@ def test_create_curation_rest_flattened_error(transport: str = "rest"): ) -def test_get_curation_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + curate_service.GetCurationRequest, + dict, + ], +) +def test_get_curation(request_type, transport: str = "grpc"): + client = ApiHubCurateClient( + credentials=ga_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.get_curation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = curate_service.Curation( + name="name_value", + display_name="display_name_value", + description="description_value", + last_execution_state=curate_service.Curation.LastExecutionState.SUCCEEDED, + last_execution_error_code=curate_service.Curation.ErrorCode.INTERNAL_ERROR, + last_execution_error_message="last_execution_error_message_value", + ) + response = client.get_curation(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = curate_service.GetCurationRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, curate_service.Curation) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert ( + response.last_execution_state + == curate_service.Curation.LastExecutionState.SUCCEEDED + ) + assert ( + response.last_execution_error_code + == curate_service.Curation.ErrorCode.INTERNAL_ERROR + ) + assert response.last_execution_error_message == "last_execution_error_message_value" + + +def test_get_curation_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = curate_service.GetCurationRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_curation), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_curation(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == curate_service.GetCurationRequest( + name="name_value", + ) + + +def test_get_curation_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1146,7 +1570,6 @@ def test_get_curation_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.get_curation] = mock_rpc - request = {} client.get_curation(request) @@ -1160,138 +1583,191 @@ def test_get_curation_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_get_curation_rest_required_fields( - request_type=curate_service.GetCurationRequest, +@pytest.mark.asyncio +async def test_get_curation_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubCurateRestTransport - - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) - - # verify fields with default values are dropped - - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_curation._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - # verify required fields with default values are now present + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - jsonified_request["name"] = "name_value" + # Ensure method has been cached + assert ( + client._client._transport.get_curation + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_curation._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.get_curation + ] = mock_rpc - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + request = {} + await client.get_curation(request) - client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type(**request_init) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - # Designate an appropriate value for the returned response. - return_value = curate_service.Curation() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result + await client.get_curation(request) - response_value = Response() - response_value.status_code = 200 + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - # Convert return value to protobuf type - return_value = curate_service.Curation.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +@pytest.mark.asyncio +async def test_get_curation_async( + transport: str = "grpc_asyncio", request_type=curate_service.GetCurationRequest +): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - response = client.get_curation(request) + # 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() - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_curation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + curate_service.Curation( + name="name_value", + display_name="display_name_value", + description="description_value", + last_execution_state=curate_service.Curation.LastExecutionState.SUCCEEDED, + last_execution_error_code=curate_service.Curation.ErrorCode.INTERNAL_ERROR, + last_execution_error_message="last_execution_error_message_value", + ) + ) + response = await client.get_curation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = curate_service.GetCurationRequest() + assert args[0] == request -def test_get_curation_rest_unset_required_fields(): - transport = transports.ApiHubCurateRestTransport( - credentials=ga_credentials.AnonymousCredentials + # Establish that the response is the type that we expect. + assert isinstance(response, curate_service.Curation) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert ( + response.last_execution_state + == curate_service.Curation.LastExecutionState.SUCCEEDED + ) + assert ( + response.last_execution_error_code + == curate_service.Curation.ErrorCode.INTERNAL_ERROR ) + assert response.last_execution_error_message == "last_execution_error_message_value" - unset_fields = transport.get_curation._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) +@pytest.mark.asyncio +async def test_get_curation_async_from_dict(): + await test_get_curation_async(request_type=dict) -def test_get_curation_rest_flattened(): + +def test_get_curation_field_headers(): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = curate_service.Curation() + # 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 = curate_service.GetCurationRequest() - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/curations/sample3" - } + request.name = "name_value" - # get truthy value for each flattened field - mock_args = dict( - name="name_value", + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_curation), "__call__") as call: + call.return_value = curate_service.Curation() + client.get_curation(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", + "name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_get_curation_field_headers_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = curate_service.GetCurationRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_curation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + curate_service.Curation() ) - mock_args.update(sample_request) + await client.get_curation(request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = curate_service.Curation.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request - client.get_curation(**mock_args) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] + + +def test_get_curation_flattened(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_curation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = curate_service.Curation() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_curation( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/curations/*}" % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_get_curation_rest_flattened_error(transport: str = "rest"): +def test_get_curation_flattened_error(): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened @@ -1303,13 +1779,125 @@ def test_get_curation_rest_flattened_error(transport: str = "rest"): ) -def test_list_curations_rest_use_cached_wrapped_rpc(): +@pytest.mark.asyncio +async def test_get_curation_flattened_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_curation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = curate_service.Curation() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + curate_service.Curation() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_curation( + name="name_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_get_curation_flattened_error_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.get_curation( + curate_service.GetCurationRequest(), + name="name_value", + ) + + +@pytest.mark.parametrize( + "request_type", + [ + curate_service.ListCurationsRequest, + dict, + ], +) +def test_list_curations(request_type, transport: str = "grpc"): + client = ApiHubCurateClient( + credentials=ga_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.list_curations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = curate_service.ListCurationsResponse( + next_page_token="next_page_token_value", + ) + response = client.list_curations(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = curate_service.ListCurationsRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListCurationsPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_curations_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = curate_service.ListCurationsRequest( + parent="parent_value", + filter="filter_value", + page_token="page_token_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_curations), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.list_curations(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == curate_service.ListCurationsRequest( + parent="parent_value", + filter="filter_value", + page_token="page_token_value", + ) + + +def test_list_curations_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1325,7 +1913,6 @@ def test_list_curations_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.list_curations] = mock_rpc - request = {} client.list_curations(request) @@ -1339,176 +1926,240 @@ def test_list_curations_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_list_curations_rest_required_fields( - request_type=curate_service.ListCurationsRequest, +@pytest.mark.asyncio +async def test_list_curations_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubCurateRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.list_curations + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_curations._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.list_curations + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.list_curations(request) - jsonified_request["parent"] = "parent_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_curations._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "filter", - "page_size", - "page_token", - ) + await client.list_curations(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_list_curations_async( + transport: str = "grpc_asyncio", request_type=curate_service.ListCurationsRequest +): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + # 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.list_curations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + curate_service.ListCurationsResponse( + next_page_token="next_page_token_value", + ) + ) + response = await client.list_curations(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = curate_service.ListCurationsRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListCurationsAsyncPager) + assert response.next_page_token == "next_page_token_value" + + +@pytest.mark.asyncio +async def test_list_curations_async_from_dict(): + await test_list_curations_async(request_type=dict) + +def test_list_curations_field_headers(): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = curate_service.ListCurationsResponse() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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 = curate_service.ListCurationsRequest() - # Convert return value to protobuf type - return_value = curate_service.ListCurationsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.parent = "parent_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_curations), "__call__") as call: + call.return_value = curate_service.ListCurationsResponse() + client.list_curations(request) - response = client.list_curations(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] -def test_list_curations_rest_unset_required_fields(): - transport = transports.ApiHubCurateRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_list_curations_field_headers_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.list_curations._get_unset_required_fields({}) - assert set(unset_fields) == ( - set( - ( - "filter", - "pageSize", - "pageToken", - ) + # 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 = curate_service.ListCurationsRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_curations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + curate_service.ListCurationsResponse() ) - & set(("parent",)) - ) + await client.list_curations(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", + "parent=parent_value", + ) in kw["metadata"] -def test_list_curations_rest_flattened(): +def test_list_curations_flattened(): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = curate_service.ListCurationsResponse() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_curations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = curate_service.ListCurationsResponse() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.list_curations( + parent="parent_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_list_curations_flattened_error(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_curations( + curate_service.ListCurationsRequest(), parent="parent_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = curate_service.ListCurationsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_curations(**mock_args) +@pytest.mark.asyncio +async def test_list_curations_flattened_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_curations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = curate_service.ListCurationsResponse() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + curate_service.ListCurationsResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.list_curations( + parent="parent_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/curations" % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val -def test_list_curations_rest_flattened_error(transport: str = "rest"): - client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_list_curations_flattened_error_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.list_curations( + await client.list_curations( curate_service.ListCurationsRequest(), parent="parent_value", ) -def test_list_curations_rest_pager(transport: str = "rest"): +def test_list_curations_pager(transport_name: str = "grpc"): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport=transport_name, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # TODO(kbandes): remove this mock unless there's a good reason for it. - # with mock.patch.object(path_template, 'transcode') as transcode: - # Set the response as a series of pages - response = ( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_curations), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( curate_service.ListCurationsResponse( curations=[ curate_service.Curation(), @@ -1533,40 +2184,249 @@ def test_list_curations_rest_pager(transport: str = "rest"): curate_service.Curation(), ], ), + RuntimeError, ) - # Two responses for two calls - response = response + response - # Wrap the values into proper Response objs - response = tuple( - curate_service.ListCurationsResponse.to_json(x) for x in response + expected_metadata = () + retry = retries.Retry() + timeout = 5 + expected_metadata = tuple(expected_metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", ""),)), ) - return_values = tuple(Response() for i in response) - for return_val, response_val in zip(return_values, response): - return_val._content = response_val.encode("UTF-8") - return_val.status_code = 200 - req.side_effect = return_values - - sample_request = {"parent": "projects/sample1/locations/sample2"} + pager = client.list_curations(request={}, retry=retry, timeout=timeout) - pager = client.list_curations(request=sample_request) + assert pager._metadata == expected_metadata + assert pager._retry == retry + assert pager._timeout == timeout results = list(pager) assert len(results) == 6 assert all(isinstance(i, curate_service.Curation) for i in results) - pages = list(client.list_curations(request=sample_request).pages) + +def test_list_curations_pages(transport_name: str = "grpc"): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport_name, + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_curations), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( + curate_service.ListCurationsResponse( + curations=[ + curate_service.Curation(), + curate_service.Curation(), + curate_service.Curation(), + ], + next_page_token="abc", + ), + curate_service.ListCurationsResponse( + curations=[], + next_page_token="def", + ), + curate_service.ListCurationsResponse( + curations=[ + curate_service.Curation(), + ], + next_page_token="ghi", + ), + curate_service.ListCurationsResponse( + curations=[ + curate_service.Curation(), + curate_service.Curation(), + ], + ), + RuntimeError, + ) + pages = list(client.list_curations(request={}).pages) for page_, token in zip(pages, ["abc", "def", "ghi", ""]): assert page_.raw_page.next_page_token == token -def test_update_curation_rest_use_cached_wrapped_rpc(): +@pytest.mark.asyncio +async def test_list_curations_async_pager(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_curations), "__call__", new_callable=mock.AsyncMock + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + curate_service.ListCurationsResponse( + curations=[ + curate_service.Curation(), + curate_service.Curation(), + curate_service.Curation(), + ], + next_page_token="abc", + ), + curate_service.ListCurationsResponse( + curations=[], + next_page_token="def", + ), + curate_service.ListCurationsResponse( + curations=[ + curate_service.Curation(), + ], + next_page_token="ghi", + ), + curate_service.ListCurationsResponse( + curations=[ + curate_service.Curation(), + curate_service.Curation(), + ], + ), + RuntimeError, + ) + async_pager = await client.list_curations( + request={}, + ) + assert async_pager.next_page_token == "abc" + responses = [] + async for response in async_pager: # pragma: no branch + responses.append(response) + + assert len(responses) == 6 + assert all(isinstance(i, curate_service.Curation) for i in responses) + + +@pytest.mark.asyncio +async def test_list_curations_async_pages(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_curations), "__call__", new_callable=mock.AsyncMock + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + curate_service.ListCurationsResponse( + curations=[ + curate_service.Curation(), + curate_service.Curation(), + curate_service.Curation(), + ], + next_page_token="abc", + ), + curate_service.ListCurationsResponse( + curations=[], + next_page_token="def", + ), + curate_service.ListCurationsResponse( + curations=[ + curate_service.Curation(), + ], + next_page_token="ghi", + ), + curate_service.ListCurationsResponse( + curations=[ + curate_service.Curation(), + curate_service.Curation(), + ], + ), + RuntimeError, + ) + pages = [] + # Workaround issue in python 3.9 related to code coverage by adding `# pragma: no branch` + # See https://github.com/googleapis/gapic-generator-python/pull/1174#issuecomment-1025132372 + async for page_ in ( # pragma: no branch + await client.list_curations(request={}) + ).pages: + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + curate_service.UpdateCurationRequest, + dict, + ], +) +def test_update_curation(request_type, transport: str = "grpc"): + client = ApiHubCurateClient( + credentials=ga_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.update_curation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = curate_service.Curation( + name="name_value", + display_name="display_name_value", + description="description_value", + last_execution_state=curate_service.Curation.LastExecutionState.SUCCEEDED, + last_execution_error_code=curate_service.Curation.ErrorCode.INTERNAL_ERROR, + last_execution_error_message="last_execution_error_message_value", + ) + response = client.update_curation(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = curate_service.UpdateCurationRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, curate_service.Curation) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert ( + response.last_execution_state + == curate_service.Curation.LastExecutionState.SUCCEEDED + ) + assert ( + response.last_execution_error_code + == curate_service.Curation.ErrorCode.INTERNAL_ERROR + ) + assert response.last_execution_error_message == "last_execution_error_message_value" + + +def test_update_curation_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = curate_service.UpdateCurationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_curation), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.update_curation(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == curate_service.UpdateCurationRequest() + + +def test_update_curation_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1582,7 +2442,6 @@ def test_update_curation_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.update_curation] = mock_rpc - request = {} client.update_curation(request) @@ -1596,138 +2455,195 @@ def test_update_curation_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_update_curation_rest_required_fields( - request_type=curate_service.UpdateCurationRequest, +@pytest.mark.asyncio +async def test_update_curation_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubCurateRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.update_curation + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_curation._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.update_curation + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.update_curation(request) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_curation._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("update_mask",)) - jsonified_request.update(unset_fields) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - # verify required fields with non-default values are left alone + await client.update_curation(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_update_curation_async( + transport: str = "grpc_asyncio", request_type=curate_service.UpdateCurationRequest +): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + 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.update_curation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + curate_service.Curation( + name="name_value", + display_name="display_name_value", + description="description_value", + last_execution_state=curate_service.Curation.LastExecutionState.SUCCEEDED, + last_execution_error_code=curate_service.Curation.ErrorCode.INTERNAL_ERROR, + last_execution_error_message="last_execution_error_message_value", + ) + ) + response = await client.update_curation(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = curate_service.UpdateCurationRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, curate_service.Curation) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert ( + response.last_execution_state + == curate_service.Curation.LastExecutionState.SUCCEEDED + ) + assert ( + response.last_execution_error_code + == curate_service.Curation.ErrorCode.INTERNAL_ERROR + ) + assert response.last_execution_error_message == "last_execution_error_message_value" + + +@pytest.mark.asyncio +async def test_update_curation_async_from_dict(): + await test_update_curation_async(request_type=dict) + +def test_update_curation_field_headers(): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = curate_service.Curation() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "patch", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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 = curate_service.UpdateCurationRequest() - # Convert return value to protobuf type - return_value = curate_service.Curation.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.curation.name = "name_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_curation), "__call__") as call: + call.return_value = curate_service.Curation() + client.update_curation(request) - response = client.update_curation(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "curation.name=name_value", + ) in kw["metadata"] -def test_update_curation_rest_unset_required_fields(): - transport = transports.ApiHubCurateRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_update_curation_field_headers_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.update_curation._get_unset_required_fields({}) - assert set(unset_fields) == (set(("updateMask",)) & set(("curation",))) + # 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 = curate_service.UpdateCurationRequest() + request.curation.name = "name_value" -def test_update_curation_rest_flattened(): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_curation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + curate_service.Curation() + ) + await client.update_curation(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", + "curation.name=name_value", + ) in kw["metadata"] + + +def test_update_curation_flattened(): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = curate_service.Curation() - - # get arguments that satisfy an http rule for this method - sample_request = { - "curation": {"name": "projects/sample1/locations/sample2/curations/sample3"} - } - - # get truthy value for each flattened field - mock_args = dict( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_curation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = curate_service.Curation() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.update_curation( curation=curate_service.Curation(name="name_value"), update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = curate_service.Curation.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - - client.update_curation(**mock_args) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{curation.name=projects/*/locations/*/curations/*}" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].curation + mock_val = curate_service.Curation(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val -def test_update_curation_rest_flattened_error(transport: str = "rest"): +def test_update_curation_flattened_error(): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened @@ -1740,13 +2656,123 @@ def test_update_curation_rest_flattened_error(transport: str = "rest"): ) -def test_delete_curation_rest_use_cached_wrapped_rpc(): +@pytest.mark.asyncio +async def test_update_curation_flattened_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_curation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = curate_service.Curation() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + curate_service.Curation() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.update_curation( + curation=curate_service.Curation(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].curation + mock_val = curate_service.Curation(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_update_curation_flattened_error_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.update_curation( + curate_service.UpdateCurationRequest(), + curation=curate_service.Curation(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +@pytest.mark.parametrize( + "request_type", + [ + curate_service.DeleteCurationRequest, + dict, + ], +) +def test_delete_curation(request_type, transport: str = "grpc"): + client = ApiHubCurateClient( + credentials=ga_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.delete_curation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.delete_curation(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = curate_service.DeleteCurationRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert response is None + + +def test_delete_curation_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = curate_service.DeleteCurationRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_curation), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.delete_curation(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == curate_service.DeleteCurationRequest( + name="name_value", + ) + + +def test_delete_curation_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1762,7 +2788,6 @@ def test_delete_curation_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.delete_curation] = mock_rpc - request = {} client.delete_curation(request) @@ -1776,47 +2801,300 @@ def test_delete_curation_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_delete_curation_rest_required_fields( - request_type=curate_service.DeleteCurationRequest, +@pytest.mark.asyncio +async def test_delete_curation_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubCurateRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.delete_curation + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_curation._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.delete_curation + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.delete_curation(request) - jsonified_request["name"] = "name_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_curation._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + await client.delete_curation(request) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + +@pytest.mark.asyncio +async def test_delete_curation_async( + transport: str = "grpc_asyncio", request_type=curate_service.DeleteCurationRequest +): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = None + # 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.delete_curation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_curation(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = curate_service.DeleteCurationRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert response is None + + +@pytest.mark.asyncio +async def test_delete_curation_async_from_dict(): + await test_delete_curation_async(request_type=dict) + + +def test_delete_curation_field_headers(): + client = ApiHubCurateClient( + credentials=ga_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 = curate_service.DeleteCurationRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_curation), "__call__") as call: + call.return_value = None + client.delete_curation(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", + "name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_delete_curation_field_headers_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = curate_service.DeleteCurationRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_curation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_curation(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", + "name=name_value", + ) in kw["metadata"] + + +def test_delete_curation_flattened(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_curation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.delete_curation( + name="name_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +def test_delete_curation_flattened_error(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_curation( + curate_service.DeleteCurationRequest(), + name="name_value", + ) + + +@pytest.mark.asyncio +async def test_delete_curation_flattened_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_curation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.delete_curation( + name="name_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_delete_curation_flattened_error_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.delete_curation( + curate_service.DeleteCurationRequest(), + name="name_value", + ) + + +def test_create_curation_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.create_curation in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.create_curation] = mock_rpc + + request = {} + client.create_curation(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.create_curation(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_create_curation_rest_required_fields( + request_type=curate_service.CreateCurationRequest, +): + transport_class = transports.ApiHubCurateRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_curation._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_curation._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("curation_id",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = curate_service.Curation() # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -1828,36 +3106,48 @@ def test_delete_curation_rest_required_fields( pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "delete", + "method": "post", "query_params": pb_request, } + transcode_result["body"] = pb_request transcode.return_value = transcode_result response_value = Response() response_value.status_code = 200 - json_return_value = "" + + # Convert return value to protobuf type + return_value = curate_service.Curation.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.delete_curation(request) + response = client.create_curation(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_delete_curation_rest_unset_required_fields(): +def test_create_curation_rest_unset_required_fields(): transport = transports.ApiHubCurateRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.delete_curation._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + unset_fields = transport.create_curation._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("curationId",)) + & set( + ( + "parent", + "curation", + ) + ) + ) -def test_delete_curation_rest_flattened(): +def test_create_curation_rest_flattened(): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -1866,40 +3156,42 @@ def test_delete_curation_rest_flattened(): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = None + return_value = curate_service.Curation() # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/curations/sample3" - } + sample_request = {"parent": "projects/sample1/locations/sample2"} # get truthy value for each flattened field mock_args = dict( - name="name_value", + parent="parent_value", + curation=curate_service.Curation(name="name_value"), + curation_id="curation_id_value", ) mock_args.update(sample_request) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - json_return_value = "" + # Convert return value to protobuf type + return_value = curate_service.Curation.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_curation(**mock_args) + client.create_curation(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/curations/*}" % client.transport._host, + "%s/v1/{parent=projects/*/locations/*}/curations" % client.transport._host, args[1], ) -def test_delete_curation_rest_flattened_error(transport: str = "rest"): +def test_create_curation_rest_flattened_error(transport: str = "rest"): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -1908,1775 +3200,3995 @@ def test_delete_curation_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.delete_curation( - curate_service.DeleteCurationRequest(), - name="name_value", + client.create_curation( + curate_service.CreateCurationRequest(), + parent="parent_value", + curation=curate_service.Curation(name="name_value"), + curation_id="curation_id_value", ) -def test_credentials_transport_error(): - # It is an error to provide credentials and a transport instance. - transport = transports.ApiHubCurateRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - with pytest.raises(ValueError): +def test_get_curation_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport="rest", ) - # It is an error to provide a credentials file and a transport instance. - transport = transports.ApiHubCurateRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - with pytest.raises(ValueError): - client = ApiHubCurateClient( - client_options={"credentials_file": "credentials.json"}, - transport=transport, + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.get_curation in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. ) + client._transport._wrapped_methods[client._transport.get_curation] = mock_rpc - # It is an error to provide an api_key and a transport instance. - transport = transports.ApiHubCurateRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = ApiHubCurateClient( - client_options=options, - transport=transport, - ) + request = {} + client.get_curation(request) - # It is an error to provide an api_key and a credential. - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = ApiHubCurateClient( - client_options=options, credentials=ga_credentials.AnonymousCredentials() - ) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - # It is an error to provide scopes and a transport instance. - transport = transports.ApiHubCurateRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - with pytest.raises(ValueError): - client = ApiHubCurateClient( - client_options={"scopes": ["1", "2"]}, - transport=transport, - ) + client.get_curation(request) + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 -def test_transport_instance(): - # A client may be instantiated with a custom transport instance. - transport = transports.ApiHubCurateRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - client = ApiHubCurateClient(transport=transport) - assert client.transport is transport +def test_get_curation_rest_required_fields( + request_type=curate_service.GetCurationRequest, +): + transport_class = transports.ApiHubCurateRestTransport -@pytest.mark.parametrize( - "transport_class", - [ - transports.ApiHubCurateRestTransport, - ], -) -def test_transport_adc(transport_class): - # Test default credentials are used if not provided. - with mock.patch.object(google.auth, "default") as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport_class() - adc.assert_called_once() + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + # verify fields with default values are dropped -def test_transport_kind_rest(): - transport = ApiHubCurateClient.get_transport_class("rest")( + unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ) - assert transport.kind == "rest" + ).get_curation._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + # verify required fields with default values are now present -def test_create_curation_rest_bad_request( - request_type=curate_service.CreateCurationRequest, -): - client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request = request_type(**request_init) + jsonified_request["name"] = "name_value" - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_curation(request) + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_curation._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" -@pytest.mark.parametrize( - "request_type", - [ - curate_service.CreateCurationRequest, - dict, - ], -) -def test_create_curation_rest_call_success(request_type): client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) + request = request_type(**request_init) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request_init["curation"] = { - "name": "name_value", - "display_name": "display_name_value", - "description": "description_value", - "endpoint": { - "application_integration_endpoint_details": { - "uri": "uri_value", - "trigger_id": "trigger_id_value", + # Designate an appropriate value for the returned response. + return_value = curate_service.Curation() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, } - }, - "plugin_instance_actions": [ - {"plugin_instance": "plugin_instance_value", "action_id": "action_id_value"} - ], - "last_execution_state": 1, - "last_execution_error_code": 1, - "last_execution_error_message": "last_execution_error_message_value", - "create_time": {"seconds": 751, "nanos": 543}, - "update_time": {}, - } - # The version of a generated dependency at test runtime may differ from the version used during generation. - # Delete any fields which are not present in the current runtime dependency - # See https://github.com/googleapis/gapic-generator-python/issues/1748 + transcode.return_value = transcode_result - # Determine if the message type is proto-plus or protobuf - test_field = curate_service.CreateCurationRequest.meta.fields["curation"] + response_value = Response() + response_value.status_code = 200 - def get_message_fields(field): - # Given a field which is a message (composite type), return a list with - # all the fields of the message. - # If the field is not a composite type, return an empty list. - message_fields = [] + # Convert return value to protobuf type + return_value = curate_service.Curation.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields + response = client.get_curation(request) - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params - subfields_not_in_runtime = [] - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["curation"].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value +def test_get_curation_rest_unset_required_fields(): + transport = transports.ApiHubCurateRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) + unset_fields = transport.get_curation._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range(0, len(request_init["curation"][field])): - del request_init["curation"][field][i][subfield] - else: - del request_init["curation"][field][subfield] - request = request_type(**request_init) + +def test_get_curation_rest_flattened(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = curate_service.Curation( + return_value = curate_service.Curation() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/curations/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( name="name_value", - display_name="display_name_value", - description="description_value", - last_execution_state=curate_service.Curation.LastExecutionState.SUCCEEDED, - last_execution_error_code=curate_service.Curation.ErrorCode.INTERNAL_ERROR, - last_execution_error_message="last_execution_error_message_value", ) + mock_args.update(sample_request) # Wrap the value into a proper Response obj - response_value = mock.Mock() + response_value = Response() response_value.status_code = 200 - # Convert return value to protobuf type return_value = curate_service.Curation.pb(return_value) json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.create_curation(request) - # Establish that the response is the type that we expect. - assert isinstance(response, curate_service.Curation) - assert response.name == "name_value" - assert response.display_name == "display_name_value" - assert response.description == "description_value" - assert ( - response.last_execution_state - == curate_service.Curation.LastExecutionState.SUCCEEDED - ) - assert ( - response.last_execution_error_code - == curate_service.Curation.ErrorCode.INTERNAL_ERROR - ) - assert response.last_execution_error_message == "last_execution_error_message_value" + client.get_curation(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/curations/*}" % client.transport._host, + args[1], + ) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_create_curation_rest_interceptors(null_interceptor): - transport = transports.ApiHubCurateRestTransport( +def test_get_curation_rest_flattened_error(transport: str = "rest"): + client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubCurateRestInterceptor(), + transport=transport, ) - client = ApiHubCurateClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubCurateRestInterceptor, "post_create_curation" - ) as post, mock.patch.object( - transports.ApiHubCurateRestInterceptor, "post_create_curation_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubCurateRestInterceptor, "pre_create_curation" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = curate_service.CreateCurationRequest.pb( - curate_service.CreateCurationRequest() + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_curation( + curate_service.GetCurationRequest(), + name="name_value", ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = curate_service.Curation.to_json(curate_service.Curation()) - req.return_value.content = return_value - request = curate_service.CreateCurationRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = curate_service.Curation() - post_with_metadata.return_value = curate_service.Curation(), metadata +def test_list_curations_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) - client.create_curation( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.list_curations in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. ) + client._transport._wrapped_methods[client._transport.list_curations] = mock_rpc - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + request = {} + client.list_curations(request) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 -def test_get_curation_rest_bad_request(request_type=curate_service.GetCurationRequest): - client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/curations/sample3"} - request = request_type(**request_init) + client.list_curations(request) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_curation(request) + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 -@pytest.mark.parametrize( - "request_type", - [ - curate_service.GetCurationRequest, - dict, - ], -) -def test_get_curation_rest_call_success(request_type): - client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) +def test_list_curations_rest_required_fields( + request_type=curate_service.ListCurationsRequest, +): + transport_class = transports.ApiHubCurateRestTransport - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/curations/sample3"} + request_init = {} + request_init["parent"] = "" request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = curate_service.Curation( - name="name_value", - display_name="display_name_value", - description="description_value", - last_execution_state=curate_service.Curation.LastExecutionState.SUCCEEDED, - last_execution_error_code=curate_service.Curation.ErrorCode.INTERNAL_ERROR, - last_execution_error_message="last_execution_error_message_value", - ) + # verify fields with default values are dropped - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_curations._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) - # Convert return value to protobuf type - return_value = curate_service.Curation.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_curation(request) + # verify required fields with default values are now present - # Establish that the response is the type that we expect. - assert isinstance(response, curate_service.Curation) - assert response.name == "name_value" - assert response.display_name == "display_name_value" - assert response.description == "description_value" - assert ( - response.last_execution_state - == curate_service.Curation.LastExecutionState.SUCCEEDED - ) - assert ( - response.last_execution_error_code - == curate_service.Curation.ErrorCode.INTERNAL_ERROR + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_curations._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "page_size", + "page_token", + ) ) - assert response.last_execution_error_message == "last_execution_error_message_value" + jsonified_request.update(unset_fields) + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_curation_rest_interceptors(null_interceptor): - transport = transports.ApiHubCurateRestTransport( + client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubCurateRestInterceptor(), + transport="rest", ) - client = ApiHubCurateClient(transport=transport) - - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubCurateRestInterceptor, "post_get_curation" - ) as post, mock.patch.object( - transports.ApiHubCurateRestInterceptor, "post_get_curation_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubCurateRestInterceptor, "pre_get_curation" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = curate_service.GetCurationRequest.pb( - curate_service.GetCurationRequest() - ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = curate_service.Curation.to_json(curate_service.Curation()) - req.return_value.content = return_value + request = request_type(**request_init) - request = curate_service.GetCurationRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = curate_service.Curation() - post_with_metadata.return_value = curate_service.Curation(), metadata + # Designate an appropriate value for the returned response. + return_value = curate_service.ListCurationsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result - client.get_curation( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], - ) + response_value = Response() + response_value.status_code = 200 - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # Convert return value to protobuf type + return_value = curate_service.ListCurationsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} -def test_list_curations_rest_bad_request( - request_type=curate_service.ListCurationsRequest, -): - client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + response = client.list_curations(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_curations_rest_unset_required_fields(): + transport = transports.ApiHubCurateRestTransport( + credentials=ga_credentials.AnonymousCredentials ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_curations(request) + unset_fields = transport.list_curations._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) -@pytest.mark.parametrize( - "request_type", - [ - curate_service.ListCurationsRequest, - dict, - ], -) -def test_list_curations_rest_call_success(request_type): +def test_list_curations_rest_flattened(): client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = curate_service.ListCurationsResponse( - next_page_token="next_page_token_value", + return_value = curate_service.ListCurationsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", ) + mock_args.update(sample_request) # Wrap the value into a proper Response obj - response_value = mock.Mock() + response_value = Response() response_value.status_code = 200 - # Convert return value to protobuf type return_value = curate_service.ListCurationsResponse.pb(return_value) json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.list_curations(request) - # Establish that the response is the type that we expect. - assert isinstance(response, pagers.ListCurationsPager) - assert response.next_page_token == "next_page_token_value" + client.list_curations(**mock_args) + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/curations" % client.transport._host, + args[1], + ) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_list_curations_rest_interceptors(null_interceptor): - transport = transports.ApiHubCurateRestTransport( + +def test_list_curations_rest_flattened_error(transport: str = "rest"): + client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubCurateRestInterceptor(), + transport=transport, ) - client = ApiHubCurateClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubCurateRestInterceptor, "post_list_curations" - ) as post, mock.patch.object( - transports.ApiHubCurateRestInterceptor, "post_list_curations_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubCurateRestInterceptor, "pre_list_curations" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = curate_service.ListCurationsRequest.pb( - curate_service.ListCurationsRequest() + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_curations( + curate_service.ListCurationsRequest(), + parent="parent_value", ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = curate_service.ListCurationsResponse.to_json( - curate_service.ListCurationsResponse() - ) - req.return_value.content = return_value - request = curate_service.ListCurationsRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = curate_service.ListCurationsResponse() - post_with_metadata.return_value = ( - curate_service.ListCurationsResponse(), - metadata, +def test_list_curations_rest_pager(transport: str = "rest"): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + curate_service.ListCurationsResponse( + curations=[ + curate_service.Curation(), + curate_service.Curation(), + curate_service.Curation(), + ], + next_page_token="abc", + ), + curate_service.ListCurationsResponse( + curations=[], + next_page_token="def", + ), + curate_service.ListCurationsResponse( + curations=[ + curate_service.Curation(), + ], + next_page_token="ghi", + ), + curate_service.ListCurationsResponse( + curations=[ + curate_service.Curation(), + curate_service.Curation(), + ], + ), ) + # Two responses for two calls + response = response + response - client.list_curations( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + # Wrap the values into proper Response objs + response = tuple( + curate_service.ListCurationsResponse.to_json(x) for x in response ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + sample_request = {"parent": "projects/sample1/locations/sample2"} + pager = client.list_curations(request=sample_request) -def test_update_curation_rest_bad_request( - request_type=curate_service.UpdateCurationRequest, -): - client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = { - "curation": {"name": "projects/sample1/locations/sample2/curations/sample3"} - } - request = request_type(**request_init) + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, curate_service.Curation) for i in results) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + pages = list(client.list_curations(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +def test_update_curation_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.update_curation in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.update_curation] = mock_rpc + + request = {} client.update_curation(request) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.update_curation(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_update_curation_rest_required_fields( + request_type=curate_service.UpdateCurationRequest, +): + transport_class = transports.ApiHubCurateRestTransport + + request_init = {} + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_curation._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_curation._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("update_mask",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone -@pytest.mark.parametrize( - "request_type", - [ - curate_service.UpdateCurationRequest, - dict, - ], -) -def test_update_curation_rest_call_success(request_type): client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) + request = request_type(**request_init) - # send a request that will satisfy transcoding - request_init = { - "curation": {"name": "projects/sample1/locations/sample2/curations/sample3"} - } - request_init["curation"] = { - "name": "projects/sample1/locations/sample2/curations/sample3", - "display_name": "display_name_value", - "description": "description_value", - "endpoint": { - "application_integration_endpoint_details": { - "uri": "uri_value", - "trigger_id": "trigger_id_value", + # Designate an appropriate value for the returned response. + return_value = curate_service.Curation() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, } - }, - "plugin_instance_actions": [ - {"plugin_instance": "plugin_instance_value", "action_id": "action_id_value"} - ], - "last_execution_state": 1, - "last_execution_error_code": 1, - "last_execution_error_message": "last_execution_error_message_value", - "create_time": {"seconds": 751, "nanos": 543}, - "update_time": {}, - } - # The version of a generated dependency at test runtime may differ from the version used during generation. - # Delete any fields which are not present in the current runtime dependency - # See https://github.com/googleapis/gapic-generator-python/issues/1748 + transcode_result["body"] = pb_request + transcode.return_value = transcode_result - # Determine if the message type is proto-plus or protobuf - test_field = curate_service.UpdateCurationRequest.meta.fields["curation"] + response_value = Response() + response_value.status_code = 200 - def get_message_fields(field): - # Given a field which is a message (composite type), return a list with - # all the fields of the message. - # If the field is not a composite type, return an empty list. - message_fields = [] + # Convert return value to protobuf type + return_value = curate_service.Curation.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields + response = client.update_curation(request) - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params - subfields_not_in_runtime = [] - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["curation"].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value +def test_update_curation_rest_unset_required_fields(): + transport = transports.ApiHubCurateRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) + unset_fields = transport.update_curation._get_unset_required_fields({}) + assert set(unset_fields) == (set(("updateMask",)) & set(("curation",))) - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range(0, len(request_init["curation"][field])): - del request_init["curation"][field][i][subfield] - else: - del request_init["curation"][field][subfield] - request = request_type(**request_init) + +def test_update_curation_rest_flattened(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = curate_service.Curation( - name="name_value", - display_name="display_name_value", - description="description_value", - last_execution_state=curate_service.Curation.LastExecutionState.SUCCEEDED, - last_execution_error_code=curate_service.Curation.ErrorCode.INTERNAL_ERROR, - last_execution_error_message="last_execution_error_message_value", + return_value = curate_service.Curation() + + # get arguments that satisfy an http rule for this method + sample_request = { + "curation": {"name": "projects/sample1/locations/sample2/curations/sample3"} + } + + # get truthy value for each flattened field + mock_args = dict( + curation=curate_service.Curation(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) + mock_args.update(sample_request) # Wrap the value into a proper Response obj - response_value = mock.Mock() + response_value = Response() response_value.status_code = 200 - # Convert return value to protobuf type return_value = curate_service.Curation.pb(return_value) json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.update_curation(request) - - # Establish that the response is the type that we expect. - assert isinstance(response, curate_service.Curation) - assert response.name == "name_value" - assert response.display_name == "display_name_value" - assert response.description == "description_value" - assert ( - response.last_execution_state - == curate_service.Curation.LastExecutionState.SUCCEEDED - ) - assert ( - response.last_execution_error_code - == curate_service.Curation.ErrorCode.INTERNAL_ERROR - ) - assert response.last_execution_error_message == "last_execution_error_message_value" + client.update_curation(**mock_args) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_update_curation_rest_interceptors(null_interceptor): - transport = transports.ApiHubCurateRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubCurateRestInterceptor(), + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{curation.name=projects/*/locations/*/curations/*}" + % client.transport._host, + args[1], + ) + + +def test_update_curation_rest_flattened_error(transport: str = "rest"): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - client = ApiHubCurateClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubCurateRestInterceptor, "post_update_curation" - ) as post, mock.patch.object( - transports.ApiHubCurateRestInterceptor, "post_update_curation_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubCurateRestInterceptor, "pre_update_curation" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = curate_service.UpdateCurationRequest.pb( - curate_service.UpdateCurationRequest() + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_curation( + curate_service.UpdateCurationRequest(), + curation=curate_service.Curation(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = curate_service.Curation.to_json(curate_service.Curation()) - req.return_value.content = return_value - request = curate_service.UpdateCurationRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = curate_service.Curation() - post_with_metadata.return_value = curate_service.Curation(), metadata +def test_delete_curation_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) - client.update_curation( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.delete_curation in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. ) + client._transport._wrapped_methods[client._transport.delete_curation] = mock_rpc - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + request = {} + client.delete_curation(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + client.delete_curation(request) -def test_delete_curation_rest_bad_request( + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_delete_curation_rest_required_fields( request_type=curate_service.DeleteCurationRequest, ): + transport_class = transports.ApiHubCurateRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_curation._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_curation._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/curations/sample3"} request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): + # Designate an appropriate value for the returned response. + return_value = None + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_curation(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_curation_rest_unset_required_fields(): + transport = transports.ApiHubCurateRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_curation._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_delete_curation_rest_flattened(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/curations/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + # Wrap the value into a proper Response obj - response_value = mock.Mock() + response_value = Response() + response_value.status_code = 200 json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() + response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_curation(request) + client.delete_curation(**mock_args) -@pytest.mark.parametrize( - "request_type", - [ - curate_service.DeleteCurationRequest, - dict, - ], -) -def test_delete_curation_rest_call_success(request_type): + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/curations/*}" % client.transport._host, + args[1], + ) + + +def test_delete_curation_rest_flattened_error(transport: str = "rest"): client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_curation( + curate_service.DeleteCurationRequest(), + name="name_value", + ) + + +def test_credentials_transport_error(): + # It is an error to provide credentials and a transport instance. + transport = transports.ApiHubCurateGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # It is an error to provide a credentials file and a transport instance. + transport = transports.ApiHubCurateGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = ApiHubCurateClient( + client_options={"credentials_file": "credentials.json"}, + transport=transport, + ) + + # It is an error to provide an api_key and a transport instance. + transport = transports.ApiHubCurateGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = ApiHubCurateClient( + client_options=options, + transport=transport, + ) + + # It is an error to provide an api_key and a credential. + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = ApiHubCurateClient( + client_options=options, credentials=ga_credentials.AnonymousCredentials() + ) + + # It is an error to provide scopes and a transport instance. + transport = transports.ApiHubCurateGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = ApiHubCurateClient( + client_options={"scopes": ["1", "2"]}, + transport=transport, + ) + + +def test_transport_instance(): + # A client may be instantiated with a custom transport instance. + transport = transports.ApiHubCurateGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + client = ApiHubCurateClient(transport=transport) + assert client.transport is transport + + +def test_transport_get_channel(): + # A client may be instantiated with a custom transport instance. + transport = transports.ApiHubCurateGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + transport = transports.ApiHubCurateGrpcAsyncIOTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubCurateGrpcTransport, + transports.ApiHubCurateGrpcAsyncIOTransport, + transports.ApiHubCurateRestTransport, + ], +) +def test_transport_adc(transport_class): + # Test default credentials are used if not provided. + with mock.patch.object(google.auth, "default") as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class() + adc.assert_called_once() + + +def test_transport_kind_grpc(): + transport = ApiHubCurateClient.get_transport_class("grpc")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "grpc" + + +def test_initialize_client_w_grpc(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_curation_empty_call_grpc(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.create_curation), "__call__") as call: + call.return_value = curate_service.Curation() + client.create_curation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = curate_service.CreateCurationRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_curation_empty_call_grpc(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_curation), "__call__") as call: + call.return_value = curate_service.Curation() + client.get_curation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = curate_service.GetCurationRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_curations_empty_call_grpc(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.list_curations), "__call__") as call: + call.return_value = curate_service.ListCurationsResponse() + client.list_curations(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = curate_service.ListCurationsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_update_curation_empty_call_grpc(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.update_curation), "__call__") as call: + call.return_value = curate_service.Curation() + client.update_curation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = curate_service.UpdateCurationRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_curation_empty_call_grpc(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.delete_curation), "__call__") as call: + call.return_value = None + client.delete_curation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = curate_service.DeleteCurationRequest() + + assert args[0] == request_msg + + +def test_transport_kind_grpc_asyncio(): + transport = ApiHubCurateAsyncClient.get_transport_class("grpc_asyncio")( + credentials=async_anonymous_credentials() + ) + assert transport.kind == "grpc_asyncio" + + +def test_initialize_client_w_grpc_asyncio(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_create_curation_empty_call_grpc_asyncio(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.create_curation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + curate_service.Curation( + name="name_value", + display_name="display_name_value", + description="description_value", + last_execution_state=curate_service.Curation.LastExecutionState.SUCCEEDED, + last_execution_error_code=curate_service.Curation.ErrorCode.INTERNAL_ERROR, + last_execution_error_message="last_execution_error_message_value", + ) + ) + await client.create_curation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = curate_service.CreateCurationRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_curation_empty_call_grpc_asyncio(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_curation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + curate_service.Curation( + name="name_value", + display_name="display_name_value", + description="description_value", + last_execution_state=curate_service.Curation.LastExecutionState.SUCCEEDED, + last_execution_error_code=curate_service.Curation.ErrorCode.INTERNAL_ERROR, + last_execution_error_message="last_execution_error_message_value", + ) + ) + await client.get_curation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = curate_service.GetCurationRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_list_curations_empty_call_grpc_asyncio(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.list_curations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + curate_service.ListCurationsResponse( + next_page_token="next_page_token_value", + ) + ) + await client.list_curations(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = curate_service.ListCurationsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_update_curation_empty_call_grpc_asyncio(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.update_curation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + curate_service.Curation( + name="name_value", + display_name="display_name_value", + description="description_value", + last_execution_state=curate_service.Curation.LastExecutionState.SUCCEEDED, + last_execution_error_code=curate_service.Curation.ErrorCode.INTERNAL_ERROR, + last_execution_error_message="last_execution_error_message_value", + ) + ) + await client.update_curation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = curate_service.UpdateCurationRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_delete_curation_empty_call_grpc_asyncio(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.delete_curation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_curation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = curate_service.DeleteCurationRequest() + + assert args[0] == request_msg + + +def test_transport_kind_rest(): + transport = ApiHubCurateClient.get_transport_class("rest")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "rest" + + +def test_create_curation_rest_bad_request( + request_type=curate_service.CreateCurationRequest, +): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.create_curation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + curate_service.CreateCurationRequest, + dict, + ], +) +def test_create_curation_rest_call_success(request_type): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["curation"] = { + "name": "name_value", + "display_name": "display_name_value", + "description": "description_value", + "endpoint": { + "application_integration_endpoint_details": { + "uri": "uri_value", + "trigger_id": "trigger_id_value", + } + }, + "plugin_instance_actions": [ + {"plugin_instance": "plugin_instance_value", "action_id": "action_id_value"} + ], + "last_execution_state": 1, + "last_execution_error_code": 1, + "last_execution_error_message": "last_execution_error_message_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = curate_service.CreateCurationRequest.meta.fields["curation"] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["curation"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["curation"][field])): + del request_init["curation"][field][i][subfield] + else: + del request_init["curation"][field][subfield] + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = curate_service.Curation( + name="name_value", + display_name="display_name_value", + description="description_value", + last_execution_state=curate_service.Curation.LastExecutionState.SUCCEEDED, + last_execution_error_code=curate_service.Curation.ErrorCode.INTERNAL_ERROR, + last_execution_error_message="last_execution_error_message_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = curate_service.Curation.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.create_curation(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, curate_service.Curation) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert ( + response.last_execution_state + == curate_service.Curation.LastExecutionState.SUCCEEDED + ) + assert ( + response.last_execution_error_code + == curate_service.Curation.ErrorCode.INTERNAL_ERROR + ) + assert response.last_execution_error_message == "last_execution_error_message_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_curation_rest_interceptors(null_interceptor): + transport = transports.ApiHubCurateRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubCurateRestInterceptor(), + ) + client = ApiHubCurateClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubCurateRestInterceptor, "post_create_curation" + ) as post, mock.patch.object( + transports.ApiHubCurateRestInterceptor, "post_create_curation_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubCurateRestInterceptor, "pre_create_curation" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = curate_service.CreateCurationRequest.pb( + curate_service.CreateCurationRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = curate_service.Curation.to_json(curate_service.Curation()) + req.return_value.content = return_value + + request = curate_service.CreateCurationRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = curate_service.Curation() + post_with_metadata.return_value = curate_service.Curation(), metadata + + client.create_curation( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_get_curation_rest_bad_request(request_type=curate_service.GetCurationRequest): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/curations/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_curation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + curate_service.GetCurationRequest, + dict, + ], +) +def test_get_curation_rest_call_success(request_type): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/curations/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = curate_service.Curation( + name="name_value", + display_name="display_name_value", + description="description_value", + last_execution_state=curate_service.Curation.LastExecutionState.SUCCEEDED, + last_execution_error_code=curate_service.Curation.ErrorCode.INTERNAL_ERROR, + last_execution_error_message="last_execution_error_message_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = curate_service.Curation.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.get_curation(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, curate_service.Curation) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert ( + response.last_execution_state + == curate_service.Curation.LastExecutionState.SUCCEEDED + ) + assert ( + response.last_execution_error_code + == curate_service.Curation.ErrorCode.INTERNAL_ERROR + ) + assert response.last_execution_error_message == "last_execution_error_message_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_curation_rest_interceptors(null_interceptor): + transport = transports.ApiHubCurateRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubCurateRestInterceptor(), + ) + client = ApiHubCurateClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubCurateRestInterceptor, "post_get_curation" + ) as post, mock.patch.object( + transports.ApiHubCurateRestInterceptor, "post_get_curation_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubCurateRestInterceptor, "pre_get_curation" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = curate_service.GetCurationRequest.pb( + curate_service.GetCurationRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = curate_service.Curation.to_json(curate_service.Curation()) + req.return_value.content = return_value + + request = curate_service.GetCurationRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = curate_service.Curation() + post_with_metadata.return_value = curate_service.Curation(), metadata + + client.get_curation( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_list_curations_rest_bad_request( + request_type=curate_service.ListCurationsRequest, +): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_curations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + curate_service.ListCurationsRequest, + dict, + ], +) +def test_list_curations_rest_call_success(request_type): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = curate_service.ListCurationsResponse( + next_page_token="next_page_token_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = curate_service.ListCurationsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.list_curations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListCurationsPager) + assert response.next_page_token == "next_page_token_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_curations_rest_interceptors(null_interceptor): + transport = transports.ApiHubCurateRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubCurateRestInterceptor(), + ) + client = ApiHubCurateClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubCurateRestInterceptor, "post_list_curations" + ) as post, mock.patch.object( + transports.ApiHubCurateRestInterceptor, "post_list_curations_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubCurateRestInterceptor, "pre_list_curations" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = curate_service.ListCurationsRequest.pb( + curate_service.ListCurationsRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = curate_service.ListCurationsResponse.to_json( + curate_service.ListCurationsResponse() + ) + req.return_value.content = return_value + + request = curate_service.ListCurationsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = curate_service.ListCurationsResponse() + post_with_metadata.return_value = ( + curate_service.ListCurationsResponse(), + metadata, + ) + + client.list_curations( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_update_curation_rest_bad_request( + request_type=curate_service.UpdateCurationRequest, +): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "curation": {"name": "projects/sample1/locations/sample2/curations/sample3"} + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.update_curation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + curate_service.UpdateCurationRequest, + dict, + ], +) +def test_update_curation_rest_call_success(request_type): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "curation": {"name": "projects/sample1/locations/sample2/curations/sample3"} + } + request_init["curation"] = { + "name": "projects/sample1/locations/sample2/curations/sample3", + "display_name": "display_name_value", + "description": "description_value", + "endpoint": { + "application_integration_endpoint_details": { + "uri": "uri_value", + "trigger_id": "trigger_id_value", + } + }, + "plugin_instance_actions": [ + {"plugin_instance": "plugin_instance_value", "action_id": "action_id_value"} + ], + "last_execution_state": 1, + "last_execution_error_code": 1, + "last_execution_error_message": "last_execution_error_message_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = curate_service.UpdateCurationRequest.meta.fields["curation"] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["curation"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["curation"][field])): + del request_init["curation"][field][i][subfield] + else: + del request_init["curation"][field][subfield] + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = curate_service.Curation( + name="name_value", + display_name="display_name_value", + description="description_value", + last_execution_state=curate_service.Curation.LastExecutionState.SUCCEEDED, + last_execution_error_code=curate_service.Curation.ErrorCode.INTERNAL_ERROR, + last_execution_error_message="last_execution_error_message_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = curate_service.Curation.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.update_curation(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, curate_service.Curation) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert ( + response.last_execution_state + == curate_service.Curation.LastExecutionState.SUCCEEDED + ) + assert ( + response.last_execution_error_code + == curate_service.Curation.ErrorCode.INTERNAL_ERROR + ) + assert response.last_execution_error_message == "last_execution_error_message_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_update_curation_rest_interceptors(null_interceptor): + transport = transports.ApiHubCurateRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubCurateRestInterceptor(), + ) + client = ApiHubCurateClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubCurateRestInterceptor, "post_update_curation" + ) as post, mock.patch.object( + transports.ApiHubCurateRestInterceptor, "post_update_curation_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubCurateRestInterceptor, "pre_update_curation" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = curate_service.UpdateCurationRequest.pb( + curate_service.UpdateCurationRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = curate_service.Curation.to_json(curate_service.Curation()) + req.return_value.content = return_value + + request = curate_service.UpdateCurationRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = curate_service.Curation() + post_with_metadata.return_value = curate_service.Curation(), metadata + + client.update_curation( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_delete_curation_rest_bad_request( + request_type=curate_service.DeleteCurationRequest, +): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/curations/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.delete_curation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + curate_service.DeleteCurationRequest, + dict, + ], +) +def test_delete_curation_rest_call_success(request_type): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/curations/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "" + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.delete_curation(request) + + # Establish that the response is the type that we expect. + assert response is None + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_curation_rest_interceptors(null_interceptor): + transport = transports.ApiHubCurateRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubCurateRestInterceptor(), + ) + client = ApiHubCurateClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubCurateRestInterceptor, "pre_delete_curation" + ) as pre: + pre.assert_not_called() + pb_message = curate_service.DeleteCurationRequest.pb( + curate_service.DeleteCurationRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + request = curate_service.DeleteCurationRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + + client.delete_curation( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + + +def test_get_location_rest_bad_request(request_type=locations_pb2.GetLocationRequest): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_location(request) + + +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.GetLocationRequest, + dict, + ], +) +def test_get_location_rest(request_type): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = locations_pb2.Location() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_location(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) + + +def test_list_locations_rest_bad_request( + request_type=locations_pb2.ListLocationsRequest, +): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict({"name": "projects/sample1"}, request) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_locations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.ListLocationsRequest, + dict, + ], +) +def test_list_locations_rest(request_type): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = locations_pb2.ListLocationsResponse() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_locations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + +def test_cancel_operation_rest_bad_request( + request_type=operations_pb2.CancelOperationRequest, +): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.cancel_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.CancelOperationRequest, + dict, + ], +) +def test_cancel_operation_rest(request_type): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "{}" + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.cancel_operation(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_delete_operation_rest_bad_request( + request_type=operations_pb2.DeleteOperationRequest, +): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.delete_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.DeleteOperationRequest, + dict, + ], +) +def test_delete_operation_rest(request_type): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "{}" + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_operation(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_get_operation_rest_bad_request( + request_type=operations_pb2.GetOperationRequest, +): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.GetOperationRequest, + dict, + ], +) +def test_get_operation_rest(request_type): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_operation(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) + + +def test_list_operations_rest_bad_request( + request_type=operations_pb2.ListOperationsRequest, +): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_operations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.ListOperationsRequest, + dict, + ], +) +def test_list_operations_rest(request_type): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.ListOperationsResponse() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_operations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) + + +def test_initialize_client_w_rest(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_curation_empty_call_rest(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.create_curation), "__call__") as call: + client.create_curation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = curate_service.CreateCurationRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_curation_empty_call_rest(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_curation), "__call__") as call: + client.get_curation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = curate_service.GetCurationRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_curations_empty_call_rest(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.list_curations), "__call__") as call: + client.list_curations(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = curate_service.ListCurationsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_update_curation_empty_call_rest(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.update_curation), "__call__") as call: + client.update_curation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = curate_service.UpdateCurationRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_curation_empty_call_rest(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.delete_curation), "__call__") as call: + client.delete_curation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = curate_service.DeleteCurationRequest() + + assert args[0] == request_msg + + +def test_transport_grpc_default(): + # A client should use the gRPC transport by default. + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert isinstance( + client.transport, + transports.ApiHubCurateGrpcTransport, + ) + + +def test_api_hub_curate_base_transport_error(): + # Passing both a credentials object and credentials_file should raise an error + with pytest.raises(core_exceptions.DuplicateCredentialArgs): + transport = transports.ApiHubCurateTransport( + credentials=ga_credentials.AnonymousCredentials(), + credentials_file="credentials.json", + ) + + +def test_api_hub_curate_base_transport(): + # Instantiate the base transport. + with mock.patch( + "google.cloud.apihub_v1.services.api_hub_curate.transports.ApiHubCurateTransport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.ApiHubCurateTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Every method on the transport should just blindly + # raise NotImplementedError. + methods = ( + "create_curation", + "get_curation", + "list_curations", + "update_curation", + "delete_curation", + "get_location", + "list_locations", + "get_operation", + "cancel_operation", + "delete_operation", + "list_operations", + ) + for method in methods: + with pytest.raises(NotImplementedError): + getattr(transport, method)(request=object()) + + with pytest.raises(NotImplementedError): + transport.close() + + # Catch all for all remaining methods and properties + remainder = [ + "kind", + ] + for r in remainder: + with pytest.raises(NotImplementedError): + getattr(transport, r)() + + +def test_api_hub_curate_base_transport_with_credentials_file(): + # Instantiate the base transport with a credentials file + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch( + "google.cloud.apihub_v1.services.api_hub_curate.transports.ApiHubCurateTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) + transport = transports.ApiHubCurateTransport( + credentials_file="credentials.json", + quota_project_id="octopus", + ) + load_creds.assert_called_once_with( + "credentials.json", + scopes=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", + ) + + +def test_api_hub_curate_base_transport_with_adc(): + # Test the default credentials are used if credentials and credentials_file are None. + with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch( + "google.cloud.apihub_v1.services.api_hub_curate.transports.ApiHubCurateTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport = transports.ApiHubCurateTransport() + adc.assert_called_once() + + +def test_api_hub_curate_auth_adc(): + # If no credentials are provided, we should use ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + ApiHubCurateClient() + adc.assert_called_once_with( + scopes=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id=None, + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubCurateGrpcTransport, + transports.ApiHubCurateGrpcAsyncIOTransport, + ], +) +def test_api_hub_curate_transport_auth_adc(transport_class): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + adc.assert_called_once_with( + scopes=["1", "2"], + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubCurateGrpcTransport, + transports.ApiHubCurateGrpcAsyncIOTransport, + transports.ApiHubCurateRestTransport, + ], +) +def test_api_hub_curate_transport_auth_gdch_credentials(transport_class): + host = "https://language.com" + api_audience_tests = [None, "https://language2.com"] + api_audience_expect = [host, "https://language2.com"] + for t, e in zip(api_audience_tests, api_audience_expect): + with mock.patch.object(google.auth, "default", autospec=True) as adc: + gdch_mock = mock.MagicMock() + type(gdch_mock).with_gdch_audience = mock.PropertyMock( + return_value=gdch_mock + ) + adc.return_value = (gdch_mock, None) + transport_class(host=host, api_audience=t) + gdch_mock.with_gdch_audience.assert_called_once_with(e) + + +@pytest.mark.parametrize( + "transport_class,grpc_helpers", + [ + (transports.ApiHubCurateGrpcTransport, grpc_helpers), + (transports.ApiHubCurateGrpcAsyncIOTransport, grpc_helpers_async), + ], +) +def test_api_hub_curate_transport_create_channel(transport_class, grpc_helpers): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel", autospec=True + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + adc.return_value = (creds, None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + + create_channel.assert_called_with( + "apihub.googleapis.com:443", + credentials=creds, + credentials_file=None, + quota_project_id="octopus", + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + scopes=["1", "2"], + default_host="apihub.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + +@pytest.mark.parametrize( + "transport_class", + [transports.ApiHubCurateGrpcTransport, transports.ApiHubCurateGrpcAsyncIOTransport], +) +def test_api_hub_curate_grpc_transport_client_cert_source_for_mtls(transport_class): + cred = ga_credentials.AnonymousCredentials() + + # Check ssl_channel_credentials is used if provided. + with mock.patch.object(transport_class, "create_channel") as mock_create_channel: + mock_ssl_channel_creds = mock.Mock() + transport_class( + host="squid.clam.whelk", + credentials=cred, + ssl_channel_credentials=mock_ssl_channel_creds, + ) + mock_create_channel.assert_called_once_with( + "squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_channel_creds, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + # Check if ssl_channel_credentials is not provided, then client_cert_source_for_mtls + # is used. + with mock.patch.object(transport_class, "create_channel", return_value=mock.Mock()): + with mock.patch("grpc.ssl_channel_credentials") as mock_ssl_cred: + transport_class( + credentials=cred, + client_cert_source_for_mtls=client_cert_source_callback, + ) + expected_cert, expected_key = client_cert_source_callback() + mock_ssl_cred.assert_called_once_with( + certificate_chain=expected_cert, private_key=expected_key + ) + + +def test_api_hub_curate_http_transport_client_cert_source_for_mtls(): + cred = ga_credentials.AnonymousCredentials() + with mock.patch( + "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" + ) as mock_configure_mtls_channel: + transports.ApiHubCurateRestTransport( + credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + ) + mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + + +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "grpc_asyncio", + "rest", + ], +) +def test_api_hub_curate_host_no_port(transport_name): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="apihub.googleapis.com" + ), + transport=transport_name, + ) + assert client.transport._host == ( + "apihub.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://apihub.googleapis.com" + ) + + +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "grpc_asyncio", + "rest", + ], +) +def test_api_hub_curate_host_with_port(transport_name): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="apihub.googleapis.com:8000" + ), + transport=transport_name, + ) + assert client.transport._host == ( + "apihub.googleapis.com:8000" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://apihub.googleapis.com:8000" + ) + + +@pytest.mark.parametrize( + "transport_name", + [ + "rest", + ], +) +def test_api_hub_curate_client_transport_session_collision(transport_name): + creds1 = ga_credentials.AnonymousCredentials() + creds2 = ga_credentials.AnonymousCredentials() + client1 = ApiHubCurateClient( + credentials=creds1, + transport=transport_name, + ) + client2 = ApiHubCurateClient( + credentials=creds2, + transport=transport_name, + ) + session1 = client1.transport.create_curation._session + session2 = client2.transport.create_curation._session + assert session1 != session2 + session1 = client1.transport.get_curation._session + session2 = client2.transport.get_curation._session + assert session1 != session2 + session1 = client1.transport.list_curations._session + session2 = client2.transport.list_curations._session + assert session1 != session2 + session1 = client1.transport.update_curation._session + session2 = client2.transport.update_curation._session + assert session1 != session2 + session1 = client1.transport.delete_curation._session + session2 = client2.transport.delete_curation._session + assert session1 != session2 + + +def test_api_hub_curate_grpc_transport_channel(): + channel = grpc.secure_channel("http://localhost/", grpc.local_channel_credentials()) + + # Check that channel is used if provided. + transport = transports.ApiHubCurateGrpcTransport( + host="squid.clam.whelk", + channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None + + +def test_api_hub_curate_grpc_asyncio_transport_channel(): + channel = aio.secure_channel("http://localhost/", grpc.local_channel_credentials()) + + # Check that channel is used if provided. + transport = transports.ApiHubCurateGrpcAsyncIOTransport( + host="squid.clam.whelk", + channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None + + +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. +@pytest.mark.parametrize( + "transport_class", + [transports.ApiHubCurateGrpcTransport, transports.ApiHubCurateGrpcAsyncIOTransport], +) +def test_api_hub_curate_transport_channel_mtls_with_client_cert_source(transport_class): + with mock.patch( + "grpc.ssl_channel_credentials", autospec=True + ) as grpc_ssl_channel_cred: + with mock.patch.object( + transport_class, "create_channel" + ) 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 = ga_credentials.AnonymousCredentials() + with pytest.warns(DeprecationWarning): + with mock.patch.object(google.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=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + assert transport._ssl_channel_credentials == mock_ssl_cred + + +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. +@pytest.mark.parametrize( + "transport_class", + [transports.ApiHubCurateGrpcTransport, transports.ApiHubCurateGrpcAsyncIOTransport], +) +def test_api_hub_curate_transport_channel_mtls_with_adc(transport_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), + ): + with mock.patch.object( + transport_class, "create_channel" + ) 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, + ) + + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=mock_cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + + +def test_curation_path(): + project = "squid" + location = "clam" + curation = "whelk" + expected = "projects/{project}/locations/{location}/curations/{curation}".format( + project=project, + location=location, + curation=curation, + ) + actual = ApiHubCurateClient.curation_path(project, location, curation) + assert expected == actual + + +def test_parse_curation_path(): + expected = { + "project": "octopus", + "location": "oyster", + "curation": "nudibranch", + } + path = ApiHubCurateClient.curation_path(**expected) + + # Check that the path construction is reversible. + actual = ApiHubCurateClient.parse_curation_path(path) + assert expected == actual + + +def test_plugin_instance_path(): + project = "cuttlefish" + location = "mussel" + plugin = "winkle" + instance = "nautilus" + expected = "projects/{project}/locations/{location}/plugins/{plugin}/instances/{instance}".format( + project=project, + location=location, + plugin=plugin, + instance=instance, ) + actual = ApiHubCurateClient.plugin_instance_path( + project, location, plugin, instance + ) + assert expected == actual - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/curations/sample3"} - request = request_type(**request_init) - - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = None - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "" - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.delete_curation(request) +def test_parse_plugin_instance_path(): + expected = { + "project": "scallop", + "location": "abalone", + "plugin": "squid", + "instance": "clam", + } + path = ApiHubCurateClient.plugin_instance_path(**expected) - # Establish that the response is the type that we expect. - assert response is None + # Check that the path construction is reversible. + actual = ApiHubCurateClient.parse_plugin_instance_path(path) + assert expected == actual -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_delete_curation_rest_interceptors(null_interceptor): - transport = transports.ApiHubCurateRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubCurateRestInterceptor(), +def test_common_billing_account_path(): + billing_account = "whelk" + expected = "billingAccounts/{billing_account}".format( + billing_account=billing_account, ) - client = ApiHubCurateClient(transport=transport) + actual = ApiHubCurateClient.common_billing_account_path(billing_account) + assert expected == actual - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubCurateRestInterceptor, "pre_delete_curation" - ) as pre: - pre.assert_not_called() - pb_message = curate_service.DeleteCurationRequest.pb( - curate_service.DeleteCurationRequest() - ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +def test_parse_common_billing_account_path(): + expected = { + "billing_account": "octopus", + } + path = ApiHubCurateClient.common_billing_account_path(**expected) - request = curate_service.DeleteCurationRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata + # Check that the path construction is reversible. + actual = ApiHubCurateClient.parse_common_billing_account_path(path) + assert expected == actual - client.delete_curation( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], - ) - pre.assert_called_once() +def test_common_folder_path(): + folder = "oyster" + expected = "folders/{folder}".format( + folder=folder, + ) + actual = ApiHubCurateClient.common_folder_path(folder) + assert expected == actual -def test_get_location_rest_bad_request(request_type=locations_pb2.GetLocationRequest): - client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2"}, request - ) +def test_parse_common_folder_path(): + expected = { + "folder": "nudibranch", + } + path = ApiHubCurateClient.common_folder_path(**expected) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_location(request) + # Check that the path construction is reversible. + actual = ApiHubCurateClient.parse_common_folder_path(path) + assert expected == actual -@pytest.mark.parametrize( - "request_type", - [ - locations_pb2.GetLocationRequest, - dict, - ], -) -def test_get_location_rest(request_type): - client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +def test_common_organization_path(): + organization = "cuttlefish" + expected = "organizations/{organization}".format( + organization=organization, ) + actual = ApiHubCurateClient.common_organization_path(organization) + assert expected == actual - request_init = {"name": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = locations_pb2.Location() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") +def test_parse_common_organization_path(): + expected = { + "organization": "mussel", + } + path = ApiHubCurateClient.common_organization_path(**expected) - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Check that the path construction is reversible. + actual = ApiHubCurateClient.parse_common_organization_path(path) + assert expected == actual - response = client.get_location(request) - # Establish that the response is the type that we expect. - assert isinstance(response, locations_pb2.Location) +def test_common_project_path(): + project = "winkle" + expected = "projects/{project}".format( + project=project, + ) + actual = ApiHubCurateClient.common_project_path(project) + assert expected == actual -def test_list_locations_rest_bad_request( - request_type=locations_pb2.ListLocationsRequest, -): - client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict({"name": "projects/sample1"}, request) +def test_parse_common_project_path(): + expected = { + "project": "nautilus", + } + path = ApiHubCurateClient.common_project_path(**expected) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_locations(request) + # Check that the path construction is reversible. + actual = ApiHubCurateClient.parse_common_project_path(path) + assert expected == actual -@pytest.mark.parametrize( - "request_type", - [ - locations_pb2.ListLocationsRequest, - dict, - ], -) -def test_list_locations_rest(request_type): - client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +def test_common_location_path(): + project = "scallop" + location = "abalone" + expected = "projects/{project}/locations/{location}".format( + project=project, + location=location, ) + actual = ApiHubCurateClient.common_location_path(project, location) + assert expected == actual - request_init = {"name": "projects/sample1"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = locations_pb2.ListLocationsResponse() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") +def test_parse_common_location_path(): + expected = { + "project": "squid", + "location": "clam", + } + path = ApiHubCurateClient.common_location_path(**expected) - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Check that the path construction is reversible. + actual = ApiHubCurateClient.parse_common_location_path(path) + assert expected == actual - response = client.list_locations(request) - # Establish that the response is the type that we expect. - assert isinstance(response, locations_pb2.ListLocationsResponse) +def test_client_with_default_client_info(): + client_info = gapic_v1.client_info.ClientInfo() + + with mock.patch.object( + transports.ApiHubCurateTransport, "_prep_wrapped_messages" + ) as prep: + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + client_info=client_info, + ) + prep.assert_called_once_with(client_info) + + with mock.patch.object( + transports.ApiHubCurateTransport, "_prep_wrapped_messages" + ) as prep: + transport_class = ApiHubCurateClient.get_transport_class() + transport = transport_class( + credentials=ga_credentials.AnonymousCredentials(), + client_info=client_info, + ) + prep.assert_called_once_with(client_info) -def test_cancel_operation_rest_bad_request( - request_type=operations_pb2.CancelOperationRequest, -): +def test_delete_operation(transport: str = "grpc"): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + transport=transport, ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.cancel_operation(request) + # 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 = operations_pb2.DeleteOperationRequest() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.delete_operation(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 -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.CancelOperationRequest, - dict, - ], -) -def test_cancel_operation_rest(request_type): - client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + # Establish that the response is the type that we expect. + assert response is None - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = None - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "{}" - response_value.content = json_return_value.encode("UTF-8") +@pytest.mark.asyncio +async def test_delete_operation_async(transport: str = "grpc_asyncio"): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # 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 = operations_pb2.DeleteOperationRequest() - response = client.cancel_operation(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_operation(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 response is the type that we expect. assert response is None -def test_delete_operation_rest_bad_request( - request_type=operations_pb2.DeleteOperationRequest, -): +def test_delete_operation_field_headers(): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_operation(request) + # 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 = operations_pb2.DeleteOperationRequest() + request.name = "locations" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + call.return_value = None -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.DeleteOperationRequest, - dict, - ], -) -def test_delete_operation_rest(request_type): - client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + client.delete_operation(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 - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = None + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "{}" - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +@pytest.mark.asyncio +async def test_delete_operation_field_headers_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) - response = client.delete_operation(request) + # 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 = operations_pb2.DeleteOperationRequest() + request.name = "locations" - # Establish that the response is the type that we expect. - assert response is None + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_operation(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", + "name=locations", + ) in kw["metadata"] -def test_get_operation_rest_bad_request( - request_type=operations_pb2.GetOperationRequest, -): + +def test_delete_operation_from_dict(): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + + response = client.delete_operation( + request={ + "name": "locations", + } + ) + call.assert_called() - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_operation(request) + +@pytest.mark.asyncio +async def test_delete_operation_from_dict_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.GetOperationRequest, - dict, - ], -) -def test_get_operation_rest(request_type): +def test_cancel_operation(transport: str = "grpc"): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation() + # 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 = operations_pb2.CancelOperationRequest() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.cancel_operation(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 - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert response is None - response = client.get_operation(request) + +@pytest.mark.asyncio +async def test_cancel_operation_async(transport: str = "grpc_asyncio"): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.CancelOperationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.cancel_operation(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 response is the type that we expect. - assert isinstance(response, operations_pb2.Operation) + assert response is None -def test_list_operations_rest_bad_request( - request_type=operations_pb2.ListOperationsRequest, -): +def test_cancel_operation_field_headers(): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2"}, request ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_operations(request) + # 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 = operations_pb2.CancelOperationRequest() + request.name = "locations" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + call.return_value = None -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.ListOperationsRequest, - dict, - ], -) -def test_list_operations_rest(request_type): - client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + client.cancel_operation(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 - request_init = {"name": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.ListOperationsResponse() + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +@pytest.mark.asyncio +async def test_cancel_operation_field_headers_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) - response = client.list_operations(request) + # 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 = operations_pb2.CancelOperationRequest() + request.name = "locations" - # Establish that the response is the type that we expect. - assert isinstance(response, operations_pb2.ListOperationsResponse) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.cancel_operation(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", + "name=locations", + ) in kw["metadata"] -def test_initialize_client_w_rest(): +def test_cancel_operation_from_dict(): client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), ) - assert client is not None + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + + response = client.cancel_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_create_curation_empty_call_rest(): +@pytest.mark.asyncio +async def test_cancel_operation_from_dict_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.cancel_operation( + request={ + "name": "locations", + } + ) + call.assert_called() + + +def test_get_operation(transport: str = "grpc"): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.create_curation), "__call__") as call: - client.create_curation(request=None) + # 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 = operations_pb2.GetOperationRequest() - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation() + response = client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = curate_service.CreateCurationRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_get_curation_empty_call_rest(): - client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +@pytest.mark.asyncio +async def test_get_operation_async(transport: str = "grpc_asyncio"): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.get_curation), "__call__") as call: - client.get_curation(request=None) + # 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 = operations_pb2.GetOperationRequest() - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + response = await client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = curate_service.GetCurationRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_list_curations_empty_call_rest(): +def test_get_operation_field_headers(): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.list_curations), "__call__") as call: - client.list_curations(request=None) + # 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 = operations_pb2.GetOperationRequest() + request.name = "locations" - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + call.return_value = operations_pb2.Operation() + + client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = curate_service.ListCurationsRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_update_curation_empty_call_rest(): - client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +@pytest.mark.asyncio +async def test_get_operation_field_headers_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), ) - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.update_curation), "__call__") as call: - client.update_curation(request=None) + # 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 = operations_pb2.GetOperationRequest() + request.name = "locations" - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + await client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = curate_service.UpdateCurationRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_delete_curation_empty_call_rest(): +def test_get_operation_from_dict(): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation() + + response = client.get_operation( + request={ + "name": "locations", + } + ) + call.assert_called() - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.delete_curation), "__call__") as call: - client.delete_curation(request=None) - # Establish that the underlying stub method was called. +@pytest.mark.asyncio +async def test_get_operation_from_dict_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + response = await client.get_operation( + request={ + "name": "locations", + } + ) call.assert_called() + + +def test_list_operations(transport: str = "grpc"): + client = ApiHubCurateClient( + credentials=ga_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 = operations_pb2.ListOperationsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.ListOperationsResponse() + response = client.list_operations(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = curate_service.DeleteCurationRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) -def test_api_hub_curate_base_transport_error(): - # Passing both a credentials object and credentials_file should raise an error - with pytest.raises(core_exceptions.DuplicateCredentialArgs): - transport = transports.ApiHubCurateTransport( - credentials=ga_credentials.AnonymousCredentials(), - credentials_file="credentials.json", - ) +@pytest.mark.asyncio +async def test_list_operations_async(transport: str = "grpc_asyncio"): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.ListOperationsRequest() -def test_api_hub_curate_base_transport(): - # Instantiate the base transport. - with mock.patch( - "google.cloud.apihub_v1.services.api_hub_curate.transports.ApiHubCurateTransport.__init__" - ) as Transport: - Transport.return_value = None - transport = transports.ApiHubCurateTransport( - credentials=ga_credentials.AnonymousCredentials(), + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() ) + response = await client.list_operations(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 - # Every method on the transport should just blindly - # raise NotImplementedError. - methods = ( - "create_curation", - "get_curation", - "list_curations", - "update_curation", - "delete_curation", - "get_location", - "list_locations", - "get_operation", - "cancel_operation", - "delete_operation", - "list_operations", + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) + + +def test_list_operations_field_headers(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), ) - for method in methods: - with pytest.raises(NotImplementedError): - getattr(transport, method)(request=object()) - with pytest.raises(NotImplementedError): - transport.close() + # 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 = operations_pb2.ListOperationsRequest() + request.name = "locations" - # Catch all for all remaining methods and properties - remainder = [ - "kind", - ] - for r in remainder: - with pytest.raises(NotImplementedError): - getattr(transport, r)() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + call.return_value = operations_pb2.ListOperationsResponse() + client.list_operations(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 -def test_api_hub_curate_base_transport_with_credentials_file(): - # Instantiate the base transport with a credentials file - with mock.patch.object( - google.auth, "load_credentials_from_file", autospec=True - ) as load_creds, mock.patch( - "google.cloud.apihub_v1.services.api_hub_curate.transports.ApiHubCurateTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None - load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) - transport = transports.ApiHubCurateTransport( - credentials_file="credentials.json", - quota_project_id="octopus", - ) - load_creds.assert_called_once_with( - "credentials.json", - scopes=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id="octopus", - ) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -def test_api_hub_curate_base_transport_with_adc(): - # Test the default credentials are used if credentials and credentials_file are None. - with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch( - "google.cloud.apihub_v1.services.api_hub_curate.transports.ApiHubCurateTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport = transports.ApiHubCurateTransport() - adc.assert_called_once() +@pytest.mark.asyncio +async def test_list_operations_field_headers_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) + # 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 = operations_pb2.ListOperationsRequest() + request.name = "locations" -def test_api_hub_curate_auth_adc(): - # If no credentials are provided, we should use ADC credentials. - with mock.patch.object(google.auth, "default", autospec=True) as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - ApiHubCurateClient() - adc.assert_called_once_with( - scopes=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id=None, + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() ) + await client.list_operations(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", + "name=locations", + ) in kw["metadata"] -def test_api_hub_curate_http_transport_client_cert_source_for_mtls(): - cred = ga_credentials.AnonymousCredentials() - with mock.patch( - "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" - ) as mock_configure_mtls_channel: - transports.ApiHubCurateRestTransport( - credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + +def test_list_operations_from_dict(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.ListOperationsResponse() + + response = client.list_operations( + request={ + "name": "locations", + } ) - mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + call.assert_called() -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_api_hub_curate_host_no_port(transport_name): +@pytest.mark.asyncio +async def test_list_operations_from_dict_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() + ) + response = await client.list_operations( + request={ + "name": "locations", + } + ) + call.assert_called() + + +def test_list_locations(transport: str = "grpc"): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - client_options=client_options.ClientOptions( - api_endpoint="apihub.googleapis.com" - ), - transport=transport_name, + transport=transport, ) - assert client.transport._host == ( - "apihub.googleapis.com:443" - if transport_name in ["grpc", "grpc_asyncio"] - else "https://apihub.googleapis.com" + + # 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 = locations_pb2.ListLocationsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.ListLocationsResponse() + response = client.list_locations(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 response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + +@pytest.mark.asyncio +async def test_list_locations_async(transport: str = "grpc_asyncio"): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + 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 = locations_pb2.ListLocationsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + response = await client.list_locations(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 response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_api_hub_curate_host_with_port(transport_name): + +def test_list_locations_field_headers(): client = ApiHubCurateClient( credentials=ga_credentials.AnonymousCredentials(), - client_options=client_options.ClientOptions( - api_endpoint="apihub.googleapis.com:8000" - ), - transport=transport_name, - ) - assert client.transport._host == ( - "apihub.googleapis.com:8000" - if transport_name in ["grpc", "grpc_asyncio"] - else "https://apihub.googleapis.com:8000" ) + # 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 = locations_pb2.ListLocationsRequest() + request.name = "locations" -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_api_hub_curate_client_transport_session_collision(transport_name): - creds1 = ga_credentials.AnonymousCredentials() - creds2 = ga_credentials.AnonymousCredentials() - client1 = ApiHubCurateClient( - credentials=creds1, - transport=transport_name, - ) - client2 = ApiHubCurateClient( - credentials=creds2, - transport=transport_name, - ) - session1 = client1.transport.create_curation._session - session2 = client2.transport.create_curation._session - assert session1 != session2 - session1 = client1.transport.get_curation._session - session2 = client2.transport.get_curation._session - assert session1 != session2 - session1 = client1.transport.list_curations._session - session2 = client2.transport.list_curations._session - assert session1 != session2 - session1 = client1.transport.update_curation._session - session2 = client2.transport.update_curation._session - assert session1 != session2 - session1 = client1.transport.delete_curation._session - session2 = client2.transport.delete_curation._session - assert session1 != session2 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + call.return_value = locations_pb2.ListLocationsResponse() + client.list_locations(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 -def test_curation_path(): - project = "squid" - location = "clam" - curation = "whelk" - expected = "projects/{project}/locations/{location}/curations/{curation}".format( - project=project, - location=location, - curation=curation, + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_list_locations_field_headers_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), ) - actual = ApiHubCurateClient.curation_path(project, location, curation) - assert expected == actual + # 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 = locations_pb2.ListLocationsRequest() + request.name = "locations" -def test_parse_curation_path(): - expected = { - "project": "octopus", - "location": "oyster", - "curation": "nudibranch", - } - path = ApiHubCurateClient.curation_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + await client.list_locations(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 - # Check that the path construction is reversible. - actual = ApiHubCurateClient.parse_curation_path(path) - assert expected == actual + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -def test_plugin_instance_path(): - project = "cuttlefish" - location = "mussel" - plugin = "winkle" - instance = "nautilus" - expected = "projects/{project}/locations/{location}/plugins/{plugin}/instances/{instance}".format( - project=project, - location=location, - plugin=plugin, - instance=instance, - ) - actual = ApiHubCurateClient.plugin_instance_path( - project, location, plugin, instance +def test_list_locations_from_dict(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), ) - assert expected == actual - + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.ListLocationsResponse() + + response = client.list_locations( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_parse_plugin_instance_path(): - expected = { - "project": "scallop", - "location": "abalone", - "plugin": "squid", - "instance": "clam", - } - path = ApiHubCurateClient.plugin_instance_path(**expected) - # Check that the path construction is reversible. - actual = ApiHubCurateClient.parse_plugin_instance_path(path) - assert expected == actual +@pytest.mark.asyncio +async def test_list_locations_from_dict_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + response = await client.list_locations( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_common_billing_account_path(): - billing_account = "whelk" - expected = "billingAccounts/{billing_account}".format( - billing_account=billing_account, +def test_get_location(transport: str = "grpc"): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - actual = ApiHubCurateClient.common_billing_account_path(billing_account) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() -def test_parse_common_billing_account_path(): - expected = { - "billing_account": "octopus", - } - path = ApiHubCurateClient.common_billing_account_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.Location() + response = client.get_location(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 - # Check that the path construction is reversible. - actual = ApiHubCurateClient.parse_common_billing_account_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) -def test_common_folder_path(): - folder = "oyster" - expected = "folders/{folder}".format( - folder=folder, +@pytest.mark.asyncio +async def test_get_location_async(transport: str = "grpc_asyncio"): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - actual = ApiHubCurateClient.common_folder_path(folder) - assert expected == actual - -def test_parse_common_folder_path(): - expected = { - "folder": "nudibranch", - } - path = ApiHubCurateClient.common_folder_path(**expected) + # 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 = locations_pb2.GetLocationRequest() - # Check that the path construction is reversible. - actual = ApiHubCurateClient.parse_common_folder_path(path) - assert expected == actual + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + response = await client.get_location(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 response is the type that we expect. + assert isinstance(response, locations_pb2.Location) -def test_common_organization_path(): - organization = "cuttlefish" - expected = "organizations/{organization}".format( - organization=organization, - ) - actual = ApiHubCurateClient.common_organization_path(organization) - assert expected == actual +def test_get_location_field_headers(): + client = ApiHubCurateClient(credentials=ga_credentials.AnonymousCredentials()) -def test_parse_common_organization_path(): - expected = { - "organization": "mussel", - } - path = ApiHubCurateClient.common_organization_path(**expected) + # 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 = locations_pb2.GetLocationRequest() + request.name = "locations/abc" - # Check that the path construction is reversible. - actual = ApiHubCurateClient.parse_common_organization_path(path) - assert expected == actual + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + call.return_value = locations_pb2.Location() + client.get_location(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 -def test_common_project_path(): - project = "winkle" - expected = "projects/{project}".format( - project=project, - ) - actual = ApiHubCurateClient.common_project_path(project) - assert expected == actual + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations/abc", + ) in kw["metadata"] -def test_parse_common_project_path(): - expected = { - "project": "nautilus", - } - path = ApiHubCurateClient.common_project_path(**expected) +@pytest.mark.asyncio +async def test_get_location_field_headers_async(): + client = ApiHubCurateAsyncClient(credentials=async_anonymous_credentials()) - # Check that the path construction is reversible. - actual = ApiHubCurateClient.parse_common_project_path(path) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() + request.name = "locations/abc" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + await client.get_location(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 -def test_common_location_path(): - project = "scallop" - location = "abalone" - expected = "projects/{project}/locations/{location}".format( - project=project, - location=location, - ) - actual = ApiHubCurateClient.common_location_path(project, location) - assert expected == actual + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations/abc", + ) in kw["metadata"] -def test_parse_common_location_path(): - expected = { - "project": "squid", - "location": "clam", - } - path = ApiHubCurateClient.common_location_path(**expected) +def test_get_location_from_dict(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.Location() + + response = client.get_location( + request={ + "name": "locations/abc", + } + ) + call.assert_called() - # Check that the path construction is reversible. - actual = ApiHubCurateClient.parse_common_location_path(path) - assert expected == actual +@pytest.mark.asyncio +async def test_get_location_from_dict_async(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + response = await client.get_location( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_client_with_default_client_info(): - client_info = gapic_v1.client_info.ClientInfo() +def test_transport_close_grpc(): + client = ApiHubCurateClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) with mock.patch.object( - transports.ApiHubCurateTransport, "_prep_wrapped_messages" - ) as prep: - client = ApiHubCurateClient( - credentials=ga_credentials.AnonymousCredentials(), - client_info=client_info, - ) - prep.assert_called_once_with(client_info) + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + +@pytest.mark.asyncio +async def test_transport_close_grpc_asyncio(): + client = ApiHubCurateAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) with mock.patch.object( - transports.ApiHubCurateTransport, "_prep_wrapped_messages" - ) as prep: - transport_class = ApiHubCurateClient.get_transport_class() - transport = transport_class( - credentials=ga_credentials.AnonymousCredentials(), - client_info=client_info, - ) - prep.assert_called_once_with(client_info) + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + async with client: + close.assert_not_called() + close.assert_called_once() def test_transport_close_rest(): @@ -3694,6 +7206,7 @@ def test_transport_close_rest(): def test_client_ctx(): transports = [ "rest", + "grpc", ] for transport in transports: client = ApiHubCurateClient( @@ -3710,7 +7223,8 @@ def test_client_ctx(): @pytest.mark.parametrize( "client_class,transport_class", [ - (ApiHubCurateClient, transports.ApiHubCurateRestTransport), + (ApiHubCurateClient, transports.ApiHubCurateGrpcTransport), + (ApiHubCurateAsyncClient, transports.ApiHubCurateGrpcAsyncIOTransport), ], ) def test_api_key_credentials(client_class, transport_class): diff --git a/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub_dependencies.py b/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub_dependencies.py index b9a97c2b8a8e..a5cde3dd52a0 100644 --- a/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub_dependencies.py +++ b/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub_dependencies.py @@ -57,6 +57,7 @@ from google.protobuf import timestamp_pb2 # type: ignore from google.cloud.apihub_v1.services.api_hub_dependencies import ( + ApiHubDependenciesAsyncClient, ApiHubDependenciesClient, pagers, transports, @@ -251,6 +252,11 @@ def test__get_client_cert_source(): "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubDependenciesClient), ) +@mock.patch.object( + ApiHubDependenciesAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubDependenciesAsyncClient), +) def test__get_api_endpoint(): api_override = "foo.com" mock_client_cert_source = mock.Mock() @@ -382,6 +388,8 @@ def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code): @pytest.mark.parametrize( "client_class,transport_name", [ + (ApiHubDependenciesClient, "grpc"), + (ApiHubDependenciesAsyncClient, "grpc_asyncio"), (ApiHubDependenciesClient, "rest"), ], ) @@ -408,6 +416,8 @@ def test_api_hub_dependencies_client_from_service_account_info( @pytest.mark.parametrize( "transport_class,transport_name", [ + (transports.ApiHubDependenciesGrpcTransport, "grpc"), + (transports.ApiHubDependenciesGrpcAsyncIOTransport, "grpc_asyncio"), (transports.ApiHubDependenciesRestTransport, "rest"), ], ) @@ -432,6 +442,8 @@ def test_api_hub_dependencies_client_service_account_always_use_jwt( @pytest.mark.parametrize( "client_class,transport_name", [ + (ApiHubDependenciesClient, "grpc"), + (ApiHubDependenciesAsyncClient, "grpc_asyncio"), (ApiHubDependenciesClient, "rest"), ], ) @@ -465,17 +477,24 @@ def test_api_hub_dependencies_client_from_service_account_file( def test_api_hub_dependencies_client_get_transport_class(): transport = ApiHubDependenciesClient.get_transport_class() available_transports = [ + transports.ApiHubDependenciesGrpcTransport, transports.ApiHubDependenciesRestTransport, ] assert transport in available_transports - transport = ApiHubDependenciesClient.get_transport_class("rest") - assert transport == transports.ApiHubDependenciesRestTransport + transport = ApiHubDependenciesClient.get_transport_class("grpc") + assert transport == transports.ApiHubDependenciesGrpcTransport @pytest.mark.parametrize( "client_class,transport_class,transport_name", [ + (ApiHubDependenciesClient, transports.ApiHubDependenciesGrpcTransport, "grpc"), + ( + ApiHubDependenciesAsyncClient, + transports.ApiHubDependenciesGrpcAsyncIOTransport, + "grpc_asyncio", + ), (ApiHubDependenciesClient, transports.ApiHubDependenciesRestTransport, "rest"), ], ) @@ -484,6 +503,11 @@ def test_api_hub_dependencies_client_get_transport_class(): "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubDependenciesClient), ) +@mock.patch.object( + ApiHubDependenciesAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubDependenciesAsyncClient), +) def test_api_hub_dependencies_client_client_options( client_class, transport_class, transport_name ): @@ -617,6 +641,30 @@ def test_api_hub_dependencies_client_client_options( @pytest.mark.parametrize( "client_class,transport_class,transport_name,use_client_cert_env", [ + ( + ApiHubDependenciesClient, + transports.ApiHubDependenciesGrpcTransport, + "grpc", + "true", + ), + ( + ApiHubDependenciesAsyncClient, + transports.ApiHubDependenciesGrpcAsyncIOTransport, + "grpc_asyncio", + "true", + ), + ( + ApiHubDependenciesClient, + transports.ApiHubDependenciesGrpcTransport, + "grpc", + "false", + ), + ( + ApiHubDependenciesAsyncClient, + transports.ApiHubDependenciesGrpcAsyncIOTransport, + "grpc_asyncio", + "false", + ), ( ApiHubDependenciesClient, transports.ApiHubDependenciesRestTransport, @@ -636,6 +684,11 @@ def test_api_hub_dependencies_client_client_options( "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubDependenciesClient), ) +@mock.patch.object( + ApiHubDependenciesAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubDependenciesAsyncClient), +) @mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) def test_api_hub_dependencies_client_mtls_env_auto( client_class, transport_class, transport_name, use_client_cert_env @@ -739,12 +792,19 @@ def test_api_hub_dependencies_client_mtls_env_auto( ) -@pytest.mark.parametrize("client_class", [ApiHubDependenciesClient]) +@pytest.mark.parametrize( + "client_class", [ApiHubDependenciesClient, ApiHubDependenciesAsyncClient] +) @mock.patch.object( ApiHubDependenciesClient, "DEFAULT_ENDPOINT", modify_default_endpoint(ApiHubDependenciesClient), ) +@mock.patch.object( + ApiHubDependenciesAsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(ApiHubDependenciesAsyncClient), +) def test_api_hub_dependencies_client_get_mtls_endpoint_and_cert_source(client_class): mock_client_cert_source = mock.Mock() @@ -836,12 +896,19 @@ def test_api_hub_dependencies_client_get_mtls_endpoint_and_cert_source(client_cl ) -@pytest.mark.parametrize("client_class", [ApiHubDependenciesClient]) +@pytest.mark.parametrize( + "client_class", [ApiHubDependenciesClient, ApiHubDependenciesAsyncClient] +) @mock.patch.object( ApiHubDependenciesClient, "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubDependenciesClient), ) +@mock.patch.object( + ApiHubDependenciesAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubDependenciesAsyncClient), +) def test_api_hub_dependencies_client_client_api_endpoint(client_class): mock_client_cert_source = client_cert_source_callback api_override = "foo.com" @@ -918,6 +985,12 @@ def test_api_hub_dependencies_client_client_api_endpoint(client_class): @pytest.mark.parametrize( "client_class,transport_class,transport_name", [ + (ApiHubDependenciesClient, transports.ApiHubDependenciesGrpcTransport, "grpc"), + ( + ApiHubDependenciesAsyncClient, + transports.ApiHubDependenciesGrpcAsyncIOTransport, + "grpc_asyncio", + ), (ApiHubDependenciesClient, transports.ApiHubDependenciesRestTransport, "rest"), ], ) @@ -949,6 +1022,18 @@ def test_api_hub_dependencies_client_client_options_scopes( @pytest.mark.parametrize( "client_class,transport_class,transport_name,grpc_helpers", [ + ( + ApiHubDependenciesClient, + transports.ApiHubDependenciesGrpcTransport, + "grpc", + grpc_helpers, + ), + ( + ApiHubDependenciesAsyncClient, + transports.ApiHubDependenciesGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), ( ApiHubDependenciesClient, transports.ApiHubDependenciesRestTransport, @@ -981,13 +1066,179 @@ def test_api_hub_dependencies_client_client_options_credentials_file( ) -def test_create_dependency_rest_use_cached_wrapped_rpc(): +def test_api_hub_dependencies_client_client_options_from_dict(): + with mock.patch( + "google.cloud.apihub_v1.services.api_hub_dependencies.transports.ApiHubDependenciesGrpcTransport.__init__" + ) as grpc_transport: + grpc_transport.return_value = None + client = ApiHubDependenciesClient( + client_options={"api_endpoint": "squid.clam.whelk"} + ) + grpc_transport.assert_called_once_with( + credentials=None, + credentials_file=None, + host="squid.clam.whelk", + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name,grpc_helpers", + [ + ( + ApiHubDependenciesClient, + transports.ApiHubDependenciesGrpcTransport, + "grpc", + grpc_helpers, + ), + ( + ApiHubDependenciesAsyncClient, + transports.ApiHubDependenciesGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), + ], +) +def test_api_hub_dependencies_client_create_channel_credentials_file( + client_class, transport_class, transport_name, grpc_helpers +): + # Check the case credentials file is provided. + options = client_options.ClientOptions(credentials_file="credentials.json") + + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options, transport=transport_name) + patched.assert_called_once_with( + credentials=None, + credentials_file="credentials.json", + host=client._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE + ), + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + # test that the credentials from file are saved and used as the credentials. + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel" + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + file_creds = ga_credentials.AnonymousCredentials() + load_creds.return_value = (file_creds, None) + adc.return_value = (creds, None) + client = client_class(client_options=options, transport=transport_name) + create_channel.assert_called_with( + "apihub.googleapis.com:443", + credentials=file_creds, + credentials_file=None, + quota_project_id=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + scopes=None, + default_host="apihub.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.CreateDependencyRequest, + dict, + ], +) +def test_create_dependency(request_type, transport: str = "grpc"): + client = ApiHubDependenciesClient( + credentials=ga_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.create_dependency), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Dependency( + name="name_value", + state=common_fields.Dependency.State.PROPOSED, + description="description_value", + discovery_mode=common_fields.Dependency.DiscoveryMode.MANUAL, + ) + response = client.create_dependency(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.CreateDependencyRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Dependency) + assert response.name == "name_value" + assert response.state == common_fields.Dependency.State.PROPOSED + assert response.description == "description_value" + assert response.discovery_mode == common_fields.Dependency.DiscoveryMode.MANUAL + + +def test_create_dependency_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.CreateDependencyRequest( + parent="parent_value", + dependency_id="dependency_id_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_dependency), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.create_dependency(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.CreateDependencyRequest( + parent="parent_value", + dependency_id="dependency_id_value", + ) + + +def test_create_dependency_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1005,7 +1256,6 @@ def test_create_dependency_rest_use_cached_wrapped_rpc(): client._transport._wrapped_methods[ client._transport.create_dependency ] = mock_rpc - request = {} client.create_dependency(request) @@ -1019,156 +1269,259 @@ def test_create_dependency_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_create_dependency_rest_required_fields( - request_type=apihub_service.CreateDependencyRequest, +@pytest.mark.asyncio +async def test_create_dependency_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubDependenciesRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.create_dependency + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_dependency._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.create_dependency + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.create_dependency(request) - jsonified_request["parent"] = "parent_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_dependency._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("dependency_id",)) - jsonified_request.update(unset_fields) + await client.create_dependency(request) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + +@pytest.mark.asyncio +async def test_create_dependency_async( + transport: str = "grpc_asyncio", request_type=apihub_service.CreateDependencyRequest +): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = common_fields.Dependency() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # 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() - response_value = Response() - response_value.status_code = 200 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_dependency), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Dependency( + name="name_value", + state=common_fields.Dependency.State.PROPOSED, + description="description_value", + discovery_mode=common_fields.Dependency.DiscoveryMode.MANUAL, + ) + ) + response = await client.create_dependency(request) - # Convert return value to protobuf type - return_value = common_fields.Dependency.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.CreateDependencyRequest() + assert args[0] == request - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Dependency) + assert response.name == "name_value" + assert response.state == common_fields.Dependency.State.PROPOSED + assert response.description == "description_value" + assert response.discovery_mode == common_fields.Dependency.DiscoveryMode.MANUAL - response = client.create_dependency(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_create_dependency_async_from_dict(): + await test_create_dependency_async(request_type=dict) -def test_create_dependency_rest_unset_required_fields(): - transport = transports.ApiHubDependenciesRestTransport( - credentials=ga_credentials.AnonymousCredentials +def test_create_dependency_field_headers(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = transport.create_dependency._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(("dependencyId",)) - & set( - ( - "parent", - "dependency", - ) - ) + # 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 = apihub_service.CreateDependencyRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_dependency), "__call__" + ) as call: + call.return_value = common_fields.Dependency() + client.create_dependency(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", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_create_dependency_field_headers_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), ) + # 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 = apihub_service.CreateDependencyRequest() -def test_create_dependency_rest_flattened(): + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_dependency), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Dependency() + ) + await client.create_dependency(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", + "parent=parent_value", + ) in kw["metadata"] + + +def test_create_dependency_flattened(): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Dependency() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_dependency), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Dependency() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.create_dependency( + parent="parent_value", + dependency=common_fields.Dependency(name="name_value"), + dependency_id="dependency_id_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].dependency + mock_val = common_fields.Dependency(name="name_value") + assert arg == mock_val + arg = args[0].dependency_id + mock_val = "dependency_id_value" + assert arg == mock_val + + +def test_create_dependency_flattened_error(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + ) - # get truthy value for each flattened field - mock_args = dict( + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_dependency( + apihub_service.CreateDependencyRequest(), parent="parent_value", dependency=common_fields.Dependency(name="name_value"), dependency_id="dependency_id_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Dependency.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_dependency(**mock_args) +@pytest.mark.asyncio +async def test_create_dependency_flattened_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + ) - # Establish that the underlying call was made with the expected - # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/dependencies" - % client.transport._host, - args[1], - ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_dependency), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Dependency() + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Dependency() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.create_dependency( + parent="parent_value", + dependency=common_fields.Dependency(name="name_value"), + dependency_id="dependency_id_value", + ) -def test_create_dependency_rest_flattened_error(transport: str = "rest"): - client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].dependency + mock_val = common_fields.Dependency(name="name_value") + assert arg == mock_val + arg = args[0].dependency_id + mock_val = "dependency_id_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_create_dependency_flattened_error_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.create_dependency( + await client.create_dependency( apihub_service.CreateDependencyRequest(), parent="parent_value", dependency=common_fields.Dependency(name="name_value"), @@ -1176,13 +1529,83 @@ def test_create_dependency_rest_flattened_error(transport: str = "rest"): ) -def test_get_dependency_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.GetDependencyRequest, + dict, + ], +) +def test_get_dependency(request_type, transport: str = "grpc"): + client = ApiHubDependenciesClient( + credentials=ga_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.get_dependency), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Dependency( + name="name_value", + state=common_fields.Dependency.State.PROPOSED, + description="description_value", + discovery_mode=common_fields.Dependency.DiscoveryMode.MANUAL, + ) + response = client.get_dependency(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.GetDependencyRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Dependency) + assert response.name == "name_value" + assert response.state == common_fields.Dependency.State.PROPOSED + assert response.description == "description_value" + assert response.discovery_mode == common_fields.Dependency.DiscoveryMode.MANUAL + + +def test_get_dependency_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.GetDependencyRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_dependency), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_dependency(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.GetDependencyRequest( + name="name_value", + ) + + +def test_get_dependency_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1198,7 +1621,6 @@ def test_get_dependency_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.get_dependency] = mock_rpc - request = {} client.get_dependency(request) @@ -1212,139 +1634,181 @@ def test_get_dependency_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_get_dependency_rest_required_fields( - request_type=apihub_service.GetDependencyRequest, +@pytest.mark.asyncio +async def test_get_dependency_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubDependenciesRestTransport - - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) - - # verify fields with default values are dropped + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_dependency._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify required fields with default values are now present + # Ensure method has been cached + assert ( + client._client._transport.get_dependency + in client._client._transport._wrapped_methods + ) - jsonified_request["name"] = "name_value" + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.get_dependency + ] = mock_rpc - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_dependency._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + request = {} + await client.get_dependency(request) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type(**request_init) + await client.get_dependency(request) - # Designate an appropriate value for the returned response. - return_value = common_fields.Dependency() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Dependency.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) +@pytest.mark.asyncio +async def test_get_dependency_async( + transport: str = "grpc_asyncio", request_type=apihub_service.GetDependencyRequest +): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # 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() - response = client.get_dependency(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_dependency), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Dependency( + name="name_value", + state=common_fields.Dependency.State.PROPOSED, + description="description_value", + discovery_mode=common_fields.Dependency.DiscoveryMode.MANUAL, + ) + ) + response = await client.get_dependency(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.GetDependencyRequest() + assert args[0] == request + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Dependency) + assert response.name == "name_value" + assert response.state == common_fields.Dependency.State.PROPOSED + assert response.description == "description_value" + assert response.discovery_mode == common_fields.Dependency.DiscoveryMode.MANUAL -def test_get_dependency_rest_unset_required_fields(): - transport = transports.ApiHubDependenciesRestTransport( - credentials=ga_credentials.AnonymousCredentials - ) - unset_fields = transport.get_dependency._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) +@pytest.mark.asyncio +async def test_get_dependency_async_from_dict(): + await test_get_dependency_async(request_type=dict) -def test_get_dependency_rest_flattened(): +def test_get_dependency_field_headers(): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Dependency() + # 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 = apihub_service.GetDependencyRequest() - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/dependencies/sample3" - } + request.name = "name_value" - # get truthy value for each flattened field - mock_args = dict( - name="name_value", + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_dependency), "__call__") as call: + call.return_value = common_fields.Dependency() + client.get_dependency(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", + "name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_get_dependency_field_headers_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = apihub_service.GetDependencyRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_dependency), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Dependency() ) - mock_args.update(sample_request) + await client.get_dependency(request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Dependency.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request - client.get_dependency(**mock_args) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] + + +def test_get_dependency_flattened(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_dependency), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Dependency() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_dependency( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/dependencies/*}" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_get_dependency_rest_flattened_error(transport: str = "rest"): +def test_get_dependency_flattened_error(): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened @@ -1356,13 +1820,127 @@ def test_get_dependency_rest_flattened_error(transport: str = "rest"): ) -def test_update_dependency_rest_use_cached_wrapped_rpc(): +@pytest.mark.asyncio +async def test_get_dependency_flattened_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_dependency), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Dependency() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Dependency() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_dependency( + name="name_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_get_dependency_flattened_error_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.get_dependency( + apihub_service.GetDependencyRequest(), + name="name_value", + ) + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.UpdateDependencyRequest, + dict, + ], +) +def test_update_dependency(request_type, transport: str = "grpc"): + client = ApiHubDependenciesClient( + credentials=ga_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.update_dependency), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Dependency( + name="name_value", + state=common_fields.Dependency.State.PROPOSED, + description="description_value", + discovery_mode=common_fields.Dependency.DiscoveryMode.MANUAL, + ) + response = client.update_dependency(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.UpdateDependencyRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Dependency) + assert response.name == "name_value" + assert response.state == common_fields.Dependency.State.PROPOSED + assert response.description == "description_value" + assert response.discovery_mode == common_fields.Dependency.DiscoveryMode.MANUAL + + +def test_update_dependency_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.UpdateDependencyRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_dependency), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.update_dependency(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.UpdateDependencyRequest() + + +def test_update_dependency_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1380,7 +1958,6 @@ def test_update_dependency_rest_use_cached_wrapped_rpc(): client._transport._wrapped_methods[ client._transport.update_dependency ] = mock_rpc - request = {} client.update_dependency(request) @@ -1394,148 +1971,193 @@ def test_update_dependency_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_update_dependency_rest_required_fields( - request_type=apihub_service.UpdateDependencyRequest, +@pytest.mark.asyncio +async def test_update_dependency_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubDependenciesRestTransport - - request_init = {} - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - # verify fields with default values are dropped + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_dependency._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Ensure method has been cached + assert ( + client._client._transport.update_dependency + in client._client._transport._wrapped_methods + ) - # verify required fields with default values are now present + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.update_dependency + ] = mock_rpc - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_dependency._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("update_mask",)) - jsonified_request.update(unset_fields) + request = {} + await client.update_dependency(request) - # verify required fields with non-default values are left alone + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type(**request_init) + await client.update_dependency(request) - # Designate an appropriate value for the returned response. - return_value = common_fields.Dependency() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "patch", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Dependency.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) +@pytest.mark.asyncio +async def test_update_dependency_async( + transport: str = "grpc_asyncio", request_type=apihub_service.UpdateDependencyRequest +): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # 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() - response = client.update_dependency(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_dependency), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Dependency( + name="name_value", + state=common_fields.Dependency.State.PROPOSED, + description="description_value", + discovery_mode=common_fields.Dependency.DiscoveryMode.MANUAL, + ) + ) + response = await client.update_dependency(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.UpdateDependencyRequest() + assert args[0] == request + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Dependency) + assert response.name == "name_value" + assert response.state == common_fields.Dependency.State.PROPOSED + assert response.description == "description_value" + assert response.discovery_mode == common_fields.Dependency.DiscoveryMode.MANUAL -def test_update_dependency_rest_unset_required_fields(): - transport = transports.ApiHubDependenciesRestTransport( - credentials=ga_credentials.AnonymousCredentials - ) - unset_fields = transport.update_dependency._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(("updateMask",)) - & set( - ( - "dependency", - "updateMask", - ) - ) - ) +@pytest.mark.asyncio +async def test_update_dependency_async_from_dict(): + await test_update_dependency_async(request_type=dict) -def test_update_dependency_rest_flattened(): +def test_update_dependency_field_headers(): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.Dependency() + # 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 = apihub_service.UpdateDependencyRequest() - # get arguments that satisfy an http rule for this method - sample_request = { - "dependency": { - "name": "projects/sample1/locations/sample2/dependencies/sample3" - } - } + request.dependency.name = "name_value" - # get truthy value for each flattened field - mock_args = dict( - dependency=common_fields.Dependency(name="name_value"), - update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_dependency), "__call__" + ) as call: + call.return_value = common_fields.Dependency() + client.update_dependency(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", + "dependency.name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_update_dependency_field_headers_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = apihub_service.UpdateDependencyRequest() + + request.dependency.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_dependency), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Dependency() ) - mock_args.update(sample_request) + await client.update_dependency(request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.Dependency.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request - client.update_dependency(**mock_args) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "dependency.name=name_value", + ) in kw["metadata"] + + +def test_update_dependency_flattened(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_dependency), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Dependency() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.update_dependency( + dependency=common_fields.Dependency(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{dependency.name=projects/*/locations/*/dependencies/*}" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].dependency + mock_val = common_fields.Dependency(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val -def test_update_dependency_rest_flattened_error(transport: str = "rest"): +def test_update_dependency_flattened_error(): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened @@ -1548,13 +2170,129 @@ def test_update_dependency_rest_flattened_error(transport: str = "rest"): ) -def test_delete_dependency_rest_use_cached_wrapped_rpc(): +@pytest.mark.asyncio +async def test_update_dependency_flattened_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_dependency), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.Dependency() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Dependency() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.update_dependency( + dependency=common_fields.Dependency(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].dependency + mock_val = common_fields.Dependency(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_update_dependency_flattened_error_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.update_dependency( + apihub_service.UpdateDependencyRequest(), + dependency=common_fields.Dependency(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.DeleteDependencyRequest, + dict, + ], +) +def test_delete_dependency(request_type, transport: str = "grpc"): + client = ApiHubDependenciesClient( + credentials=ga_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.delete_dependency), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.delete_dependency(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.DeleteDependencyRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert response is None + + +def test_delete_dependency_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.DeleteDependencyRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_dependency), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.delete_dependency(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.DeleteDependencyRequest( + name="name_value", + ) + + +def test_delete_dependency_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1572,7 +2310,6 @@ def test_delete_dependency_rest_use_cached_wrapped_rpc(): client._transport._wrapped_methods[ client._transport.delete_dependency ] = mock_rpc - request = {} client.delete_dependency(request) @@ -1586,152 +2323,310 @@ def test_delete_dependency_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_delete_dependency_rest_required_fields( - request_type=apihub_service.DeleteDependencyRequest, +@pytest.mark.asyncio +async def test_delete_dependency_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubDependenciesRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.delete_dependency + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.delete_dependency + ] = mock_rpc + + request = {} + await client.delete_dependency(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.delete_dependency(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_delete_dependency_async( + transport: str = "grpc_asyncio", request_type=apihub_service.DeleteDependencyRequest +): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # verify fields with default values are dropped + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_dependency._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_dependency), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_dependency(request) - # verify required fields with default values are now present + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.DeleteDependencyRequest() + assert args[0] == request - jsonified_request["name"] = "name_value" + # Establish that the response is the type that we expect. + assert response is None - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_dependency._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" +@pytest.mark.asyncio +async def test_delete_dependency_async_from_dict(): + await test_delete_dependency_async(request_type=dict) + +def test_delete_dependency_field_headers(): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = None - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "delete", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # 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 = apihub_service.DeleteDependencyRequest() - response_value = Response() - response_value.status_code = 200 - json_return_value = "" + request.name = "name_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_dependency), "__call__" + ) as call: + call.return_value = None + client.delete_dependency(request) - response = client.delete_dependency(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] -def test_delete_dependency_rest_unset_required_fields(): - transport = transports.ApiHubDependenciesRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_delete_dependency_field_headers_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.delete_dependency._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # 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 = apihub_service.DeleteDependencyRequest() + request.name = "name_value" -def test_delete_dependency_rest_flattened(): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_dependency), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_dependency(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", + "name=name_value", + ) in kw["metadata"] + + +def test_delete_dependency_flattened(): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = None + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_dependency), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = None + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.delete_dependency( + name="name_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/dependencies/sample3" - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_delete_dependency_flattened_error(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_dependency( + apihub_service.DeleteDependencyRequest(), name="name_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - json_return_value = "" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_dependency(**mock_args) +@pytest.mark.asyncio +async def test_delete_dependency_flattened_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_dependency), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = None + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.delete_dependency( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/dependencies/*}" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_delete_dependency_rest_flattened_error(transport: str = "rest"): - client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_delete_dependency_flattened_error_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.delete_dependency( + await client.delete_dependency( apihub_service.DeleteDependencyRequest(), name="name_value", ) -def test_list_dependencies_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.ListDependenciesRequest, + dict, + ], +) +def test_list_dependencies(request_type, transport: str = "grpc"): + client = ApiHubDependenciesClient( + credentials=ga_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.list_dependencies), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListDependenciesResponse( + next_page_token="next_page_token_value", + ) + response = client.list_dependencies(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = apihub_service.ListDependenciesRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListDependenciesPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_dependencies_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = apihub_service.ListDependenciesRequest( + parent="parent_value", + filter="filter_value", + page_token="page_token_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_dependencies), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.list_dependencies(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == apihub_service.ListDependenciesRequest( + parent="parent_value", + filter="filter_value", + page_token="page_token_value", + ) + + +def test_list_dependencies_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1749,7 +2644,6 @@ def test_list_dependencies_rest_use_cached_wrapped_rpc(): client._transport._wrapped_methods[ client._transport.list_dependencies ] = mock_rpc - request = {} client.list_dependencies(request) @@ -1763,154 +2657,183 @@ def test_list_dependencies_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_list_dependencies_rest_required_fields( - request_type=apihub_service.ListDependenciesRequest, +@pytest.mark.asyncio +async def test_list_dependencies_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubDependenciesRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.list_dependencies + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_dependencies._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.list_dependencies + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.list_dependencies(request) - jsonified_request["parent"] = "parent_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_dependencies._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "filter", - "page_size", - "page_token", - ) + await client.list_dependencies(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_list_dependencies_async( + transport: str = "grpc_asyncio", request_type=apihub_service.ListDependenciesRequest +): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + # 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.list_dependencies), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListDependenciesResponse( + next_page_token="next_page_token_value", + ) + ) + response = await client.list_dependencies(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = apihub_service.ListDependenciesRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListDependenciesAsyncPager) + assert response.next_page_token == "next_page_token_value" + + +@pytest.mark.asyncio +async def test_list_dependencies_async_from_dict(): + await test_list_dependencies_async(request_type=dict) + +def test_list_dependencies_field_headers(): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = apihub_service.ListDependenciesResponse() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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 = apihub_service.ListDependenciesRequest() - # Convert return value to protobuf type - return_value = apihub_service.ListDependenciesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.parent = "parent_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_dependencies), "__call__" + ) as call: + call.return_value = apihub_service.ListDependenciesResponse() + client.list_dependencies(request) - response = client.list_dependencies(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] -def test_list_dependencies_rest_unset_required_fields(): - transport = transports.ApiHubDependenciesRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_list_dependencies_field_headers_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.list_dependencies._get_unset_required_fields({}) - assert set(unset_fields) == ( - set( - ( - "filter", - "pageSize", - "pageToken", - ) + # 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 = apihub_service.ListDependenciesRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_dependencies), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListDependenciesResponse() ) - & set(("parent",)) - ) + await client.list_dependencies(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", + "parent=parent_value", + ) in kw["metadata"] -def test_list_dependencies_rest_flattened(): + +def test_list_dependencies_flattened(): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = apihub_service.ListDependenciesResponse() - - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} - - # get truthy value for each flattened field - mock_args = dict( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_dependencies), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListDependenciesResponse() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.list_dependencies( parent="parent_value", ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = apihub_service.ListDependenciesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - - client.list_dependencies(**mock_args) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/dependencies" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val -def test_list_dependencies_rest_flattened_error(transport: str = "rest"): +def test_list_dependencies_flattened_error(): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened @@ -1922,18 +2845,64 @@ def test_list_dependencies_rest_flattened_error(transport: str = "rest"): ) -def test_list_dependencies_rest_pager(transport: str = "rest"): +@pytest.mark.asyncio +async def test_list_dependencies_flattened_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_dependencies), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = apihub_service.ListDependenciesResponse() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListDependenciesResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.list_dependencies( + parent="parent_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_list_dependencies_flattened_error_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.list_dependencies( + apihub_service.ListDependenciesRequest(), + parent="parent_value", + ) + + +def test_list_dependencies_pager(transport_name: str = "grpc"): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport=transport_name, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # TODO(kbandes): remove this mock unless there's a good reason for it. - # with mock.patch.object(path_template, 'transcode') as transcode: - # Set the response as a series of pages - response = ( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_dependencies), "__call__" + ) as call: + # Set the response to a series of pages. + call.side_effect = ( apihub_service.ListDependenciesResponse( dependencies=[ common_fields.Dependency(), @@ -1958,1775 +2927,4369 @@ def test_list_dependencies_rest_pager(transport: str = "rest"): common_fields.Dependency(), ], ), + RuntimeError, ) - # Two responses for two calls - response = response + response - # Wrap the values into proper Response objs - response = tuple( - apihub_service.ListDependenciesResponse.to_json(x) for x in response + expected_metadata = () + retry = retries.Retry() + timeout = 5 + expected_metadata = tuple(expected_metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", ""),)), ) - return_values = tuple(Response() for i in response) - for return_val, response_val in zip(return_values, response): - return_val._content = response_val.encode("UTF-8") - return_val.status_code = 200 - req.side_effect = return_values - - sample_request = {"parent": "projects/sample1/locations/sample2"} + pager = client.list_dependencies(request={}, retry=retry, timeout=timeout) - pager = client.list_dependencies(request=sample_request) + assert pager._metadata == expected_metadata + assert pager._retry == retry + assert pager._timeout == timeout results = list(pager) assert len(results) == 6 assert all(isinstance(i, common_fields.Dependency) for i in results) - pages = list(client.list_dependencies(request=sample_request).pages) - for page_, token in zip(pages, ["abc", "def", "ghi", ""]): - assert page_.raw_page.next_page_token == token - -def test_credentials_transport_error(): - # It is an error to provide credentials and a transport instance. - transport = transports.ApiHubDependenciesRestTransport( +def test_list_dependencies_pages(transport_name: str = "grpc"): + client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), + transport=transport_name, ) - with pytest.raises(ValueError): - client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_dependencies), "__call__" + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListDependenciesResponse( + dependencies=[ + common_fields.Dependency(), + common_fields.Dependency(), + common_fields.Dependency(), + ], + next_page_token="abc", + ), + apihub_service.ListDependenciesResponse( + dependencies=[], + next_page_token="def", + ), + apihub_service.ListDependenciesResponse( + dependencies=[ + common_fields.Dependency(), + ], + next_page_token="ghi", + ), + apihub_service.ListDependenciesResponse( + dependencies=[ + common_fields.Dependency(), + common_fields.Dependency(), + ], + ), + RuntimeError, ) + pages = list(client.list_dependencies(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token - # It is an error to provide a credentials file and a transport instance. - transport = transports.ApiHubDependenciesRestTransport( - credentials=ga_credentials.AnonymousCredentials(), + +@pytest.mark.asyncio +async def test_list_dependencies_async_pager(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), ) - with pytest.raises(ValueError): - client = ApiHubDependenciesClient( - client_options={"credentials_file": "credentials.json"}, - transport=transport, + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_dependencies), + "__call__", + new_callable=mock.AsyncMock, + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListDependenciesResponse( + dependencies=[ + common_fields.Dependency(), + common_fields.Dependency(), + common_fields.Dependency(), + ], + next_page_token="abc", + ), + apihub_service.ListDependenciesResponse( + dependencies=[], + next_page_token="def", + ), + apihub_service.ListDependenciesResponse( + dependencies=[ + common_fields.Dependency(), + ], + next_page_token="ghi", + ), + apihub_service.ListDependenciesResponse( + dependencies=[ + common_fields.Dependency(), + common_fields.Dependency(), + ], + ), + RuntimeError, ) + async_pager = await client.list_dependencies( + request={}, + ) + assert async_pager.next_page_token == "abc" + responses = [] + async for response in async_pager: # pragma: no branch + responses.append(response) - # It is an error to provide an api_key and a transport instance. - transport = transports.ApiHubDependenciesRestTransport( - credentials=ga_credentials.AnonymousCredentials(), + assert len(responses) == 6 + assert all(isinstance(i, common_fields.Dependency) for i in responses) + + +@pytest.mark.asyncio +async def test_list_dependencies_async_pages(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), ) - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = ApiHubDependenciesClient( - client_options=options, - transport=transport, - ) - # It is an error to provide an api_key and a credential. - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = ApiHubDependenciesClient( - client_options=options, credentials=ga_credentials.AnonymousCredentials() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_dependencies), + "__call__", + new_callable=mock.AsyncMock, + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + apihub_service.ListDependenciesResponse( + dependencies=[ + common_fields.Dependency(), + common_fields.Dependency(), + common_fields.Dependency(), + ], + next_page_token="abc", + ), + apihub_service.ListDependenciesResponse( + dependencies=[], + next_page_token="def", + ), + apihub_service.ListDependenciesResponse( + dependencies=[ + common_fields.Dependency(), + ], + next_page_token="ghi", + ), + apihub_service.ListDependenciesResponse( + dependencies=[ + common_fields.Dependency(), + common_fields.Dependency(), + ], + ), + RuntimeError, ) + pages = [] + # Workaround issue in python 3.9 related to code coverage by adding `# pragma: no branch` + # See https://github.com/googleapis/gapic-generator-python/pull/1174#issuecomment-1025132372 + async for page_ in ( # pragma: no branch + await client.list_dependencies(request={}) + ).pages: + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token - # It is an error to provide scopes and a transport instance. - transport = transports.ApiHubDependenciesRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - with pytest.raises(ValueError): + +def test_create_dependency_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubDependenciesClient( - client_options={"scopes": ["1", "2"]}, - transport=transport, + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.create_dependency in client._transport._wrapped_methods -def test_transport_instance(): - # A client may be instantiated with a custom transport instance. - transport = transports.ApiHubDependenciesRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - client = ApiHubDependenciesClient(transport=transport) - assert client.transport is transport + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.create_dependency + ] = mock_rpc + request = {} + client.create_dependency(request) -@pytest.mark.parametrize( - "transport_class", - [ - transports.ApiHubDependenciesRestTransport, - ], -) -def test_transport_adc(transport_class): - # Test default credentials are used if not provided. - with mock.patch.object(google.auth, "default") as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport_class() - adc.assert_called_once() + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + client.create_dependency(request) -def test_transport_kind_rest(): - transport = ApiHubDependenciesClient.get_transport_class("rest")( - credentials=ga_credentials.AnonymousCredentials() - ) - assert transport.kind == "rest" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 -def test_create_dependency_rest_bad_request( +def test_create_dependency_rest_required_fields( request_type=apihub_service.CreateDependencyRequest, ): - client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + transport_class = transports.ApiHubDependenciesRestTransport + + request_init = {} + request_init["parent"] = "" request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_dependency(request) + # verify fields with default values are dropped + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_dependency._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_dependency._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("dependency_id",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" -@pytest.mark.parametrize( - "request_type", - [ - apihub_service.CreateDependencyRequest, - dict, - ], -) -def test_create_dependency_rest_call_success(request_type): client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) + request = request_type(**request_init) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request_init["dependency"] = { - "name": "name_value", - "consumer": { - "operation_resource_name": "operation_resource_name_value", - "external_api_resource_name": "external_api_resource_name_value", - "display_name": "display_name_value", - }, - "supplier": {}, - "state": 1, - "description": "description_value", - "discovery_mode": 1, - "error_detail": {"error": 1, "error_time": {"seconds": 751, "nanos": 543}}, - "create_time": {}, - "update_time": {}, - "attributes": {}, - } - # The version of a generated dependency at test runtime may differ from the version used during generation. - # Delete any fields which are not present in the current runtime dependency - # See https://github.com/googleapis/gapic-generator-python/issues/1748 + # Designate an appropriate value for the returned response. + return_value = common_fields.Dependency() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result - # Determine if the message type is proto-plus or protobuf - test_field = apihub_service.CreateDependencyRequest.meta.fields["dependency"] + response_value = Response() + response_value.status_code = 200 - def get_message_fields(field): - # Given a field which is a message (composite type), return a list with - # all the fields of the message. - # If the field is not a composite type, return an empty list. - message_fields = [] + # Convert return value to protobuf type + return_value = common_fields.Dependency.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields + response = client.create_dependency(request) - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params - subfields_not_in_runtime = [] - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["dependency"].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value +def test_create_dependency_rest_unset_required_fields(): + transport = transports.ApiHubDependenciesRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) + unset_fields = transport.create_dependency._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("dependencyId",)) + & set( + ( + "parent", + "dependency", + ) + ) + ) - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range(0, len(request_init["dependency"][field])): - del request_init["dependency"][field][i][subfield] - else: - del request_init["dependency"][field][subfield] - request = request_type(**request_init) + +def test_create_dependency_rest_flattened(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = common_fields.Dependency( - name="name_value", - state=common_fields.Dependency.State.PROPOSED, - description="description_value", - discovery_mode=common_fields.Dependency.DiscoveryMode.MANUAL, + return_value = common_fields.Dependency() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + dependency=common_fields.Dependency(name="name_value"), + dependency_id="dependency_id_value", ) + mock_args.update(sample_request) # Wrap the value into a proper Response obj - response_value = mock.Mock() + response_value = Response() response_value.status_code = 200 - # Convert return value to protobuf type return_value = common_fields.Dependency.pb(return_value) json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.create_dependency(request) - # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.Dependency) - assert response.name == "name_value" - assert response.state == common_fields.Dependency.State.PROPOSED - assert response.description == "description_value" - assert response.discovery_mode == common_fields.Dependency.DiscoveryMode.MANUAL + client.create_dependency(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/dependencies" + % client.transport._host, + args[1], + ) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_create_dependency_rest_interceptors(null_interceptor): - transport = transports.ApiHubDependenciesRestTransport( +def test_create_dependency_rest_flattened_error(transport: str = "rest"): + client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubDependenciesRestInterceptor(), + transport=transport, ) - client = ApiHubDependenciesClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubDependenciesRestInterceptor, "post_create_dependency" - ) as post, mock.patch.object( - transports.ApiHubDependenciesRestInterceptor, - "post_create_dependency_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.ApiHubDependenciesRestInterceptor, "pre_create_dependency" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = apihub_service.CreateDependencyRequest.pb( - apihub_service.CreateDependencyRequest() + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_dependency( + apihub_service.CreateDependencyRequest(), + parent="parent_value", + dependency=common_fields.Dependency(name="name_value"), + dependency_id="dependency_id_value", ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.Dependency.to_json(common_fields.Dependency()) - req.return_value.content = return_value - request = apihub_service.CreateDependencyRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = common_fields.Dependency() - post_with_metadata.return_value = common_fields.Dependency(), metadata +def test_get_dependency_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) - client.create_dependency( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.get_dependency in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. ) + client._transport._wrapped_methods[client._transport.get_dependency] = mock_rpc - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + request = {} + client.get_dependency(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + client.get_dependency(request) -def test_get_dependency_rest_bad_request( + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_get_dependency_rest_required_fields( request_type=apihub_service.GetDependencyRequest, ): + transport_class = transports.ApiHubDependenciesRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_dependency._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_dependency._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/dependencies/sample3"} request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_dependency(request) + # Designate an appropriate value for the returned response. + return_value = common_fields.Dependency() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.Dependency.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) -@pytest.mark.parametrize( - "request_type", - [ - apihub_service.GetDependencyRequest, - dict, - ], -) -def test_get_dependency_rest_call_success(request_type): - client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_dependency(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_dependency_rest_unset_required_fields(): + transport = transports.ApiHubDependenciesRestTransport( + credentials=ga_credentials.AnonymousCredentials ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/dependencies/sample3"} - request = request_type(**request_init) + unset_fields = transport.get_dependency._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_get_dependency_rest_flattened(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = common_fields.Dependency( + return_value = common_fields.Dependency() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/dependencies/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( name="name_value", - state=common_fields.Dependency.State.PROPOSED, - description="description_value", - discovery_mode=common_fields.Dependency.DiscoveryMode.MANUAL, ) + mock_args.update(sample_request) # Wrap the value into a proper Response obj - response_value = mock.Mock() + response_value = Response() response_value.status_code = 200 - # Convert return value to protobuf type return_value = common_fields.Dependency.pb(return_value) json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_dependency(request) - # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.Dependency) - assert response.name == "name_value" - assert response.state == common_fields.Dependency.State.PROPOSED - assert response.description == "description_value" - assert response.discovery_mode == common_fields.Dependency.DiscoveryMode.MANUAL + client.get_dependency(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/dependencies/*}" + % client.transport._host, + args[1], + ) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_dependency_rest_interceptors(null_interceptor): - transport = transports.ApiHubDependenciesRestTransport( +def test_get_dependency_rest_flattened_error(transport: str = "rest"): + client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubDependenciesRestInterceptor(), + transport=transport, ) - client = ApiHubDependenciesClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubDependenciesRestInterceptor, "post_get_dependency" - ) as post, mock.patch.object( - transports.ApiHubDependenciesRestInterceptor, - "post_get_dependency_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.ApiHubDependenciesRestInterceptor, "pre_get_dependency" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = apihub_service.GetDependencyRequest.pb( - apihub_service.GetDependencyRequest() + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_dependency( + apihub_service.GetDependencyRequest(), + name="name_value", ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.Dependency.to_json(common_fields.Dependency()) - req.return_value.content = return_value - request = apihub_service.GetDependencyRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = common_fields.Dependency() - post_with_metadata.return_value = common_fields.Dependency(), metadata +def test_update_dependency_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) - client.get_dependency( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.update_dependency in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. ) + client._transport._wrapped_methods[ + client._transport.update_dependency + ] = mock_rpc - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + request = {} + client.update_dependency(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + client.update_dependency(request) -def test_update_dependency_rest_bad_request( + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_update_dependency_rest_required_fields( request_type=apihub_service.UpdateDependencyRequest, ): - client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = { - "dependency": { - "name": "projects/sample1/locations/sample2/dependencies/sample3" - } - } + transport_class = transports.ApiHubDependenciesRestTransport + + request_init = {} request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.update_dependency(request) + # verify fields with default values are dropped + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_dependency._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_dependency._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("update_mask",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone -@pytest.mark.parametrize( - "request_type", - [ - apihub_service.UpdateDependencyRequest, - dict, - ], -) -def test_update_dependency_rest_call_success(request_type): client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) + request = request_type(**request_init) - # send a request that will satisfy transcoding - request_init = { - "dependency": { - "name": "projects/sample1/locations/sample2/dependencies/sample3" - } - } - request_init["dependency"] = { - "name": "projects/sample1/locations/sample2/dependencies/sample3", - "consumer": { - "operation_resource_name": "operation_resource_name_value", - "external_api_resource_name": "external_api_resource_name_value", - "display_name": "display_name_value", - }, - "supplier": {}, - "state": 1, - "description": "description_value", - "discovery_mode": 1, - "error_detail": {"error": 1, "error_time": {"seconds": 751, "nanos": 543}}, - "create_time": {}, - "update_time": {}, - "attributes": {}, - } - # The version of a generated dependency at test runtime may differ from the version used during generation. - # Delete any fields which are not present in the current runtime dependency - # See https://github.com/googleapis/gapic-generator-python/issues/1748 + # Designate an appropriate value for the returned response. + return_value = common_fields.Dependency() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result - # Determine if the message type is proto-plus or protobuf - test_field = apihub_service.UpdateDependencyRequest.meta.fields["dependency"] + response_value = Response() + response_value.status_code = 200 - def get_message_fields(field): - # Given a field which is a message (composite type), return a list with - # all the fields of the message. - # If the field is not a composite type, return an empty list. - message_fields = [] + # Convert return value to protobuf type + return_value = common_fields.Dependency.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields + response = client.update_dependency(request) - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params - subfields_not_in_runtime = [] - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["dependency"].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value +def test_update_dependency_rest_unset_required_fields(): + transport = transports.ApiHubDependenciesRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) + unset_fields = transport.update_dependency._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("updateMask",)) + & set( + ( + "dependency", + "updateMask", + ) + ) + ) - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range(0, len(request_init["dependency"][field])): - del request_init["dependency"][field][i][subfield] - else: - del request_init["dependency"][field][subfield] - request = request_type(**request_init) + +def test_update_dependency_rest_flattened(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = common_fields.Dependency( - name="name_value", - state=common_fields.Dependency.State.PROPOSED, - description="description_value", - discovery_mode=common_fields.Dependency.DiscoveryMode.MANUAL, + return_value = common_fields.Dependency() + + # get arguments that satisfy an http rule for this method + sample_request = { + "dependency": { + "name": "projects/sample1/locations/sample2/dependencies/sample3" + } + } + + # get truthy value for each flattened field + mock_args = dict( + dependency=common_fields.Dependency(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) + mock_args.update(sample_request) # Wrap the value into a proper Response obj - response_value = mock.Mock() + response_value = Response() response_value.status_code = 200 - # Convert return value to protobuf type return_value = common_fields.Dependency.pb(return_value) json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.update_dependency(request) - # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.Dependency) - assert response.name == "name_value" - assert response.state == common_fields.Dependency.State.PROPOSED - assert response.description == "description_value" - assert response.discovery_mode == common_fields.Dependency.DiscoveryMode.MANUAL + client.update_dependency(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{dependency.name=projects/*/locations/*/dependencies/*}" + % client.transport._host, + args[1], + ) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_update_dependency_rest_interceptors(null_interceptor): - transport = transports.ApiHubDependenciesRestTransport( +def test_update_dependency_rest_flattened_error(transport: str = "rest"): + client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubDependenciesRestInterceptor(), + transport=transport, ) - client = ApiHubDependenciesClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubDependenciesRestInterceptor, "post_update_dependency" - ) as post, mock.patch.object( - transports.ApiHubDependenciesRestInterceptor, - "post_update_dependency_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.ApiHubDependenciesRestInterceptor, "pre_update_dependency" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = apihub_service.UpdateDependencyRequest.pb( - apihub_service.UpdateDependencyRequest() + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_dependency( + apihub_service.UpdateDependencyRequest(), + dependency=common_fields.Dependency(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.Dependency.to_json(common_fields.Dependency()) - req.return_value.content = return_value - request = apihub_service.UpdateDependencyRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = common_fields.Dependency() - post_with_metadata.return_value = common_fields.Dependency(), metadata +def test_delete_dependency_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) - client.update_dependency( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.delete_dependency in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. ) + client._transport._wrapped_methods[ + client._transport.delete_dependency + ] = mock_rpc - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + request = {} + client.delete_dependency(request) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 -def test_delete_dependency_rest_bad_request( + client.delete_dependency(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_delete_dependency_rest_required_fields( request_type=apihub_service.DeleteDependencyRequest, ): + transport_class = transports.ApiHubDependenciesRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_dependency._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_dependency._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/dependencies/sample3"} request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_dependency(request) + # Designate an appropriate value for the returned response. + return_value = None + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + response_value = Response() + response_value.status_code = 200 + json_return_value = "" -@pytest.mark.parametrize( - "request_type", - [ - apihub_service.DeleteDependencyRequest, - dict, - ], -) -def test_delete_dependency_rest_call_success(request_type): - client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_dependency(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_dependency_rest_unset_required_fields(): + transport = transports.ApiHubDependenciesRestTransport( + credentials=ga_credentials.AnonymousCredentials ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/dependencies/sample3"} - request = request_type(**request_init) + unset_fields = transport.delete_dependency._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_delete_dependency_rest_flattened(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. return_value = None + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/dependencies/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + # Wrap the value into a proper Response obj - response_value = mock.Mock() + response_value = Response() response_value.status_code = 200 json_return_value = "" - response_value.content = json_return_value.encode("UTF-8") + response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.delete_dependency(request) - # Establish that the response is the type that we expect. - assert response is None + client.delete_dependency(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/dependencies/*}" + % client.transport._host, + args[1], + ) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_delete_dependency_rest_interceptors(null_interceptor): - transport = transports.ApiHubDependenciesRestTransport( +def test_delete_dependency_rest_flattened_error(transport: str = "rest"): + client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubDependenciesRestInterceptor(), + transport=transport, ) - client = ApiHubDependenciesClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubDependenciesRestInterceptor, "pre_delete_dependency" - ) as pre: - pre.assert_not_called() - pb_message = apihub_service.DeleteDependencyRequest.pb( - apihub_service.DeleteDependencyRequest() + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_dependency( + apihub_service.DeleteDependencyRequest(), + name="name_value", ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - request = apihub_service.DeleteDependencyRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata +def test_list_dependencies_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) - client.delete_dependency( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.list_dependencies in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. ) + client._transport._wrapped_methods[ + client._transport.list_dependencies + ] = mock_rpc - pre.assert_called_once() + request = {} + client.list_dependencies(request) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 -def test_list_dependencies_rest_bad_request( + client.list_dependencies(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_list_dependencies_rest_required_fields( request_type=apihub_service.ListDependenciesRequest, ): + transport_class = transports.ApiHubDependenciesRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_dependencies._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_dependencies._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_dependencies(request) + # Designate an appropriate value for the returned response. + return_value = apihub_service.ListDependenciesResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + response_value = Response() + response_value.status_code = 200 -@pytest.mark.parametrize( - "request_type", - [ - apihub_service.ListDependenciesRequest, - dict, - ], -) -def test_list_dependencies_rest_call_success(request_type): - client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + # Convert return value to protobuf type + return_value = apihub_service.ListDependenciesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_dependencies(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_dependencies_rest_unset_required_fields(): + transport = transports.ApiHubDependenciesRestTransport( + credentials=ga_credentials.AnonymousCredentials ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request = request_type(**request_init) + unset_fields = transport.list_dependencies._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +def test_list_dependencies_rest_flattened(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = apihub_service.ListDependenciesResponse( - next_page_token="next_page_token_value", + return_value = apihub_service.ListDependenciesResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", ) + mock_args.update(sample_request) # Wrap the value into a proper Response obj - response_value = mock.Mock() + response_value = Response() response_value.status_code = 200 - # Convert return value to protobuf type return_value = apihub_service.ListDependenciesResponse.pb(return_value) json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.list_dependencies(request) - # Establish that the response is the type that we expect. - assert isinstance(response, pagers.ListDependenciesPager) - assert response.next_page_token == "next_page_token_value" + client.list_dependencies(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/dependencies" + % client.transport._host, + args[1], + ) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_list_dependencies_rest_interceptors(null_interceptor): - transport = transports.ApiHubDependenciesRestTransport( +def test_list_dependencies_rest_flattened_error(transport: str = "rest"): + client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubDependenciesRestInterceptor(), + transport=transport, ) - client = ApiHubDependenciesClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubDependenciesRestInterceptor, "post_list_dependencies" - ) as post, mock.patch.object( - transports.ApiHubDependenciesRestInterceptor, - "post_list_dependencies_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.ApiHubDependenciesRestInterceptor, "pre_list_dependencies" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = apihub_service.ListDependenciesRequest.pb( - apihub_service.ListDependenciesRequest() + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_dependencies( + apihub_service.ListDependenciesRequest(), + parent="parent_value", ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = apihub_service.ListDependenciesResponse.to_json( - apihub_service.ListDependenciesResponse() - ) - req.return_value.content = return_value - request = apihub_service.ListDependenciesRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = apihub_service.ListDependenciesResponse() - post_with_metadata.return_value = ( - apihub_service.ListDependenciesResponse(), - metadata, +def test_list_dependencies_rest_pager(transport: str = "rest"): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + apihub_service.ListDependenciesResponse( + dependencies=[ + common_fields.Dependency(), + common_fields.Dependency(), + common_fields.Dependency(), + ], + next_page_token="abc", + ), + apihub_service.ListDependenciesResponse( + dependencies=[], + next_page_token="def", + ), + apihub_service.ListDependenciesResponse( + dependencies=[ + common_fields.Dependency(), + ], + next_page_token="ghi", + ), + apihub_service.ListDependenciesResponse( + dependencies=[ + common_fields.Dependency(), + common_fields.Dependency(), + ], + ), ) + # Two responses for two calls + response = response + response - client.list_dependencies( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + # Wrap the values into proper Response objs + response = tuple( + apihub_service.ListDependenciesResponse.to_json(x) for x in response ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + sample_request = {"parent": "projects/sample1/locations/sample2"} + pager = client.list_dependencies(request=sample_request) -def test_get_location_rest_bad_request(request_type=locations_pb2.GetLocationRequest): - client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2"}, request - ) + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, common_fields.Dependency) for i in results) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_location(request) + pages = list(client.list_dependencies(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token -@pytest.mark.parametrize( - "request_type", - [ - locations_pb2.GetLocationRequest, - dict, - ], -) -def test_get_location_rest(request_type): - client = ApiHubDependenciesClient( +def test_credentials_transport_error(): + # It is an error to provide credentials and a transport instance. + transport = transports.ApiHubDependenciesGrpcTransport( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) + with pytest.raises(ValueError): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) - request_init = {"name": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = locations_pb2.Location() + # It is an error to provide a credentials file and a transport instance. + transport = transports.ApiHubDependenciesGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = ApiHubDependenciesClient( + client_options={"credentials_file": "credentials.json"}, + transport=transport, + ) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + # It is an error to provide an api_key and a transport instance. + transport = transports.ApiHubDependenciesGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = ApiHubDependenciesClient( + client_options=options, + transport=transport, + ) - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # It is an error to provide an api_key and a credential. + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = ApiHubDependenciesClient( + client_options=options, credentials=ga_credentials.AnonymousCredentials() + ) - response = client.get_location(request) + # It is an error to provide scopes and a transport instance. + transport = transports.ApiHubDependenciesGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = ApiHubDependenciesClient( + client_options={"scopes": ["1", "2"]}, + transport=transport, + ) - # Establish that the response is the type that we expect. - assert isinstance(response, locations_pb2.Location) +def test_transport_instance(): + # A client may be instantiated with a custom transport instance. + transport = transports.ApiHubDependenciesGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + client = ApiHubDependenciesClient(transport=transport) + assert client.transport is transport -def test_list_locations_rest_bad_request( - request_type=locations_pb2.ListLocationsRequest, -): - client = ApiHubDependenciesClient( + +def test_transport_get_channel(): + # A client may be instantiated with a custom transport instance. + transport = transports.ApiHubDependenciesGrpcTransport( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type() - request = json_format.ParseDict({"name": "projects/sample1"}, request) + channel = transport.grpc_channel + assert channel - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_locations(request) + transport = transports.ApiHubDependenciesGrpcAsyncIOTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel @pytest.mark.parametrize( - "request_type", + "transport_class", [ - locations_pb2.ListLocationsRequest, - dict, + transports.ApiHubDependenciesGrpcTransport, + transports.ApiHubDependenciesGrpcAsyncIOTransport, + transports.ApiHubDependenciesRestTransport, ], ) -def test_list_locations_rest(request_type): - client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +def test_transport_adc(transport_class): + # Test default credentials are used if not provided. + with mock.patch.object(google.auth, "default") as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class() + adc.assert_called_once() + + +def test_transport_kind_grpc(): + transport = ApiHubDependenciesClient.get_transport_class("grpc")( + credentials=ga_credentials.AnonymousCredentials() ) + assert transport.kind == "grpc" - request_init = {"name": "projects/sample1"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = locations_pb2.ListLocationsResponse() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") +def test_initialize_client_w_grpc(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + assert client is not None - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.list_locations(request) +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_dependency_empty_call_grpc(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) - # Establish that the response is the type that we expect. - assert isinstance(response, locations_pb2.ListLocationsResponse) + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_dependency), "__call__" + ) as call: + call.return_value = common_fields.Dependency() + client.create_dependency(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateDependencyRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_dependency_empty_call_grpc(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_dependency), "__call__") as call: + call.return_value = common_fields.Dependency() + client.get_dependency(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetDependencyRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_update_dependency_empty_call_grpc(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.update_dependency), "__call__" + ) as call: + call.return_value = common_fields.Dependency() + client.update_dependency(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateDependencyRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_dependency_empty_call_grpc(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.delete_dependency), "__call__" + ) as call: + call.return_value = None + client.delete_dependency(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteDependencyRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_dependencies_empty_call_grpc(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_dependencies), "__call__" + ) as call: + call.return_value = apihub_service.ListDependenciesResponse() + client.list_dependencies(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListDependenciesRequest() + + assert args[0] == request_msg + + +def test_transport_kind_grpc_asyncio(): + transport = ApiHubDependenciesAsyncClient.get_transport_class("grpc_asyncio")( + credentials=async_anonymous_credentials() + ) + assert transport.kind == "grpc_asyncio" + + +def test_initialize_client_w_grpc_asyncio(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_create_dependency_empty_call_grpc_asyncio(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_dependency), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Dependency( + name="name_value", + state=common_fields.Dependency.State.PROPOSED, + description="description_value", + discovery_mode=common_fields.Dependency.DiscoveryMode.MANUAL, + ) + ) + await client.create_dependency(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateDependencyRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_dependency_empty_call_grpc_asyncio(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_dependency), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Dependency( + name="name_value", + state=common_fields.Dependency.State.PROPOSED, + description="description_value", + discovery_mode=common_fields.Dependency.DiscoveryMode.MANUAL, + ) + ) + await client.get_dependency(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetDependencyRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_update_dependency_empty_call_grpc_asyncio(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.update_dependency), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.Dependency( + name="name_value", + state=common_fields.Dependency.State.PROPOSED, + description="description_value", + discovery_mode=common_fields.Dependency.DiscoveryMode.MANUAL, + ) + ) + await client.update_dependency(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateDependencyRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_delete_dependency_empty_call_grpc_asyncio(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.delete_dependency), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_dependency(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteDependencyRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_list_dependencies_empty_call_grpc_asyncio(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_dependencies), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + apihub_service.ListDependenciesResponse( + next_page_token="next_page_token_value", + ) + ) + await client.list_dependencies(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListDependenciesRequest() + + assert args[0] == request_msg + + +def test_transport_kind_rest(): + transport = ApiHubDependenciesClient.get_transport_class("rest")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "rest" + + +def test_create_dependency_rest_bad_request( + request_type=apihub_service.CreateDependencyRequest, +): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.create_dependency(request) + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.CreateDependencyRequest, + dict, + ], +) +def test_create_dependency_rest_call_success(request_type): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["dependency"] = { + "name": "name_value", + "consumer": { + "operation_resource_name": "operation_resource_name_value", + "external_api_resource_name": "external_api_resource_name_value", + "display_name": "display_name_value", + }, + "supplier": {}, + "state": 1, + "description": "description_value", + "discovery_mode": 1, + "error_detail": {"error": 1, "error_time": {"seconds": 751, "nanos": 543}}, + "create_time": {}, + "update_time": {}, + "attributes": {}, + } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = apihub_service.CreateDependencyRequest.meta.fields["dependency"] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["dependency"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["dependency"][field])): + del request_init["dependency"][field][i][subfield] + else: + del request_init["dependency"][field][subfield] + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Dependency( + name="name_value", + state=common_fields.Dependency.State.PROPOSED, + description="description_value", + discovery_mode=common_fields.Dependency.DiscoveryMode.MANUAL, + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Dependency.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.create_dependency(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Dependency) + assert response.name == "name_value" + assert response.state == common_fields.Dependency.State.PROPOSED + assert response.description == "description_value" + assert response.discovery_mode == common_fields.Dependency.DiscoveryMode.MANUAL + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_dependency_rest_interceptors(null_interceptor): + transport = transports.ApiHubDependenciesRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubDependenciesRestInterceptor(), + ) + client = ApiHubDependenciesClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubDependenciesRestInterceptor, "post_create_dependency" + ) as post, mock.patch.object( + transports.ApiHubDependenciesRestInterceptor, + "post_create_dependency_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.ApiHubDependenciesRestInterceptor, "pre_create_dependency" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = apihub_service.CreateDependencyRequest.pb( + apihub_service.CreateDependencyRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = common_fields.Dependency.to_json(common_fields.Dependency()) + req.return_value.content = return_value + + request = apihub_service.CreateDependencyRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = common_fields.Dependency() + post_with_metadata.return_value = common_fields.Dependency(), metadata + + client.create_dependency( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_get_dependency_rest_bad_request( + request_type=apihub_service.GetDependencyRequest, +): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/dependencies/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_dependency(request) + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.GetDependencyRequest, + dict, + ], +) +def test_get_dependency_rest_call_success(request_type): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/dependencies/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Dependency( + name="name_value", + state=common_fields.Dependency.State.PROPOSED, + description="description_value", + discovery_mode=common_fields.Dependency.DiscoveryMode.MANUAL, + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Dependency.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.get_dependency(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Dependency) + assert response.name == "name_value" + assert response.state == common_fields.Dependency.State.PROPOSED + assert response.description == "description_value" + assert response.discovery_mode == common_fields.Dependency.DiscoveryMode.MANUAL + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_dependency_rest_interceptors(null_interceptor): + transport = transports.ApiHubDependenciesRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubDependenciesRestInterceptor(), + ) + client = ApiHubDependenciesClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubDependenciesRestInterceptor, "post_get_dependency" + ) as post, mock.patch.object( + transports.ApiHubDependenciesRestInterceptor, + "post_get_dependency_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.ApiHubDependenciesRestInterceptor, "pre_get_dependency" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = apihub_service.GetDependencyRequest.pb( + apihub_service.GetDependencyRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = common_fields.Dependency.to_json(common_fields.Dependency()) + req.return_value.content = return_value + + request = apihub_service.GetDependencyRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = common_fields.Dependency() + post_with_metadata.return_value = common_fields.Dependency(), metadata + + client.get_dependency( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_update_dependency_rest_bad_request( + request_type=apihub_service.UpdateDependencyRequest, +): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "dependency": { + "name": "projects/sample1/locations/sample2/dependencies/sample3" + } + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.update_dependency(request) + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.UpdateDependencyRequest, + dict, + ], +) +def test_update_dependency_rest_call_success(request_type): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "dependency": { + "name": "projects/sample1/locations/sample2/dependencies/sample3" + } + } + request_init["dependency"] = { + "name": "projects/sample1/locations/sample2/dependencies/sample3", + "consumer": { + "operation_resource_name": "operation_resource_name_value", + "external_api_resource_name": "external_api_resource_name_value", + "display_name": "display_name_value", + }, + "supplier": {}, + "state": 1, + "description": "description_value", + "discovery_mode": 1, + "error_detail": {"error": 1, "error_time": {"seconds": 751, "nanos": 543}}, + "create_time": {}, + "update_time": {}, + "attributes": {}, + } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = apihub_service.UpdateDependencyRequest.meta.fields["dependency"] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["dependency"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["dependency"][field])): + del request_init["dependency"][field][i][subfield] + else: + del request_init["dependency"][field][subfield] + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.Dependency( + name="name_value", + state=common_fields.Dependency.State.PROPOSED, + description="description_value", + discovery_mode=common_fields.Dependency.DiscoveryMode.MANUAL, + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.Dependency.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.update_dependency(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.Dependency) + assert response.name == "name_value" + assert response.state == common_fields.Dependency.State.PROPOSED + assert response.description == "description_value" + assert response.discovery_mode == common_fields.Dependency.DiscoveryMode.MANUAL + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_update_dependency_rest_interceptors(null_interceptor): + transport = transports.ApiHubDependenciesRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubDependenciesRestInterceptor(), + ) + client = ApiHubDependenciesClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubDependenciesRestInterceptor, "post_update_dependency" + ) as post, mock.patch.object( + transports.ApiHubDependenciesRestInterceptor, + "post_update_dependency_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.ApiHubDependenciesRestInterceptor, "pre_update_dependency" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = apihub_service.UpdateDependencyRequest.pb( + apihub_service.UpdateDependencyRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = common_fields.Dependency.to_json(common_fields.Dependency()) + req.return_value.content = return_value + + request = apihub_service.UpdateDependencyRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = common_fields.Dependency() + post_with_metadata.return_value = common_fields.Dependency(), metadata + + client.update_dependency( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_delete_dependency_rest_bad_request( + request_type=apihub_service.DeleteDependencyRequest, +): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/dependencies/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.delete_dependency(request) + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.DeleteDependencyRequest, + dict, + ], +) +def test_delete_dependency_rest_call_success(request_type): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/dependencies/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "" + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.delete_dependency(request) + + # Establish that the response is the type that we expect. + assert response is None + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_dependency_rest_interceptors(null_interceptor): + transport = transports.ApiHubDependenciesRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubDependenciesRestInterceptor(), + ) + client = ApiHubDependenciesClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubDependenciesRestInterceptor, "pre_delete_dependency" + ) as pre: + pre.assert_not_called() + pb_message = apihub_service.DeleteDependencyRequest.pb( + apihub_service.DeleteDependencyRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + request = apihub_service.DeleteDependencyRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + + client.delete_dependency( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + + +def test_list_dependencies_rest_bad_request( + request_type=apihub_service.ListDependenciesRequest, +): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_dependencies(request) + + +@pytest.mark.parametrize( + "request_type", + [ + apihub_service.ListDependenciesRequest, + dict, + ], +) +def test_list_dependencies_rest_call_success(request_type): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = apihub_service.ListDependenciesResponse( + next_page_token="next_page_token_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = apihub_service.ListDependenciesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.list_dependencies(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListDependenciesPager) + assert response.next_page_token == "next_page_token_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_dependencies_rest_interceptors(null_interceptor): + transport = transports.ApiHubDependenciesRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubDependenciesRestInterceptor(), + ) + client = ApiHubDependenciesClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubDependenciesRestInterceptor, "post_list_dependencies" + ) as post, mock.patch.object( + transports.ApiHubDependenciesRestInterceptor, + "post_list_dependencies_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.ApiHubDependenciesRestInterceptor, "pre_list_dependencies" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = apihub_service.ListDependenciesRequest.pb( + apihub_service.ListDependenciesRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = apihub_service.ListDependenciesResponse.to_json( + apihub_service.ListDependenciesResponse() + ) + req.return_value.content = return_value + + request = apihub_service.ListDependenciesRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = apihub_service.ListDependenciesResponse() + post_with_metadata.return_value = ( + apihub_service.ListDependenciesResponse(), + metadata, + ) + + client.list_dependencies( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_get_location_rest_bad_request(request_type=locations_pb2.GetLocationRequest): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_location(request) + + +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.GetLocationRequest, + dict, + ], +) +def test_get_location_rest(request_type): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = locations_pb2.Location() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_location(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) + + +def test_list_locations_rest_bad_request( + request_type=locations_pb2.ListLocationsRequest, +): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict({"name": "projects/sample1"}, request) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_locations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.ListLocationsRequest, + dict, + ], +) +def test_list_locations_rest(request_type): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = locations_pb2.ListLocationsResponse() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_locations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + +def test_cancel_operation_rest_bad_request( + request_type=operations_pb2.CancelOperationRequest, +): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.cancel_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.CancelOperationRequest, + dict, + ], +) +def test_cancel_operation_rest(request_type): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "{}" + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.cancel_operation(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_delete_operation_rest_bad_request( + request_type=operations_pb2.DeleteOperationRequest, +): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.delete_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.DeleteOperationRequest, + dict, + ], +) +def test_delete_operation_rest(request_type): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "{}" + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_operation(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_get_operation_rest_bad_request( + request_type=operations_pb2.GetOperationRequest, +): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.GetOperationRequest, + dict, + ], +) +def test_get_operation_rest(request_type): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_operation(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) + + +def test_list_operations_rest_bad_request( + request_type=operations_pb2.ListOperationsRequest, +): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_operations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.ListOperationsRequest, + dict, + ], +) +def test_list_operations_rest(request_type): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.ListOperationsResponse() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_operations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) + + +def test_initialize_client_w_rest(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_dependency_empty_call_rest(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_dependency), "__call__" + ) as call: + client.create_dependency(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.CreateDependencyRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_dependency_empty_call_rest(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_dependency), "__call__") as call: + client.get_dependency(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.GetDependencyRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_update_dependency_empty_call_rest(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.update_dependency), "__call__" + ) as call: + client.update_dependency(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.UpdateDependencyRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_dependency_empty_call_rest(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.delete_dependency), "__call__" + ) as call: + client.delete_dependency(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.DeleteDependencyRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_dependencies_empty_call_rest(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_dependencies), "__call__" + ) as call: + client.list_dependencies(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = apihub_service.ListDependenciesRequest() + + assert args[0] == request_msg + + +def test_transport_grpc_default(): + # A client should use the gRPC transport by default. + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert isinstance( + client.transport, + transports.ApiHubDependenciesGrpcTransport, + ) + + +def test_api_hub_dependencies_base_transport_error(): + # Passing both a credentials object and credentials_file should raise an error + with pytest.raises(core_exceptions.DuplicateCredentialArgs): + transport = transports.ApiHubDependenciesTransport( + credentials=ga_credentials.AnonymousCredentials(), + credentials_file="credentials.json", + ) + + +def test_api_hub_dependencies_base_transport(): + # Instantiate the base transport. + with mock.patch( + "google.cloud.apihub_v1.services.api_hub_dependencies.transports.ApiHubDependenciesTransport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.ApiHubDependenciesTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Every method on the transport should just blindly + # raise NotImplementedError. + methods = ( + "create_dependency", + "get_dependency", + "update_dependency", + "delete_dependency", + "list_dependencies", + "get_location", + "list_locations", + "get_operation", + "cancel_operation", + "delete_operation", + "list_operations", + ) + for method in methods: + with pytest.raises(NotImplementedError): + getattr(transport, method)(request=object()) + + with pytest.raises(NotImplementedError): + transport.close() + + # Catch all for all remaining methods and properties + remainder = [ + "kind", + ] + for r in remainder: + with pytest.raises(NotImplementedError): + getattr(transport, r)() + + +def test_api_hub_dependencies_base_transport_with_credentials_file(): + # Instantiate the base transport with a credentials file + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch( + "google.cloud.apihub_v1.services.api_hub_dependencies.transports.ApiHubDependenciesTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) + transport = transports.ApiHubDependenciesTransport( + credentials_file="credentials.json", + quota_project_id="octopus", + ) + load_creds.assert_called_once_with( + "credentials.json", + scopes=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", + ) + + +def test_api_hub_dependencies_base_transport_with_adc(): + # Test the default credentials are used if credentials and credentials_file are None. + with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch( + "google.cloud.apihub_v1.services.api_hub_dependencies.transports.ApiHubDependenciesTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport = transports.ApiHubDependenciesTransport() + adc.assert_called_once() + + +def test_api_hub_dependencies_auth_adc(): + # If no credentials are provided, we should use ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + ApiHubDependenciesClient() + adc.assert_called_once_with( + scopes=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id=None, + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubDependenciesGrpcTransport, + transports.ApiHubDependenciesGrpcAsyncIOTransport, + ], +) +def test_api_hub_dependencies_transport_auth_adc(transport_class): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + adc.assert_called_once_with( + scopes=["1", "2"], + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubDependenciesGrpcTransport, + transports.ApiHubDependenciesGrpcAsyncIOTransport, + transports.ApiHubDependenciesRestTransport, + ], +) +def test_api_hub_dependencies_transport_auth_gdch_credentials(transport_class): + host = "https://language.com" + api_audience_tests = [None, "https://language2.com"] + api_audience_expect = [host, "https://language2.com"] + for t, e in zip(api_audience_tests, api_audience_expect): + with mock.patch.object(google.auth, "default", autospec=True) as adc: + gdch_mock = mock.MagicMock() + type(gdch_mock).with_gdch_audience = mock.PropertyMock( + return_value=gdch_mock + ) + adc.return_value = (gdch_mock, None) + transport_class(host=host, api_audience=t) + gdch_mock.with_gdch_audience.assert_called_once_with(e) + + +@pytest.mark.parametrize( + "transport_class,grpc_helpers", + [ + (transports.ApiHubDependenciesGrpcTransport, grpc_helpers), + (transports.ApiHubDependenciesGrpcAsyncIOTransport, grpc_helpers_async), + ], +) +def test_api_hub_dependencies_transport_create_channel(transport_class, grpc_helpers): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel", autospec=True + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + adc.return_value = (creds, None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + + create_channel.assert_called_with( + "apihub.googleapis.com:443", + credentials=creds, + credentials_file=None, + quota_project_id="octopus", + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + scopes=["1", "2"], + default_host="apihub.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubDependenciesGrpcTransport, + transports.ApiHubDependenciesGrpcAsyncIOTransport, + ], +) +def test_api_hub_dependencies_grpc_transport_client_cert_source_for_mtls( + transport_class, +): + cred = ga_credentials.AnonymousCredentials() + + # Check ssl_channel_credentials is used if provided. + with mock.patch.object(transport_class, "create_channel") as mock_create_channel: + mock_ssl_channel_creds = mock.Mock() + transport_class( + host="squid.clam.whelk", + credentials=cred, + ssl_channel_credentials=mock_ssl_channel_creds, + ) + mock_create_channel.assert_called_once_with( + "squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_channel_creds, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + # Check if ssl_channel_credentials is not provided, then client_cert_source_for_mtls + # is used. + with mock.patch.object(transport_class, "create_channel", return_value=mock.Mock()): + with mock.patch("grpc.ssl_channel_credentials") as mock_ssl_cred: + transport_class( + credentials=cred, + client_cert_source_for_mtls=client_cert_source_callback, + ) + expected_cert, expected_key = client_cert_source_callback() + mock_ssl_cred.assert_called_once_with( + certificate_chain=expected_cert, private_key=expected_key + ) + + +def test_api_hub_dependencies_http_transport_client_cert_source_for_mtls(): + cred = ga_credentials.AnonymousCredentials() + with mock.patch( + "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" + ) as mock_configure_mtls_channel: + transports.ApiHubDependenciesRestTransport( + credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + ) + mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + + +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "grpc_asyncio", + "rest", + ], +) +def test_api_hub_dependencies_host_no_port(transport_name): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="apihub.googleapis.com" + ), + transport=transport_name, + ) + assert client.transport._host == ( + "apihub.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://apihub.googleapis.com" + ) + + +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "grpc_asyncio", + "rest", + ], +) +def test_api_hub_dependencies_host_with_port(transport_name): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="apihub.googleapis.com:8000" + ), + transport=transport_name, + ) + assert client.transport._host == ( + "apihub.googleapis.com:8000" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://apihub.googleapis.com:8000" + ) + + +@pytest.mark.parametrize( + "transport_name", + [ + "rest", + ], +) +def test_api_hub_dependencies_client_transport_session_collision(transport_name): + creds1 = ga_credentials.AnonymousCredentials() + creds2 = ga_credentials.AnonymousCredentials() + client1 = ApiHubDependenciesClient( + credentials=creds1, + transport=transport_name, + ) + client2 = ApiHubDependenciesClient( + credentials=creds2, + transport=transport_name, + ) + session1 = client1.transport.create_dependency._session + session2 = client2.transport.create_dependency._session + assert session1 != session2 + session1 = client1.transport.get_dependency._session + session2 = client2.transport.get_dependency._session + assert session1 != session2 + session1 = client1.transport.update_dependency._session + session2 = client2.transport.update_dependency._session + assert session1 != session2 + session1 = client1.transport.delete_dependency._session + session2 = client2.transport.delete_dependency._session + assert session1 != session2 + session1 = client1.transport.list_dependencies._session + session2 = client2.transport.list_dependencies._session + assert session1 != session2 + + +def test_api_hub_dependencies_grpc_transport_channel(): + channel = grpc.secure_channel("http://localhost/", grpc.local_channel_credentials()) + + # Check that channel is used if provided. + transport = transports.ApiHubDependenciesGrpcTransport( + host="squid.clam.whelk", + channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None + + +def test_api_hub_dependencies_grpc_asyncio_transport_channel(): + channel = aio.secure_channel("http://localhost/", grpc.local_channel_credentials()) + + # Check that channel is used if provided. + transport = transports.ApiHubDependenciesGrpcAsyncIOTransport( + host="squid.clam.whelk", + channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None + + +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubDependenciesGrpcTransport, + transports.ApiHubDependenciesGrpcAsyncIOTransport, + ], +) +def test_api_hub_dependencies_transport_channel_mtls_with_client_cert_source( + transport_class, +): + with mock.patch( + "grpc.ssl_channel_credentials", autospec=True + ) as grpc_ssl_channel_cred: + with mock.patch.object( + transport_class, "create_channel" + ) 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 = ga_credentials.AnonymousCredentials() + with pytest.warns(DeprecationWarning): + with mock.patch.object(google.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=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + assert transport._ssl_channel_credentials == mock_ssl_cred + + +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubDependenciesGrpcTransport, + transports.ApiHubDependenciesGrpcAsyncIOTransport, + ], +) +def test_api_hub_dependencies_transport_channel_mtls_with_adc(transport_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), + ): + with mock.patch.object( + transport_class, "create_channel" + ) 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, + ) + + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=mock_cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + + +def test_attribute_path(): + project = "squid" + location = "clam" + attribute = "whelk" + expected = "projects/{project}/locations/{location}/attributes/{attribute}".format( + project=project, + location=location, + attribute=attribute, + ) + actual = ApiHubDependenciesClient.attribute_path(project, location, attribute) + assert expected == actual + + +def test_parse_attribute_path(): + expected = { + "project": "octopus", + "location": "oyster", + "attribute": "nudibranch", + } + path = ApiHubDependenciesClient.attribute_path(**expected) + + # Check that the path construction is reversible. + actual = ApiHubDependenciesClient.parse_attribute_path(path) + assert expected == actual + + +def test_dependency_path(): + project = "cuttlefish" + location = "mussel" + dependency = "winkle" + expected = ( + "projects/{project}/locations/{location}/dependencies/{dependency}".format( + project=project, + location=location, + dependency=dependency, + ) + ) + actual = ApiHubDependenciesClient.dependency_path(project, location, dependency) + assert expected == actual + + +def test_parse_dependency_path(): + expected = { + "project": "nautilus", + "location": "scallop", + "dependency": "abalone", + } + path = ApiHubDependenciesClient.dependency_path(**expected) + + # Check that the path construction is reversible. + actual = ApiHubDependenciesClient.parse_dependency_path(path) + assert expected == actual + + +def test_common_billing_account_path(): + billing_account = "squid" + expected = "billingAccounts/{billing_account}".format( + billing_account=billing_account, + ) + actual = ApiHubDependenciesClient.common_billing_account_path(billing_account) + assert expected == actual + + +def test_parse_common_billing_account_path(): + expected = { + "billing_account": "clam", + } + path = ApiHubDependenciesClient.common_billing_account_path(**expected) + + # Check that the path construction is reversible. + actual = ApiHubDependenciesClient.parse_common_billing_account_path(path) + assert expected == actual + + +def test_common_folder_path(): + folder = "whelk" + expected = "folders/{folder}".format( + folder=folder, + ) + actual = ApiHubDependenciesClient.common_folder_path(folder) + assert expected == actual + + +def test_parse_common_folder_path(): + expected = { + "folder": "octopus", + } + path = ApiHubDependenciesClient.common_folder_path(**expected) + + # Check that the path construction is reversible. + actual = ApiHubDependenciesClient.parse_common_folder_path(path) + assert expected == actual + + +def test_common_organization_path(): + organization = "oyster" + expected = "organizations/{organization}".format( + organization=organization, + ) + actual = ApiHubDependenciesClient.common_organization_path(organization) + assert expected == actual + + +def test_parse_common_organization_path(): + expected = { + "organization": "nudibranch", + } + path = ApiHubDependenciesClient.common_organization_path(**expected) + + # Check that the path construction is reversible. + actual = ApiHubDependenciesClient.parse_common_organization_path(path) + assert expected == actual + + +def test_common_project_path(): + project = "cuttlefish" + expected = "projects/{project}".format( + project=project, + ) + actual = ApiHubDependenciesClient.common_project_path(project) + assert expected == actual + + +def test_parse_common_project_path(): + expected = { + "project": "mussel", + } + path = ApiHubDependenciesClient.common_project_path(**expected) + + # Check that the path construction is reversible. + actual = ApiHubDependenciesClient.parse_common_project_path(path) + assert expected == actual + + +def test_common_location_path(): + project = "winkle" + location = "nautilus" + expected = "projects/{project}/locations/{location}".format( + project=project, + location=location, + ) + actual = ApiHubDependenciesClient.common_location_path(project, location) + assert expected == actual + + +def test_parse_common_location_path(): + expected = { + "project": "scallop", + "location": "abalone", + } + path = ApiHubDependenciesClient.common_location_path(**expected) + + # Check that the path construction is reversible. + actual = ApiHubDependenciesClient.parse_common_location_path(path) + assert expected == actual + + +def test_client_with_default_client_info(): + client_info = gapic_v1.client_info.ClientInfo() + + with mock.patch.object( + transports.ApiHubDependenciesTransport, "_prep_wrapped_messages" + ) as prep: + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + client_info=client_info, + ) + prep.assert_called_once_with(client_info) + with mock.patch.object( + transports.ApiHubDependenciesTransport, "_prep_wrapped_messages" + ) as prep: + transport_class = ApiHubDependenciesClient.get_transport_class() + transport = transport_class( + credentials=ga_credentials.AnonymousCredentials(), + client_info=client_info, + ) + prep.assert_called_once_with(client_info) -def test_cancel_operation_rest_bad_request( - request_type=operations_pb2.CancelOperationRequest, -): + +def test_delete_operation(transport: str = "grpc"): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + transport=transport, ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.cancel_operation(request) + # 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 = operations_pb2.DeleteOperationRequest() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.delete_operation(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 -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.CancelOperationRequest, - dict, - ], -) -def test_cancel_operation_rest(request_type): - client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + # Establish that the response is the type that we expect. + assert response is None - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = None - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "{}" - response_value.content = json_return_value.encode("UTF-8") +@pytest.mark.asyncio +async def test_delete_operation_async(transport: str = "grpc_asyncio"): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # 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 = operations_pb2.DeleteOperationRequest() - response = client.cancel_operation(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_operation(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 response is the type that we expect. assert response is None -def test_delete_operation_rest_bad_request( - request_type=operations_pb2.DeleteOperationRequest, -): +def test_delete_operation_field_headers(): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_operation(request) + # 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 = operations_pb2.DeleteOperationRequest() + request.name = "locations" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + call.return_value = None -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.DeleteOperationRequest, - dict, - ], -) -def test_delete_operation_rest(request_type): - client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + client.delete_operation(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 - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = None + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "{}" - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +@pytest.mark.asyncio +async def test_delete_operation_field_headers_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + ) - response = client.delete_operation(request) + # 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 = operations_pb2.DeleteOperationRequest() + request.name = "locations" - # Establish that the response is the type that we expect. - assert response is None + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_operation(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", + "name=locations", + ) in kw["metadata"] -def test_get_operation_rest_bad_request( - request_type=operations_pb2.GetOperationRequest, -): +def test_delete_operation_from_dict(): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + + response = client.delete_operation( + request={ + "name": "locations", + } + ) + call.assert_called() - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_operation(request) +@pytest.mark.asyncio +async def test_delete_operation_from_dict_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.GetOperationRequest, - dict, - ], -) -def test_get_operation_rest(request_type): + +def test_cancel_operation(transport: str = "grpc"): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation() + # 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 = operations_pb2.CancelOperationRequest() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.cancel_operation(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 - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert response is None - response = client.get_operation(request) + +@pytest.mark.asyncio +async def test_cancel_operation_async(transport: str = "grpc_asyncio"): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.CancelOperationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.cancel_operation(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 response is the type that we expect. - assert isinstance(response, operations_pb2.Operation) + assert response is None -def test_list_operations_rest_bad_request( - request_type=operations_pb2.ListOperationsRequest, -): +def test_cancel_operation_field_headers(): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2"}, request ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_operations(request) + # 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 = operations_pb2.CancelOperationRequest() + request.name = "locations" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + call.return_value = None -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.ListOperationsRequest, - dict, - ], -) -def test_list_operations_rest(request_type): - client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + client.cancel_operation(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", + "name=locations", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_cancel_operation_field_headers_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), ) - request_init = {"name": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.ListOperationsResponse() + # 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 = operations_pb2.CancelOperationRequest() + request.name = "locations" - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.cancel_operation(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 - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] - response = client.list_operations(request) - # Establish that the response is the type that we expect. - assert isinstance(response, operations_pb2.ListOperationsResponse) +def test_cancel_operation_from_dict(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + + response = client.cancel_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_initialize_client_w_rest(): - client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" +@pytest.mark.asyncio +async def test_cancel_operation_from_dict_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), ) - assert client is not None + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.cancel_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_create_dependency_empty_call_rest(): +def test_get_operation(transport: str = "grpc"): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.create_dependency), "__call__" - ) as call: - client.create_dependency(request=None) + # 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 = operations_pb2.GetOperationRequest() - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation() + response = client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = apihub_service.CreateDependencyRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_get_dependency_empty_call_rest(): - client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +@pytest.mark.asyncio +async def test_get_operation_async(transport: str = "grpc_asyncio"): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.get_dependency), "__call__") as call: - client.get_dependency(request=None) + # 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 = operations_pb2.GetOperationRequest() - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + response = await client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = apihub_service.GetDependencyRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_update_dependency_empty_call_rest(): +def test_get_operation_field_headers(): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.update_dependency), "__call__" - ) as call: - client.update_dependency(request=None) + # 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 = operations_pb2.GetOperationRequest() + request.name = "locations" - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + call.return_value = operations_pb2.Operation() + + client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = apihub_service.UpdateDependencyRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_delete_dependency_empty_call_rest(): - client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +@pytest.mark.asyncio +async def test_get_operation_field_headers_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.delete_dependency), "__call__" - ) as call: - client.delete_dependency(request=None) + # 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 = operations_pb2.GetOperationRequest() + request.name = "locations" - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + await client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = apihub_service.DeleteDependencyRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_list_dependencies_empty_call_rest(): +def test_get_operation_from_dict(): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation() + + response = client.get_operation( + request={ + "name": "locations", + } + ) + call.assert_called() - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.list_dependencies), "__call__" - ) as call: - client.list_dependencies(request=None) - # Establish that the underlying stub method was called. +@pytest.mark.asyncio +async def test_get_operation_from_dict_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + response = await client.get_operation( + request={ + "name": "locations", + } + ) call.assert_called() + + +def test_list_operations(transport: str = "grpc"): + client = ApiHubDependenciesClient( + credentials=ga_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 = operations_pb2.ListOperationsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.ListOperationsResponse() + response = client.list_operations(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = apihub_service.ListDependenciesRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) -def test_api_hub_dependencies_base_transport_error(): - # Passing both a credentials object and credentials_file should raise an error - with pytest.raises(core_exceptions.DuplicateCredentialArgs): - transport = transports.ApiHubDependenciesTransport( - credentials=ga_credentials.AnonymousCredentials(), - credentials_file="credentials.json", - ) +@pytest.mark.asyncio +async def test_list_operations_async(transport: str = "grpc_asyncio"): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.ListOperationsRequest() -def test_api_hub_dependencies_base_transport(): - # Instantiate the base transport. - with mock.patch( - "google.cloud.apihub_v1.services.api_hub_dependencies.transports.ApiHubDependenciesTransport.__init__" - ) as Transport: - Transport.return_value = None - transport = transports.ApiHubDependenciesTransport( - credentials=ga_credentials.AnonymousCredentials(), + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() ) + response = await client.list_operations(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 - # Every method on the transport should just blindly - # raise NotImplementedError. - methods = ( - "create_dependency", - "get_dependency", - "update_dependency", - "delete_dependency", - "list_dependencies", - "get_location", - "list_locations", - "get_operation", - "cancel_operation", - "delete_operation", - "list_operations", + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) + + +def test_list_operations_field_headers(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), ) - for method in methods: - with pytest.raises(NotImplementedError): - getattr(transport, method)(request=object()) - with pytest.raises(NotImplementedError): - transport.close() + # 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 = operations_pb2.ListOperationsRequest() + request.name = "locations" - # Catch all for all remaining methods and properties - remainder = [ - "kind", - ] - for r in remainder: - with pytest.raises(NotImplementedError): - getattr(transport, r)() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + call.return_value = operations_pb2.ListOperationsResponse() + client.list_operations(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 -def test_api_hub_dependencies_base_transport_with_credentials_file(): - # Instantiate the base transport with a credentials file - with mock.patch.object( - google.auth, "load_credentials_from_file", autospec=True - ) as load_creds, mock.patch( - "google.cloud.apihub_v1.services.api_hub_dependencies.transports.ApiHubDependenciesTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None - load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) - transport = transports.ApiHubDependenciesTransport( - credentials_file="credentials.json", - quota_project_id="octopus", - ) - load_creds.assert_called_once_with( - "credentials.json", - scopes=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id="octopus", - ) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -def test_api_hub_dependencies_base_transport_with_adc(): - # Test the default credentials are used if credentials and credentials_file are None. - with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch( - "google.cloud.apihub_v1.services.api_hub_dependencies.transports.ApiHubDependenciesTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport = transports.ApiHubDependenciesTransport() - adc.assert_called_once() +@pytest.mark.asyncio +async def test_list_operations_field_headers_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + ) + # 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 = operations_pb2.ListOperationsRequest() + request.name = "locations" -def test_api_hub_dependencies_auth_adc(): - # If no credentials are provided, we should use ADC credentials. - with mock.patch.object(google.auth, "default", autospec=True) as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - ApiHubDependenciesClient() - adc.assert_called_once_with( - scopes=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id=None, + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() ) + await client.list_operations(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", + "name=locations", + ) in kw["metadata"] -def test_api_hub_dependencies_http_transport_client_cert_source_for_mtls(): - cred = ga_credentials.AnonymousCredentials() - with mock.patch( - "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" - ) as mock_configure_mtls_channel: - transports.ApiHubDependenciesRestTransport( - credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + +def test_list_operations_from_dict(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.ListOperationsResponse() + + response = client.list_operations( + request={ + "name": "locations", + } ) - mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + call.assert_called() -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_api_hub_dependencies_host_no_port(transport_name): +@pytest.mark.asyncio +async def test_list_operations_from_dict_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() + ) + response = await client.list_operations( + request={ + "name": "locations", + } + ) + call.assert_called() + + +def test_list_locations(transport: str = "grpc"): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - client_options=client_options.ClientOptions( - api_endpoint="apihub.googleapis.com" - ), - transport=transport_name, + transport=transport, ) - assert client.transport._host == ( - "apihub.googleapis.com:443" - if transport_name in ["grpc", "grpc_asyncio"] - else "https://apihub.googleapis.com" + + # 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 = locations_pb2.ListLocationsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.ListLocationsResponse() + response = client.list_locations(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 response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + +@pytest.mark.asyncio +async def test_list_locations_async(transport: str = "grpc_asyncio"): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + 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 = locations_pb2.ListLocationsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + response = await client.list_locations(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 response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_api_hub_dependencies_host_with_port(transport_name): + +def test_list_locations_field_headers(): client = ApiHubDependenciesClient( credentials=ga_credentials.AnonymousCredentials(), - client_options=client_options.ClientOptions( - api_endpoint="apihub.googleapis.com:8000" - ), - transport=transport_name, - ) - assert client.transport._host == ( - "apihub.googleapis.com:8000" - if transport_name in ["grpc", "grpc_asyncio"] - else "https://apihub.googleapis.com:8000" ) + # 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 = locations_pb2.ListLocationsRequest() + request.name = "locations" -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_api_hub_dependencies_client_transport_session_collision(transport_name): - creds1 = ga_credentials.AnonymousCredentials() - creds2 = ga_credentials.AnonymousCredentials() - client1 = ApiHubDependenciesClient( - credentials=creds1, - transport=transport_name, - ) - client2 = ApiHubDependenciesClient( - credentials=creds2, - transport=transport_name, - ) - session1 = client1.transport.create_dependency._session - session2 = client2.transport.create_dependency._session - assert session1 != session2 - session1 = client1.transport.get_dependency._session - session2 = client2.transport.get_dependency._session - assert session1 != session2 - session1 = client1.transport.update_dependency._session - session2 = client2.transport.update_dependency._session - assert session1 != session2 - session1 = client1.transport.delete_dependency._session - session2 = client2.transport.delete_dependency._session - assert session1 != session2 - session1 = client1.transport.list_dependencies._session - session2 = client2.transport.list_dependencies._session - assert session1 != session2 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + call.return_value = locations_pb2.ListLocationsResponse() + client.list_locations(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 -def test_attribute_path(): - project = "squid" - location = "clam" - attribute = "whelk" - expected = "projects/{project}/locations/{location}/attributes/{attribute}".format( - project=project, - location=location, - attribute=attribute, + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_list_locations_field_headers_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), ) - actual = ApiHubDependenciesClient.attribute_path(project, location, attribute) - assert expected == actual + # 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 = locations_pb2.ListLocationsRequest() + request.name = "locations" -def test_parse_attribute_path(): - expected = { - "project": "octopus", - "location": "oyster", - "attribute": "nudibranch", - } - path = ApiHubDependenciesClient.attribute_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + await client.list_locations(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 - # Check that the path construction is reversible. - actual = ApiHubDependenciesClient.parse_attribute_path(path) - assert expected == actual + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -def test_dependency_path(): - project = "cuttlefish" - location = "mussel" - dependency = "winkle" - expected = ( - "projects/{project}/locations/{location}/dependencies/{dependency}".format( - project=project, - location=location, - dependency=dependency, - ) +def test_list_locations_from_dict(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), ) - actual = ApiHubDependenciesClient.dependency_path(project, location, dependency) - assert expected == actual - + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.ListLocationsResponse() + + response = client.list_locations( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_parse_dependency_path(): - expected = { - "project": "nautilus", - "location": "scallop", - "dependency": "abalone", - } - path = ApiHubDependenciesClient.dependency_path(**expected) - # Check that the path construction is reversible. - actual = ApiHubDependenciesClient.parse_dependency_path(path) - assert expected == actual +@pytest.mark.asyncio +async def test_list_locations_from_dict_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + response = await client.list_locations( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_common_billing_account_path(): - billing_account = "squid" - expected = "billingAccounts/{billing_account}".format( - billing_account=billing_account, +def test_get_location(transport: str = "grpc"): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - actual = ApiHubDependenciesClient.common_billing_account_path(billing_account) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() -def test_parse_common_billing_account_path(): - expected = { - "billing_account": "clam", - } - path = ApiHubDependenciesClient.common_billing_account_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.Location() + response = client.get_location(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 - # Check that the path construction is reversible. - actual = ApiHubDependenciesClient.parse_common_billing_account_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) -def test_common_folder_path(): - folder = "whelk" - expected = "folders/{folder}".format( - folder=folder, +@pytest.mark.asyncio +async def test_get_location_async(transport: str = "grpc_asyncio"): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - actual = ApiHubDependenciesClient.common_folder_path(folder) - assert expected == actual - -def test_parse_common_folder_path(): - expected = { - "folder": "octopus", - } - path = ApiHubDependenciesClient.common_folder_path(**expected) + # 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 = locations_pb2.GetLocationRequest() - # Check that the path construction is reversible. - actual = ApiHubDependenciesClient.parse_common_folder_path(path) - assert expected == actual + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + response = await client.get_location(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 response is the type that we expect. + assert isinstance(response, locations_pb2.Location) -def test_common_organization_path(): - organization = "oyster" - expected = "organizations/{organization}".format( - organization=organization, - ) - actual = ApiHubDependenciesClient.common_organization_path(organization) - assert expected == actual +def test_get_location_field_headers(): + client = ApiHubDependenciesClient(credentials=ga_credentials.AnonymousCredentials()) -def test_parse_common_organization_path(): - expected = { - "organization": "nudibranch", - } - path = ApiHubDependenciesClient.common_organization_path(**expected) + # 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 = locations_pb2.GetLocationRequest() + request.name = "locations/abc" - # Check that the path construction is reversible. - actual = ApiHubDependenciesClient.parse_common_organization_path(path) - assert expected == actual + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + call.return_value = locations_pb2.Location() + client.get_location(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 -def test_common_project_path(): - project = "cuttlefish" - expected = "projects/{project}".format( - project=project, - ) - actual = ApiHubDependenciesClient.common_project_path(project) - assert expected == actual + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations/abc", + ) in kw["metadata"] -def test_parse_common_project_path(): - expected = { - "project": "mussel", - } - path = ApiHubDependenciesClient.common_project_path(**expected) +@pytest.mark.asyncio +async def test_get_location_field_headers_async(): + client = ApiHubDependenciesAsyncClient(credentials=async_anonymous_credentials()) - # Check that the path construction is reversible. - actual = ApiHubDependenciesClient.parse_common_project_path(path) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() + request.name = "locations/abc" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + await client.get_location(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 -def test_common_location_path(): - project = "winkle" - location = "nautilus" - expected = "projects/{project}/locations/{location}".format( - project=project, - location=location, - ) - actual = ApiHubDependenciesClient.common_location_path(project, location) - assert expected == actual + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations/abc", + ) in kw["metadata"] -def test_parse_common_location_path(): - expected = { - "project": "scallop", - "location": "abalone", - } - path = ApiHubDependenciesClient.common_location_path(**expected) +def test_get_location_from_dict(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.Location() + + response = client.get_location( + request={ + "name": "locations/abc", + } + ) + call.assert_called() - # Check that the path construction is reversible. - actual = ApiHubDependenciesClient.parse_common_location_path(path) - assert expected == actual +@pytest.mark.asyncio +async def test_get_location_from_dict_async(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + response = await client.get_location( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_client_with_default_client_info(): - client_info = gapic_v1.client_info.ClientInfo() +def test_transport_close_grpc(): + client = ApiHubDependenciesClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) with mock.patch.object( - transports.ApiHubDependenciesTransport, "_prep_wrapped_messages" - ) as prep: - client = ApiHubDependenciesClient( - credentials=ga_credentials.AnonymousCredentials(), - client_info=client_info, - ) - prep.assert_called_once_with(client_info) + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + +@pytest.mark.asyncio +async def test_transport_close_grpc_asyncio(): + client = ApiHubDependenciesAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) with mock.patch.object( - transports.ApiHubDependenciesTransport, "_prep_wrapped_messages" - ) as prep: - transport_class = ApiHubDependenciesClient.get_transport_class() - transport = transport_class( - credentials=ga_credentials.AnonymousCredentials(), - client_info=client_info, - ) - prep.assert_called_once_with(client_info) + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + async with client: + close.assert_not_called() + close.assert_called_once() def test_transport_close_rest(): @@ -3744,6 +7307,7 @@ def test_transport_close_rest(): def test_client_ctx(): transports = [ "rest", + "grpc", ] for transport in transports: client = ApiHubDependenciesClient( @@ -3760,7 +7324,11 @@ def test_client_ctx(): @pytest.mark.parametrize( "client_class,transport_class", [ - (ApiHubDependenciesClient, transports.ApiHubDependenciesRestTransport), + (ApiHubDependenciesClient, transports.ApiHubDependenciesGrpcTransport), + ( + ApiHubDependenciesAsyncClient, + transports.ApiHubDependenciesGrpcAsyncIOTransport, + ), ], ) def test_api_key_credentials(client_class, transport_class): diff --git a/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub_discovery.py b/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub_discovery.py index 18783c0d3a88..6fc37a362e15 100644 --- a/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub_discovery.py +++ b/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub_discovery.py @@ -56,6 +56,7 @@ from google.protobuf import timestamp_pb2 # type: ignore from google.cloud.apihub_v1.services.api_hub_discovery import ( + ApiHubDiscoveryAsyncClient, ApiHubDiscoveryClient, pagers, transports, @@ -241,6 +242,11 @@ def test__get_client_cert_source(): "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubDiscoveryClient), ) +@mock.patch.object( + ApiHubDiscoveryAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubDiscoveryAsyncClient), +) def test__get_api_endpoint(): api_override = "foo.com" mock_client_cert_source = mock.Mock() @@ -368,6 +374,8 @@ def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code): @pytest.mark.parametrize( "client_class,transport_name", [ + (ApiHubDiscoveryClient, "grpc"), + (ApiHubDiscoveryAsyncClient, "grpc_asyncio"), (ApiHubDiscoveryClient, "rest"), ], ) @@ -394,6 +402,8 @@ def test_api_hub_discovery_client_from_service_account_info( @pytest.mark.parametrize( "transport_class,transport_name", [ + (transports.ApiHubDiscoveryGrpcTransport, "grpc"), + (transports.ApiHubDiscoveryGrpcAsyncIOTransport, "grpc_asyncio"), (transports.ApiHubDiscoveryRestTransport, "rest"), ], ) @@ -418,6 +428,8 @@ def test_api_hub_discovery_client_service_account_always_use_jwt( @pytest.mark.parametrize( "client_class,transport_name", [ + (ApiHubDiscoveryClient, "grpc"), + (ApiHubDiscoveryAsyncClient, "grpc_asyncio"), (ApiHubDiscoveryClient, "rest"), ], ) @@ -451,17 +463,24 @@ def test_api_hub_discovery_client_from_service_account_file( def test_api_hub_discovery_client_get_transport_class(): transport = ApiHubDiscoveryClient.get_transport_class() available_transports = [ + transports.ApiHubDiscoveryGrpcTransport, transports.ApiHubDiscoveryRestTransport, ] assert transport in available_transports - transport = ApiHubDiscoveryClient.get_transport_class("rest") - assert transport == transports.ApiHubDiscoveryRestTransport + transport = ApiHubDiscoveryClient.get_transport_class("grpc") + assert transport == transports.ApiHubDiscoveryGrpcTransport @pytest.mark.parametrize( "client_class,transport_class,transport_name", [ + (ApiHubDiscoveryClient, transports.ApiHubDiscoveryGrpcTransport, "grpc"), + ( + ApiHubDiscoveryAsyncClient, + transports.ApiHubDiscoveryGrpcAsyncIOTransport, + "grpc_asyncio", + ), (ApiHubDiscoveryClient, transports.ApiHubDiscoveryRestTransport, "rest"), ], ) @@ -470,6 +489,11 @@ def test_api_hub_discovery_client_get_transport_class(): "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubDiscoveryClient), ) +@mock.patch.object( + ApiHubDiscoveryAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubDiscoveryAsyncClient), +) def test_api_hub_discovery_client_client_options( client_class, transport_class, transport_name ): @@ -603,6 +627,30 @@ def test_api_hub_discovery_client_client_options( @pytest.mark.parametrize( "client_class,transport_class,transport_name,use_client_cert_env", [ + ( + ApiHubDiscoveryClient, + transports.ApiHubDiscoveryGrpcTransport, + "grpc", + "true", + ), + ( + ApiHubDiscoveryAsyncClient, + transports.ApiHubDiscoveryGrpcAsyncIOTransport, + "grpc_asyncio", + "true", + ), + ( + ApiHubDiscoveryClient, + transports.ApiHubDiscoveryGrpcTransport, + "grpc", + "false", + ), + ( + ApiHubDiscoveryAsyncClient, + transports.ApiHubDiscoveryGrpcAsyncIOTransport, + "grpc_asyncio", + "false", + ), ( ApiHubDiscoveryClient, transports.ApiHubDiscoveryRestTransport, @@ -622,6 +670,11 @@ def test_api_hub_discovery_client_client_options( "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubDiscoveryClient), ) +@mock.patch.object( + ApiHubDiscoveryAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubDiscoveryAsyncClient), +) @mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) def test_api_hub_discovery_client_mtls_env_auto( client_class, transport_class, transport_name, use_client_cert_env @@ -725,12 +778,19 @@ def test_api_hub_discovery_client_mtls_env_auto( ) -@pytest.mark.parametrize("client_class", [ApiHubDiscoveryClient]) +@pytest.mark.parametrize( + "client_class", [ApiHubDiscoveryClient, ApiHubDiscoveryAsyncClient] +) @mock.patch.object( ApiHubDiscoveryClient, "DEFAULT_ENDPOINT", modify_default_endpoint(ApiHubDiscoveryClient), ) +@mock.patch.object( + ApiHubDiscoveryAsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(ApiHubDiscoveryAsyncClient), +) def test_api_hub_discovery_client_get_mtls_endpoint_and_cert_source(client_class): mock_client_cert_source = mock.Mock() @@ -822,12 +882,19 @@ def test_api_hub_discovery_client_get_mtls_endpoint_and_cert_source(client_class ) -@pytest.mark.parametrize("client_class", [ApiHubDiscoveryClient]) +@pytest.mark.parametrize( + "client_class", [ApiHubDiscoveryClient, ApiHubDiscoveryAsyncClient] +) @mock.patch.object( ApiHubDiscoveryClient, "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubDiscoveryClient), ) +@mock.patch.object( + ApiHubDiscoveryAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubDiscoveryAsyncClient), +) def test_api_hub_discovery_client_client_api_endpoint(client_class): mock_client_cert_source = client_cert_source_callback api_override = "foo.com" @@ -904,6 +971,12 @@ def test_api_hub_discovery_client_client_api_endpoint(client_class): @pytest.mark.parametrize( "client_class,transport_class,transport_name", [ + (ApiHubDiscoveryClient, transports.ApiHubDiscoveryGrpcTransport, "grpc"), + ( + ApiHubDiscoveryAsyncClient, + transports.ApiHubDiscoveryGrpcAsyncIOTransport, + "grpc_asyncio", + ), (ApiHubDiscoveryClient, transports.ApiHubDiscoveryRestTransport, "rest"), ], ) @@ -935,6 +1008,18 @@ def test_api_hub_discovery_client_client_options_scopes( @pytest.mark.parametrize( "client_class,transport_class,transport_name,grpc_helpers", [ + ( + ApiHubDiscoveryClient, + transports.ApiHubDiscoveryGrpcTransport, + "grpc", + grpc_helpers, + ), + ( + ApiHubDiscoveryAsyncClient, + transports.ApiHubDiscoveryGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), (ApiHubDiscoveryClient, transports.ApiHubDiscoveryRestTransport, "rest", None), ], ) @@ -962,13 +1047,173 @@ def test_api_hub_discovery_client_client_options_credentials_file( ) -def test_list_discovered_api_observations_rest_use_cached_wrapped_rpc(): +def test_api_hub_discovery_client_client_options_from_dict(): + with mock.patch( + "google.cloud.apihub_v1.services.api_hub_discovery.transports.ApiHubDiscoveryGrpcTransport.__init__" + ) as grpc_transport: + grpc_transport.return_value = None + client = ApiHubDiscoveryClient( + client_options={"api_endpoint": "squid.clam.whelk"} + ) + grpc_transport.assert_called_once_with( + credentials=None, + credentials_file=None, + host="squid.clam.whelk", + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name,grpc_helpers", + [ + ( + ApiHubDiscoveryClient, + transports.ApiHubDiscoveryGrpcTransport, + "grpc", + grpc_helpers, + ), + ( + ApiHubDiscoveryAsyncClient, + transports.ApiHubDiscoveryGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), + ], +) +def test_api_hub_discovery_client_create_channel_credentials_file( + client_class, transport_class, transport_name, grpc_helpers +): + # Check the case credentials file is provided. + options = client_options.ClientOptions(credentials_file="credentials.json") + + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options, transport=transport_name) + patched.assert_called_once_with( + credentials=None, + credentials_file="credentials.json", + host=client._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE + ), + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + # test that the credentials from file are saved and used as the credentials. + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel" + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + file_creds = ga_credentials.AnonymousCredentials() + load_creds.return_value = (file_creds, None) + adc.return_value = (creds, None) + client = client_class(client_options=options, transport=transport_name) + create_channel.assert_called_with( + "apihub.googleapis.com:443", + credentials=file_creds, + credentials_file=None, + quota_project_id=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + scopes=None, + default_host="apihub.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + +@pytest.mark.parametrize( + "request_type", + [ + discovery_service.ListDiscoveredApiObservationsRequest, + dict, + ], +) +def test_list_discovered_api_observations(request_type, transport: str = "grpc"): + client = ApiHubDiscoveryClient( + credentials=ga_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.list_discovered_api_observations), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = discovery_service.ListDiscoveredApiObservationsResponse( + next_page_token="next_page_token_value", + ) + response = client.list_discovered_api_observations(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = discovery_service.ListDiscoveredApiObservationsRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListDiscoveredApiObservationsPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_discovered_api_observations_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = discovery_service.ListDiscoveredApiObservationsRequest( + parent="parent_value", + page_token="page_token_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_observations), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.list_discovered_api_observations(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == discovery_service.ListDiscoveredApiObservationsRequest( + parent="parent_value", + page_token="page_token_value", + ) + + +def test_list_discovered_api_observations_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -989,7 +1234,6 @@ def test_list_discovered_api_observations_rest_use_cached_wrapped_rpc(): client._transport._wrapped_methods[ client._transport.list_discovered_api_observations ] = mock_rpc - request = {} client.list_discovered_api_observations(request) @@ -1003,181 +1247,253 @@ def test_list_discovered_api_observations_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_list_discovered_api_observations_rest_required_fields( - request_type=discovery_service.ListDiscoveredApiObservationsRequest, +@pytest.mark.asyncio +async def test_list_discovered_api_observations_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubDiscoveryRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.list_discovered_api_observations + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_discovered_api_observations._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.list_discovered_api_observations + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.list_discovered_api_observations(request) - jsonified_request["parent"] = "parent_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_discovered_api_observations._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "page_size", - "page_token", - ) - ) - jsonified_request.update(unset_fields) + await client.list_discovered_api_observations(request) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = discovery_service.ListDiscoveredApiObservationsResponse() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result +@pytest.mark.asyncio +async def test_list_discovered_api_observations_async( + transport: str = "grpc_asyncio", + request_type=discovery_service.ListDiscoveredApiObservationsRequest, +): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - response_value = Response() - response_value.status_code = 200 + # 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() - # Convert return value to protobuf type - return_value = discovery_service.ListDiscoveredApiObservationsResponse.pb( - return_value + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_observations), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + discovery_service.ListDiscoveredApiObservationsResponse( + next_page_token="next_page_token_value", ) - json_return_value = json_format.MessageToJson(return_value) + ) + response = await client.list_discovered_api_observations(request) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = discovery_service.ListDiscoveredApiObservationsRequest() + assert args[0] == request - response = client.list_discovered_api_observations(request) + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListDiscoveredApiObservationsAsyncPager) + assert response.next_page_token == "next_page_token_value" - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_list_discovered_api_observations_async_from_dict(): + await test_list_discovered_api_observations_async(request_type=dict) -def test_list_discovered_api_observations_rest_unset_required_fields(): - transport = transports.ApiHubDiscoveryRestTransport( - credentials=ga_credentials.AnonymousCredentials + +def test_list_discovered_api_observations_field_headers(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = ( - transport.list_discovered_api_observations._get_unset_required_fields({}) + # 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 = discovery_service.ListDiscoveredApiObservationsRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_observations), "__call__" + ) as call: + call.return_value = discovery_service.ListDiscoveredApiObservationsResponse() + client.list_discovered_api_observations(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", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_list_discovered_api_observations_field_headers_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), ) - assert set(unset_fields) == ( - set( - ( - "pageSize", - "pageToken", - ) + + # 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 = discovery_service.ListDiscoveredApiObservationsRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_observations), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + discovery_service.ListDiscoveredApiObservationsResponse() ) - & set(("parent",)) - ) + await client.list_discovered_api_observations(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request -def test_list_discovered_api_observations_rest_flattened(): + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +def test_list_discovered_api_observations_flattened(): client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = discovery_service.ListDiscoveredApiObservationsResponse() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_observations), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = discovery_service.ListDiscoveredApiObservationsResponse() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.list_discovered_api_observations( + parent="parent_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_list_discovered_api_observations_flattened_error(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_discovered_api_observations( + discovery_service.ListDiscoveredApiObservationsRequest(), parent="parent_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = discovery_service.ListDiscoveredApiObservationsResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_discovered_api_observations(**mock_args) +@pytest.mark.asyncio +async def test_list_discovered_api_observations_flattened_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_observations), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = discovery_service.ListDiscoveredApiObservationsResponse() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + discovery_service.ListDiscoveredApiObservationsResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.list_discovered_api_observations( + parent="parent_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/discoveredApiObservations" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val -def test_list_discovered_api_observations_rest_flattened_error(transport: str = "rest"): - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_list_discovered_api_observations_flattened_error_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.list_discovered_api_observations( + await client.list_discovered_api_observations( discovery_service.ListDiscoveredApiObservationsRequest(), parent="parent_value", ) -def test_list_discovered_api_observations_rest_pager(transport: str = "rest"): +def test_list_discovered_api_observations_pager(transport_name: str = "grpc"): client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport=transport_name, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # TODO(kbandes): remove this mock unless there's a good reason for it. - # with mock.patch.object(path_template, 'transcode') as transcode: - # Set the response as a series of pages - response = ( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_observations), "__call__" + ) as call: + # Set the response to a series of pages. + call.side_effect = ( discovery_service.ListDiscoveredApiObservationsResponse( discovered_api_observations=[ common_fields.DiscoveredApiObservation(), @@ -1202,24 +1518,22 @@ def test_list_discovered_api_observations_rest_pager(transport: str = "rest"): common_fields.DiscoveredApiObservation(), ], ), + RuntimeError, ) - # Two responses for two calls - response = response + response - # Wrap the values into proper Response objs - response = tuple( - discovery_service.ListDiscoveredApiObservationsResponse.to_json(x) - for x in response + expected_metadata = () + retry = retries.Retry() + timeout = 5 + expected_metadata = tuple(expected_metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", ""),)), + ) + pager = client.list_discovered_api_observations( + request={}, retry=retry, timeout=timeout ) - return_values = tuple(Response() for i in response) - for return_val, response_val in zip(return_values, response): - return_val._content = response_val.encode("UTF-8") - return_val.status_code = 200 - req.side_effect = return_values - - sample_request = {"parent": "projects/sample1/locations/sample2"} - pager = client.list_discovered_api_observations(request=sample_request) + assert pager._metadata == expected_metadata + assert pager._retry == retry + assert pager._timeout == timeout results = list(pager) assert len(results) == 6 @@ -1227,207 +1541,250 @@ def test_list_discovered_api_observations_rest_pager(transport: str = "rest"): isinstance(i, common_fields.DiscoveredApiObservation) for i in results ) - pages = list( - client.list_discovered_api_observations(request=sample_request).pages + +def test_list_discovered_api_observations_pages(transport_name: str = "grpc"): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport_name, + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_observations), "__call__" + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + discovery_service.ListDiscoveredApiObservationsResponse( + discovered_api_observations=[ + common_fields.DiscoveredApiObservation(), + common_fields.DiscoveredApiObservation(), + common_fields.DiscoveredApiObservation(), + ], + next_page_token="abc", + ), + discovery_service.ListDiscoveredApiObservationsResponse( + discovered_api_observations=[], + next_page_token="def", + ), + discovery_service.ListDiscoveredApiObservationsResponse( + discovered_api_observations=[ + common_fields.DiscoveredApiObservation(), + ], + next_page_token="ghi", + ), + discovery_service.ListDiscoveredApiObservationsResponse( + discovered_api_observations=[ + common_fields.DiscoveredApiObservation(), + common_fields.DiscoveredApiObservation(), + ], + ), + RuntimeError, ) + pages = list(client.list_discovered_api_observations(request={}).pages) for page_, token in zip(pages, ["abc", "def", "ghi", ""]): assert page_.raw_page.next_page_token == token -def test_get_discovered_api_observation_rest_use_cached_wrapped_rpc(): - # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, - # instead of constructing them on each call - with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - - # Should wrap all calls on client creation - assert wrapper_fn.call_count > 0 - wrapper_fn.reset_mock() +@pytest.mark.asyncio +async def test_list_discovered_api_observations_async_pager(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + ) - # Ensure method has been cached - assert ( - client._transport.get_discovered_api_observation - in client._transport._wrapped_methods + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_observations), + "__call__", + new_callable=mock.AsyncMock, + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + discovery_service.ListDiscoveredApiObservationsResponse( + discovered_api_observations=[ + common_fields.DiscoveredApiObservation(), + common_fields.DiscoveredApiObservation(), + common_fields.DiscoveredApiObservation(), + ], + next_page_token="abc", + ), + discovery_service.ListDiscoveredApiObservationsResponse( + discovered_api_observations=[], + next_page_token="def", + ), + discovery_service.ListDiscoveredApiObservationsResponse( + discovered_api_observations=[ + common_fields.DiscoveredApiObservation(), + ], + next_page_token="ghi", + ), + discovery_service.ListDiscoveredApiObservationsResponse( + discovered_api_observations=[ + common_fields.DiscoveredApiObservation(), + common_fields.DiscoveredApiObservation(), + ], + ), + RuntimeError, ) - - # Replace cached wrapped function with mock - mock_rpc = mock.Mock() - mock_rpc.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. + async_pager = await client.list_discovered_api_observations( + request={}, ) - client._transport._wrapped_methods[ - client._transport.get_discovered_api_observation - ] = mock_rpc + assert async_pager.next_page_token == "abc" + responses = [] + async for response in async_pager: # pragma: no branch + responses.append(response) - request = {} - client.get_discovered_api_observation(request) + assert len(responses) == 6 + assert all( + isinstance(i, common_fields.DiscoveredApiObservation) for i in responses + ) - # Establish that the underlying gRPC stub method was called. - assert mock_rpc.call_count == 1 - client.get_discovered_api_observation(request) +@pytest.mark.asyncio +async def test_list_discovered_api_observations_async_pages(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + ) - # Establish that a new wrapper was not created for this call - assert wrapper_fn.call_count == 0 - assert mock_rpc.call_count == 2 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_observations), + "__call__", + new_callable=mock.AsyncMock, + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + discovery_service.ListDiscoveredApiObservationsResponse( + discovered_api_observations=[ + common_fields.DiscoveredApiObservation(), + common_fields.DiscoveredApiObservation(), + common_fields.DiscoveredApiObservation(), + ], + next_page_token="abc", + ), + discovery_service.ListDiscoveredApiObservationsResponse( + discovered_api_observations=[], + next_page_token="def", + ), + discovery_service.ListDiscoveredApiObservationsResponse( + discovered_api_observations=[ + common_fields.DiscoveredApiObservation(), + ], + next_page_token="ghi", + ), + discovery_service.ListDiscoveredApiObservationsResponse( + discovered_api_observations=[ + common_fields.DiscoveredApiObservation(), + common_fields.DiscoveredApiObservation(), + ], + ), + RuntimeError, + ) + pages = [] + # Workaround issue in python 3.9 related to code coverage by adding `# pragma: no branch` + # See https://github.com/googleapis/gapic-generator-python/pull/1174#issuecomment-1025132372 + async for page_ in ( # pragma: no branch + await client.list_discovered_api_observations(request={}) + ).pages: + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token -def test_get_discovered_api_observation_rest_required_fields( - request_type=discovery_service.GetDiscoveredApiObservationRequest, -): - transport_class = transports.ApiHubDiscoveryRestTransport - - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) - - # verify fields with default values are dropped - - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_discovered_api_observation._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - - # verify required fields with default values are now present - - jsonified_request["name"] = "name_value" - - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_discovered_api_observation._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" - - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = common_fields.DiscoveredApiObservation() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - - response_value = Response() - response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = common_fields.DiscoveredApiObservation.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - - response = client.get_discovered_api_observation(request) - - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params - - -def test_get_discovered_api_observation_rest_unset_required_fields(): - transport = transports.ApiHubDiscoveryRestTransport( - credentials=ga_credentials.AnonymousCredentials - ) - - unset_fields = transport.get_discovered_api_observation._get_unset_required_fields( - {} - ) - assert set(unset_fields) == (set(()) & set(("name",))) - - -def test_get_discovered_api_observation_rest_flattened(): +@pytest.mark.parametrize( + "request_type", + [ + discovery_service.GetDiscoveredApiObservationRequest, + dict, + ], +) +def test_get_discovered_api_observation(request_type, transport: str = "grpc"): client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.DiscoveredApiObservation() - - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/discoveredApiObservations/sample3" - } + # 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() - # get truthy value for each flattened field - mock_args = dict( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_discovered_api_observation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.DiscoveredApiObservation( name="name_value", + style=common_fields.DiscoveredApiObservation.Style.REST, + server_ips=["server_ips_value"], + hostname="hostname_value", + source_locations=["source_locations_value"], + api_operation_count=2034, + origin="origin_value", + source_types=[common_fields.DiscoveredApiObservation.SourceType.GCP_XLB], + known_operations_count=2392, + unknown_operations_count=2619, ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.DiscoveredApiObservation.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.get_discovered_api_observation(request) - client.get_discovered_api_observation(**mock_args) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = discovery_service.GetDiscoveredApiObservationRequest() + assert args[0] == request - # Establish that the underlying call was made with the expected - # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/discoveredApiObservations/*}" - % client.transport._host, - args[1], - ) + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.DiscoveredApiObservation) + assert response.name == "name_value" + assert response.style == common_fields.DiscoveredApiObservation.Style.REST + assert response.server_ips == ["server_ips_value"] + assert response.hostname == "hostname_value" + assert response.source_locations == ["source_locations_value"] + assert response.api_operation_count == 2034 + assert response.origin == "origin_value" + assert response.source_types == [ + common_fields.DiscoveredApiObservation.SourceType.GCP_XLB + ] + assert response.known_operations_count == 2392 + assert response.unknown_operations_count == 2619 -def test_get_discovered_api_observation_rest_flattened_error(transport: str = "rest"): +def test_get_discovered_api_observation_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport="grpc", ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.get_discovered_api_observation( - discovery_service.GetDiscoveredApiObservationRequest(), + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = discovery_service.GetDiscoveredApiObservationRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_discovered_api_observation), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_discovered_api_observation(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == discovery_service.GetDiscoveredApiObservationRequest( name="name_value", ) -def test_list_discovered_api_operations_rest_use_cached_wrapped_rpc(): +def test_get_discovered_api_observation_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1436,7 +1793,7 @@ def test_list_discovered_api_operations_rest_use_cached_wrapped_rpc(): # Ensure method has been cached assert ( - client._transport.list_discovered_api_operations + client._transport.get_discovered_api_observation in client._transport._wrapped_methods ) @@ -1446,262 +1803,355 @@ def test_list_discovered_api_operations_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[ - client._transport.list_discovered_api_operations + client._transport.get_discovered_api_observation ] = mock_rpc - request = {} - client.list_discovered_api_operations(request) + client.get_discovered_api_observation(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.list_discovered_api_operations(request) + client.get_discovered_api_observation(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_list_discovered_api_operations_rest_required_fields( - request_type=discovery_service.ListDiscoveredApiOperationsRequest, +@pytest.mark.asyncio +async def test_get_discovered_api_observation_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubDiscoveryRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.get_discovered_api_observation + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_discovered_api_operations._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.get_discovered_api_observation + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.get_discovered_api_observation(request) - jsonified_request["parent"] = "parent_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_discovered_api_operations._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "page_size", - "page_token", - ) - ) - jsonified_request.update(unset_fields) + await client.get_discovered_api_observation(request) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + +@pytest.mark.asyncio +async def test_get_discovered_api_observation_async( + transport: str = "grpc_asyncio", + request_type=discovery_service.GetDiscoveredApiObservationRequest, +): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = discovery_service.ListDiscoveredApiOperationsResponse() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - - response_value = Response() - response_value.status_code = 200 + # 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() - # Convert return value to protobuf type - return_value = discovery_service.ListDiscoveredApiOperationsResponse.pb( - return_value + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_discovered_api_observation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.DiscoveredApiObservation( + name="name_value", + style=common_fields.DiscoveredApiObservation.Style.REST, + server_ips=["server_ips_value"], + hostname="hostname_value", + source_locations=["source_locations_value"], + api_operation_count=2034, + origin="origin_value", + source_types=[ + common_fields.DiscoveredApiObservation.SourceType.GCP_XLB + ], + known_operations_count=2392, + unknown_operations_count=2619, ) - json_return_value = json_format.MessageToJson(return_value) + ) + response = await client.get_discovered_api_observation(request) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = discovery_service.GetDiscoveredApiObservationRequest() + assert args[0] == request - response = client.list_discovered_api_operations(request) + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.DiscoveredApiObservation) + assert response.name == "name_value" + assert response.style == common_fields.DiscoveredApiObservation.Style.REST + assert response.server_ips == ["server_ips_value"] + assert response.hostname == "hostname_value" + assert response.source_locations == ["source_locations_value"] + assert response.api_operation_count == 2034 + assert response.origin == "origin_value" + assert response.source_types == [ + common_fields.DiscoveredApiObservation.SourceType.GCP_XLB + ] + assert response.known_operations_count == 2392 + assert response.unknown_operations_count == 2619 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_get_discovered_api_observation_async_from_dict(): + await test_get_discovered_api_observation_async(request_type=dict) -def test_list_discovered_api_operations_rest_unset_required_fields(): - transport = transports.ApiHubDiscoveryRestTransport( - credentials=ga_credentials.AnonymousCredentials + +def test_get_discovered_api_observation_field_headers(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = transport.list_discovered_api_operations._get_unset_required_fields( - {} + # 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 = discovery_service.GetDiscoveredApiObservationRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_discovered_api_observation), "__call__" + ) as call: + call.return_value = common_fields.DiscoveredApiObservation() + client.get_discovered_api_observation(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", + "name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_get_discovered_api_observation_field_headers_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), ) - assert set(unset_fields) == ( - set( - ( - "pageSize", - "pageToken", - ) + + # 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 = discovery_service.GetDiscoveredApiObservationRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_discovered_api_observation), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.DiscoveredApiObservation() ) - & set(("parent",)) - ) + await client.get_discovered_api_observation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request -def test_list_discovered_api_operations_rest_flattened(): + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] + + +def test_get_discovered_api_observation_flattened(): client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = discovery_service.ListDiscoveredApiOperationsResponse() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_discovered_api_observation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.DiscoveredApiObservation() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_discovered_api_observation( + name="name_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "parent": "projects/sample1/locations/sample2/discoveredApiObservations/sample3" - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( - parent="parent_value", - ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = discovery_service.ListDiscoveredApiOperationsResponse.pb( - return_value +def test_get_discovered_api_observation_flattened_error(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_discovered_api_observation( + discovery_service.GetDiscoveredApiObservationRequest(), + name="name_value", ) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_discovered_api_operations(**mock_args) + +@pytest.mark.asyncio +async def test_get_discovered_api_observation_flattened_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_discovered_api_observation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.DiscoveredApiObservation() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.DiscoveredApiObservation() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_discovered_api_observation( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*/discoveredApiObservations/*}/discoveredApiOperations" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_list_discovered_api_operations_rest_flattened_error(transport: str = "rest"): - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_get_discovered_api_observation_flattened_error_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.list_discovered_api_operations( - discovery_service.ListDiscoveredApiOperationsRequest(), - parent="parent_value", + await client.get_discovered_api_observation( + discovery_service.GetDiscoveredApiObservationRequest(), + name="name_value", ) -def test_list_discovered_api_operations_rest_pager(transport: str = "rest"): +@pytest.mark.parametrize( + "request_type", + [ + discovery_service.ListDiscoveredApiOperationsRequest, + dict, + ], +) +def test_list_discovered_api_operations(request_type, transport: str = "grpc"): client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # TODO(kbandes): remove this mock unless there's a good reason for it. - # with mock.patch.object(path_template, 'transcode') as transcode: - # Set the response as a series of pages - response = ( - discovery_service.ListDiscoveredApiOperationsResponse( - discovered_api_operations=[ - common_fields.DiscoveredApiOperation(), - common_fields.DiscoveredApiOperation(), - common_fields.DiscoveredApiOperation(), - ], - next_page_token="abc", - ), - discovery_service.ListDiscoveredApiOperationsResponse( - discovered_api_operations=[], - next_page_token="def", - ), - discovery_service.ListDiscoveredApiOperationsResponse( - discovered_api_operations=[ - common_fields.DiscoveredApiOperation(), - ], - next_page_token="ghi", - ), - discovery_service.ListDiscoveredApiOperationsResponse( - discovered_api_operations=[ - common_fields.DiscoveredApiOperation(), - common_fields.DiscoveredApiOperation(), - ], - ), - ) - # Two responses for two calls - response = response + response + # 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() - # Wrap the values into proper Response objs - response = tuple( - discovery_service.ListDiscoveredApiOperationsResponse.to_json(x) - for x in response + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_operations), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = discovery_service.ListDiscoveredApiOperationsResponse( + next_page_token="next_page_token_value", ) - return_values = tuple(Response() for i in response) - for return_val, response_val in zip(return_values, response): - return_val._content = response_val.encode("UTF-8") - return_val.status_code = 200 - req.side_effect = return_values + response = client.list_discovered_api_operations(request) - sample_request = { - "parent": "projects/sample1/locations/sample2/discoveredApiObservations/sample3" - } + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = discovery_service.ListDiscoveredApiOperationsRequest() + assert args[0] == request - pager = client.list_discovered_api_operations(request=sample_request) + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListDiscoveredApiOperationsPager) + assert response.next_page_token == "next_page_token_value" - results = list(pager) - assert len(results) == 6 - assert all(isinstance(i, common_fields.DiscoveredApiOperation) for i in results) - pages = list( - client.list_discovered_api_operations(request=sample_request).pages +def test_list_discovered_api_operations_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = discovery_service.ListDiscoveredApiOperationsRequest( + parent="parent_value", + page_token="page_token_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_operations), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.list_discovered_api_operations(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == discovery_service.ListDiscoveredApiOperationsRequest( + parent="parent_value", + page_token="page_token_value", ) - for page_, token in zip(pages, ["abc", "def", "ghi", ""]): - assert page_.raw_page.next_page_token == token -def test_get_discovered_api_operation_rest_use_cached_wrapped_rpc(): +def test_list_discovered_api_operations_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1710,7 +2160,7 @@ def test_get_discovered_api_operation_rest_use_cached_wrapped_rpc(): # Ensure method has been cached assert ( - client._transport.get_discovered_api_operation + client._transport.list_discovered_api_operations in client._transport._wrapped_methods ) @@ -1720,1679 +2170,4659 @@ def test_get_discovered_api_operation_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[ - client._transport.get_discovered_api_operation + client._transport.list_discovered_api_operations ] = mock_rpc - request = {} - client.get_discovered_api_operation(request) + client.list_discovered_api_operations(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.get_discovered_api_operation(request) + client.list_discovered_api_operations(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_get_discovered_api_operation_rest_required_fields( - request_type=discovery_service.GetDiscoveredApiOperationRequest, +@pytest.mark.asyncio +async def test_list_discovered_api_operations_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubDiscoveryRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.list_discovered_api_operations + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_discovered_api_operation._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.list_discovered_api_operations + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.list_discovered_api_operations(request) - jsonified_request["name"] = "name_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_discovered_api_operation._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + await client.list_discovered_api_operations(request) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + +@pytest.mark.asyncio +async def test_list_discovered_api_operations_async( + transport: str = "grpc_asyncio", + request_type=discovery_service.ListDiscoveredApiOperationsRequest, +): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = common_fields.DiscoveredApiOperation() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # 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() - response_value = Response() - response_value.status_code = 200 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_operations), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + discovery_service.ListDiscoveredApiOperationsResponse( + next_page_token="next_page_token_value", + ) + ) + response = await client.list_discovered_api_operations(request) - # Convert return value to protobuf type - return_value = common_fields.DiscoveredApiOperation.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = discovery_service.ListDiscoveredApiOperationsRequest() + assert args[0] == request - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListDiscoveredApiOperationsAsyncPager) + assert response.next_page_token == "next_page_token_value" - response = client.get_discovered_api_operation(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_list_discovered_api_operations_async_from_dict(): + await test_list_discovered_api_operations_async(request_type=dict) -def test_get_discovered_api_operation_rest_unset_required_fields(): - transport = transports.ApiHubDiscoveryRestTransport( - credentials=ga_credentials.AnonymousCredentials +def test_list_discovered_api_operations_field_headers(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = transport.get_discovered_api_operation._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # 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 = discovery_service.ListDiscoveredApiOperationsRequest() + request.parent = "parent_value" -def test_get_discovered_api_operation_rest_flattened(): - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_operations), "__call__" + ) as call: + call.return_value = discovery_service.ListDiscoveredApiOperationsResponse() + client.list_discovered_api_operations(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", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_list_discovered_api_operations_field_headers_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.DiscoveredApiOperation() + # 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 = discovery_service.ListDiscoveredApiOperationsRequest() - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/discoveredApiObservations/sample3/discoveredApiOperations/sample4" - } + request.parent = "parent_value" - # get truthy value for each flattened field - mock_args = dict( - name="name_value", + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_operations), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + discovery_service.ListDiscoveredApiOperationsResponse() ) - mock_args.update(sample_request) + await client.list_discovered_api_operations(request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.DiscoveredApiOperation.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request - client.get_discovered_api_operation(**mock_args) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +def test_list_discovered_api_operations_flattened(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_operations), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = discovery_service.ListDiscoveredApiOperationsResponse() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.list_discovered_api_operations( + parent="parent_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/discoveredApiObservations/*/discoveredApiOperations/*}" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val -def test_get_discovered_api_operation_rest_flattened_error(transport: str = "rest"): +def test_list_discovered_api_operations_flattened_error(): client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.get_discovered_api_operation( - discovery_service.GetDiscoveredApiOperationRequest(), - name="name_value", + client.list_discovered_api_operations( + discovery_service.ListDiscoveredApiOperationsRequest(), + parent="parent_value", ) -def test_credentials_transport_error(): - # It is an error to provide credentials and a transport instance. - transport = transports.ApiHubDiscoveryRestTransport( - credentials=ga_credentials.AnonymousCredentials(), +@pytest.mark.asyncio +async def test_list_discovered_api_operations_flattened_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), ) - with pytest.raises(ValueError): - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_operations), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = discovery_service.ListDiscoveredApiOperationsResponse() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + discovery_service.ListDiscoveredApiOperationsResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.list_discovered_api_operations( + parent="parent_value", ) - # It is an error to provide a credentials file and a transport instance. - transport = transports.ApiHubDiscoveryRestTransport( - credentials=ga_credentials.AnonymousCredentials(), + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_list_discovered_api_operations_flattened_error_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. with pytest.raises(ValueError): - client = ApiHubDiscoveryClient( - client_options={"credentials_file": "credentials.json"}, - transport=transport, + await client.list_discovered_api_operations( + discovery_service.ListDiscoveredApiOperationsRequest(), + parent="parent_value", ) - # It is an error to provide an api_key and a transport instance. - transport = transports.ApiHubDiscoveryRestTransport( + +def test_list_discovered_api_operations_pager(transport_name: str = "grpc"): + client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), + transport=transport_name, ) - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = ApiHubDiscoveryClient( - client_options=options, - transport=transport, + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_operations), "__call__" + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + discovery_service.ListDiscoveredApiOperationsResponse( + discovered_api_operations=[ + common_fields.DiscoveredApiOperation(), + common_fields.DiscoveredApiOperation(), + common_fields.DiscoveredApiOperation(), + ], + next_page_token="abc", + ), + discovery_service.ListDiscoveredApiOperationsResponse( + discovered_api_operations=[], + next_page_token="def", + ), + discovery_service.ListDiscoveredApiOperationsResponse( + discovered_api_operations=[ + common_fields.DiscoveredApiOperation(), + ], + next_page_token="ghi", + ), + discovery_service.ListDiscoveredApiOperationsResponse( + discovered_api_operations=[ + common_fields.DiscoveredApiOperation(), + common_fields.DiscoveredApiOperation(), + ], + ), + RuntimeError, ) - # It is an error to provide an api_key and a credential. - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = ApiHubDiscoveryClient( - client_options=options, credentials=ga_credentials.AnonymousCredentials() + expected_metadata = () + retry = retries.Retry() + timeout = 5 + expected_metadata = tuple(expected_metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", ""),)), + ) + pager = client.list_discovered_api_operations( + request={}, retry=retry, timeout=timeout ) - # It is an error to provide scopes and a transport instance. - transport = transports.ApiHubDiscoveryRestTransport( + assert pager._metadata == expected_metadata + assert pager._retry == retry + assert pager._timeout == timeout + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, common_fields.DiscoveredApiOperation) for i in results) + + +def test_list_discovered_api_operations_pages(transport_name: str = "grpc"): + client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), + transport=transport_name, ) - with pytest.raises(ValueError): - client = ApiHubDiscoveryClient( - client_options={"scopes": ["1", "2"]}, - transport=transport, - ) - -def test_transport_instance(): - # A client may be instantiated with a custom transport instance. - transport = transports.ApiHubDiscoveryRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - client = ApiHubDiscoveryClient(transport=transport) - assert client.transport is transport + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_operations), "__call__" + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + discovery_service.ListDiscoveredApiOperationsResponse( + discovered_api_operations=[ + common_fields.DiscoveredApiOperation(), + common_fields.DiscoveredApiOperation(), + common_fields.DiscoveredApiOperation(), + ], + next_page_token="abc", + ), + discovery_service.ListDiscoveredApiOperationsResponse( + discovered_api_operations=[], + next_page_token="def", + ), + discovery_service.ListDiscoveredApiOperationsResponse( + discovered_api_operations=[ + common_fields.DiscoveredApiOperation(), + ], + next_page_token="ghi", + ), + discovery_service.ListDiscoveredApiOperationsResponse( + discovered_api_operations=[ + common_fields.DiscoveredApiOperation(), + common_fields.DiscoveredApiOperation(), + ], + ), + RuntimeError, + ) + pages = list(client.list_discovered_api_operations(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token -@pytest.mark.parametrize( - "transport_class", - [ - transports.ApiHubDiscoveryRestTransport, - ], -) -def test_transport_adc(transport_class): - # Test default credentials are used if not provided. - with mock.patch.object(google.auth, "default") as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport_class() - adc.assert_called_once() +@pytest.mark.asyncio +async def test_list_discovered_api_operations_async_pager(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_operations), + "__call__", + new_callable=mock.AsyncMock, + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + discovery_service.ListDiscoveredApiOperationsResponse( + discovered_api_operations=[ + common_fields.DiscoveredApiOperation(), + common_fields.DiscoveredApiOperation(), + common_fields.DiscoveredApiOperation(), + ], + next_page_token="abc", + ), + discovery_service.ListDiscoveredApiOperationsResponse( + discovered_api_operations=[], + next_page_token="def", + ), + discovery_service.ListDiscoveredApiOperationsResponse( + discovered_api_operations=[ + common_fields.DiscoveredApiOperation(), + ], + next_page_token="ghi", + ), + discovery_service.ListDiscoveredApiOperationsResponse( + discovered_api_operations=[ + common_fields.DiscoveredApiOperation(), + common_fields.DiscoveredApiOperation(), + ], + ), + RuntimeError, + ) + async_pager = await client.list_discovered_api_operations( + request={}, + ) + assert async_pager.next_page_token == "abc" + responses = [] + async for response in async_pager: # pragma: no branch + responses.append(response) -def test_transport_kind_rest(): - transport = ApiHubDiscoveryClient.get_transport_class("rest")( - credentials=ga_credentials.AnonymousCredentials() - ) - assert transport.kind == "rest" + assert len(responses) == 6 + assert all( + isinstance(i, common_fields.DiscoveredApiOperation) for i in responses + ) -def test_list_discovered_api_observations_rest_bad_request( - request_type=discovery_service.ListDiscoveredApiObservationsRequest, -): - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" +@pytest.mark.asyncio +async def test_list_discovered_api_operations_async_pages(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_discovered_api_observations(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_operations), + "__call__", + new_callable=mock.AsyncMock, + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + discovery_service.ListDiscoveredApiOperationsResponse( + discovered_api_operations=[ + common_fields.DiscoveredApiOperation(), + common_fields.DiscoveredApiOperation(), + common_fields.DiscoveredApiOperation(), + ], + next_page_token="abc", + ), + discovery_service.ListDiscoveredApiOperationsResponse( + discovered_api_operations=[], + next_page_token="def", + ), + discovery_service.ListDiscoveredApiOperationsResponse( + discovered_api_operations=[ + common_fields.DiscoveredApiOperation(), + ], + next_page_token="ghi", + ), + discovery_service.ListDiscoveredApiOperationsResponse( + discovered_api_operations=[ + common_fields.DiscoveredApiOperation(), + common_fields.DiscoveredApiOperation(), + ], + ), + RuntimeError, + ) + pages = [] + # Workaround issue in python 3.9 related to code coverage by adding `# pragma: no branch` + # See https://github.com/googleapis/gapic-generator-python/pull/1174#issuecomment-1025132372 + async for page_ in ( # pragma: no branch + await client.list_discovered_api_operations(request={}) + ).pages: + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token @pytest.mark.parametrize( "request_type", [ - discovery_service.ListDiscoveredApiObservationsRequest, + discovery_service.GetDiscoveredApiOperationRequest, dict, ], ) -def test_list_discovered_api_observations_rest_call_success(request_type): +def test_get_discovered_api_operation(request_type, transport: str = "grpc"): client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request = request_type(**request_init) + # 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 http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = discovery_service.ListDiscoveredApiObservationsResponse( - next_page_token="next_page_token_value", + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_discovered_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.DiscoveredApiOperation( + name="name_value", + count=553, + classification=common_fields.DiscoveredApiOperation.Classification.KNOWN, ) + response = client.get_discovered_api_operation(request) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = discovery_service.ListDiscoveredApiObservationsResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.list_discovered_api_observations(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = discovery_service.GetDiscoveredApiOperationRequest() + assert args[0] == request # Establish that the response is the type that we expect. - assert isinstance(response, pagers.ListDiscoveredApiObservationsPager) - assert response.next_page_token == "next_page_token_value" + assert isinstance(response, common_fields.DiscoveredApiOperation) + assert response.name == "name_value" + assert response.count == 553 + assert ( + response.classification + == common_fields.DiscoveredApiOperation.Classification.KNOWN + ) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_list_discovered_api_observations_rest_interceptors(null_interceptor): - transport = transports.ApiHubDiscoveryRestTransport( +def test_get_discovered_api_operation_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubDiscoveryRestInterceptor(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = discovery_service.GetDiscoveredApiOperationRequest( + name="name_value", ) - client = ApiHubDiscoveryClient(transport=transport) + # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubDiscoveryRestInterceptor, - "post_list_discovered_api_observations", - ) as post, mock.patch.object( - transports.ApiHubDiscoveryRestInterceptor, - "post_list_discovered_api_observations_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.ApiHubDiscoveryRestInterceptor, - "pre_list_discovered_api_observations", - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = discovery_service.ListDiscoveredApiObservationsRequest.pb( - discovery_service.ListDiscoveredApiObservationsRequest() + type(client.transport.get_discovered_api_operation), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = discovery_service.ListDiscoveredApiObservationsResponse.to_json( - discovery_service.ListDiscoveredApiObservationsResponse() + client.get_discovered_api_operation(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == discovery_service.GetDiscoveredApiOperationRequest( + name="name_value", ) - req.return_value.content = return_value - request = discovery_service.ListDiscoveredApiObservationsRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = discovery_service.ListDiscoveredApiObservationsResponse() - post_with_metadata.return_value = ( - discovery_service.ListDiscoveredApiObservationsResponse(), - metadata, - ) - client.list_discovered_api_observations( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], +def test_get_discovered_api_operation_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() - + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() -def test_get_discovered_api_observation_rest_bad_request( - request_type=discovery_service.GetDiscoveredApiObservationRequest, + # Ensure method has been cached + assert ( + client._transport.get_discovered_api_operation + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.get_discovered_api_operation + ] = mock_rpc + request = {} + client.get_discovered_api_operation(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_discovered_api_operation(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_get_discovered_api_operation_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/discoveredApiObservations/sample3" - } - request = request_type(**request_init) + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_discovered_api_observation(request) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + # Ensure method has been cached + assert ( + client._client._transport.get_discovered_api_operation + in client._client._transport._wrapped_methods + ) -@pytest.mark.parametrize( - "request_type", - [ - discovery_service.GetDiscoveredApiObservationRequest, - dict, - ], -) -def test_get_discovered_api_observation_rest_call_success(request_type): - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.get_discovered_api_operation + ] = mock_rpc + + request = {} + await client.get_discovered_api_operation(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.get_discovered_api_operation(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_get_discovered_api_operation_async( + transport: str = "grpc_asyncio", + request_type=discovery_service.GetDiscoveredApiOperationRequest, +): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/discoveredApiObservations/sample3" - } - request = request_type(**request_init) + # 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 http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.DiscoveredApiObservation( - name="name_value", - style=common_fields.DiscoveredApiObservation.Style.REST, - server_ips=["server_ips_value"], - hostname="hostname_value", - source_locations=["source_locations_value"], - api_operation_count=2034, - origin="origin_value", - source_types=[common_fields.DiscoveredApiObservation.SourceType.GCP_XLB], - known_operations_count=2392, - unknown_operations_count=2619, + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_discovered_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.DiscoveredApiOperation( + name="name_value", + count=553, + classification=common_fields.DiscoveredApiOperation.Classification.KNOWN, + ) ) + response = await client.get_discovered_api_operation(request) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = common_fields.DiscoveredApiObservation.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_discovered_api_observation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = discovery_service.GetDiscoveredApiOperationRequest() + assert args[0] == request # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.DiscoveredApiObservation) + assert isinstance(response, common_fields.DiscoveredApiOperation) assert response.name == "name_value" - assert response.style == common_fields.DiscoveredApiObservation.Style.REST - assert response.server_ips == ["server_ips_value"] - assert response.hostname == "hostname_value" - assert response.source_locations == ["source_locations_value"] - assert response.api_operation_count == 2034 - assert response.origin == "origin_value" - assert response.source_types == [ - common_fields.DiscoveredApiObservation.SourceType.GCP_XLB - ] - assert response.known_operations_count == 2392 - assert response.unknown_operations_count == 2619 + assert response.count == 553 + assert ( + response.classification + == common_fields.DiscoveredApiOperation.Classification.KNOWN + ) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_discovered_api_observation_rest_interceptors(null_interceptor): - transport = transports.ApiHubDiscoveryRestTransport( +@pytest.mark.asyncio +async def test_get_discovered_api_operation_async_from_dict(): + await test_get_discovered_api_operation_async(request_type=dict) + + +def test_get_discovered_api_operation_field_headers(): + client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubDiscoveryRestInterceptor(), ) - client = ApiHubDiscoveryClient(transport=transport) + # 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 = discovery_service.GetDiscoveredApiOperationRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubDiscoveryRestInterceptor, "post_get_discovered_api_observation" - ) as post, mock.patch.object( - transports.ApiHubDiscoveryRestInterceptor, - "post_get_discovered_api_observation_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.ApiHubDiscoveryRestInterceptor, "pre_get_discovered_api_observation" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = discovery_service.GetDiscoveredApiObservationRequest.pb( - discovery_service.GetDiscoveredApiObservationRequest() - ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } + type(client.transport.get_discovered_api_operation), "__call__" + ) as call: + call.return_value = common_fields.DiscoveredApiOperation() + client.get_discovered_api_operation(request) - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.DiscoveredApiObservation.to_json( - common_fields.DiscoveredApiObservation() - ) - req.return_value.content = return_value + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request - request = discovery_service.GetDiscoveredApiObservationRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = common_fields.DiscoveredApiObservation() - post_with_metadata.return_value = ( - common_fields.DiscoveredApiObservation(), - metadata, + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_get_discovered_api_operation_field_headers_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = discovery_service.GetDiscoveredApiOperationRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_discovered_api_operation), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.DiscoveredApiOperation() ) + await client.get_discovered_api_operation(request) - client.get_discovered_api_observation( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + # 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", + "name=name_value", + ) in kw["metadata"] + + +def test_get_discovered_api_operation_flattened(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_discovered_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.DiscoveredApiOperation() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_discovered_api_operation( + name="name_value", ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_list_discovered_api_operations_rest_bad_request( - request_type=discovery_service.ListDiscoveredApiOperationsRequest, -): +def test_get_discovered_api_operation_flattened_error(): client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), ) - # send a request that will satisfy transcoding - request_init = { - "parent": "projects/sample1/locations/sample2/discoveredApiObservations/sample3" - } - request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_discovered_api_operations(request) + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_discovered_api_operation( + discovery_service.GetDiscoveredApiOperationRequest(), + name="name_value", + ) + + +@pytest.mark.asyncio +async def test_get_discovered_api_operation_flattened_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_discovered_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.DiscoveredApiOperation() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.DiscoveredApiOperation() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_discovered_api_operation( + name="name_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_get_discovered_api_operation_flattened_error_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.get_discovered_api_operation( + discovery_service.GetDiscoveredApiOperationRequest(), + name="name_value", + ) + + +def test_list_discovered_api_observations_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.list_discovered_api_observations + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.list_discovered_api_observations + ] = mock_rpc + + request = {} + client.list_discovered_api_observations(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.list_discovered_api_observations(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_list_discovered_api_observations_rest_required_fields( + request_type=discovery_service.ListDiscoveredApiObservationsRequest, +): + transport_class = transports.ApiHubDiscoveryRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_discovered_api_observations._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_discovered_api_observations._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = discovery_service.ListDiscoveredApiObservationsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = discovery_service.ListDiscoveredApiObservationsResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_discovered_api_observations(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_discovered_api_observations_rest_unset_required_fields(): + transport = transports.ApiHubDiscoveryRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = ( + transport.list_discovered_api_observations._get_unset_required_fields({}) + ) + assert set(unset_fields) == ( + set( + ( + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +def test_list_discovered_api_observations_rest_flattened(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = discovery_service.ListDiscoveredApiObservationsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = discovery_service.ListDiscoveredApiObservationsResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.list_discovered_api_observations(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/discoveredApiObservations" + % client.transport._host, + args[1], + ) + + +def test_list_discovered_api_observations_rest_flattened_error(transport: str = "rest"): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_discovered_api_observations( + discovery_service.ListDiscoveredApiObservationsRequest(), + parent="parent_value", + ) + + +def test_list_discovered_api_observations_rest_pager(transport: str = "rest"): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + discovery_service.ListDiscoveredApiObservationsResponse( + discovered_api_observations=[ + common_fields.DiscoveredApiObservation(), + common_fields.DiscoveredApiObservation(), + common_fields.DiscoveredApiObservation(), + ], + next_page_token="abc", + ), + discovery_service.ListDiscoveredApiObservationsResponse( + discovered_api_observations=[], + next_page_token="def", + ), + discovery_service.ListDiscoveredApiObservationsResponse( + discovered_api_observations=[ + common_fields.DiscoveredApiObservation(), + ], + next_page_token="ghi", + ), + discovery_service.ListDiscoveredApiObservationsResponse( + discovered_api_observations=[ + common_fields.DiscoveredApiObservation(), + common_fields.DiscoveredApiObservation(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + discovery_service.ListDiscoveredApiObservationsResponse.to_json(x) + for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2"} + + pager = client.list_discovered_api_observations(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all( + isinstance(i, common_fields.DiscoveredApiObservation) for i in results + ) + + pages = list( + client.list_discovered_api_observations(request=sample_request).pages + ) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +def test_get_discovered_api_observation_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.get_discovered_api_observation + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.get_discovered_api_observation + ] = mock_rpc + + request = {} + client.get_discovered_api_observation(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_discovered_api_observation(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_get_discovered_api_observation_rest_required_fields( + request_type=discovery_service.GetDiscoveredApiObservationRequest, +): + transport_class = transports.ApiHubDiscoveryRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_discovered_api_observation._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_discovered_api_observation._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.DiscoveredApiObservation() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.DiscoveredApiObservation.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_discovered_api_observation(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_discovered_api_observation_rest_unset_required_fields(): + transport = transports.ApiHubDiscoveryRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_discovered_api_observation._get_unset_required_fields( + {} + ) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_get_discovered_api_observation_rest_flattened(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.DiscoveredApiObservation() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/discoveredApiObservations/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.DiscoveredApiObservation.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.get_discovered_api_observation(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/discoveredApiObservations/*}" + % client.transport._host, + args[1], + ) + + +def test_get_discovered_api_observation_rest_flattened_error(transport: str = "rest"): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_discovered_api_observation( + discovery_service.GetDiscoveredApiObservationRequest(), + name="name_value", + ) + + +def test_list_discovered_api_operations_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.list_discovered_api_operations + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.list_discovered_api_operations + ] = mock_rpc + + request = {} + client.list_discovered_api_operations(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.list_discovered_api_operations(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_list_discovered_api_operations_rest_required_fields( + request_type=discovery_service.ListDiscoveredApiOperationsRequest, +): + transport_class = transports.ApiHubDiscoveryRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_discovered_api_operations._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_discovered_api_operations._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = discovery_service.ListDiscoveredApiOperationsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = discovery_service.ListDiscoveredApiOperationsResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_discovered_api_operations(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_discovered_api_operations_rest_unset_required_fields(): + transport = transports.ApiHubDiscoveryRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_discovered_api_operations._get_unset_required_fields( + {} + ) + assert set(unset_fields) == ( + set( + ( + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +def test_list_discovered_api_operations_rest_flattened(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = discovery_service.ListDiscoveredApiOperationsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/discoveredApiObservations/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = discovery_service.ListDiscoveredApiOperationsResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.list_discovered_api_operations(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/discoveredApiObservations/*}/discoveredApiOperations" + % client.transport._host, + args[1], + ) + + +def test_list_discovered_api_operations_rest_flattened_error(transport: str = "rest"): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_discovered_api_operations( + discovery_service.ListDiscoveredApiOperationsRequest(), + parent="parent_value", + ) + + +def test_list_discovered_api_operations_rest_pager(transport: str = "rest"): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + discovery_service.ListDiscoveredApiOperationsResponse( + discovered_api_operations=[ + common_fields.DiscoveredApiOperation(), + common_fields.DiscoveredApiOperation(), + common_fields.DiscoveredApiOperation(), + ], + next_page_token="abc", + ), + discovery_service.ListDiscoveredApiOperationsResponse( + discovered_api_operations=[], + next_page_token="def", + ), + discovery_service.ListDiscoveredApiOperationsResponse( + discovered_api_operations=[ + common_fields.DiscoveredApiOperation(), + ], + next_page_token="ghi", + ), + discovery_service.ListDiscoveredApiOperationsResponse( + discovered_api_operations=[ + common_fields.DiscoveredApiOperation(), + common_fields.DiscoveredApiOperation(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + discovery_service.ListDiscoveredApiOperationsResponse.to_json(x) + for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = { + "parent": "projects/sample1/locations/sample2/discoveredApiObservations/sample3" + } + + pager = client.list_discovered_api_operations(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, common_fields.DiscoveredApiOperation) for i in results) + + pages = list( + client.list_discovered_api_operations(request=sample_request).pages + ) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +def test_get_discovered_api_operation_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.get_discovered_api_operation + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.get_discovered_api_operation + ] = mock_rpc + + request = {} + client.get_discovered_api_operation(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_discovered_api_operation(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_get_discovered_api_operation_rest_required_fields( + request_type=discovery_service.GetDiscoveredApiOperationRequest, +): + transport_class = transports.ApiHubDiscoveryRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_discovered_api_operation._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_discovered_api_operation._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.DiscoveredApiOperation() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.DiscoveredApiOperation.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_discovered_api_operation(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_discovered_api_operation_rest_unset_required_fields(): + transport = transports.ApiHubDiscoveryRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_discovered_api_operation._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_get_discovered_api_operation_rest_flattened(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.DiscoveredApiOperation() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/discoveredApiObservations/sample3/discoveredApiOperations/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.DiscoveredApiOperation.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.get_discovered_api_operation(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/discoveredApiObservations/*/discoveredApiOperations/*}" + % client.transport._host, + args[1], + ) + + +def test_get_discovered_api_operation_rest_flattened_error(transport: str = "rest"): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_discovered_api_operation( + discovery_service.GetDiscoveredApiOperationRequest(), + name="name_value", + ) + + +def test_credentials_transport_error(): + # It is an error to provide credentials and a transport instance. + transport = transports.ApiHubDiscoveryGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # It is an error to provide a credentials file and a transport instance. + transport = transports.ApiHubDiscoveryGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = ApiHubDiscoveryClient( + client_options={"credentials_file": "credentials.json"}, + transport=transport, + ) + + # It is an error to provide an api_key and a transport instance. + transport = transports.ApiHubDiscoveryGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = ApiHubDiscoveryClient( + client_options=options, + transport=transport, + ) + + # It is an error to provide an api_key and a credential. + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = ApiHubDiscoveryClient( + client_options=options, credentials=ga_credentials.AnonymousCredentials() + ) + + # It is an error to provide scopes and a transport instance. + transport = transports.ApiHubDiscoveryGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = ApiHubDiscoveryClient( + client_options={"scopes": ["1", "2"]}, + transport=transport, + ) + + +def test_transport_instance(): + # A client may be instantiated with a custom transport instance. + transport = transports.ApiHubDiscoveryGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + client = ApiHubDiscoveryClient(transport=transport) + assert client.transport is transport + + +def test_transport_get_channel(): + # A client may be instantiated with a custom transport instance. + transport = transports.ApiHubDiscoveryGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + transport = transports.ApiHubDiscoveryGrpcAsyncIOTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubDiscoveryGrpcTransport, + transports.ApiHubDiscoveryGrpcAsyncIOTransport, + transports.ApiHubDiscoveryRestTransport, + ], +) +def test_transport_adc(transport_class): + # Test default credentials are used if not provided. + with mock.patch.object(google.auth, "default") as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class() + adc.assert_called_once() + + +def test_transport_kind_grpc(): + transport = ApiHubDiscoveryClient.get_transport_class("grpc")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "grpc" + + +def test_initialize_client_w_grpc(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_discovered_api_observations_empty_call_grpc(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_observations), "__call__" + ) as call: + call.return_value = discovery_service.ListDiscoveredApiObservationsResponse() + client.list_discovered_api_observations(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = discovery_service.ListDiscoveredApiObservationsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_discovered_api_observation_empty_call_grpc(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_discovered_api_observation), "__call__" + ) as call: + call.return_value = common_fields.DiscoveredApiObservation() + client.get_discovered_api_observation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = discovery_service.GetDiscoveredApiObservationRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_discovered_api_operations_empty_call_grpc(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_operations), "__call__" + ) as call: + call.return_value = discovery_service.ListDiscoveredApiOperationsResponse() + client.list_discovered_api_operations(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = discovery_service.ListDiscoveredApiOperationsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_discovered_api_operation_empty_call_grpc(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_discovered_api_operation), "__call__" + ) as call: + call.return_value = common_fields.DiscoveredApiOperation() + client.get_discovered_api_operation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = discovery_service.GetDiscoveredApiOperationRequest() + + assert args[0] == request_msg + + +def test_transport_kind_grpc_asyncio(): + transport = ApiHubDiscoveryAsyncClient.get_transport_class("grpc_asyncio")( + credentials=async_anonymous_credentials() + ) + assert transport.kind == "grpc_asyncio" + + +def test_initialize_client_w_grpc_asyncio(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_list_discovered_api_observations_empty_call_grpc_asyncio(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_observations), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + discovery_service.ListDiscoveredApiObservationsResponse( + next_page_token="next_page_token_value", + ) + ) + await client.list_discovered_api_observations(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = discovery_service.ListDiscoveredApiObservationsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_discovered_api_observation_empty_call_grpc_asyncio(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_discovered_api_observation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.DiscoveredApiObservation( + name="name_value", + style=common_fields.DiscoveredApiObservation.Style.REST, + server_ips=["server_ips_value"], + hostname="hostname_value", + source_locations=["source_locations_value"], + api_operation_count=2034, + origin="origin_value", + source_types=[ + common_fields.DiscoveredApiObservation.SourceType.GCP_XLB + ], + known_operations_count=2392, + unknown_operations_count=2619, + ) + ) + await client.get_discovered_api_observation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = discovery_service.GetDiscoveredApiObservationRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_list_discovered_api_operations_empty_call_grpc_asyncio(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_operations), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + discovery_service.ListDiscoveredApiOperationsResponse( + next_page_token="next_page_token_value", + ) + ) + await client.list_discovered_api_operations(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = discovery_service.ListDiscoveredApiOperationsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_discovered_api_operation_empty_call_grpc_asyncio(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_discovered_api_operation), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.DiscoveredApiOperation( + name="name_value", + count=553, + classification=common_fields.DiscoveredApiOperation.Classification.KNOWN, + ) + ) + await client.get_discovered_api_operation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = discovery_service.GetDiscoveredApiOperationRequest() + + assert args[0] == request_msg + + +def test_transport_kind_rest(): + transport = ApiHubDiscoveryClient.get_transport_class("rest")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "rest" + + +def test_list_discovered_api_observations_rest_bad_request( + request_type=discovery_service.ListDiscoveredApiObservationsRequest, +): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_discovered_api_observations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + discovery_service.ListDiscoveredApiObservationsRequest, + dict, + ], +) +def test_list_discovered_api_observations_rest_call_success(request_type): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = discovery_service.ListDiscoveredApiObservationsResponse( + next_page_token="next_page_token_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = discovery_service.ListDiscoveredApiObservationsResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.list_discovered_api_observations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListDiscoveredApiObservationsPager) + assert response.next_page_token == "next_page_token_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_discovered_api_observations_rest_interceptors(null_interceptor): + transport = transports.ApiHubDiscoveryRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubDiscoveryRestInterceptor(), + ) + client = ApiHubDiscoveryClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubDiscoveryRestInterceptor, + "post_list_discovered_api_observations", + ) as post, mock.patch.object( + transports.ApiHubDiscoveryRestInterceptor, + "post_list_discovered_api_observations_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.ApiHubDiscoveryRestInterceptor, + "pre_list_discovered_api_observations", + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = discovery_service.ListDiscoveredApiObservationsRequest.pb( + discovery_service.ListDiscoveredApiObservationsRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = discovery_service.ListDiscoveredApiObservationsResponse.to_json( + discovery_service.ListDiscoveredApiObservationsResponse() + ) + req.return_value.content = return_value + + request = discovery_service.ListDiscoveredApiObservationsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = discovery_service.ListDiscoveredApiObservationsResponse() + post_with_metadata.return_value = ( + discovery_service.ListDiscoveredApiObservationsResponse(), + metadata, + ) + + client.list_discovered_api_observations( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_get_discovered_api_observation_rest_bad_request( + request_type=discovery_service.GetDiscoveredApiObservationRequest, +): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/discoveredApiObservations/sample3" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_discovered_api_observation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + discovery_service.GetDiscoveredApiObservationRequest, + dict, + ], +) +def test_get_discovered_api_observation_rest_call_success(request_type): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/discoveredApiObservations/sample3" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.DiscoveredApiObservation( + name="name_value", + style=common_fields.DiscoveredApiObservation.Style.REST, + server_ips=["server_ips_value"], + hostname="hostname_value", + source_locations=["source_locations_value"], + api_operation_count=2034, + origin="origin_value", + source_types=[common_fields.DiscoveredApiObservation.SourceType.GCP_XLB], + known_operations_count=2392, + unknown_operations_count=2619, + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.DiscoveredApiObservation.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.get_discovered_api_observation(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.DiscoveredApiObservation) + assert response.name == "name_value" + assert response.style == common_fields.DiscoveredApiObservation.Style.REST + assert response.server_ips == ["server_ips_value"] + assert response.hostname == "hostname_value" + assert response.source_locations == ["source_locations_value"] + assert response.api_operation_count == 2034 + assert response.origin == "origin_value" + assert response.source_types == [ + common_fields.DiscoveredApiObservation.SourceType.GCP_XLB + ] + assert response.known_operations_count == 2392 + assert response.unknown_operations_count == 2619 + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_discovered_api_observation_rest_interceptors(null_interceptor): + transport = transports.ApiHubDiscoveryRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubDiscoveryRestInterceptor(), + ) + client = ApiHubDiscoveryClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubDiscoveryRestInterceptor, "post_get_discovered_api_observation" + ) as post, mock.patch.object( + transports.ApiHubDiscoveryRestInterceptor, + "post_get_discovered_api_observation_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.ApiHubDiscoveryRestInterceptor, "pre_get_discovered_api_observation" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = discovery_service.GetDiscoveredApiObservationRequest.pb( + discovery_service.GetDiscoveredApiObservationRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = common_fields.DiscoveredApiObservation.to_json( + common_fields.DiscoveredApiObservation() + ) + req.return_value.content = return_value + + request = discovery_service.GetDiscoveredApiObservationRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = common_fields.DiscoveredApiObservation() + post_with_metadata.return_value = ( + common_fields.DiscoveredApiObservation(), + metadata, + ) + + client.get_discovered_api_observation( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_list_discovered_api_operations_rest_bad_request( + request_type=discovery_service.ListDiscoveredApiOperationsRequest, +): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/discoveredApiObservations/sample3" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_discovered_api_operations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + discovery_service.ListDiscoveredApiOperationsRequest, + dict, + ], +) +def test_list_discovered_api_operations_rest_call_success(request_type): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/discoveredApiObservations/sample3" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = discovery_service.ListDiscoveredApiOperationsResponse( + next_page_token="next_page_token_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = discovery_service.ListDiscoveredApiOperationsResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.list_discovered_api_operations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListDiscoveredApiOperationsPager) + assert response.next_page_token == "next_page_token_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_discovered_api_operations_rest_interceptors(null_interceptor): + transport = transports.ApiHubDiscoveryRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubDiscoveryRestInterceptor(), + ) + client = ApiHubDiscoveryClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubDiscoveryRestInterceptor, "post_list_discovered_api_operations" + ) as post, mock.patch.object( + transports.ApiHubDiscoveryRestInterceptor, + "post_list_discovered_api_operations_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.ApiHubDiscoveryRestInterceptor, "pre_list_discovered_api_operations" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = discovery_service.ListDiscoveredApiOperationsRequest.pb( + discovery_service.ListDiscoveredApiOperationsRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = discovery_service.ListDiscoveredApiOperationsResponse.to_json( + discovery_service.ListDiscoveredApiOperationsResponse() + ) + req.return_value.content = return_value + + request = discovery_service.ListDiscoveredApiOperationsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = discovery_service.ListDiscoveredApiOperationsResponse() + post_with_metadata.return_value = ( + discovery_service.ListDiscoveredApiOperationsResponse(), + metadata, + ) + + client.list_discovered_api_operations( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_get_discovered_api_operation_rest_bad_request( + request_type=discovery_service.GetDiscoveredApiOperationRequest, +): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/discoveredApiObservations/sample3/discoveredApiOperations/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_discovered_api_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + discovery_service.GetDiscoveredApiOperationRequest, + dict, + ], +) +def test_get_discovered_api_operation_rest_call_success(request_type): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/discoveredApiObservations/sample3/discoveredApiOperations/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.DiscoveredApiOperation( + name="name_value", + count=553, + classification=common_fields.DiscoveredApiOperation.Classification.KNOWN, + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.DiscoveredApiOperation.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.get_discovered_api_operation(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.DiscoveredApiOperation) + assert response.name == "name_value" + assert response.count == 553 + assert ( + response.classification + == common_fields.DiscoveredApiOperation.Classification.KNOWN + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_discovered_api_operation_rest_interceptors(null_interceptor): + transport = transports.ApiHubDiscoveryRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubDiscoveryRestInterceptor(), + ) + client = ApiHubDiscoveryClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubDiscoveryRestInterceptor, "post_get_discovered_api_operation" + ) as post, mock.patch.object( + transports.ApiHubDiscoveryRestInterceptor, + "post_get_discovered_api_operation_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.ApiHubDiscoveryRestInterceptor, "pre_get_discovered_api_operation" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = discovery_service.GetDiscoveredApiOperationRequest.pb( + discovery_service.GetDiscoveredApiOperationRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = common_fields.DiscoveredApiOperation.to_json( + common_fields.DiscoveredApiOperation() + ) + req.return_value.content = return_value + + request = discovery_service.GetDiscoveredApiOperationRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = common_fields.DiscoveredApiOperation() + post_with_metadata.return_value = ( + common_fields.DiscoveredApiOperation(), + metadata, + ) + + client.get_discovered_api_operation( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_get_location_rest_bad_request(request_type=locations_pb2.GetLocationRequest): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_location(request) + + +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.GetLocationRequest, + dict, + ], +) +def test_get_location_rest(request_type): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = locations_pb2.Location() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_location(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) + + +def test_list_locations_rest_bad_request( + request_type=locations_pb2.ListLocationsRequest, +): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict({"name": "projects/sample1"}, request) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_locations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.ListLocationsRequest, + dict, + ], +) +def test_list_locations_rest(request_type): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = locations_pb2.ListLocationsResponse() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_locations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + +def test_cancel_operation_rest_bad_request( + request_type=operations_pb2.CancelOperationRequest, +): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.cancel_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.CancelOperationRequest, + dict, + ], +) +def test_cancel_operation_rest(request_type): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "{}" + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.cancel_operation(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_delete_operation_rest_bad_request( + request_type=operations_pb2.DeleteOperationRequest, +): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.delete_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.DeleteOperationRequest, + dict, + ], +) +def test_delete_operation_rest(request_type): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "{}" + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_operation(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_get_operation_rest_bad_request( + request_type=operations_pb2.GetOperationRequest, +): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.GetOperationRequest, + dict, + ], +) +def test_get_operation_rest(request_type): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_operation(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) + + +def test_list_operations_rest_bad_request( + request_type=operations_pb2.ListOperationsRequest, +): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_operations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.ListOperationsRequest, + dict, + ], +) +def test_list_operations_rest(request_type): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.ListOperationsResponse() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_operations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) + + +def test_initialize_client_w_rest(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_discovered_api_observations_empty_call_rest(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_observations), "__call__" + ) as call: + client.list_discovered_api_observations(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = discovery_service.ListDiscoveredApiObservationsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_discovered_api_observation_empty_call_rest(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_discovered_api_observation), "__call__" + ) as call: + client.get_discovered_api_observation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = discovery_service.GetDiscoveredApiObservationRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_discovered_api_operations_empty_call_rest(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_discovered_api_operations), "__call__" + ) as call: + client.list_discovered_api_operations(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = discovery_service.ListDiscoveredApiOperationsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_discovered_api_operation_empty_call_rest(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_discovered_api_operation), "__call__" + ) as call: + client.get_discovered_api_operation(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = discovery_service.GetDiscoveredApiOperationRequest() + + assert args[0] == request_msg + + +def test_transport_grpc_default(): + # A client should use the gRPC transport by default. + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert isinstance( + client.transport, + transports.ApiHubDiscoveryGrpcTransport, + ) + + +def test_api_hub_discovery_base_transport_error(): + # Passing both a credentials object and credentials_file should raise an error + with pytest.raises(core_exceptions.DuplicateCredentialArgs): + transport = transports.ApiHubDiscoveryTransport( + credentials=ga_credentials.AnonymousCredentials(), + credentials_file="credentials.json", + ) + + +def test_api_hub_discovery_base_transport(): + # Instantiate the base transport. + with mock.patch( + "google.cloud.apihub_v1.services.api_hub_discovery.transports.ApiHubDiscoveryTransport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.ApiHubDiscoveryTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Every method on the transport should just blindly + # raise NotImplementedError. + methods = ( + "list_discovered_api_observations", + "get_discovered_api_observation", + "list_discovered_api_operations", + "get_discovered_api_operation", + "get_location", + "list_locations", + "get_operation", + "cancel_operation", + "delete_operation", + "list_operations", + ) + for method in methods: + with pytest.raises(NotImplementedError): + getattr(transport, method)(request=object()) + + with pytest.raises(NotImplementedError): + transport.close() + + # Catch all for all remaining methods and properties + remainder = [ + "kind", + ] + for r in remainder: + with pytest.raises(NotImplementedError): + getattr(transport, r)() + + +def test_api_hub_discovery_base_transport_with_credentials_file(): + # Instantiate the base transport with a credentials file + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch( + "google.cloud.apihub_v1.services.api_hub_discovery.transports.ApiHubDiscoveryTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) + transport = transports.ApiHubDiscoveryTransport( + credentials_file="credentials.json", + quota_project_id="octopus", + ) + load_creds.assert_called_once_with( + "credentials.json", + scopes=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", + ) + + +def test_api_hub_discovery_base_transport_with_adc(): + # Test the default credentials are used if credentials and credentials_file are None. + with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch( + "google.cloud.apihub_v1.services.api_hub_discovery.transports.ApiHubDiscoveryTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport = transports.ApiHubDiscoveryTransport() + adc.assert_called_once() + + +def test_api_hub_discovery_auth_adc(): + # If no credentials are provided, we should use ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + ApiHubDiscoveryClient() + adc.assert_called_once_with( + scopes=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id=None, + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubDiscoveryGrpcTransport, + transports.ApiHubDiscoveryGrpcAsyncIOTransport, + ], +) +def test_api_hub_discovery_transport_auth_adc(transport_class): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + adc.assert_called_once_with( + scopes=["1", "2"], + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubDiscoveryGrpcTransport, + transports.ApiHubDiscoveryGrpcAsyncIOTransport, + transports.ApiHubDiscoveryRestTransport, + ], +) +def test_api_hub_discovery_transport_auth_gdch_credentials(transport_class): + host = "https://language.com" + api_audience_tests = [None, "https://language2.com"] + api_audience_expect = [host, "https://language2.com"] + for t, e in zip(api_audience_tests, api_audience_expect): + with mock.patch.object(google.auth, "default", autospec=True) as adc: + gdch_mock = mock.MagicMock() + type(gdch_mock).with_gdch_audience = mock.PropertyMock( + return_value=gdch_mock + ) + adc.return_value = (gdch_mock, None) + transport_class(host=host, api_audience=t) + gdch_mock.with_gdch_audience.assert_called_once_with(e) + + +@pytest.mark.parametrize( + "transport_class,grpc_helpers", + [ + (transports.ApiHubDiscoveryGrpcTransport, grpc_helpers), + (transports.ApiHubDiscoveryGrpcAsyncIOTransport, grpc_helpers_async), + ], +) +def test_api_hub_discovery_transport_create_channel(transport_class, grpc_helpers): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel", autospec=True + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + adc.return_value = (creds, None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + + create_channel.assert_called_with( + "apihub.googleapis.com:443", + credentials=creds, + credentials_file=None, + quota_project_id="octopus", + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + scopes=["1", "2"], + default_host="apihub.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubDiscoveryGrpcTransport, + transports.ApiHubDiscoveryGrpcAsyncIOTransport, + ], +) +def test_api_hub_discovery_grpc_transport_client_cert_source_for_mtls(transport_class): + cred = ga_credentials.AnonymousCredentials() + + # Check ssl_channel_credentials is used if provided. + with mock.patch.object(transport_class, "create_channel") as mock_create_channel: + mock_ssl_channel_creds = mock.Mock() + transport_class( + host="squid.clam.whelk", + credentials=cred, + ssl_channel_credentials=mock_ssl_channel_creds, + ) + mock_create_channel.assert_called_once_with( + "squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_channel_creds, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + # Check if ssl_channel_credentials is not provided, then client_cert_source_for_mtls + # is used. + with mock.patch.object(transport_class, "create_channel", return_value=mock.Mock()): + with mock.patch("grpc.ssl_channel_credentials") as mock_ssl_cred: + transport_class( + credentials=cred, + client_cert_source_for_mtls=client_cert_source_callback, + ) + expected_cert, expected_key = client_cert_source_callback() + mock_ssl_cred.assert_called_once_with( + certificate_chain=expected_cert, private_key=expected_key + ) + + +def test_api_hub_discovery_http_transport_client_cert_source_for_mtls(): + cred = ga_credentials.AnonymousCredentials() + with mock.patch( + "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" + ) as mock_configure_mtls_channel: + transports.ApiHubDiscoveryRestTransport( + credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + ) + mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + + +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "grpc_asyncio", + "rest", + ], +) +def test_api_hub_discovery_host_no_port(transport_name): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="apihub.googleapis.com" + ), + transport=transport_name, + ) + assert client.transport._host == ( + "apihub.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://apihub.googleapis.com" + ) + + +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "grpc_asyncio", + "rest", + ], +) +def test_api_hub_discovery_host_with_port(transport_name): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="apihub.googleapis.com:8000" + ), + transport=transport_name, + ) + assert client.transport._host == ( + "apihub.googleapis.com:8000" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://apihub.googleapis.com:8000" + ) + + +@pytest.mark.parametrize( + "transport_name", + [ + "rest", + ], +) +def test_api_hub_discovery_client_transport_session_collision(transport_name): + creds1 = ga_credentials.AnonymousCredentials() + creds2 = ga_credentials.AnonymousCredentials() + client1 = ApiHubDiscoveryClient( + credentials=creds1, + transport=transport_name, + ) + client2 = ApiHubDiscoveryClient( + credentials=creds2, + transport=transport_name, + ) + session1 = client1.transport.list_discovered_api_observations._session + session2 = client2.transport.list_discovered_api_observations._session + assert session1 != session2 + session1 = client1.transport.get_discovered_api_observation._session + session2 = client2.transport.get_discovered_api_observation._session + assert session1 != session2 + session1 = client1.transport.list_discovered_api_operations._session + session2 = client2.transport.list_discovered_api_operations._session + assert session1 != session2 + session1 = client1.transport.get_discovered_api_operation._session + session2 = client2.transport.get_discovered_api_operation._session + assert session1 != session2 + + +def test_api_hub_discovery_grpc_transport_channel(): + channel = grpc.secure_channel("http://localhost/", grpc.local_channel_credentials()) + + # Check that channel is used if provided. + transport = transports.ApiHubDiscoveryGrpcTransport( + host="squid.clam.whelk", + channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None + + +def test_api_hub_discovery_grpc_asyncio_transport_channel(): + channel = aio.secure_channel("http://localhost/", grpc.local_channel_credentials()) + + # Check that channel is used if provided. + transport = transports.ApiHubDiscoveryGrpcAsyncIOTransport( + host="squid.clam.whelk", + channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None + + +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubDiscoveryGrpcTransport, + transports.ApiHubDiscoveryGrpcAsyncIOTransport, + ], +) +def test_api_hub_discovery_transport_channel_mtls_with_client_cert_source( + transport_class, +): + with mock.patch( + "grpc.ssl_channel_credentials", autospec=True + ) as grpc_ssl_channel_cred: + with mock.patch.object( + transport_class, "create_channel" + ) 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 = ga_credentials.AnonymousCredentials() + with pytest.warns(DeprecationWarning): + with mock.patch.object(google.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=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + assert transport._ssl_channel_credentials == mock_ssl_cred +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. @pytest.mark.parametrize( - "request_type", + "transport_class", [ - discovery_service.ListDiscoveredApiOperationsRequest, - dict, + transports.ApiHubDiscoveryGrpcTransport, + transports.ApiHubDiscoveryGrpcAsyncIOTransport, ], ) -def test_list_discovered_api_operations_rest_call_success(request_type): - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) +def test_api_hub_discovery_transport_channel_mtls_with_adc(transport_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), + ): + with mock.patch.object( + transport_class, "create_channel" + ) 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, + ) - # send a request that will satisfy transcoding - request_init = { - "parent": "projects/sample1/locations/sample2/discoveredApiObservations/sample3" - } - request = request_type(**request_init) + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=mock_cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = discovery_service.ListDiscoveredApiOperationsResponse( - next_page_token="next_page_token_value", - ) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 +def test_discovered_api_observation_path(): + project = "squid" + location = "clam" + discovered_api_observation = "whelk" + expected = "projects/{project}/locations/{location}/discoveredApiObservations/{discovered_api_observation}".format( + project=project, + location=location, + discovered_api_observation=discovered_api_observation, + ) + actual = ApiHubDiscoveryClient.discovered_api_observation_path( + project, location, discovered_api_observation + ) + assert expected == actual - # Convert return value to protobuf type - return_value = discovery_service.ListDiscoveredApiOperationsResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.list_discovered_api_operations(request) - # Establish that the response is the type that we expect. - assert isinstance(response, pagers.ListDiscoveredApiOperationsPager) - assert response.next_page_token == "next_page_token_value" +def test_parse_discovered_api_observation_path(): + expected = { + "project": "octopus", + "location": "oyster", + "discovered_api_observation": "nudibranch", + } + path = ApiHubDiscoveryClient.discovered_api_observation_path(**expected) + # Check that the path construction is reversible. + actual = ApiHubDiscoveryClient.parse_discovered_api_observation_path(path) + assert expected == actual -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_list_discovered_api_operations_rest_interceptors(null_interceptor): - transport = transports.ApiHubDiscoveryRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubDiscoveryRestInterceptor(), + +def test_discovered_api_operation_path(): + project = "cuttlefish" + location = "mussel" + discovered_api_observation = "winkle" + discovered_api_operation = "nautilus" + expected = "projects/{project}/locations/{location}/discoveredApiObservations/{discovered_api_observation}/discoveredApiOperations/{discovered_api_operation}".format( + project=project, + location=location, + discovered_api_observation=discovered_api_observation, + discovered_api_operation=discovered_api_operation, ) - client = ApiHubDiscoveryClient(transport=transport) + actual = ApiHubDiscoveryClient.discovered_api_operation_path( + project, location, discovered_api_observation, discovered_api_operation + ) + assert expected == actual - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubDiscoveryRestInterceptor, "post_list_discovered_api_operations" - ) as post, mock.patch.object( - transports.ApiHubDiscoveryRestInterceptor, - "post_list_discovered_api_operations_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.ApiHubDiscoveryRestInterceptor, "pre_list_discovered_api_operations" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = discovery_service.ListDiscoveredApiOperationsRequest.pb( - discovery_service.ListDiscoveredApiOperationsRequest() - ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = discovery_service.ListDiscoveredApiOperationsResponse.to_json( - discovery_service.ListDiscoveredApiOperationsResponse() - ) - req.return_value.content = return_value +def test_parse_discovered_api_operation_path(): + expected = { + "project": "scallop", + "location": "abalone", + "discovered_api_observation": "squid", + "discovered_api_operation": "clam", + } + path = ApiHubDiscoveryClient.discovered_api_operation_path(**expected) - request = discovery_service.ListDiscoveredApiOperationsRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = discovery_service.ListDiscoveredApiOperationsResponse() - post_with_metadata.return_value = ( - discovery_service.ListDiscoveredApiOperationsResponse(), - metadata, - ) + # Check that the path construction is reversible. + actual = ApiHubDiscoveryClient.parse_discovered_api_operation_path(path) + assert expected == actual - client.list_discovered_api_operations( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], - ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() +def test_plugin_instance_path(): + project = "whelk" + location = "octopus" + plugin = "oyster" + instance = "nudibranch" + expected = "projects/{project}/locations/{location}/plugins/{plugin}/instances/{instance}".format( + project=project, + location=location, + plugin=plugin, + instance=instance, + ) + actual = ApiHubDiscoveryClient.plugin_instance_path( + project, location, plugin, instance + ) + assert expected == actual -def test_get_discovered_api_operation_rest_bad_request( - request_type=discovery_service.GetDiscoveredApiOperationRequest, -): - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/discoveredApiObservations/sample3/discoveredApiOperations/sample4" +def test_parse_plugin_instance_path(): + expected = { + "project": "cuttlefish", + "location": "mussel", + "plugin": "winkle", + "instance": "nautilus", } - request = request_type(**request_init) + path = ApiHubDiscoveryClient.plugin_instance_path(**expected) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_discovered_api_operation(request) + # Check that the path construction is reversible. + actual = ApiHubDiscoveryClient.parse_plugin_instance_path(path) + assert expected == actual -@pytest.mark.parametrize( - "request_type", - [ - discovery_service.GetDiscoveredApiOperationRequest, - dict, - ], -) -def test_get_discovered_api_operation_rest_call_success(request_type): - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" +def test_common_billing_account_path(): + billing_account = "scallop" + expected = "billingAccounts/{billing_account}".format( + billing_account=billing_account, ) + actual = ApiHubDiscoveryClient.common_billing_account_path(billing_account) + assert expected == actual - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/discoveredApiObservations/sample3/discoveredApiOperations/sample4" - } - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.DiscoveredApiOperation( - name="name_value", - count=553, - classification=common_fields.DiscoveredApiOperation.Classification.KNOWN, - ) +def test_parse_common_billing_account_path(): + expected = { + "billing_account": "abalone", + } + path = ApiHubDiscoveryClient.common_billing_account_path(**expected) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 + # Check that the path construction is reversible. + actual = ApiHubDiscoveryClient.parse_common_billing_account_path(path) + assert expected == actual - # Convert return value to protobuf type - return_value = common_fields.DiscoveredApiOperation.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_discovered_api_operation(request) - # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.DiscoveredApiOperation) - assert response.name == "name_value" - assert response.count == 553 - assert ( - response.classification - == common_fields.DiscoveredApiOperation.Classification.KNOWN +def test_common_folder_path(): + folder = "squid" + expected = "folders/{folder}".format( + folder=folder, ) + actual = ApiHubDiscoveryClient.common_folder_path(folder) + assert expected == actual -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_discovered_api_operation_rest_interceptors(null_interceptor): - transport = transports.ApiHubDiscoveryRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubDiscoveryRestInterceptor(), - ) - client = ApiHubDiscoveryClient(transport=transport) +def test_parse_common_folder_path(): + expected = { + "folder": "clam", + } + path = ApiHubDiscoveryClient.common_folder_path(**expected) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubDiscoveryRestInterceptor, "post_get_discovered_api_operation" - ) as post, mock.patch.object( - transports.ApiHubDiscoveryRestInterceptor, - "post_get_discovered_api_operation_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.ApiHubDiscoveryRestInterceptor, "pre_get_discovered_api_operation" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = discovery_service.GetDiscoveredApiOperationRequest.pb( - discovery_service.GetDiscoveredApiOperationRequest() - ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } + # Check that the path construction is reversible. + actual = ApiHubDiscoveryClient.parse_common_folder_path(path) + assert expected == actual - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.DiscoveredApiOperation.to_json( - common_fields.DiscoveredApiOperation() - ) - req.return_value.content = return_value - request = discovery_service.GetDiscoveredApiOperationRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = common_fields.DiscoveredApiOperation() - post_with_metadata.return_value = ( - common_fields.DiscoveredApiOperation(), - metadata, - ) +def test_common_organization_path(): + organization = "whelk" + expected = "organizations/{organization}".format( + organization=organization, + ) + actual = ApiHubDiscoveryClient.common_organization_path(organization) + assert expected == actual - client.get_discovered_api_operation( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], - ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() +def test_parse_common_organization_path(): + expected = { + "organization": "octopus", + } + path = ApiHubDiscoveryClient.common_organization_path(**expected) + # Check that the path construction is reversible. + actual = ApiHubDiscoveryClient.parse_common_organization_path(path) + assert expected == actual -def test_get_location_rest_bad_request(request_type=locations_pb2.GetLocationRequest): - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2"}, request + +def test_common_project_path(): + project = "oyster" + expected = "projects/{project}".format( + project=project, ) + actual = ApiHubDiscoveryClient.common_project_path(project) + assert expected == actual - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_location(request) +def test_parse_common_project_path(): + expected = { + "project": "nudibranch", + } + path = ApiHubDiscoveryClient.common_project_path(**expected) -@pytest.mark.parametrize( - "request_type", - [ - locations_pb2.GetLocationRequest, - dict, - ], -) -def test_get_location_rest(request_type): - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + # Check that the path construction is reversible. + actual = ApiHubDiscoveryClient.parse_common_project_path(path) + assert expected == actual - request_init = {"name": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = locations_pb2.Location() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") +def test_common_location_path(): + project = "cuttlefish" + location = "mussel" + expected = "projects/{project}/locations/{location}".format( + project=project, + location=location, + ) + actual = ApiHubDiscoveryClient.common_location_path(project, location) + assert expected == actual - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_location(request) +def test_parse_common_location_path(): + expected = { + "project": "winkle", + "location": "nautilus", + } + path = ApiHubDiscoveryClient.common_location_path(**expected) - # Establish that the response is the type that we expect. - assert isinstance(response, locations_pb2.Location) + # Check that the path construction is reversible. + actual = ApiHubDiscoveryClient.parse_common_location_path(path) + assert expected == actual -def test_list_locations_rest_bad_request( - request_type=locations_pb2.ListLocationsRequest, -): - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict({"name": "projects/sample1"}, request) +def test_client_with_default_client_info(): + client_info = gapic_v1.client_info.ClientInfo() - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_locations(request) + with mock.patch.object( + transports.ApiHubDiscoveryTransport, "_prep_wrapped_messages" + ) as prep: + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + client_info=client_info, + ) + prep.assert_called_once_with(client_info) + + with mock.patch.object( + transports.ApiHubDiscoveryTransport, "_prep_wrapped_messages" + ) as prep: + transport_class = ApiHubDiscoveryClient.get_transport_class() + transport = transport_class( + credentials=ga_credentials.AnonymousCredentials(), + client_info=client_info, + ) + prep.assert_called_once_with(client_info) -@pytest.mark.parametrize( - "request_type", - [ - locations_pb2.ListLocationsRequest, - dict, - ], -) -def test_list_locations_rest(request_type): +def test_delete_operation(transport: str = "grpc"): client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - request_init = {"name": "projects/sample1"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = locations_pb2.ListLocationsResponse() + # 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 = operations_pb2.DeleteOperationRequest() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.delete_operation(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 response is the type that we expect. + assert response is None - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.list_locations(request) +@pytest.mark.asyncio +async def test_delete_operation_async(transport: str = "grpc_asyncio"): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.DeleteOperationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_operation(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 response is the type that we expect. - assert isinstance(response, locations_pb2.ListLocationsResponse) + assert response is None -def test_cancel_operation_rest_bad_request( - request_type=operations_pb2.CancelOperationRequest, -): +def test_delete_operation_field_headers(): client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.cancel_operation(request) + # 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 = operations_pb2.DeleteOperationRequest() + request.name = "locations" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + call.return_value = None -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.CancelOperationRequest, - dict, - ], -) -def test_cancel_operation_rest(request_type): - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + client.delete_operation(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 - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = None + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "{}" - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +@pytest.mark.asyncio +async def test_delete_operation_field_headers_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + ) - response = client.cancel_operation(request) + # 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 = operations_pb2.DeleteOperationRequest() + request.name = "locations" - # Establish that the response is the type that we expect. - assert response is None + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_operation(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", + "name=locations", + ) in kw["metadata"] -def test_delete_operation_rest_bad_request( - request_type=operations_pb2.DeleteOperationRequest, -): + +def test_delete_operation_from_dict(): client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + + response = client.delete_operation( + request={ + "name": "locations", + } + ) + call.assert_called() - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_operation(request) + +@pytest.mark.asyncio +async def test_delete_operation_from_dict_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.DeleteOperationRequest, - dict, - ], -) -def test_delete_operation_rest(request_type): +def test_cancel_operation(transport: str = "grpc"): client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = None + # 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 = operations_pb2.CancelOperationRequest() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "{}" - response_value.content = json_return_value.encode("UTF-8") + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.cancel_operation(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 - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert response is None - response = client.delete_operation(request) + +@pytest.mark.asyncio +async def test_cancel_operation_async(transport: str = "grpc_asyncio"): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.CancelOperationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.cancel_operation(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 response is the type that we expect. assert response is None -def test_get_operation_rest_bad_request( - request_type=operations_pb2.GetOperationRequest, -): +def test_cancel_operation_field_headers(): client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_operation(request) + # 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 = operations_pb2.CancelOperationRequest() + request.name = "locations" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + call.return_value = None -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.GetOperationRequest, - dict, - ], -) -def test_get_operation_rest(request_type): - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + client.cancel_operation(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 - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation() + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +@pytest.mark.asyncio +async def test_cancel_operation_field_headers_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + ) - response = client.get_operation(request) + # 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 = operations_pb2.CancelOperationRequest() + request.name = "locations" - # Establish that the response is the type that we expect. - assert isinstance(response, operations_pb2.Operation) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.cancel_operation(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", + "name=locations", + ) in kw["metadata"] -def test_list_operations_rest_bad_request( - request_type=operations_pb2.ListOperationsRequest, -): + +def test_cancel_operation_from_dict(): client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2"}, request ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + + response = client.cancel_operation( + request={ + "name": "locations", + } + ) + call.assert_called() - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_operations(request) +@pytest.mark.asyncio +async def test_cancel_operation_from_dict_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.cancel_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.ListOperationsRequest, - dict, - ], -) -def test_list_operations_rest(request_type): + +def test_get_operation(transport: str = "grpc"): client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - request_init = {"name": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.ListOperationsResponse() + # 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 = operations_pb2.GetOperationRequest() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation() + response = client.get_operation(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 - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) - response = client.list_operations(request) - # Establish that the response is the type that we expect. - assert isinstance(response, operations_pb2.ListOperationsResponse) +@pytest.mark.asyncio +async def test_get_operation_async(transport: str = "grpc_asyncio"): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.GetOperationRequest() -def test_initialize_client_w_rest(): - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - assert client is not None + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + response = await client.get_operation(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 response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_list_discovered_api_observations_empty_call_rest(): + +def test_get_operation_field_headers(): client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.list_discovered_api_observations), "__call__" - ) as call: - client.list_discovered_api_observations(request=None) + # 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 = operations_pb2.GetOperationRequest() + request.name = "locations" - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + call.return_value = operations_pb2.Operation() + + client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = discovery_service.ListDiscoveredApiObservationsRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_get_discovered_api_observation_empty_call_rest(): - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +@pytest.mark.asyncio +async def test_get_operation_field_headers_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.get_discovered_api_observation), "__call__" - ) as call: - client.get_discovered_api_observation(request=None) + # 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 = operations_pb2.GetOperationRequest() + request.name = "locations" - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + await client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = discovery_service.GetDiscoveredApiObservationRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_list_discovered_api_operations_empty_call_rest(): +def test_get_operation_from_dict(): client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation() + + response = client.get_operation( + request={ + "name": "locations", + } + ) + call.assert_called() - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.list_discovered_api_operations), "__call__" - ) as call: - client.list_discovered_api_operations(request=None) - # Establish that the underlying stub method was called. +@pytest.mark.asyncio +async def test_get_operation_from_dict_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + response = await client.get_operation( + request={ + "name": "locations", + } + ) call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = discovery_service.ListDiscoveredApiOperationsRequest() - - assert args[0] == request_msg -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_get_discovered_api_operation_empty_call_rest(): +def test_list_operations(transport: str = "grpc"): client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.get_discovered_api_operation), "__call__" - ) as call: - client.get_discovered_api_operation(request=None) + # 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 = operations_pb2.ListOperationsRequest() - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.ListOperationsResponse() + response = client.list_operations(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = discovery_service.GetDiscoveredApiOperationRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) -def test_api_hub_discovery_base_transport_error(): - # Passing both a credentials object and credentials_file should raise an error - with pytest.raises(core_exceptions.DuplicateCredentialArgs): - transport = transports.ApiHubDiscoveryTransport( - credentials=ga_credentials.AnonymousCredentials(), - credentials_file="credentials.json", - ) +@pytest.mark.asyncio +async def test_list_operations_async(transport: str = "grpc_asyncio"): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.ListOperationsRequest() -def test_api_hub_discovery_base_transport(): - # Instantiate the base transport. - with mock.patch( - "google.cloud.apihub_v1.services.api_hub_discovery.transports.ApiHubDiscoveryTransport.__init__" - ) as Transport: - Transport.return_value = None - transport = transports.ApiHubDiscoveryTransport( - credentials=ga_credentials.AnonymousCredentials(), + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() ) + response = await client.list_operations(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 - # Every method on the transport should just blindly - # raise NotImplementedError. - methods = ( - "list_discovered_api_observations", - "get_discovered_api_observation", - "list_discovered_api_operations", - "get_discovered_api_operation", - "get_location", - "list_locations", - "get_operation", - "cancel_operation", - "delete_operation", - "list_operations", - ) - for method in methods: - with pytest.raises(NotImplementedError): - getattr(transport, method)(request=object()) + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) - with pytest.raises(NotImplementedError): - transport.close() - # Catch all for all remaining methods and properties - remainder = [ - "kind", - ] - for r in remainder: - with pytest.raises(NotImplementedError): - getattr(transport, r)() +def test_list_operations_field_headers(): + client = ApiHubDiscoveryClient( + credentials=ga_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 = operations_pb2.ListOperationsRequest() + request.name = "locations" -def test_api_hub_discovery_base_transport_with_credentials_file(): - # Instantiate the base transport with a credentials file - with mock.patch.object( - google.auth, "load_credentials_from_file", autospec=True - ) as load_creds, mock.patch( - "google.cloud.apihub_v1.services.api_hub_discovery.transports.ApiHubDiscoveryTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None - load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) - transport = transports.ApiHubDiscoveryTransport( - credentials_file="credentials.json", - quota_project_id="octopus", - ) - load_creds.assert_called_once_with( - "credentials.json", - scopes=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id="octopus", - ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + call.return_value = operations_pb2.ListOperationsResponse() + client.list_operations(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 -def test_api_hub_discovery_base_transport_with_adc(): - # Test the default credentials are used if credentials and credentials_file are None. - with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch( - "google.cloud.apihub_v1.services.api_hub_discovery.transports.ApiHubDiscoveryTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport = transports.ApiHubDiscoveryTransport() - adc.assert_called_once() + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -def test_api_hub_discovery_auth_adc(): - # If no credentials are provided, we should use ADC credentials. - with mock.patch.object(google.auth, "default", autospec=True) as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - ApiHubDiscoveryClient() - adc.assert_called_once_with( - scopes=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id=None, - ) +@pytest.mark.asyncio +async def test_list_operations_field_headers_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + ) + # 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 = operations_pb2.ListOperationsRequest() + request.name = "locations" -def test_api_hub_discovery_http_transport_client_cert_source_for_mtls(): - cred = ga_credentials.AnonymousCredentials() - with mock.patch( - "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" - ) as mock_configure_mtls_channel: - transports.ApiHubDiscoveryRestTransport( - credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() ) - mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) - + await client.list_operations(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 -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_api_hub_discovery_host_no_port(transport_name): - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), - client_options=client_options.ClientOptions( - api_endpoint="apihub.googleapis.com" - ), - transport=transport_name, - ) - assert client.transport._host == ( - "apihub.googleapis.com:443" - if transport_name in ["grpc", "grpc_asyncio"] - else "https://apihub.googleapis.com" - ) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_api_hub_discovery_host_with_port(transport_name): +def test_list_operations_from_dict(): client = ApiHubDiscoveryClient( credentials=ga_credentials.AnonymousCredentials(), - client_options=client_options.ClientOptions( - api_endpoint="apihub.googleapis.com:8000" - ), - transport=transport_name, - ) - assert client.transport._host == ( - "apihub.googleapis.com:8000" - if transport_name in ["grpc", "grpc_asyncio"] - else "https://apihub.googleapis.com:8000" ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.ListOperationsResponse() + + response = client.list_operations( + request={ + "name": "locations", + } + ) + call.assert_called() -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_api_hub_discovery_client_transport_session_collision(transport_name): - creds1 = ga_credentials.AnonymousCredentials() - creds2 = ga_credentials.AnonymousCredentials() - client1 = ApiHubDiscoveryClient( - credentials=creds1, - transport=transport_name, +@pytest.mark.asyncio +async def test_list_operations_from_dict_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), ) - client2 = ApiHubDiscoveryClient( - credentials=creds2, - transport=transport_name, - ) - session1 = client1.transport.list_discovered_api_observations._session - session2 = client2.transport.list_discovered_api_observations._session - assert session1 != session2 - session1 = client1.transport.get_discovered_api_observation._session - session2 = client2.transport.get_discovered_api_observation._session - assert session1 != session2 - session1 = client1.transport.list_discovered_api_operations._session - session2 = client2.transport.list_discovered_api_operations._session - assert session1 != session2 - session1 = client1.transport.get_discovered_api_operation._session - session2 = client2.transport.get_discovered_api_operation._session - assert session1 != session2 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() + ) + response = await client.list_operations( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_discovered_api_observation_path(): - project = "squid" - location = "clam" - discovered_api_observation = "whelk" - expected = "projects/{project}/locations/{location}/discoveredApiObservations/{discovered_api_observation}".format( - project=project, - location=location, - discovered_api_observation=discovered_api_observation, - ) - actual = ApiHubDiscoveryClient.discovered_api_observation_path( - project, location, discovered_api_observation +def test_list_locations(transport: str = "grpc"): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - assert expected == actual + # 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 = locations_pb2.ListLocationsRequest() -def test_parse_discovered_api_observation_path(): - expected = { - "project": "octopus", - "location": "oyster", - "discovered_api_observation": "nudibranch", - } - path = ApiHubDiscoveryClient.discovered_api_observation_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.ListLocationsResponse() + response = client.list_locations(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 - # Check that the path construction is reversible. - actual = ApiHubDiscoveryClient.parse_discovered_api_observation_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) -def test_discovered_api_operation_path(): - project = "cuttlefish" - location = "mussel" - discovered_api_observation = "winkle" - discovered_api_operation = "nautilus" - expected = "projects/{project}/locations/{location}/discoveredApiObservations/{discovered_api_observation}/discoveredApiOperations/{discovered_api_operation}".format( - project=project, - location=location, - discovered_api_observation=discovered_api_observation, - discovered_api_operation=discovered_api_operation, - ) - actual = ApiHubDiscoveryClient.discovered_api_operation_path( - project, location, discovered_api_observation, discovered_api_operation +@pytest.mark.asyncio +async def test_list_locations_async(transport: str = "grpc_asyncio"): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - assert expected == actual + # 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 = locations_pb2.ListLocationsRequest() -def test_parse_discovered_api_operation_path(): - expected = { - "project": "scallop", - "location": "abalone", - "discovered_api_observation": "squid", - "discovered_api_operation": "clam", - } - path = ApiHubDiscoveryClient.discovered_api_operation_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + response = await client.list_locations(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 - # Check that the path construction is reversible. - actual = ApiHubDiscoveryClient.parse_discovered_api_operation_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) -def test_plugin_instance_path(): - project = "whelk" - location = "octopus" - plugin = "oyster" - instance = "nudibranch" - expected = "projects/{project}/locations/{location}/plugins/{plugin}/instances/{instance}".format( - project=project, - location=location, - plugin=plugin, - instance=instance, - ) - actual = ApiHubDiscoveryClient.plugin_instance_path( - project, location, plugin, instance +def test_list_locations_field_headers(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), ) - assert expected == actual + # 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 = locations_pb2.ListLocationsRequest() + request.name = "locations" -def test_parse_plugin_instance_path(): - expected = { - "project": "cuttlefish", - "location": "mussel", - "plugin": "winkle", - "instance": "nautilus", - } - path = ApiHubDiscoveryClient.plugin_instance_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + call.return_value = locations_pb2.ListLocationsResponse() - # Check that the path construction is reversible. - actual = ApiHubDiscoveryClient.parse_plugin_instance_path(path) - assert expected == actual + client.list_locations(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", + "name=locations", + ) in kw["metadata"] -def test_common_billing_account_path(): - billing_account = "scallop" - expected = "billingAccounts/{billing_account}".format( - billing_account=billing_account, +@pytest.mark.asyncio +async def test_list_locations_field_headers_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), ) - actual = ApiHubDiscoveryClient.common_billing_account_path(billing_account) - assert expected == actual + # 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 = locations_pb2.ListLocationsRequest() + request.name = "locations" -def test_parse_common_billing_account_path(): - expected = { - "billing_account": "abalone", - } - path = ApiHubDiscoveryClient.common_billing_account_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + await client.list_locations(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 - # Check that the path construction is reversible. - actual = ApiHubDiscoveryClient.parse_common_billing_account_path(path) - assert expected == actual + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -def test_common_folder_path(): - folder = "squid" - expected = "folders/{folder}".format( - folder=folder, +def test_list_locations_from_dict(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), ) - actual = ApiHubDiscoveryClient.common_folder_path(folder) - assert expected == actual - + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.ListLocationsResponse() + + response = client.list_locations( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_parse_common_folder_path(): - expected = { - "folder": "clam", - } - path = ApiHubDiscoveryClient.common_folder_path(**expected) - # Check that the path construction is reversible. - actual = ApiHubDiscoveryClient.parse_common_folder_path(path) - assert expected == actual +@pytest.mark.asyncio +async def test_list_locations_from_dict_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + response = await client.list_locations( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_common_organization_path(): - organization = "whelk" - expected = "organizations/{organization}".format( - organization=organization, +def test_get_location(transport: str = "grpc"): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - actual = ApiHubDiscoveryClient.common_organization_path(organization) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() -def test_parse_common_organization_path(): - expected = { - "organization": "octopus", - } - path = ApiHubDiscoveryClient.common_organization_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.Location() + response = client.get_location(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 - # Check that the path construction is reversible. - actual = ApiHubDiscoveryClient.parse_common_organization_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) -def test_common_project_path(): - project = "oyster" - expected = "projects/{project}".format( - project=project, +@pytest.mark.asyncio +async def test_get_location_async(transport: str = "grpc_asyncio"): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - actual = ApiHubDiscoveryClient.common_project_path(project) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() -def test_parse_common_project_path(): - expected = { - "project": "nudibranch", - } - path = ApiHubDiscoveryClient.common_project_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + response = await client.get_location(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 - # Check that the path construction is reversible. - actual = ApiHubDiscoveryClient.parse_common_project_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) -def test_common_location_path(): - project = "cuttlefish" - location = "mussel" - expected = "projects/{project}/locations/{location}".format( - project=project, - location=location, - ) - actual = ApiHubDiscoveryClient.common_location_path(project, location) - assert expected == actual +def test_get_location_field_headers(): + client = ApiHubDiscoveryClient(credentials=ga_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 = locations_pb2.GetLocationRequest() + request.name = "locations/abc" -def test_parse_common_location_path(): - expected = { - "project": "winkle", - "location": "nautilus", - } - path = ApiHubDiscoveryClient.common_location_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + call.return_value = locations_pb2.Location() - # Check that the path construction is reversible. - actual = ApiHubDiscoveryClient.parse_common_location_path(path) - assert expected == actual + client.get_location(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", + "name=locations/abc", + ) in kw["metadata"] -def test_client_with_default_client_info(): - client_info = gapic_v1.client_info.ClientInfo() - with mock.patch.object( - transports.ApiHubDiscoveryTransport, "_prep_wrapped_messages" - ) as prep: - client = ApiHubDiscoveryClient( - credentials=ga_credentials.AnonymousCredentials(), - client_info=client_info, +@pytest.mark.asyncio +async def test_get_location_field_headers_async(): + client = ApiHubDiscoveryAsyncClient(credentials=async_anonymous_credentials()) + + # 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 = locations_pb2.GetLocationRequest() + request.name = "locations/abc" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() ) - prep.assert_called_once_with(client_info) + await client.get_location(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 - with mock.patch.object( - transports.ApiHubDiscoveryTransport, "_prep_wrapped_messages" - ) as prep: - transport_class = ApiHubDiscoveryClient.get_transport_class() - transport = transport_class( - credentials=ga_credentials.AnonymousCredentials(), - client_info=client_info, + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations/abc", + ) in kw["metadata"] + + +def test_get_location_from_dict(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.Location() + + response = client.get_location( + request={ + "name": "locations/abc", + } ) - prep.assert_called_once_with(client_info) + call.assert_called() + + +@pytest.mark.asyncio +async def test_get_location_from_dict_async(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + response = await client.get_location( + request={ + "name": "locations", + } + ) + call.assert_called() + + +def test_transport_close_grpc(): + client = ApiHubDiscoveryClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + with mock.patch.object( + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + +@pytest.mark.asyncio +async def test_transport_close_grpc_asyncio(): + client = ApiHubDiscoveryAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) + with mock.patch.object( + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + async with client: + close.assert_not_called() + close.assert_called_once() def test_transport_close_rest(): @@ -3410,6 +6840,7 @@ def test_transport_close_rest(): def test_client_ctx(): transports = [ "rest", + "grpc", ] for transport in transports: client = ApiHubDiscoveryClient( @@ -3426,7 +6857,8 @@ def test_client_ctx(): @pytest.mark.parametrize( "client_class,transport_class", [ - (ApiHubDiscoveryClient, transports.ApiHubDiscoveryRestTransport), + (ApiHubDiscoveryClient, transports.ApiHubDiscoveryGrpcTransport), + (ApiHubDiscoveryAsyncClient, transports.ApiHubDiscoveryGrpcAsyncIOTransport), ], ) def test_api_key_credentials(client_class, transport_class): diff --git a/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub_plugin.py b/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub_plugin.py index f23b94dbc389..1de1aa5d9201 100644 --- a/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub_plugin.py +++ b/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_api_hub_plugin.py @@ -67,6 +67,7 @@ from google.protobuf import timestamp_pb2 # type: ignore from google.cloud.apihub_v1.services.api_hub_plugin import ( + ApiHubPluginAsyncClient, ApiHubPluginClient, pagers, transports, @@ -237,6 +238,11 @@ def test__get_client_cert_source(): "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubPluginClient), ) +@mock.patch.object( + ApiHubPluginAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubPluginAsyncClient), +) def test__get_api_endpoint(): api_override = "foo.com" mock_client_cert_source = mock.Mock() @@ -364,6 +370,8 @@ def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code): @pytest.mark.parametrize( "client_class,transport_name", [ + (ApiHubPluginClient, "grpc"), + (ApiHubPluginAsyncClient, "grpc_asyncio"), (ApiHubPluginClient, "rest"), ], ) @@ -388,6 +396,8 @@ def test_api_hub_plugin_client_from_service_account_info(client_class, transport @pytest.mark.parametrize( "transport_class,transport_name", [ + (transports.ApiHubPluginGrpcTransport, "grpc"), + (transports.ApiHubPluginGrpcAsyncIOTransport, "grpc_asyncio"), (transports.ApiHubPluginRestTransport, "rest"), ], ) @@ -412,6 +422,8 @@ def test_api_hub_plugin_client_service_account_always_use_jwt( @pytest.mark.parametrize( "client_class,transport_name", [ + (ApiHubPluginClient, "grpc"), + (ApiHubPluginAsyncClient, "grpc_asyncio"), (ApiHubPluginClient, "rest"), ], ) @@ -443,17 +455,24 @@ def test_api_hub_plugin_client_from_service_account_file(client_class, transport def test_api_hub_plugin_client_get_transport_class(): transport = ApiHubPluginClient.get_transport_class() available_transports = [ + transports.ApiHubPluginGrpcTransport, transports.ApiHubPluginRestTransport, ] assert transport in available_transports - transport = ApiHubPluginClient.get_transport_class("rest") - assert transport == transports.ApiHubPluginRestTransport + transport = ApiHubPluginClient.get_transport_class("grpc") + assert transport == transports.ApiHubPluginGrpcTransport @pytest.mark.parametrize( "client_class,transport_class,transport_name", [ + (ApiHubPluginClient, transports.ApiHubPluginGrpcTransport, "grpc"), + ( + ApiHubPluginAsyncClient, + transports.ApiHubPluginGrpcAsyncIOTransport, + "grpc_asyncio", + ), (ApiHubPluginClient, transports.ApiHubPluginRestTransport, "rest"), ], ) @@ -462,6 +481,11 @@ def test_api_hub_plugin_client_get_transport_class(): "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubPluginClient), ) +@mock.patch.object( + ApiHubPluginAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubPluginAsyncClient), +) def test_api_hub_plugin_client_client_options( client_class, transport_class, transport_name ): @@ -595,6 +619,20 @@ def test_api_hub_plugin_client_client_options( @pytest.mark.parametrize( "client_class,transport_class,transport_name,use_client_cert_env", [ + (ApiHubPluginClient, transports.ApiHubPluginGrpcTransport, "grpc", "true"), + ( + ApiHubPluginAsyncClient, + transports.ApiHubPluginGrpcAsyncIOTransport, + "grpc_asyncio", + "true", + ), + (ApiHubPluginClient, transports.ApiHubPluginGrpcTransport, "grpc", "false"), + ( + ApiHubPluginAsyncClient, + transports.ApiHubPluginGrpcAsyncIOTransport, + "grpc_asyncio", + "false", + ), (ApiHubPluginClient, transports.ApiHubPluginRestTransport, "rest", "true"), (ApiHubPluginClient, transports.ApiHubPluginRestTransport, "rest", "false"), ], @@ -604,6 +642,11 @@ def test_api_hub_plugin_client_client_options( "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubPluginClient), ) +@mock.patch.object( + ApiHubPluginAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubPluginAsyncClient), +) @mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) def test_api_hub_plugin_client_mtls_env_auto( client_class, transport_class, transport_name, use_client_cert_env @@ -707,10 +750,15 @@ def test_api_hub_plugin_client_mtls_env_auto( ) -@pytest.mark.parametrize("client_class", [ApiHubPluginClient]) +@pytest.mark.parametrize("client_class", [ApiHubPluginClient, ApiHubPluginAsyncClient]) @mock.patch.object( ApiHubPluginClient, "DEFAULT_ENDPOINT", modify_default_endpoint(ApiHubPluginClient) ) +@mock.patch.object( + ApiHubPluginAsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(ApiHubPluginAsyncClient), +) def test_api_hub_plugin_client_get_mtls_endpoint_and_cert_source(client_class): mock_client_cert_source = mock.Mock() @@ -802,12 +850,17 @@ def test_api_hub_plugin_client_get_mtls_endpoint_and_cert_source(client_class): ) -@pytest.mark.parametrize("client_class", [ApiHubPluginClient]) +@pytest.mark.parametrize("client_class", [ApiHubPluginClient, ApiHubPluginAsyncClient]) @mock.patch.object( ApiHubPluginClient, "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ApiHubPluginClient), ) +@mock.patch.object( + ApiHubPluginAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ApiHubPluginAsyncClient), +) def test_api_hub_plugin_client_client_api_endpoint(client_class): mock_client_cert_source = client_cert_source_callback api_override = "foo.com" @@ -884,6 +937,12 @@ def test_api_hub_plugin_client_client_api_endpoint(client_class): @pytest.mark.parametrize( "client_class,transport_class,transport_name", [ + (ApiHubPluginClient, transports.ApiHubPluginGrpcTransport, "grpc"), + ( + ApiHubPluginAsyncClient, + transports.ApiHubPluginGrpcAsyncIOTransport, + "grpc_asyncio", + ), (ApiHubPluginClient, transports.ApiHubPluginRestTransport, "rest"), ], ) @@ -915,6 +974,18 @@ def test_api_hub_plugin_client_client_options_scopes( @pytest.mark.parametrize( "client_class,transport_class,transport_name,grpc_helpers", [ + ( + ApiHubPluginClient, + transports.ApiHubPluginGrpcTransport, + "grpc", + grpc_helpers, + ), + ( + ApiHubPluginAsyncClient, + transports.ApiHubPluginGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), (ApiHubPluginClient, transports.ApiHubPluginRestTransport, "rest", None), ], ) @@ -942,13 +1013,177 @@ def test_api_hub_plugin_client_client_options_credentials_file( ) -def test_get_plugin_rest_use_cached_wrapped_rpc(): +def test_api_hub_plugin_client_client_options_from_dict(): + with mock.patch( + "google.cloud.apihub_v1.services.api_hub_plugin.transports.ApiHubPluginGrpcTransport.__init__" + ) as grpc_transport: + grpc_transport.return_value = None + client = ApiHubPluginClient(client_options={"api_endpoint": "squid.clam.whelk"}) + grpc_transport.assert_called_once_with( + credentials=None, + credentials_file=None, + host="squid.clam.whelk", + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name,grpc_helpers", + [ + ( + ApiHubPluginClient, + transports.ApiHubPluginGrpcTransport, + "grpc", + grpc_helpers, + ), + ( + ApiHubPluginAsyncClient, + transports.ApiHubPluginGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), + ], +) +def test_api_hub_plugin_client_create_channel_credentials_file( + client_class, transport_class, transport_name, grpc_helpers +): + # Check the case credentials file is provided. + options = client_options.ClientOptions(credentials_file="credentials.json") + + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options, transport=transport_name) + patched.assert_called_once_with( + credentials=None, + credentials_file="credentials.json", + host=client._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE + ), + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + # test that the credentials from file are saved and used as the credentials. + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel" + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + file_creds = ga_credentials.AnonymousCredentials() + load_creds.return_value = (file_creds, None) + adc.return_value = (creds, None) + client = client_class(client_options=options, transport=transport_name) + create_channel.assert_called_with( + "apihub.googleapis.com:443", + credentials=file_creds, + credentials_file=None, + quota_project_id=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + scopes=None, + default_host="apihub.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.GetPluginRequest, + dict, + ], +) +def test_get_plugin(request_type, transport: str = "grpc"): + client = ApiHubPluginClient( + credentials=ga_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.get_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.Plugin( + name="name_value", + display_name="display_name_value", + description="description_value", + state=plugin_service.Plugin.State.ENABLED, + ownership_type=plugin_service.Plugin.OwnershipType.SYSTEM_OWNED, + plugin_category=common_fields.PluginCategory.API_GATEWAY, + gateway_type=plugin_service.GatewayType.APIGEE_X_AND_HYBRID, + ) + response = client.get_plugin(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = plugin_service.GetPluginRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, plugin_service.Plugin) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.state == plugin_service.Plugin.State.ENABLED + assert response.ownership_type == plugin_service.Plugin.OwnershipType.SYSTEM_OWNED + assert response.plugin_category == common_fields.PluginCategory.API_GATEWAY + assert response.gateway_type == plugin_service.GatewayType.APIGEE_X_AND_HYBRID + + +def test_get_plugin_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = plugin_service.GetPluginRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_plugin), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_plugin(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == plugin_service.GetPluginRequest( + name="name_value", + ) + + +def test_get_plugin_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -964,7 +1199,6 @@ def test_get_plugin_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.get_plugin] = mock_rpc - request = {} client.get_plugin(request) @@ -978,331 +1212,323 @@ def test_get_plugin_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_get_plugin_rest_required_fields(request_type=plugin_service.GetPluginRequest): - transport_class = transports.ApiHubPluginRestTransport - - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) - - # verify fields with default values are dropped +@pytest.mark.asyncio +async def test_get_plugin_async_use_cached_wrapped_rpc(transport: str = "grpc_asyncio"): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_plugin._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify required fields with default values are now present + # Ensure method has been cached + assert ( + client._client._transport.get_plugin + in client._client._transport._wrapped_methods + ) - jsonified_request["name"] = "name_value" + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.get_plugin + ] = mock_rpc - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_plugin._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + request = {} + await client.get_plugin(request) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type(**request_init) + await client.get_plugin(request) - # Designate an appropriate value for the returned response. - return_value = plugin_service.Plugin() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = plugin_service.Plugin.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) +@pytest.mark.asyncio +async def test_get_plugin_async( + transport: str = "grpc_asyncio", request_type=plugin_service.GetPluginRequest +): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # 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() - response = client.get_plugin(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.Plugin( + name="name_value", + display_name="display_name_value", + description="description_value", + state=plugin_service.Plugin.State.ENABLED, + ownership_type=plugin_service.Plugin.OwnershipType.SYSTEM_OWNED, + plugin_category=common_fields.PluginCategory.API_GATEWAY, + gateway_type=plugin_service.GatewayType.APIGEE_X_AND_HYBRID, + ) + ) + response = await client.get_plugin(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = plugin_service.GetPluginRequest() + assert args[0] == request + # Establish that the response is the type that we expect. + assert isinstance(response, plugin_service.Plugin) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.state == plugin_service.Plugin.State.ENABLED + assert response.ownership_type == plugin_service.Plugin.OwnershipType.SYSTEM_OWNED + assert response.plugin_category == common_fields.PluginCategory.API_GATEWAY + assert response.gateway_type == plugin_service.GatewayType.APIGEE_X_AND_HYBRID -def test_get_plugin_rest_unset_required_fields(): - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials - ) - unset_fields = transport.get_plugin._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) +@pytest.mark.asyncio +async def test_get_plugin_async_from_dict(): + await test_get_plugin_async(request_type=dict) -def test_get_plugin_rest_flattened(): +def test_get_plugin_field_headers(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = plugin_service.Plugin() + # 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 = plugin_service.GetPluginRequest() - # get arguments that satisfy an http rule for this method - sample_request = {"name": "projects/sample1/locations/sample2/plugins/sample3"} + request.name = "name_value" - # get truthy value for each flattened field - mock_args = dict( - name="name_value", - ) - mock_args.update(sample_request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_plugin), "__call__") as call: + call.return_value = plugin_service.Plugin() + client.get_plugin(request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = plugin_service.Plugin.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - - client.get_plugin(**mock_args) + # 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 underlying call was made with the expected - # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/plugins/*}" % client.transport._host, - args[1], - ) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] -def test_get_plugin_rest_flattened_error(transport: str = "rest"): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_get_plugin_field_headers_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.get_plugin( - plugin_service.GetPluginRequest(), - name="name_value", - ) - - -def test_enable_plugin_rest_use_cached_wrapped_rpc(): - # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, - # instead of constructing them on each call - with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - - # Should wrap all calls on client creation - assert wrapper_fn.call_count > 0 - wrapper_fn.reset_mock() + # 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 = plugin_service.GetPluginRequest() - # Ensure method has been cached - assert client._transport.enable_plugin in client._transport._wrapped_methods + request.name = "name_value" - # Replace cached wrapped function with mock - mock_rpc = mock.Mock() - mock_rpc.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_plugin), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.Plugin() ) - client._transport._wrapped_methods[client._transport.enable_plugin] = mock_rpc - - request = {} - client.enable_plugin(request) + await client.get_plugin(request) # Establish that the underlying gRPC stub method was called. - assert mock_rpc.call_count == 1 - - client.enable_plugin(request) - - # Establish that a new wrapper was not created for this call - assert wrapper_fn.call_count == 0 - assert mock_rpc.call_count == 2 + 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", + "name=name_value", + ) in kw["metadata"] -def test_enable_plugin_rest_required_fields( - request_type=plugin_service.EnablePluginRequest, -): - transport_class = transports.ApiHubPluginRestTransport - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) +def test_get_plugin_flattened(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), ) - # verify fields with default values are dropped - - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).enable_plugin._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - - # verify required fields with default values are now present - - jsonified_request["name"] = "name_value" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.Plugin() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_plugin( + name="name_value", + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).enable_plugin._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" +def test_get_plugin_flattened_error(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = plugin_service.Plugin() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_plugin( + plugin_service.GetPluginRequest(), + name="name_value", + ) - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = plugin_service.Plugin.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) +@pytest.mark.asyncio +async def test_get_plugin_flattened_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.Plugin() - response = client.enable_plugin(request) + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.Plugin() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_plugin( + name="name_value", + ) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_enable_plugin_rest_unset_required_fields(): - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_get_plugin_flattened_error_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.enable_plugin._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.get_plugin( + plugin_service.GetPluginRequest(), + name="name_value", + ) -def test_enable_plugin_rest_flattened(): +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.EnablePluginRequest, + dict, + ], +) +def test_enable_plugin(request_type, transport: str = "grpc"): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = plugin_service.Plugin() - - # get arguments that satisfy an http rule for this method - sample_request = {"name": "projects/sample1/locations/sample2/plugins/sample3"} + # 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() - # get truthy value for each flattened field - mock_args = dict( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.enable_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.Plugin( name="name_value", + display_name="display_name_value", + description="description_value", + state=plugin_service.Plugin.State.ENABLED, + ownership_type=plugin_service.Plugin.OwnershipType.SYSTEM_OWNED, + plugin_category=common_fields.PluginCategory.API_GATEWAY, + gateway_type=plugin_service.GatewayType.APIGEE_X_AND_HYBRID, ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = plugin_service.Plugin.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.enable_plugin(request) - client.enable_plugin(**mock_args) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = plugin_service.EnablePluginRequest() + assert args[0] == request - # Establish that the underlying call was made with the expected - # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/plugins/*}:enable" - % client.transport._host, - args[1], - ) + # Establish that the response is the type that we expect. + assert isinstance(response, plugin_service.Plugin) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.state == plugin_service.Plugin.State.ENABLED + assert response.ownership_type == plugin_service.Plugin.OwnershipType.SYSTEM_OWNED + assert response.plugin_category == common_fields.PluginCategory.API_GATEWAY + assert response.gateway_type == plugin_service.GatewayType.APIGEE_X_AND_HYBRID -def test_enable_plugin_rest_flattened_error(transport: str = "rest"): +def test_enable_plugin_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport="grpc", ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.enable_plugin( - plugin_service.EnablePluginRequest(), + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = plugin_service.EnablePluginRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.enable_plugin), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.enable_plugin(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == plugin_service.EnablePluginRequest( name="name_value", ) -def test_disable_plugin_rest_use_cached_wrapped_rpc(): +def test_enable_plugin_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1310,178 +1536,346 @@ def test_disable_plugin_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.disable_plugin in client._transport._wrapped_methods + assert client._transport.enable_plugin in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[client._transport.disable_plugin] = mock_rpc - + client._transport._wrapped_methods[client._transport.enable_plugin] = mock_rpc request = {} - client.disable_plugin(request) + client.enable_plugin(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.disable_plugin(request) + client.enable_plugin(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_disable_plugin_rest_required_fields( - request_type=plugin_service.DisablePluginRequest, +@pytest.mark.asyncio +async def test_enable_plugin_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubPluginRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.enable_plugin + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.enable_plugin + ] = mock_rpc + + request = {} + await client.enable_plugin(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.enable_plugin(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_enable_plugin_async( + transport: str = "grpc_asyncio", request_type=plugin_service.EnablePluginRequest +): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # verify fields with default values are dropped + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).disable_plugin._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.enable_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.Plugin( + name="name_value", + display_name="display_name_value", + description="description_value", + state=plugin_service.Plugin.State.ENABLED, + ownership_type=plugin_service.Plugin.OwnershipType.SYSTEM_OWNED, + plugin_category=common_fields.PluginCategory.API_GATEWAY, + gateway_type=plugin_service.GatewayType.APIGEE_X_AND_HYBRID, + ) + ) + response = await client.enable_plugin(request) - # verify required fields with default values are now present + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = plugin_service.EnablePluginRequest() + assert args[0] == request - jsonified_request["name"] = "name_value" + # Establish that the response is the type that we expect. + assert isinstance(response, plugin_service.Plugin) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.state == plugin_service.Plugin.State.ENABLED + assert response.ownership_type == plugin_service.Plugin.OwnershipType.SYSTEM_OWNED + assert response.plugin_category == common_fields.PluginCategory.API_GATEWAY + assert response.gateway_type == plugin_service.GatewayType.APIGEE_X_AND_HYBRID - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).disable_plugin._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" +@pytest.mark.asyncio +async def test_enable_plugin_async_from_dict(): + await test_enable_plugin_async(request_type=dict) + +def test_enable_plugin_field_headers(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = plugin_service.Plugin() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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 = plugin_service.EnablePluginRequest() - # Convert return value to protobuf type - return_value = plugin_service.Plugin.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.name = "name_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.enable_plugin), "__call__") as call: + call.return_value = plugin_service.Plugin() + client.enable_plugin(request) - response = client.disable_plugin(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] -def test_disable_plugin_rest_unset_required_fields(): - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_enable_plugin_field_headers_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.disable_plugin._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # 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 = plugin_service.EnablePluginRequest() + request.name = "name_value" -def test_disable_plugin_rest_flattened(): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.enable_plugin), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.Plugin() + ) + await client.enable_plugin(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", + "name=name_value", + ) in kw["metadata"] + + +def test_enable_plugin_flattened(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = plugin_service.Plugin() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.enable_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.Plugin() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.enable_plugin( + name="name_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = {"name": "projects/sample1/locations/sample2/plugins/sample3"} + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_enable_plugin_flattened_error(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.enable_plugin( + plugin_service.EnablePluginRequest(), name="name_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = plugin_service.Plugin.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.disable_plugin(**mock_args) +@pytest.mark.asyncio +async def test_enable_plugin_flattened_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.enable_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.Plugin() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.Plugin() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.enable_plugin( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/plugins/*}:disable" - % client.transport._host, - args[1], + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_enable_plugin_flattened_error_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.enable_plugin( + plugin_service.EnablePluginRequest(), + name="name_value", ) -def test_disable_plugin_rest_flattened_error(transport: str = "rest"): +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.DisablePluginRequest, + dict, + ], +) +def test_disable_plugin(request_type, transport: str = "grpc"): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.disable_plugin( - plugin_service.DisablePluginRequest(), + # 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.disable_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.Plugin( + name="name_value", + display_name="display_name_value", + description="description_value", + state=plugin_service.Plugin.State.ENABLED, + ownership_type=plugin_service.Plugin.OwnershipType.SYSTEM_OWNED, + plugin_category=common_fields.PluginCategory.API_GATEWAY, + gateway_type=plugin_service.GatewayType.APIGEE_X_AND_HYBRID, + ) + response = client.disable_plugin(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = plugin_service.DisablePluginRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, plugin_service.Plugin) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.state == plugin_service.Plugin.State.ENABLED + assert response.ownership_type == plugin_service.Plugin.OwnershipType.SYSTEM_OWNED + assert response.plugin_category == common_fields.PluginCategory.API_GATEWAY + assert response.gateway_type == plugin_service.GatewayType.APIGEE_X_AND_HYBRID + + +def test_disable_plugin_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = plugin_service.DisablePluginRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.disable_plugin), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.disable_plugin(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == plugin_service.DisablePluginRequest( name="name_value", ) -def test_create_plugin_rest_use_cached_wrapped_rpc(): +def test_disable_plugin_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1489,191 +1883,348 @@ def test_create_plugin_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.create_plugin in client._transport._wrapped_methods + assert client._transport.disable_plugin in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[client._transport.create_plugin] = mock_rpc - + client._transport._wrapped_methods[client._transport.disable_plugin] = mock_rpc request = {} - client.create_plugin(request) + client.disable_plugin(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.create_plugin(request) + client.disable_plugin(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_create_plugin_rest_required_fields( - request_type=plugin_service.CreatePluginRequest, +@pytest.mark.asyncio +async def test_disable_plugin_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubPluginRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.disable_plugin + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_plugin._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.disable_plugin + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.disable_plugin(request) - jsonified_request["parent"] = "parent_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_plugin._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("plugin_id",)) - jsonified_request.update(unset_fields) + await client.disable_plugin(request) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_disable_plugin_async( + transport: str = "grpc_asyncio", request_type=plugin_service.DisablePluginRequest +): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + 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.disable_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.Plugin( + name="name_value", + display_name="display_name_value", + description="description_value", + state=plugin_service.Plugin.State.ENABLED, + ownership_type=plugin_service.Plugin.OwnershipType.SYSTEM_OWNED, + plugin_category=common_fields.PluginCategory.API_GATEWAY, + gateway_type=plugin_service.GatewayType.APIGEE_X_AND_HYBRID, + ) + ) + response = await client.disable_plugin(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = plugin_service.DisablePluginRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, plugin_service.Plugin) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.state == plugin_service.Plugin.State.ENABLED + assert response.ownership_type == plugin_service.Plugin.OwnershipType.SYSTEM_OWNED + assert response.plugin_category == common_fields.PluginCategory.API_GATEWAY + assert response.gateway_type == plugin_service.GatewayType.APIGEE_X_AND_HYBRID + +@pytest.mark.asyncio +async def test_disable_plugin_async_from_dict(): + await test_disable_plugin_async(request_type=dict) + + +def test_disable_plugin_field_headers(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = plugin_service.Plugin() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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 = plugin_service.DisablePluginRequest() - # Convert return value to protobuf type - return_value = plugin_service.Plugin.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.name = "name_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.disable_plugin), "__call__") as call: + call.return_value = plugin_service.Plugin() + client.disable_plugin(request) - response = client.create_plugin(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] -def test_create_plugin_rest_unset_required_fields(): - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_disable_plugin_field_headers_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.create_plugin._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(("pluginId",)) - & set( - ( - "parent", - "plugin", - ) + # 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 = plugin_service.DisablePluginRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.disable_plugin), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.Plugin() ) - ) + await client.disable_plugin(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request -def test_create_plugin_rest_flattened(): + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] + + +def test_disable_plugin_flattened(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = plugin_service.Plugin() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.disable_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.Plugin() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.disable_plugin( + name="name_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( - parent="parent_value", - plugin=plugin_service.Plugin(name="name_value"), - plugin_id="plugin_id_value", + +def test_disable_plugin_flattened_error(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.disable_plugin( + plugin_service.DisablePluginRequest(), + name="name_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = plugin_service.Plugin.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_plugin(**mock_args) +@pytest.mark.asyncio +async def test_disable_plugin_flattened_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.disable_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.Plugin() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.Plugin() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.disable_plugin( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/plugins" % client.transport._host, - args[1], + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_disable_plugin_flattened_error_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.disable_plugin( + plugin_service.DisablePluginRequest(), + name="name_value", ) -def test_create_plugin_rest_flattened_error(transport: str = "rest"): +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.CreatePluginRequest, + dict, + ], +) +def test_create_plugin(request_type, transport: str = "grpc"): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.create_plugin( - plugin_service.CreatePluginRequest(), + # 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.create_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.Plugin( + name="name_value", + display_name="display_name_value", + description="description_value", + state=plugin_service.Plugin.State.ENABLED, + ownership_type=plugin_service.Plugin.OwnershipType.SYSTEM_OWNED, + plugin_category=common_fields.PluginCategory.API_GATEWAY, + gateway_type=plugin_service.GatewayType.APIGEE_X_AND_HYBRID, + ) + response = client.create_plugin(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = plugin_service.CreatePluginRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, plugin_service.Plugin) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.state == plugin_service.Plugin.State.ENABLED + assert response.ownership_type == plugin_service.Plugin.OwnershipType.SYSTEM_OWNED + assert response.plugin_category == common_fields.PluginCategory.API_GATEWAY + assert response.gateway_type == plugin_service.GatewayType.APIGEE_X_AND_HYBRID + + +def test_create_plugin_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = plugin_service.CreatePluginRequest( + parent="parent_value", + plugin_id="plugin_id_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_plugin), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.create_plugin(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == plugin_service.CreatePluginRequest( parent="parent_value", - plugin=plugin_service.Plugin(name="name_value"), plugin_id="plugin_id_value", ) -def test_list_plugins_rest_use_cached_wrapped_rpc(): +def test_create_plugin_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1681,256 +2232,358 @@ def test_list_plugins_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.list_plugins in client._transport._wrapped_methods + assert client._transport.create_plugin in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[client._transport.list_plugins] = mock_rpc - + client._transport._wrapped_methods[client._transport.create_plugin] = mock_rpc request = {} - client.list_plugins(request) + client.create_plugin(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.list_plugins(request) + client.create_plugin(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_list_plugins_rest_required_fields( - request_type=plugin_service.ListPluginsRequest, +@pytest.mark.asyncio +async def test_create_plugin_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubPluginRestTransport - - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - # verify fields with default values are dropped + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_plugins._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Ensure method has been cached + assert ( + client._client._transport.create_plugin + in client._client._transport._wrapped_methods + ) - # verify required fields with default values are now present + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.create_plugin + ] = mock_rpc - jsonified_request["parent"] = "parent_value" + request = {} + await client.create_plugin(request) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_plugins._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "filter", - "page_size", - "page_token", - ) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.create_plugin(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_create_plugin_async( + transport: str = "grpc_asyncio", request_type=plugin_service.CreatePluginRequest +): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + # 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.create_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.Plugin( + name="name_value", + display_name="display_name_value", + description="description_value", + state=plugin_service.Plugin.State.ENABLED, + ownership_type=plugin_service.Plugin.OwnershipType.SYSTEM_OWNED, + plugin_category=common_fields.PluginCategory.API_GATEWAY, + gateway_type=plugin_service.GatewayType.APIGEE_X_AND_HYBRID, + ) + ) + response = await client.create_plugin(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = plugin_service.CreatePluginRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, plugin_service.Plugin) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.state == plugin_service.Plugin.State.ENABLED + assert response.ownership_type == plugin_service.Plugin.OwnershipType.SYSTEM_OWNED + assert response.plugin_category == common_fields.PluginCategory.API_GATEWAY + assert response.gateway_type == plugin_service.GatewayType.APIGEE_X_AND_HYBRID + +@pytest.mark.asyncio +async def test_create_plugin_async_from_dict(): + await test_create_plugin_async(request_type=dict) + + +def test_create_plugin_field_headers(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = plugin_service.ListPluginsResponse() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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 = plugin_service.CreatePluginRequest() - # Convert return value to protobuf type - return_value = plugin_service.ListPluginsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.parent = "parent_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_plugin), "__call__") as call: + call.return_value = plugin_service.Plugin() + client.create_plugin(request) - response = client.list_plugins(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] -def test_list_plugins_rest_unset_required_fields(): - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_create_plugin_field_headers_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.list_plugins._get_unset_required_fields({}) - assert set(unset_fields) == ( - set( - ( - "filter", - "pageSize", - "pageToken", - ) + # 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 = plugin_service.CreatePluginRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_plugin), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.Plugin() ) - & set(("parent",)) - ) + await client.create_plugin(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request -def test_list_plugins_rest_flattened(): + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +def test_create_plugin_flattened(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = plugin_service.ListPluginsResponse() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.Plugin() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.create_plugin( + parent="parent_value", + plugin=plugin_service.Plugin(name="name_value"), + plugin_id="plugin_id_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].plugin + mock_val = plugin_service.Plugin(name="name_value") + assert arg == mock_val + arg = args[0].plugin_id + mock_val = "plugin_id_value" + assert arg == mock_val + + +def test_create_plugin_flattened_error(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + ) - # get truthy value for each flattened field - mock_args = dict( + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_plugin( + plugin_service.CreatePluginRequest(), parent="parent_value", + plugin=plugin_service.Plugin(name="name_value"), + plugin_id="plugin_id_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = plugin_service.ListPluginsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_plugins(**mock_args) +@pytest.mark.asyncio +async def test_create_plugin_flattened_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.Plugin() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.Plugin() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.create_plugin( + parent="parent_value", + plugin=plugin_service.Plugin(name="name_value"), + plugin_id="plugin_id_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/plugins" % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].plugin + mock_val = plugin_service.Plugin(name="name_value") + assert arg == mock_val + arg = args[0].plugin_id + mock_val = "plugin_id_value" + assert arg == mock_val -def test_list_plugins_rest_flattened_error(transport: str = "rest"): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_create_plugin_flattened_error_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.list_plugins( - plugin_service.ListPluginsRequest(), + await client.create_plugin( + plugin_service.CreatePluginRequest(), parent="parent_value", + plugin=plugin_service.Plugin(name="name_value"), + plugin_id="plugin_id_value", ) -def test_list_plugins_rest_pager(transport: str = "rest"): +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.ListPluginsRequest, + dict, + ], +) +def test_list_plugins(request_type, transport: str = "grpc"): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # TODO(kbandes): remove this mock unless there's a good reason for it. - # with mock.patch.object(path_template, 'transcode') as transcode: - # Set the response as a series of pages - response = ( - plugin_service.ListPluginsResponse( - plugins=[ - plugin_service.Plugin(), - plugin_service.Plugin(), - plugin_service.Plugin(), - ], - next_page_token="abc", - ), - plugin_service.ListPluginsResponse( - plugins=[], - next_page_token="def", - ), - plugin_service.ListPluginsResponse( - plugins=[ - plugin_service.Plugin(), - ], - next_page_token="ghi", - ), - plugin_service.ListPluginsResponse( - plugins=[ - plugin_service.Plugin(), - plugin_service.Plugin(), - ], - ), - ) - # Two responses for two calls - response = response + response + # 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() - # Wrap the values into proper Response objs - response = tuple( - plugin_service.ListPluginsResponse.to_json(x) for x in response + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_plugins), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.ListPluginsResponse( + next_page_token="next_page_token_value", ) - return_values = tuple(Response() for i in response) - for return_val, response_val in zip(return_values, response): - return_val._content = response_val.encode("UTF-8") - return_val.status_code = 200 - req.side_effect = return_values + response = client.list_plugins(request) - sample_request = {"parent": "projects/sample1/locations/sample2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = plugin_service.ListPluginsRequest() + assert args[0] == request - pager = client.list_plugins(request=sample_request) + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListPluginsPager) + assert response.next_page_token == "next_page_token_value" - results = list(pager) - assert len(results) == 6 - assert all(isinstance(i, plugin_service.Plugin) for i in results) - pages = list(client.list_plugins(request=sample_request).pages) - for page_, token in zip(pages, ["abc", "def", "ghi", ""]): - assert page_.raw_page.next_page_token == token +def test_list_plugins_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = plugin_service.ListPluginsRequest( + parent="parent_value", + filter="filter_value", + page_token="page_token_value", + ) -def test_delete_plugin_rest_use_cached_wrapped_rpc(): - # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, - # instead of constructing them on each call + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_plugins), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.list_plugins(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == plugin_service.ListPluginsRequest( + parent="parent_value", + filter="filter_value", + page_token="page_token_value", + ) + + +def test_list_plugins_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1938,374 +2591,513 @@ def test_delete_plugin_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.delete_plugin in client._transport._wrapped_methods + assert client._transport.list_plugins in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[client._transport.delete_plugin] = mock_rpc - + client._transport._wrapped_methods[client._transport.list_plugins] = mock_rpc request = {} - client.delete_plugin(request) + client.list_plugins(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - # Operation methods build a cached wrapper on first rpc call - # subsequent calls should use the cached wrapper + client.list_plugins(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_list_plugins_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 wrapper_fn.reset_mock() - client.delete_plugin(request) + # Ensure method has been cached + assert ( + client._client._transport.list_plugins + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.list_plugins + ] = mock_rpc + + request = {} + await client.list_plugins(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.list_plugins(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_delete_plugin_rest_required_fields( - request_type=plugin_service.DeletePluginRequest, +@pytest.mark.asyncio +async def test_list_plugins_async( + transport: str = "grpc_asyncio", request_type=plugin_service.ListPluginsRequest ): - transport_class = transports.ApiHubPluginRestTransport - - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # verify fields with default values are dropped + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_plugin._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_plugins), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.ListPluginsResponse( + next_page_token="next_page_token_value", + ) + ) + response = await client.list_plugins(request) - # verify required fields with default values are now present + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = plugin_service.ListPluginsRequest() + assert args[0] == request - jsonified_request["name"] = "name_value" + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListPluginsAsyncPager) + assert response.next_page_token == "next_page_token_value" - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_plugin._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" +@pytest.mark.asyncio +async def test_list_plugins_async_from_dict(): + await test_list_plugins_async(request_type=dict) + +def test_list_plugins_field_headers(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "delete", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # 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 = plugin_service.ListPluginsRequest() - response_value = Response() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) + request.parent = "parent_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_plugins), "__call__") as call: + call.return_value = plugin_service.ListPluginsResponse() + client.list_plugins(request) - response = client.delete_plugin(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] -def test_delete_plugin_rest_unset_required_fields(): - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_list_plugins_field_headers_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.delete_plugin._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # 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 = plugin_service.ListPluginsRequest() + request.parent = "parent_value" -def test_delete_plugin_rest_flattened(): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_plugins), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.ListPluginsResponse() + ) + await client.list_plugins(request) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request - # get arguments that satisfy an http rule for this method - sample_request = {"name": "projects/sample1/locations/sample2/plugins/sample3"} + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] - # get truthy value for each flattened field - mock_args = dict( - name="name_value", - ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +def test_list_plugins_flattened(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + ) - client.delete_plugin(**mock_args) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_plugins), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.ListPluginsResponse() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.list_plugins( + parent="parent_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/plugins/*}" % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val -def test_delete_plugin_rest_flattened_error(transport: str = "rest"): +def test_list_plugins_flattened_error(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.delete_plugin( - plugin_service.DeletePluginRequest(), - name="name_value", + client.list_plugins( + plugin_service.ListPluginsRequest(), + parent="parent_value", ) -def test_create_plugin_instance_rest_use_cached_wrapped_rpc(): - # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, - # instead of constructing them on each call - with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) +@pytest.mark.asyncio +async def test_list_plugins_flattened_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) - # Should wrap all calls on client creation - assert wrapper_fn.call_count > 0 - wrapper_fn.reset_mock() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_plugins), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.ListPluginsResponse() - # Ensure method has been cached - assert ( - client._transport.create_plugin_instance - in client._transport._wrapped_methods + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.ListPluginsResponse() ) - - # Replace cached wrapped function with mock - mock_rpc = mock.Mock() - mock_rpc.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.list_plugins( + parent="parent_value", ) - client._transport._wrapped_methods[ - client._transport.create_plugin_instance - ] = mock_rpc - - request = {} - client.create_plugin_instance(request) - - # Establish that the underlying gRPC stub method was called. - assert mock_rpc.call_count == 1 - - # Operation methods build a cached wrapper on first rpc call - # subsequent calls should use the cached wrapper - wrapper_fn.reset_mock() - - client.create_plugin_instance(request) - - # Establish that a new wrapper was not created for this call - assert wrapper_fn.call_count == 0 - assert mock_rpc.call_count == 2 + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val -def test_create_plugin_instance_rest_required_fields( - request_type=plugin_service.CreatePluginInstanceRequest, -): - transport_class = transports.ApiHubPluginRestTransport - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) +@pytest.mark.asyncio +async def test_list_plugins_flattened_error_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - # verify fields with default values are dropped - - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_plugin_instance._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.list_plugins( + plugin_service.ListPluginsRequest(), + parent="parent_value", + ) - # verify required fields with default values are now present - jsonified_request["parent"] = "parent_value" +def test_list_plugins_pager(transport_name: str = "grpc"): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport_name, + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_plugin_instance._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("plugin_instance_id",)) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_plugins), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( + plugin_service.ListPluginsResponse( + plugins=[ + plugin_service.Plugin(), + plugin_service.Plugin(), + plugin_service.Plugin(), + ], + next_page_token="abc", + ), + plugin_service.ListPluginsResponse( + plugins=[], + next_page_token="def", + ), + plugin_service.ListPluginsResponse( + plugins=[ + plugin_service.Plugin(), + ], + next_page_token="ghi", + ), + plugin_service.ListPluginsResponse( + plugins=[ + plugin_service.Plugin(), + plugin_service.Plugin(), + ], + ), + RuntimeError, + ) + + expected_metadata = () + retry = retries.Retry() + timeout = 5 + expected_metadata = tuple(expected_metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", ""),)), + ) + pager = client.list_plugins(request={}, retry=retry, timeout=timeout) + + assert pager._metadata == expected_metadata + assert pager._retry == retry + assert pager._timeout == timeout + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, plugin_service.Plugin) for i in results) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" +def test_list_plugins_pages(transport_name: str = "grpc"): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport_name, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_plugins), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( + plugin_service.ListPluginsResponse( + plugins=[ + plugin_service.Plugin(), + plugin_service.Plugin(), + plugin_service.Plugin(), + ], + next_page_token="abc", + ), + plugin_service.ListPluginsResponse( + plugins=[], + next_page_token="def", + ), + plugin_service.ListPluginsResponse( + plugins=[ + plugin_service.Plugin(), + ], + next_page_token="ghi", + ), + plugin_service.ListPluginsResponse( + plugins=[ + plugin_service.Plugin(), + plugin_service.Plugin(), + ], + ), + RuntimeError, + ) + pages = list(client.list_plugins(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token - response_value = Response() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +@pytest.mark.asyncio +async def test_list_plugins_async_pager(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) - response = client.create_plugin_instance(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_plugins), "__call__", new_callable=mock.AsyncMock + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + plugin_service.ListPluginsResponse( + plugins=[ + plugin_service.Plugin(), + plugin_service.Plugin(), + plugin_service.Plugin(), + ], + next_page_token="abc", + ), + plugin_service.ListPluginsResponse( + plugins=[], + next_page_token="def", + ), + plugin_service.ListPluginsResponse( + plugins=[ + plugin_service.Plugin(), + ], + next_page_token="ghi", + ), + plugin_service.ListPluginsResponse( + plugins=[ + plugin_service.Plugin(), + plugin_service.Plugin(), + ], + ), + RuntimeError, + ) + async_pager = await client.list_plugins( + request={}, + ) + assert async_pager.next_page_token == "abc" + responses = [] + async for response in async_pager: # pragma: no branch + responses.append(response) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + assert len(responses) == 6 + assert all(isinstance(i, plugin_service.Plugin) for i in responses) -def test_create_plugin_instance_rest_unset_required_fields(): - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_list_plugins_async_pages(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.create_plugin_instance._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(("pluginInstanceId",)) - & set( - ( - "parent", - "pluginInstance", - ) - ) - ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_plugins), "__call__", new_callable=mock.AsyncMock + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + plugin_service.ListPluginsResponse( + plugins=[ + plugin_service.Plugin(), + plugin_service.Plugin(), + plugin_service.Plugin(), + ], + next_page_token="abc", + ), + plugin_service.ListPluginsResponse( + plugins=[], + next_page_token="def", + ), + plugin_service.ListPluginsResponse( + plugins=[ + plugin_service.Plugin(), + ], + next_page_token="ghi", + ), + plugin_service.ListPluginsResponse( + plugins=[ + plugin_service.Plugin(), + plugin_service.Plugin(), + ], + ), + RuntimeError, + ) + pages = [] + # Workaround issue in python 3.9 related to code coverage by adding `# pragma: no branch` + # See https://github.com/googleapis/gapic-generator-python/pull/1174#issuecomment-1025132372 + async for page_ in ( # pragma: no branch + await client.list_plugins(request={}) + ).pages: + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token -def test_create_plugin_instance_rest_flattened(): +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.DeletePluginRequest, + dict, + ], +) +def test_delete_plugin(request_type, transport: str = "grpc"): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") - - # get arguments that satisfy an http rule for this method - sample_request = { - "parent": "projects/sample1/locations/sample2/plugins/sample3" - } - - # get truthy value for each flattened field - mock_args = dict( - parent="parent_value", - plugin_instance=plugin_service.PluginInstance(name="name_value"), - plugin_instance_id="plugin_instance_id_value", - ) - mock_args.update(sample_request) + # 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() - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/spam") + response = client.delete_plugin(request) - client.create_plugin_instance(**mock_args) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = plugin_service.DeletePluginRequest() + assert args[0] == request - # Establish that the underlying call was made with the expected - # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*/plugins/*}/instances" - % client.transport._host, - args[1], - ) + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) -def test_create_plugin_instance_rest_flattened_error(transport: str = "rest"): +def test_delete_plugin_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport="grpc", ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.create_plugin_instance( - plugin_service.CreatePluginInstanceRequest(), - parent="parent_value", - plugin_instance=plugin_service.PluginInstance(name="name_value"), - plugin_instance_id="plugin_instance_id_value", + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = plugin_service.DeletePluginRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_plugin), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.delete_plugin(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == plugin_service.DeletePluginRequest( + name="name_value", ) -def test_execute_plugin_instance_action_rest_use_cached_wrapped_rpc(): +def test_delete_plugin_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -2313,200 +3105,332 @@ def test_execute_plugin_instance_action_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert ( - client._transport.execute_plugin_instance_action - in client._transport._wrapped_methods - ) + assert client._transport.delete_plugin in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[ - client._transport.execute_plugin_instance_action - ] = mock_rpc - + client._transport._wrapped_methods[client._transport.delete_plugin] = mock_rpc request = {} - client.execute_plugin_instance_action(request) + client.delete_plugin(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - # Operation methods build a cached wrapper on first rpc call - # subsequent calls should use the cached wrapper + # Operation methods call wrapper_fn to build a cached + # client._transport.operations_client instance on first rpc call. + # Subsequent calls should use the cached wrapper wrapper_fn.reset_mock() - client.execute_plugin_instance_action(request) + client.delete_plugin(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_execute_plugin_instance_action_rest_required_fields( - request_type=plugin_service.ExecutePluginInstanceActionRequest, +@pytest.mark.asyncio +async def test_delete_plugin_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubPluginRestTransport - - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.delete_plugin + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.delete_plugin + ] = mock_rpc + + request = {} + await client.delete_plugin(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + # Operation methods call wrapper_fn to build a cached + # client._transport.operations_client instance on first rpc call. + # Subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() + + await client.delete_plugin(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_delete_plugin_async( + transport: str = "grpc_asyncio", request_type=plugin_service.DeletePluginRequest +): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # verify fields with default values are dropped + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).execute_plugin_instance_action._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + response = await client.delete_plugin(request) - # verify required fields with default values are now present + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = plugin_service.DeletePluginRequest() + assert args[0] == request - jsonified_request["name"] = "name_value" + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).execute_plugin_instance_action._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" +@pytest.mark.asyncio +async def test_delete_plugin_async_from_dict(): + await test_delete_plugin_async(request_type=dict) + +def test_delete_plugin_field_headers(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # 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 = plugin_service.DeletePluginRequest() - response_value = Response() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) + request.name = "name_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_plugin), "__call__") as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.delete_plugin(request) - response = client.execute_plugin_instance_action(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] -def test_execute_plugin_instance_action_rest_unset_required_fields(): - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_delete_plugin_field_headers_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.execute_plugin_instance_action._get_unset_required_fields( - {} - ) - assert set(unset_fields) == ( - set(()) - & set( - ( - "name", - "actionExecutionDetail", - ) + # 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 = plugin_service.DeletePluginRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_plugin), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/op") ) - ) + await client.delete_plugin(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", + "name=name_value", + ) in kw["metadata"] -def test_execute_plugin_instance_action_rest_flattened(): + +def test_delete_plugin_flattened(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.delete_plugin( + name="name_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_delete_plugin_flattened_error(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_plugin( + plugin_service.DeletePluginRequest(), name="name_value", - action_execution_detail=plugin_service.ActionExecutionDetail( - action_id="action_id_value" - ), ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.execute_plugin_instance_action(**mock_args) +@pytest.mark.asyncio +async def test_delete_plugin_flattened_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.delete_plugin( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/plugins/*/instances/*}:executeAction" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_execute_plugin_instance_action_rest_flattened_error(transport: str = "rest"): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_delete_plugin_flattened_error_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.execute_plugin_instance_action( - plugin_service.ExecutePluginInstanceActionRequest(), + await client.delete_plugin( + plugin_service.DeletePluginRequest(), name="name_value", - action_execution_detail=plugin_service.ActionExecutionDetail( - action_id="action_id_value" - ), ) -def test_get_plugin_instance_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.CreatePluginInstanceRequest, + dict, + ], +) +def test_create_plugin_instance(request_type, transport: str = "grpc"): + client = ApiHubPluginClient( + credentials=ga_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.create_plugin_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/spam") + response = client.create_plugin_instance(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = plugin_service.CreatePluginInstanceRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) + + +def test_create_plugin_instance_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = plugin_service.CreatePluginInstanceRequest( + parent="parent_value", + plugin_instance_id="plugin_instance_id_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_plugin_instance), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.create_plugin_instance(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == plugin_service.CreatePluginInstanceRequest( + parent="parent_value", + plugin_instance_id="plugin_instance_id_value", + ) + + +def test_create_plugin_instance_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -2515,7 +3439,8 @@ def test_get_plugin_instance_rest_use_cached_wrapped_rpc(): # Ensure method has been cached assert ( - client._transport.get_plugin_instance in client._transport._wrapped_methods + client._transport.create_plugin_instance + in client._transport._wrapped_methods ) # Replace cached wrapped function with mock @@ -2524,173 +3449,355 @@ def test_get_plugin_instance_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[ - client._transport.get_plugin_instance + client._transport.create_plugin_instance ] = mock_rpc - request = {} - client.get_plugin_instance(request) + client.create_plugin_instance(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.get_plugin_instance(request) + # Operation methods call wrapper_fn to build a cached + # client._transport.operations_client instance on first rpc call. + # Subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() + + client.create_plugin_instance(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_get_plugin_instance_rest_required_fields( - request_type=plugin_service.GetPluginInstanceRequest, +@pytest.mark.asyncio +async def test_create_plugin_instance_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubPluginRestTransport - - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - # verify fields with default values are dropped + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_plugin_instance._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Ensure method has been cached + assert ( + client._client._transport.create_plugin_instance + in client._client._transport._wrapped_methods + ) - # verify required fields with default values are now present + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.create_plugin_instance + ] = mock_rpc - jsonified_request["name"] = "name_value" + request = {} + await client.create_plugin_instance(request) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_plugin_instance._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + # Operation methods call wrapper_fn to build a cached + # client._transport.operations_client instance on first rpc call. + # Subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() + + await client.create_plugin_instance(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_create_plugin_instance_async( + transport: str = "grpc_asyncio", + request_type=plugin_service.CreatePluginInstanceRequest, +): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + 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.create_plugin_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + response = await client.create_plugin_instance(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = plugin_service.CreatePluginInstanceRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) + + +@pytest.mark.asyncio +async def test_create_plugin_instance_async_from_dict(): + await test_create_plugin_instance_async(request_type=dict) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" +def test_create_plugin_instance_field_headers(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = plugin_service.PluginInstance() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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 = plugin_service.CreatePluginInstanceRequest() - # Convert return value to protobuf type - return_value = plugin_service.PluginInstance.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.parent = "parent_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_plugin_instance), "__call__" + ) as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.create_plugin_instance(request) - response = client.get_plugin_instance(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] -def test_get_plugin_instance_rest_unset_required_fields(): - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_create_plugin_instance_field_headers_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.get_plugin_instance._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # 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 = plugin_service.CreatePluginInstanceRequest() + request.parent = "parent_value" -def test_get_plugin_instance_rest_flattened(): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_plugin_instance), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/op") + ) + await client.create_plugin_instance(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", + "parent=parent_value", + ) in kw["metadata"] + + +def test_create_plugin_instance_flattened(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = plugin_service.PluginInstance() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_plugin_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.create_plugin_instance( + parent="parent_value", + plugin_instance=plugin_service.PluginInstance(name="name_value"), + plugin_instance_id="plugin_instance_id_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].plugin_instance + mock_val = plugin_service.PluginInstance(name="name_value") + assert arg == mock_val + arg = args[0].plugin_instance_id + mock_val = "plugin_instance_id_value" + assert arg == mock_val + + +def test_create_plugin_instance_flattened_error(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + ) - # get truthy value for each flattened field - mock_args = dict( - name="name_value", + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_plugin_instance( + plugin_service.CreatePluginInstanceRequest(), + parent="parent_value", + plugin_instance=plugin_service.PluginInstance(name="name_value"), + plugin_instance_id="plugin_instance_id_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = plugin_service.PluginInstance.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_plugin_instance(**mock_args) +@pytest.mark.asyncio +async def test_create_plugin_instance_flattened_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_plugin_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.create_plugin_instance( + parent="parent_value", + plugin_instance=plugin_service.PluginInstance(name="name_value"), + plugin_instance_id="plugin_instance_id_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/plugins/*/instances/*}" - % client.transport._host, - args[1], + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].plugin_instance + mock_val = plugin_service.PluginInstance(name="name_value") + assert arg == mock_val + arg = args[0].plugin_instance_id + mock_val = "plugin_instance_id_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_create_plugin_instance_flattened_error_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.create_plugin_instance( + plugin_service.CreatePluginInstanceRequest(), + parent="parent_value", + plugin_instance=plugin_service.PluginInstance(name="name_value"), + plugin_instance_id="plugin_instance_id_value", ) -def test_get_plugin_instance_rest_flattened_error(transport: str = "rest"): +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.ExecutePluginInstanceActionRequest, + dict, + ], +) +def test_execute_plugin_instance_action(request_type, transport: str = "grpc"): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.get_plugin_instance( - plugin_service.GetPluginInstanceRequest(), + # 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.execute_plugin_instance_action), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/spam") + response = client.execute_plugin_instance_action(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = plugin_service.ExecutePluginInstanceActionRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) + + +def test_execute_plugin_instance_action_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = plugin_service.ExecutePluginInstanceActionRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.execute_plugin_instance_action), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.execute_plugin_instance_action(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == plugin_service.ExecutePluginInstanceActionRequest( name="name_value", ) -def test_list_plugin_instances_rest_use_cached_wrapped_rpc(): +def test_execute_plugin_instance_action_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -2699,7 +3806,7 @@ def test_list_plugin_instances_rest_use_cached_wrapped_rpc(): # Ensure method has been cached assert ( - client._transport.list_plugin_instances + client._transport.execute_plugin_instance_action in client._transport._wrapped_methods ) @@ -2709,265 +3816,373 @@ def test_list_plugin_instances_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[ - client._transport.list_plugin_instances + client._transport.execute_plugin_instance_action ] = mock_rpc - request = {} - client.list_plugin_instances(request) + client.execute_plugin_instance_action(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.list_plugin_instances(request) + # Operation methods call wrapper_fn to build a cached + # client._transport.operations_client instance on first rpc call. + # Subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() + + client.execute_plugin_instance_action(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_list_plugin_instances_rest_required_fields( - request_type=plugin_service.ListPluginInstancesRequest, +@pytest.mark.asyncio +async def test_execute_plugin_instance_action_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubPluginRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.execute_plugin_instance_action + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_plugin_instances._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.execute_plugin_instance_action + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.execute_plugin_instance_action(request) - jsonified_request["parent"] = "parent_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_plugin_instances._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "filter", - "page_size", - "page_token", - ) + # Operation methods call wrapper_fn to build a cached + # client._transport.operations_client instance on first rpc call. + # Subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() + + await client.execute_plugin_instance_action(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_execute_plugin_instance_action_async( + transport: str = "grpc_asyncio", + request_type=plugin_service.ExecutePluginInstanceActionRequest, +): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + # 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.execute_plugin_instance_action), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + response = await client.execute_plugin_instance_action(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = plugin_service.ExecutePluginInstanceActionRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) + +@pytest.mark.asyncio +async def test_execute_plugin_instance_action_async_from_dict(): + await test_execute_plugin_instance_action_async(request_type=dict) + + +def test_execute_plugin_instance_action_field_headers(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = plugin_service.ListPluginInstancesResponse() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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 = plugin_service.ExecutePluginInstanceActionRequest() - # Convert return value to protobuf type - return_value = plugin_service.ListPluginInstancesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.name = "name_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.execute_plugin_instance_action), "__call__" + ) as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.execute_plugin_instance_action(request) - response = client.list_plugin_instances(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] -def test_list_plugin_instances_rest_unset_required_fields(): - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_execute_plugin_instance_action_field_headers_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.list_plugin_instances._get_unset_required_fields({}) - assert set(unset_fields) == ( - set( - ( - "filter", - "pageSize", - "pageToken", - ) + # 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 = plugin_service.ExecutePluginInstanceActionRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.execute_plugin_instance_action), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/op") ) - & set(("parent",)) - ) + await client.execute_plugin_instance_action(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request -def test_list_plugin_instances_rest_flattened(): + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] + + +def test_execute_plugin_instance_action_flattened(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = plugin_service.ListPluginInstancesResponse() - - # get arguments that satisfy an http rule for this method - sample_request = { - "parent": "projects/sample1/locations/sample2/plugins/sample3" - } - - # get truthy value for each flattened field - mock_args = dict( - parent="parent_value", + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.execute_plugin_instance_action), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.execute_plugin_instance_action( + name="name_value", + action_execution_detail=plugin_service.ActionExecutionDetail( + action_id="action_id_value" + ), ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = plugin_service.ListPluginInstancesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - - client.list_plugin_instances(**mock_args) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*/plugins/*}/instances" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + arg = args[0].action_execution_detail + mock_val = plugin_service.ActionExecutionDetail(action_id="action_id_value") + assert arg == mock_val -def test_list_plugin_instances_rest_flattened_error(transport: str = "rest"): +def test_execute_plugin_instance_action_flattened_error(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.list_plugin_instances( - plugin_service.ListPluginInstancesRequest(), - parent="parent_value", + client.execute_plugin_instance_action( + plugin_service.ExecutePluginInstanceActionRequest(), + name="name_value", + action_execution_detail=plugin_service.ActionExecutionDetail( + action_id="action_id_value" + ), ) -def test_list_plugin_instances_rest_pager(transport: str = "rest"): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_execute_plugin_instance_action_flattened_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # TODO(kbandes): remove this mock unless there's a good reason for it. - # with mock.patch.object(path_template, 'transcode') as transcode: - # Set the response as a series of pages - response = ( - plugin_service.ListPluginInstancesResponse( - plugin_instances=[ - plugin_service.PluginInstance(), - plugin_service.PluginInstance(), - plugin_service.PluginInstance(), - ], - next_page_token="abc", - ), - plugin_service.ListPluginInstancesResponse( - plugin_instances=[], - next_page_token="def", - ), - plugin_service.ListPluginInstancesResponse( - plugin_instances=[ - plugin_service.PluginInstance(), - ], - next_page_token="ghi", - ), - plugin_service.ListPluginInstancesResponse( - plugin_instances=[ - plugin_service.PluginInstance(), - plugin_service.PluginInstance(), - ], - ), - ) - # Two responses for two calls - response = response + response + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.execute_plugin_instance_action), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") - # Wrap the values into proper Response objs - response = tuple( - plugin_service.ListPluginInstancesResponse.to_json(x) for x in response + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.execute_plugin_instance_action( + name="name_value", + action_execution_detail=plugin_service.ActionExecutionDetail( + action_id="action_id_value" + ), ) - return_values = tuple(Response() for i in response) - for return_val, response_val in zip(return_values, response): - return_val._content = response_val.encode("UTF-8") - return_val.status_code = 200 - req.side_effect = return_values - - sample_request = { - "parent": "projects/sample1/locations/sample2/plugins/sample3" - } - - pager = client.list_plugin_instances(request=sample_request) - results = list(pager) - assert len(results) == 6 - assert all(isinstance(i, plugin_service.PluginInstance) for i in results) + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + arg = args[0].action_execution_detail + mock_val = plugin_service.ActionExecutionDetail(action_id="action_id_value") + assert arg == mock_val - pages = list(client.list_plugin_instances(request=sample_request).pages) - for page_, token in zip(pages, ["abc", "def", "ghi", ""]): - assert page_.raw_page.next_page_token == token +@pytest.mark.asyncio +async def test_execute_plugin_instance_action_flattened_error_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) -def test_enable_plugin_instance_action_rest_use_cached_wrapped_rpc(): - # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, - # instead of constructing them on each call - with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.execute_plugin_instance_action( + plugin_service.ExecutePluginInstanceActionRequest(), + name="name_value", + action_execution_detail=plugin_service.ActionExecutionDetail( + action_id="action_id_value" + ), ) - # Should wrap all calls on client creation - assert wrapper_fn.call_count > 0 - wrapper_fn.reset_mock() - # Ensure method has been cached +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.GetPluginInstanceRequest, + dict, + ], +) +def test_get_plugin_instance(request_type, transport: str = "grpc"): + client = ApiHubPluginClient( + credentials=ga_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.get_plugin_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.PluginInstance( + name="name_value", + display_name="display_name_value", + state=plugin_service.PluginInstance.State.CREATING, + error_message="error_message_value", + source_project_id="source_project_id_value", + ) + response = client.get_plugin_instance(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = plugin_service.GetPluginInstanceRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, plugin_service.PluginInstance) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.state == plugin_service.PluginInstance.State.CREATING + assert response.error_message == "error_message_value" + assert response.source_project_id == "source_project_id_value" + + +def test_get_plugin_instance_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = plugin_service.GetPluginInstanceRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_plugin_instance), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_plugin_instance(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == plugin_service.GetPluginInstanceRequest( + name="name_value", + ) + + +def test_get_plugin_instance_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached assert ( - client._transport.enable_plugin_instance_action - in client._transport._wrapped_methods + client._transport.get_plugin_instance in client._transport._wrapped_methods ) # Replace cached wrapped function with mock @@ -2976,390 +4191,343 @@ def test_enable_plugin_instance_action_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[ - client._transport.enable_plugin_instance_action + client._transport.get_plugin_instance ] = mock_rpc - request = {} - client.enable_plugin_instance_action(request) + client.get_plugin_instance(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - # Operation methods build a cached wrapper on first rpc call - # subsequent calls should use the cached wrapper + client.get_plugin_instance(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_get_plugin_instance_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 wrapper_fn.reset_mock() - client.enable_plugin_instance_action(request) + # Ensure method has been cached + assert ( + client._client._transport.get_plugin_instance + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.get_plugin_instance + ] = mock_rpc + + request = {} + await client.get_plugin_instance(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.get_plugin_instance(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_enable_plugin_instance_action_rest_required_fields( - request_type=plugin_service.EnablePluginInstanceActionRequest, +@pytest.mark.asyncio +async def test_get_plugin_instance_async( + transport: str = "grpc_asyncio", + request_type=plugin_service.GetPluginInstanceRequest, ): - transport_class = transports.ApiHubPluginRestTransport - - request_init = {} - request_init["name"] = "" - request_init["action_id"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # verify fields with default values are dropped + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).enable_plugin_instance_action._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_plugin_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.PluginInstance( + name="name_value", + display_name="display_name_value", + state=plugin_service.PluginInstance.State.CREATING, + error_message="error_message_value", + source_project_id="source_project_id_value", + ) + ) + response = await client.get_plugin_instance(request) - # verify required fields with default values are now present + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = plugin_service.GetPluginInstanceRequest() + assert args[0] == request - jsonified_request["name"] = "name_value" - jsonified_request["actionId"] = "action_id_value" + # Establish that the response is the type that we expect. + assert isinstance(response, plugin_service.PluginInstance) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.state == plugin_service.PluginInstance.State.CREATING + assert response.error_message == "error_message_value" + assert response.source_project_id == "source_project_id_value" - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).enable_plugin_instance_action._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" - assert "actionId" in jsonified_request - assert jsonified_request["actionId"] == "action_id_value" +@pytest.mark.asyncio +async def test_get_plugin_instance_async_from_dict(): + await test_get_plugin_instance_async(request_type=dict) + +def test_get_plugin_instance_field_headers(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # 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 = plugin_service.GetPluginInstanceRequest() - response_value = Response() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) + request.name = "name_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_plugin_instance), "__call__" + ) as call: + call.return_value = plugin_service.PluginInstance() + client.get_plugin_instance(request) - response = client.enable_plugin_instance_action(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] -def test_enable_plugin_instance_action_rest_unset_required_fields(): - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_get_plugin_instance_field_headers_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.enable_plugin_instance_action._get_unset_required_fields( - {} - ) - assert set(unset_fields) == ( - set(()) - & set( - ( - "name", - "actionId", - ) + # 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 = plugin_service.GetPluginInstanceRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_plugin_instance), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.PluginInstance() ) - ) + await client.get_plugin_instance(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request -def test_enable_plugin_instance_action_rest_flattened(): + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] + + +def test_get_plugin_instance_flattened(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") - - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" - } - - # get truthy value for each flattened field - mock_args = dict( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_plugin_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.PluginInstance() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_plugin_instance( name="name_value", - action_id="action_id_value", ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - - client.enable_plugin_instance_action(**mock_args) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/plugins/*/instances/*}:enableAction" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_enable_plugin_instance_action_rest_flattened_error(transport: str = "rest"): +def test_get_plugin_instance_flattened_error(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.enable_plugin_instance_action( - plugin_service.EnablePluginInstanceActionRequest(), + client.get_plugin_instance( + plugin_service.GetPluginInstanceRequest(), name="name_value", - action_id="action_id_value", ) -def test_disable_plugin_instance_action_rest_use_cached_wrapped_rpc(): - # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, - # instead of constructing them on each call - with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) +@pytest.mark.asyncio +async def test_get_plugin_instance_flattened_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) - # Should wrap all calls on client creation - assert wrapper_fn.call_count > 0 - wrapper_fn.reset_mock() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_plugin_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.PluginInstance() - # Ensure method has been cached - assert ( - client._transport.disable_plugin_instance_action - in client._transport._wrapped_methods + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.PluginInstance() ) - - # Replace cached wrapped function with mock - mock_rpc = mock.Mock() - mock_rpc.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_plugin_instance( + name="name_value", ) - client._transport._wrapped_methods[ - client._transport.disable_plugin_instance_action - ] = mock_rpc - - request = {} - client.disable_plugin_instance_action(request) - - # Establish that the underlying gRPC stub method was called. - assert mock_rpc.call_count == 1 - - # Operation methods build a cached wrapper on first rpc call - # subsequent calls should use the cached wrapper - wrapper_fn.reset_mock() - - client.disable_plugin_instance_action(request) - - # Establish that a new wrapper was not created for this call - assert wrapper_fn.call_count == 0 - assert mock_rpc.call_count == 2 - - -def test_disable_plugin_instance_action_rest_required_fields( - request_type=plugin_service.DisablePluginInstanceActionRequest, -): - transport_class = transports.ApiHubPluginRestTransport - - request_init = {} - request_init["name"] = "" - request_init["action_id"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) - - # verify fields with default values are dropped - - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).disable_plugin_instance_action._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - - # verify required fields with default values are now present - - jsonified_request["name"] = "name_value" - jsonified_request["actionId"] = "action_id_value" - - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).disable_plugin_instance_action._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" - assert "actionId" in jsonified_request - assert jsonified_request["actionId"] == "action_id_value" - - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result - - response_value = Response() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - - response = client.disable_plugin_instance_action(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_disable_plugin_instance_action_rest_unset_required_fields(): - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_get_plugin_instance_flattened_error_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.disable_plugin_instance_action._get_unset_required_fields( - {} - ) - assert set(unset_fields) == ( - set(()) - & set( - ( - "name", - "actionId", - ) + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.get_plugin_instance( + plugin_service.GetPluginInstanceRequest(), + name="name_value", ) - ) -def test_disable_plugin_instance_action_rest_flattened(): +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.ListPluginInstancesRequest, + dict, + ], +) +def test_list_plugin_instances(request_type, transport: str = "grpc"): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") - - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" - } + # 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() - # get truthy value for each flattened field - mock_args = dict( - name="name_value", - action_id="action_id_value", + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_plugin_instances), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.ListPluginInstancesResponse( + next_page_token="next_page_token_value", ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.list_plugin_instances(request) - client.disable_plugin_instance_action(**mock_args) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = plugin_service.ListPluginInstancesRequest() + assert args[0] == request - # Establish that the underlying call was made with the expected - # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/plugins/*/instances/*}:disableAction" - % client.transport._host, - args[1], - ) + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListPluginInstancesPager) + assert response.next_page_token == "next_page_token_value" -def test_disable_plugin_instance_action_rest_flattened_error(transport: str = "rest"): +def test_list_plugin_instances_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport="grpc", ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.disable_plugin_instance_action( - plugin_service.DisablePluginInstanceActionRequest(), - name="name_value", - action_id="action_id_value", + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = plugin_service.ListPluginInstancesRequest( + parent="parent_value", + filter="filter_value", + page_token="page_token_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_plugin_instances), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.list_plugin_instances(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == plugin_service.ListPluginInstancesRequest( + parent="parent_value", + filter="filter_value", + page_token="page_token_value", ) -def test_update_plugin_instance_rest_use_cached_wrapped_rpc(): +def test_list_plugin_instances_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -3368,7 +4536,7 @@ def test_update_plugin_instance_rest_use_cached_wrapped_rpc(): # Ensure method has been cached assert ( - client._transport.update_plugin_instance + client._transport.list_plugin_instances in client._transport._wrapped_methods ) @@ -3378,3809 +4546,10062 @@ def test_update_plugin_instance_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[ - client._transport.update_plugin_instance + client._transport.list_plugin_instances ] = mock_rpc - request = {} - client.update_plugin_instance(request) + client.list_plugin_instances(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.update_plugin_instance(request) + client.list_plugin_instances(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_update_plugin_instance_rest_required_fields( - request_type=plugin_service.UpdatePluginInstanceRequest, +@pytest.mark.asyncio +async def test_list_plugin_instances_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ApiHubPluginRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.list_plugin_instances + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_plugin_instance._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.list_plugin_instances + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.list_plugin_instances(request) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_plugin_instance._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("update_mask",)) - jsonified_request.update(unset_fields) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - # verify required fields with non-default values are left alone + await client.list_plugin_instances(request) - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_list_plugin_instances_async( + transport: str = "grpc_asyncio", + request_type=plugin_service.ListPluginInstancesRequest, +): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = plugin_service.PluginInstance() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "patch", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # 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() - response_value = Response() - response_value.status_code = 200 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_plugin_instances), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.ListPluginInstancesResponse( + next_page_token="next_page_token_value", + ) + ) + response = await client.list_plugin_instances(request) - # Convert return value to protobuf type - return_value = plugin_service.PluginInstance.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = plugin_service.ListPluginInstancesRequest() + assert args[0] == request - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListPluginInstancesAsyncPager) + assert response.next_page_token == "next_page_token_value" - response = client.update_plugin_instance(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_list_plugin_instances_async_from_dict(): + await test_list_plugin_instances_async(request_type=dict) -def test_update_plugin_instance_rest_unset_required_fields(): - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials +def test_list_plugin_instances_field_headers(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = transport.update_plugin_instance._get_unset_required_fields({}) - assert set(unset_fields) == (set(("updateMask",)) & set(("pluginInstance",))) + # 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 = plugin_service.ListPluginInstancesRequest() + request.parent = "parent_value" -def test_update_plugin_instance_rest_flattened(): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_plugin_instances), "__call__" + ) as call: + call.return_value = plugin_service.ListPluginInstancesResponse() + client.list_plugin_instances(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", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_list_plugin_instances_field_headers_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = plugin_service.PluginInstance() + # 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 = plugin_service.ListPluginInstancesRequest() - # get arguments that satisfy an http rule for this method - sample_request = { - "plugin_instance": { - "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" - } - } + request.parent = "parent_value" - # get truthy value for each flattened field - mock_args = dict( - plugin_instance=plugin_service.PluginInstance(name="name_value"), - update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_plugin_instances), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.ListPluginInstancesResponse() ) - mock_args.update(sample_request) + await client.list_plugin_instances(request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = plugin_service.PluginInstance.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request - client.update_plugin_instance(**mock_args) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +def test_list_plugin_instances_flattened(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_plugin_instances), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.ListPluginInstancesResponse() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.list_plugin_instances( + parent="parent_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{plugin_instance.name=projects/*/locations/*/plugins/*/instances/*}" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val -def test_update_plugin_instance_rest_flattened_error(transport: str = "rest"): +def test_list_plugin_instances_flattened_error(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.update_plugin_instance( - plugin_service.UpdatePluginInstanceRequest(), - plugin_instance=plugin_service.PluginInstance(name="name_value"), - update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + client.list_plugin_instances( + plugin_service.ListPluginInstancesRequest(), + parent="parent_value", ) -def test_delete_plugin_instance_rest_use_cached_wrapped_rpc(): - # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, - # instead of constructing them on each call - with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) +@pytest.mark.asyncio +async def test_list_plugin_instances_flattened_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) - # Should wrap all calls on client creation - assert wrapper_fn.call_count > 0 - wrapper_fn.reset_mock() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_plugin_instances), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.ListPluginInstancesResponse() - # Ensure method has been cached - assert ( - client._transport.delete_plugin_instance - in client._transport._wrapped_methods + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.ListPluginInstancesResponse() ) - - # Replace cached wrapped function with mock - mock_rpc = mock.Mock() - mock_rpc.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.list_plugin_instances( + parent="parent_value", ) - client._transport._wrapped_methods[ - client._transport.delete_plugin_instance - ] = mock_rpc - - request = {} - client.delete_plugin_instance(request) - # Establish that the underlying gRPC stub method was called. - assert mock_rpc.call_count == 1 - - # Operation methods build a cached wrapper on first rpc call - # subsequent calls should use the cached wrapper - wrapper_fn.reset_mock() + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val - client.delete_plugin_instance(request) - # Establish that a new wrapper was not created for this call - assert wrapper_fn.call_count == 0 - assert mock_rpc.call_count == 2 +@pytest.mark.asyncio +async def test_list_plugin_instances_flattened_error_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.list_plugin_instances( + plugin_service.ListPluginInstancesRequest(), + parent="parent_value", + ) -def test_delete_plugin_instance_rest_required_fields( - request_type=plugin_service.DeletePluginInstanceRequest, -): - transport_class = transports.ApiHubPluginRestTransport - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) +def test_list_plugin_instances_pager(transport_name: str = "grpc"): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport_name, ) - # verify fields with default values are dropped + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_plugin_instances), "__call__" + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + plugin_service.ListPluginInstancesResponse( + plugin_instances=[ + plugin_service.PluginInstance(), + plugin_service.PluginInstance(), + plugin_service.PluginInstance(), + ], + next_page_token="abc", + ), + plugin_service.ListPluginInstancesResponse( + plugin_instances=[], + next_page_token="def", + ), + plugin_service.ListPluginInstancesResponse( + plugin_instances=[ + plugin_service.PluginInstance(), + ], + next_page_token="ghi", + ), + plugin_service.ListPluginInstancesResponse( + plugin_instances=[ + plugin_service.PluginInstance(), + plugin_service.PluginInstance(), + ], + ), + RuntimeError, + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_plugin_instance._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + expected_metadata = () + retry = retries.Retry() + timeout = 5 + expected_metadata = tuple(expected_metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", ""),)), + ) + pager = client.list_plugin_instances(request={}, retry=retry, timeout=timeout) - # verify required fields with default values are now present + assert pager._metadata == expected_metadata + assert pager._retry == retry + assert pager._timeout == timeout - jsonified_request["name"] = "name_value" + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, plugin_service.PluginInstance) for i in results) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_plugin_instance._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" +def test_list_plugin_instances_pages(transport_name: str = "grpc"): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport_name, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "delete", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_plugin_instances), "__call__" + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + plugin_service.ListPluginInstancesResponse( + plugin_instances=[ + plugin_service.PluginInstance(), + plugin_service.PluginInstance(), + plugin_service.PluginInstance(), + ], + next_page_token="abc", + ), + plugin_service.ListPluginInstancesResponse( + plugin_instances=[], + next_page_token="def", + ), + plugin_service.ListPluginInstancesResponse( + plugin_instances=[ + plugin_service.PluginInstance(), + ], + next_page_token="ghi", + ), + plugin_service.ListPluginInstancesResponse( + plugin_instances=[ + plugin_service.PluginInstance(), + plugin_service.PluginInstance(), + ], + ), + RuntimeError, + ) + pages = list(client.list_plugin_instances(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token - response_value = Response() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +@pytest.mark.asyncio +async def test_list_plugin_instances_async_pager(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) - response = client.delete_plugin_instance(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_plugin_instances), + "__call__", + new_callable=mock.AsyncMock, + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + plugin_service.ListPluginInstancesResponse( + plugin_instances=[ + plugin_service.PluginInstance(), + plugin_service.PluginInstance(), + plugin_service.PluginInstance(), + ], + next_page_token="abc", + ), + plugin_service.ListPluginInstancesResponse( + plugin_instances=[], + next_page_token="def", + ), + plugin_service.ListPluginInstancesResponse( + plugin_instances=[ + plugin_service.PluginInstance(), + ], + next_page_token="ghi", + ), + plugin_service.ListPluginInstancesResponse( + plugin_instances=[ + plugin_service.PluginInstance(), + plugin_service.PluginInstance(), + ], + ), + RuntimeError, + ) + async_pager = await client.list_plugin_instances( + request={}, + ) + assert async_pager.next_page_token == "abc" + responses = [] + async for response in async_pager: # pragma: no branch + responses.append(response) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + assert len(responses) == 6 + assert all(isinstance(i, plugin_service.PluginInstance) for i in responses) -def test_delete_plugin_instance_rest_unset_required_fields(): - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_list_plugin_instances_async_pages(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.delete_plugin_instance._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_plugin_instances), + "__call__", + new_callable=mock.AsyncMock, + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + plugin_service.ListPluginInstancesResponse( + plugin_instances=[ + plugin_service.PluginInstance(), + plugin_service.PluginInstance(), + plugin_service.PluginInstance(), + ], + next_page_token="abc", + ), + plugin_service.ListPluginInstancesResponse( + plugin_instances=[], + next_page_token="def", + ), + plugin_service.ListPluginInstancesResponse( + plugin_instances=[ + plugin_service.PluginInstance(), + ], + next_page_token="ghi", + ), + plugin_service.ListPluginInstancesResponse( + plugin_instances=[ + plugin_service.PluginInstance(), + plugin_service.PluginInstance(), + ], + ), + RuntimeError, + ) + pages = [] + # Workaround issue in python 3.9 related to code coverage by adding `# pragma: no branch` + # See https://github.com/googleapis/gapic-generator-python/pull/1174#issuecomment-1025132372 + async for page_ in ( # pragma: no branch + await client.list_plugin_instances(request={}) + ).pages: + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token -def test_delete_plugin_instance_rest_flattened(): +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.EnablePluginInstanceActionRequest, + dict, + ], +) +def test_enable_plugin_instance_action(request_type, transport: str = "grpc"): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") - - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" - } - - # get truthy value for each flattened field - mock_args = dict( - name="name_value", - ) - mock_args.update(sample_request) + # 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() - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.enable_plugin_instance_action), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/spam") + response = client.enable_plugin_instance_action(request) - client.delete_plugin_instance(**mock_args) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = plugin_service.EnablePluginInstanceActionRequest() + assert args[0] == request - # Establish that the underlying call was made with the expected - # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/plugins/*/instances/*}" - % client.transport._host, - args[1], - ) + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) -def test_delete_plugin_instance_rest_flattened_error(transport: str = "rest"): +def test_enable_plugin_instance_action_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport="grpc", ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.delete_plugin_instance( - plugin_service.DeletePluginInstanceRequest(), + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = plugin_service.EnablePluginInstanceActionRequest( + name="name_value", + action_id="action_id_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.enable_plugin_instance_action), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.enable_plugin_instance_action(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == plugin_service.EnablePluginInstanceActionRequest( name="name_value", + action_id="action_id_value", ) -def test_credentials_transport_error(): - # It is an error to provide credentials and a transport instance. - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - with pytest.raises(ValueError): +def test_enable_plugin_instance_action_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport="grpc", ) - # It is an error to provide a credentials file and a transport instance. - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - with pytest.raises(ValueError): - client = ApiHubPluginClient( - client_options={"credentials_file": "credentials.json"}, - transport=transport, - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # It is an error to provide an api_key and a transport instance. - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = ApiHubPluginClient( - client_options=options, - transport=transport, + # Ensure method has been cached + assert ( + client._transport.enable_plugin_instance_action + in client._transport._wrapped_methods ) - # It is an error to provide an api_key and a credential. - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = ApiHubPluginClient( - client_options=options, credentials=ga_credentials.AnonymousCredentials() + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. ) + client._transport._wrapped_methods[ + client._transport.enable_plugin_instance_action + ] = mock_rpc + request = {} + client.enable_plugin_instance_action(request) - # It is an error to provide scopes and a transport instance. - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - with pytest.raises(ValueError): - client = ApiHubPluginClient( - client_options={"scopes": ["1", "2"]}, - transport=transport, - ) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + # Operation methods call wrapper_fn to build a cached + # client._transport.operations_client instance on first rpc call. + # Subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() -def test_transport_instance(): - # A client may be instantiated with a custom transport instance. - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - client = ApiHubPluginClient(transport=transport) - assert client.transport is transport + client.enable_plugin_instance_action(request) + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 -@pytest.mark.parametrize( - "transport_class", - [ - transports.ApiHubPluginRestTransport, - ], -) -def test_transport_adc(transport_class): - # Test default credentials are used if not provided. - with mock.patch.object(google.auth, "default") as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport_class() - adc.assert_called_once() +@pytest.mark.asyncio +async def test_enable_plugin_instance_action_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) -def test_transport_kind_rest(): - transport = ApiHubPluginClient.get_transport_class("rest")( - credentials=ga_credentials.AnonymousCredentials() - ) - assert transport.kind == "rest" + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + # Ensure method has been cached + assert ( + client._client._transport.enable_plugin_instance_action + in client._client._transport._wrapped_methods + ) -def test_get_plugin_rest_bad_request(request_type=plugin_service.GetPluginRequest): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/plugins/sample3"} - request = request_type(**request_init) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.enable_plugin_instance_action + ] = mock_rpc - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_plugin(request) + request = {} + await client.enable_plugin_instance_action(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + # Operation methods call wrapper_fn to build a cached + # client._transport.operations_client instance on first rpc call. + # Subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() -@pytest.mark.parametrize( - "request_type", - [ - plugin_service.GetPluginRequest, - dict, - ], -) -def test_get_plugin_rest_call_success(request_type): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + await client.enable_plugin_instance_action(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_enable_plugin_instance_action_async( + transport: str = "grpc_asyncio", + request_type=plugin_service.EnablePluginInstanceActionRequest, +): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/plugins/sample3"} - request = request_type(**request_init) + # 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 http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = plugin_service.Plugin( - name="name_value", - display_name="display_name_value", - description="description_value", - state=plugin_service.Plugin.State.ENABLED, - ownership_type=plugin_service.Plugin.OwnershipType.SYSTEM_OWNED, - plugin_category=common_fields.PluginCategory.API_GATEWAY, - gateway_type=plugin_service.GatewayType.APIGEE_X_AND_HYBRID, + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.enable_plugin_instance_action), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") ) + response = await client.enable_plugin_instance_action(request) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = plugin_service.Plugin.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_plugin(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = plugin_service.EnablePluginInstanceActionRequest() + assert args[0] == request # Establish that the response is the type that we expect. - assert isinstance(response, plugin_service.Plugin) - assert response.name == "name_value" - assert response.display_name == "display_name_value" - assert response.description == "description_value" - assert response.state == plugin_service.Plugin.State.ENABLED - assert response.ownership_type == plugin_service.Plugin.OwnershipType.SYSTEM_OWNED - assert response.plugin_category == common_fields.PluginCategory.API_GATEWAY - assert response.gateway_type == plugin_service.GatewayType.APIGEE_X_AND_HYBRID + assert isinstance(response, future.Future) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_plugin_rest_interceptors(null_interceptor): - transport = transports.ApiHubPluginRestTransport( +@pytest.mark.asyncio +async def test_enable_plugin_instance_action_async_from_dict(): + await test_enable_plugin_instance_action_async(request_type=dict) + + +def test_enable_plugin_instance_action_field_headers(): + client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubPluginRestInterceptor(), ) - client = ApiHubPluginClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_get_plugin" - ) as post, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_get_plugin_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "pre_get_plugin" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = plugin_service.GetPluginRequest.pb( - plugin_service.GetPluginRequest() - ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } + # 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 = plugin_service.EnablePluginInstanceActionRequest() - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = plugin_service.Plugin.to_json(plugin_service.Plugin()) - req.return_value.content = return_value + request.name = "name_value" - request = plugin_service.GetPluginRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = plugin_service.Plugin() - post_with_metadata.return_value = plugin_service.Plugin(), metadata + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.enable_plugin_instance_action), "__call__" + ) as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.enable_plugin_instance_action(request) - client.get_plugin( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], - ) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] -def test_enable_plugin_rest_bad_request( - request_type=plugin_service.EnablePluginRequest, -): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" +@pytest.mark.asyncio +async def test_enable_plugin_instance_action_field_headers_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/plugins/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.enable_plugin(request) + # 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 = plugin_service.EnablePluginInstanceActionRequest() + request.name = "name_value" -@pytest.mark.parametrize( - "request_type", - [ - plugin_service.EnablePluginRequest, - dict, - ], -) -def test_enable_plugin_rest_call_success(request_type): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.enable_plugin_instance_action), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/op") + ) + await client.enable_plugin_instance_action(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", + "name=name_value", + ) in kw["metadata"] + + +def test_enable_plugin_instance_action_flattened(): client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/plugins/sample3"} - request = request_type(**request_init) - - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = plugin_service.Plugin( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.enable_plugin_instance_action), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.enable_plugin_instance_action( name="name_value", - display_name="display_name_value", - description="description_value", - state=plugin_service.Plugin.State.ENABLED, - ownership_type=plugin_service.Plugin.OwnershipType.SYSTEM_OWNED, - plugin_category=common_fields.PluginCategory.API_GATEWAY, - gateway_type=plugin_service.GatewayType.APIGEE_X_AND_HYBRID, + action_id="action_id_value", ) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = plugin_service.Plugin.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.enable_plugin(request) - - # Establish that the response is the type that we expect. - assert isinstance(response, plugin_service.Plugin) - assert response.name == "name_value" - assert response.display_name == "display_name_value" - assert response.description == "description_value" - assert response.state == plugin_service.Plugin.State.ENABLED - assert response.ownership_type == plugin_service.Plugin.OwnershipType.SYSTEM_OWNED - assert response.plugin_category == common_fields.PluginCategory.API_GATEWAY - assert response.gateway_type == plugin_service.GatewayType.APIGEE_X_AND_HYBRID + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + arg = args[0].action_id + mock_val = "action_id_value" + assert arg == mock_val -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_enable_plugin_rest_interceptors(null_interceptor): - transport = transports.ApiHubPluginRestTransport( +def test_enable_plugin_instance_action_flattened_error(): + client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubPluginRestInterceptor(), ) - client = ApiHubPluginClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_enable_plugin" - ) as post, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_enable_plugin_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "pre_enable_plugin" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = plugin_service.EnablePluginRequest.pb( - plugin_service.EnablePluginRequest() + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.enable_plugin_instance_action( + plugin_service.EnablePluginInstanceActionRequest(), + name="name_value", + action_id="action_id_value", ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = plugin_service.Plugin.to_json(plugin_service.Plugin()) - req.return_value.content = return_value - request = plugin_service.EnablePluginRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = plugin_service.Plugin() - post_with_metadata.return_value = plugin_service.Plugin(), metadata +@pytest.mark.asyncio +async def test_enable_plugin_instance_action_flattened_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) - client.enable_plugin( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.enable_plugin_instance_action), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.enable_plugin_instance_action( + name="name_value", + action_id="action_id_value", ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + arg = args[0].action_id + mock_val = "action_id_value" + assert arg == mock_val -def test_disable_plugin_rest_bad_request( - request_type=plugin_service.DisablePluginRequest, -): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" +@pytest.mark.asyncio +async def test_enable_plugin_instance_action_flattened_error_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/plugins/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.disable_plugin(request) + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.enable_plugin_instance_action( + plugin_service.EnablePluginInstanceActionRequest(), + name="name_value", + action_id="action_id_value", + ) @pytest.mark.parametrize( "request_type", [ - plugin_service.DisablePluginRequest, + plugin_service.DisablePluginInstanceActionRequest, dict, ], ) -def test_disable_plugin_rest_call_success(request_type): +def test_disable_plugin_instance_action(request_type, transport: str = "grpc"): client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/plugins/sample3"} - request = request_type(**request_init) - - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = plugin_service.Plugin( - name="name_value", - display_name="display_name_value", - description="description_value", - state=plugin_service.Plugin.State.ENABLED, - ownership_type=plugin_service.Plugin.OwnershipType.SYSTEM_OWNED, - plugin_category=common_fields.PluginCategory.API_GATEWAY, - gateway_type=plugin_service.GatewayType.APIGEE_X_AND_HYBRID, - ) + # 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() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.disable_plugin_instance_action), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/spam") + response = client.disable_plugin_instance_action(request) - # Convert return value to protobuf type - return_value = plugin_service.Plugin.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.disable_plugin(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = plugin_service.DisablePluginInstanceActionRequest() + assert args[0] == request # Establish that the response is the type that we expect. - assert isinstance(response, plugin_service.Plugin) - assert response.name == "name_value" - assert response.display_name == "display_name_value" - assert response.description == "description_value" - assert response.state == plugin_service.Plugin.State.ENABLED - assert response.ownership_type == plugin_service.Plugin.OwnershipType.SYSTEM_OWNED - assert response.plugin_category == common_fields.PluginCategory.API_GATEWAY - assert response.gateway_type == plugin_service.GatewayType.APIGEE_X_AND_HYBRID + assert isinstance(response, future.Future) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_disable_plugin_rest_interceptors(null_interceptor): - transport = transports.ApiHubPluginRestTransport( +def test_disable_plugin_instance_action_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubPluginRestInterceptor(), + transport="grpc", ) - client = ApiHubPluginClient(transport=transport) + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = plugin_service.DisablePluginInstanceActionRequest( + name="name_value", + action_id="action_id_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_disable_plugin" - ) as post, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_disable_plugin_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "pre_disable_plugin" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = plugin_service.DisablePluginRequest.pb( - plugin_service.DisablePluginRequest() + type(client.transport.disable_plugin_instance_action), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.disable_plugin_instance_action(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == plugin_service.DisablePluginInstanceActionRequest( + name="name_value", + action_id="action_id_value", ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = plugin_service.Plugin.to_json(plugin_service.Plugin()) - req.return_value.content = return_value - request = plugin_service.DisablePluginRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = plugin_service.Plugin() - post_with_metadata.return_value = plugin_service.Plugin(), metadata +def test_disable_plugin_instance_action_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) - client.disable_plugin( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.disable_plugin_instance_action + in client._transport._wrapped_methods ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.disable_plugin_instance_action + ] = mock_rpc + request = {} + client.disable_plugin_instance_action(request) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 -def test_create_plugin_rest_bad_request( - request_type=plugin_service.CreatePluginRequest, -): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request = request_type(**request_init) + # Operation methods call wrapper_fn to build a cached + # client._transport.operations_client instance on first rpc call. + # Subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_plugin(request) + client.disable_plugin_instance_action(request) + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 -@pytest.mark.parametrize( - "request_type", - [ - plugin_service.CreatePluginRequest, - dict, - ], -) -def test_create_plugin_rest_call_success(request_type): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request_init["plugin"] = { - "name": "name_value", - "display_name": "display_name_value", - "type_": { - "enum_values": { - "values": [ - { - "id": "id_value", - "display_name": "display_name_value", - "description": "description_value", - "immutable": True, - } - ] - }, - "string_values": {"values": ["values_value1", "values_value2"]}, - "json_values": {}, - "uri_values": {}, - "attribute": "attribute_value", - }, - "description": "description_value", - "state": 1, - "ownership_type": 1, - "hosting_service": {"service_uri": "service_uri_value"}, - "actions_config": [ - { - "id": "id_value", - "display_name": "display_name_value", - "description": "description_value", - "trigger_mode": 1, - } - ], - "documentation": {"external_uri": "external_uri_value"}, - "plugin_category": 1, - "config_template": { - "auth_config_template": { - "supported_auth_types": [1], - "service_account": {"service_account": "service_account_value"}, - }, - "additional_config_template": [ - { - "id": "id_value", - "value_type": 1, - "description": "description_value", - "validation_regex": "validation_regex_value", - "required": True, - "enum_options": [ - { - "id": "id_value", - "display_name": "display_name_value", - "description": "description_value", - } - ], - "multi_select_options": {}, - } - ], - }, - "create_time": {"seconds": 751, "nanos": 543}, - "update_time": {}, - "gateway_type": 1, - } - # The version of a generated dependency at test runtime may differ from the version used during generation. - # Delete any fields which are not present in the current runtime dependency - # See https://github.com/googleapis/gapic-generator-python/issues/1748 +@pytest.mark.asyncio +async def test_disable_plugin_instance_action_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - # Determine if the message type is proto-plus or protobuf - test_field = plugin_service.CreatePluginRequest.meta.fields["plugin"] + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - def get_message_fields(field): - # Given a field which is a message (composite type), return a list with - # all the fields of the message. - # If the field is not a composite type, return an empty list. - message_fields = [] + # Ensure method has been cached + assert ( + client._client._transport.disable_plugin_instance_action + in client._client._transport._wrapped_methods + ) - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.disable_plugin_instance_action + ] = mock_rpc - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields + request = {} + await client.disable_plugin_instance_action(request) - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - subfields_not_in_runtime = [] + # Operation methods call wrapper_fn to build a cached + # client._transport.operations_client instance on first rpc call. + # Subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["plugin"].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value + await client.disable_plugin_instance_action(request) - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range(0, len(request_init["plugin"][field])): - del request_init["plugin"][field][i][subfield] - else: - del request_init["plugin"][field][subfield] - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = plugin_service.Plugin( - name="name_value", - display_name="display_name_value", - description="description_value", - state=plugin_service.Plugin.State.ENABLED, - ownership_type=plugin_service.Plugin.OwnershipType.SYSTEM_OWNED, - plugin_category=common_fields.PluginCategory.API_GATEWAY, - gateway_type=plugin_service.GatewayType.APIGEE_X_AND_HYBRID, - ) +@pytest.mark.asyncio +async def test_disable_plugin_instance_action_async( + transport: str = "grpc_asyncio", + request_type=plugin_service.DisablePluginInstanceActionRequest, +): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 + # 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() - # Convert return value to protobuf type - return_value = plugin_service.Plugin.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.create_plugin(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.disable_plugin_instance_action), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + response = await client.disable_plugin_instance_action(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = plugin_service.DisablePluginInstanceActionRequest() + assert args[0] == request # Establish that the response is the type that we expect. - assert isinstance(response, plugin_service.Plugin) - assert response.name == "name_value" - assert response.display_name == "display_name_value" - assert response.description == "description_value" - assert response.state == plugin_service.Plugin.State.ENABLED - assert response.ownership_type == plugin_service.Plugin.OwnershipType.SYSTEM_OWNED - assert response.plugin_category == common_fields.PluginCategory.API_GATEWAY - assert response.gateway_type == plugin_service.GatewayType.APIGEE_X_AND_HYBRID + assert isinstance(response, future.Future) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_create_plugin_rest_interceptors(null_interceptor): - transport = transports.ApiHubPluginRestTransport( +@pytest.mark.asyncio +async def test_disable_plugin_instance_action_async_from_dict(): + await test_disable_plugin_instance_action_async(request_type=dict) + + +def test_disable_plugin_instance_action_field_headers(): + client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubPluginRestInterceptor(), ) - client = ApiHubPluginClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_create_plugin" - ) as post, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_create_plugin_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "pre_create_plugin" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = plugin_service.CreatePluginRequest.pb( - plugin_service.CreatePluginRequest() - ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } + # 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 = plugin_service.DisablePluginInstanceActionRequest() - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = plugin_service.Plugin.to_json(plugin_service.Plugin()) - req.return_value.content = return_value + request.name = "name_value" - request = plugin_service.CreatePluginRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = plugin_service.Plugin() - post_with_metadata.return_value = plugin_service.Plugin(), metadata + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.disable_plugin_instance_action), "__call__" + ) as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.disable_plugin_instance_action(request) - client.create_plugin( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], - ) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] -def test_list_plugins_rest_bad_request(request_type=plugin_service.ListPluginsRequest): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" +@pytest.mark.asyncio +async def test_disable_plugin_instance_action_field_headers_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_plugins(request) + # 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 = plugin_service.DisablePluginInstanceActionRequest() -@pytest.mark.parametrize( - "request_type", - [ - plugin_service.ListPluginsRequest, - dict, - ], -) -def test_list_plugins_rest_call_success(request_type): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request = request_type(**request_init) + request.name = "name_value" - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = plugin_service.ListPluginsResponse( - next_page_token="next_page_token_value", + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.disable_plugin_instance_action), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/op") ) + await client.disable_plugin_instance_action(request) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = plugin_service.ListPluginsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.list_plugins(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 response is the type that we expect. - assert isinstance(response, pagers.ListPluginsPager) - assert response.next_page_token == "next_page_token_value" + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_list_plugins_rest_interceptors(null_interceptor): - transport = transports.ApiHubPluginRestTransport( +def test_disable_plugin_instance_action_flattened(): + client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubPluginRestInterceptor(), ) - client = ApiHubPluginClient(transport=transport) + # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_list_plugins" - ) as post, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_list_plugins_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "pre_list_plugins" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = plugin_service.ListPluginsRequest.pb( - plugin_service.ListPluginsRequest() + type(client.transport.disable_plugin_instance_action), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.disable_plugin_instance_action( + name="name_value", + action_id="action_id_value", ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = plugin_service.ListPluginsResponse.to_json( - plugin_service.ListPluginsResponse() + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + arg = args[0].action_id + mock_val = "action_id_value" + assert arg == mock_val + + +def test_disable_plugin_instance_action_flattened_error(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.disable_plugin_instance_action( + plugin_service.DisablePluginInstanceActionRequest(), + name="name_value", + action_id="action_id_value", ) - req.return_value.content = return_value - request = plugin_service.ListPluginsRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = plugin_service.ListPluginsResponse() - post_with_metadata.return_value = plugin_service.ListPluginsResponse(), metadata - client.list_plugins( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], +@pytest.mark.asyncio +async def test_disable_plugin_instance_action_flattened_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.disable_plugin_instance_action), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.disable_plugin_instance_action( + name="name_value", + action_id="action_id_value", ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + arg = args[0].action_id + mock_val = "action_id_value" + assert arg == mock_val -def test_delete_plugin_rest_bad_request( - request_type=plugin_service.DeletePluginRequest, -): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" +@pytest.mark.asyncio +async def test_disable_plugin_instance_action_flattened_error_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/plugins/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_plugin(request) + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.disable_plugin_instance_action( + plugin_service.DisablePluginInstanceActionRequest(), + name="name_value", + action_id="action_id_value", + ) @pytest.mark.parametrize( "request_type", [ - plugin_service.DeletePluginRequest, + plugin_service.UpdatePluginInstanceRequest, dict, ], ) -def test_delete_plugin_rest_call_success(request_type): +def test_update_plugin_instance(request_type, transport: str = "grpc"): client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/plugins/sample3"} - request = request_type(**request_init) + # 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 http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_plugin_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.PluginInstance( + name="name_value", + display_name="display_name_value", + state=plugin_service.PluginInstance.State.CREATING, + error_message="error_message_value", + source_project_id="source_project_id_value", + ) + response = client.update_plugin_instance(request) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.delete_plugin(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = plugin_service.UpdatePluginInstanceRequest() + assert args[0] == request # Establish that the response is the type that we expect. - json_return_value = json_format.MessageToJson(return_value) + assert isinstance(response, plugin_service.PluginInstance) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.state == plugin_service.PluginInstance.State.CREATING + assert response.error_message == "error_message_value" + assert response.source_project_id == "source_project_id_value" -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_delete_plugin_rest_interceptors(null_interceptor): - transport = transports.ApiHubPluginRestTransport( +def test_update_plugin_instance_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubPluginRestInterceptor(), + transport="grpc", ) - client = ApiHubPluginClient(transport=transport) + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = plugin_service.UpdatePluginInstanceRequest() + + # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - operation.Operation, "_set_result_from_operation" - ), mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_delete_plugin" - ) as post, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_delete_plugin_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "pre_delete_plugin" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = plugin_service.DeletePluginRequest.pb( - plugin_service.DeletePluginRequest() + type(client.transport.update_plugin_instance), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } + client.update_plugin_instance(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == plugin_service.UpdatePluginInstanceRequest() - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = json_format.MessageToJson(operations_pb2.Operation()) - req.return_value.content = return_value - request = plugin_service.DeletePluginRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = operations_pb2.Operation() - post_with_metadata.return_value = operations_pb2.Operation(), metadata +def test_update_plugin_instance_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) - client.delete_plugin( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.update_plugin_instance + in client._transport._wrapped_methods ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.update_plugin_instance + ] = mock_rpc + request = {} + client.update_plugin_instance(request) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 -def test_create_plugin_instance_rest_bad_request( - request_type=plugin_service.CreatePluginInstanceRequest, -): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2/plugins/sample3"} - request = request_type(**request_init) + client.update_plugin_instance(request) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_plugin_instance(request) + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 -@pytest.mark.parametrize( - "request_type", - [ - plugin_service.CreatePluginInstanceRequest, - dict, - ], -) -def test_create_plugin_instance_rest_call_success(request_type): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) +@pytest.mark.asyncio +async def test_update_plugin_instance_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2/plugins/sample3"} - request_init["plugin_instance"] = { - "name": "name_value", - "display_name": "display_name_value", - "auth_config": { - "google_service_account_config": { - "service_account": "service_account_value" - }, - "user_password_config": { - "username": "username_value", - "password": {"secret_version": "secret_version_value"}, - }, - "api_key_config": { - "name": "name_value", - "api_key": {}, - "http_element_location": 1, - }, - "oauth2_client_credentials_config": { - "client_id": "client_id_value", - "client_secret": {}, - }, - "auth_type": 1, - }, - "additional_config": {}, - "state": 1, - "error_message": "error_message_value", - "actions": [ - { - "hub_instance_action": { - "current_execution_state": 1, - "last_execution": { - "result": 1, - "error_message": "error_message_value", - "start_time": {"seconds": 751, "nanos": 543}, - "end_time": {}, - }, - }, - "action_id": "action_id_value", - "state": 1, - "schedule_cron_expression": "schedule_cron_expression_value", - "curation_config": { - "custom_curation": {"curation": "curation_value"}, - "curation_type": 1, - }, - "schedule_time_zone": "schedule_time_zone_value", - "service_account": "service_account_value", - "resource_config": { - "action_type": 1, - "pubsub_topic": "pubsub_topic_value", - }, - } - ], - "create_time": {}, - "update_time": {}, - "source_project_id": "source_project_id_value", - } - # The version of a generated dependency at test runtime may differ from the version used during generation. - # Delete any fields which are not present in the current runtime dependency - # See https://github.com/googleapis/gapic-generator-python/issues/1748 + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # Determine if the message type is proto-plus or protobuf - test_field = plugin_service.CreatePluginInstanceRequest.meta.fields[ - "plugin_instance" - ] + # Ensure method has been cached + assert ( + client._client._transport.update_plugin_instance + in client._client._transport._wrapped_methods + ) - def get_message_fields(field): - # Given a field which is a message (composite type), return a list with - # all the fields of the message. - # If the field is not a composite type, return an empty list. - message_fields = [] + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.update_plugin_instance + ] = mock_rpc - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + request = {} + await client.update_plugin_instance(request) - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] + await client.update_plugin_instance(request) - subfields_not_in_runtime = [] + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["plugin_instance"].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) +@pytest.mark.asyncio +async def test_update_plugin_instance_async( + transport: str = "grpc_asyncio", + request_type=plugin_service.UpdatePluginInstanceRequest, +): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range(0, len(request_init["plugin_instance"][field])): - del request_init["plugin_instance"][field][i][subfield] - else: - del request_init["plugin_instance"][field][subfield] - request = request_type(**request_init) + # 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 http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_plugin_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.PluginInstance( + name="name_value", + display_name="display_name_value", + state=plugin_service.PluginInstance.State.CREATING, + error_message="error_message_value", + source_project_id="source_project_id_value", + ) + ) + response = await client.update_plugin_instance(request) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.create_plugin_instance(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = plugin_service.UpdatePluginInstanceRequest() + assert args[0] == request # Establish that the response is the type that we expect. - json_return_value = json_format.MessageToJson(return_value) + assert isinstance(response, plugin_service.PluginInstance) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.state == plugin_service.PluginInstance.State.CREATING + assert response.error_message == "error_message_value" + assert response.source_project_id == "source_project_id_value" -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_create_plugin_instance_rest_interceptors(null_interceptor): - transport = transports.ApiHubPluginRestTransport( +@pytest.mark.asyncio +async def test_update_plugin_instance_async_from_dict(): + await test_update_plugin_instance_async(request_type=dict) + + +def test_update_plugin_instance_field_headers(): + client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubPluginRestInterceptor(), ) - client = ApiHubPluginClient(transport=transport) + # 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 = plugin_service.UpdatePluginInstanceRequest() + + request.plugin_instance.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - operation.Operation, "_set_result_from_operation" - ), mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_create_plugin_instance" - ) as post, mock.patch.object( - transports.ApiHubPluginRestInterceptor, - "post_create_plugin_instance_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "pre_create_plugin_instance" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = plugin_service.CreatePluginInstanceRequest.pb( - plugin_service.CreatePluginInstanceRequest() + type(client.transport.update_plugin_instance), "__call__" + ) as call: + call.return_value = plugin_service.PluginInstance() + client.update_plugin_instance(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", + "plugin_instance.name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_update_plugin_instance_field_headers_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = plugin_service.UpdatePluginInstanceRequest() + + request.plugin_instance.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_plugin_instance), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.PluginInstance() ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } + await client.update_plugin_instance(request) - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = json_format.MessageToJson(operations_pb2.Operation()) - req.return_value.content = return_value + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request - request = plugin_service.CreatePluginInstanceRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = operations_pb2.Operation() - post_with_metadata.return_value = operations_pb2.Operation(), metadata + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "plugin_instance.name=name_value", + ) in kw["metadata"] - client.create_plugin_instance( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + +def test_update_plugin_instance_flattened(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_plugin_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.PluginInstance() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.update_plugin_instance( + plugin_instance=plugin_service.PluginInstance(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].plugin_instance + mock_val = plugin_service.PluginInstance(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val -def test_execute_plugin_instance_action_rest_bad_request( - request_type=plugin_service.ExecutePluginInstanceActionRequest, -): +def test_update_plugin_instance_flattened_error(): client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" - } - request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.execute_plugin_instance_action(request) + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_plugin_instance( + plugin_service.UpdatePluginInstanceRequest(), + plugin_instance=plugin_service.PluginInstance(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +@pytest.mark.asyncio +async def test_update_plugin_instance_flattened_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_plugin_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = plugin_service.PluginInstance() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.PluginInstance() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.update_plugin_instance( + plugin_instance=plugin_service.PluginInstance(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].plugin_instance + mock_val = plugin_service.PluginInstance(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_update_plugin_instance_flattened_error_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.update_plugin_instance( + plugin_service.UpdatePluginInstanceRequest(), + plugin_instance=plugin_service.PluginInstance(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) @pytest.mark.parametrize( "request_type", [ - plugin_service.ExecutePluginInstanceActionRequest, + plugin_service.DeletePluginInstanceRequest, dict, ], ) -def test_execute_plugin_instance_action_rest_call_success(request_type): +def test_delete_plugin_instance(request_type, transport: str = "grpc"): client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" - } - request = request_type(**request_init) + # 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 http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_plugin_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/spam") + response = client.delete_plugin_instance(request) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.execute_plugin_instance_action(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = plugin_service.DeletePluginInstanceRequest() + assert args[0] == request # Establish that the response is the type that we expect. - json_return_value = json_format.MessageToJson(return_value) + assert isinstance(response, future.Future) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_execute_plugin_instance_action_rest_interceptors(null_interceptor): - transport = transports.ApiHubPluginRestTransport( +def test_delete_plugin_instance_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubPluginRestInterceptor(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = plugin_service.DeletePluginInstanceRequest( + name="name_value", ) - client = ApiHubPluginClient(transport=transport) + # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - operation.Operation, "_set_result_from_operation" - ), mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_execute_plugin_instance_action" - ) as post, mock.patch.object( - transports.ApiHubPluginRestInterceptor, - "post_execute_plugin_instance_action_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "pre_execute_plugin_instance_action" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = plugin_service.ExecutePluginInstanceActionRequest.pb( - plugin_service.ExecutePluginInstanceActionRequest() + type(client.transport.delete_plugin_instance), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.delete_plugin_instance(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == plugin_service.DeletePluginInstanceRequest( + name="name_value", ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = json_format.MessageToJson(operations_pb2.Operation()) - req.return_value.content = return_value - request = plugin_service.ExecutePluginInstanceActionRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = operations_pb2.Operation() - post_with_metadata.return_value = operations_pb2.Operation(), metadata +def test_delete_plugin_instance_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) - client.execute_plugin_instance_action( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.delete_plugin_instance + in client._transport._wrapped_methods ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.delete_plugin_instance + ] = mock_rpc + request = {} + client.delete_plugin_instance(request) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 -def test_get_plugin_instance_rest_bad_request( - request_type=plugin_service.GetPluginInstanceRequest, -): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" - } - request = request_type(**request_init) + # Operation methods call wrapper_fn to build a cached + # client._transport.operations_client instance on first rpc call. + # Subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_plugin_instance(request) + client.delete_plugin_instance(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 -@pytest.mark.parametrize( - "request_type", - [ - plugin_service.GetPluginInstanceRequest, - dict, - ], -) -def test_get_plugin_instance_rest_call_success(request_type): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) +@pytest.mark.asyncio +async def test_delete_plugin_instance_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" - } - request = request_type(**request_init) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = plugin_service.PluginInstance( - name="name_value", - display_name="display_name_value", - state=plugin_service.PluginInstance.State.CREATING, - error_message="error_message_value", - source_project_id="source_project_id_value", + # Ensure method has been cached + assert ( + client._client._transport.delete_plugin_instance + in client._client._transport._wrapped_methods ) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.delete_plugin_instance + ] = mock_rpc - # Convert return value to protobuf type - return_value = plugin_service.PluginInstance.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_plugin_instance(request) + request = {} + await client.delete_plugin_instance(request) - # Establish that the response is the type that we expect. - assert isinstance(response, plugin_service.PluginInstance) - assert response.name == "name_value" - assert response.display_name == "display_name_value" - assert response.state == plugin_service.PluginInstance.State.CREATING - assert response.error_message == "error_message_value" - assert response.source_project_id == "source_project_id_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + # Operation methods call wrapper_fn to build a cached + # client._transport.operations_client instance on first rpc call. + # Subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_plugin_instance_rest_interceptors(null_interceptor): - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubPluginRestInterceptor(), + await client.delete_plugin_instance(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_delete_plugin_instance_async( + transport: str = "grpc_asyncio", + request_type=plugin_service.DeletePluginInstanceRequest, +): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - client = ApiHubPluginClient(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._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_get_plugin_instance" - ) as post, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_get_plugin_instance_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "pre_get_plugin_instance" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = plugin_service.GetPluginInstanceRequest.pb( - plugin_service.GetPluginInstanceRequest() + type(client.transport.delete_plugin_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } + response = await client.delete_plugin_instance(request) - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = plugin_service.PluginInstance.to_json( - plugin_service.PluginInstance() - ) - req.return_value.content = return_value + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = plugin_service.DeletePluginInstanceRequest() + assert args[0] == request - request = plugin_service.GetPluginInstanceRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = plugin_service.PluginInstance() - post_with_metadata.return_value = plugin_service.PluginInstance(), metadata + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) - client.get_plugin_instance( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], - ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() +@pytest.mark.asyncio +async def test_delete_plugin_instance_async_from_dict(): + await test_delete_plugin_instance_async(request_type=dict) -def test_list_plugin_instances_rest_bad_request( - request_type=plugin_service.ListPluginInstancesRequest, -): +def test_delete_plugin_instance_field_headers(): client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2/plugins/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_plugin_instances(request) + # 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 = plugin_service.DeletePluginInstanceRequest() + request.name = "name_value" -@pytest.mark.parametrize( - "request_type", - [ - plugin_service.ListPluginInstancesRequest, - dict, - ], -) -def test_list_plugin_instances_rest_call_success(request_type): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_plugin_instance), "__call__" + ) as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.delete_plugin_instance(request) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2/plugins/sample3"} - request = request_type(**request_init) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = plugin_service.ListPluginInstancesResponse( - next_page_token="next_page_token_value", - ) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = plugin_service.ListPluginInstancesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.list_plugin_instances(request) +@pytest.mark.asyncio +async def test_delete_plugin_instance_field_headers_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) - # Establish that the response is the type that we expect. - assert isinstance(response, pagers.ListPluginInstancesPager) - assert response.next_page_token == "next_page_token_value" + # 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 = plugin_service.DeletePluginInstanceRequest() + request.name = "name_value" -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_list_plugin_instances_rest_interceptors(null_interceptor): - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubPluginRestInterceptor(), - ) - client = ApiHubPluginClient(transport=transport) - + # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_list_plugin_instances" - ) as post, mock.patch.object( - transports.ApiHubPluginRestInterceptor, - "post_list_plugin_instances_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "pre_list_plugin_instances" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = plugin_service.ListPluginInstancesRequest.pb( - plugin_service.ListPluginInstancesRequest() + type(client.transport.delete_plugin_instance), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/op") ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } + await client.delete_plugin_instance(request) - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = plugin_service.ListPluginInstancesResponse.to_json( - plugin_service.ListPluginInstancesResponse() - ) - req.return_value.content = return_value + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request - request = plugin_service.ListPluginInstancesRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = plugin_service.ListPluginInstancesResponse() - post_with_metadata.return_value = ( - plugin_service.ListPluginInstancesResponse(), - metadata, - ) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] - client.list_plugin_instances( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + +def test_delete_plugin_instance_flattened(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_plugin_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.delete_plugin_instance( + name="name_value", ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_enable_plugin_instance_action_rest_bad_request( - request_type=plugin_service.EnablePluginInstanceActionRequest, -): +def test_delete_plugin_instance_flattened_error(): client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" - } - request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.enable_plugin_instance_action(request) + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_plugin_instance( + plugin_service.DeletePluginInstanceRequest(), + name="name_value", + ) -@pytest.mark.parametrize( - "request_type", - [ - plugin_service.EnablePluginInstanceActionRequest, - dict, - ], -) -def test_enable_plugin_instance_action_rest_call_success(request_type): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" +@pytest.mark.asyncio +async def test_delete_plugin_instance_flattened_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" - } - request = request_type(**request_init) - - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_plugin_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.enable_plugin_instance_action(request) + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.delete_plugin_instance( + name="name_value", + ) - # Establish that the response is the type that we expect. - json_return_value = json_format.MessageToJson(return_value) + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_enable_plugin_instance_action_rest_interceptors(null_interceptor): - transport = transports.ApiHubPluginRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubPluginRestInterceptor(), +@pytest.mark.asyncio +async def test_delete_plugin_instance_flattened_error_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - client = ApiHubPluginClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - operation.Operation, "_set_result_from_operation" - ), mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_enable_plugin_instance_action" - ) as post, mock.patch.object( - transports.ApiHubPluginRestInterceptor, - "post_enable_plugin_instance_action_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "pre_enable_plugin_instance_action" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = plugin_service.EnablePluginInstanceActionRequest.pb( - plugin_service.EnablePluginInstanceActionRequest() + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.delete_plugin_instance( + plugin_service.DeletePluginInstanceRequest(), + name="name_value", ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = json_format.MessageToJson(operations_pb2.Operation()) - req.return_value.content = return_value - - request = plugin_service.EnablePluginInstanceActionRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = operations_pb2.Operation() - post_with_metadata.return_value = operations_pb2.Operation(), metadata - client.enable_plugin_instance_action( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], +def test_get_plugin_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + # Ensure method has been cached + assert client._transport.get_plugin in client._transport._wrapped_methods -def test_disable_plugin_instance_action_rest_bad_request( - request_type=plugin_service.DisablePluginInstanceActionRequest, -): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" - } - request = request_type(**request_init) + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.get_plugin] = mock_rpc - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.disable_plugin_instance_action(request) + request = {} + client.get_plugin(request) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 -@pytest.mark.parametrize( - "request_type", - [ - plugin_service.DisablePluginInstanceActionRequest, - dict, - ], -) -def test_disable_plugin_instance_action_rest_call_success(request_type): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + client.get_plugin(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_get_plugin_rest_required_fields(request_type=plugin_service.GetPluginRequest): + transport_class = transports.ApiHubPluginRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" - } + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_plugin._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_plugin._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) request = request_type(**request_init) + # Designate an appropriate value for the returned response. + return_value = plugin_service.Plugin() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = plugin_service.Plugin.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_plugin(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_plugin_rest_unset_required_fields(): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_plugin._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_get_plugin_rest_flattened(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + return_value = plugin_service.Plugin() + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/plugins/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) # Wrap the value into a proper Response obj - response_value = mock.Mock() + response_value = Response() response_value.status_code = 200 + # Convert return value to protobuf type + return_value = plugin_service.Plugin.pb(return_value) json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.disable_plugin_instance_action(request) - # Establish that the response is the type that we expect. - json_return_value = json_format.MessageToJson(return_value) + client.get_plugin(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/plugins/*}" % client.transport._host, + args[1], + ) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_disable_plugin_instance_action_rest_interceptors(null_interceptor): - transport = transports.ApiHubPluginRestTransport( +def test_get_plugin_rest_flattened_error(transport: str = "rest"): + client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubPluginRestInterceptor(), + transport=transport, ) - client = ApiHubPluginClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - operation.Operation, "_set_result_from_operation" - ), mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_disable_plugin_instance_action" - ) as post, mock.patch.object( - transports.ApiHubPluginRestInterceptor, - "post_disable_plugin_instance_action_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "pre_disable_plugin_instance_action" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = plugin_service.DisablePluginInstanceActionRequest.pb( - plugin_service.DisablePluginInstanceActionRequest() + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_plugin( + plugin_service.GetPluginRequest(), + name="name_value", ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = json_format.MessageToJson(operations_pb2.Operation()) - req.return_value.content = return_value - request = plugin_service.DisablePluginInstanceActionRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = operations_pb2.Operation() - post_with_metadata.return_value = operations_pb2.Operation(), metadata - client.disable_plugin_instance_action( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], +def test_enable_plugin_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + # Ensure method has been cached + assert client._transport.enable_plugin in client._transport._wrapped_methods -def test_update_plugin_instance_rest_bad_request( - request_type=plugin_service.UpdatePluginInstanceRequest, -): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = { - "plugin_instance": { - "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" - } - } - request = request_type(**request_init) + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.enable_plugin] = mock_rpc - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.update_plugin_instance(request) + request = {} + client.enable_plugin(request) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 -@pytest.mark.parametrize( - "request_type", - [ - plugin_service.UpdatePluginInstanceRequest, - dict, - ], -) -def test_update_plugin_instance_rest_call_success(request_type): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) + client.enable_plugin(request) - # send a request that will satisfy transcoding - request_init = { - "plugin_instance": { - "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" - } - } - request_init["plugin_instance"] = { - "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4", - "display_name": "display_name_value", - "auth_config": { - "google_service_account_config": { - "service_account": "service_account_value" - }, - "user_password_config": { - "username": "username_value", - "password": {"secret_version": "secret_version_value"}, - }, - "api_key_config": { - "name": "name_value", - "api_key": {}, - "http_element_location": 1, - }, - "oauth2_client_credentials_config": { - "client_id": "client_id_value", - "client_secret": {}, - }, - "auth_type": 1, - }, - "additional_config": {}, - "state": 1, - "error_message": "error_message_value", - "actions": [ - { - "hub_instance_action": { - "current_execution_state": 1, - "last_execution": { - "result": 1, - "error_message": "error_message_value", - "start_time": {"seconds": 751, "nanos": 543}, - "end_time": {}, - }, - }, - "action_id": "action_id_value", - "state": 1, - "schedule_cron_expression": "schedule_cron_expression_value", - "curation_config": { - "custom_curation": {"curation": "curation_value"}, - "curation_type": 1, - }, - "schedule_time_zone": "schedule_time_zone_value", - "service_account": "service_account_value", - "resource_config": { - "action_type": 1, - "pubsub_topic": "pubsub_topic_value", - }, - } - ], - "create_time": {}, - "update_time": {}, - "source_project_id": "source_project_id_value", - } - # The version of a generated dependency at test runtime may differ from the version used during generation. - # Delete any fields which are not present in the current runtime dependency - # See https://github.com/googleapis/gapic-generator-python/issues/1748 + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - # Determine if the message type is proto-plus or protobuf - test_field = plugin_service.UpdatePluginInstanceRequest.meta.fields[ - "plugin_instance" - ] - def get_message_fields(field): - # Given a field which is a message (composite type), return a list with - # all the fields of the message. - # If the field is not a composite type, return an empty list. - message_fields = [] +def test_enable_plugin_rest_required_fields( + request_type=plugin_service.EnablePluginRequest, +): + transport_class = transports.ApiHubPluginRestTransport - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields + # verify fields with default values are dropped - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).enable_plugin._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) - subfields_not_in_runtime = [] + # verify required fields with default values are now present - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["plugin_instance"].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value + jsonified_request["name"] = "name_value" - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).enable_plugin._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range(0, len(request_init["plugin_instance"][field])): - del request_init["plugin_instance"][field][i][subfield] - else: - del request_init["plugin_instance"][field][subfield] + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) request = request_type(**request_init) + # Designate an appropriate value for the returned response. + return_value = plugin_service.Plugin() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = plugin_service.Plugin.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.enable_plugin(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_enable_plugin_rest_unset_required_fields(): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.enable_plugin._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_enable_plugin_rest_flattened(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = plugin_service.PluginInstance( + return_value = plugin_service.Plugin() + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/plugins/sample3"} + + # get truthy value for each flattened field + mock_args = dict( name="name_value", - display_name="display_name_value", - state=plugin_service.PluginInstance.State.CREATING, - error_message="error_message_value", - source_project_id="source_project_id_value", ) + mock_args.update(sample_request) # Wrap the value into a proper Response obj - response_value = mock.Mock() + response_value = Response() response_value.status_code = 200 - # Convert return value to protobuf type - return_value = plugin_service.PluginInstance.pb(return_value) + return_value = plugin_service.Plugin.pb(return_value) json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.update_plugin_instance(request) - # Establish that the response is the type that we expect. - assert isinstance(response, plugin_service.PluginInstance) - assert response.name == "name_value" - assert response.display_name == "display_name_value" - assert response.state == plugin_service.PluginInstance.State.CREATING - assert response.error_message == "error_message_value" - assert response.source_project_id == "source_project_id_value" + client.enable_plugin(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/plugins/*}:enable" + % client.transport._host, + args[1], + ) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_update_plugin_instance_rest_interceptors(null_interceptor): - transport = transports.ApiHubPluginRestTransport( +def test_enable_plugin_rest_flattened_error(transport: str = "rest"): + client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubPluginRestInterceptor(), + transport=transport, ) - client = ApiHubPluginClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_update_plugin_instance" - ) as post, mock.patch.object( - transports.ApiHubPluginRestInterceptor, - "post_update_plugin_instance_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "pre_update_plugin_instance" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = plugin_service.UpdatePluginInstanceRequest.pb( - plugin_service.UpdatePluginInstanceRequest() + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.enable_plugin( + plugin_service.EnablePluginRequest(), + name="name_value", ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 + +def test_disable_plugin_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.disable_plugin in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.disable_plugin] = mock_rpc + + request = {} + client.disable_plugin(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.disable_plugin(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_disable_plugin_rest_required_fields( + request_type=plugin_service.DisablePluginRequest, +): + transport_class = transports.ApiHubPluginRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).disable_plugin._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).disable_plugin._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = plugin_service.Plugin() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = plugin_service.Plugin.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.disable_plugin(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_disable_plugin_rest_unset_required_fields(): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.disable_plugin._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_disable_plugin_rest_flattened(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = plugin_service.Plugin() + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/plugins/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = plugin_service.Plugin.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = plugin_service.PluginInstance.to_json( - plugin_service.PluginInstance() + + client.disable_plugin(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/plugins/*}:disable" + % client.transport._host, + args[1], ) - req.return_value.content = return_value - request = plugin_service.UpdatePluginInstanceRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = plugin_service.PluginInstance() - post_with_metadata.return_value = plugin_service.PluginInstance(), metadata - client.update_plugin_instance( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], +def test_disable_plugin_rest_flattened_error(transport: str = "rest"): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.disable_plugin( + plugin_service.DisablePluginRequest(), + name="name_value", + ) + + +def test_create_plugin_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.create_plugin in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.create_plugin] = mock_rpc + + request = {} + client.create_plugin(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.create_plugin(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_create_plugin_rest_required_fields( + request_type=plugin_service.CreatePluginRequest, +): + transport_class = transports.ApiHubPluginRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_plugin._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_plugin._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("plugin_id",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = plugin_service.Plugin() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = plugin_service.Plugin.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.create_plugin(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_plugin_rest_unset_required_fields(): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_plugin._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("pluginId",)) + & set( + ( + "parent", + "plugin", + ) + ) + ) + + +def test_create_plugin_rest_flattened(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = plugin_service.Plugin() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + plugin=plugin_service.Plugin(name="name_value"), + plugin_id="plugin_id_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = plugin_service.Plugin.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.create_plugin(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/plugins" % client.transport._host, + args[1], + ) + + +def test_create_plugin_rest_flattened_error(transport: str = "rest"): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_plugin( + plugin_service.CreatePluginRequest(), + parent="parent_value", + plugin=plugin_service.Plugin(name="name_value"), + plugin_id="plugin_id_value", + ) + + +def test_list_plugins_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.list_plugins in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.list_plugins] = mock_rpc + + request = {} + client.list_plugins(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.list_plugins(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_list_plugins_rest_required_fields( + request_type=plugin_service.ListPluginsRequest, +): + transport_class = transports.ApiHubPluginRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_plugins._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_plugins._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = plugin_service.ListPluginsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = plugin_service.ListPluginsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_plugins(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_plugins_rest_unset_required_fields(): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_plugins._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +def test_list_plugins_rest_flattened(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = plugin_service.ListPluginsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = plugin_service.ListPluginsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.list_plugins(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/plugins" % client.transport._host, + args[1], + ) + + +def test_list_plugins_rest_flattened_error(transport: str = "rest"): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_plugins( + plugin_service.ListPluginsRequest(), + parent="parent_value", + ) + + +def test_list_plugins_rest_pager(transport: str = "rest"): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + plugin_service.ListPluginsResponse( + plugins=[ + plugin_service.Plugin(), + plugin_service.Plugin(), + plugin_service.Plugin(), + ], + next_page_token="abc", + ), + plugin_service.ListPluginsResponse( + plugins=[], + next_page_token="def", + ), + plugin_service.ListPluginsResponse( + plugins=[ + plugin_service.Plugin(), + ], + next_page_token="ghi", + ), + plugin_service.ListPluginsResponse( + plugins=[ + plugin_service.Plugin(), + plugin_service.Plugin(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + plugin_service.ListPluginsResponse.to_json(x) for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2"} + + pager = client.list_plugins(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, plugin_service.Plugin) for i in results) + + pages = list(client.list_plugins(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +def test_delete_plugin_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.delete_plugin in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.delete_plugin] = mock_rpc + + request = {} + client.delete_plugin(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + # Operation methods build a cached wrapper on first rpc call + # subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() + + client.delete_plugin(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_delete_plugin_rest_required_fields( + request_type=plugin_service.DeletePluginRequest, +): + transport_class = transports.ApiHubPluginRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_plugin._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_plugin._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_plugin(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_plugin_rest_unset_required_fields(): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_plugin._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_delete_plugin_rest_flattened(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/plugins/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.delete_plugin(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/plugins/*}" % client.transport._host, + args[1], + ) + + +def test_delete_plugin_rest_flattened_error(transport: str = "rest"): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_plugin( + plugin_service.DeletePluginRequest(), + name="name_value", + ) + + +def test_create_plugin_instance_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.create_plugin_instance + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.create_plugin_instance + ] = mock_rpc + + request = {} + client.create_plugin_instance(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + # Operation methods build a cached wrapper on first rpc call + # subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() + + client.create_plugin_instance(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_create_plugin_instance_rest_required_fields( + request_type=plugin_service.CreatePluginInstanceRequest, +): + transport_class = transports.ApiHubPluginRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_plugin_instance._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_plugin_instance._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("plugin_instance_id",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.create_plugin_instance(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_plugin_instance_rest_unset_required_fields(): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_plugin_instance._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("pluginInstanceId",)) + & set( + ( + "parent", + "pluginInstance", + ) + ) + ) + + +def test_create_plugin_instance_rest_flattened(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/plugins/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + plugin_instance=plugin_service.PluginInstance(name="name_value"), + plugin_instance_id="plugin_instance_id_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.create_plugin_instance(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/plugins/*}/instances" + % client.transport._host, + args[1], + ) + + +def test_create_plugin_instance_rest_flattened_error(transport: str = "rest"): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_plugin_instance( + plugin_service.CreatePluginInstanceRequest(), + parent="parent_value", + plugin_instance=plugin_service.PluginInstance(name="name_value"), + plugin_instance_id="plugin_instance_id_value", + ) + + +def test_execute_plugin_instance_action_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.execute_plugin_instance_action + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.execute_plugin_instance_action + ] = mock_rpc + + request = {} + client.execute_plugin_instance_action(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + # Operation methods build a cached wrapper on first rpc call + # subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() + + client.execute_plugin_instance_action(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_execute_plugin_instance_action_rest_required_fields( + request_type=plugin_service.ExecutePluginInstanceActionRequest, +): + transport_class = transports.ApiHubPluginRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).execute_plugin_instance_action._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).execute_plugin_instance_action._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.execute_plugin_instance_action(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_execute_plugin_instance_action_rest_unset_required_fields(): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.execute_plugin_instance_action._get_unset_required_fields( + {} + ) + assert set(unset_fields) == ( + set(()) + & set( + ( + "name", + "actionExecutionDetail", + ) + ) + ) + + +def test_execute_plugin_instance_action_rest_flattened(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + action_execution_detail=plugin_service.ActionExecutionDetail( + action_id="action_id_value" + ), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.execute_plugin_instance_action(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/plugins/*/instances/*}:executeAction" + % client.transport._host, + args[1], + ) + + +def test_execute_plugin_instance_action_rest_flattened_error(transport: str = "rest"): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.execute_plugin_instance_action( + plugin_service.ExecutePluginInstanceActionRequest(), + name="name_value", + action_execution_detail=plugin_service.ActionExecutionDetail( + action_id="action_id_value" + ), + ) + + +def test_get_plugin_instance_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.get_plugin_instance in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.get_plugin_instance + ] = mock_rpc + + request = {} + client.get_plugin_instance(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_plugin_instance(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_get_plugin_instance_rest_required_fields( + request_type=plugin_service.GetPluginInstanceRequest, +): + transport_class = transports.ApiHubPluginRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_plugin_instance._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_plugin_instance._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = plugin_service.PluginInstance() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = plugin_service.PluginInstance.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_plugin_instance(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_plugin_instance_rest_unset_required_fields(): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_plugin_instance._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_get_plugin_instance_rest_flattened(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = plugin_service.PluginInstance() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = plugin_service.PluginInstance.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.get_plugin_instance(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/plugins/*/instances/*}" + % client.transport._host, + args[1], + ) + + +def test_get_plugin_instance_rest_flattened_error(transport: str = "rest"): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_plugin_instance( + plugin_service.GetPluginInstanceRequest(), + name="name_value", + ) + + +def test_list_plugin_instances_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.list_plugin_instances + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.list_plugin_instances + ] = mock_rpc + + request = {} + client.list_plugin_instances(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.list_plugin_instances(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_list_plugin_instances_rest_required_fields( + request_type=plugin_service.ListPluginInstancesRequest, +): + transport_class = transports.ApiHubPluginRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_plugin_instances._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_plugin_instances._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = plugin_service.ListPluginInstancesResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = plugin_service.ListPluginInstancesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_plugin_instances(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_plugin_instances_rest_unset_required_fields(): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_plugin_instances._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +def test_list_plugin_instances_rest_flattened(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = plugin_service.ListPluginInstancesResponse() + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/plugins/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = plugin_service.ListPluginInstancesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.list_plugin_instances(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/plugins/*}/instances" + % client.transport._host, + args[1], + ) + + +def test_list_plugin_instances_rest_flattened_error(transport: str = "rest"): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_plugin_instances( + plugin_service.ListPluginInstancesRequest(), + parent="parent_value", + ) + + +def test_list_plugin_instances_rest_pager(transport: str = "rest"): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + plugin_service.ListPluginInstancesResponse( + plugin_instances=[ + plugin_service.PluginInstance(), + plugin_service.PluginInstance(), + plugin_service.PluginInstance(), + ], + next_page_token="abc", + ), + plugin_service.ListPluginInstancesResponse( + plugin_instances=[], + next_page_token="def", + ), + plugin_service.ListPluginInstancesResponse( + plugin_instances=[ + plugin_service.PluginInstance(), + ], + next_page_token="ghi", + ), + plugin_service.ListPluginInstancesResponse( + plugin_instances=[ + plugin_service.PluginInstance(), + plugin_service.PluginInstance(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + plugin_service.ListPluginInstancesResponse.to_json(x) for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = { + "parent": "projects/sample1/locations/sample2/plugins/sample3" + } + + pager = client.list_plugin_instances(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, plugin_service.PluginInstance) for i in results) + + pages = list(client.list_plugin_instances(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +def test_enable_plugin_instance_action_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.enable_plugin_instance_action + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.enable_plugin_instance_action + ] = mock_rpc + + request = {} + client.enable_plugin_instance_action(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + # Operation methods build a cached wrapper on first rpc call + # subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() + + client.enable_plugin_instance_action(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_enable_plugin_instance_action_rest_required_fields( + request_type=plugin_service.EnablePluginInstanceActionRequest, +): + transport_class = transports.ApiHubPluginRestTransport + + request_init = {} + request_init["name"] = "" + request_init["action_id"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).enable_plugin_instance_action._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + jsonified_request["actionId"] = "action_id_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).enable_plugin_instance_action._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + assert "actionId" in jsonified_request + assert jsonified_request["actionId"] == "action_id_value" + + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.enable_plugin_instance_action(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_enable_plugin_instance_action_rest_unset_required_fields(): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.enable_plugin_instance_action._get_unset_required_fields( + {} + ) + assert set(unset_fields) == ( + set(()) + & set( + ( + "name", + "actionId", + ) + ) + ) + + +def test_enable_plugin_instance_action_rest_flattened(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + action_id="action_id_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.enable_plugin_instance_action(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/plugins/*/instances/*}:enableAction" + % client.transport._host, + args[1], + ) + + +def test_enable_plugin_instance_action_rest_flattened_error(transport: str = "rest"): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.enable_plugin_instance_action( + plugin_service.EnablePluginInstanceActionRequest(), + name="name_value", + action_id="action_id_value", + ) + + +def test_disable_plugin_instance_action_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.disable_plugin_instance_action + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.disable_plugin_instance_action + ] = mock_rpc + + request = {} + client.disable_plugin_instance_action(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + # Operation methods build a cached wrapper on first rpc call + # subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() + + client.disable_plugin_instance_action(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_disable_plugin_instance_action_rest_required_fields( + request_type=plugin_service.DisablePluginInstanceActionRequest, +): + transport_class = transports.ApiHubPluginRestTransport + + request_init = {} + request_init["name"] = "" + request_init["action_id"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).disable_plugin_instance_action._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + jsonified_request["actionId"] = "action_id_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).disable_plugin_instance_action._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + assert "actionId" in jsonified_request + assert jsonified_request["actionId"] == "action_id_value" + + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.disable_plugin_instance_action(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_disable_plugin_instance_action_rest_unset_required_fields(): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.disable_plugin_instance_action._get_unset_required_fields( + {} + ) + assert set(unset_fields) == ( + set(()) + & set( + ( + "name", + "actionId", + ) + ) + ) + + +def test_disable_plugin_instance_action_rest_flattened(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + action_id="action_id_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.disable_plugin_instance_action(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/plugins/*/instances/*}:disableAction" + % client.transport._host, + args[1], + ) + + +def test_disable_plugin_instance_action_rest_flattened_error(transport: str = "rest"): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.disable_plugin_instance_action( + plugin_service.DisablePluginInstanceActionRequest(), + name="name_value", + action_id="action_id_value", + ) + + +def test_update_plugin_instance_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.update_plugin_instance + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.update_plugin_instance + ] = mock_rpc + + request = {} + client.update_plugin_instance(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.update_plugin_instance(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_update_plugin_instance_rest_required_fields( + request_type=plugin_service.UpdatePluginInstanceRequest, +): + transport_class = transports.ApiHubPluginRestTransport + + request_init = {} + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_plugin_instance._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_plugin_instance._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("update_mask",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = plugin_service.PluginInstance() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = plugin_service.PluginInstance.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.update_plugin_instance(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_plugin_instance_rest_unset_required_fields(): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.update_plugin_instance._get_unset_required_fields({}) + assert set(unset_fields) == (set(("updateMask",)) & set(("pluginInstance",))) + + +def test_update_plugin_instance_rest_flattened(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = plugin_service.PluginInstance() + + # get arguments that satisfy an http rule for this method + sample_request = { + "plugin_instance": { + "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" + } + } + + # get truthy value for each flattened field + mock_args = dict( + plugin_instance=plugin_service.PluginInstance(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = plugin_service.PluginInstance.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.update_plugin_instance(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{plugin_instance.name=projects/*/locations/*/plugins/*/instances/*}" + % client.transport._host, + args[1], + ) + + +def test_update_plugin_instance_rest_flattened_error(transport: str = "rest"): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_plugin_instance( + plugin_service.UpdatePluginInstanceRequest(), + plugin_instance=plugin_service.PluginInstance(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +def test_delete_plugin_instance_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.delete_plugin_instance + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.delete_plugin_instance + ] = mock_rpc + + request = {} + client.delete_plugin_instance(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + # Operation methods build a cached wrapper on first rpc call + # subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() + + client.delete_plugin_instance(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_delete_plugin_instance_rest_required_fields( + request_type=plugin_service.DeletePluginInstanceRequest, +): + transport_class = transports.ApiHubPluginRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_plugin_instance._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_plugin_instance._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_plugin_instance(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_plugin_instance_rest_unset_required_fields(): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_plugin_instance._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_delete_plugin_instance_rest_flattened(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.delete_plugin_instance(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/plugins/*/instances/*}" + % client.transport._host, + args[1], + ) + + +def test_delete_plugin_instance_rest_flattened_error(transport: str = "rest"): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_plugin_instance( + plugin_service.DeletePluginInstanceRequest(), + name="name_value", + ) + + +def test_credentials_transport_error(): + # It is an error to provide credentials and a transport instance. + transport = transports.ApiHubPluginGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # It is an error to provide a credentials file and a transport instance. + transport = transports.ApiHubPluginGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = ApiHubPluginClient( + client_options={"credentials_file": "credentials.json"}, + transport=transport, + ) + + # It is an error to provide an api_key and a transport instance. + transport = transports.ApiHubPluginGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = ApiHubPluginClient( + client_options=options, + transport=transport, + ) + + # It is an error to provide an api_key and a credential. + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = ApiHubPluginClient( + client_options=options, credentials=ga_credentials.AnonymousCredentials() + ) + + # It is an error to provide scopes and a transport instance. + transport = transports.ApiHubPluginGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = ApiHubPluginClient( + client_options={"scopes": ["1", "2"]}, + transport=transport, + ) + + +def test_transport_instance(): + # A client may be instantiated with a custom transport instance. + transport = transports.ApiHubPluginGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + client = ApiHubPluginClient(transport=transport) + assert client.transport is transport + + +def test_transport_get_channel(): + # A client may be instantiated with a custom transport instance. + transport = transports.ApiHubPluginGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + transport = transports.ApiHubPluginGrpcAsyncIOTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubPluginGrpcTransport, + transports.ApiHubPluginGrpcAsyncIOTransport, + transports.ApiHubPluginRestTransport, + ], +) +def test_transport_adc(transport_class): + # Test default credentials are used if not provided. + with mock.patch.object(google.auth, "default") as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class() + adc.assert_called_once() + + +def test_transport_kind_grpc(): + transport = ApiHubPluginClient.get_transport_class("grpc")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "grpc" + + +def test_initialize_client_w_grpc(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_plugin_empty_call_grpc(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_plugin), "__call__") as call: + call.return_value = plugin_service.Plugin() + client.get_plugin(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.GetPluginRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_enable_plugin_empty_call_grpc(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.enable_plugin), "__call__") as call: + call.return_value = plugin_service.Plugin() + client.enable_plugin(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.EnablePluginRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_disable_plugin_empty_call_grpc(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.disable_plugin), "__call__") as call: + call.return_value = plugin_service.Plugin() + client.disable_plugin(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.DisablePluginRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_plugin_empty_call_grpc(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.create_plugin), "__call__") as call: + call.return_value = plugin_service.Plugin() + client.create_plugin(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.CreatePluginRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_plugins_empty_call_grpc(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.list_plugins), "__call__") as call: + call.return_value = plugin_service.ListPluginsResponse() + client.list_plugins(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.ListPluginsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_plugin_empty_call_grpc(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.delete_plugin), "__call__") as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.delete_plugin(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.DeletePluginRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_plugin_instance_empty_call_grpc(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_plugin_instance), "__call__" + ) as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.create_plugin_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.CreatePluginInstanceRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_execute_plugin_instance_action_empty_call_grpc(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.execute_plugin_instance_action), "__call__" + ) as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.execute_plugin_instance_action(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.ExecutePluginInstanceActionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_plugin_instance_empty_call_grpc(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_plugin_instance), "__call__" + ) as call: + call.return_value = plugin_service.PluginInstance() + client.get_plugin_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.GetPluginInstanceRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_plugin_instances_empty_call_grpc(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_plugin_instances), "__call__" + ) as call: + call.return_value = plugin_service.ListPluginInstancesResponse() + client.list_plugin_instances(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.ListPluginInstancesRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_enable_plugin_instance_action_empty_call_grpc(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.enable_plugin_instance_action), "__call__" + ) as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.enable_plugin_instance_action(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.EnablePluginInstanceActionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_disable_plugin_instance_action_empty_call_grpc(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.disable_plugin_instance_action), "__call__" + ) as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.disable_plugin_instance_action(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.DisablePluginInstanceActionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_update_plugin_instance_empty_call_grpc(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.update_plugin_instance), "__call__" + ) as call: + call.return_value = plugin_service.PluginInstance() + client.update_plugin_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.UpdatePluginInstanceRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_plugin_instance_empty_call_grpc(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.delete_plugin_instance), "__call__" + ) as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.delete_plugin_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.DeletePluginInstanceRequest() + + assert args[0] == request_msg + + +def test_transport_kind_grpc_asyncio(): + transport = ApiHubPluginAsyncClient.get_transport_class("grpc_asyncio")( + credentials=async_anonymous_credentials() + ) + assert transport.kind == "grpc_asyncio" + + +def test_initialize_client_w_grpc_asyncio(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_plugin_empty_call_grpc_asyncio(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.Plugin( + name="name_value", + display_name="display_name_value", + description="description_value", + state=plugin_service.Plugin.State.ENABLED, + ownership_type=plugin_service.Plugin.OwnershipType.SYSTEM_OWNED, + plugin_category=common_fields.PluginCategory.API_GATEWAY, + gateway_type=plugin_service.GatewayType.APIGEE_X_AND_HYBRID, + ) + ) + await client.get_plugin(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.GetPluginRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_enable_plugin_empty_call_grpc_asyncio(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.enable_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.Plugin( + name="name_value", + display_name="display_name_value", + description="description_value", + state=plugin_service.Plugin.State.ENABLED, + ownership_type=plugin_service.Plugin.OwnershipType.SYSTEM_OWNED, + plugin_category=common_fields.PluginCategory.API_GATEWAY, + gateway_type=plugin_service.GatewayType.APIGEE_X_AND_HYBRID, + ) + ) + await client.enable_plugin(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.EnablePluginRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_disable_plugin_empty_call_grpc_asyncio(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.disable_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.Plugin( + name="name_value", + display_name="display_name_value", + description="description_value", + state=plugin_service.Plugin.State.ENABLED, + ownership_type=plugin_service.Plugin.OwnershipType.SYSTEM_OWNED, + plugin_category=common_fields.PluginCategory.API_GATEWAY, + gateway_type=plugin_service.GatewayType.APIGEE_X_AND_HYBRID, + ) + ) + await client.disable_plugin(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.DisablePluginRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_create_plugin_empty_call_grpc_asyncio(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.create_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.Plugin( + name="name_value", + display_name="display_name_value", + description="description_value", + state=plugin_service.Plugin.State.ENABLED, + ownership_type=plugin_service.Plugin.OwnershipType.SYSTEM_OWNED, + plugin_category=common_fields.PluginCategory.API_GATEWAY, + gateway_type=plugin_service.GatewayType.APIGEE_X_AND_HYBRID, + ) + ) + await client.create_plugin(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.CreatePluginRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_list_plugins_empty_call_grpc_asyncio(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.list_plugins), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.ListPluginsResponse( + next_page_token="next_page_token_value", + ) + ) + await client.list_plugins(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.ListPluginsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_delete_plugin_empty_call_grpc_asyncio(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.delete_plugin), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + await client.delete_plugin(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.DeletePluginRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_create_plugin_instance_empty_call_grpc_asyncio(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_plugin_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + await client.create_plugin_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.CreatePluginInstanceRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_execute_plugin_instance_action_empty_call_grpc_asyncio(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.execute_plugin_instance_action), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + await client.execute_plugin_instance_action(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.ExecutePluginInstanceActionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_plugin_instance_empty_call_grpc_asyncio(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_plugin_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.PluginInstance( + name="name_value", + display_name="display_name_value", + state=plugin_service.PluginInstance.State.CREATING, + error_message="error_message_value", + source_project_id="source_project_id_value", + ) + ) + await client.get_plugin_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.GetPluginInstanceRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_list_plugin_instances_empty_call_grpc_asyncio(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_plugin_instances), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.ListPluginInstancesResponse( + next_page_token="next_page_token_value", + ) + ) + await client.list_plugin_instances(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.ListPluginInstancesRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_enable_plugin_instance_action_empty_call_grpc_asyncio(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.enable_plugin_instance_action), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + await client.enable_plugin_instance_action(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.EnablePluginInstanceActionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_disable_plugin_instance_action_empty_call_grpc_asyncio(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.disable_plugin_instance_action), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + await client.disable_plugin_instance_action(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.DisablePluginInstanceActionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_update_plugin_instance_empty_call_grpc_asyncio(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.update_plugin_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + plugin_service.PluginInstance( + name="name_value", + display_name="display_name_value", + state=plugin_service.PluginInstance.State.CREATING, + error_message="error_message_value", + source_project_id="source_project_id_value", + ) + ) + await client.update_plugin_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.UpdatePluginInstanceRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_delete_plugin_instance_empty_call_grpc_asyncio(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.delete_plugin_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + await client.delete_plugin_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.DeletePluginInstanceRequest() + + assert args[0] == request_msg + + +def test_transport_kind_rest(): + transport = ApiHubPluginClient.get_transport_class("rest")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "rest" + + +def test_get_plugin_rest_bad_request(request_type=plugin_service.GetPluginRequest): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/plugins/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_plugin(request) + + +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.GetPluginRequest, + dict, + ], +) +def test_get_plugin_rest_call_success(request_type): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/plugins/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = plugin_service.Plugin( + name="name_value", + display_name="display_name_value", + description="description_value", + state=plugin_service.Plugin.State.ENABLED, + ownership_type=plugin_service.Plugin.OwnershipType.SYSTEM_OWNED, + plugin_category=common_fields.PluginCategory.API_GATEWAY, + gateway_type=plugin_service.GatewayType.APIGEE_X_AND_HYBRID, + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = plugin_service.Plugin.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.get_plugin(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, plugin_service.Plugin) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.state == plugin_service.Plugin.State.ENABLED + assert response.ownership_type == plugin_service.Plugin.OwnershipType.SYSTEM_OWNED + assert response.plugin_category == common_fields.PluginCategory.API_GATEWAY + assert response.gateway_type == plugin_service.GatewayType.APIGEE_X_AND_HYBRID + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_plugin_rest_interceptors(null_interceptor): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubPluginRestInterceptor(), + ) + client = ApiHubPluginClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_get_plugin" + ) as post, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_get_plugin_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "pre_get_plugin" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = plugin_service.GetPluginRequest.pb( + plugin_service.GetPluginRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = plugin_service.Plugin.to_json(plugin_service.Plugin()) + req.return_value.content = return_value + + request = plugin_service.GetPluginRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = plugin_service.Plugin() + post_with_metadata.return_value = plugin_service.Plugin(), metadata + + client.get_plugin( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_enable_plugin_rest_bad_request( + request_type=plugin_service.EnablePluginRequest, +): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/plugins/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.enable_plugin(request) + + +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.EnablePluginRequest, + dict, + ], +) +def test_enable_plugin_rest_call_success(request_type): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/plugins/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = plugin_service.Plugin( + name="name_value", + display_name="display_name_value", + description="description_value", + state=plugin_service.Plugin.State.ENABLED, + ownership_type=plugin_service.Plugin.OwnershipType.SYSTEM_OWNED, + plugin_category=common_fields.PluginCategory.API_GATEWAY, + gateway_type=plugin_service.GatewayType.APIGEE_X_AND_HYBRID, + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = plugin_service.Plugin.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.enable_plugin(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, plugin_service.Plugin) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.state == plugin_service.Plugin.State.ENABLED + assert response.ownership_type == plugin_service.Plugin.OwnershipType.SYSTEM_OWNED + assert response.plugin_category == common_fields.PluginCategory.API_GATEWAY + assert response.gateway_type == plugin_service.GatewayType.APIGEE_X_AND_HYBRID + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_enable_plugin_rest_interceptors(null_interceptor): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubPluginRestInterceptor(), + ) + client = ApiHubPluginClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_enable_plugin" + ) as post, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_enable_plugin_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "pre_enable_plugin" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = plugin_service.EnablePluginRequest.pb( + plugin_service.EnablePluginRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = plugin_service.Plugin.to_json(plugin_service.Plugin()) + req.return_value.content = return_value + + request = plugin_service.EnablePluginRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = plugin_service.Plugin() + post_with_metadata.return_value = plugin_service.Plugin(), metadata + + client.enable_plugin( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_disable_plugin_rest_bad_request( + request_type=plugin_service.DisablePluginRequest, +): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/plugins/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.disable_plugin(request) + + +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.DisablePluginRequest, + dict, + ], +) +def test_disable_plugin_rest_call_success(request_type): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/plugins/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = plugin_service.Plugin( + name="name_value", + display_name="display_name_value", + description="description_value", + state=plugin_service.Plugin.State.ENABLED, + ownership_type=plugin_service.Plugin.OwnershipType.SYSTEM_OWNED, + plugin_category=common_fields.PluginCategory.API_GATEWAY, + gateway_type=plugin_service.GatewayType.APIGEE_X_AND_HYBRID, + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = plugin_service.Plugin.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.disable_plugin(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, plugin_service.Plugin) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.state == plugin_service.Plugin.State.ENABLED + assert response.ownership_type == plugin_service.Plugin.OwnershipType.SYSTEM_OWNED + assert response.plugin_category == common_fields.PluginCategory.API_GATEWAY + assert response.gateway_type == plugin_service.GatewayType.APIGEE_X_AND_HYBRID + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_disable_plugin_rest_interceptors(null_interceptor): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubPluginRestInterceptor(), + ) + client = ApiHubPluginClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_disable_plugin" + ) as post, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_disable_plugin_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "pre_disable_plugin" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = plugin_service.DisablePluginRequest.pb( + plugin_service.DisablePluginRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = plugin_service.Plugin.to_json(plugin_service.Plugin()) + req.return_value.content = return_value + + request = plugin_service.DisablePluginRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = plugin_service.Plugin() + post_with_metadata.return_value = plugin_service.Plugin(), metadata + + client.disable_plugin( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_create_plugin_rest_bad_request( + request_type=plugin_service.CreatePluginRequest, +): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.create_plugin(request) + + +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.CreatePluginRequest, + dict, + ], +) +def test_create_plugin_rest_call_success(request_type): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["plugin"] = { + "name": "name_value", + "display_name": "display_name_value", + "type_": { + "enum_values": { + "values": [ + { + "id": "id_value", + "display_name": "display_name_value", + "description": "description_value", + "immutable": True, + } + ] + }, + "string_values": {"values": ["values_value1", "values_value2"]}, + "json_values": {}, + "uri_values": {}, + "attribute": "attribute_value", + }, + "description": "description_value", + "state": 1, + "ownership_type": 1, + "hosting_service": {"service_uri": "service_uri_value"}, + "actions_config": [ + { + "id": "id_value", + "display_name": "display_name_value", + "description": "description_value", + "trigger_mode": 1, + } + ], + "documentation": {"external_uri": "external_uri_value"}, + "plugin_category": 1, + "config_template": { + "auth_config_template": { + "supported_auth_types": [1], + "service_account": {"service_account": "service_account_value"}, + }, + "additional_config_template": [ + { + "id": "id_value", + "value_type": 1, + "description": "description_value", + "validation_regex": "validation_regex_value", + "required": True, + "enum_options": [ + { + "id": "id_value", + "display_name": "display_name_value", + "description": "description_value", + } + ], + "multi_select_options": {}, + } + ], + }, + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "gateway_type": 1, + } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = plugin_service.CreatePluginRequest.meta.fields["plugin"] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["plugin"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["plugin"][field])): + del request_init["plugin"][field][i][subfield] + else: + del request_init["plugin"][field][subfield] + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = plugin_service.Plugin( + name="name_value", + display_name="display_name_value", + description="description_value", + state=plugin_service.Plugin.State.ENABLED, + ownership_type=plugin_service.Plugin.OwnershipType.SYSTEM_OWNED, + plugin_category=common_fields.PluginCategory.API_GATEWAY, + gateway_type=plugin_service.GatewayType.APIGEE_X_AND_HYBRID, + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = plugin_service.Plugin.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.create_plugin(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, plugin_service.Plugin) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.state == plugin_service.Plugin.State.ENABLED + assert response.ownership_type == plugin_service.Plugin.OwnershipType.SYSTEM_OWNED + assert response.plugin_category == common_fields.PluginCategory.API_GATEWAY + assert response.gateway_type == plugin_service.GatewayType.APIGEE_X_AND_HYBRID + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_plugin_rest_interceptors(null_interceptor): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubPluginRestInterceptor(), + ) + client = ApiHubPluginClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_create_plugin" + ) as post, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_create_plugin_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "pre_create_plugin" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = plugin_service.CreatePluginRequest.pb( + plugin_service.CreatePluginRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = plugin_service.Plugin.to_json(plugin_service.Plugin()) + req.return_value.content = return_value + + request = plugin_service.CreatePluginRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = plugin_service.Plugin() + post_with_metadata.return_value = plugin_service.Plugin(), metadata + + client.create_plugin( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_list_plugins_rest_bad_request(request_type=plugin_service.ListPluginsRequest): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_plugins(request) + + +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.ListPluginsRequest, + dict, + ], +) +def test_list_plugins_rest_call_success(request_type): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = plugin_service.ListPluginsResponse( + next_page_token="next_page_token_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = plugin_service.ListPluginsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.list_plugins(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListPluginsPager) + assert response.next_page_token == "next_page_token_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_plugins_rest_interceptors(null_interceptor): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubPluginRestInterceptor(), + ) + client = ApiHubPluginClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_list_plugins" + ) as post, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_list_plugins_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "pre_list_plugins" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = plugin_service.ListPluginsRequest.pb( + plugin_service.ListPluginsRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = plugin_service.ListPluginsResponse.to_json( + plugin_service.ListPluginsResponse() + ) + req.return_value.content = return_value + + request = plugin_service.ListPluginsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = plugin_service.ListPluginsResponse() + post_with_metadata.return_value = plugin_service.ListPluginsResponse(), metadata + + client.list_plugins( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_delete_plugin_rest_bad_request( + request_type=plugin_service.DeletePluginRequest, +): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/plugins/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.delete_plugin(request) + + +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.DeletePluginRequest, + dict, + ], +) +def test_delete_plugin_rest_call_success(request_type): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/plugins/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.delete_plugin(request) + + # Establish that the response is the type that we expect. + json_return_value = json_format.MessageToJson(return_value) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_plugin_rest_interceptors(null_interceptor): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubPluginRestInterceptor(), + ) + client = ApiHubPluginClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_delete_plugin" + ) as post, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_delete_plugin_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "pre_delete_plugin" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = plugin_service.DeletePluginRequest.pb( + plugin_service.DeletePluginRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = json_format.MessageToJson(operations_pb2.Operation()) + req.return_value.content = return_value + + request = plugin_service.DeletePluginRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + post_with_metadata.return_value = operations_pb2.Operation(), metadata + + client.delete_plugin( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_create_plugin_instance_rest_bad_request( + request_type=plugin_service.CreatePluginInstanceRequest, +): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/plugins/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.create_plugin_instance(request) + + +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.CreatePluginInstanceRequest, + dict, + ], +) +def test_create_plugin_instance_rest_call_success(request_type): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/plugins/sample3"} + request_init["plugin_instance"] = { + "name": "name_value", + "display_name": "display_name_value", + "auth_config": { + "google_service_account_config": { + "service_account": "service_account_value" + }, + "user_password_config": { + "username": "username_value", + "password": {"secret_version": "secret_version_value"}, + }, + "api_key_config": { + "name": "name_value", + "api_key": {}, + "http_element_location": 1, + }, + "oauth2_client_credentials_config": { + "client_id": "client_id_value", + "client_secret": {}, + }, + "auth_type": 1, + }, + "additional_config": {}, + "state": 1, + "error_message": "error_message_value", + "actions": [ + { + "hub_instance_action": { + "current_execution_state": 1, + "last_execution": { + "result": 1, + "error_message": "error_message_value", + "start_time": {"seconds": 751, "nanos": 543}, + "end_time": {}, + }, + }, + "action_id": "action_id_value", + "state": 1, + "schedule_cron_expression": "schedule_cron_expression_value", + "curation_config": { + "custom_curation": {"curation": "curation_value"}, + "curation_type": 1, + }, + "schedule_time_zone": "schedule_time_zone_value", + "service_account": "service_account_value", + "resource_config": { + "action_type": 1, + "pubsub_topic": "pubsub_topic_value", + }, + } + ], + "create_time": {}, + "update_time": {}, + "source_project_id": "source_project_id_value", + } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = plugin_service.CreatePluginInstanceRequest.meta.fields[ + "plugin_instance" + ] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["plugin_instance"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["plugin_instance"][field])): + del request_init["plugin_instance"][field][i][subfield] + else: + del request_init["plugin_instance"][field][subfield] + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.create_plugin_instance(request) + + # Establish that the response is the type that we expect. + json_return_value = json_format.MessageToJson(return_value) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_plugin_instance_rest_interceptors(null_interceptor): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubPluginRestInterceptor(), + ) + client = ApiHubPluginClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_create_plugin_instance" + ) as post, mock.patch.object( + transports.ApiHubPluginRestInterceptor, + "post_create_plugin_instance_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "pre_create_plugin_instance" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = plugin_service.CreatePluginInstanceRequest.pb( + plugin_service.CreatePluginInstanceRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = json_format.MessageToJson(operations_pb2.Operation()) + req.return_value.content = return_value + + request = plugin_service.CreatePluginInstanceRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + post_with_metadata.return_value = operations_pb2.Operation(), metadata + + client.create_plugin_instance( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_execute_plugin_instance_action_rest_bad_request( + request_type=plugin_service.ExecutePluginInstanceActionRequest, +): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.execute_plugin_instance_action(request) + + +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.ExecutePluginInstanceActionRequest, + dict, + ], +) +def test_execute_plugin_instance_action_rest_call_success(request_type): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.execute_plugin_instance_action(request) + + # Establish that the response is the type that we expect. + json_return_value = json_format.MessageToJson(return_value) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_execute_plugin_instance_action_rest_interceptors(null_interceptor): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubPluginRestInterceptor(), + ) + client = ApiHubPluginClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_execute_plugin_instance_action" + ) as post, mock.patch.object( + transports.ApiHubPluginRestInterceptor, + "post_execute_plugin_instance_action_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "pre_execute_plugin_instance_action" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = plugin_service.ExecutePluginInstanceActionRequest.pb( + plugin_service.ExecutePluginInstanceActionRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = json_format.MessageToJson(operations_pb2.Operation()) + req.return_value.content = return_value + + request = plugin_service.ExecutePluginInstanceActionRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + post_with_metadata.return_value = operations_pb2.Operation(), metadata + + client.execute_plugin_instance_action( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_get_plugin_instance_rest_bad_request( + request_type=plugin_service.GetPluginInstanceRequest, +): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_plugin_instance(request) + + +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.GetPluginInstanceRequest, + dict, + ], +) +def test_get_plugin_instance_rest_call_success(request_type): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = plugin_service.PluginInstance( + name="name_value", + display_name="display_name_value", + state=plugin_service.PluginInstance.State.CREATING, + error_message="error_message_value", + source_project_id="source_project_id_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = plugin_service.PluginInstance.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.get_plugin_instance(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, plugin_service.PluginInstance) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.state == plugin_service.PluginInstance.State.CREATING + assert response.error_message == "error_message_value" + assert response.source_project_id == "source_project_id_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_plugin_instance_rest_interceptors(null_interceptor): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubPluginRestInterceptor(), + ) + client = ApiHubPluginClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_get_plugin_instance" + ) as post, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_get_plugin_instance_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "pre_get_plugin_instance" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = plugin_service.GetPluginInstanceRequest.pb( + plugin_service.GetPluginInstanceRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = plugin_service.PluginInstance.to_json( + plugin_service.PluginInstance() + ) + req.return_value.content = return_value + + request = plugin_service.GetPluginInstanceRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = plugin_service.PluginInstance() + post_with_metadata.return_value = plugin_service.PluginInstance(), metadata + + client.get_plugin_instance( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_list_plugin_instances_rest_bad_request( + request_type=plugin_service.ListPluginInstancesRequest, +): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/plugins/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_plugin_instances(request) + + +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.ListPluginInstancesRequest, + dict, + ], +) +def test_list_plugin_instances_rest_call_success(request_type): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/plugins/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = plugin_service.ListPluginInstancesResponse( + next_page_token="next_page_token_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = plugin_service.ListPluginInstancesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.list_plugin_instances(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListPluginInstancesPager) + assert response.next_page_token == "next_page_token_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_plugin_instances_rest_interceptors(null_interceptor): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubPluginRestInterceptor(), + ) + client = ApiHubPluginClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_list_plugin_instances" + ) as post, mock.patch.object( + transports.ApiHubPluginRestInterceptor, + "post_list_plugin_instances_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "pre_list_plugin_instances" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = plugin_service.ListPluginInstancesRequest.pb( + plugin_service.ListPluginInstancesRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = plugin_service.ListPluginInstancesResponse.to_json( + plugin_service.ListPluginInstancesResponse() + ) + req.return_value.content = return_value + + request = plugin_service.ListPluginInstancesRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = plugin_service.ListPluginInstancesResponse() + post_with_metadata.return_value = ( + plugin_service.ListPluginInstancesResponse(), + metadata, + ) + + client.list_plugin_instances( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_enable_plugin_instance_action_rest_bad_request( + request_type=plugin_service.EnablePluginInstanceActionRequest, +): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.enable_plugin_instance_action(request) + + +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.EnablePluginInstanceActionRequest, + dict, + ], +) +def test_enable_plugin_instance_action_rest_call_success(request_type): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.enable_plugin_instance_action(request) + + # Establish that the response is the type that we expect. + json_return_value = json_format.MessageToJson(return_value) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_enable_plugin_instance_action_rest_interceptors(null_interceptor): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubPluginRestInterceptor(), + ) + client = ApiHubPluginClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_enable_plugin_instance_action" + ) as post, mock.patch.object( + transports.ApiHubPluginRestInterceptor, + "post_enable_plugin_instance_action_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "pre_enable_plugin_instance_action" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = plugin_service.EnablePluginInstanceActionRequest.pb( + plugin_service.EnablePluginInstanceActionRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = json_format.MessageToJson(operations_pb2.Operation()) + req.return_value.content = return_value + + request = plugin_service.EnablePluginInstanceActionRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + post_with_metadata.return_value = operations_pb2.Operation(), metadata + + client.enable_plugin_instance_action( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_disable_plugin_instance_action_rest_bad_request( + request_type=plugin_service.DisablePluginInstanceActionRequest, +): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.disable_plugin_instance_action(request) + + +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.DisablePluginInstanceActionRequest, + dict, + ], +) +def test_disable_plugin_instance_action_rest_call_success(request_type): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.disable_plugin_instance_action(request) + + # Establish that the response is the type that we expect. + json_return_value = json_format.MessageToJson(return_value) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_disable_plugin_instance_action_rest_interceptors(null_interceptor): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubPluginRestInterceptor(), + ) + client = ApiHubPluginClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_disable_plugin_instance_action" + ) as post, mock.patch.object( + transports.ApiHubPluginRestInterceptor, + "post_disable_plugin_instance_action_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "pre_disable_plugin_instance_action" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = plugin_service.DisablePluginInstanceActionRequest.pb( + plugin_service.DisablePluginInstanceActionRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = json_format.MessageToJson(operations_pb2.Operation()) + req.return_value.content = return_value + + request = plugin_service.DisablePluginInstanceActionRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + post_with_metadata.return_value = operations_pb2.Operation(), metadata + + client.disable_plugin_instance_action( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_update_plugin_instance_rest_bad_request( + request_type=plugin_service.UpdatePluginInstanceRequest, +): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "plugin_instance": { + "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" + } + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.update_plugin_instance(request) + + +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.UpdatePluginInstanceRequest, + dict, + ], +) +def test_update_plugin_instance_rest_call_success(request_type): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "plugin_instance": { + "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" + } + } + request_init["plugin_instance"] = { + "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4", + "display_name": "display_name_value", + "auth_config": { + "google_service_account_config": { + "service_account": "service_account_value" + }, + "user_password_config": { + "username": "username_value", + "password": {"secret_version": "secret_version_value"}, + }, + "api_key_config": { + "name": "name_value", + "api_key": {}, + "http_element_location": 1, + }, + "oauth2_client_credentials_config": { + "client_id": "client_id_value", + "client_secret": {}, + }, + "auth_type": 1, + }, + "additional_config": {}, + "state": 1, + "error_message": "error_message_value", + "actions": [ + { + "hub_instance_action": { + "current_execution_state": 1, + "last_execution": { + "result": 1, + "error_message": "error_message_value", + "start_time": {"seconds": 751, "nanos": 543}, + "end_time": {}, + }, + }, + "action_id": "action_id_value", + "state": 1, + "schedule_cron_expression": "schedule_cron_expression_value", + "curation_config": { + "custom_curation": {"curation": "curation_value"}, + "curation_type": 1, + }, + "schedule_time_zone": "schedule_time_zone_value", + "service_account": "service_account_value", + "resource_config": { + "action_type": 1, + "pubsub_topic": "pubsub_topic_value", + }, + } + ], + "create_time": {}, + "update_time": {}, + "source_project_id": "source_project_id_value", + } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = plugin_service.UpdatePluginInstanceRequest.meta.fields[ + "plugin_instance" + ] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["plugin_instance"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["plugin_instance"][field])): + del request_init["plugin_instance"][field][i][subfield] + else: + del request_init["plugin_instance"][field][subfield] + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = plugin_service.PluginInstance( + name="name_value", + display_name="display_name_value", + state=plugin_service.PluginInstance.State.CREATING, + error_message="error_message_value", + source_project_id="source_project_id_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = plugin_service.PluginInstance.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.update_plugin_instance(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, plugin_service.PluginInstance) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.state == plugin_service.PluginInstance.State.CREATING + assert response.error_message == "error_message_value" + assert response.source_project_id == "source_project_id_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_update_plugin_instance_rest_interceptors(null_interceptor): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubPluginRestInterceptor(), + ) + client = ApiHubPluginClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_update_plugin_instance" + ) as post, mock.patch.object( + transports.ApiHubPluginRestInterceptor, + "post_update_plugin_instance_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "pre_update_plugin_instance" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = plugin_service.UpdatePluginInstanceRequest.pb( + plugin_service.UpdatePluginInstanceRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = plugin_service.PluginInstance.to_json( + plugin_service.PluginInstance() + ) + req.return_value.content = return_value + + request = plugin_service.UpdatePluginInstanceRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = plugin_service.PluginInstance() + post_with_metadata.return_value = plugin_service.PluginInstance(), metadata + + client.update_plugin_instance( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_delete_plugin_instance_rest_bad_request( + request_type=plugin_service.DeletePluginInstanceRequest, +): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.delete_plugin_instance(request) + + +@pytest.mark.parametrize( + "request_type", + [ + plugin_service.DeletePluginInstanceRequest, + dict, + ], +) +def test_delete_plugin_instance_rest_call_success(request_type): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.delete_plugin_instance(request) + + # Establish that the response is the type that we expect. + json_return_value = json_format.MessageToJson(return_value) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_plugin_instance_rest_interceptors(null_interceptor): + transport = transports.ApiHubPluginRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ApiHubPluginRestInterceptor(), + ) + client = ApiHubPluginClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.ApiHubPluginRestInterceptor, "post_delete_plugin_instance" + ) as post, mock.patch.object( + transports.ApiHubPluginRestInterceptor, + "post_delete_plugin_instance_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.ApiHubPluginRestInterceptor, "pre_delete_plugin_instance" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = plugin_service.DeletePluginInstanceRequest.pb( + plugin_service.DeletePluginInstanceRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = json_format.MessageToJson(operations_pb2.Operation()) + req.return_value.content = return_value + + request = plugin_service.DeletePluginInstanceRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + post_with_metadata.return_value = operations_pb2.Operation(), metadata + + client.delete_plugin_instance( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_get_location_rest_bad_request(request_type=locations_pb2.GetLocationRequest): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_location(request) + + +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.GetLocationRequest, + dict, + ], +) +def test_get_location_rest(request_type): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = locations_pb2.Location() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_location(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) + + +def test_list_locations_rest_bad_request( + request_type=locations_pb2.ListLocationsRequest, +): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict({"name": "projects/sample1"}, request) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_locations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.ListLocationsRequest, + dict, + ], +) +def test_list_locations_rest(request_type): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = locations_pb2.ListLocationsResponse() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_locations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + +def test_cancel_operation_rest_bad_request( + request_type=operations_pb2.CancelOperationRequest, +): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.cancel_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.CancelOperationRequest, + dict, + ], +) +def test_cancel_operation_rest(request_type): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "{}" + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.cancel_operation(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_delete_operation_rest_bad_request( + request_type=operations_pb2.DeleteOperationRequest, +): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.delete_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.DeleteOperationRequest, + dict, + ], +) +def test_delete_operation_rest(request_type): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "{}" + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_operation(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_get_operation_rest_bad_request( + request_type=operations_pb2.GetOperationRequest, +): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.GetOperationRequest, + dict, + ], +) +def test_get_operation_rest(request_type): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_operation(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) + + +def test_list_operations_rest_bad_request( + request_type=operations_pb2.ListOperationsRequest, +): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_operations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.ListOperationsRequest, + dict, + ], +) +def test_list_operations_rest(request_type): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.ListOperationsResponse() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_operations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) + + +def test_initialize_client_w_rest(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_plugin_empty_call_rest(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_plugin), "__call__") as call: + client.get_plugin(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.GetPluginRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_enable_plugin_empty_call_rest(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.enable_plugin), "__call__") as call: + client.enable_plugin(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.EnablePluginRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_disable_plugin_empty_call_rest(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.disable_plugin), "__call__") as call: + client.disable_plugin(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.DisablePluginRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_plugin_empty_call_rest(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.create_plugin), "__call__") as call: + client.create_plugin(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.CreatePluginRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_plugins_empty_call_rest(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.list_plugins), "__call__") as call: + client.list_plugins(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.ListPluginsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_plugin_empty_call_rest(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.delete_plugin), "__call__") as call: + client.delete_plugin(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.DeletePluginRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_plugin_instance_empty_call_rest(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_plugin_instance), "__call__" + ) as call: + client.create_plugin_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.CreatePluginInstanceRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_execute_plugin_instance_action_empty_call_rest(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.execute_plugin_instance_action), "__call__" + ) as call: + client.execute_plugin_instance_action(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.ExecutePluginInstanceActionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_plugin_instance_empty_call_rest(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_plugin_instance), "__call__" + ) as call: + client.get_plugin_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.GetPluginInstanceRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_plugin_instances_empty_call_rest(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_plugin_instances), "__call__" + ) as call: + client.list_plugin_instances(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.ListPluginInstancesRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_enable_plugin_instance_action_empty_call_rest(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.enable_plugin_instance_action), "__call__" + ) as call: + client.enable_plugin_instance_action(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.EnablePluginInstanceActionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_disable_plugin_instance_action_empty_call_rest(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.disable_plugin_instance_action), "__call__" + ) as call: + client.disable_plugin_instance_action(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.DisablePluginInstanceActionRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_update_plugin_instance_empty_call_rest(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.update_plugin_instance), "__call__" + ) as call: + client.update_plugin_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.UpdatePluginInstanceRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_plugin_instance_empty_call_rest(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.delete_plugin_instance), "__call__" + ) as call: + client.delete_plugin_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = plugin_service.DeletePluginInstanceRequest() + + assert args[0] == request_msg + + +def test_api_hub_plugin_rest_lro_client(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + transport = client.transport + + # Ensure that we have an api-core operations client. + assert isinstance( + transport.operations_client, + operations_v1.AbstractOperationsClient, + ) + + # Ensure that subsequent calls to the property send the exact same object. + assert transport.operations_client is transport.operations_client + + +def test_transport_grpc_default(): + # A client should use the gRPC transport by default. + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert isinstance( + client.transport, + transports.ApiHubPluginGrpcTransport, + ) + + +def test_api_hub_plugin_base_transport_error(): + # Passing both a credentials object and credentials_file should raise an error + with pytest.raises(core_exceptions.DuplicateCredentialArgs): + transport = transports.ApiHubPluginTransport( + credentials=ga_credentials.AnonymousCredentials(), + credentials_file="credentials.json", + ) + + +def test_api_hub_plugin_base_transport(): + # Instantiate the base transport. + with mock.patch( + "google.cloud.apihub_v1.services.api_hub_plugin.transports.ApiHubPluginTransport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.ApiHubPluginTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Every method on the transport should just blindly + # raise NotImplementedError. + methods = ( + "get_plugin", + "enable_plugin", + "disable_plugin", + "create_plugin", + "list_plugins", + "delete_plugin", + "create_plugin_instance", + "execute_plugin_instance_action", + "get_plugin_instance", + "list_plugin_instances", + "enable_plugin_instance_action", + "disable_plugin_instance_action", + "update_plugin_instance", + "delete_plugin_instance", + "get_location", + "list_locations", + "get_operation", + "cancel_operation", + "delete_operation", + "list_operations", + ) + for method in methods: + with pytest.raises(NotImplementedError): + getattr(transport, method)(request=object()) + + with pytest.raises(NotImplementedError): + transport.close() + + # Additionally, the LRO client (a property) should + # also raise NotImplementedError + with pytest.raises(NotImplementedError): + transport.operations_client + + # Catch all for all remaining methods and properties + remainder = [ + "kind", + ] + for r in remainder: + with pytest.raises(NotImplementedError): + getattr(transport, r)() + + +def test_api_hub_plugin_base_transport_with_credentials_file(): + # Instantiate the base transport with a credentials file + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch( + "google.cloud.apihub_v1.services.api_hub_plugin.transports.ApiHubPluginTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) + transport = transports.ApiHubPluginTransport( + credentials_file="credentials.json", + quota_project_id="octopus", + ) + load_creds.assert_called_once_with( + "credentials.json", + scopes=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", + ) + + +def test_api_hub_plugin_base_transport_with_adc(): + # Test the default credentials are used if credentials and credentials_file are None. + with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch( + "google.cloud.apihub_v1.services.api_hub_plugin.transports.ApiHubPluginTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport = transports.ApiHubPluginTransport() + adc.assert_called_once() + + +def test_api_hub_plugin_auth_adc(): + # If no credentials are provided, we should use ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + ApiHubPluginClient() + adc.assert_called_once_with( + scopes=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id=None, ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubPluginGrpcTransport, + transports.ApiHubPluginGrpcAsyncIOTransport, + ], +) +def test_api_hub_plugin_transport_auth_adc(transport_class): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + adc.assert_called_once_with( + scopes=["1", "2"], + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", + ) -def test_delete_plugin_instance_rest_bad_request( - request_type=plugin_service.DeletePluginInstanceRequest, -): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" - } - request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_plugin_instance(request) +@pytest.mark.parametrize( + "transport_class", + [ + transports.ApiHubPluginGrpcTransport, + transports.ApiHubPluginGrpcAsyncIOTransport, + transports.ApiHubPluginRestTransport, + ], +) +def test_api_hub_plugin_transport_auth_gdch_credentials(transport_class): + host = "https://language.com" + api_audience_tests = [None, "https://language2.com"] + api_audience_expect = [host, "https://language2.com"] + for t, e in zip(api_audience_tests, api_audience_expect): + with mock.patch.object(google.auth, "default", autospec=True) as adc: + gdch_mock = mock.MagicMock() + type(gdch_mock).with_gdch_audience = mock.PropertyMock( + return_value=gdch_mock + ) + adc.return_value = (gdch_mock, None) + transport_class(host=host, api_audience=t) + gdch_mock.with_gdch_audience.assert_called_once_with(e) @pytest.mark.parametrize( - "request_type", + "transport_class,grpc_helpers", [ - plugin_service.DeletePluginInstanceRequest, - dict, + (transports.ApiHubPluginGrpcTransport, grpc_helpers), + (transports.ApiHubPluginGrpcAsyncIOTransport, grpc_helpers_async), ], ) -def test_delete_plugin_instance_rest_call_success(request_type): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) +def test_api_hub_plugin_transport_create_channel(transport_class, grpc_helpers): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel", autospec=True + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + adc.return_value = (creds, None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + + create_channel.assert_called_with( + "apihub.googleapis.com:443", + credentials=creds, + credentials_file=None, + quota_project_id="octopus", + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + scopes=["1", "2"], + default_host="apihub.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/plugins/sample3/instances/sample4" - } - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") +@pytest.mark.parametrize( + "transport_class", + [transports.ApiHubPluginGrpcTransport, transports.ApiHubPluginGrpcAsyncIOTransport], +) +def test_api_hub_plugin_grpc_transport_client_cert_source_for_mtls(transport_class): + cred = ga_credentials.AnonymousCredentials() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.delete_plugin_instance(request) + # Check ssl_channel_credentials is used if provided. + with mock.patch.object(transport_class, "create_channel") as mock_create_channel: + mock_ssl_channel_creds = mock.Mock() + transport_class( + host="squid.clam.whelk", + credentials=cred, + ssl_channel_credentials=mock_ssl_channel_creds, + ) + mock_create_channel.assert_called_once_with( + "squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_channel_creds, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) - # Establish that the response is the type that we expect. - json_return_value = json_format.MessageToJson(return_value) + # Check if ssl_channel_credentials is not provided, then client_cert_source_for_mtls + # is used. + with mock.patch.object(transport_class, "create_channel", return_value=mock.Mock()): + with mock.patch("grpc.ssl_channel_credentials") as mock_ssl_cred: + transport_class( + credentials=cred, + client_cert_source_for_mtls=client_cert_source_callback, + ) + expected_cert, expected_key = client_cert_source_callback() + mock_ssl_cred.assert_called_once_with( + certificate_chain=expected_cert, private_key=expected_key + ) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_delete_plugin_instance_rest_interceptors(null_interceptor): - transport = transports.ApiHubPluginRestTransport( +def test_api_hub_plugin_http_transport_client_cert_source_for_mtls(): + cred = ga_credentials.AnonymousCredentials() + with mock.patch( + "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" + ) as mock_configure_mtls_channel: + transports.ApiHubPluginRestTransport( + credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + ) + mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + + +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "grpc_asyncio", + "rest", + ], +) +def test_api_hub_plugin_host_no_port(transport_name): + client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ApiHubPluginRestInterceptor(), + client_options=client_options.ClientOptions( + api_endpoint="apihub.googleapis.com" + ), + transport=transport_name, + ) + assert client.transport._host == ( + "apihub.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://apihub.googleapis.com" ) - client = ApiHubPluginClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - operation.Operation, "_set_result_from_operation" - ), mock.patch.object( - transports.ApiHubPluginRestInterceptor, "post_delete_plugin_instance" - ) as post, mock.patch.object( - transports.ApiHubPluginRestInterceptor, - "post_delete_plugin_instance_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.ApiHubPluginRestInterceptor, "pre_delete_plugin_instance" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = plugin_service.DeletePluginInstanceRequest.pb( - plugin_service.DeletePluginInstanceRequest() - ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = json_format.MessageToJson(operations_pb2.Operation()) - req.return_value.content = return_value +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "grpc_asyncio", + "rest", + ], +) +def test_api_hub_plugin_host_with_port(transport_name): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="apihub.googleapis.com:8000" + ), + transport=transport_name, + ) + assert client.transport._host == ( + "apihub.googleapis.com:8000" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://apihub.googleapis.com:8000" + ) - request = plugin_service.DeletePluginInstanceRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = operations_pb2.Operation() - post_with_metadata.return_value = operations_pb2.Operation(), metadata - client.delete_plugin_instance( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], - ) +@pytest.mark.parametrize( + "transport_name", + [ + "rest", + ], +) +def test_api_hub_plugin_client_transport_session_collision(transport_name): + creds1 = ga_credentials.AnonymousCredentials() + creds2 = ga_credentials.AnonymousCredentials() + client1 = ApiHubPluginClient( + credentials=creds1, + transport=transport_name, + ) + client2 = ApiHubPluginClient( + credentials=creds2, + transport=transport_name, + ) + session1 = client1.transport.get_plugin._session + session2 = client2.transport.get_plugin._session + assert session1 != session2 + session1 = client1.transport.enable_plugin._session + session2 = client2.transport.enable_plugin._session + assert session1 != session2 + session1 = client1.transport.disable_plugin._session + session2 = client2.transport.disable_plugin._session + assert session1 != session2 + session1 = client1.transport.create_plugin._session + session2 = client2.transport.create_plugin._session + assert session1 != session2 + session1 = client1.transport.list_plugins._session + session2 = client2.transport.list_plugins._session + assert session1 != session2 + session1 = client1.transport.delete_plugin._session + session2 = client2.transport.delete_plugin._session + assert session1 != session2 + session1 = client1.transport.create_plugin_instance._session + session2 = client2.transport.create_plugin_instance._session + assert session1 != session2 + session1 = client1.transport.execute_plugin_instance_action._session + session2 = client2.transport.execute_plugin_instance_action._session + assert session1 != session2 + session1 = client1.transport.get_plugin_instance._session + session2 = client2.transport.get_plugin_instance._session + assert session1 != session2 + session1 = client1.transport.list_plugin_instances._session + session2 = client2.transport.list_plugin_instances._session + assert session1 != session2 + session1 = client1.transport.enable_plugin_instance_action._session + session2 = client2.transport.enable_plugin_instance_action._session + assert session1 != session2 + session1 = client1.transport.disable_plugin_instance_action._session + session2 = client2.transport.disable_plugin_instance_action._session + assert session1 != session2 + session1 = client1.transport.update_plugin_instance._session + session2 = client2.transport.update_plugin_instance._session + assert session1 != session2 + session1 = client1.transport.delete_plugin_instance._session + session2 = client2.transport.delete_plugin_instance._session + assert session1 != session2 - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() +def test_api_hub_plugin_grpc_transport_channel(): + channel = grpc.secure_channel("http://localhost/", grpc.local_channel_credentials()) -def test_get_location_rest_bad_request(request_type=locations_pb2.GetLocationRequest): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + # Check that channel is used if provided. + transport = transports.ApiHubPluginGrpcTransport( + host="squid.clam.whelk", + channel=channel, ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2"}, request + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None + + +def test_api_hub_plugin_grpc_asyncio_transport_channel(): + channel = aio.secure_channel("http://localhost/", grpc.local_channel_credentials()) + + # Check that channel is used if provided. + transport = transports.ApiHubPluginGrpcAsyncIOTransport( + host="squid.clam.whelk", + channel=channel, ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_location(request) + +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. +@pytest.mark.parametrize( + "transport_class", + [transports.ApiHubPluginGrpcTransport, transports.ApiHubPluginGrpcAsyncIOTransport], +) +def test_api_hub_plugin_transport_channel_mtls_with_client_cert_source(transport_class): + with mock.patch( + "grpc.ssl_channel_credentials", autospec=True + ) as grpc_ssl_channel_cred: + with mock.patch.object( + transport_class, "create_channel" + ) 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 = ga_credentials.AnonymousCredentials() + with pytest.warns(DeprecationWarning): + with mock.patch.object(google.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=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + assert transport._ssl_channel_credentials == mock_ssl_cred +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. @pytest.mark.parametrize( - "request_type", - [ - locations_pb2.GetLocationRequest, - dict, - ], + "transport_class", + [transports.ApiHubPluginGrpcTransport, transports.ApiHubPluginGrpcAsyncIOTransport], ) -def test_get_location_rest(request_type): +def test_api_hub_plugin_transport_channel_mtls_with_adc(transport_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), + ): + with mock.patch.object( + transport_class, "create_channel" + ) 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, + ) + + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=mock_cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + + +def test_api_hub_plugin_grpc_lro_client(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) + transport = client.transport - request_init = {"name": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = locations_pb2.Location() + # Ensure that we have a api-core operations client. + assert isinstance( + transport.operations_client, + operations_v1.OperationsClient, + ) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + # Ensure that subsequent calls to the property send the exact same object. + assert transport.operations_client is transport.operations_client - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_location(request) +def test_api_hub_plugin_grpc_lro_async_client(): + client = ApiHubPluginAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc_asyncio", + ) + transport = client.transport - # Establish that the response is the type that we expect. - assert isinstance(response, locations_pb2.Location) + # Ensure that we have a api-core operations client. + assert isinstance( + transport.operations_client, + operations_v1.OperationsAsyncClient, + ) + + # Ensure that subsequent calls to the property send the exact same object. + assert transport.operations_client is transport.operations_client -def test_list_locations_rest_bad_request( - request_type=locations_pb2.ListLocationsRequest, -): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +def test_attribute_path(): + project = "squid" + location = "clam" + attribute = "whelk" + expected = "projects/{project}/locations/{location}/attributes/{attribute}".format( + project=project, + location=location, + attribute=attribute, ) - request = request_type() - request = json_format.ParseDict({"name": "projects/sample1"}, request) + actual = ApiHubPluginClient.attribute_path(project, location, attribute) + assert expected == actual - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_locations(request) +def test_parse_attribute_path(): + expected = { + "project": "octopus", + "location": "oyster", + "attribute": "nudibranch", + } + path = ApiHubPluginClient.attribute_path(**expected) -@pytest.mark.parametrize( - "request_type", - [ - locations_pb2.ListLocationsRequest, - dict, - ], -) -def test_list_locations_rest(request_type): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + # Check that the path construction is reversible. + actual = ApiHubPluginClient.parse_attribute_path(path) + assert expected == actual - request_init = {"name": "projects/sample1"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = locations_pb2.ListLocationsResponse() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") +def test_plugin_path(): + project = "cuttlefish" + location = "mussel" + plugin = "winkle" + expected = "projects/{project}/locations/{location}/plugins/{plugin}".format( + project=project, + location=location, + plugin=plugin, + ) + actual = ApiHubPluginClient.plugin_path(project, location, plugin) + assert expected == actual - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.list_locations(request) +def test_parse_plugin_path(): + expected = { + "project": "nautilus", + "location": "scallop", + "plugin": "abalone", + } + path = ApiHubPluginClient.plugin_path(**expected) - # Establish that the response is the type that we expect. - assert isinstance(response, locations_pb2.ListLocationsResponse) + # Check that the path construction is reversible. + actual = ApiHubPluginClient.parse_plugin_path(path) + assert expected == actual -def test_cancel_operation_rest_bad_request( - request_type=operations_pb2.CancelOperationRequest, -): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +def test_plugin_instance_path(): + project = "squid" + location = "clam" + plugin = "whelk" + instance = "octopus" + expected = "projects/{project}/locations/{location}/plugins/{plugin}/instances/{instance}".format( + project=project, + location=location, + plugin=plugin, + instance=instance, ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + actual = ApiHubPluginClient.plugin_instance_path( + project, location, plugin, instance ) + assert expected == actual - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.cancel_operation(request) +def test_parse_plugin_instance_path(): + expected = { + "project": "oyster", + "location": "nudibranch", + "plugin": "cuttlefish", + "instance": "mussel", + } + path = ApiHubPluginClient.plugin_instance_path(**expected) -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.CancelOperationRequest, - dict, - ], -) -def test_cancel_operation_rest(request_type): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + # Check that the path construction is reversible. + actual = ApiHubPluginClient.parse_plugin_instance_path(path) + assert expected == actual - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = None - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "{}" - response_value.content = json_return_value.encode("UTF-8") +def test_service_account_path(): + project = "winkle" + service_account = "nautilus" + expected = "projects/{project}/serviceAccounts/{service_account}".format( + project=project, + service_account=service_account, + ) + actual = ApiHubPluginClient.service_account_path(project, service_account) + assert expected == actual - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.cancel_operation(request) +def test_parse_service_account_path(): + expected = { + "project": "scallop", + "service_account": "abalone", + } + path = ApiHubPluginClient.service_account_path(**expected) - # Establish that the response is the type that we expect. - assert response is None + # Check that the path construction is reversible. + actual = ApiHubPluginClient.parse_service_account_path(path) + assert expected == actual -def test_delete_operation_rest_bad_request( - request_type=operations_pb2.DeleteOperationRequest, -): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request +def test_common_billing_account_path(): + billing_account = "squid" + expected = "billingAccounts/{billing_account}".format( + billing_account=billing_account, ) + actual = ApiHubPluginClient.common_billing_account_path(billing_account) + assert expected == actual - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_operation(request) +def test_parse_common_billing_account_path(): + expected = { + "billing_account": "clam", + } + path = ApiHubPluginClient.common_billing_account_path(**expected) -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.DeleteOperationRequest, - dict, - ], -) -def test_delete_operation_rest(request_type): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + # Check that the path construction is reversible. + actual = ApiHubPluginClient.parse_common_billing_account_path(path) + assert expected == actual - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = None - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "{}" - response_value.content = json_return_value.encode("UTF-8") +def test_common_folder_path(): + folder = "whelk" + expected = "folders/{folder}".format( + folder=folder, + ) + actual = ApiHubPluginClient.common_folder_path(folder) + assert expected == actual - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.delete_operation(request) +def test_parse_common_folder_path(): + expected = { + "folder": "octopus", + } + path = ApiHubPluginClient.common_folder_path(**expected) - # Establish that the response is the type that we expect. - assert response is None + # Check that the path construction is reversible. + actual = ApiHubPluginClient.parse_common_folder_path(path) + assert expected == actual -def test_get_operation_rest_bad_request( - request_type=operations_pb2.GetOperationRequest, -): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request +def test_common_organization_path(): + organization = "oyster" + expected = "organizations/{organization}".format( + organization=organization, ) + actual = ApiHubPluginClient.common_organization_path(organization) + assert expected == actual - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_operation(request) +def test_parse_common_organization_path(): + expected = { + "organization": "nudibranch", + } + path = ApiHubPluginClient.common_organization_path(**expected) + + # Check that the path construction is reversible. + actual = ApiHubPluginClient.parse_common_organization_path(path) + assert expected == actual -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.GetOperationRequest, - dict, - ], -) -def test_get_operation_rest(request_type): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + +def test_common_project_path(): + project = "cuttlefish" + expected = "projects/{project}".format( + project=project, ) + actual = ApiHubPluginClient.common_project_path(project) + assert expected == actual - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") +def test_parse_common_project_path(): + expected = { + "project": "mussel", + } + path = ApiHubPluginClient.common_project_path(**expected) - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Check that the path construction is reversible. + actual = ApiHubPluginClient.parse_common_project_path(path) + assert expected == actual - response = client.get_operation(request) - # Establish that the response is the type that we expect. - assert isinstance(response, operations_pb2.Operation) +def test_common_location_path(): + project = "winkle" + location = "nautilus" + expected = "projects/{project}/locations/{location}".format( + project=project, + location=location, + ) + actual = ApiHubPluginClient.common_location_path(project, location) + assert expected == actual -def test_list_operations_rest_bad_request( - request_type=operations_pb2.ListOperationsRequest, -): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2"}, request - ) +def test_parse_common_location_path(): + expected = { + "project": "scallop", + "location": "abalone", + } + path = ApiHubPluginClient.common_location_path(**expected) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_operations(request) + # Check that the path construction is reversible. + actual = ApiHubPluginClient.parse_common_location_path(path) + assert expected == actual -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.ListOperationsRequest, - dict, - ], -) -def test_list_operations_rest(request_type): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) +def test_client_with_default_client_info(): + client_info = gapic_v1.client_info.ClientInfo() - request_init = {"name": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.ListOperationsResponse() + with mock.patch.object( + transports.ApiHubPluginTransport, "_prep_wrapped_messages" + ) as prep: + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + client_info=client_info, + ) + prep.assert_called_once_with(client_info) + + with mock.patch.object( + transports.ApiHubPluginTransport, "_prep_wrapped_messages" + ) as prep: + transport_class = ApiHubPluginClient.get_transport_class() + transport = transport_class( + credentials=ga_credentials.AnonymousCredentials(), + client_info=client_info, + ) + prep.assert_called_once_with(client_info) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +def test_delete_operation(transport: str = "grpc"): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) - response = client.list_operations(request) + # 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 = operations_pb2.DeleteOperationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.delete_operation(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 response is the type that we expect. - assert isinstance(response, operations_pb2.ListOperationsResponse) + assert response is None -def test_initialize_client_w_rest(): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" +@pytest.mark.asyncio +async def test_delete_operation_async(transport: str = "grpc_asyncio"): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - assert client is not None + # 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 = operations_pb2.DeleteOperationRequest() -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_get_plugin_empty_call_rest(): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_operation(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 response is the type that we expect. + assert response is None + + +def test_delete_operation_field_headers(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.get_plugin), "__call__") as call: - client.get_plugin(request=None) + # 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 = operations_pb2.DeleteOperationRequest() + request.name = "locations" - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + call.return_value = None + + client.delete_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = plugin_service.GetPluginRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_enable_plugin_empty_call_rest(): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +@pytest.mark.asyncio +async def test_delete_operation_field_headers_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.enable_plugin), "__call__") as call: - client.enable_plugin(request=None) + # 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 = operations_pb2.DeleteOperationRequest() + request.name = "locations" - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = plugin_service.EnablePluginRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_disable_plugin_empty_call_rest(): +def test_delete_operation_from_dict(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.disable_plugin), "__call__") as call: - client.disable_plugin(request=None) - - # Establish that the underlying stub method was called. + response = client.delete_operation( + request={ + "name": "locations", + } + ) call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = plugin_service.DisablePluginRequest() - assert args[0] == request_msg +@pytest.mark.asyncio +async def test_delete_operation_from_dict_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_create_plugin_empty_call_rest(): + +def test_cancel_operation(transport: str = "grpc"): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.create_plugin), "__call__") as call: - client.create_plugin(request=None) + # 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 = operations_pb2.CancelOperationRequest() - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.cancel_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = plugin_service.CreatePluginRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the response is the type that we expect. + assert response is None -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_list_plugins_empty_call_rest(): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +@pytest.mark.asyncio +async def test_cancel_operation_async(transport: str = "grpc_asyncio"): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.list_plugins), "__call__") as call: - client.list_plugins(request=None) + # 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 = operations_pb2.CancelOperationRequest() - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.cancel_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = plugin_service.ListPluginsRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the response is the type that we expect. + assert response is None -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_delete_plugin_empty_call_rest(): +def test_cancel_operation_field_headers(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.delete_plugin), "__call__") as call: - client.delete_plugin(request=None) + # 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 = operations_pb2.CancelOperationRequest() + request.name = "locations" - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + call.return_value = None + + client.cancel_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = plugin_service.DeletePluginRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_create_plugin_instance_empty_call_rest(): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +@pytest.mark.asyncio +async def test_cancel_operation_field_headers_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.create_plugin_instance), "__call__" - ) as call: - client.create_plugin_instance(request=None) + # 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 = operations_pb2.CancelOperationRequest() + request.name = "locations" - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.cancel_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = plugin_service.CreatePluginInstanceRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_execute_plugin_instance_action_empty_call_rest(): +def test_cancel_operation_from_dict(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.execute_plugin_instance_action), "__call__" - ) as call: - client.execute_plugin_instance_action(request=None) - - # Establish that the underlying stub method was called. + response = client.cancel_operation( + request={ + "name": "locations", + } + ) call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = plugin_service.ExecutePluginInstanceActionRequest() - assert args[0] == request_msg +@pytest.mark.asyncio +async def test_cancel_operation_from_dict_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.cancel_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_get_plugin_instance_empty_call_rest(): + +def test_get_operation(transport: str = "grpc"): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.get_plugin_instance), "__call__" - ) as call: - client.get_plugin_instance(request=None) + # 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 = operations_pb2.GetOperationRequest() - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation() + response = client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = plugin_service.GetPluginInstanceRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_list_plugin_instances_empty_call_rest(): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +@pytest.mark.asyncio +async def test_get_operation_async(transport: str = "grpc_asyncio"): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.list_plugin_instances), "__call__" - ) as call: - client.list_plugin_instances(request=None) + # 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 = operations_pb2.GetOperationRequest() - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + response = await client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = plugin_service.ListPluginInstancesRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_enable_plugin_instance_action_empty_call_rest(): +def test_get_operation_field_headers(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.enable_plugin_instance_action), "__call__" - ) as call: - client.enable_plugin_instance_action(request=None) + # 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 = operations_pb2.GetOperationRequest() + request.name = "locations" - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + call.return_value = operations_pb2.Operation() + + client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = plugin_service.EnablePluginInstanceActionRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_disable_plugin_instance_action_empty_call_rest(): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +@pytest.mark.asyncio +async def test_get_operation_field_headers_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.disable_plugin_instance_action), "__call__" - ) as call: - client.disable_plugin_instance_action(request=None) + # 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 = operations_pb2.GetOperationRequest() + request.name = "locations" - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + await client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = plugin_service.DisablePluginInstanceActionRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_update_plugin_instance_empty_call_rest(): +def test_get_operation_from_dict(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation() - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.update_plugin_instance), "__call__" - ) as call: - client.update_plugin_instance(request=None) + response = client.get_operation( + request={ + "name": "locations", + } + ) + call.assert_called() - # Establish that the underlying stub method was called. + +@pytest.mark.asyncio +async def test_get_operation_from_dict_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + response = await client.get_operation( + request={ + "name": "locations", + } + ) call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = plugin_service.UpdatePluginInstanceRequest() - - assert args[0] == request_msg -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_delete_plugin_instance_empty_call_rest(): +def test_list_operations(transport: str = "grpc"): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.delete_plugin_instance), "__call__" - ) as call: - client.delete_plugin_instance(request=None) + # 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 = operations_pb2.ListOperationsRequest() - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.ListOperationsResponse() + response = client.list_operations(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = plugin_service.DeletePluginInstanceRequest() - - assert args[0] == request_msg + assert args[0] == request + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) -def test_api_hub_plugin_rest_lro_client(): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - transport = client.transport - # Ensure that we have an api-core operations client. - assert isinstance( - transport.operations_client, - operations_v1.AbstractOperationsClient, +@pytest.mark.asyncio +async def test_list_operations_async(transport: str = "grpc_asyncio"): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # Ensure that subsequent calls to the property send the exact same object. - assert transport.operations_client is transport.operations_client - + # 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 = operations_pb2.ListOperationsRequest() -def test_api_hub_plugin_base_transport_error(): - # Passing both a credentials object and credentials_file should raise an error - with pytest.raises(core_exceptions.DuplicateCredentialArgs): - transport = transports.ApiHubPluginTransport( - credentials=ga_credentials.AnonymousCredentials(), - credentials_file="credentials.json", + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() ) + response = await client.list_operations(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 response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) -def test_api_hub_plugin_base_transport(): - # Instantiate the base transport. - with mock.patch( - "google.cloud.apihub_v1.services.api_hub_plugin.transports.ApiHubPluginTransport.__init__" - ) as Transport: - Transport.return_value = None - transport = transports.ApiHubPluginTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - # Every method on the transport should just blindly - # raise NotImplementedError. - methods = ( - "get_plugin", - "enable_plugin", - "disable_plugin", - "create_plugin", - "list_plugins", - "delete_plugin", - "create_plugin_instance", - "execute_plugin_instance_action", - "get_plugin_instance", - "list_plugin_instances", - "enable_plugin_instance_action", - "disable_plugin_instance_action", - "update_plugin_instance", - "delete_plugin_instance", - "get_location", - "list_locations", - "get_operation", - "cancel_operation", - "delete_operation", - "list_operations", +def test_list_operations_field_headers(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), ) - for method in methods: - with pytest.raises(NotImplementedError): - getattr(transport, method)(request=object()) - with pytest.raises(NotImplementedError): - transport.close() - - # Additionally, the LRO client (a property) should - # also raise NotImplementedError - with pytest.raises(NotImplementedError): - transport.operations_client + # 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 = operations_pb2.ListOperationsRequest() + request.name = "locations" - # Catch all for all remaining methods and properties - remainder = [ - "kind", - ] - for r in remainder: - with pytest.raises(NotImplementedError): - getattr(transport, r)() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + call.return_value = operations_pb2.ListOperationsResponse() + client.list_operations(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 -def test_api_hub_plugin_base_transport_with_credentials_file(): - # Instantiate the base transport with a credentials file - with mock.patch.object( - google.auth, "load_credentials_from_file", autospec=True - ) as load_creds, mock.patch( - "google.cloud.apihub_v1.services.api_hub_plugin.transports.ApiHubPluginTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None - load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) - transport = transports.ApiHubPluginTransport( - credentials_file="credentials.json", - quota_project_id="octopus", - ) - load_creds.assert_called_once_with( - "credentials.json", - scopes=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id="octopus", - ) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -def test_api_hub_plugin_base_transport_with_adc(): - # Test the default credentials are used if credentials and credentials_file are None. - with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch( - "google.cloud.apihub_v1.services.api_hub_plugin.transports.ApiHubPluginTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport = transports.ApiHubPluginTransport() - adc.assert_called_once() +@pytest.mark.asyncio +async def test_list_operations_field_headers_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) + # 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 = operations_pb2.ListOperationsRequest() + request.name = "locations" -def test_api_hub_plugin_auth_adc(): - # If no credentials are provided, we should use ADC credentials. - with mock.patch.object(google.auth, "default", autospec=True) as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - ApiHubPluginClient() - adc.assert_called_once_with( - scopes=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id=None, + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() ) + await client.list_operations(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 - -def test_api_hub_plugin_http_transport_client_cert_source_for_mtls(): - cred = ga_credentials.AnonymousCredentials() - with mock.patch( - "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" - ) as mock_configure_mtls_channel: - transports.ApiHubPluginRestTransport( - credentials=cred, client_cert_source_for_mtls=client_cert_source_callback - ) - mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_api_hub_plugin_host_no_port(transport_name): +def test_list_operations_from_dict(): client = ApiHubPluginClient( credentials=ga_credentials.AnonymousCredentials(), - client_options=client_options.ClientOptions( - api_endpoint="apihub.googleapis.com" - ), - transport=transport_name, - ) - assert client.transport._host == ( - "apihub.googleapis.com:443" - if transport_name in ["grpc", "grpc_asyncio"] - else "https://apihub.googleapis.com" ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.ListOperationsResponse() + response = client.list_operations( + request={ + "name": "locations", + } + ) + call.assert_called() -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_api_hub_plugin_host_with_port(transport_name): - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - client_options=client_options.ClientOptions( - api_endpoint="apihub.googleapis.com:8000" - ), - transport=transport_name, - ) - assert client.transport._host == ( - "apihub.googleapis.com:8000" - if transport_name in ["grpc", "grpc_asyncio"] - else "https://apihub.googleapis.com:8000" + +@pytest.mark.asyncio +async def test_list_operations_from_dict_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() + ) + response = await client.list_operations( + request={ + "name": "locations", + } + ) + call.assert_called() -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_api_hub_plugin_client_transport_session_collision(transport_name): - creds1 = ga_credentials.AnonymousCredentials() - creds2 = ga_credentials.AnonymousCredentials() - client1 = ApiHubPluginClient( - credentials=creds1, - transport=transport_name, - ) - client2 = ApiHubPluginClient( - credentials=creds2, - transport=transport_name, - ) - session1 = client1.transport.get_plugin._session - session2 = client2.transport.get_plugin._session - assert session1 != session2 - session1 = client1.transport.enable_plugin._session - session2 = client2.transport.enable_plugin._session - assert session1 != session2 - session1 = client1.transport.disable_plugin._session - session2 = client2.transport.disable_plugin._session - assert session1 != session2 - session1 = client1.transport.create_plugin._session - session2 = client2.transport.create_plugin._session - assert session1 != session2 - session1 = client1.transport.list_plugins._session - session2 = client2.transport.list_plugins._session - assert session1 != session2 - session1 = client1.transport.delete_plugin._session - session2 = client2.transport.delete_plugin._session - assert session1 != session2 - session1 = client1.transport.create_plugin_instance._session - session2 = client2.transport.create_plugin_instance._session - assert session1 != session2 - session1 = client1.transport.execute_plugin_instance_action._session - session2 = client2.transport.execute_plugin_instance_action._session - assert session1 != session2 - session1 = client1.transport.get_plugin_instance._session - session2 = client2.transport.get_plugin_instance._session - assert session1 != session2 - session1 = client1.transport.list_plugin_instances._session - session2 = client2.transport.list_plugin_instances._session - assert session1 != session2 - session1 = client1.transport.enable_plugin_instance_action._session - session2 = client2.transport.enable_plugin_instance_action._session - assert session1 != session2 - session1 = client1.transport.disable_plugin_instance_action._session - session2 = client2.transport.disable_plugin_instance_action._session - assert session1 != session2 - session1 = client1.transport.update_plugin_instance._session - session2 = client2.transport.update_plugin_instance._session - assert session1 != session2 - session1 = client1.transport.delete_plugin_instance._session - session2 = client2.transport.delete_plugin_instance._session - assert session1 != session2 +def test_list_locations(transport: str = "grpc"): + client = ApiHubPluginClient( + credentials=ga_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 = locations_pb2.ListLocationsRequest() -def test_attribute_path(): - project = "squid" - location = "clam" - attribute = "whelk" - expected = "projects/{project}/locations/{location}/attributes/{attribute}".format( - project=project, - location=location, - attribute=attribute, + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.ListLocationsResponse() + response = client.list_locations(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 response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + +@pytest.mark.asyncio +async def test_list_locations_async(transport: str = "grpc_asyncio"): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - actual = ApiHubPluginClient.attribute_path(project, location, attribute) - assert expected == actual + # 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 = locations_pb2.ListLocationsRequest() -def test_parse_attribute_path(): - expected = { - "project": "octopus", - "location": "oyster", - "attribute": "nudibranch", - } - path = ApiHubPluginClient.attribute_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + response = await client.list_locations(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 - # Check that the path construction is reversible. - actual = ApiHubPluginClient.parse_attribute_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) -def test_plugin_path(): - project = "cuttlefish" - location = "mussel" - plugin = "winkle" - expected = "projects/{project}/locations/{location}/plugins/{plugin}".format( - project=project, - location=location, - plugin=plugin, +def test_list_locations_field_headers(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), ) - actual = ApiHubPluginClient.plugin_path(project, location, plugin) - assert expected == actual + # 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 = locations_pb2.ListLocationsRequest() + request.name = "locations" -def test_parse_plugin_path(): - expected = { - "project": "nautilus", - "location": "scallop", - "plugin": "abalone", - } - path = ApiHubPluginClient.plugin_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + call.return_value = locations_pb2.ListLocationsResponse() - # Check that the path construction is reversible. - actual = ApiHubPluginClient.parse_plugin_path(path) - assert expected == actual + client.list_locations(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", + "name=locations", + ) in kw["metadata"] -def test_plugin_instance_path(): - project = "squid" - location = "clam" - plugin = "whelk" - instance = "octopus" - expected = "projects/{project}/locations/{location}/plugins/{plugin}/instances/{instance}".format( - project=project, - location=location, - plugin=plugin, - instance=instance, - ) - actual = ApiHubPluginClient.plugin_instance_path( - project, location, plugin, instance + +@pytest.mark.asyncio +async def test_list_locations_field_headers_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), ) - assert expected == actual + # 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 = locations_pb2.ListLocationsRequest() + request.name = "locations" -def test_parse_plugin_instance_path(): - expected = { - "project": "oyster", - "location": "nudibranch", - "plugin": "cuttlefish", - "instance": "mussel", - } - path = ApiHubPluginClient.plugin_instance_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + await client.list_locations(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 - # Check that the path construction is reversible. - actual = ApiHubPluginClient.parse_plugin_instance_path(path) - assert expected == actual + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -def test_service_account_path(): - project = "winkle" - service_account = "nautilus" - expected = "projects/{project}/serviceAccounts/{service_account}".format( - project=project, - service_account=service_account, +def test_list_locations_from_dict(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), ) - actual = ApiHubPluginClient.service_account_path(project, service_account) - assert expected == actual + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.ListLocationsResponse() + response = client.list_locations( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_parse_service_account_path(): - expected = { - "project": "scallop", - "service_account": "abalone", - } - path = ApiHubPluginClient.service_account_path(**expected) - # Check that the path construction is reversible. - actual = ApiHubPluginClient.parse_service_account_path(path) - assert expected == actual +@pytest.mark.asyncio +async def test_list_locations_from_dict_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + response = await client.list_locations( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_common_billing_account_path(): - billing_account = "squid" - expected = "billingAccounts/{billing_account}".format( - billing_account=billing_account, +def test_get_location(transport: str = "grpc"): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - actual = ApiHubPluginClient.common_billing_account_path(billing_account) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() -def test_parse_common_billing_account_path(): - expected = { - "billing_account": "clam", - } - path = ApiHubPluginClient.common_billing_account_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.Location() + response = client.get_location(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 - # Check that the path construction is reversible. - actual = ApiHubPluginClient.parse_common_billing_account_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) -def test_common_folder_path(): - folder = "whelk" - expected = "folders/{folder}".format( - folder=folder, +@pytest.mark.asyncio +async def test_get_location_async(transport: str = "grpc_asyncio"): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - actual = ApiHubPluginClient.common_folder_path(folder) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() -def test_parse_common_folder_path(): - expected = { - "folder": "octopus", - } - path = ApiHubPluginClient.common_folder_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + response = await client.get_location(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 - # Check that the path construction is reversible. - actual = ApiHubPluginClient.parse_common_folder_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) -def test_common_organization_path(): - organization = "oyster" - expected = "organizations/{organization}".format( - organization=organization, - ) - actual = ApiHubPluginClient.common_organization_path(organization) - assert expected == actual +def test_get_location_field_headers(): + client = ApiHubPluginClient(credentials=ga_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 = locations_pb2.GetLocationRequest() + request.name = "locations/abc" -def test_parse_common_organization_path(): - expected = { - "organization": "nudibranch", - } - path = ApiHubPluginClient.common_organization_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + call.return_value = locations_pb2.Location() - # Check that the path construction is reversible. - actual = ApiHubPluginClient.parse_common_organization_path(path) - assert expected == actual + client.get_location(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", + "name=locations/abc", + ) in kw["metadata"] -def test_common_project_path(): - project = "cuttlefish" - expected = "projects/{project}".format( - project=project, - ) - actual = ApiHubPluginClient.common_project_path(project) - assert expected == actual +@pytest.mark.asyncio +async def test_get_location_field_headers_async(): + client = ApiHubPluginAsyncClient(credentials=async_anonymous_credentials()) -def test_parse_common_project_path(): - expected = { - "project": "mussel", - } - path = ApiHubPluginClient.common_project_path(**expected) + # 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 = locations_pb2.GetLocationRequest() + request.name = "locations/abc" - # Check that the path construction is reversible. - actual = ApiHubPluginClient.parse_common_project_path(path) - assert expected == actual + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + await client.get_location(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", + "name=locations/abc", + ) in kw["metadata"] -def test_common_location_path(): - project = "winkle" - location = "nautilus" - expected = "projects/{project}/locations/{location}".format( - project=project, - location=location, - ) - actual = ApiHubPluginClient.common_location_path(project, location) - assert expected == actual +def test_get_location_from_dict(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.Location() -def test_parse_common_location_path(): - expected = { - "project": "scallop", - "location": "abalone", - } - path = ApiHubPluginClient.common_location_path(**expected) + response = client.get_location( + request={ + "name": "locations/abc", + } + ) + call.assert_called() - # Check that the path construction is reversible. - actual = ApiHubPluginClient.parse_common_location_path(path) - assert expected == actual +@pytest.mark.asyncio +async def test_get_location_from_dict_async(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + response = await client.get_location( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_client_with_default_client_info(): - client_info = gapic_v1.client_info.ClientInfo() +def test_transport_close_grpc(): + client = ApiHubPluginClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) with mock.patch.object( - transports.ApiHubPluginTransport, "_prep_wrapped_messages" - ) as prep: - client = ApiHubPluginClient( - credentials=ga_credentials.AnonymousCredentials(), - client_info=client_info, - ) - prep.assert_called_once_with(client_info) + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + +@pytest.mark.asyncio +async def test_transport_close_grpc_asyncio(): + client = ApiHubPluginAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) with mock.patch.object( - transports.ApiHubPluginTransport, "_prep_wrapped_messages" - ) as prep: - transport_class = ApiHubPluginClient.get_transport_class() - transport = transport_class( - credentials=ga_credentials.AnonymousCredentials(), - client_info=client_info, - ) - prep.assert_called_once_with(client_info) + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + async with client: + close.assert_not_called() + close.assert_called_once() def test_transport_close_rest(): @@ -7198,6 +14619,7 @@ def test_transport_close_rest(): def test_client_ctx(): transports = [ "rest", + "grpc", ] for transport in transports: client = ApiHubPluginClient( @@ -7214,7 +14636,8 @@ def test_client_ctx(): @pytest.mark.parametrize( "client_class,transport_class", [ - (ApiHubPluginClient, transports.ApiHubPluginRestTransport), + (ApiHubPluginClient, transports.ApiHubPluginGrpcTransport), + (ApiHubPluginAsyncClient, transports.ApiHubPluginGrpcAsyncIOTransport), ], ) def test_api_key_credentials(client_class, transport_class): diff --git a/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_host_project_registration_service.py b/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_host_project_registration_service.py index c7d4b997ff13..434f0c6a7e20 100644 --- a/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_host_project_registration_service.py +++ b/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_host_project_registration_service.py @@ -56,6 +56,7 @@ from google.protobuf import timestamp_pb2 # type: ignore from google.cloud.apihub_v1.services.host_project_registration_service import ( + HostProjectRegistrationServiceAsyncClient, HostProjectRegistrationServiceClient, pagers, transports, @@ -259,6 +260,11 @@ def test__get_client_cert_source(): "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(HostProjectRegistrationServiceClient), ) +@mock.patch.object( + HostProjectRegistrationServiceAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(HostProjectRegistrationServiceAsyncClient), +) def test__get_api_endpoint(): api_override = "foo.com" mock_client_cert_source = mock.Mock() @@ -400,6 +406,8 @@ def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code): @pytest.mark.parametrize( "client_class,transport_name", [ + (HostProjectRegistrationServiceClient, "grpc"), + (HostProjectRegistrationServiceAsyncClient, "grpc_asyncio"), (HostProjectRegistrationServiceClient, "rest"), ], ) @@ -426,6 +434,8 @@ def test_host_project_registration_service_client_from_service_account_info( @pytest.mark.parametrize( "transport_class,transport_name", [ + (transports.HostProjectRegistrationServiceGrpcTransport, "grpc"), + (transports.HostProjectRegistrationServiceGrpcAsyncIOTransport, "grpc_asyncio"), (transports.HostProjectRegistrationServiceRestTransport, "rest"), ], ) @@ -450,6 +460,8 @@ def test_host_project_registration_service_client_service_account_always_use_jwt @pytest.mark.parametrize( "client_class,transport_name", [ + (HostProjectRegistrationServiceClient, "grpc"), + (HostProjectRegistrationServiceAsyncClient, "grpc_asyncio"), (HostProjectRegistrationServiceClient, "rest"), ], ) @@ -483,17 +495,28 @@ def test_host_project_registration_service_client_from_service_account_file( def test_host_project_registration_service_client_get_transport_class(): transport = HostProjectRegistrationServiceClient.get_transport_class() available_transports = [ + transports.HostProjectRegistrationServiceGrpcTransport, transports.HostProjectRegistrationServiceRestTransport, ] assert transport in available_transports - transport = HostProjectRegistrationServiceClient.get_transport_class("rest") - assert transport == transports.HostProjectRegistrationServiceRestTransport + transport = HostProjectRegistrationServiceClient.get_transport_class("grpc") + assert transport == transports.HostProjectRegistrationServiceGrpcTransport @pytest.mark.parametrize( "client_class,transport_class,transport_name", [ + ( + HostProjectRegistrationServiceClient, + transports.HostProjectRegistrationServiceGrpcTransport, + "grpc", + ), + ( + HostProjectRegistrationServiceAsyncClient, + transports.HostProjectRegistrationServiceGrpcAsyncIOTransport, + "grpc_asyncio", + ), ( HostProjectRegistrationServiceClient, transports.HostProjectRegistrationServiceRestTransport, @@ -506,6 +529,11 @@ def test_host_project_registration_service_client_get_transport_class(): "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(HostProjectRegistrationServiceClient), ) +@mock.patch.object( + HostProjectRegistrationServiceAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(HostProjectRegistrationServiceAsyncClient), +) def test_host_project_registration_service_client_client_options( client_class, transport_class, transport_name ): @@ -643,6 +671,30 @@ def test_host_project_registration_service_client_client_options( @pytest.mark.parametrize( "client_class,transport_class,transport_name,use_client_cert_env", [ + ( + HostProjectRegistrationServiceClient, + transports.HostProjectRegistrationServiceGrpcTransport, + "grpc", + "true", + ), + ( + HostProjectRegistrationServiceAsyncClient, + transports.HostProjectRegistrationServiceGrpcAsyncIOTransport, + "grpc_asyncio", + "true", + ), + ( + HostProjectRegistrationServiceClient, + transports.HostProjectRegistrationServiceGrpcTransport, + "grpc", + "false", + ), + ( + HostProjectRegistrationServiceAsyncClient, + transports.HostProjectRegistrationServiceGrpcAsyncIOTransport, + "grpc_asyncio", + "false", + ), ( HostProjectRegistrationServiceClient, transports.HostProjectRegistrationServiceRestTransport, @@ -662,6 +714,11 @@ def test_host_project_registration_service_client_client_options( "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(HostProjectRegistrationServiceClient), ) +@mock.patch.object( + HostProjectRegistrationServiceAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(HostProjectRegistrationServiceAsyncClient), +) @mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) def test_host_project_registration_service_client_mtls_env_auto( client_class, transport_class, transport_name, use_client_cert_env @@ -765,12 +822,20 @@ def test_host_project_registration_service_client_mtls_env_auto( ) -@pytest.mark.parametrize("client_class", [HostProjectRegistrationServiceClient]) +@pytest.mark.parametrize( + "client_class", + [HostProjectRegistrationServiceClient, HostProjectRegistrationServiceAsyncClient], +) @mock.patch.object( HostProjectRegistrationServiceClient, "DEFAULT_ENDPOINT", modify_default_endpoint(HostProjectRegistrationServiceClient), ) +@mock.patch.object( + HostProjectRegistrationServiceAsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(HostProjectRegistrationServiceAsyncClient), +) def test_host_project_registration_service_client_get_mtls_endpoint_and_cert_source( client_class, ): @@ -864,12 +929,20 @@ def test_host_project_registration_service_client_get_mtls_endpoint_and_cert_sou ) -@pytest.mark.parametrize("client_class", [HostProjectRegistrationServiceClient]) +@pytest.mark.parametrize( + "client_class", + [HostProjectRegistrationServiceClient, HostProjectRegistrationServiceAsyncClient], +) @mock.patch.object( HostProjectRegistrationServiceClient, "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(HostProjectRegistrationServiceClient), ) +@mock.patch.object( + HostProjectRegistrationServiceAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(HostProjectRegistrationServiceAsyncClient), +) def test_host_project_registration_service_client_client_api_endpoint(client_class): mock_client_cert_source = client_cert_source_callback api_override = "foo.com" @@ -950,6 +1023,16 @@ def test_host_project_registration_service_client_client_api_endpoint(client_cla @pytest.mark.parametrize( "client_class,transport_class,transport_name", [ + ( + HostProjectRegistrationServiceClient, + transports.HostProjectRegistrationServiceGrpcTransport, + "grpc", + ), + ( + HostProjectRegistrationServiceAsyncClient, + transports.HostProjectRegistrationServiceGrpcAsyncIOTransport, + "grpc_asyncio", + ), ( HostProjectRegistrationServiceClient, transports.HostProjectRegistrationServiceRestTransport, @@ -985,6 +1068,18 @@ def test_host_project_registration_service_client_client_options_scopes( @pytest.mark.parametrize( "client_class,transport_class,transport_name,grpc_helpers", [ + ( + HostProjectRegistrationServiceClient, + transports.HostProjectRegistrationServiceGrpcTransport, + "grpc", + grpc_helpers, + ), + ( + HostProjectRegistrationServiceAsyncClient, + transports.HostProjectRegistrationServiceGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), ( HostProjectRegistrationServiceClient, transports.HostProjectRegistrationServiceRestTransport, @@ -1017,13 +1112,181 @@ def test_host_project_registration_service_client_client_options_credentials_fil ) -def test_create_host_project_registration_rest_use_cached_wrapped_rpc(): +def test_host_project_registration_service_client_client_options_from_dict(): + with mock.patch( + "google.cloud.apihub_v1.services.host_project_registration_service.transports.HostProjectRegistrationServiceGrpcTransport.__init__" + ) as grpc_transport: + grpc_transport.return_value = None + client = HostProjectRegistrationServiceClient( + client_options={"api_endpoint": "squid.clam.whelk"} + ) + grpc_transport.assert_called_once_with( + credentials=None, + credentials_file=None, + host="squid.clam.whelk", + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name,grpc_helpers", + [ + ( + HostProjectRegistrationServiceClient, + transports.HostProjectRegistrationServiceGrpcTransport, + "grpc", + grpc_helpers, + ), + ( + HostProjectRegistrationServiceAsyncClient, + transports.HostProjectRegistrationServiceGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), + ], +) +def test_host_project_registration_service_client_create_channel_credentials_file( + client_class, transport_class, transport_name, grpc_helpers +): + # Check the case credentials file is provided. + options = client_options.ClientOptions(credentials_file="credentials.json") + + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options, transport=transport_name) + patched.assert_called_once_with( + credentials=None, + credentials_file="credentials.json", + host=client._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE + ), + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + # test that the credentials from file are saved and used as the credentials. + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel" + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + file_creds = ga_credentials.AnonymousCredentials() + load_creds.return_value = (file_creds, None) + adc.return_value = (creds, None) + client = client_class(client_options=options, transport=transport_name) + create_channel.assert_called_with( + "apihub.googleapis.com:443", + credentials=file_creds, + credentials_file=None, + quota_project_id=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + scopes=None, + default_host="apihub.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + +@pytest.mark.parametrize( + "request_type", + [ + host_project_registration_service.CreateHostProjectRegistrationRequest, + dict, + ], +) +def test_create_host_project_registration(request_type, transport: str = "grpc"): + client = HostProjectRegistrationServiceClient( + credentials=ga_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.create_host_project_registration), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = host_project_registration_service.HostProjectRegistration( + name="name_value", + gcp_project="gcp_project_value", + ) + response = client.create_host_project_registration(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = ( + host_project_registration_service.CreateHostProjectRegistrationRequest() + ) + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance( + response, host_project_registration_service.HostProjectRegistration + ) + assert response.name == "name_value" + assert response.gcp_project == "gcp_project_value" + + +def test_create_host_project_registration_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = host_project_registration_service.CreateHostProjectRegistrationRequest( + parent="parent_value", + host_project_registration_id="host_project_registration_id_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_host_project_registration), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.create_host_project_registration(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[ + 0 + ] == host_project_registration_service.CreateHostProjectRegistrationRequest( + parent="parent_value", + host_project_registration_id="host_project_registration_id_value", + ) + + +def test_create_host_project_registration_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1044,7 +1307,6 @@ def test_create_host_project_registration_rest_use_cached_wrapped_rpc(): client._transport._wrapped_methods[ client._transport.create_host_project_registration ] = mock_rpc - request = {} client.create_host_project_registration(request) @@ -1058,186 +1320,270 @@ def test_create_host_project_registration_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_create_host_project_registration_rest_required_fields( - request_type=host_project_registration_service.CreateHostProjectRegistrationRequest, +@pytest.mark.asyncio +async def test_create_host_project_registration_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.HostProjectRegistrationServiceRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request_init["host_project_registration_id"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped - assert "hostProjectRegistrationId" not in jsonified_request + # Ensure method has been cached + assert ( + client._client._transport.create_host_project_registration + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_host_project_registration._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.create_host_project_registration + ] = mock_rpc - # verify required fields with default values are now present - assert "hostProjectRegistrationId" in jsonified_request - assert ( - jsonified_request["hostProjectRegistrationId"] - == request_init["host_project_registration_id"] + request = {} + await client.create_host_project_registration(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.create_host_project_registration(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_create_host_project_registration_async( + transport: str = "grpc_asyncio", + request_type=host_project_registration_service.CreateHostProjectRegistrationRequest, +): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - jsonified_request["parent"] = "parent_value" - jsonified_request[ - "hostProjectRegistrationId" - ] = "host_project_registration_id_value" + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_host_project_registration._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("host_project_registration_id",)) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_host_project_registration), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + host_project_registration_service.HostProjectRegistration( + name="name_value", + gcp_project="gcp_project_value", + ) + ) + response = await client.create_host_project_registration(request) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" - assert "hostProjectRegistrationId" in jsonified_request - assert ( - jsonified_request["hostProjectRegistrationId"] - == "host_project_registration_id_value" + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = ( + host_project_registration_service.CreateHostProjectRegistrationRequest() + ) + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance( + response, host_project_registration_service.HostProjectRegistration ) + assert response.name == "name_value" + assert response.gcp_project == "gcp_project_value" + + +@pytest.mark.asyncio +async def test_create_host_project_registration_async_from_dict(): + await test_create_host_project_registration_async(request_type=dict) + +def test_create_host_project_registration_field_headers(): client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = host_project_registration_service.HostProjectRegistration() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # 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 = host_project_registration_service.CreateHostProjectRegistrationRequest() - response_value = Response() - response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = host_project_registration_service.HostProjectRegistration.pb( - return_value - ) - json_return_value = json_format.MessageToJson(return_value) + request.parent = "parent_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_host_project_registration), "__call__" + ) as call: + call.return_value = host_project_registration_service.HostProjectRegistration() + client.create_host_project_registration(request) - response = client.create_host_project_registration(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 - expected_params = [ - ( - "hostProjectRegistrationId", - "", - ), - ("$alt", "json;enum-encoding=int"), - ] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] -def test_create_host_project_registration_rest_unset_required_fields(): - transport = transports.HostProjectRegistrationServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_create_host_project_registration_field_headers_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = ( - transport.create_host_project_registration._get_unset_required_fields({}) - ) - assert set(unset_fields) == ( - set(("hostProjectRegistrationId",)) - & set( - ( - "parent", - "hostProjectRegistrationId", - "hostProjectRegistration", - ) + # 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 = host_project_registration_service.CreateHostProjectRegistrationRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_host_project_registration), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + host_project_registration_service.HostProjectRegistration() ) - ) + await client.create_host_project_registration(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request -def test_create_host_project_registration_rest_flattened(): + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +def test_create_host_project_registration_flattened(): client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = host_project_registration_service.HostProjectRegistration() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_host_project_registration), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = host_project_registration_service.HostProjectRegistration() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.create_host_project_registration( + parent="parent_value", + host_project_registration=host_project_registration_service.HostProjectRegistration( + name="name_value" + ), + host_project_registration_id="host_project_registration_id_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].host_project_registration + mock_val = host_project_registration_service.HostProjectRegistration( + name="name_value" + ) + assert arg == mock_val + arg = args[0].host_project_registration_id + mock_val = "host_project_registration_id_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_create_host_project_registration_flattened_error(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_host_project_registration( + host_project_registration_service.CreateHostProjectRegistrationRequest(), parent="parent_value", host_project_registration=host_project_registration_service.HostProjectRegistration( name="name_value" ), host_project_registration_id="host_project_registration_id_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = host_project_registration_service.HostProjectRegistration.pb( - return_value - ) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_host_project_registration(**mock_args) +@pytest.mark.asyncio +async def test_create_host_project_registration_flattened_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_host_project_registration), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = host_project_registration_service.HostProjectRegistration() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + host_project_registration_service.HostProjectRegistration() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.create_host_project_registration( + parent="parent_value", + host_project_registration=host_project_registration_service.HostProjectRegistration( + name="name_value" + ), + host_project_registration_id="host_project_registration_id_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/hostProjectRegistrations" - % client.transport._host, - args[1], + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].host_project_registration + mock_val = host_project_registration_service.HostProjectRegistration( + name="name_value" ) + assert arg == mock_val + arg = args[0].host_project_registration_id + mock_val = "host_project_registration_id_value" + assert arg == mock_val -def test_create_host_project_registration_rest_flattened_error(transport: str = "rest"): - client = HostProjectRegistrationServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_create_host_project_registration_flattened_error_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.create_host_project_registration( + await client.create_host_project_registration( host_project_registration_service.CreateHostProjectRegistrationRequest(), parent="parent_value", host_project_registration=host_project_registration_service.HostProjectRegistration( @@ -1247,13 +1593,87 @@ def test_create_host_project_registration_rest_flattened_error(transport: str = ) -def test_get_host_project_registration_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + host_project_registration_service.GetHostProjectRegistrationRequest, + dict, + ], +) +def test_get_host_project_registration(request_type, transport: str = "grpc"): + client = HostProjectRegistrationServiceClient( + credentials=ga_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.get_host_project_registration), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = host_project_registration_service.HostProjectRegistration( + name="name_value", + gcp_project="gcp_project_value", + ) + response = client.get_host_project_registration(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = host_project_registration_service.GetHostProjectRegistrationRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance( + response, host_project_registration_service.HostProjectRegistration + ) + assert response.name == "name_value" + assert response.gcp_project == "gcp_project_value" + + +def test_get_host_project_registration_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = host_project_registration_service.GetHostProjectRegistrationRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_host_project_registration), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_host_project_registration(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[ + 0 + ] == host_project_registration_service.GetHostProjectRegistrationRequest( + name="name_value", + ) + + +def test_get_host_project_registration_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1274,7 +1694,6 @@ def test_get_host_project_registration_rest_use_cached_wrapped_rpc(): client._transport._wrapped_methods[ client._transport.get_host_project_registration ] = mock_rpc - request = {} client.get_host_project_registration(request) @@ -1288,163 +1707,332 @@ def test_get_host_project_registration_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_get_host_project_registration_rest_required_fields( - request_type=host_project_registration_service.GetHostProjectRegistrationRequest, +@pytest.mark.asyncio +async def test_get_host_project_registration_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.HostProjectRegistrationServiceRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.get_host_project_registration + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_host_project_registration._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.get_host_project_registration + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.get_host_project_registration(request) - jsonified_request["name"] = "name_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_host_project_registration._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + await client.get_host_project_registration(request) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - client = HostProjectRegistrationServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + +@pytest.mark.asyncio +async def test_get_host_project_registration_async( + transport: str = "grpc_asyncio", + request_type=host_project_registration_service.GetHostProjectRegistrationRequest, +): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = host_project_registration_service.HostProjectRegistration() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - - response_value = Response() - response_value.status_code = 200 + # 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() - # Convert return value to protobuf type - return_value = host_project_registration_service.HostProjectRegistration.pb( - return_value + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_host_project_registration), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + host_project_registration_service.HostProjectRegistration( + name="name_value", + gcp_project="gcp_project_value", ) - json_return_value = json_format.MessageToJson(return_value) + ) + response = await client.get_host_project_registration(request) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = host_project_registration_service.GetHostProjectRegistrationRequest() + assert args[0] == request - response = client.get_host_project_registration(request) + # Establish that the response is the type that we expect. + assert isinstance( + response, host_project_registration_service.HostProjectRegistration + ) + assert response.name == "name_value" + assert response.gcp_project == "gcp_project_value" - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_get_host_project_registration_async_from_dict(): + await test_get_host_project_registration_async(request_type=dict) -def test_get_host_project_registration_rest_unset_required_fields(): - transport = transports.HostProjectRegistrationServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials + +def test_get_host_project_registration_field_headers(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = transport.get_host_project_registration._get_unset_required_fields( - {} + # 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 = host_project_registration_service.GetHostProjectRegistrationRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_host_project_registration), "__call__" + ) as call: + call.return_value = host_project_registration_service.HostProjectRegistration() + client.get_host_project_registration(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", + "name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_get_host_project_registration_field_headers_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), ) - assert set(unset_fields) == (set(()) & set(("name",))) + # 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 = host_project_registration_service.GetHostProjectRegistrationRequest() -def test_get_host_project_registration_rest_flattened(): + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_host_project_registration), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + host_project_registration_service.HostProjectRegistration() + ) + await client.get_host_project_registration(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", + "name=name_value", + ) in kw["metadata"] + + +def test_get_host_project_registration_flattened(): client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = host_project_registration_service.HostProjectRegistration() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_host_project_registration), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = host_project_registration_service.HostProjectRegistration() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_host_project_registration( + name="name_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/hostProjectRegistrations/sample3" - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_get_host_project_registration_flattened_error(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_host_project_registration( + host_project_registration_service.GetHostProjectRegistrationRequest(), name="name_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = host_project_registration_service.HostProjectRegistration.pb( - return_value - ) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_host_project_registration(**mock_args) +@pytest.mark.asyncio +async def test_get_host_project_registration_flattened_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_host_project_registration), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = host_project_registration_service.HostProjectRegistration() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + host_project_registration_service.HostProjectRegistration() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_host_project_registration( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/hostProjectRegistrations/*}" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_get_host_project_registration_rest_flattened_error(transport: str = "rest"): - client = HostProjectRegistrationServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_get_host_project_registration_flattened_error_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.get_host_project_registration( + await client.get_host_project_registration( host_project_registration_service.GetHostProjectRegistrationRequest(), name="name_value", ) -def test_list_host_project_registrations_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + host_project_registration_service.ListHostProjectRegistrationsRequest, + dict, + ], +) +def test_list_host_project_registrations(request_type, transport: str = "grpc"): + client = HostProjectRegistrationServiceClient( + credentials=ga_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.list_host_project_registrations), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = ( + host_project_registration_service.ListHostProjectRegistrationsResponse( + next_page_token="next_page_token_value", + ) + ) + response = client.list_host_project_registrations(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = ( + host_project_registration_service.ListHostProjectRegistrationsRequest() + ) + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListHostProjectRegistrationsPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_host_project_registrations_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = host_project_registration_service.ListHostProjectRegistrationsRequest( + parent="parent_value", + page_token="page_token_value", + filter="filter_value", + order_by="order_by_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_host_project_registrations), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.list_host_project_registrations(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[ + 0 + ] == host_project_registration_service.ListHostProjectRegistrationsRequest( + parent="parent_value", + page_token="page_token_value", + filter="filter_value", + order_by="order_by_value", + ) + + +def test_list_host_project_registrations_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1465,7 +2053,6 @@ def test_list_host_project_registrations_rest_use_cached_wrapped_rpc(): client._transport._wrapped_methods[ client._transport.list_host_project_registrations ] = mock_rpc - request = {} client.list_host_project_registrations(request) @@ -1479,191 +2066,261 @@ def test_list_host_project_registrations_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_list_host_project_registrations_rest_required_fields( - request_type=host_project_registration_service.ListHostProjectRegistrationsRequest, +@pytest.mark.asyncio +async def test_list_host_project_registrations_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.HostProjectRegistrationServiceRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.list_host_project_registrations + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_host_project_registrations._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.list_host_project_registrations + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.list_host_project_registrations(request) - jsonified_request["parent"] = "parent_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_host_project_registrations._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "filter", - "order_by", - "page_size", - "page_token", - ) - ) - jsonified_request.update(unset_fields) + await client.list_host_project_registrations(request) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - client = HostProjectRegistrationServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = ( - host_project_registration_service.ListHostProjectRegistrationsResponse() +@pytest.mark.asyncio +async def test_list_host_project_registrations_async( + transport: str = "grpc_asyncio", + request_type=host_project_registration_service.ListHostProjectRegistrationsRequest, +): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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() - # Convert return value to protobuf type - return_value = host_project_registration_service.ListHostProjectRegistrationsResponse.pb( - return_value + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_host_project_registrations), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + host_project_registration_service.ListHostProjectRegistrationsResponse( + next_page_token="next_page_token_value", ) - json_return_value = json_format.MessageToJson(return_value) + ) + response = await client.list_host_project_registrations(request) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = ( + host_project_registration_service.ListHostProjectRegistrationsRequest() + ) + assert args[0] == request - response = client.list_host_project_registrations(request) + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListHostProjectRegistrationsAsyncPager) + assert response.next_page_token == "next_page_token_value" - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_list_host_project_registrations_async_from_dict(): + await test_list_host_project_registrations_async(request_type=dict) -def test_list_host_project_registrations_rest_unset_required_fields(): - transport = transports.HostProjectRegistrationServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials - ) - unset_fields = transport.list_host_project_registrations._get_unset_required_fields( - {} +def test_list_host_project_registrations_field_headers(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), ) - assert set(unset_fields) == ( - set( - ( - "filter", - "orderBy", - "pageSize", - "pageToken", - ) + + # 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 = host_project_registration_service.ListHostProjectRegistrationsRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_host_project_registrations), "__call__" + ) as call: + call.return_value = ( + host_project_registration_service.ListHostProjectRegistrationsResponse() ) - & set(("parent",)) + client.list_host_project_registrations(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", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_list_host_project_registrations_field_headers_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), ) + # 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 = host_project_registration_service.ListHostProjectRegistrationsRequest() -def test_list_host_project_registrations_rest_flattened(): + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_host_project_registrations), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + host_project_registration_service.ListHostProjectRegistrationsResponse() + ) + await client.list_host_project_registrations(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", + "parent=parent_value", + ) in kw["metadata"] + + +def test_list_host_project_registrations_flattened(): client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = ( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_host_project_registrations), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = ( host_project_registration_service.ListHostProjectRegistrationsResponse() ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.list_host_project_registrations( + parent="parent_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_list_host_project_registrations_flattened_error(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_host_project_registrations( + host_project_registration_service.ListHostProjectRegistrationsRequest(), parent="parent_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = ( - host_project_registration_service.ListHostProjectRegistrationsResponse.pb( - return_value - ) + +@pytest.mark.asyncio +async def test_list_host_project_registrations_flattened_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_host_project_registrations), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = ( + host_project_registration_service.ListHostProjectRegistrationsResponse() ) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_host_project_registrations(**mock_args) + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + host_project_registration_service.ListHostProjectRegistrationsResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.list_host_project_registrations( + parent="parent_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/hostProjectRegistrations" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val -def test_list_host_project_registrations_rest_flattened_error(transport: str = "rest"): - client = HostProjectRegistrationServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_list_host_project_registrations_flattened_error_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.list_host_project_registrations( + await client.list_host_project_registrations( host_project_registration_service.ListHostProjectRegistrationsRequest(), parent="parent_value", ) -def test_list_host_project_registrations_rest_pager(transport: str = "rest"): +def test_list_host_project_registrations_pager(transport_name: str = "grpc"): client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport=transport_name, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # TODO(kbandes): remove this mock unless there's a good reason for it. - # with mock.patch.object(path_template, 'transcode') as transcode: - # Set the response as a series of pages - response = ( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_host_project_registrations), "__call__" + ) as call: + # Set the response to a series of pages. + call.side_effect = ( host_project_registration_service.ListHostProjectRegistrationsResponse( host_project_registrations=[ host_project_registration_service.HostProjectRegistration(), @@ -1688,26 +2345,22 @@ def test_list_host_project_registrations_rest_pager(transport: str = "rest"): host_project_registration_service.HostProjectRegistration(), ], ), + RuntimeError, ) - # Two responses for two calls - response = response + response - # Wrap the values into proper Response objs - response = tuple( - host_project_registration_service.ListHostProjectRegistrationsResponse.to_json( - x - ) - for x in response + expected_metadata = () + retry = retries.Retry() + timeout = 5 + expected_metadata = tuple(expected_metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", ""),)), + ) + pager = client.list_host_project_registrations( + request={}, retry=retry, timeout=timeout ) - return_values = tuple(Response() for i in response) - for return_val, response_val in zip(return_values, response): - return_val._content = response_val.encode("UTF-8") - return_val.status_code = 200 - req.side_effect = return_values - - sample_request = {"parent": "projects/sample1/locations/sample2"} - pager = client.list_host_project_registrations(request=sample_request) + assert pager._metadata == expected_metadata + assert pager._retry == retry + assert pager._timeout == timeout results = list(pager) assert len(results) == 6 @@ -1716,1400 +2369,3624 @@ def test_list_host_project_registrations_rest_pager(transport: str = "rest"): for i in results ) - pages = list( - client.list_host_project_registrations(request=sample_request).pages + +def test_list_host_project_registrations_pages(transport_name: str = "grpc"): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport_name, + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_host_project_registrations), "__call__" + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + host_project_registration_service.ListHostProjectRegistrationsResponse( + host_project_registrations=[ + host_project_registration_service.HostProjectRegistration(), + host_project_registration_service.HostProjectRegistration(), + host_project_registration_service.HostProjectRegistration(), + ], + next_page_token="abc", + ), + host_project_registration_service.ListHostProjectRegistrationsResponse( + host_project_registrations=[], + next_page_token="def", + ), + host_project_registration_service.ListHostProjectRegistrationsResponse( + host_project_registrations=[ + host_project_registration_service.HostProjectRegistration(), + ], + next_page_token="ghi", + ), + host_project_registration_service.ListHostProjectRegistrationsResponse( + host_project_registrations=[ + host_project_registration_service.HostProjectRegistration(), + host_project_registration_service.HostProjectRegistration(), + ], + ), + RuntimeError, ) + pages = list(client.list_host_project_registrations(request={}).pages) for page_, token in zip(pages, ["abc", "def", "ghi", ""]): assert page_.raw_page.next_page_token == token -def test_credentials_transport_error(): - # It is an error to provide credentials and a transport instance. - transport = transports.HostProjectRegistrationServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials(), +@pytest.mark.asyncio +async def test_list_host_project_registrations_async_pager(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), ) - with pytest.raises(ValueError): - client = HostProjectRegistrationServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_host_project_registrations), + "__call__", + new_callable=mock.AsyncMock, + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + host_project_registration_service.ListHostProjectRegistrationsResponse( + host_project_registrations=[ + host_project_registration_service.HostProjectRegistration(), + host_project_registration_service.HostProjectRegistration(), + host_project_registration_service.HostProjectRegistration(), + ], + next_page_token="abc", + ), + host_project_registration_service.ListHostProjectRegistrationsResponse( + host_project_registrations=[], + next_page_token="def", + ), + host_project_registration_service.ListHostProjectRegistrationsResponse( + host_project_registrations=[ + host_project_registration_service.HostProjectRegistration(), + ], + next_page_token="ghi", + ), + host_project_registration_service.ListHostProjectRegistrationsResponse( + host_project_registrations=[ + host_project_registration_service.HostProjectRegistration(), + host_project_registration_service.HostProjectRegistration(), + ], + ), + RuntimeError, + ) + async_pager = await client.list_host_project_registrations( + request={}, ) + assert async_pager.next_page_token == "abc" + responses = [] + async for response in async_pager: # pragma: no branch + responses.append(response) - # It is an error to provide a credentials file and a transport instance. - transport = transports.HostProjectRegistrationServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - with pytest.raises(ValueError): - client = HostProjectRegistrationServiceClient( - client_options={"credentials_file": "credentials.json"}, - transport=transport, + assert len(responses) == 6 + assert all( + isinstance(i, host_project_registration_service.HostProjectRegistration) + for i in responses ) - # It is an error to provide an api_key and a transport instance. - transport = transports.HostProjectRegistrationServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials(), + +@pytest.mark.asyncio +async def test_list_host_project_registrations_async_pages(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), ) - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = HostProjectRegistrationServiceClient( - client_options=options, - transport=transport, - ) - # It is an error to provide an api_key and a credential. - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = HostProjectRegistrationServiceClient( - client_options=options, credentials=ga_credentials.AnonymousCredentials() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_host_project_registrations), + "__call__", + new_callable=mock.AsyncMock, + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + host_project_registration_service.ListHostProjectRegistrationsResponse( + host_project_registrations=[ + host_project_registration_service.HostProjectRegistration(), + host_project_registration_service.HostProjectRegistration(), + host_project_registration_service.HostProjectRegistration(), + ], + next_page_token="abc", + ), + host_project_registration_service.ListHostProjectRegistrationsResponse( + host_project_registrations=[], + next_page_token="def", + ), + host_project_registration_service.ListHostProjectRegistrationsResponse( + host_project_registrations=[ + host_project_registration_service.HostProjectRegistration(), + ], + next_page_token="ghi", + ), + host_project_registration_service.ListHostProjectRegistrationsResponse( + host_project_registrations=[ + host_project_registration_service.HostProjectRegistration(), + host_project_registration_service.HostProjectRegistration(), + ], + ), + RuntimeError, ) + pages = [] + # Workaround issue in python 3.9 related to code coverage by adding `# pragma: no branch` + # See https://github.com/googleapis/gapic-generator-python/pull/1174#issuecomment-1025132372 + async for page_ in ( # pragma: no branch + await client.list_host_project_registrations(request={}) + ).pages: + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token - # It is an error to provide scopes and a transport instance. - transport = transports.HostProjectRegistrationServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - with pytest.raises(ValueError): + +def test_create_host_project_registration_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = HostProjectRegistrationServiceClient( - client_options={"scopes": ["1", "2"]}, - transport=transport, + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() -def test_transport_instance(): - # A client may be instantiated with a custom transport instance. - transport = transports.HostProjectRegistrationServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - client = HostProjectRegistrationServiceClient(transport=transport) - assert client.transport is transport + # Ensure method has been cached + assert ( + client._transport.create_host_project_registration + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.create_host_project_registration + ] = mock_rpc + request = {} + client.create_host_project_registration(request) -@pytest.mark.parametrize( - "transport_class", - [ - transports.HostProjectRegistrationServiceRestTransport, - ], -) -def test_transport_adc(transport_class): - # Test default credentials are used if not provided. - with mock.patch.object(google.auth, "default") as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport_class() - adc.assert_called_once() + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + client.create_host_project_registration(request) -def test_transport_kind_rest(): - transport = HostProjectRegistrationServiceClient.get_transport_class("rest")( - credentials=ga_credentials.AnonymousCredentials() - ) - assert transport.kind == "rest" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 -def test_create_host_project_registration_rest_bad_request( +def test_create_host_project_registration_rest_required_fields( request_type=host_project_registration_service.CreateHostProjectRegistrationRequest, ): - client = HostProjectRegistrationServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + transport_class = transports.HostProjectRegistrationServiceRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["host_project_registration_id"] = "" request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_host_project_registration(request) + # verify fields with default values are dropped + assert "hostProjectRegistrationId" not in jsonified_request + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_host_project_registration._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) -@pytest.mark.parametrize( - "request_type", - [ - host_project_registration_service.CreateHostProjectRegistrationRequest, - dict, - ], -) -def test_create_host_project_registration_rest_call_success(request_type): - client = HostProjectRegistrationServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + # verify required fields with default values are now present + assert "hostProjectRegistrationId" in jsonified_request + assert ( + jsonified_request["hostProjectRegistrationId"] + == request_init["host_project_registration_id"] ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request_init["host_project_registration"] = { - "name": "name_value", - "gcp_project": "gcp_project_value", - "create_time": {"seconds": 751, "nanos": 543}, - } - # The version of a generated dependency at test runtime may differ from the version used during generation. - # Delete any fields which are not present in the current runtime dependency - # See https://github.com/googleapis/gapic-generator-python/issues/1748 + jsonified_request["parent"] = "parent_value" + jsonified_request[ + "hostProjectRegistrationId" + ] = "host_project_registration_id_value" - # Determine if the message type is proto-plus or protobuf - test_field = host_project_registration_service.CreateHostProjectRegistrationRequest.meta.fields[ - "host_project_registration" - ] + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_host_project_registration._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("host_project_registration_id",)) + jsonified_request.update(unset_fields) - def get_message_fields(field): - # Given a field which is a message (composite type), return a list with - # all the fields of the message. - # If the field is not a composite type, return an empty list. - message_fields = [] + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "hostProjectRegistrationId" in jsonified_request + assert ( + jsonified_request["hostProjectRegistrationId"] + == "host_project_registration_id_value" + ) - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields + # Designate an appropriate value for the returned response. + return_value = host_project_registration_service.HostProjectRegistration() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] + response_value = Response() + response_value.status_code = 200 - subfields_not_in_runtime = [] + # Convert return value to protobuf type + return_value = host_project_registration_service.HostProjectRegistration.pb( + return_value + ) + json_return_value = json_format.MessageToJson(return_value) - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init[ - "host_project_registration" - ].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) + response = client.create_host_project_registration(request) - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range( - 0, len(request_init["host_project_registration"][field]) - ): - del request_init["host_project_registration"][field][i][subfield] - else: - del request_init["host_project_registration"][field][subfield] - request = request_type(**request_init) + expected_params = [ + ( + "hostProjectRegistrationId", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_host_project_registration_rest_unset_required_fields(): + transport = transports.HostProjectRegistrationServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = ( + transport.create_host_project_registration._get_unset_required_fields({}) + ) + assert set(unset_fields) == ( + set(("hostProjectRegistrationId",)) + & set( + ( + "parent", + "hostProjectRegistrationId", + "hostProjectRegistration", + ) + ) + ) + + +def test_create_host_project_registration_rest_flattened(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = host_project_registration_service.HostProjectRegistration( - name="name_value", - gcp_project="gcp_project_value", + return_value = host_project_registration_service.HostProjectRegistration() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + host_project_registration=host_project_registration_service.HostProjectRegistration( + name="name_value" + ), + host_project_registration_id="host_project_registration_id_value", ) + mock_args.update(sample_request) # Wrap the value into a proper Response obj - response_value = mock.Mock() + response_value = Response() response_value.status_code = 200 - # Convert return value to protobuf type return_value = host_project_registration_service.HostProjectRegistration.pb( return_value ) json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.create_host_project_registration(request) - # Establish that the response is the type that we expect. - assert isinstance( - response, host_project_registration_service.HostProjectRegistration - ) - assert response.name == "name_value" - assert response.gcp_project == "gcp_project_value" + client.create_host_project_registration(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/hostProjectRegistrations" + % client.transport._host, + args[1], + ) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_create_host_project_registration_rest_interceptors(null_interceptor): - transport = transports.HostProjectRegistrationServiceRestTransport( +def test_create_host_project_registration_rest_flattened_error(transport: str = "rest"): + client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.HostProjectRegistrationServiceRestInterceptor(), + transport=transport, ) - client = HostProjectRegistrationServiceClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.HostProjectRegistrationServiceRestInterceptor, - "post_create_host_project_registration", - ) as post, mock.patch.object( - transports.HostProjectRegistrationServiceRestInterceptor, - "post_create_host_project_registration_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.HostProjectRegistrationServiceRestInterceptor, - "pre_create_host_project_registration", - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = ( - host_project_registration_service.CreateHostProjectRegistrationRequest.pb( - host_project_registration_service.CreateHostProjectRegistrationRequest() - ) + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_host_project_registration( + host_project_registration_service.CreateHostProjectRegistrationRequest(), + parent="parent_value", + host_project_registration=host_project_registration_service.HostProjectRegistration( + name="name_value" + ), + host_project_registration_id="host_project_registration_id_value", ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = ( - host_project_registration_service.HostProjectRegistration.to_json( - host_project_registration_service.HostProjectRegistration() - ) - ) - req.return_value.content = return_value - request = ( - host_project_registration_service.CreateHostProjectRegistrationRequest() +def test_get_host_project_registration_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = host_project_registration_service.HostProjectRegistration() - post_with_metadata.return_value = ( - host_project_registration_service.HostProjectRegistration(), - metadata, + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.get_host_project_registration + in client._transport._wrapped_methods ) - client.create_host_project_registration( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. ) + client._transport._wrapped_methods[ + client._transport.get_host_project_registration + ] = mock_rpc - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + request = {} + client.get_host_project_registration(request) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 -def test_get_host_project_registration_rest_bad_request( + client.get_host_project_registration(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_get_host_project_registration_rest_required_fields( request_type=host_project_registration_service.GetHostProjectRegistrationRequest, ): - client = HostProjectRegistrationServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/hostProjectRegistrations/sample3" - } + transport_class = transports.HostProjectRegistrationServiceRestTransport + + request_init = {} + request_init["name"] = "" request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_host_project_registration(request) + # verify fields with default values are dropped + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_host_project_registration._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) -@pytest.mark.parametrize( - "request_type", - [ - host_project_registration_service.GetHostProjectRegistrationRequest, - dict, - ], -) -def test_get_host_project_registration_rest_call_success(request_type): - client = HostProjectRegistrationServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) + # verify required fields with default values are now present - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/hostProjectRegistrations/sample3" - } + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_host_project_registration._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) request = request_type(**request_init) + # Designate an appropriate value for the returned response. + return_value = host_project_registration_service.HostProjectRegistration() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = host_project_registration_service.HostProjectRegistration.pb( + return_value + ) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_host_project_registration(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_host_project_registration_rest_unset_required_fields(): + transport = transports.HostProjectRegistrationServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_host_project_registration._get_unset_required_fields( + {} + ) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_get_host_project_registration_rest_flattened(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = host_project_registration_service.HostProjectRegistration( + return_value = host_project_registration_service.HostProjectRegistration() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/hostProjectRegistrations/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( name="name_value", - gcp_project="gcp_project_value", ) + mock_args.update(sample_request) # Wrap the value into a proper Response obj - response_value = mock.Mock() + response_value = Response() response_value.status_code = 200 - # Convert return value to protobuf type return_value = host_project_registration_service.HostProjectRegistration.pb( return_value ) json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_host_project_registration(request) - # Establish that the response is the type that we expect. - assert isinstance( - response, host_project_registration_service.HostProjectRegistration - ) - assert response.name == "name_value" - assert response.gcp_project == "gcp_project_value" + client.get_host_project_registration(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/hostProjectRegistrations/*}" + % client.transport._host, + args[1], + ) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_host_project_registration_rest_interceptors(null_interceptor): - transport = transports.HostProjectRegistrationServiceRestTransport( +def test_get_host_project_registration_rest_flattened_error(transport: str = "rest"): + client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.HostProjectRegistrationServiceRestInterceptor(), + transport=transport, ) - client = HostProjectRegistrationServiceClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.HostProjectRegistrationServiceRestInterceptor, - "post_get_host_project_registration", - ) as post, mock.patch.object( - transports.HostProjectRegistrationServiceRestInterceptor, - "post_get_host_project_registration_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.HostProjectRegistrationServiceRestInterceptor, - "pre_get_host_project_registration", - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = ( - host_project_registration_service.GetHostProjectRegistrationRequest.pb( - host_project_registration_service.GetHostProjectRegistrationRequest() - ) + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_host_project_registration( + host_project_registration_service.GetHostProjectRegistrationRequest(), + name="name_value", ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = ( - host_project_registration_service.HostProjectRegistration.to_json( - host_project_registration_service.HostProjectRegistration() - ) - ) - req.return_value.content = return_value - request = host_project_registration_service.GetHostProjectRegistrationRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = host_project_registration_service.HostProjectRegistration() - post_with_metadata.return_value = ( - host_project_registration_service.HostProjectRegistration(), - metadata, +def test_list_host_project_registrations_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - client.get_host_project_registration( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.list_host_project_registrations + in client._transport._wrapped_methods ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.list_host_project_registrations + ] = mock_rpc + request = {} + client.list_host_project_registrations(request) -def test_list_host_project_registrations_rest_bad_request( - request_type=host_project_registration_service.ListHostProjectRegistrationsRequest, -): - client = HostProjectRegistrationServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request = request_type(**request_init) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} client.list_host_project_registrations(request) + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 -@pytest.mark.parametrize( - "request_type", - [ - host_project_registration_service.ListHostProjectRegistrationsRequest, - dict, - ], -) -def test_list_host_project_registrations_rest_call_success(request_type): - client = HostProjectRegistrationServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} +def test_list_host_project_registrations_rest_required_fields( + request_type=host_project_registration_service.ListHostProjectRegistrationsRequest, +): + transport_class = transports.HostProjectRegistrationServiceRestTransport + + request_init = {} + request_init["parent"] = "" request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = ( - host_project_registration_service.ListHostProjectRegistrationsResponse( - next_page_token="next_page_token_value", - ) + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_host_project_registrations._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_host_project_registrations._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "order_by", + "page_size", + "page_token", ) + ) + jsonified_request.update(unset_fields) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" - # Convert return value to protobuf type - return_value = ( - host_project_registration_service.ListHostProjectRegistrationsResponse.pb( + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = ( + host_project_registration_service.ListHostProjectRegistrationsResponse() + ) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = host_project_registration_service.ListHostProjectRegistrationsResponse.pb( return_value ) - ) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.list_host_project_registrations(request) + json_return_value = json_format.MessageToJson(return_value) - # Establish that the response is the type that we expect. - assert isinstance(response, pagers.ListHostProjectRegistrationsPager) - assert response.next_page_token == "next_page_token_value" + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.list_host_project_registrations(request) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_list_host_project_registrations_rest_interceptors(null_interceptor): + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_host_project_registrations_rest_unset_required_fields(): transport = transports.HostProjectRegistrationServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.HostProjectRegistrationServiceRestInterceptor(), + credentials=ga_credentials.AnonymousCredentials ) - client = HostProjectRegistrationServiceClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.HostProjectRegistrationServiceRestInterceptor, - "post_list_host_project_registrations", - ) as post, mock.patch.object( - transports.HostProjectRegistrationServiceRestInterceptor, - "post_list_host_project_registrations_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.HostProjectRegistrationServiceRestInterceptor, - "pre_list_host_project_registrations", - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = ( - host_project_registration_service.ListHostProjectRegistrationsRequest.pb( - host_project_registration_service.ListHostProjectRegistrationsRequest() + unset_fields = transport.list_host_project_registrations._get_unset_required_fields( + {} + ) + assert set(unset_fields) == ( + set( + ( + "filter", + "orderBy", + "pageSize", + "pageToken", ) ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } + & set(("parent",)) + ) - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = host_project_registration_service.ListHostProjectRegistrationsResponse.to_json( - host_project_registration_service.ListHostProjectRegistrationsResponse() - ) - req.return_value.content = return_value - request = ( - host_project_registration_service.ListHostProjectRegistrationsRequest() - ) - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = ( +def test_list_host_project_registrations_rest_flattened(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = ( host_project_registration_service.ListHostProjectRegistrationsResponse() ) - post_with_metadata.return_value = ( - host_project_registration_service.ListHostProjectRegistrationsResponse(), - metadata, + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", ) + mock_args.update(sample_request) - client.list_host_project_registrations( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = ( + host_project_registration_service.ListHostProjectRegistrationsResponse.pb( + return_value + ) ) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + client.list_host_project_registrations(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/hostProjectRegistrations" + % client.transport._host, + args[1], + ) -def test_get_location_rest_bad_request(request_type=locations_pb2.GetLocationRequest): +def test_list_host_project_registrations_rest_flattened_error(transport: str = "rest"): client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2"}, request + transport=transport, ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_location(request) + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_host_project_registrations( + host_project_registration_service.ListHostProjectRegistrationsRequest(), + parent="parent_value", + ) -@pytest.mark.parametrize( - "request_type", - [ - locations_pb2.GetLocationRequest, - dict, - ], -) -def test_get_location_rest(request_type): +def test_list_host_project_registrations_rest_pager(transport: str = "rest"): client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - request_init = {"name": "projects/sample1/locations/sample2"} - request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = locations_pb2.Location() - - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + host_project_registration_service.ListHostProjectRegistrationsResponse( + host_project_registrations=[ + host_project_registration_service.HostProjectRegistration(), + host_project_registration_service.HostProjectRegistration(), + host_project_registration_service.HostProjectRegistration(), + ], + next_page_token="abc", + ), + host_project_registration_service.ListHostProjectRegistrationsResponse( + host_project_registrations=[], + next_page_token="def", + ), + host_project_registration_service.ListHostProjectRegistrationsResponse( + host_project_registrations=[ + host_project_registration_service.HostProjectRegistration(), + ], + next_page_token="ghi", + ), + host_project_registration_service.ListHostProjectRegistrationsResponse( + host_project_registrations=[ + host_project_registration_service.HostProjectRegistration(), + host_project_registration_service.HostProjectRegistration(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + host_project_registration_service.ListHostProjectRegistrationsResponse.to_json( + x + ) + for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2"} + + pager = client.list_host_project_registrations(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all( + isinstance(i, host_project_registration_service.HostProjectRegistration) + for i in results + ) + + pages = list( + client.list_host_project_registrations(request=sample_request).pages + ) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +def test_credentials_transport_error(): + # It is an error to provide credentials and a transport instance. + transport = transports.HostProjectRegistrationServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # It is an error to provide a credentials file and a transport instance. + transport = transports.HostProjectRegistrationServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = HostProjectRegistrationServiceClient( + client_options={"credentials_file": "credentials.json"}, + transport=transport, + ) + + # It is an error to provide an api_key and a transport instance. + transport = transports.HostProjectRegistrationServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = HostProjectRegistrationServiceClient( + client_options=options, + transport=transport, + ) + + # It is an error to provide an api_key and a credential. + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = HostProjectRegistrationServiceClient( + client_options=options, credentials=ga_credentials.AnonymousCredentials() + ) + + # It is an error to provide scopes and a transport instance. + transport = transports.HostProjectRegistrationServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = HostProjectRegistrationServiceClient( + client_options={"scopes": ["1", "2"]}, + transport=transport, + ) + + +def test_transport_instance(): + # A client may be instantiated with a custom transport instance. + transport = transports.HostProjectRegistrationServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + client = HostProjectRegistrationServiceClient(transport=transport) + assert client.transport is transport + + +def test_transport_get_channel(): + # A client may be instantiated with a custom transport instance. + transport = transports.HostProjectRegistrationServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + transport = transports.HostProjectRegistrationServiceGrpcAsyncIOTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.HostProjectRegistrationServiceGrpcTransport, + transports.HostProjectRegistrationServiceGrpcAsyncIOTransport, + transports.HostProjectRegistrationServiceRestTransport, + ], +) +def test_transport_adc(transport_class): + # Test default credentials are used if not provided. + with mock.patch.object(google.auth, "default") as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class() + adc.assert_called_once() + + +def test_transport_kind_grpc(): + transport = HostProjectRegistrationServiceClient.get_transport_class("grpc")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "grpc" + + +def test_initialize_client_w_grpc(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_host_project_registration_empty_call_grpc(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_host_project_registration), "__call__" + ) as call: + call.return_value = host_project_registration_service.HostProjectRegistration() + client.create_host_project_registration(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + host_project_registration_service.CreateHostProjectRegistrationRequest() + ) + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_host_project_registration_empty_call_grpc(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_host_project_registration), "__call__" + ) as call: + call.return_value = host_project_registration_service.HostProjectRegistration() + client.get_host_project_registration(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + host_project_registration_service.GetHostProjectRegistrationRequest() + ) + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_host_project_registrations_empty_call_grpc(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_host_project_registrations), "__call__" + ) as call: + call.return_value = ( + host_project_registration_service.ListHostProjectRegistrationsResponse() + ) + client.list_host_project_registrations(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + host_project_registration_service.ListHostProjectRegistrationsRequest() + ) + + assert args[0] == request_msg + + +def test_transport_kind_grpc_asyncio(): + transport = HostProjectRegistrationServiceAsyncClient.get_transport_class( + "grpc_asyncio" + )(credentials=async_anonymous_credentials()) + assert transport.kind == "grpc_asyncio" + + +def test_initialize_client_w_grpc_asyncio(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_create_host_project_registration_empty_call_grpc_asyncio(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_host_project_registration), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + host_project_registration_service.HostProjectRegistration( + name="name_value", + gcp_project="gcp_project_value", + ) + ) + await client.create_host_project_registration(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + host_project_registration_service.CreateHostProjectRegistrationRequest() + ) + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_host_project_registration_empty_call_grpc_asyncio(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_host_project_registration), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + host_project_registration_service.HostProjectRegistration( + name="name_value", + gcp_project="gcp_project_value", + ) + ) + await client.get_host_project_registration(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + host_project_registration_service.GetHostProjectRegistrationRequest() + ) + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_list_host_project_registrations_empty_call_grpc_asyncio(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_host_project_registrations), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + host_project_registration_service.ListHostProjectRegistrationsResponse( + next_page_token="next_page_token_value", + ) + ) + await client.list_host_project_registrations(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + host_project_registration_service.ListHostProjectRegistrationsRequest() + ) + + assert args[0] == request_msg + + +def test_transport_kind_rest(): + transport = HostProjectRegistrationServiceClient.get_transport_class("rest")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "rest" + + +def test_create_host_project_registration_rest_bad_request( + request_type=host_project_registration_service.CreateHostProjectRegistrationRequest, +): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.create_host_project_registration(request) + + +@pytest.mark.parametrize( + "request_type", + [ + host_project_registration_service.CreateHostProjectRegistrationRequest, + dict, + ], +) +def test_create_host_project_registration_rest_call_success(request_type): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["host_project_registration"] = { + "name": "name_value", + "gcp_project": "gcp_project_value", + "create_time": {"seconds": 751, "nanos": 543}, + } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = host_project_registration_service.CreateHostProjectRegistrationRequest.meta.fields[ + "host_project_registration" + ] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init[ + "host_project_registration" + ].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range( + 0, len(request_init["host_project_registration"][field]) + ): + del request_init["host_project_registration"][field][i][subfield] + else: + del request_init["host_project_registration"][field][subfield] + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = host_project_registration_service.HostProjectRegistration( + name="name_value", + gcp_project="gcp_project_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = host_project_registration_service.HostProjectRegistration.pb( + return_value + ) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.create_host_project_registration(request) + + # Establish that the response is the type that we expect. + assert isinstance( + response, host_project_registration_service.HostProjectRegistration + ) + assert response.name == "name_value" + assert response.gcp_project == "gcp_project_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_host_project_registration_rest_interceptors(null_interceptor): + transport = transports.HostProjectRegistrationServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.HostProjectRegistrationServiceRestInterceptor(), + ) + client = HostProjectRegistrationServiceClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.HostProjectRegistrationServiceRestInterceptor, + "post_create_host_project_registration", + ) as post, mock.patch.object( + transports.HostProjectRegistrationServiceRestInterceptor, + "post_create_host_project_registration_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.HostProjectRegistrationServiceRestInterceptor, + "pre_create_host_project_registration", + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = ( + host_project_registration_service.CreateHostProjectRegistrationRequest.pb( + host_project_registration_service.CreateHostProjectRegistrationRequest() + ) + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = ( + host_project_registration_service.HostProjectRegistration.to_json( + host_project_registration_service.HostProjectRegistration() + ) + ) + req.return_value.content = return_value + + request = ( + host_project_registration_service.CreateHostProjectRegistrationRequest() + ) + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = host_project_registration_service.HostProjectRegistration() + post_with_metadata.return_value = ( + host_project_registration_service.HostProjectRegistration(), + metadata, + ) + + client.create_host_project_registration( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_get_host_project_registration_rest_bad_request( + request_type=host_project_registration_service.GetHostProjectRegistrationRequest, +): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/hostProjectRegistrations/sample3" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_host_project_registration(request) + + +@pytest.mark.parametrize( + "request_type", + [ + host_project_registration_service.GetHostProjectRegistrationRequest, + dict, + ], +) +def test_get_host_project_registration_rest_call_success(request_type): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/hostProjectRegistrations/sample3" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = host_project_registration_service.HostProjectRegistration( + name="name_value", + gcp_project="gcp_project_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = host_project_registration_service.HostProjectRegistration.pb( + return_value + ) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.get_host_project_registration(request) + + # Establish that the response is the type that we expect. + assert isinstance( + response, host_project_registration_service.HostProjectRegistration + ) + assert response.name == "name_value" + assert response.gcp_project == "gcp_project_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_host_project_registration_rest_interceptors(null_interceptor): + transport = transports.HostProjectRegistrationServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.HostProjectRegistrationServiceRestInterceptor(), + ) + client = HostProjectRegistrationServiceClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.HostProjectRegistrationServiceRestInterceptor, + "post_get_host_project_registration", + ) as post, mock.patch.object( + transports.HostProjectRegistrationServiceRestInterceptor, + "post_get_host_project_registration_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.HostProjectRegistrationServiceRestInterceptor, + "pre_get_host_project_registration", + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = ( + host_project_registration_service.GetHostProjectRegistrationRequest.pb( + host_project_registration_service.GetHostProjectRegistrationRequest() + ) + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = ( + host_project_registration_service.HostProjectRegistration.to_json( + host_project_registration_service.HostProjectRegistration() + ) + ) + req.return_value.content = return_value + + request = host_project_registration_service.GetHostProjectRegistrationRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = host_project_registration_service.HostProjectRegistration() + post_with_metadata.return_value = ( + host_project_registration_service.HostProjectRegistration(), + metadata, + ) + + client.get_host_project_registration( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_list_host_project_registrations_rest_bad_request( + request_type=host_project_registration_service.ListHostProjectRegistrationsRequest, +): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_host_project_registrations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + host_project_registration_service.ListHostProjectRegistrationsRequest, + dict, + ], +) +def test_list_host_project_registrations_rest_call_success(request_type): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = ( + host_project_registration_service.ListHostProjectRegistrationsResponse( + next_page_token="next_page_token_value", + ) + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = ( + host_project_registration_service.ListHostProjectRegistrationsResponse.pb( + return_value + ) + ) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.list_host_project_registrations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListHostProjectRegistrationsPager) + assert response.next_page_token == "next_page_token_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_host_project_registrations_rest_interceptors(null_interceptor): + transport = transports.HostProjectRegistrationServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.HostProjectRegistrationServiceRestInterceptor(), + ) + client = HostProjectRegistrationServiceClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.HostProjectRegistrationServiceRestInterceptor, + "post_list_host_project_registrations", + ) as post, mock.patch.object( + transports.HostProjectRegistrationServiceRestInterceptor, + "post_list_host_project_registrations_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.HostProjectRegistrationServiceRestInterceptor, + "pre_list_host_project_registrations", + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = ( + host_project_registration_service.ListHostProjectRegistrationsRequest.pb( + host_project_registration_service.ListHostProjectRegistrationsRequest() + ) + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = host_project_registration_service.ListHostProjectRegistrationsResponse.to_json( + host_project_registration_service.ListHostProjectRegistrationsResponse() + ) + req.return_value.content = return_value + + request = ( + host_project_registration_service.ListHostProjectRegistrationsRequest() + ) + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = ( + host_project_registration_service.ListHostProjectRegistrationsResponse() + ) + post_with_metadata.return_value = ( + host_project_registration_service.ListHostProjectRegistrationsResponse(), + metadata, + ) + + client.list_host_project_registrations( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_get_location_rest_bad_request(request_type=locations_pb2.GetLocationRequest): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_location(request) + + +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.GetLocationRequest, + dict, + ], +) +def test_get_location_rest(request_type): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = locations_pb2.Location() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_location(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) + + +def test_list_locations_rest_bad_request( + request_type=locations_pb2.ListLocationsRequest, +): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict({"name": "projects/sample1"}, request) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_locations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.ListLocationsRequest, + dict, + ], +) +def test_list_locations_rest(request_type): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = locations_pb2.ListLocationsResponse() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_locations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + +def test_cancel_operation_rest_bad_request( + request_type=operations_pb2.CancelOperationRequest, +): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.cancel_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.CancelOperationRequest, + dict, + ], +) +def test_cancel_operation_rest(request_type): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "{}" + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.cancel_operation(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_delete_operation_rest_bad_request( + request_type=operations_pb2.DeleteOperationRequest, +): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.delete_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.DeleteOperationRequest, + dict, + ], +) +def test_delete_operation_rest(request_type): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "{}" + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_operation(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_get_operation_rest_bad_request( + request_type=operations_pb2.GetOperationRequest, +): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.GetOperationRequest, + dict, + ], +) +def test_get_operation_rest(request_type): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_operation(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) + + +def test_list_operations_rest_bad_request( + request_type=operations_pb2.ListOperationsRequest, +): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_operations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.ListOperationsRequest, + dict, + ], +) +def test_list_operations_rest(request_type): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.ListOperationsResponse() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_operations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) + + +def test_initialize_client_w_rest(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_host_project_registration_empty_call_rest(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_host_project_registration), "__call__" + ) as call: + client.create_host_project_registration(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + host_project_registration_service.CreateHostProjectRegistrationRequest() + ) + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_host_project_registration_empty_call_rest(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_host_project_registration), "__call__" + ) as call: + client.get_host_project_registration(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + host_project_registration_service.GetHostProjectRegistrationRequest() + ) + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_host_project_registrations_empty_call_rest(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_host_project_registrations), "__call__" + ) as call: + client.list_host_project_registrations(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + host_project_registration_service.ListHostProjectRegistrationsRequest() + ) + + assert args[0] == request_msg + + +def test_transport_grpc_default(): + # A client should use the gRPC transport by default. + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert isinstance( + client.transport, + transports.HostProjectRegistrationServiceGrpcTransport, + ) + + +def test_host_project_registration_service_base_transport_error(): + # Passing both a credentials object and credentials_file should raise an error + with pytest.raises(core_exceptions.DuplicateCredentialArgs): + transport = transports.HostProjectRegistrationServiceTransport( + credentials=ga_credentials.AnonymousCredentials(), + credentials_file="credentials.json", + ) + + +def test_host_project_registration_service_base_transport(): + # Instantiate the base transport. + with mock.patch( + "google.cloud.apihub_v1.services.host_project_registration_service.transports.HostProjectRegistrationServiceTransport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.HostProjectRegistrationServiceTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Every method on the transport should just blindly + # raise NotImplementedError. + methods = ( + "create_host_project_registration", + "get_host_project_registration", + "list_host_project_registrations", + "get_location", + "list_locations", + "get_operation", + "cancel_operation", + "delete_operation", + "list_operations", + ) + for method in methods: + with pytest.raises(NotImplementedError): + getattr(transport, method)(request=object()) + + with pytest.raises(NotImplementedError): + transport.close() + + # Catch all for all remaining methods and properties + remainder = [ + "kind", + ] + for r in remainder: + with pytest.raises(NotImplementedError): + getattr(transport, r)() + + +def test_host_project_registration_service_base_transport_with_credentials_file(): + # Instantiate the base transport with a credentials file + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch( + "google.cloud.apihub_v1.services.host_project_registration_service.transports.HostProjectRegistrationServiceTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) + transport = transports.HostProjectRegistrationServiceTransport( + credentials_file="credentials.json", + quota_project_id="octopus", + ) + load_creds.assert_called_once_with( + "credentials.json", + scopes=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", + ) + + +def test_host_project_registration_service_base_transport_with_adc(): + # Test the default credentials are used if credentials and credentials_file are None. + with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch( + "google.cloud.apihub_v1.services.host_project_registration_service.transports.HostProjectRegistrationServiceTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport = transports.HostProjectRegistrationServiceTransport() + adc.assert_called_once() + + +def test_host_project_registration_service_auth_adc(): + # If no credentials are provided, we should use ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + HostProjectRegistrationServiceClient() + adc.assert_called_once_with( + scopes=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id=None, + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.HostProjectRegistrationServiceGrpcTransport, + transports.HostProjectRegistrationServiceGrpcAsyncIOTransport, + ], +) +def test_host_project_registration_service_transport_auth_adc(transport_class): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + adc.assert_called_once_with( + scopes=["1", "2"], + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.HostProjectRegistrationServiceGrpcTransport, + transports.HostProjectRegistrationServiceGrpcAsyncIOTransport, + transports.HostProjectRegistrationServiceRestTransport, + ], +) +def test_host_project_registration_service_transport_auth_gdch_credentials( + transport_class, +): + host = "https://language.com" + api_audience_tests = [None, "https://language2.com"] + api_audience_expect = [host, "https://language2.com"] + for t, e in zip(api_audience_tests, api_audience_expect): + with mock.patch.object(google.auth, "default", autospec=True) as adc: + gdch_mock = mock.MagicMock() + type(gdch_mock).with_gdch_audience = mock.PropertyMock( + return_value=gdch_mock + ) + adc.return_value = (gdch_mock, None) + transport_class(host=host, api_audience=t) + gdch_mock.with_gdch_audience.assert_called_once_with(e) + + +@pytest.mark.parametrize( + "transport_class,grpc_helpers", + [ + (transports.HostProjectRegistrationServiceGrpcTransport, grpc_helpers), + ( + transports.HostProjectRegistrationServiceGrpcAsyncIOTransport, + grpc_helpers_async, + ), + ], +) +def test_host_project_registration_service_transport_create_channel( + transport_class, grpc_helpers +): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel", autospec=True + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + adc.return_value = (creds, None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + + create_channel.assert_called_with( + "apihub.googleapis.com:443", + credentials=creds, + credentials_file=None, + quota_project_id="octopus", + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + scopes=["1", "2"], + default_host="apihub.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.HostProjectRegistrationServiceGrpcTransport, + transports.HostProjectRegistrationServiceGrpcAsyncIOTransport, + ], +) +def test_host_project_registration_service_grpc_transport_client_cert_source_for_mtls( + transport_class, +): + cred = ga_credentials.AnonymousCredentials() + + # Check ssl_channel_credentials is used if provided. + with mock.patch.object(transport_class, "create_channel") as mock_create_channel: + mock_ssl_channel_creds = mock.Mock() + transport_class( + host="squid.clam.whelk", + credentials=cred, + ssl_channel_credentials=mock_ssl_channel_creds, + ) + mock_create_channel.assert_called_once_with( + "squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_channel_creds, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + # Check if ssl_channel_credentials is not provided, then client_cert_source_for_mtls + # is used. + with mock.patch.object(transport_class, "create_channel", return_value=mock.Mock()): + with mock.patch("grpc.ssl_channel_credentials") as mock_ssl_cred: + transport_class( + credentials=cred, + client_cert_source_for_mtls=client_cert_source_callback, + ) + expected_cert, expected_key = client_cert_source_callback() + mock_ssl_cred.assert_called_once_with( + certificate_chain=expected_cert, private_key=expected_key + ) + + +def test_host_project_registration_service_http_transport_client_cert_source_for_mtls(): + cred = ga_credentials.AnonymousCredentials() + with mock.patch( + "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" + ) as mock_configure_mtls_channel: + transports.HostProjectRegistrationServiceRestTransport( + credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + ) + mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + + +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "grpc_asyncio", + "rest", + ], +) +def test_host_project_registration_service_host_no_port(transport_name): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="apihub.googleapis.com" + ), + transport=transport_name, + ) + assert client.transport._host == ( + "apihub.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://apihub.googleapis.com" + ) + + +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "grpc_asyncio", + "rest", + ], +) +def test_host_project_registration_service_host_with_port(transport_name): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="apihub.googleapis.com:8000" + ), + transport=transport_name, + ) + assert client.transport._host == ( + "apihub.googleapis.com:8000" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://apihub.googleapis.com:8000" + ) + + +@pytest.mark.parametrize( + "transport_name", + [ + "rest", + ], +) +def test_host_project_registration_service_client_transport_session_collision( + transport_name, +): + creds1 = ga_credentials.AnonymousCredentials() + creds2 = ga_credentials.AnonymousCredentials() + client1 = HostProjectRegistrationServiceClient( + credentials=creds1, + transport=transport_name, + ) + client2 = HostProjectRegistrationServiceClient( + credentials=creds2, + transport=transport_name, + ) + session1 = client1.transport.create_host_project_registration._session + session2 = client2.transport.create_host_project_registration._session + assert session1 != session2 + session1 = client1.transport.get_host_project_registration._session + session2 = client2.transport.get_host_project_registration._session + assert session1 != session2 + session1 = client1.transport.list_host_project_registrations._session + session2 = client2.transport.list_host_project_registrations._session + assert session1 != session2 + + +def test_host_project_registration_service_grpc_transport_channel(): + channel = grpc.secure_channel("http://localhost/", grpc.local_channel_credentials()) + + # Check that channel is used if provided. + transport = transports.HostProjectRegistrationServiceGrpcTransport( + host="squid.clam.whelk", + channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None + + +def test_host_project_registration_service_grpc_asyncio_transport_channel(): + channel = aio.secure_channel("http://localhost/", grpc.local_channel_credentials()) + + # Check that channel is used if provided. + transport = transports.HostProjectRegistrationServiceGrpcAsyncIOTransport( + host="squid.clam.whelk", + channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None + + +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. +@pytest.mark.parametrize( + "transport_class", + [ + transports.HostProjectRegistrationServiceGrpcTransport, + transports.HostProjectRegistrationServiceGrpcAsyncIOTransport, + ], +) +def test_host_project_registration_service_transport_channel_mtls_with_client_cert_source( + transport_class, +): + with mock.patch( + "grpc.ssl_channel_credentials", autospec=True + ) as grpc_ssl_channel_cred: + with mock.patch.object( + transport_class, "create_channel" + ) 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 = ga_credentials.AnonymousCredentials() + with pytest.warns(DeprecationWarning): + with mock.patch.object(google.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=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + assert transport._ssl_channel_credentials == mock_ssl_cred + + +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. +@pytest.mark.parametrize( + "transport_class", + [ + transports.HostProjectRegistrationServiceGrpcTransport, + transports.HostProjectRegistrationServiceGrpcAsyncIOTransport, + ], +) +def test_host_project_registration_service_transport_channel_mtls_with_adc( + transport_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), + ): + with mock.patch.object( + transport_class, "create_channel" + ) 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, + ) + + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=mock_cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + + +def test_host_project_registration_path(): + project = "squid" + location = "clam" + host_project_registration = "whelk" + expected = "projects/{project}/locations/{location}/hostProjectRegistrations/{host_project_registration}".format( + project=project, + location=location, + host_project_registration=host_project_registration, + ) + actual = HostProjectRegistrationServiceClient.host_project_registration_path( + project, location, host_project_registration + ) + assert expected == actual + + +def test_parse_host_project_registration_path(): + expected = { + "project": "octopus", + "location": "oyster", + "host_project_registration": "nudibranch", + } + path = HostProjectRegistrationServiceClient.host_project_registration_path( + **expected + ) + + # Check that the path construction is reversible. + actual = HostProjectRegistrationServiceClient.parse_host_project_registration_path( + path + ) + assert expected == actual + + +def test_common_billing_account_path(): + billing_account = "cuttlefish" + expected = "billingAccounts/{billing_account}".format( + billing_account=billing_account, + ) + actual = HostProjectRegistrationServiceClient.common_billing_account_path( + billing_account + ) + assert expected == actual + + +def test_parse_common_billing_account_path(): + expected = { + "billing_account": "mussel", + } + path = HostProjectRegistrationServiceClient.common_billing_account_path(**expected) + + # Check that the path construction is reversible. + actual = HostProjectRegistrationServiceClient.parse_common_billing_account_path( + path + ) + assert expected == actual + + +def test_common_folder_path(): + folder = "winkle" + expected = "folders/{folder}".format( + folder=folder, + ) + actual = HostProjectRegistrationServiceClient.common_folder_path(folder) + assert expected == actual + + +def test_parse_common_folder_path(): + expected = { + "folder": "nautilus", + } + path = HostProjectRegistrationServiceClient.common_folder_path(**expected) + + # Check that the path construction is reversible. + actual = HostProjectRegistrationServiceClient.parse_common_folder_path(path) + assert expected == actual + + +def test_common_organization_path(): + organization = "scallop" + expected = "organizations/{organization}".format( + organization=organization, + ) + actual = HostProjectRegistrationServiceClient.common_organization_path(organization) + assert expected == actual + + +def test_parse_common_organization_path(): + expected = { + "organization": "abalone", + } + path = HostProjectRegistrationServiceClient.common_organization_path(**expected) + + # Check that the path construction is reversible. + actual = HostProjectRegistrationServiceClient.parse_common_organization_path(path) + assert expected == actual + + +def test_common_project_path(): + project = "squid" + expected = "projects/{project}".format( + project=project, + ) + actual = HostProjectRegistrationServiceClient.common_project_path(project) + assert expected == actual + + +def test_parse_common_project_path(): + expected = { + "project": "clam", + } + path = HostProjectRegistrationServiceClient.common_project_path(**expected) + + # Check that the path construction is reversible. + actual = HostProjectRegistrationServiceClient.parse_common_project_path(path) + assert expected == actual + + +def test_common_location_path(): + project = "whelk" + location = "octopus" + expected = "projects/{project}/locations/{location}".format( + project=project, + location=location, + ) + actual = HostProjectRegistrationServiceClient.common_location_path( + project, location + ) + assert expected == actual + + +def test_parse_common_location_path(): + expected = { + "project": "oyster", + "location": "nudibranch", + } + path = HostProjectRegistrationServiceClient.common_location_path(**expected) + + # Check that the path construction is reversible. + actual = HostProjectRegistrationServiceClient.parse_common_location_path(path) + assert expected == actual + + +def test_client_with_default_client_info(): + client_info = gapic_v1.client_info.ClientInfo() + + with mock.patch.object( + transports.HostProjectRegistrationServiceTransport, "_prep_wrapped_messages" + ) as prep: + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + client_info=client_info, + ) + prep.assert_called_once_with(client_info) + + with mock.patch.object( + transports.HostProjectRegistrationServiceTransport, "_prep_wrapped_messages" + ) as prep: + transport_class = HostProjectRegistrationServiceClient.get_transport_class() + transport = transport_class( + credentials=ga_credentials.AnonymousCredentials(), + client_info=client_info, + ) + prep.assert_called_once_with(client_info) + + +def test_delete_operation(transport: str = "grpc"): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # 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 = operations_pb2.DeleteOperationRequest() - response = client.get_location(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.delete_operation(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 response is the type that we expect. - assert isinstance(response, locations_pb2.Location) + assert response is None -def test_list_locations_rest_bad_request( - request_type=locations_pb2.ListLocationsRequest, -): - client = HostProjectRegistrationServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +@pytest.mark.asyncio +async def test_delete_operation_async(transport: str = "grpc_asyncio"): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - request = request_type() - request = json_format.ParseDict({"name": "projects/sample1"}, request) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_locations(request) + # 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 = operations_pb2.DeleteOperationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_operation(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 response is the type that we expect. + assert response is None -@pytest.mark.parametrize( - "request_type", - [ - locations_pb2.ListLocationsRequest, - dict, - ], -) -def test_list_locations_rest(request_type): + +def test_delete_operation_field_headers(): client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request_init = {"name": "projects/sample1"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = locations_pb2.ListLocationsResponse() + # 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 = operations_pb2.DeleteOperationRequest() + request.name = "locations" - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + call.return_value = None - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.delete_operation(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 - response = client.list_locations(request) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] - # Establish that the response is the type that we expect. - assert isinstance(response, locations_pb2.ListLocationsResponse) +@pytest.mark.asyncio +async def test_delete_operation_field_headers_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) -def test_cancel_operation_rest_bad_request( - request_type=operations_pb2.CancelOperationRequest, -): + # 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 = operations_pb2.DeleteOperationRequest() + request.name = "locations" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_operation(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", + "name=locations", + ) in kw["metadata"] + + +def test_delete_operation_from_dict(): client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + + response = client.delete_operation( + request={ + "name": "locations", + } + ) + call.assert_called() - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.cancel_operation(request) +@pytest.mark.asyncio +async def test_delete_operation_from_dict_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.CancelOperationRequest, - dict, - ], -) -def test_cancel_operation_rest(request_type): + +def test_cancel_operation(transport: str = "grpc"): client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = None - - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "{}" - response_value.content = json_return_value.encode("UTF-8") - - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # 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 = operations_pb2.CancelOperationRequest() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None response = client.cancel_operation(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 response is the type that we expect. assert response is None -def test_delete_operation_rest_bad_request( - request_type=operations_pb2.DeleteOperationRequest, -): - client = HostProjectRegistrationServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request +@pytest.mark.asyncio +async def test_cancel_operation_async(transport: str = "grpc_asyncio"): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_operation(request) + # 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 = operations_pb2.CancelOperationRequest() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.cancel_operation(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 -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.DeleteOperationRequest, - dict, - ], -) -def test_delete_operation_rest(request_type): + # Establish that the response is the type that we expect. + assert response is None + + +def test_cancel_operation_field_headers(): client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = None + # 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 = operations_pb2.CancelOperationRequest() + request.name = "locations" - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "{}" - response_value.content = json_return_value.encode("UTF-8") + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + call.return_value = None - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.cancel_operation(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 - response = client.delete_operation(request) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] - # Establish that the response is the type that we expect. - assert response is None +@pytest.mark.asyncio +async def test_cancel_operation_field_headers_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) -def test_get_operation_rest_bad_request( - request_type=operations_pb2.GetOperationRequest, -): + # 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 = operations_pb2.CancelOperationRequest() + request.name = "locations" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.cancel_operation(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", + "name=locations", + ) in kw["metadata"] + + +def test_cancel_operation_from_dict(): client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + + response = client.cancel_operation( + request={ + "name": "locations", + } + ) + call.assert_called() - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_operation(request) + +@pytest.mark.asyncio +async def test_cancel_operation_from_dict_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.cancel_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.GetOperationRequest, - dict, - ], -) -def test_get_operation_rest(request_type): +def test_get_operation(transport: str = "grpc"): client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation() + # 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 = operations_pb2.GetOperationRequest() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation() + response = client.get_operation(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 response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) + + +@pytest.mark.asyncio +async def test_get_operation_async(transport: str = "grpc_asyncio"): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # 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 = operations_pb2.GetOperationRequest() - response = client.get_operation(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + response = await client.get_operation(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 response is the type that we expect. assert isinstance(response, operations_pb2.Operation) -def test_list_operations_rest_bad_request( - request_type=operations_pb2.ListOperationsRequest, -): +def test_get_operation_field_headers(): client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2"}, request ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_operations(request) + # 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 = operations_pb2.GetOperationRequest() + request.name = "locations" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + call.return_value = operations_pb2.Operation() -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.ListOperationsRequest, - dict, - ], -) -def test_list_operations_rest(request_type): - client = HostProjectRegistrationServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + client.get_operation(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 - request_init = {"name": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.ListOperationsResponse() + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +@pytest.mark.asyncio +async def test_get_operation_field_headers_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) - response = client.list_operations(request) + # 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 = operations_pb2.GetOperationRequest() + request.name = "locations" - # Establish that the response is the type that we expect. - assert isinstance(response, operations_pb2.ListOperationsResponse) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + await client.get_operation(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", + "name=locations", + ) in kw["metadata"] -def test_initialize_client_w_rest(): + +def test_get_operation_from_dict(): client = HostProjectRegistrationServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), ) - assert client is not None + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation() + + response = client.get_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_create_host_project_registration_empty_call_rest(): +@pytest.mark.asyncio +async def test_get_operation_from_dict_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + response = await client.get_operation( + request={ + "name": "locations", + } + ) + call.assert_called() + + +def test_list_operations(transport: str = "grpc"): client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.create_host_project_registration), "__call__" - ) as call: - client.create_host_project_registration(request=None) + # 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 = operations_pb2.ListOperationsRequest() - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.ListOperationsResponse() + response = client.list_operations(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = ( - host_project_registration_service.CreateHostProjectRegistrationRequest() - ) + assert args[0] == request - assert args[0] == request_msg + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_get_host_project_registration_empty_call_rest(): - client = HostProjectRegistrationServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +@pytest.mark.asyncio +async def test_list_operations_async(transport: str = "grpc_asyncio"): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.get_host_project_registration), "__call__" - ) as call: - client.get_host_project_registration(request=None) + # 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 = operations_pb2.ListOperationsRequest() - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = ( - host_project_registration_service.GetHostProjectRegistrationRequest() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() ) + response = await client.list_operations(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 - assert args[0] == request_msg + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_list_host_project_registrations_empty_call_rest(): +def test_list_operations_field_headers(): client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.list_host_project_registrations), "__call__" - ) as call: - client.list_host_project_registrations(request=None) + # 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 = operations_pb2.ListOperationsRequest() + request.name = "locations" - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + call.return_value = operations_pb2.ListOperationsResponse() + + client.list_operations(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = ( - host_project_registration_service.ListHostProjectRegistrationsRequest() - ) + assert args[0] == request - assert args[0] == request_msg + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -def test_host_project_registration_service_base_transport_error(): - # Passing both a credentials object and credentials_file should raise an error - with pytest.raises(core_exceptions.DuplicateCredentialArgs): - transport = transports.HostProjectRegistrationServiceTransport( - credentials=ga_credentials.AnonymousCredentials(), - credentials_file="credentials.json", - ) +@pytest.mark.asyncio +async def test_list_operations_field_headers_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + # 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 = operations_pb2.ListOperationsRequest() + request.name = "locations" -def test_host_project_registration_service_base_transport(): - # Instantiate the base transport. - with mock.patch( - "google.cloud.apihub_v1.services.host_project_registration_service.transports.HostProjectRegistrationServiceTransport.__init__" - ) as Transport: - Transport.return_value = None - transport = transports.HostProjectRegistrationServiceTransport( - credentials=ga_credentials.AnonymousCredentials(), + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() ) + await client.list_operations(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 - # Every method on the transport should just blindly - # raise NotImplementedError. - methods = ( - "create_host_project_registration", - "get_host_project_registration", - "list_host_project_registrations", - "get_location", - "list_locations", - "get_operation", - "cancel_operation", - "delete_operation", - "list_operations", - ) - for method in methods: - with pytest.raises(NotImplementedError): - getattr(transport, method)(request=object()) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] - with pytest.raises(NotImplementedError): - transport.close() - # Catch all for all remaining methods and properties - remainder = [ - "kind", - ] - for r in remainder: - with pytest.raises(NotImplementedError): - getattr(transport, r)() +def test_list_operations_from_dict(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.ListOperationsResponse() + + response = client.list_operations( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_host_project_registration_service_base_transport_with_credentials_file(): - # Instantiate the base transport with a credentials file - with mock.patch.object( - google.auth, "load_credentials_from_file", autospec=True - ) as load_creds, mock.patch( - "google.cloud.apihub_v1.services.host_project_registration_service.transports.HostProjectRegistrationServiceTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None - load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) - transport = transports.HostProjectRegistrationServiceTransport( - credentials_file="credentials.json", - quota_project_id="octopus", +@pytest.mark.asyncio +async def test_list_operations_from_dict_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() ) - load_creds.assert_called_once_with( - "credentials.json", - scopes=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id="octopus", + response = await client.list_operations( + request={ + "name": "locations", + } ) + call.assert_called() -def test_host_project_registration_service_base_transport_with_adc(): - # Test the default credentials are used if credentials and credentials_file are None. - with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch( - "google.cloud.apihub_v1.services.host_project_registration_service.transports.HostProjectRegistrationServiceTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport = transports.HostProjectRegistrationServiceTransport() - adc.assert_called_once() +def test_list_locations(transport: str = "grpc"): + client = HostProjectRegistrationServiceClient( + credentials=ga_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 = locations_pb2.ListLocationsRequest() -def test_host_project_registration_service_auth_adc(): - # If no credentials are provided, we should use ADC credentials. - with mock.patch.object(google.auth, "default", autospec=True) as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - HostProjectRegistrationServiceClient() - adc.assert_called_once_with( - scopes=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id=None, - ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.ListLocationsResponse() + response = client.list_locations(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 response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) -def test_host_project_registration_service_http_transport_client_cert_source_for_mtls(): - cred = ga_credentials.AnonymousCredentials() - with mock.patch( - "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" - ) as mock_configure_mtls_channel: - transports.HostProjectRegistrationServiceRestTransport( - credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + +@pytest.mark.asyncio +async def test_list_locations_async(transport: str = "grpc_asyncio"): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + 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 = locations_pb2.ListLocationsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() ) - mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + response = await client.list_locations(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 response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_host_project_registration_service_host_no_port(transport_name): +def test_list_locations_field_headers(): client = HostProjectRegistrationServiceClient( credentials=ga_credentials.AnonymousCredentials(), - client_options=client_options.ClientOptions( - api_endpoint="apihub.googleapis.com" - ), - transport=transport_name, - ) - assert client.transport._host == ( - "apihub.googleapis.com:443" - if transport_name in ["grpc", "grpc_asyncio"] - else "https://apihub.googleapis.com" ) + # 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 = locations_pb2.ListLocationsRequest() + request.name = "locations" -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_host_project_registration_service_host_with_port(transport_name): - client = HostProjectRegistrationServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - client_options=client_options.ClientOptions( - api_endpoint="apihub.googleapis.com:8000" - ), - transport=transport_name, - ) - assert client.transport._host == ( - "apihub.googleapis.com:8000" - if transport_name in ["grpc", "grpc_asyncio"] - else "https://apihub.googleapis.com:8000" - ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + call.return_value = locations_pb2.ListLocationsResponse() + client.list_locations(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 -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_host_project_registration_service_client_transport_session_collision( - transport_name, -): - creds1 = ga_credentials.AnonymousCredentials() - creds2 = ga_credentials.AnonymousCredentials() - client1 = HostProjectRegistrationServiceClient( - credentials=creds1, - transport=transport_name, - ) - client2 = HostProjectRegistrationServiceClient( - credentials=creds2, - transport=transport_name, - ) - session1 = client1.transport.create_host_project_registration._session - session2 = client2.transport.create_host_project_registration._session - assert session1 != session2 - session1 = client1.transport.get_host_project_registration._session - session2 = client2.transport.get_host_project_registration._session - assert session1 != session2 - session1 = client1.transport.list_host_project_registrations._session - session2 = client2.transport.list_host_project_registrations._session - assert session1 != session2 + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -def test_host_project_registration_path(): - project = "squid" - location = "clam" - host_project_registration = "whelk" - expected = "projects/{project}/locations/{location}/hostProjectRegistrations/{host_project_registration}".format( - project=project, - location=location, - host_project_registration=host_project_registration, +@pytest.mark.asyncio +async def test_list_locations_field_headers_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), ) - actual = HostProjectRegistrationServiceClient.host_project_registration_path( - project, location, host_project_registration - ) - assert expected == actual + # 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 = locations_pb2.ListLocationsRequest() + request.name = "locations" -def test_parse_host_project_registration_path(): - expected = { - "project": "octopus", - "location": "oyster", - "host_project_registration": "nudibranch", - } - path = HostProjectRegistrationServiceClient.host_project_registration_path( - **expected - ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + await client.list_locations(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 - # Check that the path construction is reversible. - actual = HostProjectRegistrationServiceClient.parse_host_project_registration_path( - path - ) - assert expected == actual + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -def test_common_billing_account_path(): - billing_account = "cuttlefish" - expected = "billingAccounts/{billing_account}".format( - billing_account=billing_account, - ) - actual = HostProjectRegistrationServiceClient.common_billing_account_path( - billing_account +def test_list_locations_from_dict(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), ) - assert expected == actual - + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.ListLocationsResponse() + + response = client.list_locations( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_parse_common_billing_account_path(): - expected = { - "billing_account": "mussel", - } - path = HostProjectRegistrationServiceClient.common_billing_account_path(**expected) - # Check that the path construction is reversible. - actual = HostProjectRegistrationServiceClient.parse_common_billing_account_path( - path +@pytest.mark.asyncio +async def test_list_locations_from_dict_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), ) - assert expected == actual + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + response = await client.list_locations( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_common_folder_path(): - folder = "winkle" - expected = "folders/{folder}".format( - folder=folder, +def test_get_location(transport: str = "grpc"): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - actual = HostProjectRegistrationServiceClient.common_folder_path(folder) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() -def test_parse_common_folder_path(): - expected = { - "folder": "nautilus", - } - path = HostProjectRegistrationServiceClient.common_folder_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.Location() + response = client.get_location(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 - # Check that the path construction is reversible. - actual = HostProjectRegistrationServiceClient.parse_common_folder_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) -def test_common_organization_path(): - organization = "scallop" - expected = "organizations/{organization}".format( - organization=organization, +@pytest.mark.asyncio +async def test_get_location_async(transport: str = "grpc_asyncio"): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - actual = HostProjectRegistrationServiceClient.common_organization_path(organization) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() -def test_parse_common_organization_path(): - expected = { - "organization": "abalone", - } - path = HostProjectRegistrationServiceClient.common_organization_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + response = await client.get_location(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 - # Check that the path construction is reversible. - actual = HostProjectRegistrationServiceClient.parse_common_organization_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) -def test_common_project_path(): - project = "squid" - expected = "projects/{project}".format( - project=project, +def test_get_location_field_headers(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials() ) - actual = HostProjectRegistrationServiceClient.common_project_path(project) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() + request.name = "locations/abc" -def test_parse_common_project_path(): - expected = { - "project": "clam", - } - path = HostProjectRegistrationServiceClient.common_project_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + call.return_value = locations_pb2.Location() - # Check that the path construction is reversible. - actual = HostProjectRegistrationServiceClient.parse_common_project_path(path) - assert expected == actual + client.get_location(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", + "name=locations/abc", + ) in kw["metadata"] -def test_common_location_path(): - project = "whelk" - location = "octopus" - expected = "projects/{project}/locations/{location}".format( - project=project, - location=location, - ) - actual = HostProjectRegistrationServiceClient.common_location_path( - project, location + +@pytest.mark.asyncio +async def test_get_location_field_headers_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials() ) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() + request.name = "locations/abc" -def test_parse_common_location_path(): - expected = { - "project": "oyster", - "location": "nudibranch", - } - path = HostProjectRegistrationServiceClient.common_location_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + await client.get_location(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 - # Check that the path construction is reversible. - actual = HostProjectRegistrationServiceClient.parse_common_location_path(path) - assert expected == actual + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations/abc", + ) in kw["metadata"] -def test_client_with_default_client_info(): - client_info = gapic_v1.client_info.ClientInfo() +def test_get_location_from_dict(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.Location() + + response = client.get_location( + request={ + "name": "locations/abc", + } + ) + call.assert_called() - with mock.patch.object( - transports.HostProjectRegistrationServiceTransport, "_prep_wrapped_messages" - ) as prep: - client = HostProjectRegistrationServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - client_info=client_info, + +@pytest.mark.asyncio +async def test_get_location_from_dict_async(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() ) - prep.assert_called_once_with(client_info) + response = await client.get_location( + request={ + "name": "locations", + } + ) + call.assert_called() + +def test_transport_close_grpc(): + client = HostProjectRegistrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) with mock.patch.object( - transports.HostProjectRegistrationServiceTransport, "_prep_wrapped_messages" - ) as prep: - transport_class = HostProjectRegistrationServiceClient.get_transport_class() - transport = transport_class( - credentials=ga_credentials.AnonymousCredentials(), - client_info=client_info, - ) - prep.assert_called_once_with(client_info) + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + +@pytest.mark.asyncio +async def test_transport_close_grpc_asyncio(): + client = HostProjectRegistrationServiceAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) + with mock.patch.object( + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + async with client: + close.assert_not_called() + close.assert_called_once() def test_transport_close_rest(): @@ -3127,6 +6004,7 @@ def test_transport_close_rest(): def test_client_ctx(): transports = [ "rest", + "grpc", ] for transport in transports: client = HostProjectRegistrationServiceClient( @@ -3145,7 +6023,11 @@ def test_client_ctx(): [ ( HostProjectRegistrationServiceClient, - transports.HostProjectRegistrationServiceRestTransport, + transports.HostProjectRegistrationServiceGrpcTransport, + ), + ( + HostProjectRegistrationServiceAsyncClient, + transports.HostProjectRegistrationServiceGrpcAsyncIOTransport, ), ], ) diff --git a/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_linting_service.py b/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_linting_service.py index 30f20044a86e..5f431ab38f52 100644 --- a/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_linting_service.py +++ b/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_linting_service.py @@ -56,6 +56,7 @@ from google.protobuf import field_mask_pb2 # type: ignore from google.cloud.apihub_v1.services.linting_service import ( + LintingServiceAsyncClient, LintingServiceClient, transports, ) @@ -240,6 +241,11 @@ def test__get_client_cert_source(): "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(LintingServiceClient), ) +@mock.patch.object( + LintingServiceAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(LintingServiceAsyncClient), +) def test__get_api_endpoint(): api_override = "foo.com" mock_client_cert_source = mock.Mock() @@ -367,6 +373,8 @@ def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code): @pytest.mark.parametrize( "client_class,transport_name", [ + (LintingServiceClient, "grpc"), + (LintingServiceAsyncClient, "grpc_asyncio"), (LintingServiceClient, "rest"), ], ) @@ -391,6 +399,8 @@ def test_linting_service_client_from_service_account_info(client_class, transpor @pytest.mark.parametrize( "transport_class,transport_name", [ + (transports.LintingServiceGrpcTransport, "grpc"), + (transports.LintingServiceGrpcAsyncIOTransport, "grpc_asyncio"), (transports.LintingServiceRestTransport, "rest"), ], ) @@ -415,6 +425,8 @@ def test_linting_service_client_service_account_always_use_jwt( @pytest.mark.parametrize( "client_class,transport_name", [ + (LintingServiceClient, "grpc"), + (LintingServiceAsyncClient, "grpc_asyncio"), (LintingServiceClient, "rest"), ], ) @@ -446,17 +458,24 @@ def test_linting_service_client_from_service_account_file(client_class, transpor def test_linting_service_client_get_transport_class(): transport = LintingServiceClient.get_transport_class() available_transports = [ + transports.LintingServiceGrpcTransport, transports.LintingServiceRestTransport, ] assert transport in available_transports - transport = LintingServiceClient.get_transport_class("rest") - assert transport == transports.LintingServiceRestTransport + transport = LintingServiceClient.get_transport_class("grpc") + assert transport == transports.LintingServiceGrpcTransport @pytest.mark.parametrize( "client_class,transport_class,transport_name", [ + (LintingServiceClient, transports.LintingServiceGrpcTransport, "grpc"), + ( + LintingServiceAsyncClient, + transports.LintingServiceGrpcAsyncIOTransport, + "grpc_asyncio", + ), (LintingServiceClient, transports.LintingServiceRestTransport, "rest"), ], ) @@ -465,6 +484,11 @@ def test_linting_service_client_get_transport_class(): "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(LintingServiceClient), ) +@mock.patch.object( + LintingServiceAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(LintingServiceAsyncClient), +) def test_linting_service_client_client_options( client_class, transport_class, transport_name ): @@ -598,6 +622,20 @@ def test_linting_service_client_client_options( @pytest.mark.parametrize( "client_class,transport_class,transport_name,use_client_cert_env", [ + (LintingServiceClient, transports.LintingServiceGrpcTransport, "grpc", "true"), + ( + LintingServiceAsyncClient, + transports.LintingServiceGrpcAsyncIOTransport, + "grpc_asyncio", + "true", + ), + (LintingServiceClient, transports.LintingServiceGrpcTransport, "grpc", "false"), + ( + LintingServiceAsyncClient, + transports.LintingServiceGrpcAsyncIOTransport, + "grpc_asyncio", + "false", + ), (LintingServiceClient, transports.LintingServiceRestTransport, "rest", "true"), (LintingServiceClient, transports.LintingServiceRestTransport, "rest", "false"), ], @@ -607,6 +645,11 @@ def test_linting_service_client_client_options( "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(LintingServiceClient), ) +@mock.patch.object( + LintingServiceAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(LintingServiceAsyncClient), +) @mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) def test_linting_service_client_mtls_env_auto( client_class, transport_class, transport_name, use_client_cert_env @@ -710,12 +753,19 @@ def test_linting_service_client_mtls_env_auto( ) -@pytest.mark.parametrize("client_class", [LintingServiceClient]) +@pytest.mark.parametrize( + "client_class", [LintingServiceClient, LintingServiceAsyncClient] +) @mock.patch.object( LintingServiceClient, "DEFAULT_ENDPOINT", modify_default_endpoint(LintingServiceClient), ) +@mock.patch.object( + LintingServiceAsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(LintingServiceAsyncClient), +) def test_linting_service_client_get_mtls_endpoint_and_cert_source(client_class): mock_client_cert_source = mock.Mock() @@ -807,12 +857,19 @@ def test_linting_service_client_get_mtls_endpoint_and_cert_source(client_class): ) -@pytest.mark.parametrize("client_class", [LintingServiceClient]) +@pytest.mark.parametrize( + "client_class", [LintingServiceClient, LintingServiceAsyncClient] +) @mock.patch.object( LintingServiceClient, "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(LintingServiceClient), ) +@mock.patch.object( + LintingServiceAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(LintingServiceAsyncClient), +) def test_linting_service_client_client_api_endpoint(client_class): mock_client_cert_source = client_cert_source_callback api_override = "foo.com" @@ -889,6 +946,12 @@ def test_linting_service_client_client_api_endpoint(client_class): @pytest.mark.parametrize( "client_class,transport_class,transport_name", [ + (LintingServiceClient, transports.LintingServiceGrpcTransport, "grpc"), + ( + LintingServiceAsyncClient, + transports.LintingServiceGrpcAsyncIOTransport, + "grpc_asyncio", + ), (LintingServiceClient, transports.LintingServiceRestTransport, "rest"), ], ) @@ -920,6 +983,18 @@ def test_linting_service_client_client_options_scopes( @pytest.mark.parametrize( "client_class,transport_class,transport_name,grpc_helpers", [ + ( + LintingServiceClient, + transports.LintingServiceGrpcTransport, + "grpc", + grpc_helpers, + ), + ( + LintingServiceAsyncClient, + transports.LintingServiceGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), (LintingServiceClient, transports.LintingServiceRestTransport, "rest", None), ], ) @@ -947,13 +1022,169 @@ def test_linting_service_client_client_options_credentials_file( ) -def test_get_style_guide_rest_use_cached_wrapped_rpc(): +def test_linting_service_client_client_options_from_dict(): + with mock.patch( + "google.cloud.apihub_v1.services.linting_service.transports.LintingServiceGrpcTransport.__init__" + ) as grpc_transport: + grpc_transport.return_value = None + client = LintingServiceClient( + client_options={"api_endpoint": "squid.clam.whelk"} + ) + grpc_transport.assert_called_once_with( + credentials=None, + credentials_file=None, + host="squid.clam.whelk", + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name,grpc_helpers", + [ + ( + LintingServiceClient, + transports.LintingServiceGrpcTransport, + "grpc", + grpc_helpers, + ), + ( + LintingServiceAsyncClient, + transports.LintingServiceGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), + ], +) +def test_linting_service_client_create_channel_credentials_file( + client_class, transport_class, transport_name, grpc_helpers +): + # Check the case credentials file is provided. + options = client_options.ClientOptions(credentials_file="credentials.json") + + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options, transport=transport_name) + patched.assert_called_once_with( + credentials=None, + credentials_file="credentials.json", + host=client._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE + ), + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + # test that the credentials from file are saved and used as the credentials. + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel" + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + file_creds = ga_credentials.AnonymousCredentials() + load_creds.return_value = (file_creds, None) + adc.return_value = (creds, None) + client = client_class(client_options=options, transport=transport_name) + create_channel.assert_called_with( + "apihub.googleapis.com:443", + credentials=file_creds, + credentials_file=None, + quota_project_id=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + scopes=None, + default_host="apihub.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + +@pytest.mark.parametrize( + "request_type", + [ + linting_service.GetStyleGuideRequest, + dict, + ], +) +def test_get_style_guide(request_type, transport: str = "grpc"): + client = LintingServiceClient( + credentials=ga_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.get_style_guide), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = linting_service.StyleGuide( + name="name_value", + linter=common_fields.Linter.SPECTRAL, + ) + response = client.get_style_guide(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = linting_service.GetStyleGuideRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, linting_service.StyleGuide) + assert response.name == "name_value" + assert response.linter == common_fields.Linter.SPECTRAL + + +def test_get_style_guide_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = linting_service.GetStyleGuideRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_style_guide), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_style_guide(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == linting_service.GetStyleGuideRequest( + name="name_value", + ) + + +def test_get_style_guide_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -969,7 +1200,6 @@ def test_get_style_guide_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.get_style_guide] = mock_rpc - request = {} client.get_style_guide(request) @@ -983,157 +1213,305 @@ def test_get_style_guide_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_get_style_guide_rest_required_fields( - request_type=linting_service.GetStyleGuideRequest, +@pytest.mark.asyncio +async def test_get_style_guide_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.LintingServiceRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.get_style_guide + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.get_style_guide + ] = mock_rpc + + request = {} + await client.get_style_guide(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.get_style_guide(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_get_style_guide_async( + transport: str = "grpc_asyncio", request_type=linting_service.GetStyleGuideRequest +): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # verify fields with default values are dropped + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_style_guide._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_style_guide), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + linting_service.StyleGuide( + name="name_value", + linter=common_fields.Linter.SPECTRAL, + ) + ) + response = await client.get_style_guide(request) - # verify required fields with default values are now present + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = linting_service.GetStyleGuideRequest() + assert args[0] == request - jsonified_request["name"] = "name_value" + # Establish that the response is the type that we expect. + assert isinstance(response, linting_service.StyleGuide) + assert response.name == "name_value" + assert response.linter == common_fields.Linter.SPECTRAL - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_style_guide._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" +@pytest.mark.asyncio +async def test_get_style_guide_async_from_dict(): + await test_get_style_guide_async(request_type=dict) + +def test_get_style_guide_field_headers(): client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = linting_service.StyleGuide() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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 = linting_service.GetStyleGuideRequest() - # Convert return value to protobuf type - return_value = linting_service.StyleGuide.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + request.name = "name_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_style_guide), "__call__") as call: + call.return_value = linting_service.StyleGuide() + client.get_style_guide(request) - response = client.get_style_guide(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] -def test_get_style_guide_rest_unset_required_fields(): - transport = transports.LintingServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_get_style_guide_field_headers_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.get_style_guide._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # 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 = linting_service.GetStyleGuideRequest() + request.name = "name_value" -def test_get_style_guide_rest_flattened(): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_style_guide), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + linting_service.StyleGuide() + ) + await client.get_style_guide(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", + "name=name_value", + ) in kw["metadata"] + + +def test_get_style_guide_flattened(): client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = linting_service.StyleGuide() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_style_guide), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = linting_service.StyleGuide() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_style_guide( + name="name_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/plugins/sample3/styleGuide" - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_get_style_guide_flattened_error(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_style_guide( + linting_service.GetStyleGuideRequest(), name="name_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = linting_service.StyleGuide.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_style_guide(**mock_args) +@pytest.mark.asyncio +async def test_get_style_guide_flattened_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_style_guide), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = linting_service.StyleGuide() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + linting_service.StyleGuide() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_style_guide( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/plugins/*/styleGuide}" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_get_style_guide_rest_flattened_error(transport: str = "rest"): - client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_get_style_guide_flattened_error_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.get_style_guide( + await client.get_style_guide( linting_service.GetStyleGuideRequest(), name="name_value", ) -def test_update_style_guide_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + linting_service.UpdateStyleGuideRequest, + dict, + ], +) +def test_update_style_guide(request_type, transport: str = "grpc"): + client = LintingServiceClient( + credentials=ga_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.update_style_guide), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = linting_service.StyleGuide( + name="name_value", + linter=common_fields.Linter.SPECTRAL, + ) + response = client.update_style_guide(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = linting_service.UpdateStyleGuideRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, linting_service.StyleGuide) + assert response.name == "name_value" + assert response.linter == common_fields.Linter.SPECTRAL + + +def test_update_style_guide_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = linting_service.UpdateStyleGuideRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_style_guide), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.update_style_guide(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == linting_service.UpdateStyleGuideRequest() + + +def test_update_style_guide_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1153,7 +1531,6 @@ def test_update_style_guide_rest_use_cached_wrapped_rpc(): client._transport._wrapped_methods[ client._transport.update_style_guide ] = mock_rpc - request = {} client.update_style_guide(request) @@ -1167,159 +1544,330 @@ def test_update_style_guide_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_update_style_guide_rest_required_fields( - request_type=linting_service.UpdateStyleGuideRequest, +@pytest.mark.asyncio +async def test_update_style_guide_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.LintingServiceRestTransport - - request_init = {} - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - # verify fields with default values are dropped + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_style_guide._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Ensure method has been cached + assert ( + client._client._transport.update_style_guide + in client._client._transport._wrapped_methods + ) - # verify required fields with default values are now present + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.update_style_guide + ] = mock_rpc - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).update_style_guide._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("update_mask",)) - jsonified_request.update(unset_fields) + request = {} + await client.update_style_guide(request) - # verify required fields with non-default values are left alone + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type(**request_init) + await client.update_style_guide(request) - # Designate an appropriate value for the returned response. - return_value = linting_service.StyleGuide() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "patch", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = linting_service.StyleGuide.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) +@pytest.mark.asyncio +async def test_update_style_guide_async( + transport: str = "grpc_asyncio", + request_type=linting_service.UpdateStyleGuideRequest, +): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # 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() - response = client.update_style_guide(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_style_guide), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + linting_service.StyleGuide( + name="name_value", + linter=common_fields.Linter.SPECTRAL, + ) + ) + response = await client.update_style_guide(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = linting_service.UpdateStyleGuideRequest() + assert args[0] == request + # Establish that the response is the type that we expect. + assert isinstance(response, linting_service.StyleGuide) + assert response.name == "name_value" + assert response.linter == common_fields.Linter.SPECTRAL -def test_update_style_guide_rest_unset_required_fields(): - transport = transports.LintingServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials - ) - unset_fields = transport.update_style_guide._get_unset_required_fields({}) - assert set(unset_fields) == (set(("updateMask",)) & set(("styleGuide",))) +@pytest.mark.asyncio +async def test_update_style_guide_async_from_dict(): + await test_update_style_guide_async(request_type=dict) -def test_update_style_guide_rest_flattened(): +def test_update_style_guide_field_headers(): client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = linting_service.StyleGuide() + # 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 = linting_service.UpdateStyleGuideRequest() - # get arguments that satisfy an http rule for this method - sample_request = { - "style_guide": { - "name": "projects/sample1/locations/sample2/plugins/sample3/styleGuide" - } - } + request.style_guide.name = "name_value" - # get truthy value for each flattened field - mock_args = dict( - style_guide=linting_service.StyleGuide(name="name_value"), - update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), - ) - mock_args.update(sample_request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_style_guide), "__call__" + ) as call: + call.return_value = linting_service.StyleGuide() + client.update_style_guide(request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = linting_service.StyleGuide.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request - client.update_style_guide(**mock_args) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "style_guide.name=name_value", + ) in kw["metadata"] - # Establish that the underlying call was made with the expected - # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{style_guide.name=projects/*/locations/*/plugins/*/styleGuide}" - % client.transport._host, - args[1], + +@pytest.mark.asyncio +async def test_update_style_guide_field_headers_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = linting_service.UpdateStyleGuideRequest() + + request.style_guide.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_style_guide), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + linting_service.StyleGuide() ) + await client.update_style_guide(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request -def test_update_style_guide_rest_flattened_error(transport: str = "rest"): + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "style_guide.name=name_value", + ) in kw["metadata"] + + +def test_update_style_guide_flattened(): client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.update_style_guide( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_style_guide), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = linting_service.StyleGuide() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.update_style_guide( + style_guide=linting_service.StyleGuide(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].style_guide + mock_val = linting_service.StyleGuide(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val + + +def test_update_style_guide_flattened_error(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_style_guide( linting_service.UpdateStyleGuideRequest(), style_guide=linting_service.StyleGuide(name="name_value"), update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) -def test_get_style_guide_contents_rest_use_cached_wrapped_rpc(): +@pytest.mark.asyncio +async def test_update_style_guide_flattened_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.update_style_guide), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = linting_service.StyleGuide() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + linting_service.StyleGuide() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.update_style_guide( + style_guide=linting_service.StyleGuide(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].style_guide + mock_val = linting_service.StyleGuide(name="name_value") + assert arg == mock_val + arg = args[0].update_mask + mock_val = field_mask_pb2.FieldMask(paths=["paths_value"]) + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_update_style_guide_flattened_error_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.update_style_guide( + linting_service.UpdateStyleGuideRequest(), + style_guide=linting_service.StyleGuide(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +@pytest.mark.parametrize( + "request_type", + [ + linting_service.GetStyleGuideContentsRequest, + dict, + ], +) +def test_get_style_guide_contents(request_type, transport: str = "grpc"): + client = LintingServiceClient( + credentials=ga_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.get_style_guide_contents), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = linting_service.StyleGuideContents( + contents=b"contents_blob", + mime_type="mime_type_value", + ) + response = client.get_style_guide_contents(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = linting_service.GetStyleGuideContentsRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, linting_service.StyleGuideContents) + assert response.contents == b"contents_blob" + assert response.mime_type == "mime_type_value" + + +def test_get_style_guide_contents_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = linting_service.GetStyleGuideContentsRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_style_guide_contents), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_style_guide_contents(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == linting_service.GetStyleGuideContentsRequest( + name="name_value", + ) + + +def test_get_style_guide_contents_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1340,7 +1888,6 @@ def test_get_style_guide_contents_rest_use_cached_wrapped_rpc(): client._transport._wrapped_methods[ client._transport.get_style_guide_contents ] = mock_rpc - request = {} client.get_style_guide_contents(request) @@ -1354,157 +1901,311 @@ def test_get_style_guide_contents_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_get_style_guide_contents_rest_required_fields( - request_type=linting_service.GetStyleGuideContentsRequest, +@pytest.mark.asyncio +async def test_get_style_guide_contents_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.LintingServiceRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.get_style_guide_contents + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_style_guide_contents._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.get_style_guide_contents + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.get_style_guide_contents(request) - jsonified_request["name"] = "name_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_style_guide_contents._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + await client.get_style_guide_contents(request) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + +@pytest.mark.asyncio +async def test_get_style_guide_contents_async( + transport: str = "grpc_asyncio", + request_type=linting_service.GetStyleGuideContentsRequest, +): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = linting_service.StyleGuideContents() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # 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() - response_value = Response() - response_value.status_code = 200 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_style_guide_contents), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + linting_service.StyleGuideContents( + contents=b"contents_blob", + mime_type="mime_type_value", + ) + ) + response = await client.get_style_guide_contents(request) - # Convert return value to protobuf type - return_value = linting_service.StyleGuideContents.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = linting_service.GetStyleGuideContentsRequest() + assert args[0] == request - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert isinstance(response, linting_service.StyleGuideContents) + assert response.contents == b"contents_blob" + assert response.mime_type == "mime_type_value" - response = client.get_style_guide_contents(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_get_style_guide_contents_async_from_dict(): + await test_get_style_guide_contents_async(request_type=dict) -def test_get_style_guide_contents_rest_unset_required_fields(): - transport = transports.LintingServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials +def test_get_style_guide_contents_field_headers(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = transport.get_style_guide_contents._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # 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 = linting_service.GetStyleGuideContentsRequest() + request.name = "name_value" -def test_get_style_guide_contents_rest_flattened(): - client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_style_guide_contents), "__call__" + ) as call: + call.return_value = linting_service.StyleGuideContents() + client.get_style_guide_contents(request) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = linting_service.StyleGuideContents() + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/plugins/sample3/styleGuide" - } + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] - # get truthy value for each flattened field - mock_args = dict( - name="name_value", - ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = linting_service.StyleGuideContents.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +@pytest.mark.asyncio +async def test_get_style_guide_contents_field_headers_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) - client.get_style_guide_contents(**mock_args) + # 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 = linting_service.GetStyleGuideContentsRequest() - # Establish that the underlying call was made with the expected - # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/plugins/*/styleGuide}:contents" - % client.transport._host, - args[1], + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_style_guide_contents), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + linting_service.StyleGuideContents() ) + await client.get_style_guide_contents(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", + "name=name_value", + ) in kw["metadata"] -def test_get_style_guide_contents_rest_flattened_error(transport: str = "rest"): +def test_get_style_guide_contents_flattened(): client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_style_guide_contents), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = linting_service.StyleGuideContents() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_style_guide_contents( + name="name_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +def test_get_style_guide_contents_flattened_error(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): client.get_style_guide_contents( linting_service.GetStyleGuideContentsRequest(), name="name_value", ) -def test_lint_spec_rest_use_cached_wrapped_rpc(): +@pytest.mark.asyncio +async def test_get_style_guide_contents_flattened_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_style_guide_contents), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = linting_service.StyleGuideContents() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + linting_service.StyleGuideContents() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_style_guide_contents( + name="name_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_get_style_guide_contents_flattened_error_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.get_style_guide_contents( + linting_service.GetStyleGuideContentsRequest(), + name="name_value", + ) + + +@pytest.mark.parametrize( + "request_type", + [ + linting_service.LintSpecRequest, + dict, + ], +) +def test_lint_spec(request_type, transport: str = "grpc"): + client = LintingServiceClient( + credentials=ga_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.lint_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.lint_spec(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = linting_service.LintSpecRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert response is None + + +def test_lint_spec_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = linting_service.LintSpecRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.lint_spec), "__call__") as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.lint_spec(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == linting_service.LintSpecRequest( + name="name_value", + ) + + +def test_lint_spec_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1520,7 +2221,6 @@ def test_lint_spec_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[client._transport.lint_spec] = mock_rpc - request = {} client.lint_spec(request) @@ -1534,7 +2234,178 @@ def test_lint_spec_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_lint_spec_rest_required_fields(request_type=linting_service.LintSpecRequest): +@pytest.mark.asyncio +async def test_lint_spec_async_use_cached_wrapped_rpc(transport: str = "grpc_asyncio"): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.lint_spec + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.lint_spec + ] = mock_rpc + + request = {} + await client.lint_spec(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.lint_spec(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_lint_spec_async( + transport: str = "grpc_asyncio", request_type=linting_service.LintSpecRequest +): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + 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.lint_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.lint_spec(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = linting_service.LintSpecRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert response is None + + +@pytest.mark.asyncio +async def test_lint_spec_async_from_dict(): + await test_lint_spec_async(request_type=dict) + + +def test_lint_spec_field_headers(): + client = LintingServiceClient( + credentials=ga_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 = linting_service.LintSpecRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.lint_spec), "__call__") as call: + call.return_value = None + client.lint_spec(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", + "name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_lint_spec_field_headers_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = linting_service.LintSpecRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.lint_spec), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.lint_spec(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", + "name=name_value", + ) in kw["metadata"] + + +def test_get_style_guide_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.get_style_guide in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.get_style_guide] = mock_rpc + + request = {} + client.get_style_guide(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_style_guide(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_get_style_guide_rest_required_fields( + request_type=linting_service.GetStyleGuideRequest, +): transport_class = transports.LintingServiceRestTransport request_init = {} @@ -1549,7 +2420,7 @@ def test_lint_spec_rest_required_fields(request_type=linting_service.LintSpecReq unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).lint_spec._get_unset_required_fields(jsonified_request) + ).get_style_guide._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present @@ -1558,7 +2429,7 @@ def test_lint_spec_rest_required_fields(request_type=linting_service.LintSpecReq unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).lint_spec._get_unset_required_fields(jsonified_request) + ).get_style_guide._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone @@ -1572,7 +2443,7 @@ def test_lint_spec_rest_required_fields(request_type=linting_service.LintSpecReq request = request_type(**request_init) # Designate an appropriate value for the returned response. - return_value = None + return_value = linting_service.StyleGuide() # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -1584,1528 +2455,3464 @@ def test_lint_spec_rest_required_fields(request_type=linting_service.LintSpecReq pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "post", + "method": "get", "query_params": pb_request, } - transcode_result["body"] = pb_request transcode.return_value = transcode_result response_value = Response() response_value.status_code = 200 - json_return_value = "" + + # Convert return value to protobuf type + return_value = linting_service.StyleGuide.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.lint_spec(request) + response = client.get_style_guide(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_lint_spec_rest_unset_required_fields(): +def test_get_style_guide_rest_unset_required_fields(): transport = transports.LintingServiceRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.lint_spec._get_unset_required_fields({}) + unset_fields = transport.get_style_guide._get_unset_required_fields({}) assert set(unset_fields) == (set(()) & set(("name",))) -def test_credentials_transport_error(): - # It is an error to provide credentials and a transport instance. - transport = transports.LintingServiceRestTransport( +def test_get_style_guide_rest_flattened(): + client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - with pytest.raises(ValueError): - client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - # It is an error to provide a credentials file and a transport instance. - transport = transports.LintingServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - with pytest.raises(ValueError): - client = LintingServiceClient( - client_options={"credentials_file": "credentials.json"}, - transport=transport, + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = linting_service.StyleGuide() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/plugins/sample3/styleGuide" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", ) + mock_args.update(sample_request) - # It is an error to provide an api_key and a transport instance. - transport = transports.LintingServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = LintingServiceClient( - client_options=options, - transport=transport, - ) + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = linting_service.StyleGuide.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - # It is an error to provide an api_key and a credential. - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = LintingServiceClient( - client_options=options, credentials=ga_credentials.AnonymousCredentials() + client.get_style_guide(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/plugins/*/styleGuide}" + % client.transport._host, + args[1], ) - # It is an error to provide scopes and a transport instance. - transport = transports.LintingServiceRestTransport( + +def test_get_style_guide_rest_flattened_error(transport: str = "rest"): + client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. with pytest.raises(ValueError): + client.get_style_guide( + linting_service.GetStyleGuideRequest(), + name="name_value", + ) + + +def test_update_style_guide_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = LintingServiceClient( - client_options={"scopes": ["1", "2"]}, - transport=transport, + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() -def test_transport_instance(): - # A client may be instantiated with a custom transport instance. - transport = transports.LintingServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials(), + # Ensure method has been cached + assert ( + client._transport.update_style_guide in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.update_style_guide + ] = mock_rpc + + request = {} + client.update_style_guide(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.update_style_guide(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_update_style_guide_rest_required_fields( + request_type=linting_service.UpdateStyleGuideRequest, +): + transport_class = transports.LintingServiceRestTransport + + request_init = {} + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) ) - client = LintingServiceClient(transport=transport) - assert client.transport is transport + # verify fields with default values are dropped -@pytest.mark.parametrize( - "transport_class", - [ - transports.LintingServiceRestTransport, - ], -) -def test_transport_adc(transport_class): - # Test default credentials are used if not provided. - with mock.patch.object(google.auth, "default") as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport_class() - adc.assert_called_once() + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_style_guide._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + # verify required fields with default values are now present -def test_transport_kind_rest(): - transport = LintingServiceClient.get_transport_class("rest")( + unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ) - assert transport.kind == "rest" + ).update_style_guide._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("update_mask",)) + jsonified_request.update(unset_fields) + # verify required fields with non-default values are left alone -def test_get_style_guide_rest_bad_request( - request_type=linting_service.GetStyleGuideRequest, -): client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/plugins/sample3/styleGuide" - } request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_style_guide(request) + # Designate an appropriate value for the returned response. + return_value = linting_service.StyleGuide() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + response_value = Response() + response_value.status_code = 200 -@pytest.mark.parametrize( - "request_type", - [ - linting_service.GetStyleGuideRequest, - dict, - ], -) -def test_get_style_guide_rest_call_success(request_type): - client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + # Convert return value to protobuf type + return_value = linting_service.StyleGuide.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.update_style_guide(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_style_guide_rest_unset_required_fields(): + transport = transports.LintingServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/plugins/sample3/styleGuide" - } - request = request_type(**request_init) + unset_fields = transport.update_style_guide._get_unset_required_fields({}) + assert set(unset_fields) == (set(("updateMask",)) & set(("styleGuide",))) + + +def test_update_style_guide_rest_flattened(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = linting_service.StyleGuide( - name="name_value", - linter=common_fields.Linter.SPECTRAL, + return_value = linting_service.StyleGuide() + + # get arguments that satisfy an http rule for this method + sample_request = { + "style_guide": { + "name": "projects/sample1/locations/sample2/plugins/sample3/styleGuide" + } + } + + # get truthy value for each flattened field + mock_args = dict( + style_guide=linting_service.StyleGuide(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) + mock_args.update(sample_request) # Wrap the value into a proper Response obj - response_value = mock.Mock() + response_value = Response() response_value.status_code = 200 - # Convert return value to protobuf type return_value = linting_service.StyleGuide.pb(return_value) json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_style_guide(request) - # Establish that the response is the type that we expect. - assert isinstance(response, linting_service.StyleGuide) - assert response.name == "name_value" - assert response.linter == common_fields.Linter.SPECTRAL + client.update_style_guide(**mock_args) + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{style_guide.name=projects/*/locations/*/plugins/*/styleGuide}" + % client.transport._host, + args[1], + ) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_style_guide_rest_interceptors(null_interceptor): - transport = transports.LintingServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.LintingServiceRestInterceptor(), + +def test_update_style_guide_rest_flattened_error(transport: str = "rest"): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - client = LintingServiceClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.LintingServiceRestInterceptor, "post_get_style_guide" - ) as post, mock.patch.object( - transports.LintingServiceRestInterceptor, "post_get_style_guide_with_metadata" - ) as post_with_metadata, mock.patch.object( - transports.LintingServiceRestInterceptor, "pre_get_style_guide" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = linting_service.GetStyleGuideRequest.pb( - linting_service.GetStyleGuideRequest() + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_style_guide( + linting_service.UpdateStyleGuideRequest(), + style_guide=linting_service.StyleGuide(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = linting_service.StyleGuide.to_json(linting_service.StyleGuide()) - req.return_value.content = return_value - request = linting_service.GetStyleGuideRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = linting_service.StyleGuide() - post_with_metadata.return_value = linting_service.StyleGuide(), metadata - client.get_style_guide( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], +def test_get_style_guide_contents_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + # Ensure method has been cached + assert ( + client._transport.get_style_guide_contents + in client._transport._wrapped_methods + ) -def test_update_style_guide_rest_bad_request( - request_type=linting_service.UpdateStyleGuideRequest, -): - client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = { - "style_guide": { - "name": "projects/sample1/locations/sample2/plugins/sample3/styleGuide" - } - } - request = request_type(**request_init) + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.get_style_guide_contents + ] = mock_rpc - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.update_style_guide(request) + request = {} + client.get_style_guide_contents(request) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 -@pytest.mark.parametrize( - "request_type", - [ - linting_service.UpdateStyleGuideRequest, - dict, - ], -) -def test_update_style_guide_rest_call_success(request_type): - client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) + client.get_style_guide_contents(request) - # send a request that will satisfy transcoding - request_init = { - "style_guide": { - "name": "projects/sample1/locations/sample2/plugins/sample3/styleGuide" - } - } - request_init["style_guide"] = { - "name": "projects/sample1/locations/sample2/plugins/sample3/styleGuide", - "linter": 1, - "contents": {"contents": b"contents_blob", "mime_type": "mime_type_value"}, - } - # The version of a generated dependency at test runtime may differ from the version used during generation. - # Delete any fields which are not present in the current runtime dependency - # See https://github.com/googleapis/gapic-generator-python/issues/1748 + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - # Determine if the message type is proto-plus or protobuf - test_field = linting_service.UpdateStyleGuideRequest.meta.fields["style_guide"] - def get_message_fields(field): - # Given a field which is a message (composite type), return a list with - # all the fields of the message. - # If the field is not a composite type, return an empty list. - message_fields = [] +def test_get_style_guide_contents_rest_required_fields( + request_type=linting_service.GetStyleGuideContentsRequest, +): + transport_class = transports.LintingServiceRestTransport - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields + # verify fields with default values are dropped - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_style_guide_contents._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) - subfields_not_in_runtime = [] + # verify required fields with default values are now present - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["style_guide"].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value + jsonified_request["name"] = "name_value" - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_style_guide_contents._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range(0, len(request_init["style_guide"][field])): - del request_init["style_guide"][field][i][subfield] - else: - del request_init["style_guide"][field][subfield] + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) request = request_type(**request_init) + # Designate an appropriate value for the returned response. + return_value = linting_service.StyleGuideContents() # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = linting_service.StyleGuide( - name="name_value", - linter=common_fields.Linter.SPECTRAL, - ) + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 + response_value = Response() + response_value.status_code = 200 - # Convert return value to protobuf type - return_value = linting_service.StyleGuide.pb(return_value) + # Convert return value to protobuf type + return_value = linting_service.StyleGuideContents.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_style_guide_contents(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_style_guide_contents_rest_unset_required_fields(): + transport = transports.LintingServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_style_guide_contents._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_get_style_guide_contents_rest_flattened(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = linting_service.StyleGuideContents() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/plugins/sample3/styleGuide" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = linting_service.StyleGuideContents.pb(return_value) json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.update_style_guide(request) - # Establish that the response is the type that we expect. - assert isinstance(response, linting_service.StyleGuide) - assert response.name == "name_value" - assert response.linter == common_fields.Linter.SPECTRAL + client.get_style_guide_contents(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/plugins/*/styleGuide}:contents" + % client.transport._host, + args[1], + ) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_update_style_guide_rest_interceptors(null_interceptor): - transport = transports.LintingServiceRestTransport( +def test_get_style_guide_contents_rest_flattened_error(transport: str = "rest"): + client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.LintingServiceRestInterceptor(), + transport=transport, ) - client = LintingServiceClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.LintingServiceRestInterceptor, "post_update_style_guide" - ) as post, mock.patch.object( - transports.LintingServiceRestInterceptor, - "post_update_style_guide_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.LintingServiceRestInterceptor, "pre_update_style_guide" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = linting_service.UpdateStyleGuideRequest.pb( - linting_service.UpdateStyleGuideRequest() + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_style_guide_contents( + linting_service.GetStyleGuideContentsRequest(), + name="name_value", ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = linting_service.StyleGuide.to_json(linting_service.StyleGuide()) - req.return_value.content = return_value - request = linting_service.UpdateStyleGuideRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = linting_service.StyleGuide() - post_with_metadata.return_value = linting_service.StyleGuide(), metadata +def test_lint_spec_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) - client.update_style_guide( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.lint_spec in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. ) + client._transport._wrapped_methods[client._transport.lint_spec] = mock_rpc - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + request = {} + client.lint_spec(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.lint_spec(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_lint_spec_rest_required_fields(request_type=linting_service.LintSpecRequest): + transport_class = transports.LintingServiceRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).lint_spec._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).lint_spec._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = None + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.lint_spec(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_lint_spec_rest_unset_required_fields(): + transport = transports.LintingServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.lint_spec._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_credentials_transport_error(): + # It is an error to provide credentials and a transport instance. + transport = transports.LintingServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # It is an error to provide a credentials file and a transport instance. + transport = transports.LintingServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = LintingServiceClient( + client_options={"credentials_file": "credentials.json"}, + transport=transport, + ) + + # It is an error to provide an api_key and a transport instance. + transport = transports.LintingServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = LintingServiceClient( + client_options=options, + transport=transport, + ) + + # It is an error to provide an api_key and a credential. + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = LintingServiceClient( + client_options=options, credentials=ga_credentials.AnonymousCredentials() + ) + + # It is an error to provide scopes and a transport instance. + transport = transports.LintingServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = LintingServiceClient( + client_options={"scopes": ["1", "2"]}, + transport=transport, + ) + + +def test_transport_instance(): + # A client may be instantiated with a custom transport instance. + transport = transports.LintingServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + client = LintingServiceClient(transport=transport) + assert client.transport is transport + + +def test_transport_get_channel(): + # A client may be instantiated with a custom transport instance. + transport = transports.LintingServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + transport = transports.LintingServiceGrpcAsyncIOTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.LintingServiceGrpcTransport, + transports.LintingServiceGrpcAsyncIOTransport, + transports.LintingServiceRestTransport, + ], +) +def test_transport_adc(transport_class): + # Test default credentials are used if not provided. + with mock.patch.object(google.auth, "default") as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class() + adc.assert_called_once() + + +def test_transport_kind_grpc(): + transport = LintingServiceClient.get_transport_class("grpc")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "grpc" + + +def test_initialize_client_w_grpc(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_style_guide_empty_call_grpc(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_style_guide), "__call__") as call: + call.return_value = linting_service.StyleGuide() + client.get_style_guide(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = linting_service.GetStyleGuideRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_update_style_guide_empty_call_grpc(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.update_style_guide), "__call__" + ) as call: + call.return_value = linting_service.StyleGuide() + client.update_style_guide(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = linting_service.UpdateStyleGuideRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_style_guide_contents_empty_call_grpc(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_style_guide_contents), "__call__" + ) as call: + call.return_value = linting_service.StyleGuideContents() + client.get_style_guide_contents(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = linting_service.GetStyleGuideContentsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_lint_spec_empty_call_grpc(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.lint_spec), "__call__") as call: + call.return_value = None + client.lint_spec(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = linting_service.LintSpecRequest() + + assert args[0] == request_msg + + +def test_transport_kind_grpc_asyncio(): + transport = LintingServiceAsyncClient.get_transport_class("grpc_asyncio")( + credentials=async_anonymous_credentials() + ) + assert transport.kind == "grpc_asyncio" + + +def test_initialize_client_w_grpc_asyncio(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_style_guide_empty_call_grpc_asyncio(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_style_guide), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + linting_service.StyleGuide( + name="name_value", + linter=common_fields.Linter.SPECTRAL, + ) + ) + await client.get_style_guide(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = linting_service.GetStyleGuideRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_update_style_guide_empty_call_grpc_asyncio(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.update_style_guide), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + linting_service.StyleGuide( + name="name_value", + linter=common_fields.Linter.SPECTRAL, + ) + ) + await client.update_style_guide(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = linting_service.UpdateStyleGuideRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_style_guide_contents_empty_call_grpc_asyncio(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_style_guide_contents), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + linting_service.StyleGuideContents( + contents=b"contents_blob", + mime_type="mime_type_value", + ) + ) + await client.get_style_guide_contents(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = linting_service.GetStyleGuideContentsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_lint_spec_empty_call_grpc_asyncio(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.lint_spec), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.lint_spec(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = linting_service.LintSpecRequest() + + assert args[0] == request_msg + + +def test_transport_kind_rest(): + transport = LintingServiceClient.get_transport_class("rest")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "rest" + + +def test_get_style_guide_rest_bad_request( + request_type=linting_service.GetStyleGuideRequest, +): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/plugins/sample3/styleGuide" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_style_guide(request) + + +@pytest.mark.parametrize( + "request_type", + [ + linting_service.GetStyleGuideRequest, + dict, + ], +) +def test_get_style_guide_rest_call_success(request_type): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/plugins/sample3/styleGuide" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = linting_service.StyleGuide( + name="name_value", + linter=common_fields.Linter.SPECTRAL, + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = linting_service.StyleGuide.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.get_style_guide(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, linting_service.StyleGuide) + assert response.name == "name_value" + assert response.linter == common_fields.Linter.SPECTRAL + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_style_guide_rest_interceptors(null_interceptor): + transport = transports.LintingServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.LintingServiceRestInterceptor(), + ) + client = LintingServiceClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.LintingServiceRestInterceptor, "post_get_style_guide" + ) as post, mock.patch.object( + transports.LintingServiceRestInterceptor, "post_get_style_guide_with_metadata" + ) as post_with_metadata, mock.patch.object( + transports.LintingServiceRestInterceptor, "pre_get_style_guide" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = linting_service.GetStyleGuideRequest.pb( + linting_service.GetStyleGuideRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = linting_service.StyleGuide.to_json(linting_service.StyleGuide()) + req.return_value.content = return_value + + request = linting_service.GetStyleGuideRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = linting_service.StyleGuide() + post_with_metadata.return_value = linting_service.StyleGuide(), metadata + + client.get_style_guide( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_update_style_guide_rest_bad_request( + request_type=linting_service.UpdateStyleGuideRequest, +): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "style_guide": { + "name": "projects/sample1/locations/sample2/plugins/sample3/styleGuide" + } + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.update_style_guide(request) + + +@pytest.mark.parametrize( + "request_type", + [ + linting_service.UpdateStyleGuideRequest, + dict, + ], +) +def test_update_style_guide_rest_call_success(request_type): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "style_guide": { + "name": "projects/sample1/locations/sample2/plugins/sample3/styleGuide" + } + } + request_init["style_guide"] = { + "name": "projects/sample1/locations/sample2/plugins/sample3/styleGuide", + "linter": 1, + "contents": {"contents": b"contents_blob", "mime_type": "mime_type_value"}, + } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = linting_service.UpdateStyleGuideRequest.meta.fields["style_guide"] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["style_guide"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["style_guide"][field])): + del request_init["style_guide"][field][i][subfield] + else: + del request_init["style_guide"][field][subfield] + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = linting_service.StyleGuide( + name="name_value", + linter=common_fields.Linter.SPECTRAL, + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = linting_service.StyleGuide.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.update_style_guide(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, linting_service.StyleGuide) + assert response.name == "name_value" + assert response.linter == common_fields.Linter.SPECTRAL + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_update_style_guide_rest_interceptors(null_interceptor): + transport = transports.LintingServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.LintingServiceRestInterceptor(), + ) + client = LintingServiceClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.LintingServiceRestInterceptor, "post_update_style_guide" + ) as post, mock.patch.object( + transports.LintingServiceRestInterceptor, + "post_update_style_guide_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.LintingServiceRestInterceptor, "pre_update_style_guide" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = linting_service.UpdateStyleGuideRequest.pb( + linting_service.UpdateStyleGuideRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = linting_service.StyleGuide.to_json(linting_service.StyleGuide()) + req.return_value.content = return_value + + request = linting_service.UpdateStyleGuideRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = linting_service.StyleGuide() + post_with_metadata.return_value = linting_service.StyleGuide(), metadata + + client.update_style_guide( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_get_style_guide_contents_rest_bad_request( + request_type=linting_service.GetStyleGuideContentsRequest, +): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/plugins/sample3/styleGuide" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_style_guide_contents(request) + + +@pytest.mark.parametrize( + "request_type", + [ + linting_service.GetStyleGuideContentsRequest, + dict, + ], +) +def test_get_style_guide_contents_rest_call_success(request_type): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/plugins/sample3/styleGuide" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = linting_service.StyleGuideContents( + contents=b"contents_blob", + mime_type="mime_type_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = linting_service.StyleGuideContents.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.get_style_guide_contents(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, linting_service.StyleGuideContents) + assert response.contents == b"contents_blob" + assert response.mime_type == "mime_type_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_style_guide_contents_rest_interceptors(null_interceptor): + transport = transports.LintingServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.LintingServiceRestInterceptor(), + ) + client = LintingServiceClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.LintingServiceRestInterceptor, "post_get_style_guide_contents" + ) as post, mock.patch.object( + transports.LintingServiceRestInterceptor, + "post_get_style_guide_contents_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.LintingServiceRestInterceptor, "pre_get_style_guide_contents" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = linting_service.GetStyleGuideContentsRequest.pb( + linting_service.GetStyleGuideContentsRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = linting_service.StyleGuideContents.to_json( + linting_service.StyleGuideContents() + ) + req.return_value.content = return_value + + request = linting_service.GetStyleGuideContentsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = linting_service.StyleGuideContents() + post_with_metadata.return_value = linting_service.StyleGuideContents(), metadata + + client.get_style_guide_contents( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_lint_spec_rest_bad_request(request_type=linting_service.LintSpecRequest): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.lint_spec(request) + + +@pytest.mark.parametrize( + "request_type", + [ + linting_service.LintSpecRequest, + dict, + ], +) +def test_lint_spec_rest_call_success(request_type): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "" + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.lint_spec(request) + + # Establish that the response is the type that we expect. + assert response is None + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_lint_spec_rest_interceptors(null_interceptor): + transport = transports.LintingServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.LintingServiceRestInterceptor(), + ) + client = LintingServiceClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.LintingServiceRestInterceptor, "pre_lint_spec" + ) as pre: + pre.assert_not_called() + pb_message = linting_service.LintSpecRequest.pb( + linting_service.LintSpecRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + request = linting_service.LintSpecRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + + client.lint_spec( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + + +def test_get_location_rest_bad_request(request_type=locations_pb2.GetLocationRequest): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_location(request) + + +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.GetLocationRequest, + dict, + ], +) +def test_get_location_rest(request_type): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = locations_pb2.Location() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_location(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) + + +def test_list_locations_rest_bad_request( + request_type=locations_pb2.ListLocationsRequest, +): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict({"name": "projects/sample1"}, request) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_locations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.ListLocationsRequest, + dict, + ], +) +def test_list_locations_rest(request_type): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = locations_pb2.ListLocationsResponse() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_locations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + +def test_cancel_operation_rest_bad_request( + request_type=operations_pb2.CancelOperationRequest, +): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.cancel_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.CancelOperationRequest, + dict, + ], +) +def test_cancel_operation_rest(request_type): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "{}" + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.cancel_operation(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_delete_operation_rest_bad_request( + request_type=operations_pb2.DeleteOperationRequest, +): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.delete_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.DeleteOperationRequest, + dict, + ], +) +def test_delete_operation_rest(request_type): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "{}" + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_operation(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_get_operation_rest_bad_request( + request_type=operations_pb2.GetOperationRequest, +): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.GetOperationRequest, + dict, + ], +) +def test_get_operation_rest(request_type): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_operation(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) + + +def test_list_operations_rest_bad_request( + request_type=operations_pb2.ListOperationsRequest, +): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_operations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.ListOperationsRequest, + dict, + ], +) +def test_list_operations_rest(request_type): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.ListOperationsResponse() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_operations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) + + +def test_initialize_client_w_rest(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_style_guide_empty_call_rest(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.get_style_guide), "__call__") as call: + client.get_style_guide(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = linting_service.GetStyleGuideRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_update_style_guide_empty_call_rest(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.update_style_guide), "__call__" + ) as call: + client.update_style_guide(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = linting_service.UpdateStyleGuideRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_style_guide_contents_empty_call_rest(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_style_guide_contents), "__call__" + ) as call: + client.get_style_guide_contents(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = linting_service.GetStyleGuideContentsRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_lint_spec_empty_call_rest(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object(type(client.transport.lint_spec), "__call__") as call: + client.lint_spec(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = linting_service.LintSpecRequest() + + assert args[0] == request_msg + + +def test_transport_grpc_default(): + # A client should use the gRPC transport by default. + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert isinstance( + client.transport, + transports.LintingServiceGrpcTransport, + ) + + +def test_linting_service_base_transport_error(): + # Passing both a credentials object and credentials_file should raise an error + with pytest.raises(core_exceptions.DuplicateCredentialArgs): + transport = transports.LintingServiceTransport( + credentials=ga_credentials.AnonymousCredentials(), + credentials_file="credentials.json", + ) + + +def test_linting_service_base_transport(): + # Instantiate the base transport. + with mock.patch( + "google.cloud.apihub_v1.services.linting_service.transports.LintingServiceTransport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.LintingServiceTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Every method on the transport should just blindly + # raise NotImplementedError. + methods = ( + "get_style_guide", + "update_style_guide", + "get_style_guide_contents", + "lint_spec", + "get_location", + "list_locations", + "get_operation", + "cancel_operation", + "delete_operation", + "list_operations", + ) + for method in methods: + with pytest.raises(NotImplementedError): + getattr(transport, method)(request=object()) + + with pytest.raises(NotImplementedError): + transport.close() + + # Catch all for all remaining methods and properties + remainder = [ + "kind", + ] + for r in remainder: + with pytest.raises(NotImplementedError): + getattr(transport, r)() + + +def test_linting_service_base_transport_with_credentials_file(): + # Instantiate the base transport with a credentials file + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch( + "google.cloud.apihub_v1.services.linting_service.transports.LintingServiceTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) + transport = transports.LintingServiceTransport( + credentials_file="credentials.json", + quota_project_id="octopus", + ) + load_creds.assert_called_once_with( + "credentials.json", + scopes=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", + ) + + +def test_linting_service_base_transport_with_adc(): + # Test the default credentials are used if credentials and credentials_file are None. + with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch( + "google.cloud.apihub_v1.services.linting_service.transports.LintingServiceTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport = transports.LintingServiceTransport() + adc.assert_called_once() + + +def test_linting_service_auth_adc(): + # If no credentials are provided, we should use ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + LintingServiceClient() + adc.assert_called_once_with( + scopes=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id=None, + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.LintingServiceGrpcTransport, + transports.LintingServiceGrpcAsyncIOTransport, + ], +) +def test_linting_service_transport_auth_adc(transport_class): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + adc.assert_called_once_with( + scopes=["1", "2"], + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.LintingServiceGrpcTransport, + transports.LintingServiceGrpcAsyncIOTransport, + transports.LintingServiceRestTransport, + ], +) +def test_linting_service_transport_auth_gdch_credentials(transport_class): + host = "https://language.com" + api_audience_tests = [None, "https://language2.com"] + api_audience_expect = [host, "https://language2.com"] + for t, e in zip(api_audience_tests, api_audience_expect): + with mock.patch.object(google.auth, "default", autospec=True) as adc: + gdch_mock = mock.MagicMock() + type(gdch_mock).with_gdch_audience = mock.PropertyMock( + return_value=gdch_mock + ) + adc.return_value = (gdch_mock, None) + transport_class(host=host, api_audience=t) + gdch_mock.with_gdch_audience.assert_called_once_with(e) + + +@pytest.mark.parametrize( + "transport_class,grpc_helpers", + [ + (transports.LintingServiceGrpcTransport, grpc_helpers), + (transports.LintingServiceGrpcAsyncIOTransport, grpc_helpers_async), + ], +) +def test_linting_service_transport_create_channel(transport_class, grpc_helpers): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel", autospec=True + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + adc.return_value = (creds, None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + + create_channel.assert_called_with( + "apihub.googleapis.com:443", + credentials=creds, + credentials_file=None, + quota_project_id="octopus", + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + scopes=["1", "2"], + default_host="apihub.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.LintingServiceGrpcTransport, + transports.LintingServiceGrpcAsyncIOTransport, + ], +) +def test_linting_service_grpc_transport_client_cert_source_for_mtls(transport_class): + cred = ga_credentials.AnonymousCredentials() + + # Check ssl_channel_credentials is used if provided. + with mock.patch.object(transport_class, "create_channel") as mock_create_channel: + mock_ssl_channel_creds = mock.Mock() + transport_class( + host="squid.clam.whelk", + credentials=cred, + ssl_channel_credentials=mock_ssl_channel_creds, + ) + mock_create_channel.assert_called_once_with( + "squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_channel_creds, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + # Check if ssl_channel_credentials is not provided, then client_cert_source_for_mtls + # is used. + with mock.patch.object(transport_class, "create_channel", return_value=mock.Mock()): + with mock.patch("grpc.ssl_channel_credentials") as mock_ssl_cred: + transport_class( + credentials=cred, + client_cert_source_for_mtls=client_cert_source_callback, + ) + expected_cert, expected_key = client_cert_source_callback() + mock_ssl_cred.assert_called_once_with( + certificate_chain=expected_cert, private_key=expected_key + ) + + +def test_linting_service_http_transport_client_cert_source_for_mtls(): + cred = ga_credentials.AnonymousCredentials() + with mock.patch( + "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" + ) as mock_configure_mtls_channel: + transports.LintingServiceRestTransport( + credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + ) + mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) -def test_get_style_guide_contents_rest_bad_request( - request_type=linting_service.GetStyleGuideContentsRequest, -): +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "grpc_asyncio", + "rest", + ], +) +def test_linting_service_host_no_port(transport_name): client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="apihub.googleapis.com" + ), + transport=transport_name, + ) + assert client.transport._host == ( + "apihub.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://apihub.googleapis.com" ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/plugins/sample3/styleGuide" - } - request = request_type(**request_init) - - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_style_guide_contents(request) @pytest.mark.parametrize( - "request_type", + "transport_name", [ - linting_service.GetStyleGuideContentsRequest, - dict, + "grpc", + "grpc_asyncio", + "rest", ], ) -def test_get_style_guide_contents_rest_call_success(request_type): +def test_linting_service_host_with_port(transport_name): client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="apihub.googleapis.com:8000" + ), + transport=transport_name, + ) + assert client.transport._host == ( + "apihub.googleapis.com:8000" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://apihub.googleapis.com:8000" ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/plugins/sample3/styleGuide" - } - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = linting_service.StyleGuideContents( - contents=b"contents_blob", - mime_type="mime_type_value", - ) +@pytest.mark.parametrize( + "transport_name", + [ + "rest", + ], +) +def test_linting_service_client_transport_session_collision(transport_name): + creds1 = ga_credentials.AnonymousCredentials() + creds2 = ga_credentials.AnonymousCredentials() + client1 = LintingServiceClient( + credentials=creds1, + transport=transport_name, + ) + client2 = LintingServiceClient( + credentials=creds2, + transport=transport_name, + ) + session1 = client1.transport.get_style_guide._session + session2 = client2.transport.get_style_guide._session + assert session1 != session2 + session1 = client1.transport.update_style_guide._session + session2 = client2.transport.update_style_guide._session + assert session1 != session2 + session1 = client1.transport.get_style_guide_contents._session + session2 = client2.transport.get_style_guide_contents._session + assert session1 != session2 + session1 = client1.transport.lint_spec._session + session2 = client2.transport.lint_spec._session + assert session1 != session2 - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = linting_service.StyleGuideContents.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_style_guide_contents(request) +def test_linting_service_grpc_transport_channel(): + channel = grpc.secure_channel("http://localhost/", grpc.local_channel_credentials()) - # Establish that the response is the type that we expect. - assert isinstance(response, linting_service.StyleGuideContents) - assert response.contents == b"contents_blob" - assert response.mime_type == "mime_type_value" + # Check that channel is used if provided. + transport = transports.LintingServiceGrpcTransport( + host="squid.clam.whelk", + channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_style_guide_contents_rest_interceptors(null_interceptor): - transport = transports.LintingServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.LintingServiceRestInterceptor(), +def test_linting_service_grpc_asyncio_transport_channel(): + channel = aio.secure_channel("http://localhost/", grpc.local_channel_credentials()) + + # Check that channel is used if provided. + transport = transports.LintingServiceGrpcAsyncIOTransport( + host="squid.clam.whelk", + channel=channel, ) - client = LintingServiceClient(transport=transport) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.LintingServiceRestInterceptor, "post_get_style_guide_contents" - ) as post, mock.patch.object( - transports.LintingServiceRestInterceptor, - "post_get_style_guide_contents_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.LintingServiceRestInterceptor, "pre_get_style_guide_contents" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = linting_service.GetStyleGuideContentsRequest.pb( - linting_service.GetStyleGuideContentsRequest() - ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = linting_service.StyleGuideContents.to_json( - linting_service.StyleGuideContents() - ) - req.return_value.content = return_value +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. +@pytest.mark.parametrize( + "transport_class", + [ + transports.LintingServiceGrpcTransport, + transports.LintingServiceGrpcAsyncIOTransport, + ], +) +def test_linting_service_transport_channel_mtls_with_client_cert_source( + transport_class, +): + with mock.patch( + "grpc.ssl_channel_credentials", autospec=True + ) as grpc_ssl_channel_cred: + with mock.patch.object( + transport_class, "create_channel" + ) 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 = ga_credentials.AnonymousCredentials() + with pytest.warns(DeprecationWarning): + with mock.patch.object(google.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() - request = linting_service.GetStyleGuideContentsRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = linting_service.StyleGuideContents() - post_with_metadata.return_value = linting_service.StyleGuideContents(), metadata + 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=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + assert transport._ssl_channel_credentials == mock_ssl_cred - client.get_style_guide_contents( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], - ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. +@pytest.mark.parametrize( + "transport_class", + [ + transports.LintingServiceGrpcTransport, + transports.LintingServiceGrpcAsyncIOTransport, + ], +) +def test_linting_service_transport_channel_mtls_with_adc(transport_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), + ): + with mock.patch.object( + transport_class, "create_channel" + ) 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, + ) + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=mock_cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel -def test_lint_spec_rest_bad_request(request_type=linting_service.LintSpecRequest): - client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + +def test_spec_path(): + project = "squid" + location = "clam" + api = "whelk" + version = "octopus" + spec = "oyster" + expected = "projects/{project}/locations/{location}/apis/{api}/versions/{version}/specs/{spec}".format( + project=project, + location=location, + api=api, + version=version, + spec=spec, ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" + actual = LintingServiceClient.spec_path(project, location, api, version, spec) + assert expected == actual + + +def test_parse_spec_path(): + expected = { + "project": "nudibranch", + "location": "cuttlefish", + "api": "mussel", + "version": "winkle", + "spec": "nautilus", + } + path = LintingServiceClient.spec_path(**expected) + + # Check that the path construction is reversible. + actual = LintingServiceClient.parse_spec_path(path) + assert expected == actual + + +def test_style_guide_path(): + project = "scallop" + location = "abalone" + plugin = "squid" + expected = ( + "projects/{project}/locations/{location}/plugins/{plugin}/styleGuide".format( + project=project, + location=location, + plugin=plugin, + ) + ) + actual = LintingServiceClient.style_guide_path(project, location, plugin) + assert expected == actual + + +def test_parse_style_guide_path(): + expected = { + "project": "clam", + "location": "whelk", + "plugin": "octopus", } - request = request_type(**request_init) + path = LintingServiceClient.style_guide_path(**expected) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.lint_spec(request) + # Check that the path construction is reversible. + actual = LintingServiceClient.parse_style_guide_path(path) + assert expected == actual -@pytest.mark.parametrize( - "request_type", - [ - linting_service.LintSpecRequest, - dict, - ], -) -def test_lint_spec_rest_call_success(request_type): - client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" +def test_common_billing_account_path(): + billing_account = "oyster" + expected = "billingAccounts/{billing_account}".format( + billing_account=billing_account, ) + actual = LintingServiceClient.common_billing_account_path(billing_account) + assert expected == actual - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/apis/sample3/versions/sample4/specs/sample5" + +def test_parse_common_billing_account_path(): + expected = { + "billing_account": "nudibranch", } - request = request_type(**request_init) + path = LintingServiceClient.common_billing_account_path(**expected) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = None + # Check that the path construction is reversible. + actual = LintingServiceClient.parse_common_billing_account_path(path) + assert expected == actual - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "" - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.lint_spec(request) - # Establish that the response is the type that we expect. - assert response is None +def test_common_folder_path(): + folder = "cuttlefish" + expected = "folders/{folder}".format( + folder=folder, + ) + actual = LintingServiceClient.common_folder_path(folder) + assert expected == actual -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_lint_spec_rest_interceptors(null_interceptor): - transport = transports.LintingServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.LintingServiceRestInterceptor(), - ) - client = LintingServiceClient(transport=transport) +def test_parse_common_folder_path(): + expected = { + "folder": "mussel", + } + path = LintingServiceClient.common_folder_path(**expected) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.LintingServiceRestInterceptor, "pre_lint_spec" - ) as pre: - pre.assert_not_called() - pb_message = linting_service.LintSpecRequest.pb( - linting_service.LintSpecRequest() - ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } + # Check that the path construction is reversible. + actual = LintingServiceClient.parse_common_folder_path(path) + assert expected == actual - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - request = linting_service.LintSpecRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata +def test_common_organization_path(): + organization = "winkle" + expected = "organizations/{organization}".format( + organization=organization, + ) + actual = LintingServiceClient.common_organization_path(organization) + assert expected == actual - client.lint_spec( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], - ) - pre.assert_called_once() +def test_parse_common_organization_path(): + expected = { + "organization": "nautilus", + } + path = LintingServiceClient.common_organization_path(**expected) + # Check that the path construction is reversible. + actual = LintingServiceClient.parse_common_organization_path(path) + assert expected == actual -def test_get_location_rest_bad_request(request_type=locations_pb2.GetLocationRequest): - client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2"}, request + +def test_common_project_path(): + project = "scallop" + expected = "projects/{project}".format( + project=project, ) + actual = LintingServiceClient.common_project_path(project) + assert expected == actual - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_location(request) +def test_parse_common_project_path(): + expected = { + "project": "abalone", + } + path = LintingServiceClient.common_project_path(**expected) -@pytest.mark.parametrize( - "request_type", - [ - locations_pb2.GetLocationRequest, - dict, - ], -) -def test_get_location_rest(request_type): - client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + # Check that the path construction is reversible. + actual = LintingServiceClient.parse_common_project_path(path) + assert expected == actual - request_init = {"name": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = locations_pb2.Location() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") +def test_common_location_path(): + project = "squid" + location = "clam" + expected = "projects/{project}/locations/{location}".format( + project=project, + location=location, + ) + actual = LintingServiceClient.common_location_path(project, location) + assert expected == actual - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_location(request) +def test_parse_common_location_path(): + expected = { + "project": "whelk", + "location": "octopus", + } + path = LintingServiceClient.common_location_path(**expected) - # Establish that the response is the type that we expect. - assert isinstance(response, locations_pb2.Location) + # Check that the path construction is reversible. + actual = LintingServiceClient.parse_common_location_path(path) + assert expected == actual -def test_list_locations_rest_bad_request( - request_type=locations_pb2.ListLocationsRequest, -): - client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict({"name": "projects/sample1"}, request) +def test_client_with_default_client_info(): + client_info = gapic_v1.client_info.ClientInfo() - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_locations(request) + with mock.patch.object( + transports.LintingServiceTransport, "_prep_wrapped_messages" + ) as prep: + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + client_info=client_info, + ) + prep.assert_called_once_with(client_info) + + with mock.patch.object( + transports.LintingServiceTransport, "_prep_wrapped_messages" + ) as prep: + transport_class = LintingServiceClient.get_transport_class() + transport = transport_class( + credentials=ga_credentials.AnonymousCredentials(), + client_info=client_info, + ) + prep.assert_called_once_with(client_info) -@pytest.mark.parametrize( - "request_type", - [ - locations_pb2.ListLocationsRequest, - dict, - ], -) -def test_list_locations_rest(request_type): +def test_delete_operation(transport: str = "grpc"): client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - request_init = {"name": "projects/sample1"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = locations_pb2.ListLocationsResponse() + # 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 = operations_pb2.DeleteOperationRequest() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.delete_operation(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 - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert response is None - response = client.list_locations(request) + +@pytest.mark.asyncio +async def test_delete_operation_async(transport: str = "grpc_asyncio"): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.DeleteOperationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_operation(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 response is the type that we expect. - assert isinstance(response, locations_pb2.ListLocationsResponse) + assert response is None -def test_cancel_operation_rest_bad_request( - request_type=operations_pb2.CancelOperationRequest, -): +def test_delete_operation_field_headers(): client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.cancel_operation(request) + # 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 = operations_pb2.DeleteOperationRequest() + request.name = "locations" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + call.return_value = None -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.CancelOperationRequest, - dict, - ], -) -def test_cancel_operation_rest(request_type): - client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + client.delete_operation(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 - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = None + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "{}" - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +@pytest.mark.asyncio +async def test_delete_operation_field_headers_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) - response = client.cancel_operation(request) + # 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 = operations_pb2.DeleteOperationRequest() + request.name = "locations" - # Establish that the response is the type that we expect. - assert response is None + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_operation(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", + "name=locations", + ) in kw["metadata"] -def test_delete_operation_rest_bad_request( - request_type=operations_pb2.DeleteOperationRequest, -): +def test_delete_operation_from_dict(): client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + + response = client.delete_operation( + request={ + "name": "locations", + } + ) + call.assert_called() - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_operation(request) +@pytest.mark.asyncio +async def test_delete_operation_from_dict_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.DeleteOperationRequest, - dict, - ], -) -def test_delete_operation_rest(request_type): + +def test_cancel_operation(transport: str = "grpc"): client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = None + # 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 = operations_pb2.CancelOperationRequest() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "{}" - response_value.content = json_return_value.encode("UTF-8") + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.cancel_operation(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 - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert response is None - response = client.delete_operation(request) + +@pytest.mark.asyncio +async def test_cancel_operation_async(transport: str = "grpc_asyncio"): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.CancelOperationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.cancel_operation(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 response is the type that we expect. assert response is None -def test_get_operation_rest_bad_request( - request_type=operations_pb2.GetOperationRequest, -): +def test_cancel_operation_field_headers(): client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_operation(request) + # 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 = operations_pb2.CancelOperationRequest() + request.name = "locations" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + call.return_value = None -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.GetOperationRequest, - dict, - ], -) -def test_get_operation_rest(request_type): - client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + client.cancel_operation(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 - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation() + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +@pytest.mark.asyncio +async def test_cancel_operation_field_headers_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) - response = client.get_operation(request) + # 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 = operations_pb2.CancelOperationRequest() + request.name = "locations" - # Establish that the response is the type that we expect. - assert isinstance(response, operations_pb2.Operation) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.cancel_operation(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", + "name=locations", + ) in kw["metadata"] -def test_list_operations_rest_bad_request( - request_type=operations_pb2.ListOperationsRequest, -): + +def test_cancel_operation_from_dict(): client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2"}, request - ) - - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_operations(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + + response = client.cancel_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.ListOperationsRequest, - dict, - ], -) -def test_list_operations_rest(request_type): - client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +@pytest.mark.asyncio +async def test_cancel_operation_from_dict_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.cancel_operation( + request={ + "name": "locations", + } + ) + call.assert_called() - request_init = {"name": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.ListOperationsResponse() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") +def test_get_operation(transport: str = "grpc"): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # 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 = operations_pb2.GetOperationRequest() - response = client.list_operations(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation() + response = client.get_operation(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 response is the type that we expect. - assert isinstance(response, operations_pb2.ListOperationsResponse) + assert isinstance(response, operations_pb2.Operation) -def test_initialize_client_w_rest(): - client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" +@pytest.mark.asyncio +async def test_get_operation_async(transport: str = "grpc_asyncio"): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - assert client is not None + # 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 = operations_pb2.GetOperationRequest() -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_get_style_guide_empty_call_rest(): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + response = await client.get_operation(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 response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) + + +def test_get_operation_field_headers(): client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.get_style_guide), "__call__") as call: - client.get_style_guide(request=None) + # 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 = operations_pb2.GetOperationRequest() + request.name = "locations" - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + call.return_value = operations_pb2.Operation() + + client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = linting_service.GetStyleGuideRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_update_style_guide_empty_call_rest(): - client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +@pytest.mark.asyncio +async def test_get_operation_field_headers_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.update_style_guide), "__call__" - ) as call: - client.update_style_guide(request=None) + # 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 = operations_pb2.GetOperationRequest() + request.name = "locations" - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + await client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = linting_service.UpdateStyleGuideRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_get_style_guide_contents_empty_call_rest(): +def test_get_operation_from_dict(): client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation() + + response = client.get_operation( + request={ + "name": "locations", + } + ) + call.assert_called() - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.get_style_guide_contents), "__call__" - ) as call: - client.get_style_guide_contents(request=None) - # Establish that the underlying stub method was called. +@pytest.mark.asyncio +async def test_get_operation_from_dict_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + response = await client.get_operation( + request={ + "name": "locations", + } + ) call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = linting_service.GetStyleGuideContentsRequest() - - assert args[0] == request_msg -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_lint_spec_empty_call_rest(): +def test_list_operations(transport: str = "grpc"): client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the actual call, and fake the request. - with mock.patch.object(type(client.transport.lint_spec), "__call__") as call: - client.lint_spec(request=None) + # 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 = operations_pb2.ListOperationsRequest() - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.ListOperationsResponse() + response = client.list_operations(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = linting_service.LintSpecRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) -def test_linting_service_base_transport_error(): - # Passing both a credentials object and credentials_file should raise an error - with pytest.raises(core_exceptions.DuplicateCredentialArgs): - transport = transports.LintingServiceTransport( - credentials=ga_credentials.AnonymousCredentials(), - credentials_file="credentials.json", - ) +@pytest.mark.asyncio +async def test_list_operations_async(transport: str = "grpc_asyncio"): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.ListOperationsRequest() -def test_linting_service_base_transport(): - # Instantiate the base transport. - with mock.patch( - "google.cloud.apihub_v1.services.linting_service.transports.LintingServiceTransport.__init__" - ) as Transport: - Transport.return_value = None - transport = transports.LintingServiceTransport( - credentials=ga_credentials.AnonymousCredentials(), + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() ) + response = await client.list_operations(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 - # Every method on the transport should just blindly - # raise NotImplementedError. - methods = ( - "get_style_guide", - "update_style_guide", - "get_style_guide_contents", - "lint_spec", - "get_location", - "list_locations", - "get_operation", - "cancel_operation", - "delete_operation", - "list_operations", + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) + + +def test_list_operations_field_headers(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), ) - for method in methods: - with pytest.raises(NotImplementedError): - getattr(transport, method)(request=object()) - with pytest.raises(NotImplementedError): - transport.close() + # 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 = operations_pb2.ListOperationsRequest() + request.name = "locations" - # Catch all for all remaining methods and properties - remainder = [ - "kind", - ] - for r in remainder: - with pytest.raises(NotImplementedError): - getattr(transport, r)() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + call.return_value = operations_pb2.ListOperationsResponse() + client.list_operations(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 -def test_linting_service_base_transport_with_credentials_file(): - # Instantiate the base transport with a credentials file - with mock.patch.object( - google.auth, "load_credentials_from_file", autospec=True - ) as load_creds, mock.patch( - "google.cloud.apihub_v1.services.linting_service.transports.LintingServiceTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None - load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) - transport = transports.LintingServiceTransport( - credentials_file="credentials.json", - quota_project_id="octopus", - ) - load_creds.assert_called_once_with( - "credentials.json", - scopes=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id="octopus", - ) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -def test_linting_service_base_transport_with_adc(): - # Test the default credentials are used if credentials and credentials_file are None. - with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch( - "google.cloud.apihub_v1.services.linting_service.transports.LintingServiceTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport = transports.LintingServiceTransport() - adc.assert_called_once() +@pytest.mark.asyncio +async def test_list_operations_field_headers_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + # 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 = operations_pb2.ListOperationsRequest() + request.name = "locations" -def test_linting_service_auth_adc(): - # If no credentials are provided, we should use ADC credentials. - with mock.patch.object(google.auth, "default", autospec=True) as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - LintingServiceClient() - adc.assert_called_once_with( - scopes=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id=None, + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() ) + await client.list_operations(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", + "name=locations", + ) in kw["metadata"] -def test_linting_service_http_transport_client_cert_source_for_mtls(): - cred = ga_credentials.AnonymousCredentials() - with mock.patch( - "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" - ) as mock_configure_mtls_channel: - transports.LintingServiceRestTransport( - credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + +def test_list_operations_from_dict(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.ListOperationsResponse() + + response = client.list_operations( + request={ + "name": "locations", + } ) - mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + call.assert_called() -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_linting_service_host_no_port(transport_name): +@pytest.mark.asyncio +async def test_list_operations_from_dict_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() + ) + response = await client.list_operations( + request={ + "name": "locations", + } + ) + call.assert_called() + + +def test_list_locations(transport: str = "grpc"): client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - client_options=client_options.ClientOptions( - api_endpoint="apihub.googleapis.com" - ), - transport=transport_name, + transport=transport, ) - assert client.transport._host == ( - "apihub.googleapis.com:443" - if transport_name in ["grpc", "grpc_asyncio"] - else "https://apihub.googleapis.com" + + # 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 = locations_pb2.ListLocationsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.ListLocationsResponse() + response = client.list_locations(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 response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + +@pytest.mark.asyncio +async def test_list_locations_async(transport: str = "grpc_asyncio"): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + 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 = locations_pb2.ListLocationsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + response = await client.list_locations(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 response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_linting_service_host_with_port(transport_name): + +def test_list_locations_field_headers(): client = LintingServiceClient( credentials=ga_credentials.AnonymousCredentials(), - client_options=client_options.ClientOptions( - api_endpoint="apihub.googleapis.com:8000" - ), - transport=transport_name, - ) - assert client.transport._host == ( - "apihub.googleapis.com:8000" - if transport_name in ["grpc", "grpc_asyncio"] - else "https://apihub.googleapis.com:8000" ) + # 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 = locations_pb2.ListLocationsRequest() + request.name = "locations" -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_linting_service_client_transport_session_collision(transport_name): - creds1 = ga_credentials.AnonymousCredentials() - creds2 = ga_credentials.AnonymousCredentials() - client1 = LintingServiceClient( - credentials=creds1, - transport=transport_name, - ) - client2 = LintingServiceClient( - credentials=creds2, - transport=transport_name, - ) - session1 = client1.transport.get_style_guide._session - session2 = client2.transport.get_style_guide._session - assert session1 != session2 - session1 = client1.transport.update_style_guide._session - session2 = client2.transport.update_style_guide._session - assert session1 != session2 - session1 = client1.transport.get_style_guide_contents._session - session2 = client2.transport.get_style_guide_contents._session - assert session1 != session2 - session1 = client1.transport.lint_spec._session - session2 = client2.transport.lint_spec._session - assert session1 != session2 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + call.return_value = locations_pb2.ListLocationsResponse() + client.list_locations(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 -def test_spec_path(): - project = "squid" - location = "clam" - api = "whelk" - version = "octopus" - spec = "oyster" - expected = "projects/{project}/locations/{location}/apis/{api}/versions/{version}/specs/{spec}".format( - project=project, - location=location, - api=api, - version=version, - spec=spec, + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_list_locations_field_headers_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), ) - actual = LintingServiceClient.spec_path(project, location, api, version, spec) - assert expected == actual + # 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 = locations_pb2.ListLocationsRequest() + request.name = "locations" -def test_parse_spec_path(): - expected = { - "project": "nudibranch", - "location": "cuttlefish", - "api": "mussel", - "version": "winkle", - "spec": "nautilus", - } - path = LintingServiceClient.spec_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + await client.list_locations(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 - # Check that the path construction is reversible. - actual = LintingServiceClient.parse_spec_path(path) - assert expected == actual + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -def test_style_guide_path(): - project = "scallop" - location = "abalone" - plugin = "squid" - expected = ( - "projects/{project}/locations/{location}/plugins/{plugin}/styleGuide".format( - project=project, - location=location, - plugin=plugin, - ) +def test_list_locations_from_dict(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), ) - actual = LintingServiceClient.style_guide_path(project, location, plugin) - assert expected == actual - + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.ListLocationsResponse() + + response = client.list_locations( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_parse_style_guide_path(): - expected = { - "project": "clam", - "location": "whelk", - "plugin": "octopus", - } - path = LintingServiceClient.style_guide_path(**expected) - # Check that the path construction is reversible. - actual = LintingServiceClient.parse_style_guide_path(path) - assert expected == actual +@pytest.mark.asyncio +async def test_list_locations_from_dict_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + response = await client.list_locations( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_common_billing_account_path(): - billing_account = "oyster" - expected = "billingAccounts/{billing_account}".format( - billing_account=billing_account, +def test_get_location(transport: str = "grpc"): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - actual = LintingServiceClient.common_billing_account_path(billing_account) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() -def test_parse_common_billing_account_path(): - expected = { - "billing_account": "nudibranch", - } - path = LintingServiceClient.common_billing_account_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.Location() + response = client.get_location(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 - # Check that the path construction is reversible. - actual = LintingServiceClient.parse_common_billing_account_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) -def test_common_folder_path(): - folder = "cuttlefish" - expected = "folders/{folder}".format( - folder=folder, +@pytest.mark.asyncio +async def test_get_location_async(transport: str = "grpc_asyncio"): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - actual = LintingServiceClient.common_folder_path(folder) - assert expected == actual - -def test_parse_common_folder_path(): - expected = { - "folder": "mussel", - } - path = LintingServiceClient.common_folder_path(**expected) + # 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 = locations_pb2.GetLocationRequest() - # Check that the path construction is reversible. - actual = LintingServiceClient.parse_common_folder_path(path) - assert expected == actual + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + response = await client.get_location(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 response is the type that we expect. + assert isinstance(response, locations_pb2.Location) -def test_common_organization_path(): - organization = "winkle" - expected = "organizations/{organization}".format( - organization=organization, - ) - actual = LintingServiceClient.common_organization_path(organization) - assert expected == actual +def test_get_location_field_headers(): + client = LintingServiceClient(credentials=ga_credentials.AnonymousCredentials()) -def test_parse_common_organization_path(): - expected = { - "organization": "nautilus", - } - path = LintingServiceClient.common_organization_path(**expected) + # 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 = locations_pb2.GetLocationRequest() + request.name = "locations/abc" - # Check that the path construction is reversible. - actual = LintingServiceClient.parse_common_organization_path(path) - assert expected == actual + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + call.return_value = locations_pb2.Location() + client.get_location(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 -def test_common_project_path(): - project = "scallop" - expected = "projects/{project}".format( - project=project, - ) - actual = LintingServiceClient.common_project_path(project) - assert expected == actual + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations/abc", + ) in kw["metadata"] -def test_parse_common_project_path(): - expected = { - "project": "abalone", - } - path = LintingServiceClient.common_project_path(**expected) +@pytest.mark.asyncio +async def test_get_location_field_headers_async(): + client = LintingServiceAsyncClient(credentials=async_anonymous_credentials()) - # Check that the path construction is reversible. - actual = LintingServiceClient.parse_common_project_path(path) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() + request.name = "locations/abc" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + await client.get_location(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 -def test_common_location_path(): - project = "squid" - location = "clam" - expected = "projects/{project}/locations/{location}".format( - project=project, - location=location, - ) - actual = LintingServiceClient.common_location_path(project, location) - assert expected == actual + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations/abc", + ) in kw["metadata"] -def test_parse_common_location_path(): - expected = { - "project": "whelk", - "location": "octopus", - } - path = LintingServiceClient.common_location_path(**expected) +def test_get_location_from_dict(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.Location() + + response = client.get_location( + request={ + "name": "locations/abc", + } + ) + call.assert_called() - # Check that the path construction is reversible. - actual = LintingServiceClient.parse_common_location_path(path) - assert expected == actual +@pytest.mark.asyncio +async def test_get_location_from_dict_async(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + response = await client.get_location( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_client_with_default_client_info(): - client_info = gapic_v1.client_info.ClientInfo() +def test_transport_close_grpc(): + client = LintingServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) with mock.patch.object( - transports.LintingServiceTransport, "_prep_wrapped_messages" - ) as prep: - client = LintingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - client_info=client_info, - ) - prep.assert_called_once_with(client_info) + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + +@pytest.mark.asyncio +async def test_transport_close_grpc_asyncio(): + client = LintingServiceAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) with mock.patch.object( - transports.LintingServiceTransport, "_prep_wrapped_messages" - ) as prep: - transport_class = LintingServiceClient.get_transport_class() - transport = transport_class( - credentials=ga_credentials.AnonymousCredentials(), - client_info=client_info, - ) - prep.assert_called_once_with(client_info) + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + async with client: + close.assert_not_called() + close.assert_called_once() def test_transport_close_rest(): @@ -3123,6 +5930,7 @@ def test_transport_close_rest(): def test_client_ctx(): transports = [ "rest", + "grpc", ] for transport in transports: client = LintingServiceClient( @@ -3139,7 +5947,8 @@ def test_client_ctx(): @pytest.mark.parametrize( "client_class,transport_class", [ - (LintingServiceClient, transports.LintingServiceRestTransport), + (LintingServiceClient, transports.LintingServiceGrpcTransport), + (LintingServiceAsyncClient, transports.LintingServiceGrpcAsyncIOTransport), ], ) def test_api_key_credentials(client_class, transport_class): diff --git a/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_provisioning.py b/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_provisioning.py index ae1a8733470c..e99998c32089 100644 --- a/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_provisioning.py +++ b/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_provisioning.py @@ -65,7 +65,11 @@ from google.protobuf import empty_pb2 # type: ignore from google.protobuf import timestamp_pb2 # type: ignore -from google.cloud.apihub_v1.services.provisioning import ProvisioningClient, transports +from google.cloud.apihub_v1.services.provisioning import ( + ProvisioningAsyncClient, + ProvisioningClient, + transports, +) from google.cloud.apihub_v1.types import common_fields, provisioning_service CRED_INFO_JSON = { @@ -232,6 +236,11 @@ def test__get_client_cert_source(): "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ProvisioningClient), ) +@mock.patch.object( + ProvisioningAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ProvisioningAsyncClient), +) def test__get_api_endpoint(): api_override = "foo.com" mock_client_cert_source = mock.Mock() @@ -359,6 +368,8 @@ def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code): @pytest.mark.parametrize( "client_class,transport_name", [ + (ProvisioningClient, "grpc"), + (ProvisioningAsyncClient, "grpc_asyncio"), (ProvisioningClient, "rest"), ], ) @@ -383,6 +394,8 @@ def test_provisioning_client_from_service_account_info(client_class, transport_n @pytest.mark.parametrize( "transport_class,transport_name", [ + (transports.ProvisioningGrpcTransport, "grpc"), + (transports.ProvisioningGrpcAsyncIOTransport, "grpc_asyncio"), (transports.ProvisioningRestTransport, "rest"), ], ) @@ -407,6 +420,8 @@ def test_provisioning_client_service_account_always_use_jwt( @pytest.mark.parametrize( "client_class,transport_name", [ + (ProvisioningClient, "grpc"), + (ProvisioningAsyncClient, "grpc_asyncio"), (ProvisioningClient, "rest"), ], ) @@ -438,17 +453,24 @@ def test_provisioning_client_from_service_account_file(client_class, transport_n def test_provisioning_client_get_transport_class(): transport = ProvisioningClient.get_transport_class() available_transports = [ + transports.ProvisioningGrpcTransport, transports.ProvisioningRestTransport, ] assert transport in available_transports - transport = ProvisioningClient.get_transport_class("rest") - assert transport == transports.ProvisioningRestTransport + transport = ProvisioningClient.get_transport_class("grpc") + assert transport == transports.ProvisioningGrpcTransport @pytest.mark.parametrize( "client_class,transport_class,transport_name", [ + (ProvisioningClient, transports.ProvisioningGrpcTransport, "grpc"), + ( + ProvisioningAsyncClient, + transports.ProvisioningGrpcAsyncIOTransport, + "grpc_asyncio", + ), (ProvisioningClient, transports.ProvisioningRestTransport, "rest"), ], ) @@ -457,6 +479,11 @@ def test_provisioning_client_get_transport_class(): "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ProvisioningClient), ) +@mock.patch.object( + ProvisioningAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ProvisioningAsyncClient), +) def test_provisioning_client_client_options( client_class, transport_class, transport_name ): @@ -590,6 +617,20 @@ def test_provisioning_client_client_options( @pytest.mark.parametrize( "client_class,transport_class,transport_name,use_client_cert_env", [ + (ProvisioningClient, transports.ProvisioningGrpcTransport, "grpc", "true"), + ( + ProvisioningAsyncClient, + transports.ProvisioningGrpcAsyncIOTransport, + "grpc_asyncio", + "true", + ), + (ProvisioningClient, transports.ProvisioningGrpcTransport, "grpc", "false"), + ( + ProvisioningAsyncClient, + transports.ProvisioningGrpcAsyncIOTransport, + "grpc_asyncio", + "false", + ), (ProvisioningClient, transports.ProvisioningRestTransport, "rest", "true"), (ProvisioningClient, transports.ProvisioningRestTransport, "rest", "false"), ], @@ -599,6 +640,11 @@ def test_provisioning_client_client_options( "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ProvisioningClient), ) +@mock.patch.object( + ProvisioningAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ProvisioningAsyncClient), +) @mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) def test_provisioning_client_mtls_env_auto( client_class, transport_class, transport_name, use_client_cert_env @@ -702,10 +748,15 @@ def test_provisioning_client_mtls_env_auto( ) -@pytest.mark.parametrize("client_class", [ProvisioningClient]) +@pytest.mark.parametrize("client_class", [ProvisioningClient, ProvisioningAsyncClient]) @mock.patch.object( ProvisioningClient, "DEFAULT_ENDPOINT", modify_default_endpoint(ProvisioningClient) ) +@mock.patch.object( + ProvisioningAsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(ProvisioningAsyncClient), +) def test_provisioning_client_get_mtls_endpoint_and_cert_source(client_class): mock_client_cert_source = mock.Mock() @@ -797,12 +848,17 @@ def test_provisioning_client_get_mtls_endpoint_and_cert_source(client_class): ) -@pytest.mark.parametrize("client_class", [ProvisioningClient]) +@pytest.mark.parametrize("client_class", [ProvisioningClient, ProvisioningAsyncClient]) @mock.patch.object( ProvisioningClient, "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(ProvisioningClient), ) +@mock.patch.object( + ProvisioningAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(ProvisioningAsyncClient), +) def test_provisioning_client_client_api_endpoint(client_class): mock_client_cert_source = client_cert_source_callback api_override = "foo.com" @@ -879,6 +935,12 @@ def test_provisioning_client_client_api_endpoint(client_class): @pytest.mark.parametrize( "client_class,transport_class,transport_name", [ + (ProvisioningClient, transports.ProvisioningGrpcTransport, "grpc"), + ( + ProvisioningAsyncClient, + transports.ProvisioningGrpcAsyncIOTransport, + "grpc_asyncio", + ), (ProvisioningClient, transports.ProvisioningRestTransport, "rest"), ], ) @@ -910,6 +972,18 @@ def test_provisioning_client_client_options_scopes( @pytest.mark.parametrize( "client_class,transport_class,transport_name,grpc_helpers", [ + ( + ProvisioningClient, + transports.ProvisioningGrpcTransport, + "grpc", + grpc_helpers, + ), + ( + ProvisioningAsyncClient, + transports.ProvisioningGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), (ProvisioningClient, transports.ProvisioningRestTransport, "rest", None), ], ) @@ -937,13 +1011,168 @@ def test_provisioning_client_client_options_credentials_file( ) -def test_create_api_hub_instance_rest_use_cached_wrapped_rpc(): +def test_provisioning_client_client_options_from_dict(): + with mock.patch( + "google.cloud.apihub_v1.services.provisioning.transports.ProvisioningGrpcTransport.__init__" + ) as grpc_transport: + grpc_transport.return_value = None + client = ProvisioningClient(client_options={"api_endpoint": "squid.clam.whelk"}) + grpc_transport.assert_called_once_with( + credentials=None, + credentials_file=None, + host="squid.clam.whelk", + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name,grpc_helpers", + [ + ( + ProvisioningClient, + transports.ProvisioningGrpcTransport, + "grpc", + grpc_helpers, + ), + ( + ProvisioningAsyncClient, + transports.ProvisioningGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), + ], +) +def test_provisioning_client_create_channel_credentials_file( + client_class, transport_class, transport_name, grpc_helpers +): + # Check the case credentials file is provided. + options = client_options.ClientOptions(credentials_file="credentials.json") + + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options, transport=transport_name) + patched.assert_called_once_with( + credentials=None, + credentials_file="credentials.json", + host=client._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE + ), + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + # test that the credentials from file are saved and used as the credentials. + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel" + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + file_creds = ga_credentials.AnonymousCredentials() + load_creds.return_value = (file_creds, None) + adc.return_value = (creds, None) + client = client_class(client_options=options, transport=transport_name) + create_channel.assert_called_with( + "apihub.googleapis.com:443", + credentials=file_creds, + credentials_file=None, + quota_project_id=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + scopes=None, + default_host="apihub.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + +@pytest.mark.parametrize( + "request_type", + [ + provisioning_service.CreateApiHubInstanceRequest, + dict, + ], +) +def test_create_api_hub_instance(request_type, transport: str = "grpc"): + client = ProvisioningClient( + credentials=ga_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.create_api_hub_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/spam") + response = client.create_api_hub_instance(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = provisioning_service.CreateApiHubInstanceRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) + + +def test_create_api_hub_instance_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = provisioning_service.CreateApiHubInstanceRequest( + parent="parent_value", + api_hub_instance_id="api_hub_instance_id_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_api_hub_instance), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.create_api_hub_instance(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == provisioning_service.CreateApiHubInstanceRequest( + parent="parent_value", + api_hub_instance_id="api_hub_instance_id_value", + ) + + +def test_create_api_hub_instance_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -964,15 +1193,15 @@ def test_create_api_hub_instance_rest_use_cached_wrapped_rpc(): client._transport._wrapped_methods[ client._transport.create_api_hub_instance ] = mock_rpc - request = {} client.create_api_hub_instance(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - # Operation methods build a cached wrapper on first rpc call - # subsequent calls should use the cached wrapper + # Operation methods call wrapper_fn to build a cached + # client._transport.operations_client instance on first rpc call. + # Subsequent calls should use the cached wrapper wrapper_fn.reset_mock() client.create_api_hub_instance(request) @@ -982,145 +1211,194 @@ def test_create_api_hub_instance_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_create_api_hub_instance_rest_required_fields( - request_type=provisioning_service.CreateApiHubInstanceRequest, +@pytest.mark.asyncio +async def test_create_api_hub_instance_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ProvisioningRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.create_api_hub_instance + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.create_api_hub_instance + ] = mock_rpc + + request = {} + await client.create_api_hub_instance(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + # Operation methods call wrapper_fn to build a cached + # client._transport.operations_client instance on first rpc call. + # Subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() + + await client.create_api_hub_instance(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_create_api_hub_instance_async( + transport: str = "grpc_asyncio", + request_type=provisioning_service.CreateApiHubInstanceRequest, +): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # verify fields with default values are dropped + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_api_hub_instance._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_api_hub_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + response = await client.create_api_hub_instance(request) - # verify required fields with default values are now present + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = provisioning_service.CreateApiHubInstanceRequest() + assert args[0] == request - jsonified_request["parent"] = "parent_value" + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_api_hub_instance._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("api_hub_instance_id",)) - jsonified_request.update(unset_fields) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" +@pytest.mark.asyncio +async def test_create_api_hub_instance_async_from_dict(): + await test_create_api_hub_instance_async(request_type=dict) + +def test_create_api_hub_instance_field_headers(): client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result + # 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 = provisioning_service.CreateApiHubInstanceRequest() - response_value = Response() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) + request.parent = "parent_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_api_hub_instance), "__call__" + ) as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.create_api_hub_instance(request) - response = client.create_api_hub_instance(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 - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] -def test_create_api_hub_instance_rest_unset_required_fields(): - transport = transports.ProvisioningRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_create_api_hub_instance_field_headers_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.create_api_hub_instance._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(("apiHubInstanceId",)) - & set( - ( - "parent", - "apiHubInstance", - ) + # 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 = provisioning_service.CreateApiHubInstanceRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_api_hub_instance), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/op") ) - ) + await client.create_api_hub_instance(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request -def test_create_api_hub_instance_rest_flattened(): + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +def test_create_api_hub_instance_flattened(): client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") - - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} - - # get truthy value for each flattened field - mock_args = dict( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_api_hub_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.create_api_hub_instance( parent="parent_value", api_hub_instance=common_fields.ApiHubInstance(name="name_value"), api_hub_instance_id="api_hub_instance_id_value", ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - - client.create_api_hub_instance(**mock_args) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/apiHubInstances" - % client.transport._host, - args[1], - ) - - -def test_create_api_hub_instance_rest_flattened_error(transport: str = "rest"): + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].api_hub_instance + mock_val = common_fields.ApiHubInstance(name="name_value") + assert arg == mock_val + arg = args[0].api_hub_instance_id + mock_val = "api_hub_instance_id_value" + assert arg == mock_val + + +def test_create_api_hub_instance_flattened_error(): client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened @@ -1134,13 +1412,134 @@ def test_create_api_hub_instance_rest_flattened_error(transport: str = "rest"): ) -def test_delete_api_hub_instance_rest_use_cached_wrapped_rpc(): +@pytest.mark.asyncio +async def test_create_api_hub_instance_flattened_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_api_hub_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.create_api_hub_instance( + parent="parent_value", + api_hub_instance=common_fields.ApiHubInstance(name="name_value"), + api_hub_instance_id="api_hub_instance_id_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].api_hub_instance + mock_val = common_fields.ApiHubInstance(name="name_value") + assert arg == mock_val + arg = args[0].api_hub_instance_id + mock_val = "api_hub_instance_id_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_create_api_hub_instance_flattened_error_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.create_api_hub_instance( + provisioning_service.CreateApiHubInstanceRequest(), + parent="parent_value", + api_hub_instance=common_fields.ApiHubInstance(name="name_value"), + api_hub_instance_id="api_hub_instance_id_value", + ) + + +@pytest.mark.parametrize( + "request_type", + [ + provisioning_service.DeleteApiHubInstanceRequest, + dict, + ], +) +def test_delete_api_hub_instance(request_type, transport: str = "grpc"): + client = ProvisioningClient( + credentials=ga_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.delete_api_hub_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/spam") + response = client.delete_api_hub_instance(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = provisioning_service.DeleteApiHubInstanceRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) + + +def test_delete_api_hub_instance_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = provisioning_service.DeleteApiHubInstanceRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_api_hub_instance), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.delete_api_hub_instance(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == provisioning_service.DeleteApiHubInstanceRequest( + name="name_value", + ) + + +def test_delete_api_hub_instance_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1161,15 +1560,15 @@ def test_delete_api_hub_instance_rest_use_cached_wrapped_rpc(): client._transport._wrapped_methods[ client._transport.delete_api_hub_instance ] = mock_rpc - request = {} client.delete_api_hub_instance(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - # Operation methods build a cached wrapper on first rpc call - # subsequent calls should use the cached wrapper + # Operation methods call wrapper_fn to build a cached + # client._transport.operations_client instance on first rpc call. + # Subsequent calls should use the cached wrapper wrapper_fn.reset_mock() client.delete_api_hub_instance(request) @@ -1179,336 +1578,324 @@ def test_delete_api_hub_instance_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_delete_api_hub_instance_rest_required_fields( - request_type=provisioning_service.DeleteApiHubInstanceRequest, +@pytest.mark.asyncio +async def test_delete_api_hub_instance_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ProvisioningRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.delete_api_hub_instance + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_api_hub_instance._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.delete_api_hub_instance + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.delete_api_hub_instance(request) - jsonified_request["name"] = "name_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_api_hub_instance._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Operation methods call wrapper_fn to build a cached + # client._transport.operations_client instance on first rpc call. + # Subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + await client.delete_api_hub_instance(request) - client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type(**request_init) + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "delete", - "query_params": pb_request, - } - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) +@pytest.mark.asyncio +async def test_delete_api_hub_instance_async( + transport: str = "grpc_asyncio", + request_type=provisioning_service.DeleteApiHubInstanceRequest, +): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # 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() - response = client.delete_api_hub_instance(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_api_hub_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + response = await client.delete_api_hub_instance(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = provisioning_service.DeleteApiHubInstanceRequest() + assert args[0] == request + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) -def test_delete_api_hub_instance_rest_unset_required_fields(): - transport = transports.ProvisioningRestTransport( - credentials=ga_credentials.AnonymousCredentials - ) - unset_fields = transport.delete_api_hub_instance._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) +@pytest.mark.asyncio +async def test_delete_api_hub_instance_async_from_dict(): + await test_delete_api_hub_instance_async(request_type=dict) -def test_delete_api_hub_instance_rest_flattened(): +def test_delete_api_hub_instance_field_headers(): client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + # 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 = provisioning_service.DeleteApiHubInstanceRequest() - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/apiHubInstances/sample3" - } + request.name = "name_value" - # get truthy value for each flattened field - mock_args = dict( - name="name_value", - ) - mock_args.update(sample_request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_api_hub_instance), "__call__" + ) as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.delete_api_hub_instance(request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request - client.delete_api_hub_instance(**mock_args) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] - # Establish that the underlying call was made with the expected - # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/apiHubInstances/*}" - % client.transport._host, - args[1], + +@pytest.mark.asyncio +async def test_delete_api_hub_instance_field_headers_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = provisioning_service.DeleteApiHubInstanceRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_api_hub_instance), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/op") ) + await client.delete_api_hub_instance(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", + "name=name_value", + ) in kw["metadata"] -def test_delete_api_hub_instance_rest_flattened_error(transport: str = "rest"): + +def test_delete_api_hub_instance_flattened(): client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_api_hub_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. client.delete_api_hub_instance( - provisioning_service.DeleteApiHubInstanceRequest(), name="name_value", ) + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_get_api_hub_instance_rest_use_cached_wrapped_rpc(): - # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, - # instead of constructing them on each call - with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: - client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - - # Should wrap all calls on client creation - assert wrapper_fn.call_count > 0 - wrapper_fn.reset_mock() - - # Ensure method has been cached - assert ( - client._transport.get_api_hub_instance in client._transport._wrapped_methods - ) - - # Replace cached wrapped function with mock - mock_rpc = mock.Mock() - mock_rpc.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client._transport._wrapped_methods[ - client._transport.get_api_hub_instance - ] = mock_rpc - - request = {} - client.get_api_hub_instance(request) - - # Establish that the underlying gRPC stub method was called. - assert mock_rpc.call_count == 1 - - client.get_api_hub_instance(request) - - # Establish that a new wrapper was not created for this call - assert wrapper_fn.call_count == 0 - assert mock_rpc.call_count == 2 - - -def test_get_api_hub_instance_rest_required_fields( - request_type=provisioning_service.GetApiHubInstanceRequest, -): - transport_class = transports.ProvisioningRestTransport - - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) - - # verify fields with default values are dropped - - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_api_hub_instance._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - - # verify required fields with default values are now present - - jsonified_request["name"] = "name_value" - - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_api_hub_instance._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" +def test_delete_api_hub_instance_flattened_error(): client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = common_fields.ApiHubInstance() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_api_hub_instance( + provisioning_service.DeleteApiHubInstanceRequest(), + name="name_value", + ) - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.ApiHubInstance.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) +@pytest.mark.asyncio +async def test_delete_api_hub_instance_flattened_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + ) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_api_hub_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") - response = client.get_api_hub_instance(request) + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.delete_api_hub_instance( + name="name_value", + ) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_get_api_hub_instance_rest_unset_required_fields(): - transport = transports.ProvisioningRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_delete_api_hub_instance_flattened_error_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = transport.get_api_hub_instance._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.delete_api_hub_instance( + provisioning_service.DeleteApiHubInstanceRequest(), + name="name_value", + ) -def test_get_api_hub_instance_rest_flattened(): +@pytest.mark.parametrize( + "request_type", + [ + provisioning_service.GetApiHubInstanceRequest, + dict, + ], +) +def test_get_api_hub_instance(request_type, transport: str = "grpc"): client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.ApiHubInstance() - - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/apiHubInstances/sample3" - } + # 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() - # get truthy value for each flattened field - mock_args = dict( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_api_hub_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ApiHubInstance( name="name_value", + state=common_fields.ApiHubInstance.State.INACTIVE, + state_message="state_message_value", + description="description_value", ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = common_fields.ApiHubInstance.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.get_api_hub_instance(request) - client.get_api_hub_instance(**mock_args) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = provisioning_service.GetApiHubInstanceRequest() + assert args[0] == request - # Establish that the underlying call was made with the expected - # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/apiHubInstances/*}" - % client.transport._host, - args[1], - ) + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.ApiHubInstance) + assert response.name == "name_value" + assert response.state == common_fields.ApiHubInstance.State.INACTIVE + assert response.state_message == "state_message_value" + assert response.description == "description_value" -def test_get_api_hub_instance_rest_flattened_error(transport: str = "rest"): +def test_get_api_hub_instance_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport="grpc", ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.get_api_hub_instance( - provisioning_service.GetApiHubInstanceRequest(), + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = provisioning_service.GetApiHubInstanceRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_api_hub_instance), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_api_hub_instance(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == provisioning_service.GetApiHubInstanceRequest( name="name_value", ) -def test_lookup_api_hub_instance_rest_use_cached_wrapped_rpc(): +def test_get_api_hub_instance_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1517,8 +1904,7 @@ def test_lookup_api_hub_instance_rest_use_cached_wrapped_rpc(): # Ensure method has been cached assert ( - client._transport.lookup_api_hub_instance - in client._transport._wrapped_methods + client._transport.get_api_hub_instance in client._transport._wrapped_methods ) # Replace cached wrapped function with mock @@ -1527,1672 +1913,4271 @@ def test_lookup_api_hub_instance_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[ - client._transport.lookup_api_hub_instance + client._transport.get_api_hub_instance ] = mock_rpc - request = {} - client.lookup_api_hub_instance(request) + client.get_api_hub_instance(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.lookup_api_hub_instance(request) + client.get_api_hub_instance(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_lookup_api_hub_instance_rest_required_fields( - request_type=provisioning_service.LookupApiHubInstanceRequest, +@pytest.mark.asyncio +async def test_get_api_hub_instance_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.ProvisioningRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.get_api_hub_instance + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).lookup_api_hub_instance._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.get_api_hub_instance + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.get_api_hub_instance(request) - jsonified_request["parent"] = "parent_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).lookup_api_hub_instance._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + await client.get_api_hub_instance(request) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = provisioning_service.LookupApiHubInstanceResponse() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result +@pytest.mark.asyncio +async def test_get_api_hub_instance_async( + transport: str = "grpc_asyncio", + request_type=provisioning_service.GetApiHubInstanceRequest, +): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - response_value = Response() - response_value.status_code = 200 + # 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() - # Convert return value to protobuf type - return_value = provisioning_service.LookupApiHubInstanceResponse.pb( - return_value + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_api_hub_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ApiHubInstance( + name="name_value", + state=common_fields.ApiHubInstance.State.INACTIVE, + state_message="state_message_value", + description="description_value", ) - json_return_value = json_format.MessageToJson(return_value) + ) + response = await client.get_api_hub_instance(request) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = provisioning_service.GetApiHubInstanceRequest() + assert args[0] == request - response = client.lookup_api_hub_instance(request) + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.ApiHubInstance) + assert response.name == "name_value" + assert response.state == common_fields.ApiHubInstance.State.INACTIVE + assert response.state_message == "state_message_value" + assert response.description == "description_value" - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_get_api_hub_instance_async_from_dict(): + await test_get_api_hub_instance_async(request_type=dict) -def test_lookup_api_hub_instance_rest_unset_required_fields(): - transport = transports.ProvisioningRestTransport( - credentials=ga_credentials.AnonymousCredentials + +def test_get_api_hub_instance_field_headers(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = transport.lookup_api_hub_instance._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("parent",))) + # 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 = provisioning_service.GetApiHubInstanceRequest() + request.name = "name_value" -def test_lookup_api_hub_instance_rest_flattened(): - client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_api_hub_instance), "__call__" + ) as call: + call.return_value = common_fields.ApiHubInstance() + client.get_api_hub_instance(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", + "name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_get_api_hub_instance_field_headers_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = provisioning_service.LookupApiHubInstanceResponse() + # 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 = provisioning_service.GetApiHubInstanceRequest() - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + request.name = "name_value" - # get truthy value for each flattened field - mock_args = dict( - parent="parent_value", + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_api_hub_instance), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ApiHubInstance() ) - mock_args.update(sample_request) + await client.get_api_hub_instance(request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = provisioning_service.LookupApiHubInstanceResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request - client.lookup_api_hub_instance(**mock_args) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] + + +def test_get_api_hub_instance_flattened(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_api_hub_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ApiHubInstance() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_api_hub_instance( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/apiHubInstances:lookup" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_lookup_api_hub_instance_rest_flattened_error(transport: str = "rest"): +def test_get_api_hub_instance_flattened_error(): client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.lookup_api_hub_instance( - provisioning_service.LookupApiHubInstanceRequest(), - parent="parent_value", + client.get_api_hub_instance( + provisioning_service.GetApiHubInstanceRequest(), + name="name_value", ) -def test_credentials_transport_error(): - # It is an error to provide credentials and a transport instance. - transport = transports.ProvisioningRestTransport( - credentials=ga_credentials.AnonymousCredentials(), +@pytest.mark.asyncio +async def test_get_api_hub_instance_flattened_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), ) - with pytest.raises(ValueError): - client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - # It is an error to provide a credentials file and a transport instance. - transport = transports.ProvisioningRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - with pytest.raises(ValueError): - client = ProvisioningClient( - client_options={"credentials_file": "credentials.json"}, - transport=transport, - ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_api_hub_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = common_fields.ApiHubInstance() - # It is an error to provide an api_key and a transport instance. - transport = transports.ProvisioningRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = ProvisioningClient( - client_options=options, - transport=transport, + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ApiHubInstance() ) - - # It is an error to provide an api_key and a credential. - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = ProvisioningClient( - client_options=options, credentials=ga_credentials.AnonymousCredentials() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_api_hub_instance( + name="name_value", ) - # It is an error to provide scopes and a transport instance. - transport = transports.ProvisioningRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - with pytest.raises(ValueError): - client = ProvisioningClient( - client_options={"scopes": ["1", "2"]}, - transport=transport, - ) + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_transport_instance(): - # A client may be instantiated with a custom transport instance. - transport = transports.ProvisioningRestTransport( - credentials=ga_credentials.AnonymousCredentials(), +@pytest.mark.asyncio +async def test_get_api_hub_instance_flattened_error_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), ) - client = ProvisioningClient(transport=transport) - assert client.transport is transport + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.get_api_hub_instance( + provisioning_service.GetApiHubInstanceRequest(), + name="name_value", + ) @pytest.mark.parametrize( - "transport_class", + "request_type", [ - transports.ProvisioningRestTransport, + provisioning_service.LookupApiHubInstanceRequest, + dict, ], ) -def test_transport_adc(transport_class): - # Test default credentials are used if not provided. - with mock.patch.object(google.auth, "default") as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport_class() - adc.assert_called_once() +def test_lookup_api_hub_instance(request_type, transport: str = "grpc"): + client = ProvisioningClient( + credentials=ga_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() -def test_transport_kind_rest(): - transport = ProvisioningClient.get_transport_class("rest")( - credentials=ga_credentials.AnonymousCredentials() - ) - assert transport.kind == "rest" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.lookup_api_hub_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = provisioning_service.LookupApiHubInstanceResponse() + response = client.lookup_api_hub_instance(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = provisioning_service.LookupApiHubInstanceRequest() + assert args[0] == request -def test_create_api_hub_instance_rest_bad_request( - request_type=provisioning_service.CreateApiHubInstanceRequest, -): + # Establish that the response is the type that we expect. + assert isinstance(response, provisioning_service.LookupApiHubInstanceResponse) + + +def test_lookup_api_hub_instance_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_api_hub_instance(request) + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = provisioning_service.LookupApiHubInstanceRequest( + parent="parent_value", + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.lookup_api_hub_instance), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.lookup_api_hub_instance(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == provisioning_service.LookupApiHubInstanceRequest( + parent="parent_value", + ) -@pytest.mark.parametrize( - "request_type", - [ - provisioning_service.CreateApiHubInstanceRequest, - dict, - ], -) -def test_create_api_hub_instance_rest_call_success(request_type): - client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request_init["api_hub_instance"] = { - "name": "name_value", - "create_time": {"seconds": 751, "nanos": 543}, - "update_time": {}, - "state": 1, - "state_message": "state_message_value", - "config": { - "cmek_key_name": "cmek_key_name_value", - "disable_search": True, - "vertex_location": "vertex_location_value", - "encryption_type": 1, - }, - "labels": {}, - "description": "description_value", - } - # The version of a generated dependency at test runtime may differ from the version used during generation. - # Delete any fields which are not present in the current runtime dependency - # See https://github.com/googleapis/gapic-generator-python/issues/1748 +def test_lookup_api_hub_instance_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) - # Determine if the message type is proto-plus or protobuf - test_field = provisioning_service.CreateApiHubInstanceRequest.meta.fields[ - "api_hub_instance" - ] + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - def get_message_fields(field): - # Given a field which is a message (composite type), return a list with - # all the fields of the message. - # If the field is not a composite type, return an empty list. - message_fields = [] + # Ensure method has been cached + assert ( + client._transport.lookup_api_hub_instance + in client._transport._wrapped_methods + ) - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.lookup_api_hub_instance + ] = mock_rpc + request = {} + client.lookup_api_hub_instance(request) - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] + client.lookup_api_hub_instance(request) - subfields_not_in_runtime = [] + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["api_hub_instance"].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) +@pytest.mark.asyncio +async def test_lookup_api_hub_instance_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range(0, len(request_init["api_hub_instance"][field])): - del request_init["api_hub_instance"][field][i][subfield] - else: - del request_init["api_hub_instance"][field][subfield] - request = request_type(**request_init) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + # Ensure method has been cached + assert ( + client._client._transport.lookup_api_hub_instance + in client._client._transport._wrapped_methods + ) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.create_api_hub_instance(request) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.lookup_api_hub_instance + ] = mock_rpc - # Establish that the response is the type that we expect. - json_return_value = json_format.MessageToJson(return_value) + request = {} + await client.lookup_api_hub_instance(request) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_create_api_hub_instance_rest_interceptors(null_interceptor): - transport = transports.ProvisioningRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ProvisioningRestInterceptor(), + await client.lookup_api_hub_instance(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_lookup_api_hub_instance_async( + transport: str = "grpc_asyncio", + request_type=provisioning_service.LookupApiHubInstanceRequest, +): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - client = ProvisioningClient(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._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - operation.Operation, "_set_result_from_operation" - ), mock.patch.object( - transports.ProvisioningRestInterceptor, "post_create_api_hub_instance" - ) as post, mock.patch.object( - transports.ProvisioningRestInterceptor, - "post_create_api_hub_instance_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.ProvisioningRestInterceptor, "pre_create_api_hub_instance" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = provisioning_service.CreateApiHubInstanceRequest.pb( - provisioning_service.CreateApiHubInstanceRequest() + type(client.transport.lookup_api_hub_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + provisioning_service.LookupApiHubInstanceResponse() ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } + response = await client.lookup_api_hub_instance(request) - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = json_format.MessageToJson(operations_pb2.Operation()) - req.return_value.content = return_value + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = provisioning_service.LookupApiHubInstanceRequest() + assert args[0] == request - request = provisioning_service.CreateApiHubInstanceRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = operations_pb2.Operation() - post_with_metadata.return_value = operations_pb2.Operation(), metadata + # Establish that the response is the type that we expect. + assert isinstance(response, provisioning_service.LookupApiHubInstanceResponse) - client.create_api_hub_instance( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], - ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() +@pytest.mark.asyncio +async def test_lookup_api_hub_instance_async_from_dict(): + await test_lookup_api_hub_instance_async(request_type=dict) -def test_delete_api_hub_instance_rest_bad_request( - request_type=provisioning_service.DeleteApiHubInstanceRequest, -): +def test_lookup_api_hub_instance_field_headers(): client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/apiHubInstances/sample3" - } - request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_api_hub_instance(request) + # 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 = provisioning_service.LookupApiHubInstanceRequest() + request.parent = "parent_value" -@pytest.mark.parametrize( - "request_type", - [ - provisioning_service.DeleteApiHubInstanceRequest, - dict, - ], -) -def test_delete_api_hub_instance_rest_call_success(request_type): - client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.lookup_api_hub_instance), "__call__" + ) as call: + call.return_value = provisioning_service.LookupApiHubInstanceResponse() + client.lookup_api_hub_instance(request) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/apiHubInstances/sample3" - } - request = request_type(**request_init) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.delete_api_hub_instance(request) - # Establish that the response is the type that we expect. - json_return_value = json_format.MessageToJson(return_value) +@pytest.mark.asyncio +async def test_lookup_api_hub_instance_field_headers_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + ) + # 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 = provisioning_service.LookupApiHubInstanceRequest() -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_delete_api_hub_instance_rest_interceptors(null_interceptor): - transport = transports.ProvisioningRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ProvisioningRestInterceptor(), - ) - client = ProvisioningClient(transport=transport) + request.parent = "parent_value" + # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - operation.Operation, "_set_result_from_operation" - ), mock.patch.object( - transports.ProvisioningRestInterceptor, "post_delete_api_hub_instance" - ) as post, mock.patch.object( - transports.ProvisioningRestInterceptor, - "post_delete_api_hub_instance_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.ProvisioningRestInterceptor, "pre_delete_api_hub_instance" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = provisioning_service.DeleteApiHubInstanceRequest.pb( - provisioning_service.DeleteApiHubInstanceRequest() + type(client.transport.lookup_api_hub_instance), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + provisioning_service.LookupApiHubInstanceResponse() ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } + await client.lookup_api_hub_instance(request) - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = json_format.MessageToJson(operations_pb2.Operation()) - req.return_value.content = return_value + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request - request = provisioning_service.DeleteApiHubInstanceRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = operations_pb2.Operation() - post_with_metadata.return_value = operations_pb2.Operation(), metadata + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] - client.delete_api_hub_instance( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + +def test_lookup_api_hub_instance_flattened(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.lookup_api_hub_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = provisioning_service.LookupApiHubInstanceResponse() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.lookup_api_hub_instance( + parent="parent_value", ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val -def test_get_api_hub_instance_rest_bad_request( - request_type=provisioning_service.GetApiHubInstanceRequest, -): +def test_lookup_api_hub_instance_flattened_error(): client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/apiHubInstances/sample3" - } - request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_api_hub_instance(request) + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.lookup_api_hub_instance( + provisioning_service.LookupApiHubInstanceRequest(), + parent="parent_value", + ) -@pytest.mark.parametrize( - "request_type", - [ - provisioning_service.GetApiHubInstanceRequest, - dict, - ], -) -def test_get_api_hub_instance_rest_call_success(request_type): - client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" +@pytest.mark.asyncio +async def test_lookup_api_hub_instance_flattened_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/apiHubInstances/sample3" - } - request = request_type(**request_init) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.lookup_api_hub_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = provisioning_service.LookupApiHubInstanceResponse() - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = common_fields.ApiHubInstance( - name="name_value", - state=common_fields.ApiHubInstance.State.INACTIVE, - state_message="state_message_value", - description="description_value", + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + provisioning_service.LookupApiHubInstanceResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.lookup_api_hub_instance( + parent="parent_value", ) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = common_fields.ApiHubInstance.pb(return_value) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_api_hub_instance(request) - - # Establish that the response is the type that we expect. - assert isinstance(response, common_fields.ApiHubInstance) - assert response.name == "name_value" - assert response.state == common_fields.ApiHubInstance.State.INACTIVE - assert response.state_message == "state_message_value" - assert response.description == "description_value" + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_api_hub_instance_rest_interceptors(null_interceptor): - transport = transports.ProvisioningRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ProvisioningRestInterceptor(), +@pytest.mark.asyncio +async def test_lookup_api_hub_instance_flattened_error_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), ) - client = ProvisioningClient(transport=transport) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ProvisioningRestInterceptor, "post_get_api_hub_instance" - ) as post, mock.patch.object( - transports.ProvisioningRestInterceptor, - "post_get_api_hub_instance_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.ProvisioningRestInterceptor, "pre_get_api_hub_instance" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = provisioning_service.GetApiHubInstanceRequest.pb( - provisioning_service.GetApiHubInstanceRequest() + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.lookup_api_hub_instance( + provisioning_service.LookupApiHubInstanceRequest(), + parent="parent_value", ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = common_fields.ApiHubInstance.to_json( - common_fields.ApiHubInstance() + +def test_create_api_hub_instance_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - req.return_value.content = return_value - request = provisioning_service.GetApiHubInstanceRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = common_fields.ApiHubInstance() - post_with_metadata.return_value = common_fields.ApiHubInstance(), metadata + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - client.get_api_hub_instance( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + # Ensure method has been cached + assert ( + client._transport.create_api_hub_instance + in client._transport._wrapped_methods ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.create_api_hub_instance + ] = mock_rpc + request = {} + client.create_api_hub_instance(request) -def test_lookup_api_hub_instance_rest_bad_request( - request_type=provisioning_service.LookupApiHubInstanceRequest, -): - client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request = request_type(**request_init) + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.lookup_api_hub_instance(request) + # Operation methods build a cached wrapper on first rpc call + # subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() + client.create_api_hub_instance(request) -@pytest.mark.parametrize( - "request_type", - [ - provisioning_service.LookupApiHubInstanceRequest, - dict, - ], -) -def test_lookup_api_hub_instance_rest_call_success(request_type): - client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + +def test_create_api_hub_instance_rest_required_fields( + request_type=provisioning_service.CreateApiHubInstanceRequest, +): + transport_class = transports.ProvisioningRestTransport + + request_init = {} + request_init["parent"] = "" request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = provisioning_service.LookupApiHubInstanceResponse() + # verify fields with default values are dropped - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_api_hub_instance._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) - # Convert return value to protobuf type - return_value = provisioning_service.LookupApiHubInstanceResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.lookup_api_hub_instance(request) + # verify required fields with default values are now present - # Establish that the response is the type that we expect. - assert isinstance(response, provisioning_service.LookupApiHubInstanceResponse) + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_api_hub_instance._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("api_hub_instance_id",)) + jsonified_request.update(unset_fields) + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_lookup_api_hub_instance_rest_interceptors(null_interceptor): - transport = transports.ProvisioningRestTransport( + client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.ProvisioningRestInterceptor(), + transport="rest", ) - client = ProvisioningClient(transport=transport) + request = request_type(**request_init) - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.ProvisioningRestInterceptor, "post_lookup_api_hub_instance" - ) as post, mock.patch.object( - transports.ProvisioningRestInterceptor, - "post_lookup_api_hub_instance_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.ProvisioningRestInterceptor, "pre_lookup_api_hub_instance" - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = provisioning_service.LookupApiHubInstanceRequest.pb( - provisioning_service.LookupApiHubInstanceRequest() - ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = provisioning_service.LookupApiHubInstanceResponse.to_json( - provisioning_service.LookupApiHubInstanceResponse() - ) - req.return_value.content = return_value + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) - request = provisioning_service.LookupApiHubInstanceRequest() - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = provisioning_service.LookupApiHubInstanceResponse() - post_with_metadata.return_value = ( - provisioning_service.LookupApiHubInstanceResponse(), - metadata, - ) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.lookup_api_hub_instance( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], - ) + response = client.create_api_hub_instance(request) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params -def test_get_location_rest_bad_request(request_type=locations_pb2.GetLocationRequest): - client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2"}, request +def test_create_api_hub_instance_rest_unset_required_fields(): + transport = transports.ProvisioningRestTransport( + credentials=ga_credentials.AnonymousCredentials ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_location(request) + unset_fields = transport.create_api_hub_instance._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("apiHubInstanceId",)) + & set( + ( + "parent", + "apiHubInstance", + ) + ) + ) -@pytest.mark.parametrize( - "request_type", - [ - locations_pb2.GetLocationRequest, - dict, - ], -) -def test_get_location_rest(request_type): +def test_create_api_hub_instance_rest_flattened(): client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) - request_init = {"name": "projects/sample1/locations/sample2"} - request = request_type(**request_init) # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: + with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = locations_pb2.Location() + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + api_hub_instance=common_fields.ApiHubInstance(name="name_value"), + api_hub_instance_id="api_hub_instance_id_value", + ) + mock_args.update(sample_request) # Wrap the value into a proper Response obj - response_value = mock.Mock() + response_value = Response() response_value.status_code = 200 json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - + response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_location(request) + client.create_api_hub_instance(**mock_args) - # Establish that the response is the type that we expect. - assert isinstance(response, locations_pb2.Location) + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/apiHubInstances" + % client.transport._host, + args[1], + ) -def test_list_locations_rest_bad_request( - request_type=locations_pb2.ListLocationsRequest, +def test_create_api_hub_instance_rest_flattened_error(transport: str = "rest"): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_api_hub_instance( + provisioning_service.CreateApiHubInstanceRequest(), + parent="parent_value", + api_hub_instance=common_fields.ApiHubInstance(name="name_value"), + api_hub_instance_id="api_hub_instance_id_value", + ) + + +def test_delete_api_hub_instance_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.delete_api_hub_instance + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.delete_api_hub_instance + ] = mock_rpc + + request = {} + client.delete_api_hub_instance(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + # Operation methods build a cached wrapper on first rpc call + # subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() + + client.delete_api_hub_instance(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_delete_api_hub_instance_rest_required_fields( + request_type=provisioning_service.DeleteApiHubInstanceRequest, ): + transport_class = transports.ProvisioningRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_api_hub_instance._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_api_hub_instance._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) - request = request_type() - request = json_format.ParseDict({"name": "projects/sample1"}, request) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_api_hub_instance(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_api_hub_instance_rest_unset_required_fields(): + transport = transports.ProvisioningRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_api_hub_instance._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_delete_api_hub_instance_rest_flattened(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/apiHubInstances/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): # Wrap the value into a proper Response obj response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_locations(request) + client.delete_api_hub_instance(**mock_args) -@pytest.mark.parametrize( - "request_type", - [ - locations_pb2.ListLocationsRequest, - dict, - ], -) -def test_list_locations_rest(request_type): + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/apiHubInstances/*}" + % client.transport._host, + args[1], + ) + + +def test_delete_api_hub_instance_rest_flattened_error(transport: str = "rest"): client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - request_init = {"name": "projects/sample1"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = locations_pb2.ListLocationsResponse() + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_api_hub_instance( + provisioning_service.DeleteApiHubInstanceRequest(), + name="name_value", + ) + + +def test_get_api_hub_instance_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.get_api_hub_instance in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.get_api_hub_instance + ] = mock_rpc + + request = {} + client.get_api_hub_instance(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_api_hub_instance(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_get_api_hub_instance_rest_required_fields( + request_type=provisioning_service.GetApiHubInstanceRequest, +): + transport_class = transports.ProvisioningRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_api_hub_instance._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_api_hub_instance._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = common_fields.ApiHubInstance() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.ApiHubInstance.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_api_hub_instance(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_api_hub_instance_rest_unset_required_fields(): + transport = transports.ProvisioningRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_api_hub_instance._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_get_api_hub_instance_rest_flattened(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.ApiHubInstance() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/apiHubInstances/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = common_fields.ApiHubInstance.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.get_api_hub_instance(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/apiHubInstances/*}" + % client.transport._host, + args[1], + ) + + +def test_get_api_hub_instance_rest_flattened_error(transport: str = "rest"): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_api_hub_instance( + provisioning_service.GetApiHubInstanceRequest(), + name="name_value", + ) + + +def test_lookup_api_hub_instance_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.lookup_api_hub_instance + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.lookup_api_hub_instance + ] = mock_rpc + + request = {} + client.lookup_api_hub_instance(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.lookup_api_hub_instance(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_lookup_api_hub_instance_rest_required_fields( + request_type=provisioning_service.LookupApiHubInstanceRequest, +): + transport_class = transports.ProvisioningRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).lookup_api_hub_instance._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).lookup_api_hub_instance._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = provisioning_service.LookupApiHubInstanceResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = provisioning_service.LookupApiHubInstanceResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.lookup_api_hub_instance(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_lookup_api_hub_instance_rest_unset_required_fields(): + transport = transports.ProvisioningRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.lookup_api_hub_instance._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("parent",))) + + +def test_lookup_api_hub_instance_rest_flattened(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = provisioning_service.LookupApiHubInstanceResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = provisioning_service.LookupApiHubInstanceResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.lookup_api_hub_instance(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/apiHubInstances:lookup" + % client.transport._host, + args[1], + ) + + +def test_lookup_api_hub_instance_rest_flattened_error(transport: str = "rest"): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.lookup_api_hub_instance( + provisioning_service.LookupApiHubInstanceRequest(), + parent="parent_value", + ) + + +def test_credentials_transport_error(): + # It is an error to provide credentials and a transport instance. + transport = transports.ProvisioningGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # It is an error to provide a credentials file and a transport instance. + transport = transports.ProvisioningGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = ProvisioningClient( + client_options={"credentials_file": "credentials.json"}, + transport=transport, + ) + + # It is an error to provide an api_key and a transport instance. + transport = transports.ProvisioningGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = ProvisioningClient( + client_options=options, + transport=transport, + ) + + # It is an error to provide an api_key and a credential. + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = ProvisioningClient( + client_options=options, credentials=ga_credentials.AnonymousCredentials() + ) + + # It is an error to provide scopes and a transport instance. + transport = transports.ProvisioningGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = ProvisioningClient( + client_options={"scopes": ["1", "2"]}, + transport=transport, + ) + + +def test_transport_instance(): + # A client may be instantiated with a custom transport instance. + transport = transports.ProvisioningGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + client = ProvisioningClient(transport=transport) + assert client.transport is transport + + +def test_transport_get_channel(): + # A client may be instantiated with a custom transport instance. + transport = transports.ProvisioningGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + transport = transports.ProvisioningGrpcAsyncIOTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.ProvisioningGrpcTransport, + transports.ProvisioningGrpcAsyncIOTransport, + transports.ProvisioningRestTransport, + ], +) +def test_transport_adc(transport_class): + # Test default credentials are used if not provided. + with mock.patch.object(google.auth, "default") as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class() + adc.assert_called_once() + + +def test_transport_kind_grpc(): + transport = ProvisioningClient.get_transport_class("grpc")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "grpc" + + +def test_initialize_client_w_grpc(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_api_hub_instance_empty_call_grpc(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_api_hub_instance), "__call__" + ) as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.create_api_hub_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = provisioning_service.CreateApiHubInstanceRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_api_hub_instance_empty_call_grpc(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.delete_api_hub_instance), "__call__" + ) as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.delete_api_hub_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = provisioning_service.DeleteApiHubInstanceRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_api_hub_instance_empty_call_grpc(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_api_hub_instance), "__call__" + ) as call: + call.return_value = common_fields.ApiHubInstance() + client.get_api_hub_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = provisioning_service.GetApiHubInstanceRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_lookup_api_hub_instance_empty_call_grpc(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.lookup_api_hub_instance), "__call__" + ) as call: + call.return_value = provisioning_service.LookupApiHubInstanceResponse() + client.lookup_api_hub_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = provisioning_service.LookupApiHubInstanceRequest() + + assert args[0] == request_msg + + +def test_transport_kind_grpc_asyncio(): + transport = ProvisioningAsyncClient.get_transport_class("grpc_asyncio")( + credentials=async_anonymous_credentials() + ) + assert transport.kind == "grpc_asyncio" + + +def test_initialize_client_w_grpc_asyncio(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_create_api_hub_instance_empty_call_grpc_asyncio(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_api_hub_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + await client.create_api_hub_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = provisioning_service.CreateApiHubInstanceRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_delete_api_hub_instance_empty_call_grpc_asyncio(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.delete_api_hub_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + await client.delete_api_hub_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = provisioning_service.DeleteApiHubInstanceRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_api_hub_instance_empty_call_grpc_asyncio(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_api_hub_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + common_fields.ApiHubInstance( + name="name_value", + state=common_fields.ApiHubInstance.State.INACTIVE, + state_message="state_message_value", + description="description_value", + ) + ) + await client.get_api_hub_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = provisioning_service.GetApiHubInstanceRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_lookup_api_hub_instance_empty_call_grpc_asyncio(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.lookup_api_hub_instance), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + provisioning_service.LookupApiHubInstanceResponse() + ) + await client.lookup_api_hub_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = provisioning_service.LookupApiHubInstanceRequest() + + assert args[0] == request_msg + + +def test_transport_kind_rest(): + transport = ProvisioningClient.get_transport_class("rest")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "rest" + + +def test_create_api_hub_instance_rest_bad_request( + request_type=provisioning_service.CreateApiHubInstanceRequest, +): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.create_api_hub_instance(request) + + +@pytest.mark.parametrize( + "request_type", + [ + provisioning_service.CreateApiHubInstanceRequest, + dict, + ], +) +def test_create_api_hub_instance_rest_call_success(request_type): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["api_hub_instance"] = { + "name": "name_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "state": 1, + "state_message": "state_message_value", + "config": { + "cmek_key_name": "cmek_key_name_value", + "disable_search": True, + "vertex_location": "vertex_location_value", + "encryption_type": 1, + }, + "labels": {}, + "description": "description_value", + } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = provisioning_service.CreateApiHubInstanceRequest.meta.fields[ + "api_hub_instance" + ] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["api_hub_instance"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["api_hub_instance"][field])): + del request_init["api_hub_instance"][field][i][subfield] + else: + del request_init["api_hub_instance"][field][subfield] + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.create_api_hub_instance(request) + + # Establish that the response is the type that we expect. + json_return_value = json_format.MessageToJson(return_value) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_api_hub_instance_rest_interceptors(null_interceptor): + transport = transports.ProvisioningRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ProvisioningRestInterceptor(), + ) + client = ProvisioningClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.ProvisioningRestInterceptor, "post_create_api_hub_instance" + ) as post, mock.patch.object( + transports.ProvisioningRestInterceptor, + "post_create_api_hub_instance_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.ProvisioningRestInterceptor, "pre_create_api_hub_instance" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = provisioning_service.CreateApiHubInstanceRequest.pb( + provisioning_service.CreateApiHubInstanceRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = json_format.MessageToJson(operations_pb2.Operation()) + req.return_value.content = return_value + + request = provisioning_service.CreateApiHubInstanceRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + post_with_metadata.return_value = operations_pb2.Operation(), metadata + + client.create_api_hub_instance( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_delete_api_hub_instance_rest_bad_request( + request_type=provisioning_service.DeleteApiHubInstanceRequest, +): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/apiHubInstances/sample3" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.delete_api_hub_instance(request) + + +@pytest.mark.parametrize( + "request_type", + [ + provisioning_service.DeleteApiHubInstanceRequest, + dict, + ], +) +def test_delete_api_hub_instance_rest_call_success(request_type): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/apiHubInstances/sample3" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.delete_api_hub_instance(request) + + # Establish that the response is the type that we expect. + json_return_value = json_format.MessageToJson(return_value) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_api_hub_instance_rest_interceptors(null_interceptor): + transport = transports.ProvisioningRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ProvisioningRestInterceptor(), + ) + client = ProvisioningClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.ProvisioningRestInterceptor, "post_delete_api_hub_instance" + ) as post, mock.patch.object( + transports.ProvisioningRestInterceptor, + "post_delete_api_hub_instance_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.ProvisioningRestInterceptor, "pre_delete_api_hub_instance" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = provisioning_service.DeleteApiHubInstanceRequest.pb( + provisioning_service.DeleteApiHubInstanceRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = json_format.MessageToJson(operations_pb2.Operation()) + req.return_value.content = return_value + + request = provisioning_service.DeleteApiHubInstanceRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + post_with_metadata.return_value = operations_pb2.Operation(), metadata + + client.delete_api_hub_instance( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_get_api_hub_instance_rest_bad_request( + request_type=provisioning_service.GetApiHubInstanceRequest, +): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/apiHubInstances/sample3" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_api_hub_instance(request) + + +@pytest.mark.parametrize( + "request_type", + [ + provisioning_service.GetApiHubInstanceRequest, + dict, + ], +) +def test_get_api_hub_instance_rest_call_success(request_type): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/apiHubInstances/sample3" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = common_fields.ApiHubInstance( + name="name_value", + state=common_fields.ApiHubInstance.State.INACTIVE, + state_message="state_message_value", + description="description_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = common_fields.ApiHubInstance.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.get_api_hub_instance(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, common_fields.ApiHubInstance) + assert response.name == "name_value" + assert response.state == common_fields.ApiHubInstance.State.INACTIVE + assert response.state_message == "state_message_value" + assert response.description == "description_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_api_hub_instance_rest_interceptors(null_interceptor): + transport = transports.ProvisioningRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ProvisioningRestInterceptor(), + ) + client = ProvisioningClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ProvisioningRestInterceptor, "post_get_api_hub_instance" + ) as post, mock.patch.object( + transports.ProvisioningRestInterceptor, + "post_get_api_hub_instance_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.ProvisioningRestInterceptor, "pre_get_api_hub_instance" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = provisioning_service.GetApiHubInstanceRequest.pb( + provisioning_service.GetApiHubInstanceRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = common_fields.ApiHubInstance.to_json( + common_fields.ApiHubInstance() + ) + req.return_value.content = return_value + + request = provisioning_service.GetApiHubInstanceRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = common_fields.ApiHubInstance() + post_with_metadata.return_value = common_fields.ApiHubInstance(), metadata + + client.get_api_hub_instance( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_lookup_api_hub_instance_rest_bad_request( + request_type=provisioning_service.LookupApiHubInstanceRequest, +): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.lookup_api_hub_instance(request) + + +@pytest.mark.parametrize( + "request_type", + [ + provisioning_service.LookupApiHubInstanceRequest, + dict, + ], +) +def test_lookup_api_hub_instance_rest_call_success(request_type): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = provisioning_service.LookupApiHubInstanceResponse() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = provisioning_service.LookupApiHubInstanceResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.lookup_api_hub_instance(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, provisioning_service.LookupApiHubInstanceResponse) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_lookup_api_hub_instance_rest_interceptors(null_interceptor): + transport = transports.ProvisioningRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.ProvisioningRestInterceptor(), + ) + client = ProvisioningClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.ProvisioningRestInterceptor, "post_lookup_api_hub_instance" + ) as post, mock.patch.object( + transports.ProvisioningRestInterceptor, + "post_lookup_api_hub_instance_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.ProvisioningRestInterceptor, "pre_lookup_api_hub_instance" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = provisioning_service.LookupApiHubInstanceRequest.pb( + provisioning_service.LookupApiHubInstanceRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = provisioning_service.LookupApiHubInstanceResponse.to_json( + provisioning_service.LookupApiHubInstanceResponse() + ) + req.return_value.content = return_value + + request = provisioning_service.LookupApiHubInstanceRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = provisioning_service.LookupApiHubInstanceResponse() + post_with_metadata.return_value = ( + provisioning_service.LookupApiHubInstanceResponse(), + metadata, + ) + + client.lookup_api_hub_instance( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_get_location_rest_bad_request(request_type=locations_pb2.GetLocationRequest): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_location(request) + + +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.GetLocationRequest, + dict, + ], +) +def test_get_location_rest(request_type): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = locations_pb2.Location() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_location(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) + + +def test_list_locations_rest_bad_request( + request_type=locations_pb2.ListLocationsRequest, +): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict({"name": "projects/sample1"}, request) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_locations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.ListLocationsRequest, + dict, + ], +) +def test_list_locations_rest(request_type): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = locations_pb2.ListLocationsResponse() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_locations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + +def test_cancel_operation_rest_bad_request( + request_type=operations_pb2.CancelOperationRequest, +): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.cancel_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.CancelOperationRequest, + dict, + ], +) +def test_cancel_operation_rest(request_type): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "{}" + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.cancel_operation(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_delete_operation_rest_bad_request( + request_type=operations_pb2.DeleteOperationRequest, +): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.delete_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.DeleteOperationRequest, + dict, + ], +) +def test_delete_operation_rest(request_type): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "{}" + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_operation(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_get_operation_rest_bad_request( + request_type=operations_pb2.GetOperationRequest, +): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.GetOperationRequest, + dict, + ], +) +def test_get_operation_rest(request_type): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_operation(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) + + +def test_list_operations_rest_bad_request( + request_type=operations_pb2.ListOperationsRequest, +): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_operations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.ListOperationsRequest, + dict, + ], +) +def test_list_operations_rest(request_type): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.ListOperationsResponse() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_operations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) + + +def test_initialize_client_w_rest(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_api_hub_instance_empty_call_rest(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_api_hub_instance), "__call__" + ) as call: + client.create_api_hub_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = provisioning_service.CreateApiHubInstanceRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_api_hub_instance_empty_call_rest(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.delete_api_hub_instance), "__call__" + ) as call: + client.delete_api_hub_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = provisioning_service.DeleteApiHubInstanceRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_api_hub_instance_empty_call_rest(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_api_hub_instance), "__call__" + ) as call: + client.get_api_hub_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = provisioning_service.GetApiHubInstanceRequest() + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_lookup_api_hub_instance_empty_call_rest(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.lookup_api_hub_instance), "__call__" + ) as call: + client.lookup_api_hub_instance(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = provisioning_service.LookupApiHubInstanceRequest() + + assert args[0] == request_msg + + +def test_provisioning_rest_lro_client(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + transport = client.transport + + # Ensure that we have an api-core operations client. + assert isinstance( + transport.operations_client, + operations_v1.AbstractOperationsClient, + ) + + # Ensure that subsequent calls to the property send the exact same object. + assert transport.operations_client is transport.operations_client + + +def test_transport_grpc_default(): + # A client should use the gRPC transport by default. + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert isinstance( + client.transport, + transports.ProvisioningGrpcTransport, + ) + + +def test_provisioning_base_transport_error(): + # Passing both a credentials object and credentials_file should raise an error + with pytest.raises(core_exceptions.DuplicateCredentialArgs): + transport = transports.ProvisioningTransport( + credentials=ga_credentials.AnonymousCredentials(), + credentials_file="credentials.json", + ) + + +def test_provisioning_base_transport(): + # Instantiate the base transport. + with mock.patch( + "google.cloud.apihub_v1.services.provisioning.transports.ProvisioningTransport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.ProvisioningTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Every method on the transport should just blindly + # raise NotImplementedError. + methods = ( + "create_api_hub_instance", + "delete_api_hub_instance", + "get_api_hub_instance", + "lookup_api_hub_instance", + "get_location", + "list_locations", + "get_operation", + "cancel_operation", + "delete_operation", + "list_operations", + ) + for method in methods: + with pytest.raises(NotImplementedError): + getattr(transport, method)(request=object()) + + with pytest.raises(NotImplementedError): + transport.close() + + # Additionally, the LRO client (a property) should + # also raise NotImplementedError + with pytest.raises(NotImplementedError): + transport.operations_client + + # Catch all for all remaining methods and properties + remainder = [ + "kind", + ] + for r in remainder: + with pytest.raises(NotImplementedError): + getattr(transport, r)() + + +def test_provisioning_base_transport_with_credentials_file(): + # Instantiate the base transport with a credentials file + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch( + "google.cloud.apihub_v1.services.provisioning.transports.ProvisioningTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) + transport = transports.ProvisioningTransport( + credentials_file="credentials.json", + quota_project_id="octopus", + ) + load_creds.assert_called_once_with( + "credentials.json", + scopes=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", + ) + + +def test_provisioning_base_transport_with_adc(): + # Test the default credentials are used if credentials and credentials_file are None. + with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch( + "google.cloud.apihub_v1.services.provisioning.transports.ProvisioningTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport = transports.ProvisioningTransport() + adc.assert_called_once() + + +def test_provisioning_auth_adc(): + # If no credentials are provided, we should use ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + ProvisioningClient() + adc.assert_called_once_with( + scopes=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id=None, + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.ProvisioningGrpcTransport, + transports.ProvisioningGrpcAsyncIOTransport, + ], +) +def test_provisioning_transport_auth_adc(transport_class): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + adc.assert_called_once_with( + scopes=["1", "2"], + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.ProvisioningGrpcTransport, + transports.ProvisioningGrpcAsyncIOTransport, + transports.ProvisioningRestTransport, + ], +) +def test_provisioning_transport_auth_gdch_credentials(transport_class): + host = "https://language.com" + api_audience_tests = [None, "https://language2.com"] + api_audience_expect = [host, "https://language2.com"] + for t, e in zip(api_audience_tests, api_audience_expect): + with mock.patch.object(google.auth, "default", autospec=True) as adc: + gdch_mock = mock.MagicMock() + type(gdch_mock).with_gdch_audience = mock.PropertyMock( + return_value=gdch_mock + ) + adc.return_value = (gdch_mock, None) + transport_class(host=host, api_audience=t) + gdch_mock.with_gdch_audience.assert_called_once_with(e) + + +@pytest.mark.parametrize( + "transport_class,grpc_helpers", + [ + (transports.ProvisioningGrpcTransport, grpc_helpers), + (transports.ProvisioningGrpcAsyncIOTransport, grpc_helpers_async), + ], +) +def test_provisioning_transport_create_channel(transport_class, grpc_helpers): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel", autospec=True + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + adc.return_value = (creds, None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + + create_channel.assert_called_with( + "apihub.googleapis.com:443", + credentials=creds, + credentials_file=None, + quota_project_id="octopus", + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + scopes=["1", "2"], + default_host="apihub.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + +@pytest.mark.parametrize( + "transport_class", + [transports.ProvisioningGrpcTransport, transports.ProvisioningGrpcAsyncIOTransport], +) +def test_provisioning_grpc_transport_client_cert_source_for_mtls(transport_class): + cred = ga_credentials.AnonymousCredentials() + + # Check ssl_channel_credentials is used if provided. + with mock.patch.object(transport_class, "create_channel") as mock_create_channel: + mock_ssl_channel_creds = mock.Mock() + transport_class( + host="squid.clam.whelk", + credentials=cred, + ssl_channel_credentials=mock_ssl_channel_creds, + ) + mock_create_channel.assert_called_once_with( + "squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_channel_creds, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + # Check if ssl_channel_credentials is not provided, then client_cert_source_for_mtls + # is used. + with mock.patch.object(transport_class, "create_channel", return_value=mock.Mock()): + with mock.patch("grpc.ssl_channel_credentials") as mock_ssl_cred: + transport_class( + credentials=cred, + client_cert_source_for_mtls=client_cert_source_callback, + ) + expected_cert, expected_key = client_cert_source_callback() + mock_ssl_cred.assert_called_once_with( + certificate_chain=expected_cert, private_key=expected_key + ) + + +def test_provisioning_http_transport_client_cert_source_for_mtls(): + cred = ga_credentials.AnonymousCredentials() + with mock.patch( + "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" + ) as mock_configure_mtls_channel: + transports.ProvisioningRestTransport( + credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + ) + mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + + +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "grpc_asyncio", + "rest", + ], +) +def test_provisioning_host_no_port(transport_name): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="apihub.googleapis.com" + ), + transport=transport_name, + ) + assert client.transport._host == ( + "apihub.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://apihub.googleapis.com" + ) + + +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "grpc_asyncio", + "rest", + ], +) +def test_provisioning_host_with_port(transport_name): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="apihub.googleapis.com:8000" + ), + transport=transport_name, + ) + assert client.transport._host == ( + "apihub.googleapis.com:8000" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://apihub.googleapis.com:8000" + ) + + +@pytest.mark.parametrize( + "transport_name", + [ + "rest", + ], +) +def test_provisioning_client_transport_session_collision(transport_name): + creds1 = ga_credentials.AnonymousCredentials() + creds2 = ga_credentials.AnonymousCredentials() + client1 = ProvisioningClient( + credentials=creds1, + transport=transport_name, + ) + client2 = ProvisioningClient( + credentials=creds2, + transport=transport_name, + ) + session1 = client1.transport.create_api_hub_instance._session + session2 = client2.transport.create_api_hub_instance._session + assert session1 != session2 + session1 = client1.transport.delete_api_hub_instance._session + session2 = client2.transport.delete_api_hub_instance._session + assert session1 != session2 + session1 = client1.transport.get_api_hub_instance._session + session2 = client2.transport.get_api_hub_instance._session + assert session1 != session2 + session1 = client1.transport.lookup_api_hub_instance._session + session2 = client2.transport.lookup_api_hub_instance._session + assert session1 != session2 + + +def test_provisioning_grpc_transport_channel(): + channel = grpc.secure_channel("http://localhost/", grpc.local_channel_credentials()) + + # Check that channel is used if provided. + transport = transports.ProvisioningGrpcTransport( + host="squid.clam.whelk", + channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None + + +def test_provisioning_grpc_asyncio_transport_channel(): + channel = aio.secure_channel("http://localhost/", grpc.local_channel_credentials()) + + # Check that channel is used if provided. + transport = transports.ProvisioningGrpcAsyncIOTransport( + host="squid.clam.whelk", + channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None + + +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. +@pytest.mark.parametrize( + "transport_class", + [transports.ProvisioningGrpcTransport, transports.ProvisioningGrpcAsyncIOTransport], +) +def test_provisioning_transport_channel_mtls_with_client_cert_source(transport_class): + with mock.patch( + "grpc.ssl_channel_credentials", autospec=True + ) as grpc_ssl_channel_cred: + with mock.patch.object( + transport_class, "create_channel" + ) 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 = ga_credentials.AnonymousCredentials() + with pytest.warns(DeprecationWarning): + with mock.patch.object(google.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=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + assert transport._ssl_channel_credentials == mock_ssl_cred + + +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. +@pytest.mark.parametrize( + "transport_class", + [transports.ProvisioningGrpcTransport, transports.ProvisioningGrpcAsyncIOTransport], +) +def test_provisioning_transport_channel_mtls_with_adc(transport_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), + ): + with mock.patch.object( + transport_class, "create_channel" + ) 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, + ) + + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=mock_cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + + +def test_provisioning_grpc_lro_client(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + transport = client.transport + + # Ensure that we have a api-core operations client. + assert isinstance( + transport.operations_client, + operations_v1.OperationsClient, + ) + + # Ensure that subsequent calls to the property send the exact same object. + assert transport.operations_client is transport.operations_client + + +def test_provisioning_grpc_lro_async_client(): + client = ProvisioningAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc_asyncio", + ) + transport = client.transport + + # Ensure that we have a api-core operations client. + assert isinstance( + transport.operations_client, + operations_v1.OperationsAsyncClient, + ) + + # Ensure that subsequent calls to the property send the exact same object. + assert transport.operations_client is transport.operations_client + + +def test_api_hub_instance_path(): + project = "squid" + location = "clam" + api_hub_instance = "whelk" + expected = "projects/{project}/locations/{location}/apiHubInstances/{api_hub_instance}".format( + project=project, + location=location, + api_hub_instance=api_hub_instance, + ) + actual = ProvisioningClient.api_hub_instance_path( + project, location, api_hub_instance + ) + assert expected == actual + + +def test_parse_api_hub_instance_path(): + expected = { + "project": "octopus", + "location": "oyster", + "api_hub_instance": "nudibranch", + } + path = ProvisioningClient.api_hub_instance_path(**expected) + + # Check that the path construction is reversible. + actual = ProvisioningClient.parse_api_hub_instance_path(path) + assert expected == actual + + +def test_common_billing_account_path(): + billing_account = "cuttlefish" + expected = "billingAccounts/{billing_account}".format( + billing_account=billing_account, + ) + actual = ProvisioningClient.common_billing_account_path(billing_account) + assert expected == actual + + +def test_parse_common_billing_account_path(): + expected = { + "billing_account": "mussel", + } + path = ProvisioningClient.common_billing_account_path(**expected) + + # Check that the path construction is reversible. + actual = ProvisioningClient.parse_common_billing_account_path(path) + assert expected == actual + + +def test_common_folder_path(): + folder = "winkle" + expected = "folders/{folder}".format( + folder=folder, + ) + actual = ProvisioningClient.common_folder_path(folder) + assert expected == actual + + +def test_parse_common_folder_path(): + expected = { + "folder": "nautilus", + } + path = ProvisioningClient.common_folder_path(**expected) + + # Check that the path construction is reversible. + actual = ProvisioningClient.parse_common_folder_path(path) + assert expected == actual + + +def test_common_organization_path(): + organization = "scallop" + expected = "organizations/{organization}".format( + organization=organization, + ) + actual = ProvisioningClient.common_organization_path(organization) + assert expected == actual + + +def test_parse_common_organization_path(): + expected = { + "organization": "abalone", + } + path = ProvisioningClient.common_organization_path(**expected) + + # Check that the path construction is reversible. + actual = ProvisioningClient.parse_common_organization_path(path) + assert expected == actual + + +def test_common_project_path(): + project = "squid" + expected = "projects/{project}".format( + project=project, + ) + actual = ProvisioningClient.common_project_path(project) + assert expected == actual + + +def test_parse_common_project_path(): + expected = { + "project": "clam", + } + path = ProvisioningClient.common_project_path(**expected) + + # Check that the path construction is reversible. + actual = ProvisioningClient.parse_common_project_path(path) + assert expected == actual + + +def test_common_location_path(): + project = "whelk" + location = "octopus" + expected = "projects/{project}/locations/{location}".format( + project=project, + location=location, + ) + actual = ProvisioningClient.common_location_path(project, location) + assert expected == actual + + +def test_parse_common_location_path(): + expected = { + "project": "oyster", + "location": "nudibranch", + } + path = ProvisioningClient.common_location_path(**expected) + + # Check that the path construction is reversible. + actual = ProvisioningClient.parse_common_location_path(path) + assert expected == actual - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +def test_client_with_default_client_info(): + client_info = gapic_v1.client_info.ClientInfo() - response = client.list_locations(request) + with mock.patch.object( + transports.ProvisioningTransport, "_prep_wrapped_messages" + ) as prep: + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + client_info=client_info, + ) + prep.assert_called_once_with(client_info) - # Establish that the response is the type that we expect. - assert isinstance(response, locations_pb2.ListLocationsResponse) + with mock.patch.object( + transports.ProvisioningTransport, "_prep_wrapped_messages" + ) as prep: + transport_class = ProvisioningClient.get_transport_class() + transport = transport_class( + credentials=ga_credentials.AnonymousCredentials(), + client_info=client_info, + ) + prep.assert_called_once_with(client_info) -def test_cancel_operation_rest_bad_request( - request_type=operations_pb2.CancelOperationRequest, -): +def test_delete_operation(transport: str = "grpc"): client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + transport=transport, ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.cancel_operation(request) + # 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 = operations_pb2.DeleteOperationRequest() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.delete_operation(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 -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.CancelOperationRequest, - dict, - ], -) -def test_cancel_operation_rest(request_type): - client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + # Establish that the response is the type that we expect. + assert response is None - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = None - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "{}" - response_value.content = json_return_value.encode("UTF-8") +@pytest.mark.asyncio +async def test_delete_operation_async(transport: str = "grpc_asyncio"): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # 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 = operations_pb2.DeleteOperationRequest() - response = client.cancel_operation(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_operation(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 response is the type that we expect. assert response is None -def test_delete_operation_rest_bad_request( - request_type=operations_pb2.DeleteOperationRequest, -): +def test_delete_operation_field_headers(): client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_operation(request) + # 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 = operations_pb2.DeleteOperationRequest() + request.name = "locations" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + call.return_value = None -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.DeleteOperationRequest, - dict, - ], -) -def test_delete_operation_rest(request_type): - client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + client.delete_operation(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 - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = None + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "{}" - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +@pytest.mark.asyncio +async def test_delete_operation_field_headers_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + ) - response = client.delete_operation(request) + # 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 = operations_pb2.DeleteOperationRequest() + request.name = "locations" - # Establish that the response is the type that we expect. - assert response is None + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_operation(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", + "name=locations", + ) in kw["metadata"] -def test_get_operation_rest_bad_request( - request_type=operations_pb2.GetOperationRequest, -): +def test_delete_operation_from_dict(): client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + + response = client.delete_operation( + request={ + "name": "locations", + } + ) + call.assert_called() - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_operation(request) +@pytest.mark.asyncio +async def test_delete_operation_from_dict_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.GetOperationRequest, - dict, - ], -) -def test_get_operation_rest(request_type): + +def test_cancel_operation(transport: str = "grpc"): client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation() + # 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 = operations_pb2.CancelOperationRequest() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.cancel_operation(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 - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert response is None - response = client.get_operation(request) + +@pytest.mark.asyncio +async def test_cancel_operation_async(transport: str = "grpc_asyncio"): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.CancelOperationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.cancel_operation(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 response is the type that we expect. - assert isinstance(response, operations_pb2.Operation) + assert response is None -def test_list_operations_rest_bad_request( - request_type=operations_pb2.ListOperationsRequest, -): +def test_cancel_operation_field_headers(): client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2"}, request ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_operations(request) + # 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 = operations_pb2.CancelOperationRequest() + request.name = "locations" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + call.return_value = None -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.ListOperationsRequest, - dict, - ], -) -def test_list_operations_rest(request_type): - client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + client.cancel_operation(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", + "name=locations", + ) in kw["metadata"] - request_init = {"name": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.ListOperationsResponse() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") +@pytest.mark.asyncio +async def test_cancel_operation_field_headers_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + ) - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # 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 = operations_pb2.CancelOperationRequest() + request.name = "locations" - response = client.list_operations(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.cancel_operation(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 response is the type that we expect. - assert isinstance(response, operations_pb2.ListOperationsResponse) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -def test_initialize_client_w_rest(): +def test_cancel_operation_from_dict(): client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), ) - assert client is not None + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + + response = client.cancel_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_create_api_hub_instance_empty_call_rest(): +@pytest.mark.asyncio +async def test_cancel_operation_from_dict_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.cancel_operation( + request={ + "name": "locations", + } + ) + call.assert_called() + + +def test_get_operation(transport: str = "grpc"): client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.create_api_hub_instance), "__call__" - ) as call: - client.create_api_hub_instance(request=None) + # 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 = operations_pb2.GetOperationRequest() - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation() + response = client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = provisioning_service.CreateApiHubInstanceRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_delete_api_hub_instance_empty_call_rest(): - client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +@pytest.mark.asyncio +async def test_get_operation_async(transport: str = "grpc_asyncio"): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.delete_api_hub_instance), "__call__" - ) as call: - client.delete_api_hub_instance(request=None) + # 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 = operations_pb2.GetOperationRequest() - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + response = await client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = provisioning_service.DeleteApiHubInstanceRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_get_api_hub_instance_empty_call_rest(): +def test_get_operation_field_headers(): client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.get_api_hub_instance), "__call__" - ) as call: - client.get_api_hub_instance(request=None) + # 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 = operations_pb2.GetOperationRequest() + request.name = "locations" - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + call.return_value = operations_pb2.Operation() + + client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = provisioning_service.GetApiHubInstanceRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_lookup_api_hub_instance_empty_call_rest(): - client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +@pytest.mark.asyncio +async def test_get_operation_field_headers_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.lookup_api_hub_instance), "__call__" - ) as call: - client.lookup_api_hub_instance(request=None) + # 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 = operations_pb2.GetOperationRequest() + request.name = "locations" - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + await client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = provisioning_service.LookupApiHubInstanceRequest() + assert args[0] == request - assert args[0] == request_msg + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -def test_provisioning_rest_lro_client(): +def test_get_operation_from_dict(): client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - transport = client.transport + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation() + + response = client.get_operation( + request={ + "name": "locations", + } + ) + call.assert_called() - # Ensure that we have an api-core operations client. - assert isinstance( - transport.operations_client, - operations_v1.AbstractOperationsClient, + +@pytest.mark.asyncio +async def test_get_operation_from_dict_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + response = await client.get_operation( + request={ + "name": "locations", + } + ) + call.assert_called() - # Ensure that subsequent calls to the property send the exact same object. - assert transport.operations_client is transport.operations_client +def test_list_operations(transport: str = "grpc"): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) -def test_provisioning_base_transport_error(): - # Passing both a credentials object and credentials_file should raise an error - with pytest.raises(core_exceptions.DuplicateCredentialArgs): - transport = transports.ProvisioningTransport( - credentials=ga_credentials.AnonymousCredentials(), - credentials_file="credentials.json", - ) + # 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 = operations_pb2.ListOperationsRequest() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.ListOperationsResponse() + response = client.list_operations(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 -def test_provisioning_base_transport(): - # Instantiate the base transport. - with mock.patch( - "google.cloud.apihub_v1.services.provisioning.transports.ProvisioningTransport.__init__" - ) as Transport: - Transport.return_value = None - transport = transports.ProvisioningTransport( - credentials=ga_credentials.AnonymousCredentials(), + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) + + +@pytest.mark.asyncio +async def test_list_operations_async(transport: str = "grpc_asyncio"): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.ListOperationsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() ) + response = await client.list_operations(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 - # Every method on the transport should just blindly - # raise NotImplementedError. - methods = ( - "create_api_hub_instance", - "delete_api_hub_instance", - "get_api_hub_instance", - "lookup_api_hub_instance", - "get_location", - "list_locations", - "get_operation", - "cancel_operation", - "delete_operation", - "list_operations", + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) + + +def test_list_operations_field_headers(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), ) - for method in methods: - with pytest.raises(NotImplementedError): - getattr(transport, method)(request=object()) - with pytest.raises(NotImplementedError): - transport.close() + # 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 = operations_pb2.ListOperationsRequest() + request.name = "locations" - # Additionally, the LRO client (a property) should - # also raise NotImplementedError - with pytest.raises(NotImplementedError): - transport.operations_client + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + call.return_value = operations_pb2.ListOperationsResponse() - # Catch all for all remaining methods and properties - remainder = [ - "kind", - ] - for r in remainder: - with pytest.raises(NotImplementedError): - getattr(transport, r)() + client.list_operations(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", + "name=locations", + ) in kw["metadata"] -def test_provisioning_base_transport_with_credentials_file(): - # Instantiate the base transport with a credentials file - with mock.patch.object( - google.auth, "load_credentials_from_file", autospec=True - ) as load_creds, mock.patch( - "google.cloud.apihub_v1.services.provisioning.transports.ProvisioningTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None - load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) - transport = transports.ProvisioningTransport( - credentials_file="credentials.json", - quota_project_id="octopus", - ) - load_creds.assert_called_once_with( - "credentials.json", - scopes=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id="octopus", - ) +@pytest.mark.asyncio +async def test_list_operations_field_headers_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + ) -def test_provisioning_base_transport_with_adc(): - # Test the default credentials are used if credentials and credentials_file are None. - with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch( - "google.cloud.apihub_v1.services.provisioning.transports.ProvisioningTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport = transports.ProvisioningTransport() - adc.assert_called_once() + # 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 = operations_pb2.ListOperationsRequest() + request.name = "locations" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() + ) + await client.list_operations(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 -def test_provisioning_auth_adc(): - # If no credentials are provided, we should use ADC credentials. - with mock.patch.object(google.auth, "default", autospec=True) as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - ProvisioningClient() - adc.assert_called_once_with( - scopes=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id=None, + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] + + +def test_list_operations_from_dict(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.ListOperationsResponse() + + response = client.list_operations( + request={ + "name": "locations", + } ) + call.assert_called() -def test_provisioning_http_transport_client_cert_source_for_mtls(): - cred = ga_credentials.AnonymousCredentials() - with mock.patch( - "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" - ) as mock_configure_mtls_channel: - transports.ProvisioningRestTransport( - credentials=cred, client_cert_source_for_mtls=client_cert_source_callback +@pytest.mark.asyncio +async def test_list_operations_from_dict_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() ) - mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + response = await client.list_operations( + request={ + "name": "locations", + } + ) + call.assert_called() -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_provisioning_host_no_port(transport_name): +def test_list_locations(transport: str = "grpc"): client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - client_options=client_options.ClientOptions( - api_endpoint="apihub.googleapis.com" - ), - transport=transport_name, + transport=transport, ) - assert client.transport._host == ( - "apihub.googleapis.com:443" - if transport_name in ["grpc", "grpc_asyncio"] - else "https://apihub.googleapis.com" + + # 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 = locations_pb2.ListLocationsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.ListLocationsResponse() + response = client.list_locations(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 response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + +@pytest.mark.asyncio +async def test_list_locations_async(transport: str = "grpc_asyncio"): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + 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 = locations_pb2.ListLocationsRequest() -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_provisioning_host_with_port(transport_name): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + response = await client.list_locations(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 response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + +def test_list_locations_field_headers(): client = ProvisioningClient( credentials=ga_credentials.AnonymousCredentials(), - client_options=client_options.ClientOptions( - api_endpoint="apihub.googleapis.com:8000" - ), - transport=transport_name, - ) - assert client.transport._host == ( - "apihub.googleapis.com:8000" - if transport_name in ["grpc", "grpc_asyncio"] - else "https://apihub.googleapis.com:8000" ) + # 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 = locations_pb2.ListLocationsRequest() + request.name = "locations" -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_provisioning_client_transport_session_collision(transport_name): - creds1 = ga_credentials.AnonymousCredentials() - creds2 = ga_credentials.AnonymousCredentials() - client1 = ProvisioningClient( - credentials=creds1, - transport=transport_name, - ) - client2 = ProvisioningClient( - credentials=creds2, - transport=transport_name, - ) - session1 = client1.transport.create_api_hub_instance._session - session2 = client2.transport.create_api_hub_instance._session - assert session1 != session2 - session1 = client1.transport.delete_api_hub_instance._session - session2 = client2.transport.delete_api_hub_instance._session - assert session1 != session2 - session1 = client1.transport.get_api_hub_instance._session - session2 = client2.transport.get_api_hub_instance._session - assert session1 != session2 - session1 = client1.transport.lookup_api_hub_instance._session - session2 = client2.transport.lookup_api_hub_instance._session - assert session1 != session2 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + call.return_value = locations_pb2.ListLocationsResponse() + client.list_locations(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 -def test_api_hub_instance_path(): - project = "squid" - location = "clam" - api_hub_instance = "whelk" - expected = "projects/{project}/locations/{location}/apiHubInstances/{api_hub_instance}".format( - project=project, - location=location, - api_hub_instance=api_hub_instance, - ) - actual = ProvisioningClient.api_hub_instance_path( - project, location, api_hub_instance + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_list_locations_field_headers_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), ) - assert expected == actual + # 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 = locations_pb2.ListLocationsRequest() + request.name = "locations" -def test_parse_api_hub_instance_path(): - expected = { - "project": "octopus", - "location": "oyster", - "api_hub_instance": "nudibranch", - } - path = ProvisioningClient.api_hub_instance_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + await client.list_locations(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 - # Check that the path construction is reversible. - actual = ProvisioningClient.parse_api_hub_instance_path(path) - assert expected == actual + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -def test_common_billing_account_path(): - billing_account = "cuttlefish" - expected = "billingAccounts/{billing_account}".format( - billing_account=billing_account, +def test_list_locations_from_dict(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), ) - actual = ProvisioningClient.common_billing_account_path(billing_account) - assert expected == actual - + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.ListLocationsResponse() + + response = client.list_locations( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_parse_common_billing_account_path(): - expected = { - "billing_account": "mussel", - } - path = ProvisioningClient.common_billing_account_path(**expected) - # Check that the path construction is reversible. - actual = ProvisioningClient.parse_common_billing_account_path(path) - assert expected == actual +@pytest.mark.asyncio +async def test_list_locations_from_dict_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + response = await client.list_locations( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_common_folder_path(): - folder = "winkle" - expected = "folders/{folder}".format( - folder=folder, +def test_get_location(transport: str = "grpc"): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - actual = ProvisioningClient.common_folder_path(folder) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() -def test_parse_common_folder_path(): - expected = { - "folder": "nautilus", - } - path = ProvisioningClient.common_folder_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.Location() + response = client.get_location(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 - # Check that the path construction is reversible. - actual = ProvisioningClient.parse_common_folder_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) -def test_common_organization_path(): - organization = "scallop" - expected = "organizations/{organization}".format( - organization=organization, +@pytest.mark.asyncio +async def test_get_location_async(transport: str = "grpc_asyncio"): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - actual = ProvisioningClient.common_organization_path(organization) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() -def test_parse_common_organization_path(): - expected = { - "organization": "abalone", - } - path = ProvisioningClient.common_organization_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + response = await client.get_location(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 - # Check that the path construction is reversible. - actual = ProvisioningClient.parse_common_organization_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) -def test_common_project_path(): - project = "squid" - expected = "projects/{project}".format( - project=project, - ) - actual = ProvisioningClient.common_project_path(project) - assert expected == actual +def test_get_location_field_headers(): + client = ProvisioningClient(credentials=ga_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 = locations_pb2.GetLocationRequest() + request.name = "locations/abc" -def test_parse_common_project_path(): - expected = { - "project": "clam", - } - path = ProvisioningClient.common_project_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + call.return_value = locations_pb2.Location() - # Check that the path construction is reversible. - actual = ProvisioningClient.parse_common_project_path(path) - assert expected == actual + client.get_location(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", + "name=locations/abc", + ) in kw["metadata"] -def test_common_location_path(): - project = "whelk" - location = "octopus" - expected = "projects/{project}/locations/{location}".format( - project=project, - location=location, - ) - actual = ProvisioningClient.common_location_path(project, location) - assert expected == actual +@pytest.mark.asyncio +async def test_get_location_field_headers_async(): + client = ProvisioningAsyncClient(credentials=async_anonymous_credentials()) -def test_parse_common_location_path(): - expected = { - "project": "oyster", - "location": "nudibranch", - } - path = ProvisioningClient.common_location_path(**expected) + # 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 = locations_pb2.GetLocationRequest() + request.name = "locations/abc" - # Check that the path construction is reversible. - actual = ProvisioningClient.parse_common_location_path(path) - assert expected == actual + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + await client.get_location(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", + "name=locations/abc", + ) in kw["metadata"] -def test_client_with_default_client_info(): - client_info = gapic_v1.client_info.ClientInfo() - with mock.patch.object( - transports.ProvisioningTransport, "_prep_wrapped_messages" - ) as prep: - client = ProvisioningClient( - credentials=ga_credentials.AnonymousCredentials(), - client_info=client_info, +def test_get_location_from_dict(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.Location() + + response = client.get_location( + request={ + "name": "locations/abc", + } ) - prep.assert_called_once_with(client_info) + call.assert_called() - with mock.patch.object( - transports.ProvisioningTransport, "_prep_wrapped_messages" - ) as prep: - transport_class = ProvisioningClient.get_transport_class() - transport = transport_class( - credentials=ga_credentials.AnonymousCredentials(), - client_info=client_info, + +@pytest.mark.asyncio +async def test_get_location_from_dict_async(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() ) - prep.assert_called_once_with(client_info) + response = await client.get_location( + request={ + "name": "locations", + } + ) + call.assert_called() + + +def test_transport_close_grpc(): + client = ProvisioningClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + with mock.patch.object( + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + +@pytest.mark.asyncio +async def test_transport_close_grpc_asyncio(): + client = ProvisioningAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) + with mock.patch.object( + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + async with client: + close.assert_not_called() + close.assert_called_once() def test_transport_close_rest(): @@ -3210,6 +6195,7 @@ def test_transport_close_rest(): def test_client_ctx(): transports = [ "rest", + "grpc", ] for transport in transports: client = ProvisioningClient( @@ -3226,7 +6212,8 @@ def test_client_ctx(): @pytest.mark.parametrize( "client_class,transport_class", [ - (ProvisioningClient, transports.ProvisioningRestTransport), + (ProvisioningClient, transports.ProvisioningGrpcTransport), + (ProvisioningAsyncClient, transports.ProvisioningGrpcAsyncIOTransport), ], ) def test_api_key_credentials(client_class, transport_class): diff --git a/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_runtime_project_attachment_service.py b/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_runtime_project_attachment_service.py index a251212fb3c2..7cc691dddc8e 100644 --- a/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_runtime_project_attachment_service.py +++ b/packages/google-cloud-apihub/tests/unit/gapic/apihub_v1/test_runtime_project_attachment_service.py @@ -56,6 +56,7 @@ from google.protobuf import timestamp_pb2 # type: ignore from google.cloud.apihub_v1.services.runtime_project_attachment_service import ( + RuntimeProjectAttachmentServiceAsyncClient, RuntimeProjectAttachmentServiceClient, pagers, transports, @@ -263,6 +264,11 @@ def test__get_client_cert_source(): "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(RuntimeProjectAttachmentServiceClient), ) +@mock.patch.object( + RuntimeProjectAttachmentServiceAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(RuntimeProjectAttachmentServiceAsyncClient), +) def test__get_api_endpoint(): api_override = "foo.com" mock_client_cert_source = mock.Mock() @@ -404,6 +410,8 @@ def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code): @pytest.mark.parametrize( "client_class,transport_name", [ + (RuntimeProjectAttachmentServiceClient, "grpc"), + (RuntimeProjectAttachmentServiceAsyncClient, "grpc_asyncio"), (RuntimeProjectAttachmentServiceClient, "rest"), ], ) @@ -430,6 +438,11 @@ def test_runtime_project_attachment_service_client_from_service_account_info( @pytest.mark.parametrize( "transport_class,transport_name", [ + (transports.RuntimeProjectAttachmentServiceGrpcTransport, "grpc"), + ( + transports.RuntimeProjectAttachmentServiceGrpcAsyncIOTransport, + "grpc_asyncio", + ), (transports.RuntimeProjectAttachmentServiceRestTransport, "rest"), ], ) @@ -454,6 +467,8 @@ def test_runtime_project_attachment_service_client_service_account_always_use_jw @pytest.mark.parametrize( "client_class,transport_name", [ + (RuntimeProjectAttachmentServiceClient, "grpc"), + (RuntimeProjectAttachmentServiceAsyncClient, "grpc_asyncio"), (RuntimeProjectAttachmentServiceClient, "rest"), ], ) @@ -487,17 +502,28 @@ def test_runtime_project_attachment_service_client_from_service_account_file( def test_runtime_project_attachment_service_client_get_transport_class(): transport = RuntimeProjectAttachmentServiceClient.get_transport_class() available_transports = [ + transports.RuntimeProjectAttachmentServiceGrpcTransport, transports.RuntimeProjectAttachmentServiceRestTransport, ] assert transport in available_transports - transport = RuntimeProjectAttachmentServiceClient.get_transport_class("rest") - assert transport == transports.RuntimeProjectAttachmentServiceRestTransport + transport = RuntimeProjectAttachmentServiceClient.get_transport_class("grpc") + assert transport == transports.RuntimeProjectAttachmentServiceGrpcTransport @pytest.mark.parametrize( "client_class,transport_class,transport_name", [ + ( + RuntimeProjectAttachmentServiceClient, + transports.RuntimeProjectAttachmentServiceGrpcTransport, + "grpc", + ), + ( + RuntimeProjectAttachmentServiceAsyncClient, + transports.RuntimeProjectAttachmentServiceGrpcAsyncIOTransport, + "grpc_asyncio", + ), ( RuntimeProjectAttachmentServiceClient, transports.RuntimeProjectAttachmentServiceRestTransport, @@ -510,6 +536,11 @@ def test_runtime_project_attachment_service_client_get_transport_class(): "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(RuntimeProjectAttachmentServiceClient), ) +@mock.patch.object( + RuntimeProjectAttachmentServiceAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(RuntimeProjectAttachmentServiceAsyncClient), +) def test_runtime_project_attachment_service_client_client_options( client_class, transport_class, transport_name ): @@ -647,6 +678,30 @@ def test_runtime_project_attachment_service_client_client_options( @pytest.mark.parametrize( "client_class,transport_class,transport_name,use_client_cert_env", [ + ( + RuntimeProjectAttachmentServiceClient, + transports.RuntimeProjectAttachmentServiceGrpcTransport, + "grpc", + "true", + ), + ( + RuntimeProjectAttachmentServiceAsyncClient, + transports.RuntimeProjectAttachmentServiceGrpcAsyncIOTransport, + "grpc_asyncio", + "true", + ), + ( + RuntimeProjectAttachmentServiceClient, + transports.RuntimeProjectAttachmentServiceGrpcTransport, + "grpc", + "false", + ), + ( + RuntimeProjectAttachmentServiceAsyncClient, + transports.RuntimeProjectAttachmentServiceGrpcAsyncIOTransport, + "grpc_asyncio", + "false", + ), ( RuntimeProjectAttachmentServiceClient, transports.RuntimeProjectAttachmentServiceRestTransport, @@ -666,6 +721,11 @@ def test_runtime_project_attachment_service_client_client_options( "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(RuntimeProjectAttachmentServiceClient), ) +@mock.patch.object( + RuntimeProjectAttachmentServiceAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(RuntimeProjectAttachmentServiceAsyncClient), +) @mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) def test_runtime_project_attachment_service_client_mtls_env_auto( client_class, transport_class, transport_name, use_client_cert_env @@ -769,12 +829,20 @@ def test_runtime_project_attachment_service_client_mtls_env_auto( ) -@pytest.mark.parametrize("client_class", [RuntimeProjectAttachmentServiceClient]) +@pytest.mark.parametrize( + "client_class", + [RuntimeProjectAttachmentServiceClient, RuntimeProjectAttachmentServiceAsyncClient], +) @mock.patch.object( RuntimeProjectAttachmentServiceClient, "DEFAULT_ENDPOINT", modify_default_endpoint(RuntimeProjectAttachmentServiceClient), ) +@mock.patch.object( + RuntimeProjectAttachmentServiceAsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(RuntimeProjectAttachmentServiceAsyncClient), +) def test_runtime_project_attachment_service_client_get_mtls_endpoint_and_cert_source( client_class, ): @@ -868,12 +936,20 @@ def test_runtime_project_attachment_service_client_get_mtls_endpoint_and_cert_so ) -@pytest.mark.parametrize("client_class", [RuntimeProjectAttachmentServiceClient]) +@pytest.mark.parametrize( + "client_class", + [RuntimeProjectAttachmentServiceClient, RuntimeProjectAttachmentServiceAsyncClient], +) @mock.patch.object( RuntimeProjectAttachmentServiceClient, "_DEFAULT_ENDPOINT_TEMPLATE", modify_default_endpoint_template(RuntimeProjectAttachmentServiceClient), ) +@mock.patch.object( + RuntimeProjectAttachmentServiceAsyncClient, + "_DEFAULT_ENDPOINT_TEMPLATE", + modify_default_endpoint_template(RuntimeProjectAttachmentServiceAsyncClient), +) def test_runtime_project_attachment_service_client_client_api_endpoint(client_class): mock_client_cert_source = client_cert_source_callback api_override = "foo.com" @@ -954,6 +1030,16 @@ def test_runtime_project_attachment_service_client_client_api_endpoint(client_cl @pytest.mark.parametrize( "client_class,transport_class,transport_name", [ + ( + RuntimeProjectAttachmentServiceClient, + transports.RuntimeProjectAttachmentServiceGrpcTransport, + "grpc", + ), + ( + RuntimeProjectAttachmentServiceAsyncClient, + transports.RuntimeProjectAttachmentServiceGrpcAsyncIOTransport, + "grpc_asyncio", + ), ( RuntimeProjectAttachmentServiceClient, transports.RuntimeProjectAttachmentServiceRestTransport, @@ -989,6 +1075,18 @@ def test_runtime_project_attachment_service_client_client_options_scopes( @pytest.mark.parametrize( "client_class,transport_class,transport_name,grpc_helpers", [ + ( + RuntimeProjectAttachmentServiceClient, + transports.RuntimeProjectAttachmentServiceGrpcTransport, + "grpc", + grpc_helpers, + ), + ( + RuntimeProjectAttachmentServiceAsyncClient, + transports.RuntimeProjectAttachmentServiceGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), ( RuntimeProjectAttachmentServiceClient, transports.RuntimeProjectAttachmentServiceRestTransport, @@ -1021,13 +1119,181 @@ def test_runtime_project_attachment_service_client_client_options_credentials_fi ) -def test_create_runtime_project_attachment_rest_use_cached_wrapped_rpc(): +def test_runtime_project_attachment_service_client_client_options_from_dict(): + with mock.patch( + "google.cloud.apihub_v1.services.runtime_project_attachment_service.transports.RuntimeProjectAttachmentServiceGrpcTransport.__init__" + ) as grpc_transport: + grpc_transport.return_value = None + client = RuntimeProjectAttachmentServiceClient( + client_options={"api_endpoint": "squid.clam.whelk"} + ) + grpc_transport.assert_called_once_with( + credentials=None, + credentials_file=None, + host="squid.clam.whelk", + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name,grpc_helpers", + [ + ( + RuntimeProjectAttachmentServiceClient, + transports.RuntimeProjectAttachmentServiceGrpcTransport, + "grpc", + grpc_helpers, + ), + ( + RuntimeProjectAttachmentServiceAsyncClient, + transports.RuntimeProjectAttachmentServiceGrpcAsyncIOTransport, + "grpc_asyncio", + grpc_helpers_async, + ), + ], +) +def test_runtime_project_attachment_service_client_create_channel_credentials_file( + client_class, transport_class, transport_name, grpc_helpers +): + # Check the case credentials file is provided. + options = client_options.ClientOptions(credentials_file="credentials.json") + + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options, transport=transport_name) + patched.assert_called_once_with( + credentials=None, + credentials_file="credentials.json", + host=client._DEFAULT_ENDPOINT_TEMPLATE.format( + UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE + ), + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, + api_audience=None, + ) + + # test that the credentials from file are saved and used as the credentials. + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel" + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + file_creds = ga_credentials.AnonymousCredentials() + load_creds.return_value = (file_creds, None) + adc.return_value = (creds, None) + client = client_class(client_options=options, transport=transport_name) + create_channel.assert_called_with( + "apihub.googleapis.com:443", + credentials=file_creds, + credentials_file=None, + quota_project_id=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + scopes=None, + default_host="apihub.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + +@pytest.mark.parametrize( + "request_type", + [ + runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest, + dict, + ], +) +def test_create_runtime_project_attachment(request_type, transport: str = "grpc"): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_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.create_runtime_project_attachment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = runtime_project_attachment_service.RuntimeProjectAttachment( + name="name_value", + runtime_project="runtime_project_value", + ) + response = client.create_runtime_project_attachment(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = ( + runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest() + ) + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance( + response, runtime_project_attachment_service.RuntimeProjectAttachment + ) + assert response.name == "name_value" + assert response.runtime_project == "runtime_project_value" + + +def test_create_runtime_project_attachment_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest( + parent="parent_value", + runtime_project_attachment_id="runtime_project_attachment_id_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_runtime_project_attachment), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.create_runtime_project_attachment(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[ + 0 + ] == runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest( + parent="parent_value", + runtime_project_attachment_id="runtime_project_attachment_id_value", + ) + + +def test_create_runtime_project_attachment_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1048,7 +1314,6 @@ def test_create_runtime_project_attachment_rest_use_cached_wrapped_rpc(): client._transport._wrapped_methods[ client._transport.create_runtime_project_attachment ] = mock_rpc - request = {} client.create_runtime_project_attachment(request) @@ -1062,190 +1327,276 @@ def test_create_runtime_project_attachment_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_create_runtime_project_attachment_rest_required_fields( - request_type=runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest, +@pytest.mark.asyncio +async def test_create_runtime_project_attachment_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.RuntimeProjectAttachmentServiceRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request_init["runtime_project_attachment_id"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped - assert "runtimeProjectAttachmentId" not in jsonified_request + # Ensure method has been cached + assert ( + client._client._transport.create_runtime_project_attachment + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_runtime_project_attachment._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.create_runtime_project_attachment + ] = mock_rpc - # verify required fields with default values are now present - assert "runtimeProjectAttachmentId" in jsonified_request - assert ( - jsonified_request["runtimeProjectAttachmentId"] - == request_init["runtime_project_attachment_id"] + request = {} + await client.create_runtime_project_attachment(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.create_runtime_project_attachment(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_create_runtime_project_attachment_async( + transport: str = "grpc_asyncio", + request_type=runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest, +): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - jsonified_request["parent"] = "parent_value" - jsonified_request[ - "runtimeProjectAttachmentId" - ] = "runtime_project_attachment_id_value" + # 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() - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).create_runtime_project_attachment._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("runtime_project_attachment_id",)) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_runtime_project_attachment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + runtime_project_attachment_service.RuntimeProjectAttachment( + name="name_value", + runtime_project="runtime_project_value", + ) + ) + response = await client.create_runtime_project_attachment(request) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" - assert "runtimeProjectAttachmentId" in jsonified_request - assert ( - jsonified_request["runtimeProjectAttachmentId"] - == "runtime_project_attachment_id_value" + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = ( + runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest() + ) + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance( + response, runtime_project_attachment_service.RuntimeProjectAttachment ) + assert response.name == "name_value" + assert response.runtime_project == "runtime_project_value" + +@pytest.mark.asyncio +async def test_create_runtime_project_attachment_async_from_dict(): + await test_create_runtime_project_attachment_async(request_type=dict) + + +def test_create_runtime_project_attachment_field_headers(): client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = runtime_project_attachment_service.RuntimeProjectAttachment() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "post", - "query_params": pb_request, - } - transcode_result["body"] = pb_request - transcode.return_value = transcode_result - - response_value = Response() - response_value.status_code = 200 + # 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 = runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest() - # Convert return value to protobuf type - return_value = ( - runtime_project_attachment_service.RuntimeProjectAttachment.pb( - return_value - ) - ) - json_return_value = json_format.MessageToJson(return_value) + request.parent = "parent_value" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_runtime_project_attachment), "__call__" + ) as call: + call.return_value = ( + runtime_project_attachment_service.RuntimeProjectAttachment() + ) + client.create_runtime_project_attachment(request) - response = client.create_runtime_project_attachment(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 - expected_params = [ - ( - "runtimeProjectAttachmentId", - "", - ), - ("$alt", "json;enum-encoding=int"), - ] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] -def test_create_runtime_project_attachment_rest_unset_required_fields(): - transport = transports.RuntimeProjectAttachmentServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials +@pytest.mark.asyncio +async def test_create_runtime_project_attachment_field_headers_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), ) - unset_fields = ( - transport.create_runtime_project_attachment._get_unset_required_fields({}) - ) - assert set(unset_fields) == ( - set(("runtimeProjectAttachmentId",)) - & set( - ( - "parent", - "runtimeProjectAttachmentId", - "runtimeProjectAttachment", - ) + # 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 = runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_runtime_project_attachment), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + runtime_project_attachment_service.RuntimeProjectAttachment() ) - ) + await client.create_runtime_project_attachment(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", + "parent=parent_value", + ) in kw["metadata"] -def test_create_runtime_project_attachment_rest_flattened(): + +def test_create_runtime_project_attachment_flattened(): client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = runtime_project_attachment_service.RuntimeProjectAttachment() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_runtime_project_attachment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = ( + runtime_project_attachment_service.RuntimeProjectAttachment() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.create_runtime_project_attachment( + parent="parent_value", + runtime_project_attachment=runtime_project_attachment_service.RuntimeProjectAttachment( + name="name_value" + ), + runtime_project_attachment_id="runtime_project_attachment_id_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].runtime_project_attachment + mock_val = runtime_project_attachment_service.RuntimeProjectAttachment( + name="name_value" + ) + assert arg == mock_val + arg = args[0].runtime_project_attachment_id + mock_val = "runtime_project_attachment_id_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_create_runtime_project_attachment_flattened_error(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_runtime_project_attachment( + runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest(), parent="parent_value", runtime_project_attachment=runtime_project_attachment_service.RuntimeProjectAttachment( name="name_value" ), runtime_project_attachment_id="runtime_project_attachment_id_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = runtime_project_attachment_service.RuntimeProjectAttachment.pb( - return_value + +@pytest.mark.asyncio +async def test_create_runtime_project_attachment_flattened_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.create_runtime_project_attachment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = ( + runtime_project_attachment_service.RuntimeProjectAttachment() ) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_runtime_project_attachment(**mock_args) + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + runtime_project_attachment_service.RuntimeProjectAttachment() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.create_runtime_project_attachment( + parent="parent_value", + runtime_project_attachment=runtime_project_attachment_service.RuntimeProjectAttachment( + name="name_value" + ), + runtime_project_attachment_id="runtime_project_attachment_id_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/runtimeProjectAttachments" - % client.transport._host, - args[1], + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].runtime_project_attachment + mock_val = runtime_project_attachment_service.RuntimeProjectAttachment( + name="name_value" ) + assert arg == mock_val + arg = args[0].runtime_project_attachment_id + mock_val = "runtime_project_attachment_id_value" + assert arg == mock_val -def test_create_runtime_project_attachment_rest_flattened_error( - transport: str = "rest", -): - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_create_runtime_project_attachment_flattened_error_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.create_runtime_project_attachment( + await client.create_runtime_project_attachment( runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest(), parent="parent_value", runtime_project_attachment=runtime_project_attachment_service.RuntimeProjectAttachment( @@ -1255,13 +1606,89 @@ def test_create_runtime_project_attachment_rest_flattened_error( ) -def test_get_runtime_project_attachment_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest, + dict, + ], +) +def test_get_runtime_project_attachment(request_type, transport: str = "grpc"): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_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.get_runtime_project_attachment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = runtime_project_attachment_service.RuntimeProjectAttachment( + name="name_value", + runtime_project="runtime_project_value", + ) + response = client.get_runtime_project_attachment(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = ( + runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest() + ) + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance( + response, runtime_project_attachment_service.RuntimeProjectAttachment + ) + assert response.name == "name_value" + assert response.runtime_project == "runtime_project_value" + + +def test_get_runtime_project_attachment_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_runtime_project_attachment), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_runtime_project_attachment(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[ + 0 + ] == runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest( + name="name_value", + ) + + +def test_get_runtime_project_attachment_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1282,7 +1709,6 @@ def test_get_runtime_project_attachment_rest_use_cached_wrapped_rpc(): client._transport._wrapped_methods[ client._transport.get_runtime_project_attachment ] = mock_rpc - request = {} client.get_runtime_project_attachment(request) @@ -1296,165 +1722,340 @@ def test_get_runtime_project_attachment_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_get_runtime_project_attachment_rest_required_fields( - request_type=runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest, +@pytest.mark.asyncio +async def test_get_runtime_project_attachment_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.RuntimeProjectAttachmentServiceRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.get_runtime_project_attachment + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_runtime_project_attachment._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.get_runtime_project_attachment + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.get_runtime_project_attachment(request) - jsonified_request["name"] = "name_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).get_runtime_project_attachment._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + await client.get_runtime_project_attachment(request) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + +@pytest.mark.asyncio +async def test_get_runtime_project_attachment_async( + transport: str = "grpc_asyncio", + request_type=runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest, +): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = runtime_project_attachment_service.RuntimeProjectAttachment() - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - - response_value = Response() - response_value.status_code = 200 + # 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() - # Convert return value to protobuf type - return_value = ( - runtime_project_attachment_service.RuntimeProjectAttachment.pb( - return_value - ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_runtime_project_attachment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + runtime_project_attachment_service.RuntimeProjectAttachment( + name="name_value", + runtime_project="runtime_project_value", ) - json_return_value = json_format.MessageToJson(return_value) + ) + response = await client.get_runtime_project_attachment(request) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = ( + runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest() + ) + assert args[0] == request - response = client.get_runtime_project_attachment(request) + # Establish that the response is the type that we expect. + assert isinstance( + response, runtime_project_attachment_service.RuntimeProjectAttachment + ) + assert response.name == "name_value" + assert response.runtime_project == "runtime_project_value" - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_get_runtime_project_attachment_async_from_dict(): + await test_get_runtime_project_attachment_async(request_type=dict) -def test_get_runtime_project_attachment_rest_unset_required_fields(): - transport = transports.RuntimeProjectAttachmentServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials + +def test_get_runtime_project_attachment_field_headers(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = transport.get_runtime_project_attachment._get_unset_required_fields( - {} + # 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 = runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_runtime_project_attachment), "__call__" + ) as call: + call.return_value = ( + runtime_project_attachment_service.RuntimeProjectAttachment() + ) + client.get_runtime_project_attachment(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", + "name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_get_runtime_project_attachment_field_headers_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), ) - assert set(unset_fields) == (set(()) & set(("name",))) + # 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 = runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest() -def test_get_runtime_project_attachment_rest_flattened(): + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_runtime_project_attachment), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + runtime_project_attachment_service.RuntimeProjectAttachment() + ) + await client.get_runtime_project_attachment(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", + "name=name_value", + ) in kw["metadata"] + + +def test_get_runtime_project_attachment_flattened(): client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = runtime_project_attachment_service.RuntimeProjectAttachment() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_runtime_project_attachment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = ( + runtime_project_attachment_service.RuntimeProjectAttachment() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_runtime_project_attachment( + name="name_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/runtimeProjectAttachments/sample3" - } + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_get_runtime_project_attachment_flattened_error(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_runtime_project_attachment( + runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest(), name="name_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = runtime_project_attachment_service.RuntimeProjectAttachment.pb( - return_value + +@pytest.mark.asyncio +async def test_get_runtime_project_attachment_flattened_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_runtime_project_attachment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = ( + runtime_project_attachment_service.RuntimeProjectAttachment() ) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_runtime_project_attachment(**mock_args) + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + runtime_project_attachment_service.RuntimeProjectAttachment() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_runtime_project_attachment( + name="name_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/runtimeProjectAttachments/*}" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_get_runtime_project_attachment_rest_flattened_error(transport: str = "rest"): - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_get_runtime_project_attachment_flattened_error_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.get_runtime_project_attachment( + await client.get_runtime_project_attachment( runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest(), name="name_value", ) -def test_list_runtime_project_attachments_rest_use_cached_wrapped_rpc(): +@pytest.mark.parametrize( + "request_type", + [ + runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest, + dict, + ], +) +def test_list_runtime_project_attachments(request_type, transport: str = "grpc"): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_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.list_runtime_project_attachments), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( + next_page_token="next_page_token_value", + ) + ) + response = client.list_runtime_project_attachments(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest() + ) + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListRuntimeProjectAttachmentsPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_runtime_project_attachments_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest( + parent="parent_value", + page_token="page_token_value", + filter="filter_value", + order_by="order_by_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_runtime_project_attachments), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.list_runtime_project_attachments(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[ + 0 + ] == runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest( + parent="parent_value", + page_token="page_token_value", + filter="filter_value", + order_by="order_by_value", + ) + + +def test_list_runtime_project_attachments_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1475,7 +2076,6 @@ def test_list_runtime_project_attachments_rest_use_cached_wrapped_rpc(): client._transport._wrapped_methods[ client._transport.list_runtime_project_attachments ] = mock_rpc - request = {} client.list_runtime_project_attachments(request) @@ -1489,191 +2089,261 @@ def test_list_runtime_project_attachments_rest_use_cached_wrapped_rpc(): assert mock_rpc.call_count == 2 -def test_list_runtime_project_attachments_rest_required_fields( - request_type=runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest, +@pytest.mark.asyncio +async def test_list_runtime_project_attachments_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.RuntimeProjectAttachmentServiceRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["parent"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.list_runtime_project_attachments + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_runtime_project_attachments._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.list_runtime_project_attachments + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.list_runtime_project_attachments(request) - jsonified_request["parent"] = "parent_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).list_runtime_project_attachments._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "filter", - "order_by", - "page_size", - "page_token", - ) - ) - jsonified_request.update(unset_fields) + await client.list_runtime_project_attachments(request) - # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = ( - runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse() +@pytest.mark.asyncio +async def test_list_runtime_project_attachments_async( + transport: str = "grpc_asyncio", + request_type=runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest, +): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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() - # Convert return value to protobuf type - return_value = runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse.pb( - return_value + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_runtime_project_attachments), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( + next_page_token="next_page_token_value", ) - json_return_value = json_format.MessageToJson(return_value) + ) + response = await client.list_runtime_project_attachments(request) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest() + ) + assert args[0] == request - response = client.list_runtime_project_attachments(request) + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListRuntimeProjectAttachmentsAsyncPager) + assert response.next_page_token == "next_page_token_value" - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_list_runtime_project_attachments_async_from_dict(): + await test_list_runtime_project_attachments_async(request_type=dict) -def test_list_runtime_project_attachments_rest_unset_required_fields(): - transport = transports.RuntimeProjectAttachmentServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials - ) - unset_fields = ( - transport.list_runtime_project_attachments._get_unset_required_fields({}) +def test_list_runtime_project_attachments_field_headers(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), ) - assert set(unset_fields) == ( - set( - ( - "filter", - "orderBy", - "pageSize", - "pageToken", - ) + + # 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 = runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_runtime_project_attachments), "__call__" + ) as call: + call.return_value = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse() ) - & set(("parent",)) + client.list_runtime_project_attachments(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", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_list_runtime_project_attachments_field_headers_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), ) + # 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 = runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest() -def test_list_runtime_project_attachments_rest_flattened(): + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_runtime_project_attachments), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse() + ) + await client.list_runtime_project_attachments(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", + "parent=parent_value", + ) in kw["metadata"] + + +def test_list_runtime_project_attachments_flattened(): client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = ( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_runtime_project_attachments), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = ( runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse() ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.list_runtime_project_attachments( + parent="parent_value", + ) - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val - # get truthy value for each flattened field - mock_args = dict( + +def test_list_runtime_project_attachments_flattened_error(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_runtime_project_attachments( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest(), parent="parent_value", ) - mock_args.update(sample_request) - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = ( - runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse.pb( - return_value - ) + +@pytest.mark.asyncio +async def test_list_runtime_project_attachments_flattened_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_runtime_project_attachments), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse() ) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_runtime_project_attachments(**mock_args) + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.list_runtime_project_attachments( + parent="parent_value", + ) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/runtimeProjectAttachments" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val -def test_list_runtime_project_attachments_rest_flattened_error(transport: str = "rest"): - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.asyncio +async def test_list_runtime_project_attachments_flattened_error_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.list_runtime_project_attachments( + await client.list_runtime_project_attachments( runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest(), parent="parent_value", ) -def test_list_runtime_project_attachments_rest_pager(transport: str = "rest"): +def test_list_runtime_project_attachments_pager(transport_name: str = "grpc"): client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + transport=transport_name, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # TODO(kbandes): remove this mock unless there's a good reason for it. - # with mock.patch.object(path_template, 'transcode') as transcode: - # Set the response as a series of pages - response = ( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_runtime_project_attachments), "__call__" + ) as call: + # Set the response to a series of pages. + call.side_effect = ( runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( runtime_project_attachments=[ runtime_project_attachment_service.RuntimeProjectAttachment(), @@ -1698,26 +2368,22 @@ def test_list_runtime_project_attachments_rest_pager(transport: str = "rest"): runtime_project_attachment_service.RuntimeProjectAttachment(), ], ), + RuntimeError, ) - # Two responses for two calls - response = response + response - # Wrap the values into proper Response objs - response = tuple( - runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse.to_json( - x - ) - for x in response + expected_metadata = () + retry = retries.Retry() + timeout = 5 + expected_metadata = tuple(expected_metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", ""),)), + ) + pager = client.list_runtime_project_attachments( + request={}, retry=retry, timeout=timeout ) - return_values = tuple(Response() for i in response) - for return_val, response_val in zip(return_values, response): - return_val._content = response_val.encode("UTF-8") - return_val.status_code = 200 - req.side_effect = return_values - - sample_request = {"parent": "projects/sample1/locations/sample2"} - pager = client.list_runtime_project_attachments(request=sample_request) + assert pager._metadata == expected_metadata + assert pager._retry == retry + assert pager._timeout == timeout results = list(pager) assert len(results) == 6 @@ -1726,204 +2392,232 @@ def test_list_runtime_project_attachments_rest_pager(transport: str = "rest"): for i in results ) - pages = list( - client.list_runtime_project_attachments(request=sample_request).pages - ) - for page_, token in zip(pages, ["abc", "def", "ghi", ""]): - assert page_.raw_page.next_page_token == token +def test_list_runtime_project_attachments_pages(transport_name: str = "grpc"): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport_name, + ) -def test_delete_runtime_project_attachment_rest_use_cached_wrapped_rpc(): - # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, - # instead of constructing them on each call - with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_runtime_project_attachments), "__call__" + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( + runtime_project_attachments=[ + runtime_project_attachment_service.RuntimeProjectAttachment(), + runtime_project_attachment_service.RuntimeProjectAttachment(), + runtime_project_attachment_service.RuntimeProjectAttachment(), + ], + next_page_token="abc", + ), + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( + runtime_project_attachments=[], + next_page_token="def", + ), + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( + runtime_project_attachments=[ + runtime_project_attachment_service.RuntimeProjectAttachment(), + ], + next_page_token="ghi", + ), + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( + runtime_project_attachments=[ + runtime_project_attachment_service.RuntimeProjectAttachment(), + runtime_project_attachment_service.RuntimeProjectAttachment(), + ], + ), + RuntimeError, ) + pages = list(client.list_runtime_project_attachments(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token - # Should wrap all calls on client creation - assert wrapper_fn.call_count > 0 - wrapper_fn.reset_mock() - # Ensure method has been cached - assert ( - client._transport.delete_runtime_project_attachment - in client._transport._wrapped_methods - ) +@pytest.mark.asyncio +async def test_list_runtime_project_attachments_async_pager(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) - # Replace cached wrapped function with mock - mock_rpc = mock.Mock() - mock_rpc.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client._transport._wrapped_methods[ - client._transport.delete_runtime_project_attachment - ] = mock_rpc - - request = {} - client.delete_runtime_project_attachment(request) - - # Establish that the underlying gRPC stub method was called. - assert mock_rpc.call_count == 1 - - client.delete_runtime_project_attachment(request) - - # Establish that a new wrapper was not created for this call - assert wrapper_fn.call_count == 0 - assert mock_rpc.call_count == 2 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_runtime_project_attachments), + "__call__", + new_callable=mock.AsyncMock, + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( + runtime_project_attachments=[ + runtime_project_attachment_service.RuntimeProjectAttachment(), + runtime_project_attachment_service.RuntimeProjectAttachment(), + runtime_project_attachment_service.RuntimeProjectAttachment(), + ], + next_page_token="abc", + ), + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( + runtime_project_attachments=[], + next_page_token="def", + ), + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( + runtime_project_attachments=[ + runtime_project_attachment_service.RuntimeProjectAttachment(), + ], + next_page_token="ghi", + ), + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( + runtime_project_attachments=[ + runtime_project_attachment_service.RuntimeProjectAttachment(), + runtime_project_attachment_service.RuntimeProjectAttachment(), + ], + ), + RuntimeError, + ) + async_pager = await client.list_runtime_project_attachments( + request={}, + ) + assert async_pager.next_page_token == "abc" + responses = [] + async for response in async_pager: # pragma: no branch + responses.append(response) + assert len(responses) == 6 + assert all( + isinstance(i, runtime_project_attachment_service.RuntimeProjectAttachment) + for i in responses + ) -def test_delete_runtime_project_attachment_rest_required_fields( - request_type=runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest, -): - transport_class = transports.RuntimeProjectAttachmentServiceRestTransport - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) +@pytest.mark.asyncio +async def test_list_runtime_project_attachments_async_pages(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), ) - # verify fields with default values are dropped - - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_runtime_project_attachment._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) - - # verify required fields with default values are now present - - jsonified_request["name"] = "name_value" - - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).delete_runtime_project_attachment._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_runtime_project_attachments), + "__call__", + new_callable=mock.AsyncMock, + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( + runtime_project_attachments=[ + runtime_project_attachment_service.RuntimeProjectAttachment(), + runtime_project_attachment_service.RuntimeProjectAttachment(), + runtime_project_attachment_service.RuntimeProjectAttachment(), + ], + next_page_token="abc", + ), + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( + runtime_project_attachments=[], + next_page_token="def", + ), + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( + runtime_project_attachments=[ + runtime_project_attachment_service.RuntimeProjectAttachment(), + ], + next_page_token="ghi", + ), + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( + runtime_project_attachments=[ + runtime_project_attachment_service.RuntimeProjectAttachment(), + runtime_project_attachment_service.RuntimeProjectAttachment(), + ], + ), + RuntimeError, + ) + pages = [] + # Workaround issue in python 3.9 related to code coverage by adding `# pragma: no branch` + # See https://github.com/googleapis/gapic-generator-python/pull/1174#issuecomment-1025132372 + async for page_ in ( # pragma: no branch + await client.list_runtime_project_attachments(request={}) + ).pages: + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" +@pytest.mark.parametrize( + "request_type", + [ + runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest, + dict, + ], +) +def test_delete_runtime_project_attachment(request_type, transport: str = "grpc"): client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - request = request_type(**request_init) - - # Designate an appropriate value for the returned response. - return_value = None - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "delete", - "query_params": pb_request, - } - transcode.return_value = transcode_result - - response_value = Response() - response_value.status_code = 200 - json_return_value = "" - - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - - response = client.delete_runtime_project_attachment(request) - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params + # 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.delete_runtime_project_attachment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.delete_runtime_project_attachment(request) -def test_delete_runtime_project_attachment_rest_unset_required_fields(): - transport = transports.RuntimeProjectAttachmentServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials - ) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = ( + runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest() + ) + assert args[0] == request - unset_fields = ( - transport.delete_runtime_project_attachment._get_unset_required_fields({}) - ) - assert set(unset_fields) == (set(()) & set(("name",))) + # Establish that the response is the type that we expect. + assert response is None -def test_delete_runtime_project_attachment_rest_flattened(): +def test_delete_runtime_project_attachment_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = None - - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/runtimeProjectAttachments/sample3" - } - - # get truthy value for each flattened field - mock_args = dict( - name="name_value", - ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - json_return_value = "" - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - - client.delete_runtime_project_attachment(**mock_args) - - # Establish that the underlying call was made with the expected - # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/runtimeProjectAttachments/*}" - % client.transport._host, - args[1], - ) - - -def test_delete_runtime_project_attachment_rest_flattened_error( - transport: str = "rest", -): - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest( + name="name_value", ) - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.delete_runtime_project_attachment( - runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest(), + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_runtime_project_attachment), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.delete_runtime_project_attachment(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[ + 0 + ] == runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest( name="name_value", ) -def test_lookup_runtime_project_attachment_rest_use_cached_wrapped_rpc(): +def test_delete_runtime_project_attachment_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport="grpc", ) # Should wrap all calls on client creation @@ -1932,7 +2626,7 @@ def test_lookup_runtime_project_attachment_rest_use_cached_wrapped_rpc(): # Ensure method has been cached assert ( - client._transport.lookup_runtime_project_attachment + client._transport.delete_runtime_project_attachment in client._transport._wrapped_methods ) @@ -1942,1884 +2636,4892 @@ def test_lookup_runtime_project_attachment_rest_use_cached_wrapped_rpc(): "foo" # operation_request.operation in compute client(s) expect a string. ) client._transport._wrapped_methods[ - client._transport.lookup_runtime_project_attachment + client._transport.delete_runtime_project_attachment ] = mock_rpc - request = {} - client.lookup_runtime_project_attachment(request) + client.delete_runtime_project_attachment(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.lookup_runtime_project_attachment(request) + client.delete_runtime_project_attachment(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_lookup_runtime_project_attachment_rest_required_fields( - request_type=runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest, +@pytest.mark.asyncio +async def test_delete_runtime_project_attachment_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", ): - transport_class = transports.RuntimeProjectAttachmentServiceRestTransport + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson(pb_request, use_integers_for_enums=False) - ) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # verify fields with default values are dropped + # Ensure method has been cached + assert ( + client._client._transport.delete_runtime_project_attachment + in client._client._transport._wrapped_methods + ) - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).lookup_runtime_project_attachment._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.delete_runtime_project_attachment + ] = mock_rpc - # verify required fields with default values are now present + request = {} + await client.delete_runtime_project_attachment(request) - jsonified_request["name"] = "name_value" + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - unset_fields = transport_class( - credentials=ga_credentials.AnonymousCredentials() - ).lookup_runtime_project_attachment._get_unset_required_fields(jsonified_request) - jsonified_request.update(unset_fields) + await client.delete_runtime_project_attachment(request) - # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type(**request_init) - # Designate an appropriate value for the returned response. - return_value = ( - runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse() +@pytest.mark.asyncio +async def test_delete_runtime_project_attachment_async( + transport: str = "grpc_asyncio", + request_type=runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest, +): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # We need to mock transcode() because providing default values - # for required fields will fail the real version if the http_options - # expect actual values for those fields. - with mock.patch.object(path_template, "transcode") as transcode: - # A uri without fields and an empty body will force all the - # request fields to show up in the query_params. - pb_request = request_type.pb(request) - transcode_result = { - "uri": "v1/sample_method", - "method": "get", - "query_params": pb_request, - } - transcode.return_value = transcode_result - response_value = Response() - response_value.status_code = 200 + # 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() - # Convert return value to protobuf type - return_value = runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(return_value) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_runtime_project_attachment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_runtime_project_attachment(request) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = ( + runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest() + ) + assert args[0] == request - response = client.lookup_runtime_project_attachment(request) + # Establish that the response is the type that we expect. + assert response is None - expected_params = [("$alt", "json;enum-encoding=int")] - actual_params = req.call_args.kwargs["params"] - assert expected_params == actual_params +@pytest.mark.asyncio +async def test_delete_runtime_project_attachment_async_from_dict(): + await test_delete_runtime_project_attachment_async(request_type=dict) -def test_lookup_runtime_project_attachment_rest_unset_required_fields(): - transport = transports.RuntimeProjectAttachmentServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials + +def test_delete_runtime_project_attachment_field_headers(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), ) - unset_fields = ( - transport.lookup_runtime_project_attachment._get_unset_required_fields({}) + # 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 = runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_runtime_project_attachment), "__call__" + ) as call: + call.return_value = None + client.delete_runtime_project_attachment(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", + "name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_delete_runtime_project_attachment_field_headers_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), ) - assert set(unset_fields) == (set(()) & set(("name",))) + # 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 = runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest() -def test_lookup_runtime_project_attachment_rest_flattened(): + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_runtime_project_attachment), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_runtime_project_attachment(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", + "name=name_value", + ) in kw["metadata"] + + +def test_delete_runtime_project_attachment_flattened(): client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = ( - runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse() - ) - - # get arguments that satisfy an http rule for this method - sample_request = {"name": "projects/sample1/locations/sample2"} - - # get truthy value for each flattened field - mock_args = dict( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_runtime_project_attachment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = None + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.delete_runtime_project_attachment( name="name_value", ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - # Convert return value to protobuf type - return_value = runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - - client.lookup_runtime_project_attachment(**mock_args) # Establish that the underlying call was made with the expected # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*}:lookupRuntimeProjectAttachment" - % client.transport._host, - args[1], - ) + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_lookup_runtime_project_attachment_rest_flattened_error( - transport: str = "rest", -): +def test_delete_runtime_project_attachment_flattened_error(): client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport=transport, ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.lookup_runtime_project_attachment( - runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest(), + client.delete_runtime_project_attachment( + runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest(), name="name_value", ) -def test_credentials_transport_error(): - # It is an error to provide credentials and a transport instance. - transport = transports.RuntimeProjectAttachmentServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials(), +@pytest.mark.asyncio +async def test_delete_runtime_project_attachment_flattened_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), ) - with pytest.raises(ValueError): - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - # It is an error to provide a credentials file and a transport instance. - transport = transports.RuntimeProjectAttachmentServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - with pytest.raises(ValueError): - client = RuntimeProjectAttachmentServiceClient( - client_options={"credentials_file": "credentials.json"}, - transport=transport, - ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.delete_runtime_project_attachment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = None - # It is an error to provide an api_key and a transport instance. - transport = transports.RuntimeProjectAttachmentServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = RuntimeProjectAttachmentServiceClient( - client_options=options, - transport=transport, + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.delete_runtime_project_attachment( + name="name_value", ) - # It is an error to provide an api_key and a credential. - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = RuntimeProjectAttachmentServiceClient( - client_options=options, credentials=ga_credentials.AnonymousCredentials() - ) + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - # It is an error to provide scopes and a transport instance. - transport = transports.RuntimeProjectAttachmentServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials(), + +@pytest.mark.asyncio +async def test_delete_runtime_project_attachment_flattened_error_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. with pytest.raises(ValueError): - client = RuntimeProjectAttachmentServiceClient( - client_options={"scopes": ["1", "2"]}, - transport=transport, + await client.delete_runtime_project_attachment( + runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest(), + name="name_value", ) -def test_transport_instance(): - # A client may be instantiated with a custom transport instance. - transport = transports.RuntimeProjectAttachmentServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - client = RuntimeProjectAttachmentServiceClient(transport=transport) - assert client.transport is transport - - @pytest.mark.parametrize( - "transport_class", + "request_type", [ - transports.RuntimeProjectAttachmentServiceRestTransport, + runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest, + dict, ], ) -def test_transport_adc(transport_class): - # Test default credentials are used if not provided. - with mock.patch.object(google.auth, "default") as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport_class() - adc.assert_called_once() +def test_lookup_runtime_project_attachment(request_type, transport: str = "grpc"): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_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() -def test_transport_kind_rest(): - transport = RuntimeProjectAttachmentServiceClient.get_transport_class("rest")( - credentials=ga_credentials.AnonymousCredentials() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.lookup_runtime_project_attachment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = ( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse() + ) + response = client.lookup_runtime_project_attachment(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = ( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest() + ) + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance( + response, + runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse, ) - assert transport.kind == "rest" -def test_create_runtime_project_attachment_rest_bad_request( - request_type=runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest, -): +def test_lookup_runtime_project_attachment_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.create_runtime_project_attachment(request) + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest( + name="name_value", + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.lookup_runtime_project_attachment), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.lookup_runtime_project_attachment(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[ + 0 + ] == runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest( + name="name_value", + ) -@pytest.mark.parametrize( - "request_type", - [ - runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest, - dict, - ], -) -def test_create_runtime_project_attachment_rest_call_success(request_type): - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request_init["runtime_project_attachment"] = { - "name": "name_value", - "runtime_project": "runtime_project_value", - "create_time": {"seconds": 751, "nanos": 543}, - } - # The version of a generated dependency at test runtime may differ from the version used during generation. - # Delete any fields which are not present in the current runtime dependency - # See https://github.com/googleapis/gapic-generator-python/issues/1748 +def test_lookup_runtime_project_attachment_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) - # Determine if the message type is proto-plus or protobuf - test_field = runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest.meta.fields[ - "runtime_project_attachment" - ] + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - def get_message_fields(field): - # Given a field which is a message (composite type), return a list with - # all the fields of the message. - # If the field is not a composite type, return an empty list. - message_fields = [] + # Ensure method has been cached + assert ( + client._transport.lookup_runtime_project_attachment + in client._transport._wrapped_methods + ) - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.lookup_runtime_project_attachment + ] = mock_rpc + request = {} + client.lookup_runtime_project_attachment(request) - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] + client.lookup_runtime_project_attachment(request) - subfields_not_in_runtime = [] + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init[ - "runtime_project_attachment" - ].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) +@pytest.mark.asyncio +async def test_lookup_runtime_project_attachment_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range( - 0, len(request_init["runtime_project_attachment"][field]) - ): - del request_init["runtime_project_attachment"][field][i][subfield] - else: - del request_init["runtime_project_attachment"][field][subfield] - request = request_type(**request_init) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = runtime_project_attachment_service.RuntimeProjectAttachment( - name="name_value", - runtime_project="runtime_project_value", + # Ensure method has been cached + assert ( + client._client._transport.lookup_runtime_project_attachment + in client._client._transport._wrapped_methods ) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 + # Replace cached wrapped function with mock + mock_rpc = mock.AsyncMock() + mock_rpc.return_value = mock.Mock() + client._client._transport._wrapped_methods[ + client._client._transport.lookup_runtime_project_attachment + ] = mock_rpc - # Convert return value to protobuf type - return_value = runtime_project_attachment_service.RuntimeProjectAttachment.pb( - return_value + request = {} + await client.lookup_runtime_project_attachment(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + await client.lookup_runtime_project_attachment(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_lookup_runtime_project_attachment_async( + transport: str = "grpc_asyncio", + request_type=runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest, +): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + 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.lookup_runtime_project_attachment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse() ) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.create_runtime_project_attachment(request) + response = await client.lookup_runtime_project_attachment(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = ( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest() + ) + assert args[0] == request # Establish that the response is the type that we expect. assert isinstance( - response, runtime_project_attachment_service.RuntimeProjectAttachment + response, + runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse, ) - assert response.name == "name_value" - assert response.runtime_project == "runtime_project_value" -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_create_runtime_project_attachment_rest_interceptors(null_interceptor): - transport = transports.RuntimeProjectAttachmentServiceRestTransport( +@pytest.mark.asyncio +async def test_lookup_runtime_project_attachment_async_from_dict(): + await test_lookup_runtime_project_attachment_async(request_type=dict) + + +def test_lookup_runtime_project_attachment_field_headers(): + client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.RuntimeProjectAttachmentServiceRestInterceptor(), ) - client = RuntimeProjectAttachmentServiceClient(transport=transport) + # 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 = runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.RuntimeProjectAttachmentServiceRestInterceptor, - "post_create_runtime_project_attachment", - ) as post, mock.patch.object( - transports.RuntimeProjectAttachmentServiceRestInterceptor, - "post_create_runtime_project_attachment_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.RuntimeProjectAttachmentServiceRestInterceptor, - "pre_create_runtime_project_attachment", - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest.pb( - runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest() + type(client.transport.lookup_runtime_project_attachment), "__call__" + ) as call: + call.return_value = ( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse() ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } + client.lookup_runtime_project_attachment(request) - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = ( - runtime_project_attachment_service.RuntimeProjectAttachment.to_json( - runtime_project_attachment_service.RuntimeProjectAttachment() - ) - ) - req.return_value.content = return_value + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request - request = ( - runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest() - ) - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = ( - runtime_project_attachment_service.RuntimeProjectAttachment() - ) - post_with_metadata.return_value = ( - runtime_project_attachment_service.RuntimeProjectAttachment(), - metadata, - ) + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] - client.create_runtime_project_attachment( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], - ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() +@pytest.mark.asyncio +async def test_lookup_runtime_project_attachment_field_headers_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + # 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 = runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest() -def test_get_runtime_project_attachment_rest_bad_request( - request_type=runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest, -): - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/runtimeProjectAttachments/sample3" - } - request = request_type(**request_init) + request.name = "name_value" - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_runtime_project_attachment(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.lookup_runtime_project_attachment), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse() + ) + await client.lookup_runtime_project_attachment(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request -@pytest.mark.parametrize( - "request_type", - [ - runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest, - dict, - ], -) -def test_get_runtime_project_attachment_rest_call_success(request_type): + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] + + +def test_lookup_runtime_project_attachment_flattened(): client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/runtimeProjectAttachments/sample3" - } - request = request_type(**request_init) - - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = runtime_project_attachment_service.RuntimeProjectAttachment( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.lookup_runtime_project_attachment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = ( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.lookup_runtime_project_attachment( name="name_value", - runtime_project="runtime_project_value", ) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val - # Convert return value to protobuf type - return_value = runtime_project_attachment_service.RuntimeProjectAttachment.pb( - return_value - ) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_runtime_project_attachment(request) - # Establish that the response is the type that we expect. - assert isinstance( - response, runtime_project_attachment_service.RuntimeProjectAttachment +def test_lookup_runtime_project_attachment_flattened_error(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), ) - assert response.name == "name_value" - assert response.runtime_project == "runtime_project_value" + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.lookup_runtime_project_attachment( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest(), + name="name_value", + ) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_runtime_project_attachment_rest_interceptors(null_interceptor): - transport = transports.RuntimeProjectAttachmentServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.RuntimeProjectAttachmentServiceRestInterceptor(), +@pytest.mark.asyncio +async def test_lookup_runtime_project_attachment_flattened_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), ) - client = RuntimeProjectAttachmentServiceClient(transport=transport) + # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.RuntimeProjectAttachmentServiceRestInterceptor, - "post_get_runtime_project_attachment", - ) as post, mock.patch.object( - transports.RuntimeProjectAttachmentServiceRestInterceptor, - "post_get_runtime_project_attachment_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.RuntimeProjectAttachmentServiceRestInterceptor, - "pre_get_runtime_project_attachment", - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = ( - runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest.pb( - runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest() - ) - ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = ( - runtime_project_attachment_service.RuntimeProjectAttachment.to_json( - runtime_project_attachment_service.RuntimeProjectAttachment() - ) + type(client.transport.lookup_runtime_project_attachment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = ( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse() ) - req.return_value.content = return_value - request = ( - runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest() - ) - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = ( - runtime_project_attachment_service.RuntimeProjectAttachment() - ) - post_with_metadata.return_value = ( - runtime_project_attachment_service.RuntimeProjectAttachment(), - metadata, + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse() ) - - client.get_runtime_project_attachment( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.lookup_runtime_project_attachment( + name="name_value", ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val -def test_list_runtime_project_attachments_rest_bad_request( - request_type=runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest, -): - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" +@pytest.mark.asyncio +async def test_lookup_runtime_project_attachment_flattened_error_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_runtime_project_attachments(request) + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.lookup_runtime_project_attachment( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest(), + name="name_value", + ) -@pytest.mark.parametrize( - "request_type", - [ - runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest, - dict, - ], -) -def test_list_runtime_project_attachments_rest_call_success(request_type): - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) +def test_create_runtime_project_attachment_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) - # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request = request_type(**request_init) + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = ( - runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( - next_page_token="next_page_token_value", - ) + # Ensure method has been cached + assert ( + client._transport.create_runtime_project_attachment + in client._transport._wrapped_methods ) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = ( - runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse.pb( - return_value + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.create_runtime_project_attachment + ] = mock_rpc + + request = {} + client.create_runtime_project_attachment(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.create_runtime_project_attachment(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_create_runtime_project_attachment_rest_required_fields( + request_type=runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest, +): + transport_class = transports.RuntimeProjectAttachmentServiceRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["runtime_project_attachment_id"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + assert "runtimeProjectAttachmentId" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_runtime_project_attachment._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "runtimeProjectAttachmentId" in jsonified_request + assert ( + jsonified_request["runtimeProjectAttachmentId"] + == request_init["runtime_project_attachment_id"] + ) + + jsonified_request["parent"] = "parent_value" + jsonified_request[ + "runtimeProjectAttachmentId" + ] = "runtime_project_attachment_id_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_runtime_project_attachment._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("runtime_project_attachment_id",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "runtimeProjectAttachmentId" in jsonified_request + assert ( + jsonified_request["runtimeProjectAttachmentId"] + == "runtime_project_attachment_id_value" + ) + + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = runtime_project_attachment_service.RuntimeProjectAttachment() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = ( + runtime_project_attachment_service.RuntimeProjectAttachment.pb( + return_value + ) + ) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.create_runtime_project_attachment(request) + + expected_params = [ + ( + "runtimeProjectAttachmentId", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_runtime_project_attachment_rest_unset_required_fields(): + transport = transports.RuntimeProjectAttachmentServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = ( + transport.create_runtime_project_attachment._get_unset_required_fields({}) + ) + assert set(unset_fields) == ( + set(("runtimeProjectAttachmentId",)) + & set( + ( + "parent", + "runtimeProjectAttachmentId", + "runtimeProjectAttachment", ) ) + ) + + +def test_create_runtime_project_attachment_rest_flattened(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = runtime_project_attachment_service.RuntimeProjectAttachment() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + runtime_project_attachment=runtime_project_attachment_service.RuntimeProjectAttachment( + name="name_value" + ), + runtime_project_attachment_id="runtime_project_attachment_id_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = runtime_project_attachment_service.RuntimeProjectAttachment.pb( + return_value + ) json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.list_runtime_project_attachments(request) - # Establish that the response is the type that we expect. - assert isinstance(response, pagers.ListRuntimeProjectAttachmentsPager) - assert response.next_page_token == "next_page_token_value" + client.create_runtime_project_attachment(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/runtimeProjectAttachments" + % client.transport._host, + args[1], + ) + + +def test_create_runtime_project_attachment_rest_flattened_error( + transport: str = "rest", +): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_runtime_project_attachment( + runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest(), + parent="parent_value", + runtime_project_attachment=runtime_project_attachment_service.RuntimeProjectAttachment( + name="name_value" + ), + runtime_project_attachment_id="runtime_project_attachment_id_value", + ) + + +def test_get_runtime_project_attachment_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.get_runtime_project_attachment + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.get_runtime_project_attachment + ] = mock_rpc + + request = {} + client.get_runtime_project_attachment(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_runtime_project_attachment(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_get_runtime_project_attachment_rest_required_fields( + request_type=runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest, +): + transport_class = transports.RuntimeProjectAttachmentServiceRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_runtime_project_attachment._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_runtime_project_attachment._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = runtime_project_attachment_service.RuntimeProjectAttachment() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = ( + runtime_project_attachment_service.RuntimeProjectAttachment.pb( + return_value + ) + ) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_runtime_project_attachment(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_runtime_project_attachment_rest_unset_required_fields(): + transport = transports.RuntimeProjectAttachmentServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_runtime_project_attachment._get_unset_required_fields( + {} + ) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_get_runtime_project_attachment_rest_flattened(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = runtime_project_attachment_service.RuntimeProjectAttachment() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/runtimeProjectAttachments/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = runtime_project_attachment_service.RuntimeProjectAttachment.pb( + return_value + ) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.get_runtime_project_attachment(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/runtimeProjectAttachments/*}" + % client.transport._host, + args[1], + ) + + +def test_get_runtime_project_attachment_rest_flattened_error(transport: str = "rest"): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_runtime_project_attachment( + runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest(), + name="name_value", + ) + + +def test_list_runtime_project_attachments_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.list_runtime_project_attachments + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.list_runtime_project_attachments + ] = mock_rpc + + request = {} + client.list_runtime_project_attachments(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.list_runtime_project_attachments(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_list_runtime_project_attachments_rest_required_fields( + request_type=runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest, +): + transport_class = transports.RuntimeProjectAttachmentServiceRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_runtime_project_attachments._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_runtime_project_attachments._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "order_by", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse() + ) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_runtime_project_attachments(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_runtime_project_attachments_rest_unset_required_fields(): + transport = transports.RuntimeProjectAttachmentServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = ( + transport.list_runtime_project_attachments._get_unset_required_fields({}) + ) + assert set(unset_fields) == ( + set( + ( + "filter", + "orderBy", + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +def test_list_runtime_project_attachments_rest_flattened(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse() + ) + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse.pb( + return_value + ) + ) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.list_runtime_project_attachments(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/runtimeProjectAttachments" + % client.transport._host, + args[1], + ) + + +def test_list_runtime_project_attachments_rest_flattened_error(transport: str = "rest"): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_runtime_project_attachments( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest(), + parent="parent_value", + ) + + +def test_list_runtime_project_attachments_rest_pager(transport: str = "rest"): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( + runtime_project_attachments=[ + runtime_project_attachment_service.RuntimeProjectAttachment(), + runtime_project_attachment_service.RuntimeProjectAttachment(), + runtime_project_attachment_service.RuntimeProjectAttachment(), + ], + next_page_token="abc", + ), + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( + runtime_project_attachments=[], + next_page_token="def", + ), + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( + runtime_project_attachments=[ + runtime_project_attachment_service.RuntimeProjectAttachment(), + ], + next_page_token="ghi", + ), + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( + runtime_project_attachments=[ + runtime_project_attachment_service.RuntimeProjectAttachment(), + runtime_project_attachment_service.RuntimeProjectAttachment(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse.to_json( + x + ) + for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2"} + + pager = client.list_runtime_project_attachments(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all( + isinstance(i, runtime_project_attachment_service.RuntimeProjectAttachment) + for i in results + ) + + pages = list( + client.list_runtime_project_attachments(request=sample_request).pages + ) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +def test_delete_runtime_project_attachment_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.delete_runtime_project_attachment + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.delete_runtime_project_attachment + ] = mock_rpc + + request = {} + client.delete_runtime_project_attachment(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.delete_runtime_project_attachment(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_delete_runtime_project_attachment_rest_required_fields( + request_type=runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest, +): + transport_class = transports.RuntimeProjectAttachmentServiceRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_runtime_project_attachment._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_runtime_project_attachment._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = None + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_runtime_project_attachment(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_runtime_project_attachment_rest_unset_required_fields(): + transport = transports.RuntimeProjectAttachmentServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = ( + transport.delete_runtime_project_attachment._get_unset_required_fields({}) + ) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_delete_runtime_project_attachment_rest_flattened(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/runtimeProjectAttachments/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.delete_runtime_project_attachment(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/runtimeProjectAttachments/*}" + % client.transport._host, + args[1], + ) + + +def test_delete_runtime_project_attachment_rest_flattened_error( + transport: str = "rest", +): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_runtime_project_attachment( + runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest(), + name="name_value", + ) + + +def test_lookup_runtime_project_attachment_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.lookup_runtime_project_attachment + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.lookup_runtime_project_attachment + ] = mock_rpc + + request = {} + client.lookup_runtime_project_attachment(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.lookup_runtime_project_attachment(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_lookup_runtime_project_attachment_rest_required_fields( + request_type=runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest, +): + transport_class = transports.RuntimeProjectAttachmentServiceRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).lookup_runtime_project_attachment._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).lookup_runtime_project_attachment._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = ( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse() + ) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.lookup_runtime_project_attachment(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_lookup_runtime_project_attachment_rest_unset_required_fields(): + transport = transports.RuntimeProjectAttachmentServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = ( + transport.lookup_runtime_project_attachment._get_unset_required_fields({}) + ) + assert set(unset_fields) == (set(()) & set(("name",))) + + +def test_lookup_runtime_project_attachment_rest_flattened(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = ( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse() + ) + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + client.lookup_runtime_project_attachment(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*}:lookupRuntimeProjectAttachment" + % client.transport._host, + args[1], + ) + + +def test_lookup_runtime_project_attachment_rest_flattened_error( + transport: str = "rest", +): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.lookup_runtime_project_attachment( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest(), + name="name_value", + ) + + +def test_credentials_transport_error(): + # It is an error to provide credentials and a transport instance. + transport = transports.RuntimeProjectAttachmentServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # It is an error to provide a credentials file and a transport instance. + transport = transports.RuntimeProjectAttachmentServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = RuntimeProjectAttachmentServiceClient( + client_options={"credentials_file": "credentials.json"}, + transport=transport, + ) + + # It is an error to provide an api_key and a transport instance. + transport = transports.RuntimeProjectAttachmentServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = RuntimeProjectAttachmentServiceClient( + client_options=options, + transport=transport, + ) + + # It is an error to provide an api_key and a credential. + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = RuntimeProjectAttachmentServiceClient( + client_options=options, credentials=ga_credentials.AnonymousCredentials() + ) + + # It is an error to provide scopes and a transport instance. + transport = transports.RuntimeProjectAttachmentServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = RuntimeProjectAttachmentServiceClient( + client_options={"scopes": ["1", "2"]}, + transport=transport, + ) + + +def test_transport_instance(): + # A client may be instantiated with a custom transport instance. + transport = transports.RuntimeProjectAttachmentServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + client = RuntimeProjectAttachmentServiceClient(transport=transport) + assert client.transport is transport + + +def test_transport_get_channel(): + # A client may be instantiated with a custom transport instance. + transport = transports.RuntimeProjectAttachmentServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + transport = transports.RuntimeProjectAttachmentServiceGrpcAsyncIOTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.RuntimeProjectAttachmentServiceGrpcTransport, + transports.RuntimeProjectAttachmentServiceGrpcAsyncIOTransport, + transports.RuntimeProjectAttachmentServiceRestTransport, + ], +) +def test_transport_adc(transport_class): + # Test default credentials are used if not provided. + with mock.patch.object(google.auth, "default") as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class() + adc.assert_called_once() + + +def test_transport_kind_grpc(): + transport = RuntimeProjectAttachmentServiceClient.get_transport_class("grpc")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "grpc" + + +def test_initialize_client_w_grpc(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_runtime_project_attachment_empty_call_grpc(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_runtime_project_attachment), "__call__" + ) as call: + call.return_value = ( + runtime_project_attachment_service.RuntimeProjectAttachment() + ) + client.create_runtime_project_attachment(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest() + ) + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_runtime_project_attachment_empty_call_grpc(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_runtime_project_attachment), "__call__" + ) as call: + call.return_value = ( + runtime_project_attachment_service.RuntimeProjectAttachment() + ) + client.get_runtime_project_attachment(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest() + ) + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_runtime_project_attachments_empty_call_grpc(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_runtime_project_attachments), "__call__" + ) as call: + call.return_value = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse() + ) + client.list_runtime_project_attachments(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest() + ) + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_runtime_project_attachment_empty_call_grpc(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.delete_runtime_project_attachment), "__call__" + ) as call: + call.return_value = None + client.delete_runtime_project_attachment(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest() + ) + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_lookup_runtime_project_attachment_empty_call_grpc(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.lookup_runtime_project_attachment), "__call__" + ) as call: + call.return_value = ( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse() + ) + client.lookup_runtime_project_attachment(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest() + ) + + assert args[0] == request_msg + + +def test_transport_kind_grpc_asyncio(): + transport = RuntimeProjectAttachmentServiceAsyncClient.get_transport_class( + "grpc_asyncio" + )(credentials=async_anonymous_credentials()) + assert transport.kind == "grpc_asyncio" + + +def test_initialize_client_w_grpc_asyncio(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_create_runtime_project_attachment_empty_call_grpc_asyncio(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_runtime_project_attachment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + runtime_project_attachment_service.RuntimeProjectAttachment( + name="name_value", + runtime_project="runtime_project_value", + ) + ) + await client.create_runtime_project_attachment(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest() + ) + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_get_runtime_project_attachment_empty_call_grpc_asyncio(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_runtime_project_attachment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + runtime_project_attachment_service.RuntimeProjectAttachment( + name="name_value", + runtime_project="runtime_project_value", + ) + ) + await client.get_runtime_project_attachment(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest() + ) + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_list_runtime_project_attachments_empty_call_grpc_asyncio(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_runtime_project_attachments), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( + next_page_token="next_page_token_value", + ) + ) + await client.list_runtime_project_attachments(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest() + ) + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_delete_runtime_project_attachment_empty_call_grpc_asyncio(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.delete_runtime_project_attachment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_runtime_project_attachment(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest() + ) + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +@pytest.mark.asyncio +async def test_lookup_runtime_project_attachment_empty_call_grpc_asyncio(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.lookup_runtime_project_attachment), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse() + ) + await client.lookup_runtime_project_attachment(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest() + ) + + assert args[0] == request_msg + + +def test_transport_kind_rest(): + transport = RuntimeProjectAttachmentServiceClient.get_transport_class("rest")( + credentials=ga_credentials.AnonymousCredentials() + ) + assert transport.kind == "rest" + + +def test_create_runtime_project_attachment_rest_bad_request( + request_type=runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest, +): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.create_runtime_project_attachment(request) + + +@pytest.mark.parametrize( + "request_type", + [ + runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest, + dict, + ], +) +def test_create_runtime_project_attachment_rest_call_success(request_type): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["runtime_project_attachment"] = { + "name": "name_value", + "runtime_project": "runtime_project_value", + "create_time": {"seconds": 751, "nanos": 543}, + } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest.meta.fields[ + "runtime_project_attachment" + ] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init[ + "runtime_project_attachment" + ].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range( + 0, len(request_init["runtime_project_attachment"][field]) + ): + del request_init["runtime_project_attachment"][field][i][subfield] + else: + del request_init["runtime_project_attachment"][field][subfield] + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = runtime_project_attachment_service.RuntimeProjectAttachment( + name="name_value", + runtime_project="runtime_project_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = runtime_project_attachment_service.RuntimeProjectAttachment.pb( + return_value + ) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.create_runtime_project_attachment(request) + + # Establish that the response is the type that we expect. + assert isinstance( + response, runtime_project_attachment_service.RuntimeProjectAttachment + ) + assert response.name == "name_value" + assert response.runtime_project == "runtime_project_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_runtime_project_attachment_rest_interceptors(null_interceptor): + transport = transports.RuntimeProjectAttachmentServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.RuntimeProjectAttachmentServiceRestInterceptor(), + ) + client = RuntimeProjectAttachmentServiceClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.RuntimeProjectAttachmentServiceRestInterceptor, + "post_create_runtime_project_attachment", + ) as post, mock.patch.object( + transports.RuntimeProjectAttachmentServiceRestInterceptor, + "post_create_runtime_project_attachment_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.RuntimeProjectAttachmentServiceRestInterceptor, + "pre_create_runtime_project_attachment", + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest.pb( + runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = ( + runtime_project_attachment_service.RuntimeProjectAttachment.to_json( + runtime_project_attachment_service.RuntimeProjectAttachment() + ) + ) + req.return_value.content = return_value + + request = ( + runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest() + ) + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = ( + runtime_project_attachment_service.RuntimeProjectAttachment() + ) + post_with_metadata.return_value = ( + runtime_project_attachment_service.RuntimeProjectAttachment(), + metadata, + ) + + client.create_runtime_project_attachment( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_get_runtime_project_attachment_rest_bad_request( + request_type=runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest, +): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/runtimeProjectAttachments/sample3" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_runtime_project_attachment(request) + + +@pytest.mark.parametrize( + "request_type", + [ + runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest, + dict, + ], +) +def test_get_runtime_project_attachment_rest_call_success(request_type): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/runtimeProjectAttachments/sample3" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = runtime_project_attachment_service.RuntimeProjectAttachment( + name="name_value", + runtime_project="runtime_project_value", + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = runtime_project_attachment_service.RuntimeProjectAttachment.pb( + return_value + ) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.get_runtime_project_attachment(request) + + # Establish that the response is the type that we expect. + assert isinstance( + response, runtime_project_attachment_service.RuntimeProjectAttachment + ) + assert response.name == "name_value" + assert response.runtime_project == "runtime_project_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_runtime_project_attachment_rest_interceptors(null_interceptor): + transport = transports.RuntimeProjectAttachmentServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.RuntimeProjectAttachmentServiceRestInterceptor(), + ) + client = RuntimeProjectAttachmentServiceClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.RuntimeProjectAttachmentServiceRestInterceptor, + "post_get_runtime_project_attachment", + ) as post, mock.patch.object( + transports.RuntimeProjectAttachmentServiceRestInterceptor, + "post_get_runtime_project_attachment_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.RuntimeProjectAttachmentServiceRestInterceptor, + "pre_get_runtime_project_attachment", + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = ( + runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest.pb( + runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest() + ) + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = ( + runtime_project_attachment_service.RuntimeProjectAttachment.to_json( + runtime_project_attachment_service.RuntimeProjectAttachment() + ) + ) + req.return_value.content = return_value + + request = ( + runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest() + ) + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = ( + runtime_project_attachment_service.RuntimeProjectAttachment() + ) + post_with_metadata.return_value = ( + runtime_project_attachment_service.RuntimeProjectAttachment(), + metadata, + ) + + client.get_runtime_project_attachment( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_list_runtime_project_attachments_rest_bad_request( + request_type=runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest, +): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_runtime_project_attachments(request) + + +@pytest.mark.parametrize( + "request_type", + [ + runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest, + dict, + ], +) +def test_list_runtime_project_attachments_rest_call_success(request_type): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse( + next_page_token="next_page_token_value", + ) + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse.pb( + return_value + ) + ) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.list_runtime_project_attachments(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListRuntimeProjectAttachmentsPager) + assert response.next_page_token == "next_page_token_value" + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_runtime_project_attachments_rest_interceptors(null_interceptor): + transport = transports.RuntimeProjectAttachmentServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.RuntimeProjectAttachmentServiceRestInterceptor(), + ) + client = RuntimeProjectAttachmentServiceClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.RuntimeProjectAttachmentServiceRestInterceptor, + "post_list_runtime_project_attachments", + ) as post, mock.patch.object( + transports.RuntimeProjectAttachmentServiceRestInterceptor, + "post_list_runtime_project_attachments_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.RuntimeProjectAttachmentServiceRestInterceptor, + "pre_list_runtime_project_attachments", + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest.pb( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse.to_json( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse() + ) + req.return_value.content = return_value + + request = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest() + ) + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse() + ) + post_with_metadata.return_value = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse(), + metadata, + ) + + client.list_runtime_project_attachments( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_delete_runtime_project_attachment_rest_bad_request( + request_type=runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest, +): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/runtimeProjectAttachments/sample3" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.delete_runtime_project_attachment(request) + + +@pytest.mark.parametrize( + "request_type", + [ + runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest, + dict, + ], +) +def test_delete_runtime_project_attachment_rest_call_success(request_type): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/runtimeProjectAttachments/sample3" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "" + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.delete_runtime_project_attachment(request) + + # Establish that the response is the type that we expect. + assert response is None + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_runtime_project_attachment_rest_interceptors(null_interceptor): + transport = transports.RuntimeProjectAttachmentServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.RuntimeProjectAttachmentServiceRestInterceptor(), + ) + client = RuntimeProjectAttachmentServiceClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.RuntimeProjectAttachmentServiceRestInterceptor, + "pre_delete_runtime_project_attachment", + ) as pre: + pre.assert_not_called() + pb_message = runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest.pb( + runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + request = ( + runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest() + ) + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + + client.delete_runtime_project_attachment( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + + +def test_lookup_runtime_project_attachment_rest_bad_request( + request_type=runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest, +): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = mock.Mock() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = mock.Mock() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.lookup_runtime_project_attachment(request) + + +@pytest.mark.parametrize( + "request_type", + [ + runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest, + dict, + ], +) +def test_lookup_runtime_project_attachment_rest_call_success(request_type): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = ( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse() + ) + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + response = client.lookup_runtime_project_attachment(request) + + # Establish that the response is the type that we expect. + assert isinstance( + response, + runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse, + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_lookup_runtime_project_attachment_rest_interceptors(null_interceptor): + transport = transports.RuntimeProjectAttachmentServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.RuntimeProjectAttachmentServiceRestInterceptor(), + ) + client = RuntimeProjectAttachmentServiceClient(transport=transport) + + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.RuntimeProjectAttachmentServiceRestInterceptor, + "post_lookup_runtime_project_attachment", + ) as post, mock.patch.object( + transports.RuntimeProjectAttachmentServiceRestInterceptor, + "post_lookup_runtime_project_attachment_with_metadata", + ) as post_with_metadata, mock.patch.object( + transports.RuntimeProjectAttachmentServiceRestInterceptor, + "pre_lookup_runtime_project_attachment", + ) as pre: + pre.assert_not_called() + post.assert_not_called() + post_with_metadata.assert_not_called() + pb_message = runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest.pb( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = mock.Mock() + req.return_value.status_code = 200 + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + return_value = runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse.to_json( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse() + ) + req.return_value.content = return_value + + request = ( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest() + ) + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = ( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse() + ) + post_with_metadata.return_value = ( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse(), + metadata, + ) + + client.lookup_runtime_project_attachment( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + post_with_metadata.assert_called_once() + + +def test_get_location_rest_bad_request(request_type=locations_pb2.GetLocationRequest): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_location(request) + + +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.GetLocationRequest, + dict, + ], +) +def test_get_location_rest(request_type): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = locations_pb2.Location() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_location(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) + + +def test_list_locations_rest_bad_request( + request_type=locations_pb2.ListLocationsRequest, +): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict({"name": "projects/sample1"}, request) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_locations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.ListLocationsRequest, + dict, + ], +) +def test_list_locations_rest(request_type): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = locations_pb2.ListLocationsResponse() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_locations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + +def test_cancel_operation_rest_bad_request( + request_type=operations_pb2.CancelOperationRequest, +): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.cancel_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.CancelOperationRequest, + dict, + ], +) +def test_cancel_operation_rest(request_type): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "{}" + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.cancel_operation(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_delete_operation_rest_bad_request( + request_type=operations_pb2.DeleteOperationRequest, +): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.delete_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.DeleteOperationRequest, + dict, + ], +) +def test_delete_operation_rest(request_type): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = "{}" + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.delete_operation(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_get_operation_rest_bad_request( + request_type=operations_pb2.GetOperationRequest, +): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.get_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.GetOperationRequest, + dict, + ], +) +def test_get_operation_rest(request_type): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.get_operation(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) + + +def test_list_operations_rest_bad_request( + request_type=operations_pb2.ListOperationsRequest, +): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + json_return_value = "" + response_value.json = mock.Mock(return_value={}) + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + client.list_operations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.ListOperationsRequest, + dict, + ], +) +def test_list_operations_rest(request_type): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.ListOperationsResponse() + + # Wrap the value into a proper Response obj + response_value = mock.Mock() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value.content = json_return_value.encode("UTF-8") + + req.return_value = response_value + req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + + response = client.list_operations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) + + +def test_initialize_client_w_rest(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + assert client is not None + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_create_runtime_project_attachment_empty_call_rest(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.create_runtime_project_attachment), "__call__" + ) as call: + client.create_runtime_project_attachment(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest() + ) + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_get_runtime_project_attachment_empty_call_rest(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.get_runtime_project_attachment), "__call__" + ) as call: + client.get_runtime_project_attachment(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest() + ) + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_list_runtime_project_attachments_empty_call_rest(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.list_runtime_project_attachments), "__call__" + ) as call: + client.list_runtime_project_attachments(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest() + ) + + assert args[0] == request_msg + + +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_delete_runtime_project_attachment_empty_call_rest(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the actual call, and fake the request. + with mock.patch.object( + type(client.transport.delete_runtime_project_attachment), "__call__" + ) as call: + client.delete_runtime_project_attachment(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest() + ) + + assert args[0] == request_msg -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_list_runtime_project_attachments_rest_interceptors(null_interceptor): - transport = transports.RuntimeProjectAttachmentServiceRestTransport( +# This test is a coverage failsafe to make sure that totally empty calls, +# i.e. request == None and no flattened fields passed, work. +def test_lookup_runtime_project_attachment_empty_call_rest(): + client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.RuntimeProjectAttachmentServiceRestInterceptor(), + transport="rest", ) - client = RuntimeProjectAttachmentServiceClient(transport=transport) + # Mock the actual call, and fake the request. with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.RuntimeProjectAttachmentServiceRestInterceptor, - "post_list_runtime_project_attachments", - ) as post, mock.patch.object( - transports.RuntimeProjectAttachmentServiceRestInterceptor, - "post_list_runtime_project_attachments_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.RuntimeProjectAttachmentServiceRestInterceptor, - "pre_list_runtime_project_attachments", - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest.pb( - runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest() + type(client.transport.lookup_runtime_project_attachment), "__call__" + ) as call: + client.lookup_runtime_project_attachment(request=None) + + # Establish that the underlying stub method was called. + call.assert_called() + _, args, _ = call.mock_calls[0] + request_msg = ( + runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest() ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse.to_json( - runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse() + assert args[0] == request_msg + + +def test_transport_grpc_default(): + # A client should use the gRPC transport by default. + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert isinstance( + client.transport, + transports.RuntimeProjectAttachmentServiceGrpcTransport, + ) + + +def test_runtime_project_attachment_service_base_transport_error(): + # Passing both a credentials object and credentials_file should raise an error + with pytest.raises(core_exceptions.DuplicateCredentialArgs): + transport = transports.RuntimeProjectAttachmentServiceTransport( + credentials=ga_credentials.AnonymousCredentials(), + credentials_file="credentials.json", ) - req.return_value.content = return_value - request = ( - runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest() + +def test_runtime_project_attachment_service_base_transport(): + # Instantiate the base transport. + with mock.patch( + "google.cloud.apihub_v1.services.runtime_project_attachment_service.transports.RuntimeProjectAttachmentServiceTransport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.RuntimeProjectAttachmentServiceTransport( + credentials=ga_credentials.AnonymousCredentials(), ) - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = ( - runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse() + + # Every method on the transport should just blindly + # raise NotImplementedError. + methods = ( + "create_runtime_project_attachment", + "get_runtime_project_attachment", + "list_runtime_project_attachments", + "delete_runtime_project_attachment", + "lookup_runtime_project_attachment", + "get_location", + "list_locations", + "get_operation", + "cancel_operation", + "delete_operation", + "list_operations", + ) + for method in methods: + with pytest.raises(NotImplementedError): + getattr(transport, method)(request=object()) + + with pytest.raises(NotImplementedError): + transport.close() + + # Catch all for all remaining methods and properties + remainder = [ + "kind", + ] + for r in remainder: + with pytest.raises(NotImplementedError): + getattr(transport, r)() + + +def test_runtime_project_attachment_service_base_transport_with_credentials_file(): + # Instantiate the base transport with a credentials file + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch( + "google.cloud.apihub_v1.services.runtime_project_attachment_service.transports.RuntimeProjectAttachmentServiceTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) + transport = transports.RuntimeProjectAttachmentServiceTransport( + credentials_file="credentials.json", + quota_project_id="octopus", ) - post_with_metadata.return_value = ( - runtime_project_attachment_service.ListRuntimeProjectAttachmentsResponse(), - metadata, + load_creds.assert_called_once_with( + "credentials.json", + scopes=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", ) - client.list_runtime_project_attachments( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], + +def test_runtime_project_attachment_service_base_transport_with_adc(): + # Test the default credentials are used if credentials and credentials_file are None. + with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch( + "google.cloud.apihub_v1.services.runtime_project_attachment_service.transports.RuntimeProjectAttachmentServiceTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport = transports.RuntimeProjectAttachmentServiceTransport() + adc.assert_called_once() + + +def test_runtime_project_attachment_service_auth_adc(): + # If no credentials are provided, we should use ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + RuntimeProjectAttachmentServiceClient() + adc.assert_called_once_with( + scopes=None, + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id=None, ) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + +@pytest.mark.parametrize( + "transport_class", + [ + transports.RuntimeProjectAttachmentServiceGrpcTransport, + transports.RuntimeProjectAttachmentServiceGrpcAsyncIOTransport, + ], +) +def test_runtime_project_attachment_service_transport_auth_adc(transport_class): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object(google.auth, "default", autospec=True) as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + adc.assert_called_once_with( + scopes=["1", "2"], + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", + ) -def test_delete_runtime_project_attachment_rest_bad_request( - request_type=runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest, +@pytest.mark.parametrize( + "transport_class", + [ + transports.RuntimeProjectAttachmentServiceGrpcTransport, + transports.RuntimeProjectAttachmentServiceGrpcAsyncIOTransport, + transports.RuntimeProjectAttachmentServiceRestTransport, + ], +) +def test_runtime_project_attachment_service_transport_auth_gdch_credentials( + transport_class, ): - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/runtimeProjectAttachments/sample3" - } - request = request_type(**request_init) + host = "https://language.com" + api_audience_tests = [None, "https://language2.com"] + api_audience_expect = [host, "https://language2.com"] + for t, e in zip(api_audience_tests, api_audience_expect): + with mock.patch.object(google.auth, "default", autospec=True) as adc: + gdch_mock = mock.MagicMock() + type(gdch_mock).with_gdch_audience = mock.PropertyMock( + return_value=gdch_mock + ) + adc.return_value = (gdch_mock, None) + transport_class(host=host, api_audience=t) + gdch_mock.with_gdch_audience.assert_called_once_with(e) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_runtime_project_attachment(request) + +@pytest.mark.parametrize( + "transport_class,grpc_helpers", + [ + (transports.RuntimeProjectAttachmentServiceGrpcTransport, grpc_helpers), + ( + transports.RuntimeProjectAttachmentServiceGrpcAsyncIOTransport, + grpc_helpers_async, + ), + ], +) +def test_runtime_project_attachment_service_transport_create_channel( + transport_class, grpc_helpers +): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object( + google.auth, "default", autospec=True + ) as adc, mock.patch.object( + grpc_helpers, "create_channel", autospec=True + ) as create_channel: + creds = ga_credentials.AnonymousCredentials() + adc.return_value = (creds, None) + transport_class(quota_project_id="octopus", scopes=["1", "2"]) + + create_channel.assert_called_with( + "apihub.googleapis.com:443", + credentials=creds, + credentials_file=None, + quota_project_id="octopus", + default_scopes=("https://www.googleapis.com/auth/cloud-platform",), + scopes=["1", "2"], + default_host="apihub.googleapis.com", + ssl_credentials=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.RuntimeProjectAttachmentServiceGrpcTransport, + transports.RuntimeProjectAttachmentServiceGrpcAsyncIOTransport, + ], +) +def test_runtime_project_attachment_service_grpc_transport_client_cert_source_for_mtls( + transport_class, +): + cred = ga_credentials.AnonymousCredentials() + + # Check ssl_channel_credentials is used if provided. + with mock.patch.object(transport_class, "create_channel") as mock_create_channel: + mock_ssl_channel_creds = mock.Mock() + transport_class( + host="squid.clam.whelk", + credentials=cred, + ssl_channel_credentials=mock_ssl_channel_creds, + ) + mock_create_channel.assert_called_once_with( + "squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_channel_creds, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + # Check if ssl_channel_credentials is not provided, then client_cert_source_for_mtls + # is used. + with mock.patch.object(transport_class, "create_channel", return_value=mock.Mock()): + with mock.patch("grpc.ssl_channel_credentials") as mock_ssl_cred: + transport_class( + credentials=cred, + client_cert_source_for_mtls=client_cert_source_callback, + ) + expected_cert, expected_key = client_cert_source_callback() + mock_ssl_cred.assert_called_once_with( + certificate_chain=expected_cert, private_key=expected_key + ) + + +def test_runtime_project_attachment_service_http_transport_client_cert_source_for_mtls(): + cred = ga_credentials.AnonymousCredentials() + with mock.patch( + "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" + ) as mock_configure_mtls_channel: + transports.RuntimeProjectAttachmentServiceRestTransport( + credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + ) + mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) @pytest.mark.parametrize( - "request_type", + "transport_name", [ - runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest, - dict, + "grpc", + "grpc_asyncio", + "rest", ], ) -def test_delete_runtime_project_attachment_rest_call_success(request_type): +def test_runtime_project_attachment_service_host_no_port(transport_name): client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="apihub.googleapis.com" + ), + transport=transport_name, + ) + assert client.transport._host == ( + "apihub.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://apihub.googleapis.com" ) - # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/runtimeProjectAttachments/sample3" - } - request = request_type(**request_init) - - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = None - - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "" - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.delete_runtime_project_attachment(request) - # Establish that the response is the type that we expect. - assert response is None +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "grpc_asyncio", + "rest", + ], +) +def test_runtime_project_attachment_service_host_with_port(transport_name): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="apihub.googleapis.com:8000" + ), + transport=transport_name, + ) + assert client.transport._host == ( + "apihub.googleapis.com:8000" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://apihub.googleapis.com:8000" + ) -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_delete_runtime_project_attachment_rest_interceptors(null_interceptor): - transport = transports.RuntimeProjectAttachmentServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.RuntimeProjectAttachmentServiceRestInterceptor(), +@pytest.mark.parametrize( + "transport_name", + [ + "rest", + ], +) +def test_runtime_project_attachment_service_client_transport_session_collision( + transport_name, +): + creds1 = ga_credentials.AnonymousCredentials() + creds2 = ga_credentials.AnonymousCredentials() + client1 = RuntimeProjectAttachmentServiceClient( + credentials=creds1, + transport=transport_name, ) - client = RuntimeProjectAttachmentServiceClient(transport=transport) + client2 = RuntimeProjectAttachmentServiceClient( + credentials=creds2, + transport=transport_name, + ) + session1 = client1.transport.create_runtime_project_attachment._session + session2 = client2.transport.create_runtime_project_attachment._session + assert session1 != session2 + session1 = client1.transport.get_runtime_project_attachment._session + session2 = client2.transport.get_runtime_project_attachment._session + assert session1 != session2 + session1 = client1.transport.list_runtime_project_attachments._session + session2 = client2.transport.list_runtime_project_attachments._session + assert session1 != session2 + session1 = client1.transport.delete_runtime_project_attachment._session + session2 = client2.transport.delete_runtime_project_attachment._session + assert session1 != session2 + session1 = client1.transport.lookup_runtime_project_attachment._session + session2 = client2.transport.lookup_runtime_project_attachment._session + assert session1 != session2 - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.RuntimeProjectAttachmentServiceRestInterceptor, - "pre_delete_runtime_project_attachment", - ) as pre: - pre.assert_not_called() - pb_message = runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest.pb( - runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest() - ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +def test_runtime_project_attachment_service_grpc_transport_channel(): + channel = grpc.secure_channel("http://localhost/", grpc.local_channel_credentials()) - request = ( - runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest() - ) - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata + # Check that channel is used if provided. + transport = transports.RuntimeProjectAttachmentServiceGrpcTransport( + host="squid.clam.whelk", + channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None - client.delete_runtime_project_attachment( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], - ) - pre.assert_called_once() +def test_runtime_project_attachment_service_grpc_asyncio_transport_channel(): + channel = aio.secure_channel("http://localhost/", grpc.local_channel_credentials()) + # Check that channel is used if provided. + transport = transports.RuntimeProjectAttachmentServiceGrpcAsyncIOTransport( + host="squid.clam.whelk", + channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + assert transport._ssl_channel_credentials == None -def test_lookup_runtime_project_attachment_rest_bad_request( - request_type=runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest, + +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. +@pytest.mark.parametrize( + "transport_class", + [ + transports.RuntimeProjectAttachmentServiceGrpcTransport, + transports.RuntimeProjectAttachmentServiceGrpcAsyncIOTransport, + ], +) +def test_runtime_project_attachment_service_transport_channel_mtls_with_client_cert_source( + transport_class, ): - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2"} - request = request_type(**request_init) + with mock.patch( + "grpc.ssl_channel_credentials", autospec=True + ) as grpc_ssl_channel_cred: + with mock.patch.object( + transport_class, "create_channel" + ) 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 = ga_credentials.AnonymousCredentials() + with pytest.warns(DeprecationWarning): + with mock.patch.object(google.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() - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = mock.Mock() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = mock.Mock() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.lookup_runtime_project_attachment(request) + 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=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + assert transport._ssl_channel_credentials == mock_ssl_cred +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. @pytest.mark.parametrize( - "request_type", + "transport_class", [ - runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest, - dict, + transports.RuntimeProjectAttachmentServiceGrpcTransport, + transports.RuntimeProjectAttachmentServiceGrpcAsyncIOTransport, ], ) -def test_lookup_runtime_project_attachment_rest_call_success(request_type): - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" - ) +def test_runtime_project_attachment_service_transport_channel_mtls_with_adc( + transport_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), + ): + with mock.patch.object( + transport_class, "create_channel" + ) 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, + ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2"} - request = request_type(**request_init) + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=mock_cred, + credentials_file=None, + scopes=None, + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = ( - runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse() - ) - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 +def test_runtime_project_attachment_path(): + project = "squid" + location = "clam" + runtime_project_attachment = "whelk" + expected = "projects/{project}/locations/{location}/runtimeProjectAttachments/{runtime_project_attachment}".format( + project=project, + location=location, + runtime_project_attachment=runtime_project_attachment, + ) + actual = RuntimeProjectAttachmentServiceClient.runtime_project_attachment_path( + project, location, runtime_project_attachment + ) + assert expected == actual - # Convert return value to protobuf type - return_value = runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse.pb( - return_value + +def test_parse_runtime_project_attachment_path(): + expected = { + "project": "octopus", + "location": "oyster", + "runtime_project_attachment": "nudibranch", + } + path = RuntimeProjectAttachmentServiceClient.runtime_project_attachment_path( + **expected + ) + + # Check that the path construction is reversible. + actual = ( + RuntimeProjectAttachmentServiceClient.parse_runtime_project_attachment_path( + path ) - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.lookup_runtime_project_attachment(request) + ) + assert expected == actual - # Establish that the response is the type that we expect. - assert isinstance( - response, - runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse, + +def test_common_billing_account_path(): + billing_account = "cuttlefish" + expected = "billingAccounts/{billing_account}".format( + billing_account=billing_account, + ) + actual = RuntimeProjectAttachmentServiceClient.common_billing_account_path( + billing_account ) + assert expected == actual -@pytest.mark.parametrize("null_interceptor", [True, False]) -def test_lookup_runtime_project_attachment_rest_interceptors(null_interceptor): - transport = transports.RuntimeProjectAttachmentServiceRestTransport( - credentials=ga_credentials.AnonymousCredentials(), - interceptor=None - if null_interceptor - else transports.RuntimeProjectAttachmentServiceRestInterceptor(), +def test_parse_common_billing_account_path(): + expected = { + "billing_account": "mussel", + } + path = RuntimeProjectAttachmentServiceClient.common_billing_account_path(**expected) + + # Check that the path construction is reversible. + actual = RuntimeProjectAttachmentServiceClient.parse_common_billing_account_path( + path ) - client = RuntimeProjectAttachmentServiceClient(transport=transport) + assert expected == actual - with mock.patch.object( - type(client.transport._session), "request" - ) as req, mock.patch.object( - path_template, "transcode" - ) as transcode, mock.patch.object( - transports.RuntimeProjectAttachmentServiceRestInterceptor, - "post_lookup_runtime_project_attachment", - ) as post, mock.patch.object( - transports.RuntimeProjectAttachmentServiceRestInterceptor, - "post_lookup_runtime_project_attachment_with_metadata", - ) as post_with_metadata, mock.patch.object( - transports.RuntimeProjectAttachmentServiceRestInterceptor, - "pre_lookup_runtime_project_attachment", - ) as pre: - pre.assert_not_called() - post.assert_not_called() - post_with_metadata.assert_not_called() - pb_message = runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest.pb( - runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest() - ) - transcode.return_value = { - "method": "post", - "uri": "my_uri", - "body": pb_message, - "query_params": pb_message, - } - req.return_value = mock.Mock() - req.return_value.status_code = 200 - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - return_value = runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse.to_json( - runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse() - ) - req.return_value.content = return_value +def test_common_folder_path(): + folder = "winkle" + expected = "folders/{folder}".format( + folder=folder, + ) + actual = RuntimeProjectAttachmentServiceClient.common_folder_path(folder) + assert expected == actual - request = ( - runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest() - ) - metadata = [ - ("key", "val"), - ("cephalopod", "squid"), - ] - pre.return_value = request, metadata - post.return_value = ( - runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse() - ) - post_with_metadata.return_value = ( - runtime_project_attachment_service.LookupRuntimeProjectAttachmentResponse(), - metadata, - ) - client.lookup_runtime_project_attachment( - request, - metadata=[ - ("key", "val"), - ("cephalopod", "squid"), - ], - ) +def test_parse_common_folder_path(): + expected = { + "folder": "nautilus", + } + path = RuntimeProjectAttachmentServiceClient.common_folder_path(**expected) - pre.assert_called_once() - post.assert_called_once() - post_with_metadata.assert_called_once() + # Check that the path construction is reversible. + actual = RuntimeProjectAttachmentServiceClient.parse_common_folder_path(path) + assert expected == actual -def test_get_location_rest_bad_request(request_type=locations_pb2.GetLocationRequest): - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +def test_common_organization_path(): + organization = "scallop" + expected = "organizations/{organization}".format( + organization=organization, ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2"}, request + actual = RuntimeProjectAttachmentServiceClient.common_organization_path( + organization ) + assert expected == actual - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_location(request) +def test_parse_common_organization_path(): + expected = { + "organization": "abalone", + } + path = RuntimeProjectAttachmentServiceClient.common_organization_path(**expected) -@pytest.mark.parametrize( - "request_type", - [ - locations_pb2.GetLocationRequest, - dict, - ], -) -def test_get_location_rest(request_type): - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + # Check that the path construction is reversible. + actual = RuntimeProjectAttachmentServiceClient.parse_common_organization_path(path) + assert expected == actual - request_init = {"name": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = locations_pb2.Location() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") +def test_common_project_path(): + project = "squid" + expected = "projects/{project}".format( + project=project, + ) + actual = RuntimeProjectAttachmentServiceClient.common_project_path(project) + assert expected == actual - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - response = client.get_location(request) +def test_parse_common_project_path(): + expected = { + "project": "clam", + } + path = RuntimeProjectAttachmentServiceClient.common_project_path(**expected) - # Establish that the response is the type that we expect. - assert isinstance(response, locations_pb2.Location) + # Check that the path construction is reversible. + actual = RuntimeProjectAttachmentServiceClient.parse_common_project_path(path) + assert expected == actual -def test_list_locations_rest_bad_request( - request_type=locations_pb2.ListLocationsRequest, -): - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +def test_common_location_path(): + project = "whelk" + location = "octopus" + expected = "projects/{project}/locations/{location}".format( + project=project, + location=location, ) - request = request_type() - request = json_format.ParseDict({"name": "projects/sample1"}, request) - - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_locations(request) + actual = RuntimeProjectAttachmentServiceClient.common_location_path( + project, location + ) + assert expected == actual -@pytest.mark.parametrize( - "request_type", - [ - locations_pb2.ListLocationsRequest, - dict, - ], -) -def test_list_locations_rest(request_type): - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) +def test_parse_common_location_path(): + expected = { + "project": "oyster", + "location": "nudibranch", + } + path = RuntimeProjectAttachmentServiceClient.common_location_path(**expected) - request_init = {"name": "projects/sample1"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = locations_pb2.ListLocationsResponse() + # Check that the path construction is reversible. + actual = RuntimeProjectAttachmentServiceClient.parse_common_location_path(path) + assert expected == actual - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +def test_client_with_default_client_info(): + client_info = gapic_v1.client_info.ClientInfo() - response = client.list_locations(request) + with mock.patch.object( + transports.RuntimeProjectAttachmentServiceTransport, "_prep_wrapped_messages" + ) as prep: + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + client_info=client_info, + ) + prep.assert_called_once_with(client_info) - # Establish that the response is the type that we expect. - assert isinstance(response, locations_pb2.ListLocationsResponse) + with mock.patch.object( + transports.RuntimeProjectAttachmentServiceTransport, "_prep_wrapped_messages" + ) as prep: + transport_class = RuntimeProjectAttachmentServiceClient.get_transport_class() + transport = transport_class( + credentials=ga_credentials.AnonymousCredentials(), + client_info=client_info, + ) + prep.assert_called_once_with(client_info) -def test_cancel_operation_rest_bad_request( - request_type=operations_pb2.CancelOperationRequest, -): +def test_delete_operation(transport: str = "grpc"): client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + transport=transport, ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.cancel_operation(request) + # 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 = operations_pb2.DeleteOperationRequest() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.delete_operation(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 -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.CancelOperationRequest, - dict, - ], -) -def test_cancel_operation_rest(request_type): - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + # Establish that the response is the type that we expect. + assert response is None - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = None - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "{}" - response_value.content = json_return_value.encode("UTF-8") +@pytest.mark.asyncio +async def test_delete_operation_async(transport: str = "grpc_asyncio"): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, + ) - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # 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 = operations_pb2.DeleteOperationRequest() - response = client.cancel_operation(request) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_operation(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 response is the type that we expect. assert response is None -def test_delete_operation_rest_bad_request( - request_type=operations_pb2.DeleteOperationRequest, -): +def test_delete_operation_field_headers(): client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.delete_operation(request) + # 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 = operations_pb2.DeleteOperationRequest() + request.name = "locations" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + call.return_value = None -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.DeleteOperationRequest, - dict, - ], -) -def test_delete_operation_rest(request_type): - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + client.delete_operation(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 - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = None + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = "{}" - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +@pytest.mark.asyncio +async def test_delete_operation_field_headers_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) - response = client.delete_operation(request) + # 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 = operations_pb2.DeleteOperationRequest() + request.name = "locations" - # Establish that the response is the type that we expect. - assert response is None + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.delete_operation(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", + "name=locations", + ) in kw["metadata"] -def test_get_operation_rest_bad_request( - request_type=operations_pb2.GetOperationRequest, -): + +def test_delete_operation_from_dict(): client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2/operations/sample3"}, request ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.get_operation(request) + response = client.delete_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.GetOperationRequest, - dict, - ], -) -def test_get_operation_rest(request_type): +@pytest.mark.asyncio +async def test_delete_operation_from_dict_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.delete_operation( + request={ + "name": "locations", + } + ) + call.assert_called() + + +def test_cancel_operation(transport: str = "grpc"): client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation() + # 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 = operations_pb2.CancelOperationRequest() - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + response = client.cancel_operation(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 - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} + # Establish that the response is the type that we expect. + assert response is None - response = client.get_operation(request) + +@pytest.mark.asyncio +async def test_cancel_operation_async(transport: str = "grpc_asyncio"): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.CancelOperationRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.cancel_operation(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 response is the type that we expect. - assert isinstance(response, operations_pb2.Operation) + assert response is None -def test_list_operations_rest_bad_request( - request_type=operations_pb2.ListOperationsRequest, -): +def test_cancel_operation_field_headers(): client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - request = request_type() - request = json_format.ParseDict( - {"name": "projects/sample1/locations/sample2"}, request ) - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - json_return_value = "" - response_value.json = mock.Mock(return_value={}) - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} - client.list_operations(request) + # 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 = operations_pb2.CancelOperationRequest() + request.name = "locations" + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + call.return_value = None -@pytest.mark.parametrize( - "request_type", - [ - operations_pb2.ListOperationsRequest, - dict, - ], -) -def test_list_operations_rest(request_type): - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) + client.cancel_operation(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 - request_init = {"name": "projects/sample1/locations/sample2"} - request = request_type(**request_init) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.ListOperationsResponse() + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] - # Wrap the value into a proper Response obj - response_value = mock.Mock() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value.content = json_return_value.encode("UTF-8") - req.return_value = response_value - req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"} +@pytest.mark.asyncio +async def test_cancel_operation_field_headers_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) - response = client.list_operations(request) + # 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 = operations_pb2.CancelOperationRequest() + request.name = "locations" - # Establish that the response is the type that we expect. - assert isinstance(response, operations_pb2.ListOperationsResponse) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.cancel_operation(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", + "name=locations", + ) in kw["metadata"] -def test_initialize_client_w_rest(): + +def test_cancel_operation_from_dict(): client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), ) - assert client is not None + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = None + + response = client.cancel_operation( + request={ + "name": "locations", + } + ) + call.assert_called() + + +@pytest.mark.asyncio +async def test_cancel_operation_from_dict_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + response = await client.cancel_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_create_runtime_project_attachment_empty_call_rest(): +def test_get_operation(transport: str = "grpc"): client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.create_runtime_project_attachment), "__call__" - ) as call: - client.create_runtime_project_attachment(request=None) + # 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 = operations_pb2.GetOperationRequest() - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation() + response = client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = ( - runtime_project_attachment_service.CreateRuntimeProjectAttachmentRequest() - ) + assert args[0] == request - assert args[0] == request_msg + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_get_runtime_project_attachment_empty_call_rest(): - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", +@pytest.mark.asyncio +async def test_get_operation_async(transport: str = "grpc_asyncio"): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.get_runtime_project_attachment), "__call__" - ) as call: - client.get_runtime_project_attachment(request=None) + # 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 = operations_pb2.GetOperationRequest() - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = ( - runtime_project_attachment_service.GetRuntimeProjectAttachmentRequest() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() ) + response = await client.get_operation(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 - assert args[0] == request_msg + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_list_runtime_project_attachments_empty_call_rest(): +def test_get_operation_field_headers(): client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.list_runtime_project_attachments), "__call__" - ) as call: - client.list_runtime_project_attachments(request=None) + # 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 = operations_pb2.GetOperationRequest() + request.name = "locations" - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + call.return_value = operations_pb2.Operation() + + client.get_operation(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = ( - runtime_project_attachment_service.ListRuntimeProjectAttachmentsRequest() + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_get_operation_field_headers_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = operations_pb2.GetOperationRequest() + request.name = "locations" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() ) + await client.get_operation(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 - assert args[0] == request_msg + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_delete_runtime_project_attachment_empty_call_rest(): +def test_get_operation_from_dict(): client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation() - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.delete_runtime_project_attachment), "__call__" - ) as call: - client.delete_runtime_project_attachment(request=None) - - # Establish that the underlying stub method was called. - call.assert_called() - _, args, _ = call.mock_calls[0] - request_msg = ( - runtime_project_attachment_service.DeleteRuntimeProjectAttachmentRequest() + response = client.get_operation( + request={ + "name": "locations", + } ) + call.assert_called() - assert args[0] == request_msg +@pytest.mark.asyncio +async def test_get_operation_from_dict_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_operation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation() + ) + response = await client.get_operation( + request={ + "name": "locations", + } + ) + call.assert_called() -# This test is a coverage failsafe to make sure that totally empty calls, -# i.e. request == None and no flattened fields passed, work. -def test_lookup_runtime_project_attachment_empty_call_rest(): + +def test_list_operations(transport: str = "grpc"): client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # Mock the actual call, and fake the request. - with mock.patch.object( - type(client.transport.lookup_runtime_project_attachment), "__call__" - ) as call: - client.lookup_runtime_project_attachment(request=None) + # 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 = operations_pb2.ListOperationsRequest() - # Establish that the underlying stub method was called. - call.assert_called() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.ListOperationsResponse() + response = client.list_operations(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - request_msg = ( - runtime_project_attachment_service.LookupRuntimeProjectAttachmentRequest() - ) + assert args[0] == request - assert args[0] == request_msg + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) -def test_runtime_project_attachment_service_base_transport_error(): - # Passing both a credentials object and credentials_file should raise an error - with pytest.raises(core_exceptions.DuplicateCredentialArgs): - transport = transports.RuntimeProjectAttachmentServiceTransport( - credentials=ga_credentials.AnonymousCredentials(), - credentials_file="credentials.json", - ) +@pytest.mark.asyncio +async def test_list_operations_async(transport: str = "grpc_asyncio"): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + 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 = operations_pb2.ListOperationsRequest() -def test_runtime_project_attachment_service_base_transport(): - # Instantiate the base transport. - with mock.patch( - "google.cloud.apihub_v1.services.runtime_project_attachment_service.transports.RuntimeProjectAttachmentServiceTransport.__init__" - ) as Transport: - Transport.return_value = None - transport = transports.RuntimeProjectAttachmentServiceTransport( - credentials=ga_credentials.AnonymousCredentials(), + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() ) + response = await client.list_operations(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 - # Every method on the transport should just blindly - # raise NotImplementedError. - methods = ( - "create_runtime_project_attachment", - "get_runtime_project_attachment", - "list_runtime_project_attachments", - "delete_runtime_project_attachment", - "lookup_runtime_project_attachment", - "get_location", - "list_locations", - "get_operation", - "cancel_operation", - "delete_operation", - "list_operations", + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) + + +def test_list_operations_field_headers(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), ) - for method in methods: - with pytest.raises(NotImplementedError): - getattr(transport, method)(request=object()) - with pytest.raises(NotImplementedError): - transport.close() + # 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 = operations_pb2.ListOperationsRequest() + request.name = "locations" - # Catch all for all remaining methods and properties - remainder = [ - "kind", - ] - for r in remainder: - with pytest.raises(NotImplementedError): - getattr(transport, r)() + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + call.return_value = operations_pb2.ListOperationsResponse() + client.list_operations(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 -def test_runtime_project_attachment_service_base_transport_with_credentials_file(): - # Instantiate the base transport with a credentials file - with mock.patch.object( - google.auth, "load_credentials_from_file", autospec=True - ) as load_creds, mock.patch( - "google.cloud.apihub_v1.services.runtime_project_attachment_service.transports.RuntimeProjectAttachmentServiceTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None - load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) - transport = transports.RuntimeProjectAttachmentServiceTransport( - credentials_file="credentials.json", - quota_project_id="octopus", - ) - load_creds.assert_called_once_with( - "credentials.json", - scopes=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id="octopus", + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_list_operations_field_headers_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + + # 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 = operations_pb2.ListOperationsRequest() + request.name = "locations" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() ) + await client.list_operations(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", + "name=locations", + ) in kw["metadata"] -def test_runtime_project_attachment_service_base_transport_with_adc(): - # Test the default credentials are used if credentials and credentials_file are None. - with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch( - "google.cloud.apihub_v1.services.runtime_project_attachment_service.transports.RuntimeProjectAttachmentServiceTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport = transports.RuntimeProjectAttachmentServiceTransport() - adc.assert_called_once() +def test_list_operations_from_dict(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.ListOperationsResponse() -def test_runtime_project_attachment_service_auth_adc(): - # If no credentials are provided, we should use ADC credentials. - with mock.patch.object(google.auth, "default", autospec=True) as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - RuntimeProjectAttachmentServiceClient() - adc.assert_called_once_with( - scopes=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id=None, + response = client.list_operations( + request={ + "name": "locations", + } ) + call.assert_called() -def test_runtime_project_attachment_service_http_transport_client_cert_source_for_mtls(): - cred = ga_credentials.AnonymousCredentials() - with mock.patch( - "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" - ) as mock_configure_mtls_channel: - transports.RuntimeProjectAttachmentServiceRestTransport( - credentials=cred, client_cert_source_for_mtls=client_cert_source_callback +@pytest.mark.asyncio +async def test_list_operations_from_dict_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_operations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.ListOperationsResponse() ) - mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + response = await client.list_operations( + request={ + "name": "locations", + } + ) + call.assert_called() -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_runtime_project_attachment_service_host_no_port(transport_name): +def test_list_locations(transport: str = "grpc"): client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - client_options=client_options.ClientOptions( - api_endpoint="apihub.googleapis.com" - ), - transport=transport_name, + transport=transport, ) - assert client.transport._host == ( - "apihub.googleapis.com:443" - if transport_name in ["grpc", "grpc_asyncio"] - else "https://apihub.googleapis.com" + + # 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 = locations_pb2.ListLocationsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.ListLocationsResponse() + response = client.list_locations(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 response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + +@pytest.mark.asyncio +async def test_list_locations_async(transport: str = "grpc_asyncio"): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + 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 = locations_pb2.ListLocationsRequest() -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_runtime_project_attachment_service_host_with_port(transport_name): + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + response = await client.list_locations(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 response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + +def test_list_locations_field_headers(): client = RuntimeProjectAttachmentServiceClient( credentials=ga_credentials.AnonymousCredentials(), - client_options=client_options.ClientOptions( - api_endpoint="apihub.googleapis.com:8000" - ), - transport=transport_name, - ) - assert client.transport._host == ( - "apihub.googleapis.com:8000" - if transport_name in ["grpc", "grpc_asyncio"] - else "https://apihub.googleapis.com:8000" ) + # 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 = locations_pb2.ListLocationsRequest() + request.name = "locations" -@pytest.mark.parametrize( - "transport_name", - [ - "rest", - ], -) -def test_runtime_project_attachment_service_client_transport_session_collision( - transport_name, -): - creds1 = ga_credentials.AnonymousCredentials() - creds2 = ga_credentials.AnonymousCredentials() - client1 = RuntimeProjectAttachmentServiceClient( - credentials=creds1, - transport=transport_name, - ) - client2 = RuntimeProjectAttachmentServiceClient( - credentials=creds2, - transport=transport_name, - ) - session1 = client1.transport.create_runtime_project_attachment._session - session2 = client2.transport.create_runtime_project_attachment._session - assert session1 != session2 - session1 = client1.transport.get_runtime_project_attachment._session - session2 = client2.transport.get_runtime_project_attachment._session - assert session1 != session2 - session1 = client1.transport.list_runtime_project_attachments._session - session2 = client2.transport.list_runtime_project_attachments._session - assert session1 != session2 - session1 = client1.transport.delete_runtime_project_attachment._session - session2 = client2.transport.delete_runtime_project_attachment._session - assert session1 != session2 - session1 = client1.transport.lookup_runtime_project_attachment._session - session2 = client2.transport.lookup_runtime_project_attachment._session - assert session1 != session2 + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + call.return_value = locations_pb2.ListLocationsResponse() + client.list_locations(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 -def test_runtime_project_attachment_path(): - project = "squid" - location = "clam" - runtime_project_attachment = "whelk" - expected = "projects/{project}/locations/{location}/runtimeProjectAttachments/{runtime_project_attachment}".format( - project=project, - location=location, - runtime_project_attachment=runtime_project_attachment, - ) - actual = RuntimeProjectAttachmentServiceClient.runtime_project_attachment_path( - project, location, runtime_project_attachment - ) - assert expected == actual + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations", + ) in kw["metadata"] -def test_parse_runtime_project_attachment_path(): - expected = { - "project": "octopus", - "location": "oyster", - "runtime_project_attachment": "nudibranch", - } - path = RuntimeProjectAttachmentServiceClient.runtime_project_attachment_path( - **expected +@pytest.mark.asyncio +async def test_list_locations_field_headers_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), ) - # Check that the path construction is reversible. - actual = ( - RuntimeProjectAttachmentServiceClient.parse_runtime_project_attachment_path( - path + # 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 = locations_pb2.ListLocationsRequest() + request.name = "locations" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() ) - ) - assert expected == actual + await client.list_locations(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", + "name=locations", + ) in kw["metadata"] -def test_common_billing_account_path(): - billing_account = "cuttlefish" - expected = "billingAccounts/{billing_account}".format( - billing_account=billing_account, - ) - actual = RuntimeProjectAttachmentServiceClient.common_billing_account_path( - billing_account + +def test_list_locations_from_dict(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), ) - assert expected == actual + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.ListLocationsResponse() + response = client.list_locations( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_parse_common_billing_account_path(): - expected = { - "billing_account": "mussel", - } - path = RuntimeProjectAttachmentServiceClient.common_billing_account_path(**expected) - # Check that the path construction is reversible. - actual = RuntimeProjectAttachmentServiceClient.parse_common_billing_account_path( - path +@pytest.mark.asyncio +async def test_list_locations_from_dict_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), ) - assert expected == actual + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.ListLocationsResponse() + ) + response = await client.list_locations( + request={ + "name": "locations", + } + ) + call.assert_called() -def test_common_folder_path(): - folder = "winkle" - expected = "folders/{folder}".format( - folder=folder, +def test_get_location(transport: str = "grpc"): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - actual = RuntimeProjectAttachmentServiceClient.common_folder_path(folder) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() -def test_parse_common_folder_path(): - expected = { - "folder": "nautilus", - } - path = RuntimeProjectAttachmentServiceClient.common_folder_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.Location() + response = client.get_location(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 - # Check that the path construction is reversible. - actual = RuntimeProjectAttachmentServiceClient.parse_common_folder_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) -def test_common_organization_path(): - organization = "scallop" - expected = "organizations/{organization}".format( - organization=organization, - ) - actual = RuntimeProjectAttachmentServiceClient.common_organization_path( - organization +@pytest.mark.asyncio +async def test_get_location_async(transport: str = "grpc_asyncio"): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + transport=transport, ) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() -def test_parse_common_organization_path(): - expected = { - "organization": "abalone", - } - path = RuntimeProjectAttachmentServiceClient.common_organization_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + response = await client.get_location(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 - # Check that the path construction is reversible. - actual = RuntimeProjectAttachmentServiceClient.parse_common_organization_path(path) - assert expected == actual + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) -def test_common_project_path(): - project = "squid" - expected = "projects/{project}".format( - project=project, +def test_get_location_field_headers(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials() ) - actual = RuntimeProjectAttachmentServiceClient.common_project_path(project) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() + request.name = "locations/abc" -def test_parse_common_project_path(): - expected = { - "project": "clam", - } - path = RuntimeProjectAttachmentServiceClient.common_project_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + call.return_value = locations_pb2.Location() - # Check that the path construction is reversible. - actual = RuntimeProjectAttachmentServiceClient.parse_common_project_path(path) - assert expected == actual + client.get_location(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", + "name=locations/abc", + ) in kw["metadata"] -def test_common_location_path(): - project = "whelk" - location = "octopus" - expected = "projects/{project}/locations/{location}".format( - project=project, - location=location, - ) - actual = RuntimeProjectAttachmentServiceClient.common_location_path( - project, location + +@pytest.mark.asyncio +async def test_get_location_field_headers_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials() ) - assert expected == actual + # 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 = locations_pb2.GetLocationRequest() + request.name = "locations/abc" -def test_parse_common_location_path(): - expected = { - "project": "oyster", - "location": "nudibranch", - } - path = RuntimeProjectAttachmentServiceClient.common_location_path(**expected) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_location), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() + ) + await client.get_location(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 - # Check that the path construction is reversible. - actual = RuntimeProjectAttachmentServiceClient.parse_common_location_path(path) - assert expected == actual + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=locations/abc", + ) in kw["metadata"] -def test_client_with_default_client_info(): - client_info = gapic_v1.client_info.ClientInfo() +def test_get_location_from_dict(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = locations_pb2.Location() - with mock.patch.object( - transports.RuntimeProjectAttachmentServiceTransport, "_prep_wrapped_messages" - ) as prep: - client = RuntimeProjectAttachmentServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - client_info=client_info, + response = client.get_location( + request={ + "name": "locations/abc", + } ) - prep.assert_called_once_with(client_info) + call.assert_called() - with mock.patch.object( - transports.RuntimeProjectAttachmentServiceTransport, "_prep_wrapped_messages" - ) as prep: - transport_class = RuntimeProjectAttachmentServiceClient.get_transport_class() - transport = transport_class( - credentials=ga_credentials.AnonymousCredentials(), - client_info=client_info, + +@pytest.mark.asyncio +async def test_get_location_from_dict_async(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), + ) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_locations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + locations_pb2.Location() ) - prep.assert_called_once_with(client_info) + response = await client.get_location( + request={ + "name": "locations", + } + ) + call.assert_called() + + +def test_transport_close_grpc(): + client = RuntimeProjectAttachmentServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc" + ) + with mock.patch.object( + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + +@pytest.mark.asyncio +async def test_transport_close_grpc_asyncio(): + client = RuntimeProjectAttachmentServiceAsyncClient( + credentials=async_anonymous_credentials(), transport="grpc_asyncio" + ) + with mock.patch.object( + type(getattr(client.transport, "_grpc_channel")), "close" + ) as close: + async with client: + close.assert_not_called() + close.assert_called_once() def test_transport_close_rest(): @@ -3837,6 +7539,7 @@ def test_transport_close_rest(): def test_client_ctx(): transports = [ "rest", + "grpc", ] for transport in transports: client = RuntimeProjectAttachmentServiceClient( @@ -3855,7 +7558,11 @@ def test_client_ctx(): [ ( RuntimeProjectAttachmentServiceClient, - transports.RuntimeProjectAttachmentServiceRestTransport, + transports.RuntimeProjectAttachmentServiceGrpcTransport, + ), + ( + RuntimeProjectAttachmentServiceAsyncClient, + transports.RuntimeProjectAttachmentServiceGrpcAsyncIOTransport, ), ], )