user17439146
user17439146

Reputation:

TypeError: Invalid constructor input for ListRecommendationsRequest:

I'm working on a python script to retrieve the recommendations from the Google Api Recommender and getting this error: "TypeError: Invalid constructor input for ListRecommendationsRequest:" My code looks like this:

from google.cloud import recommender


PROJECT = 'myproject'

LOCATION= 'us-east1'

RECOMMENDERS = [                                                                                                              
    # recommenders related with Commitments                                                                                   
    'google.compute.commitment.UsageCommitmentRecommender',                                                                   
    'google.cloudbilling.commitment.SpendBasedCommitmentRecommender',                                                         
    # recommenders related with clousql                                                                                       
    'google.cloudsql.instance.OutOfDiskRecommender',                                                                          
    'google.cloudsql.instance.IdleRecommender',                                                                               
    'google.cloudsql.instance.OverprovisionedRecommender',                                                                    
    # recommenders related with Compute                                                                                       
    'google.compute.image.IdleResourceRecommender',                                                                           
    'google.compute.address.IdleResourceRecommender',                                                                         
    'google.compute.disk.IdleResourceRecommender',                                                                            
    'google.compute.instance.IdleResourceRecommender',                                                                        
    'google.compute.instanceGroupManager.MachineTypeRecommender',                                                             
    'google.compute.instance.MachineTypeRecommender'                                                                          
]
client = recommender.RecommenderClient()             
parent = client.recommender_path(PROJECT, LOCATION, RECOMMENDERS[0])
client.list_recommendations(parent=parent)
  
for element in client.list_recommendations(parent):
    print(i)

Upvotes: 0

Views: 2588

Answers (1)

jccampanero
jccampanero

Reputation: 53391

I think you are using the version 2.x of the API.

As you can see in the documentation, the new API version introduced a breaking change consisting in the use of requests objects to encapsulate the request information.

They provide the following example:

Before:

from google.cloud import recommender

client = recommender.RecommenderClient()
name = client.insight_path('[PROJECT]', '[LOCATION]', '[INSIGHT_TYPE]', > '[INSIGHT]')
etag = "my_etag"
response = client.mark_insight_accepted(name=name, etag=etag)

After:

from google.cloud import recommender

client = recommender.RecommenderClient()
name = client.insight_path('[PROJECT]', '[LOCATION]', '[INSIGHT_TYPE]', '[INSIGHT]')
etag = "my_etag"
response = client.mark_insight_accepted(request={"name": name, "etag": etag})

Note the inclusion of the request parameter.

In your specific use case, list_recommendations has the following signature:

async def list_recommendations(
        self,
        request: Union[recommender_service.ListRecommendationsRequest, dict] = None,
        *,
        parent: str = None,
        filter: str = None,
        retry: OptionalRetry = gapic_v1.method.DEFAULT,
        timeout: float = None,
        metadata: Sequence[Tuple[str, str]] = (),
    ) -> pagers.ListRecommendationsAsyncPager

Note how request is the first positional argument.

As a consequence, the error in your code is in this line:

for element in client.list_recommendations(parent):

because the API is assuming parent as the request object.

To solve the problem, either provide the parent argument as a named argument as you are in fact doing:

client.list_recommendations(parent=parent)

Or define the request parameter appropriately (a dict will do the trick):

client.list_recommendations(request={"parent": parent})

Please, consider read the documentation about the different information you can provide for ListRecommendationsRequest.

Upvotes: 1

Related Questions