Search code examples
pythongoogle-cloud-platformgoogle-cloud-functionsgoogle-api-python-client

How can I get a list of Google Cloud Functions using Google Python Client for Cloud Functions?


I want to perform what I think is a fairly simple task - using python I would like to obtain a list of Google Cloud Functions in a given project and want to figure out the correct library for doing so.

I'm trying to understand the myriad of repos at https://github.com/googleapis. I can see there is

however at https://github.com/googleapis/google-api-python-client#other-google-api-libraries it is stated:

The maintainers of this repository recommend using Cloud Client Libraries for Python, where possible, for new code development

and following the link there leads me to

at which there is a link to

This would seem to be the more appropriate library to use (pip install google-cloud-functions) for the reasons stated at https://github.com/googleapis/google-api-python-client#other-google-api-libraries.

The problem I have is that there seem to be many examples/samples that demonstrate the use of https://github.com/googleapis/google-api-python-client but less so for https://github.com/googleapis/python-functions. In fact my google-fu has failed me miserably because I can't find any examples of how I might use https://github.com/googleapis/python-functions to achieve my task (i.e. get a list of Google Cloud Functions). The API documentation at https://googleapis.dev/python/cloudfunctions/latest/index.html is somewhat helpful however it seems rather lacking in sample code. One question I'm asking myself, for example, is should I be using

or

or perhaps even something else.

Can someone explain how I can use CloudFunctionsServiceClient or CloudFunctionsServiceAsyncClient to get a list of functions?


Solution

  • Figured it out. These work

    from google.cloud.functions_v1.services.cloud_functions_service import CloudFunctionsServiceAsyncClient
    from google.cloud.functions_v1.types import ListFunctionsRequest
    list_functions_request = ListFunctionsRequest(parent=f"projects/{project}/locations/{region}")
    await CloudFunctionsServiceAsyncClient().list_functions(list_functions_request)
    
    from google.cloud.functions_v1.services.cloud_functions_service import CloudFunctionsServiceClient
    from google.cloud.functions_v1.types import ListFunctionsRequest
    list_functions_request = ListFunctionsRequest(parent=f"projects/{project}/locations/{region}")
    await CloudFunctionsServiceClient().list_functions(list_functions_request)