監控及觸發檔案型資料來源處理程序

透過 Merchant API,你可以監控檔案型資料來源的處理狀態,並手動立即觸發擷取作業。

取得最新檔案上傳狀態

如要查看檔案型資料來源最近一次處理作業的狀態,請使用 fileUploads.get 方法。您必須使用別名 latest 才能擷取 fileuploadId,以取得最近一次上傳或擷取的狀態。

這個方法會傳回下列項目的詳細資料:

  • 處理狀態 (例如 SUCCEEDEDFAILEDIN_PROGRESS)
  • 處理的項目數量
  • 遇到的任何問題
  • 上傳時間戳記
GET https://merchantapi.googleapis.com/datasources/v1beta/accounts/{ACCOUNT_ID}/dataSources/{DATASOURCE_ID}/fileUploads/latest

如果要求成功,系統會傳回包含狀態詳細資料的 FileUpload 資源。

以下是上傳成功但有警告的回應範例。

{
  "name": "accounts/123456789/dataSources/111222333/fileUploads/latest",
  "dataSourceId": "111222333",
  "processingState": "SUCCEEDED",
  "issues": [
    {
      "title": "Missing recommended attribute",
      "description": "Products are missing a recommended attribute 'description'.",
      "code": "validation/missing_recommended_attribute",
      "count": "5",
      "severity": "WARNING",
      "documentationUri": "https://support.google.com/merchants/answer/6324468"
    }
  ],
  "itemsTotal": "100",
  "itemsCreated": "90",
  "itemsUpdated": "10",
  "uploadTime": "2023-10-26T10:30:00Z"
}

這是上傳作業進行中的回應範例。

{
  "name": "accounts/123456789/dataSources/111222333/fileUploads/latest",
  "dataSourceId": "111222333",
  "processingState": "IN_PROGRESS",
  "uploadTime": "2023-10-26T11:00:00Z"
}

以下是上傳失敗的回應範例。

{
  "name": "accounts/123456789/dataSources/111222333/fileUploads/latest",
  "dataSourceId": "111222333",
  "processingState": "FAILED",
  "issues": [
    {
      "title": "Invalid file format",
      "description": "The uploaded file is not a valid XML or CSV file.",
      "code": "validation/invalid_file_format",
      "count": "1",
      "severity": "ERROR",
      "documentationUri": "https://support.google.com/merchants/answer/188494"
    }
  ],
  "uploadTime": "2023-10-26T11:15:00Z"
}

以下程式碼範例說明如何使用 fileUploads.get 方法和 latest 別名,取得最近一次檔案上傳的狀態。

Java

import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.FileUpload;
import com.google.shopping.merchant.datasources.v1beta.FileUploadName;
import com.google.shopping.merchant.datasources.v1beta.FileUploadsServiceClient;
import com.google.shopping.merchant.datasources.v1beta.FileUploadsServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.GetFileUploadRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;

/** This class demonstrates how to get the latest data source file upload. */
public class GetFileUploadSample {

  public static void getFileUpload(Config config, String datasourceId) throws Exception {

    // Obtains OAuth token based on the user's configuration.
    GoogleCredentials credential = new Authenticator().authenticate();

    // Creates service settings using the credentials retrieved above.
    FileUploadsServiceSettings fileUploadsServiceSettings =
        FileUploadsServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    // Creates FileUpload name with datasource id to identify datasource.
    String name =
        FileUploadName.newBuilder()
            .setAccount(config.getAccountId().toString())
            .setDatasource(datasourceId)
            .setFileupload("latest")
            .build()
            .toString();

    // Calls the API and catches and prints any network failures/errors.
    try (FileUploadsServiceClient fileUploadsServiceClient =
        FileUploadsServiceClient.create(fileUploadsServiceSettings)) {

      // The name has the format: accounts/{account}/datasources/{datasource}/fileUploads/latest
      GetFileUploadRequest request = GetFileUploadRequest.newBuilder().setName(name).build();

      System.out.println("Sending get FileUpload request:");
      FileUpload response = fileUploadsServiceClient.getFileUpload(request);

      System.out.println("Retrieved FileUpload below");
      System.out.println(response);
      //   return response;
    } catch (Exception e) {
      System.out.println(e);
    }
  }

  public static void main(String[] args) throws Exception {
    Config config = Config.load();
    // An ID assigned to a datasource by Google.
    String datasourceId = "123456789";

    getFileUpload(config, datasourceId);
  }
}

cURL

curl \
"https://merchantapi.googleapis.com/datasources/v1beta/accounts/{ACCOUNT_ID}/dataSources/{DATASOURCE_ID}/fileUploads/latest" \
-H "Authorization: Bearer <API_TOKEN>" \
-H "Content-Type: application/json"

立即擷取資料來源

您可以使用 dataSources.fetch 方法,要求系統立即擷取及處理檔案型資料來源。如果您已更新來源檔案,並希望 Google 盡快擷取檔案 (而非等到下次排定的擷取時間),這個功能就非常實用。

POST https://merchantapi.googleapis.com/datasources/v1beta/accounts/{ACCOUNT_ID}/dataSources/{DATASOURCE_ID}:fetch

成功發出擷取啟動要求後,系統會傳回空白回應,因為實際處理作業是以非同步方式進行。您可以使用先前所述的 fileUploads.get 方法監控結果。

下列程式碼範例說明如何使用 dataSources.fetch 方法,要求立即擷取指定資料來源。

Java

import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.DataSourceName;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.FetchDataSourceRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;

/**
 * This class demonstrates how to fetch a specific FileInput DataSource for a given Merchant Center
 * account.
 */
public class FetchFileDataSourceSample {

  public static void fetchDataSource(Config config, String dataSourceId) throws Exception {

    // Obtains OAuth token based on the user's configuration.
    GoogleCredentials credential = new Authenticator().authenticate();

    // Creates service settings using the credentials retrieved above.
    DataSourcesServiceSettings dataSourcesServiceSettings =
        DataSourcesServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    // Creates datasource name to identify datasource.
    String name =
        DataSourceName.newBuilder()
            .setAccount(config.getAccountId().toString())
            .setDatasource(dataSourceId)
            .build()
            .toString();

    // Calls the API and catches and prints any network failures/errors.
    try (DataSourcesServiceClient dataSourcesServiceClient =
        DataSourcesServiceClient.create(dataSourcesServiceSettings)) {

      // The name has the format: accounts/{account}/datasources/{datasource}
      FetchDataSourceRequest request = FetchDataSourceRequest.newBuilder().setName(name).build();

      System.out.println("Sending FETCH DataSource request:");
      // Fetch works ONLY for FileInput DataSource type.
      dataSourcesServiceClient.fetchDataSource(request); // No response returned on success

      System.out.println("Successfully fetched DataSource.");

    } catch (Exception e) {
      System.out.println(e);
      System.exit(1);
    }
  }

  public static void main(String[] args) throws Exception {
    Config config = Config.load();
    String datasourceId = "<DATASOURCE_ID>"; // Replace with your FileInput DataSource ID.

    fetchDataSource(config, datasourceId);
  }
}

cURL

curl -X POST \
"https://merchantapi.googleapis.com/datasources/v1beta/accounts/{ACCOUNT_ID}/dataSources/{DATASOURCE_ID}:fetch" \
-H "Authorization: Bearer <API_TOKEN>" \
-H "Content-Type: application/json" \
-d '{}'