Dosya tabanlı veri kaynağı işlemeyi izleme ve tetikleme

Merchant API, dosya tabanlı veri kaynaklarınızın işleme durumunu izlemenize ve bu kaynakların anında getirilmesini manuel olarak tetiklemenize olanak tanır.

En son dosya yükleme durumunu alma

Dosya tabanlı bir veri kaynağı için en son işleme denemesinin durumunu kontrol etmek üzere fileUploads.get yöntemini kullanın. En son yükleme veya getirme işleminin durumunu almak için fileuploadId için latest takma adını kullanmanız gerekir.

Bu yöntem, aşağıdakilerle ilgili ayrıntıları döndürür:

  • işleme durumu (örneğin, SUCCEEDED, FAILED, IN_PROGRESS)
  • işlenen öğe sayısı
  • Karşılaşılan sorunlar
  • yükleme zaman damgası
GET https://merchantapi.googleapis.com/datasources/v1/accounts/{ACCOUNT_ID}/dataSources/{DATASOURCE_ID}/fileUploads/latest

Başarılı bir istek, durum ayrıntılarını içeren FileUpload kaynağını döndürür.

Bu, uyarılarla birlikte başarılı bir yükleme işlemine verilen yanıt örneğidir.

{
  "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"
}

Bu, devam eden bir yükleme işlemine verilen yanıtın örneğidir.

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

Bu, başarısız bir yükleme için verilen yanıt örneğidir.

{
  "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"
}

Aşağıdaki kod örneklerinde, latest takma adıyla fileUploads.get yöntemi kullanılarak en son dosya yüklemenin durumunun nasıl alınacağı gösterilmektedir.

Java

import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1.FileUpload;
import com.google.shopping.merchant.datasources.v1.FileUploadName;
import com.google.shopping.merchant.datasources.v1.FileUploadsServiceClient;
import com.google.shopping.merchant.datasources.v1.FileUploadsServiceSettings;
import com.google.shopping.merchant.datasources.v1.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/v1/accounts/{ACCOUNT_ID}/dataSources/{DATASOURCE_ID}/fileUploads/latest" \
-H "Authorization: Bearer <API_TOKEN>" \
-H "Content-Type: application/json"

Veri kaynağını hemen getirme

Bu yöntem, dataSources.fetch yöntemini kullanarak dosya tabanlı bir veri kaynağının anında getirilmesini ve işlenmesini istemenize olanak tanır. Bu özellik, kaynak dosyanızı güncellediyseniz ve Google'ın dosyayı bir sonraki planlanmış getirme işleminden daha önce almasını istiyorsanız kullanışlıdır.

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

Getirme işlemini başlatma isteği başarılı olursa gerçek işleme eşzamansız olarak gerçekleştiği için boş bir yanıt döndürülür. Sonucu, daha önce açıklanan fileUploads.get yöntemini kullanarak izleyebilirsiniz.

Aşağıdaki kod örneklerinde, belirli bir veri kaynağının hemen getirilmesini istemek için dataSources.fetch yönteminin nasıl kullanılacağı gösterilmektedir.

Java

import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1.DataSourceName;
import com.google.shopping.merchant.datasources.v1.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1.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/v1/accounts/{ACCOUNT_ID}/dataSources/{DATASOURCE_ID}:fetch" \
-H "Authorization: Bearer <API_TOKEN>" \
-H "Content-Type: application/json" \
-d '{}'