Merchant API cho phép bạn theo dõi trạng thái xử lý của các nguồn dữ liệu dựa trên tệp và kích hoạt ngay việc tìm nạp các nguồn dữ liệu đó theo cách thủ công.
Xem trạng thái tải tệp lên mới nhất
Để kiểm tra trạng thái của lần xử lý gần đây nhất cho một nguồn dữ liệu dựa trên tệp, hãy sử dụng phương thức fileUploads.get
. Bạn phải sử dụng biệt hiệu latest
cho fileuploadId
để truy xuất trạng thái của lần tải lên hoặc tìm nạp gần đây nhất.
Phương thức này trả về thông tin chi tiết về:
- trạng thái xử lý (chẳng hạn như
SUCCEEDED
,FAILED
,IN_PROGRESS
) - số lượng mặt hàng được xử lý
- mọi vấn đề gặp phải
- dấu thời gian tải lên
GET https://merchantapi.googleapis.com/datasources/v1beta/accounts/{ACCOUNT_ID}/dataSources/{DATASOURCE_ID}/fileUploads/latest
Yêu cầu thành công sẽ trả về tài nguyên FileUpload
chứa thông tin chi tiết về trạng thái.
Đây là ví dụ về phản hồi cho một lượt tải lên thành công có cảnh báo.
{
"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"
}
Đây là ví dụ về phản hồi cho một tệp đang tải lên.
{
"name": "accounts/123456789/dataSources/111222333/fileUploads/latest",
"dataSourceId": "111222333",
"processingState": "IN_PROGRESS",
"uploadTime": "2023-10-26T11:00:00Z"
}
Đây là ví dụ về phản hồi cho một lần tải lên không thành công.
{
"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"
}
Các mã mẫu sau đây cho biết cách lấy trạng thái của tệp được tải lên gần đây nhất bằng phương thức fileUploads.get
với bí danh 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"
Tìm nạp nguồn dữ liệu ngay lập tức
Phương thức này cho phép bạn yêu cầu tìm nạp và xử lý ngay một nguồn dữ liệu dựa trên tệp bằng phương thức dataSources.fetch
. Điều này hữu ích nếu bạn đã cập nhật tệp nguồn và muốn Google truy xuất tệp đó sớm hơn thời gian tìm nạp theo lịch tiếp theo.
POST https://merchantapi.googleapis.com/datasources/v1beta/accounts/{ACCOUNT_ID}/dataSources/{DATASOURCE_ID}:fetch
Một yêu cầu thành công để bắt đầu tìm nạp sẽ trả về một phản hồi trống vì quá trình xử lý thực tế diễn ra không đồng bộ. Bạn có thể theo dõi kết quả bằng phương thức fileUploads.get
được mô tả ở trên.
Các mã mẫu sau đây cho biết cách sử dụng phương thức dataSources.fetch
để yêu cầu tìm nạp ngay một nguồn dữ liệu nhất định.
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 '{}'