Batch Request in python to Google Search Console API

此生再无相见时 提交于 2021-02-08 13:16:40

问题


For the past two days I've been trying to send a batch request to Google Search Console API in Python but I am unsure of the documentation and how to proceed.

I'm not sure which documents to follow.

https://developers.google.com/api-client-library/python/guide/batch

https://developers.google.com/webmaster-tools/v3/how-tos/batch

The first document says to use this format:

from apiclient.http import BatchHttpRequest

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

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

batch = service.new_batch_http_request(callback=insert_animal)

batch.add(service.animals().insert(name="sheep"))
batch.add(service.animals().insert(name="pig"))
batch.add(service.animals().insert(name="llama"))
batch.execute(http=http)

but the second post says to do this:

POST /batch HTTP/1.1
Authorization: Bearer your_auth_token
Host: www.googleapis.com
Content-Type: multipart/mixed; boundary=batch_foobarbaz
Content-Length: total_content_length

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item1:12930812@barnyard.example.com>

GET /farm/v1/animals/pony

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item2:12930812@barnyard.example.com>

PUT /farm/v1/animals/sheep
Content-Type: application/json
Content-Length: part_content_length
If-Match: "etag/sheep"

{
  "animalName": "sheep",
  "animalAge": "5"
  "peltColor": "green",
}

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item3:12930812@barnyard.example.com>

GET /farm/v1/animals
If-None-Match: "etag/animals"

--batch_foobarbaz--

but I'm not sure the last one wants me to do with this code?

Also, when trying to make a Post request in Request package, where can I enter a body parameter?

Thank you.


回答1:


Here's a technique for making GET requests, and presumably POST requests would be very similar. In this example we are making batch requests to the search analytics API.

#-------------------------------------#
# Define property and request payloads
#-------------------------------------#

web_property = 'https://example.com/'

request_body_1 = {
  "startDate": "2015-01-01",
  "endDate": "2016-01-01",
  "dimensions": ["country", "device"]
}
request_body_2 = {
  "startDate": "2016-01-01",
  "endDate": "2017-01-01",
  "dimensions": ["country", "device"]
}

#--------------------------------#
# Make container to store results
#--------------------------------#

class DataContainer:
    def __init__(self):
        self.data = []

    def callback(self, request_id, response, exception):
        if exception is not None:
            # do something ...
            pass
        else:
            print(request_id)
            self.data.append(response)

dc = DataContainer()

#--------------------------------#
# Build and send batch requests
#--------------------------------#

batch = service.new_batch_http_request(callback=dc.callback)

batch.add(service.query(siteUrl=web_property,
                        body=request_body_1))
batch.add(service.query(siteUrl=web_property,
                        body=request_body_2))

batch.execute()
print(dc.data)


来源:https://stackoverflow.com/questions/41503866/batch-request-in-python-to-google-search-console-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!