Page Summary
-
This content provides code samples in Java, PHP, and Python for creating a file-based primary product data source using the Merchant API.
-
The examples demonstrate how to configure a
FileInputwithFetchSettingsto enable automatic data retrieval from a specified URL on a daily schedule at 22:00 in the Europe/London time zone. -
These code snippets set the primary product data source's channel to "ONLINE_PRODUCTS", which can be used for online products, and they define the content language as English ("en") and set the feed label as "GB" for Great Britain.
-
The code shows how to authenticate and create a data source service client, and then use that to create the new file data source.
-
The file input being created in these code samples is a
Fetchtype, meaning it will automatically pull its data from a specified url, as opposed to being uploaded manually through the UI.
Merchant API code sample to create a file fetch primary product data source.
Java
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package
shopping.merchant.samples.datasources.v1
;
import
com.google.api.gax.core.FixedCredentialsProvider
;
import
com.google.auth.oauth2.GoogleCredentials
;
import
com.google.shopping.merchant.datasources.v1.CreateDataSourceRequest
;
import
com.google.shopping.merchant.datasources.v1.DataSource
;
import
com.google.shopping.merchant.datasources.v1.DataSourcesServiceClient
;
import
com.google.shopping.merchant.datasources.v1.DataSourcesServiceSettings
;
import
com.google.shopping.merchant.datasources.v1.FileInput
;
import
com.google.shopping.merchant.datasources.v1.FileInput.FetchSettings
;
import
com.google.shopping.merchant.datasources.v1.PrimaryProductDataSource
;
import
com.google.type.TimeOfDay
;
import
shopping.merchant.samples.utils.Authenticator
;
import
shopping.merchant.samples.utils.Config
;
/** This class demonstrates how to insert a File primary product datasource */
public
class
CreateFileFetchPrimaryProductDataSourceSample
{
private
static
String
getParent
(
String
merchantId
)
{
return
String
.
format
(
"accounts/%s"
,
merchantId
);
}
private
static
FileInput
setFileInput
()
{
FetchSettings
fetchSettings
=
FetchSettings
.
newBuilder
()
.
setEnabled
(
true
)
// Note that the system only respects hours for the fetch schedule.
.
setTimeOfDay
(
TimeOfDay
.
newBuilder
().
setHours
(
22
).
build
())
.
setTimeZone
(
"Europe/London"
)
.
setFrequency
(
FetchSettings
.
Frequency
.
FREQUENCY_DAILY
)
.
setFetchUri
(
"https://example.file.com/products"
)
.
build
();
return
FileInput
.
newBuilder
().
setFetchSettings
(
fetchSettings
).
build
();
}
public
static
String
createDataSource
(
Config
config
,
String
displayName
,
FileInput
fileInput
)
throws
Exception
{
GoogleCredentials
credential
=
new
Authenticator
().
authenticate
();
DataSourcesServiceSettings
dataSourcesServiceSettings
=
DataSourcesServiceSettings
.
newBuilder
()
.
setCredentialsProvider
(
FixedCredentialsProvider
.
create
(
credential
))
.
build
();
String
parent
=
getParent
(
config
.
getAccountId
().
toString
());
// The type of data that this datasource will receive.
PrimaryProductDataSource
primaryProductDataSource
=
PrimaryProductDataSource
.
newBuilder
()
.
addCountries
(
"GB"
)
// `contentLanguage` and `feedLabel` must be set.
.
setContentLanguage
(
"en"
)
.
setFeedLabel
(
"GB"
)
.
build
();
try
(
DataSourcesServiceClient
dataSourcesServiceClient
=
DataSourcesServiceClient
.
create
(
dataSourcesServiceSettings
))
{
CreateDataSourceRequest
request
=
CreateDataSourceRequest
.
newBuilder
()
.
setParent
(
parent
)
.
setDataSource
(
DataSource
.
newBuilder
()
.
setDisplayName
(
displayName
)
.
setPrimaryProductDataSource
(
primaryProductDataSource
)
.
setFileInput
(
fileInput
)
.
build
())
.
build
();
System
.
out
.
println
(
"Sending Create File Fetch PrimaryProduct DataSource request"
);
DataSource
response
=
dataSourcesServiceClient
.
createDataSource
(
request
);
System
.
out
.
println
(
"Created DataSource Name below"
);
System
.
out
.
println
(
response
.
getName
());
return
response
.
getName
();
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
e
);
System
.
exit
(
1
);
return
null
;
}
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
Config
config
=
Config
.
load
();
// The displayed datasource name in the Merchant Center UI.
String
displayName
=
"British File Fetch Primary Product Data"
;
// The file input data that this datasource will receive.
FileInput
fileInput
=
setFileInput
();
createDataSource
(
config
,
displayName
,
fileInput
);
}
}
PHP
< ?php
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require_once __DIR__ . '/../../../vendor/autoload.php';
require_once __DIR__ . '/../../Authentication/Authentication.php';
require_once __DIR__ . '/../../Authentication/Config.php';
use Google\ApiCore\ApiException;
use Google\Shopping\Merchant\DataSources\V1\Client\DataSourcesServiceClient;
use Google\Shopping\Merchant\DataSources\V1\CreateDataSourceRequest;
use Google\Shopping\Merchant\DataSources\V1\DataSource;
use Google\Shopping\Merchant\DataSources\V1\FileInput;
use Google\Shopping\Merchant\DataSources\V1\FileInput\FetchSettings;
use Google\Shopping\Merchant\DataSources\V1\PrimaryProductDataSource;
use Google\Type\TimeOfDay;
/**
* This class demonstrates how to insert a primary product datasource with a
* file input that is fetched from a URL.
*/
class CreateFileFetchPrimaryProductDataSourceSample
{
private static function getFileInput(): FileInput
{
// If FetchSettings were not set, then this would be an `UPLOAD` file
// type that you must manually upload via the Merchant Center UI.
$fetchSettings =
(new FetchSettings())
->setEnabled(true)
// Note that the system only respects hours for the fetch schedule.
->setTimeOfDay((new TimeOfDay())->setHours(22))
->setTimeZone('Europe/London')
->setFrequency(FetchSettings\Frequency::FREQUENCY_DAILY)
->setFetchUri('https://example.file.com/products');
// Creates the file input with the fetch settings
return (new FileInput())->setFetchSettings($fetchSettings);
}
/**
* Creates a new data source
*
* @param string $merchantId The Merchant Center Account ID
* @param string $displayName The displayed data source name in the Merchant Center UI
* @param FileInput $fileInput The file input data that this datasource will receive
* @return string The name of the newly created data source
*/
public static function createDataSource(string $merchantId, string $displayName, FileInput $fileInput): string
{
// Gets the OAuth credentials to make the request.
$credentials = Authentication::useServiceAccountOrTokenFile();
// Creates options config containing credentials for the client to use.
$options = ['credentials' => $credentials];
// Creates a client.
$dataSourcesServiceClient = new DataSourcesServiceClient($options);
// The account to create the data source for
$parent = sprintf('accounts/%s', $merchantId);
// The type of data that this datasource will receive.
$primaryProductDataSource =
(new PrimaryProductDataSource())
->setCountries(['GB'])
// `contentLanguage` and `feedLabel` must be set.
->setContentLanguage('en')
->setFeedLabel('GB');
// Creates the data source
try {
// Creates the request
$request =
(new CreateDataSourceRequest())
->setParent($parent)
->setDataSource(
(new DataSource())
->setDisplayName($displayName)
->setPrimaryProductDataSource($primaryProductDataSource)
->setFileInput($fileInput)
);
print('Sending Create File Fetch PrimaryProduct DataSource request' . PHP_EOL);
// Makes the request
$response = $dataSourcesServiceClient->createDataSource($request);
print('Created DataSource Name below' . PHP_EOL);
print($response->getName() . PHP_EOL);
return $response->getName();
} catch (ApiException $ex) {
printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
exit(1);
}
}
public static function callSample(): void
{
$config = Config::generateConfig();
// The Merchant Center Account ID.
$merchantId = $config['accountId'];
// The displayed datasource name in the Merchant Center UI.
$displayName = 'British File Fetch Primary Product Data';
// The file input data that this datasource will receive.
$fileInput = self::getFileInput();
self::createDataSource($merchantId, $displayName, $fileInput);
}
}
$sample = new CreateFileFetchPrimaryProductDataSourceSample();
$sample->callSample();

