googleapis / google-api-python-client

🐍 The official Python client library for Google's discovery based APIs.
https://googleapis.github.io/google-api-python-client/docs/
Apache License 2.0
7.65k stars 2.39k forks source link

new_batch_http_request() has no mechanism to change the batch endpoint #1107

Open mbookman opened 3 years ago

mbookman commented 3 years ago

When creating a service the discovery.build() function calls in to create a new Resource(). The Resource adds a the new_batch_http_request() function here:

https://github.com/googleapis/google-api-python-client/blob/c6912836e88eea45aef7d515383e549082d37717/googleapiclient/discovery.py#L1342

In that block of code, the batch URI is set based on fixed values from the service description:

            batch_uri = "%s%s" % (
                rootDesc["rootUrl"],
                rootDesc.get("batchPath", "batch"),
            )

for other endpoints, the URI can be overridden with client_options

https://github.com/googleapis/google-api-python-client/blob/c6912836e88eea45aef7d515383e549082d37717/googleapiclient/discovery.py#L493

    # If an API Endpoint is provided on client options, use that as the base URL
    base = urljoin(service["rootUrl"], service["servicePath"])
    if client_options.api_endpoint:
        base = client_options.api_endpoint

   ...

    return Resource(
        http=http,
        baseUrl=base,
        model=model,
        developerKey=developerKey,
        requestBuilder=requestBuilder,
        resourceDesc=service,
        rootDesc=service,
        schema=schema,
    )

This is important for the Google Lifesciences API, which supports regional batch endpoints. So when sending batch requests for operations in the us-west2 region, for example, we need to be able to target:

https://us-west2-lifesciences.googleapis.com/batch

I've worked around this for the time being by creating my own new_batch_http_request() function that allows for a custom batch_uri, but it would be nice for the Python client here to support a client_options field for this.

parthea commented 3 years ago

Hi @mbookman. Thanks for suggesting an improvement! Are you interested in creating a pull request?

mbookman commented 3 years ago

I'd be happy to. Any guidance on:

Thanks,

-Matt

busunkim96 commented 3 years ago

@mbookman Thanks for filing this issue! I would expect setting api_endpoint to change the endpoint for batch requests as well.

I'd prefer option 2 (using self._baseUrl instead of rootDesc["rootUrl"] when constructing the batch path) in Resource.

https://github.com/googleapis/google-api-python-client/blob/c6912836e88eea45aef7d515383e549082d37717/googleapiclient/discovery.py#L1342-L1348

parthea commented 3 years ago

I'm going to relabel this issue as a feature request since the OP mentioned that there is a currently a solution available to set the _batch_uri manually which works without modifications to the source. For example, using the code below:

from googleapiclient.discovery import build
lifesciences = build('lifesciences', 'v2beta')
ls_batch = lifesciences.new_batch_http_request(callback=_no_op)
ls_batch._batch_uri = 'https://us-west2-lifesciences.googleapis.com/batch'
print(ls_batch._batch_uri)

Pull requests are welcome!