googleapis / gapic-generator-python

Generate Python API client libraries from Protocol Buffers.
Apache License 2.0
122 stars 69 forks source link

Improve autogenerated code snippets for google-cloud-gke-multicloud to use regional endpoint #1649

Open emilianooddo opened 1 year ago

emilianooddo commented 1 year ago

Hi, I tried to use the list_attached_clusters method with AttachedClustersClient, but I receive the following error:

E0123 16:45:39.593000000 12572 src/core/ext/transport/chttp2/transport/hpack_parser.cc:1227] Error parsing metadata: error=invalid value key=content-type value=text/html; charset=UTF-8
Traceback (most recent call last):
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python38\lib\site-packages\google\api_core\grpc_helpers.py", line 72, in error_remapped_callable
    return callable_(*args, **kwargs)
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python38\lib\site-packages\grpc\_channel.py", line 946, in __call__
    return _end_unary_response_blocking(state, call, False, None)
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python38\lib\site-packages\grpc\_channel.py", line 849, in _end_unary_response_blocking
    raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
        status = StatusCode.UNIMPLEMENTED
        details = "Received http2 header with status: 404"
        debug_error_string = "UNKNOWN:Error received from peer ipv4:142.250.184.106:443 {grpc_message:"Received http2 header with status: 404", grpc_status:12, created_time:"2023-01-23T15:45:39.6079098+00:00"}"
>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File ".\test.py", line 57, in <module>
    main()
  File ".\test.py", line 54, in main
    sample_list_attached_clusters()
  File ".\test.py", line 40, in sample_list_attached_clusters
    page_result = client.list_attached_clusters(request=request)
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python38\lib\site-packages\google\cloud\gke_multicloud_v1\services\attached_clusters\client.py", line 1174, in list_attached_clusters
    response = rpc(
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python38\lib\site-packages\google\api_core\gapic_v1\method.py", line 113, in __call__
    return wrapped_func(*args, **kwargs)
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python38\lib\site-packages\google\api_core\retry.py", line 349, in retry_wrapped_func
    return retry_target(
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python38\lib\site-packages\google\api_core\retry.py", line 191, in retry_target
    return target()
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python38\lib\site-packages\google\api_core\timeout.py", line 120, in func_with_timeout
    return func(*args, **kwargs)
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python38\lib\site-packages\google\api_core\grpc_helpers.py", line 74, in error_remapped_callable
    raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.MethodNotImplemented: 501 Received http2 header with status: 404

For better understanding, I write down the code sample that I've used:

    client = gke_multicloud_v1.AttachedClustersClient(credentials=credentials)

    # Initialize request argument(s)
    request = gke_multicloud_v1.ListAttachedClustersRequest(
        parent=f"projects/{project_id}/locations/us-central1",
    )

    # Make the request
    page_result = client.list_attached_clusters(request=request)

    # Handle the response
    for response in page_result:
        print(response)

Thanks in advance

parthea commented 1 year ago

I was able to reproduce the issue. I was able to get the code to work by specifying an api_endpoint. For example, the code below works

    endpoint = "us-west1-gkemulticloud.googleapis.com"
    client_options = {"api_endpoint": endpoint}
    client = gke_multicloud_v1.AttachedClustersClient(client_options=client_options)

See the full code below

from google.cloud import gke_multicloud_v1

def sample_list_attached_clusters():
    # Specify the endpoint
    endpoint = "us-west1-gkemulticloud.googleapis.com"
    client_options = {"api_endpoint": endpoint}

    # Create a client
    client = gke_multicloud_v1.AttachedClustersClient(client_options=client_options)

    # Initialize request argument(s)
    request = gke_multicloud_v1.ListAttachedClustersRequest(
        parent="projects/partheniou-303520/locations/us-west1",
    )

    # Make the request
    page_result = client.list_attached_clusters(request=request)

    # Handle the response
    for response in page_result:
        print(response)

sample_list_attached_clusters()

I'll keep this issue open so we can improve the sample code and/or client.

parthea commented 1 year ago

I'll transfer this issue to the repository that generates the code snippets so we can track improving the code snippets that we generate.