2017-01-04 3 views
1

公式docによれば、Python用Google APIクライアントライブラリからバッチリクエストを行うことができます。ここには例がありますGoogle API Client for Python。バッチリクエスト:コールバック内の特定のリクエストにアクセスする方法

from apiclient.http import BatchHttpRequest 

def list_animals(request_id, response, exception): 
    if exception is not None: 
    # Do something with the exception 
    pass 
    else: 
    # Do something with the response 
    pass 

def list_farmers(request_id, response): 
    """Do something with the farmers list response.""" 
    pass 

service = build('farm', 'v2') 

batch = service.new_batch_http_request() 

batch.add(service.animals().list(), callback=list_animals) 
batch.add(service.farmers().list(), callback=list_farmers) 
batch.execute(http=http) 

コールバックのリクエストにアクセスする方法はありますか?たとえば、例外がある場合はリクエスト(request_idではなく)を出力しますか?

答えて

0

私はcompute.instances().aggregatedList_next()機能を使用するための元の要求が必要なので、私はこの同じ問題に遭遇しました。

私はユニークなrequest_idbatch.add()に渡してしまい、元の要求を追跡するための辞書を作成しました。次に、コールバック内で、その辞書を参照し、固有のrequest_idを使用して要求を取り出しました。呼び出し可能な関数内

requests = {} 
project = 'example-project' 
batch = service.new_batch_http_request(callback=callback_callable) 
new_request = compute.instances().aggregatedList(project=project) 
requests[project] = new_request 
batch.add(new_request, request_id=project) 

:REQUEST_IDに渡された値が文字列である必要が

def callback_callable(self, request_id, response, exception): 
    original_request = requests[request_id] 

だから私は、次のように沿って何かをしました。 intが渡された場合、それはTypeErrorのquote_from_bytes()の予想バイトを送出します。

これが誰かを助けることを願っています!