批次要求

本文說明如何批次處理 API 呼叫,以減少用戶端必須建立的 HTTP 連線數量。

本文件專門說明如何透過傳送 HTTP 要求來提出批次要求。但如果您使用 Google 用戶端程式庫提出批次要求,請參閱用戶端程式庫的說明文件

總覽

用戶端建立的每個 HTTP 連線都會造成一定程度的負擔。Manufacturer Center API 支援批次作業,可讓用戶端在單一 HTTP 要求中加入數個 API 呼叫。

以下是您可能想要使用批次作業的狀況範例:

    • 正在上傳大量產品。

    • 刪除大量產品。

    • 擷取大量產品。

這樣一來,您就能將這些呼叫分組為單一 HTTP 要求,而不必分別傳送每個呼叫。所有內部要求都必須前往同一個 Google API。

單一批次要求最多包含 1000 個呼叫。如果您需要超過數量的呼叫,請使用多個批次要求。

注意:Manufacturer Center API 批次系統使用的語法與 OData 批次處理系統相同,但語意不同。

批次詳細資料

批次要求是由多個 API 呼叫合併成一個 HTTP 要求,可傳送至 API 探索文件中指定的 batchPath。預設路徑為 /batch/api_name/api_version。本節詳細說明批次語法,並在後半段提供範例

注意:一組批次處理的 n 要求會計入 n 要求,而不是一個要求。在處理之前,系統會先將批次要求分成一組要求。

批次要求的格式

批次要求是單一標準 HTTP 要求,內含多個使用 multipart/mixed 內容類型的 Manufacturer Center API 呼叫。在主要 HTTP 要求中,每個部分都含有一個巢狀的 HTTP 要求。

每個部分都以自己的 Content-Type: application/http HTTP 標頭開頭。也可以使用選用的 Content-ID 標頭。不過,部分標頭只是用來標示該部分的開頭;這些標頭與巢狀要求各自獨立。伺服器將批次要求解除包裝為不同的要求後,就會忽略部分標頭。

每個分部的主體本身就是一個完整的 HTTP 要求,有自己的動詞、URL、標頭和內文。HTTP 要求只能包含 URL 的路徑部分;批次要求禁止納入完整的 URL。

外部批次要求的 HTTP 標頭 (例如 Content-TypeContent- 標頭除外) 會套用至批次中的每個要求。如果您在外部要求和個別呼叫中均指定所提供的 HTTP 標頭,則個別呼叫標頭的值會覆寫外部批次要求標頭的值。個別呼叫的標頭只會套用於該呼叫。

例如,如果您提供 Authorization 標頭用於特定呼叫,則該標頭只會套用於該呼叫。如果您提供 Authorization 標頭用於外部要求,則除非個別呼叫以自己的 Authorization 標頭覆寫所提供的標頭,否則所提供的 Authorization 標頭會套用於所有個別呼叫。

當伺服器收到批次要求時,即會將外部要求的查詢參數和標頭 (在適用情況下) 套用至每一個分部,然後將每個分部視為不同的 HTTP 要求。

回應批次要求

伺服器的回應是包含 multipart/mixed 內容類型的單一標準 HTTP 回應;每個部分都是批次要求中其中一個要求的回應,回應順序與要求順序相同。

就像要求中的分部一樣,每個回應分部都含有完整的 HTTP 回應,包括狀態碼、標頭和內文;如同要求中的部分,每個回應部分前面都會加上 Content-Type 標頭,標示部分開頭。

如果要求的特定部分有 Content-ID 標頭,則回應的對應部分會有相符的 Content-ID 標頭,且原始值前面會加上 response- 字串,如以下範例所示。

注意事項:伺服器得按任意順序執行您的呼叫。不要期望呼叫會按您的指定順序執行。如果您想要確保兩個呼叫依指定順序執行,就不能以單一要求傳送它們,而要先傳送第一個呼叫,然後等到第一個呼叫的回應後才能傳送第二個呼叫。

範例

以下範例顯示搭配 Manufacturer Center API 使用批次作業的方式。

批次要求範例


POST https://manufacturers.googleapis.com/batch
Authorization: Bearer your_auth_token
Content-Type: multipart/mixed; boundary=--batch_item

--batch_item
Content-Type: application/http
Content-ID: 

PUT /v1/accounts/account_id/products/targetCountry:contentLanguage:productId
Content-Type: application/json

{
   "gtin": "gtin",
   "product_name": "product_name",
   "description": "description",
   "image_link": {
       "image_url": "image_url"
   }
}
--batch_item
Content-Type: application/http
Content-ID: 

GET /v1/accounts/account_id/products/targetCountry:contentLanguage:productId
--batch_item
Content-Type: application/http
Content-ID: 

DELETE /v1/accounts/account_id/products/targetCountry:contentLanguage:productId
--batch_item--

批次回應範例

這是前一節中範例要求的回應。



--batch_OycPgXWaQD5f20sVgri2ETiygT65fMaa
Content-Type: application/http
Content-ID: 

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Vary: Origin
Vary: X-Origin
Vary: Referer

{}

--batch_OycPgXWaQD5f20sVgri2ETiygT65fMaa
Content-Type: application/http
Content-ID: 

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Vary: Origin
Vary: X-Origin
Vary: Referer

{
  "parent": "accounts/account_id",
  "name": "targetCountry:contentLanguage:productId",
  "targetCountry": "targetCountry",
  "contentLanguage": "contentLanguage",
  "productId": "productId"
}

--batch_OycPgXWaQD5f20sVgri2ETiygT65fMaa
Content-Type: application/http
Content-ID: 

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Vary: Origin
Vary: X-Origin
Vary: Referer

{}

--batch_OycPgXWaQD5f20sVgri2ETiygT65fMaa--

必要條件

Manufacturer Center 帳戶

批次範例

下列程式碼示範如何使用 Java 批次傳送產品更新。

Java

String parent = "accounts/123456";
String newProductName = "US:en:product_id_1";

Image image = new Image();
image.setUrl("http://www.example.com/example.png");

Attributes attributes = new Attributes();
attributes.setGtin(ImmmutableList.of("1234567890"));
attributes.setImageLink(image);

// Creates a new BatchRequest object from the ManufacturerCenter object.
BatchRequest batch = manufacturerCenter.batch();

// JsonBatchCallback generic type is Empty to match the return type of update API.
JsonBatchCallback updateProductCallback =  new JsonBatchCallback() {
    public void onSuccess(Empty empty, HttpHeaders responseHeaders) {
        System.out.printf("Product updated successfully.\n");
    }

    public void onFailure(GoogleJsonError error, HttpHeaders responseHeaders)
            throws IOException {
        System.out.printf("Error updating product: %s.\n", error.getMessage());
    }
}

// Adds update product request to batch object.
manufacturerCenter.accounts().products().update(parent, newProductName, attributes)
    .queue(batch, updateProductCallback);

String getProductName = "US:en:product_id_2";

// JsonBatchCallback generic type is Product to match the return type of get API.
JsonBatchCallback getProductCallback =  new JsonBatchCallback() {
    public void onSuccess(Product product, HttpHeaders responseHeaders) {
        System.out.printf("Found product: %s.\n", product.getName());
    }

    public void onFailure(GoogleJsonError error, HttpHeaders responseHeaders)
            throws IOException {
        System.out.printf("Error retrieving product: %s.\n", error.getMessage());
    }
}

// Adds get product request to batch object.
manufacturerCenter.accounts().products().get(parent, getProductName)
    .queue(batch, getProductCallback);

// Sends batch request to Manufacturer Center API.
batch.execute();