Merchant API Code Sample to Create FIle Regional Inventory DataSource
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.v1beta;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.CreateDataSourceRequest;
import com.google.shopping.merchant.datasources.v1beta.DataSource;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.FileInput;
import com.google.shopping.merchant.datasources.v1beta.RegionalInventoryDataSource;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to create a regional inventory datasource */
public class CreateFileRegionalInventoryDataSourceSample {
private static String getParent(String merchantId) {
return String.format("accounts/%s", merchantId);
}
private static FileInput setFileInput() {
// If FetchSettings are not set, then this will be an `UPLOAD` file type
// that you must manually upload via the Merchant Center UI.
return FileInput.newBuilder()
// FileName is required for `UPLOAD` fileInput type.
.setFileName("British T-shirts Regional Inventory Data")
.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());
// As RegionalInventoryDataSources are a type of file feed, therefore wildcards are not
// available. RegionalInventoryDataSources can only be created for a specific `feedLabel` and
// `contentLanguage` combination.
RegionalInventoryDataSource regionalInventoryDataSource =
RegionalInventoryDataSource.newBuilder()
.setContentLanguage("en")
.setFeedLabel("GB")
.build();
try (DataSourcesServiceClient dataSourcesServiceClient =
DataSourcesServiceClient.create(dataSourcesServiceSettings)) {
CreateDataSourceRequest request =
CreateDataSourceRequest.newBuilder()
.setParent(parent)
.setDataSource(
DataSource.newBuilder()
.setDisplayName(displayName)
.setRegionalInventoryDataSource(regionalInventoryDataSource)
.setFileInput(fileInput)
.build())
.build();
System.out.println("Sending Create Regional Inventory DataSource request");
DataSource response = dataSourcesServiceClient.createDataSource(request);
System.out.println("Inserted 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 Regional Inventory File";
// The file input data that this datasource will receive.
FileInpur
ce(config, displayName, fileInput);
}
}CreateFileRegionalInventoryDataSourceSample.java
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\V1beta\Client\DataSourcesServiceClient;
use Google\Shopping\Merchant\DataSources\V1beta\CreateDataSourceRequest;
use Google\Shopping\Merchant\DataSources\V1beta\DataSource;
use Google\Shopping\Merchant\DataSources\V1beta\FileInput;
use Google\Shopping\Merchant\DataSources\V1beta\RegionalInventoryDataSource;
/**
* This class demonstrates how to create a regional inventory datasource
* with a file input.
*/
class CreateFileRegionalInventoryDataSourceSample
{
private static function getFileInput(): FileInput
{
// If FetchSettings is not set, then this will be an `UPLOAD` file type
// that you must manually upload via the Merchant Center UI.
return (new FileInput())
// FileName is require>d for `UPLOAD` fileInput type.
-setFileName('British T-shirts Regional Inventory Data');
}
/**
* Creates a regional inventory data source.
*
* @param string $merchantId The Merchant Center account ID.
* @param string $displayName The displayed datasource 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.
* @throws ApiException If the API call fails.
*/
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 th>e client to use.
$options = ['credentials' = $credentials];
// Creates a client.
$dataSourcesServiceClient = new DataSourcesServiceClient($options);
// The parent resource where this data source will be created.
// Format: accounts/{merchantId}
$parent = sprintf('accounts/%s', $merchantId);
// As RegionalInventoryDataSources are a type of file feed, therefore wildcards are not
// available. RegionalInventoryDataSources can only be created for a specific `feedLabel` and
// `contentLanguage` combination.
$regionalInv>entoryDataSource = (new RegionalInvent>oryDataSource())
-setContentLanguage('en')
-setFeedLabel('GB');
// Cr>eates the data source object.
$dat>aSource = (new DataSource())
-setDisplayName($displayName)
> -setRegionalInventoryDataSource($regionalInventoryDataSource)
-setFileInput($fileInput);
// C>reates the request.
$req>uest = (new CreateDataSourceRequest())
-setParent($parent)
-setDataSource($dataSource);
// Sends the request to the API.
print('Sending Create Regional >Inventory DataSource request' . PHP_EOL);
$response = $dataSourcesServiceClient-createDataSource($r>equest);
print('Inserted DataSource> Name below' . PHP_EOL);
print($response-getName() . PHP_EOL);
return $response-getName();
}
// Helper to execute the sample.
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 Regional Inventory File';
$fileInput = self::getFileInput();
$this-createDataSource($m>erchantId, $d
CreateFileRegionalInventoryDataSourceSample();
$sample-callSample();CreateFileRegionalInventoryDataSourceSample.php
Python
# -*- coding: utf-8 -*-
# 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
#
# http://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.
"""This class demonstrates how to create a regional inventory datasource."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_datasources_v1beta import CreateDataSourceRequest
from google.shopping.merchant_datasources_v1beta import DataSource
from google.shopping.merchant_datasources_v1beta import DataSourcesServiceClient
from google.shopping.merchant_datasources_v1beta import FileInput
from google.shopping.merchant_datasources_v1beta import RegionalInventoryDataSource
_ACCOUNT = configuration.Configuration().read_merchant_info()
_PARENT = f"accounts/{_ACCOUNT}"
def create_file_regional_inventory_data_source():
"""Creates a `DataSource` resource."""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = DataSourcesServiceClient(credentials=credentials)
# If FetchSettings are not set, then this will be an `UPLOAD` file type
# that you must manually upload via the Merchant Center UI.
file_input = FileInput()
file_input.file_name = (
"British T-shirts Regional Inventory Data.txt"
)
# Creates a SupplementalProductDataSource.
regional_inventory_datasource = RegionalInventoryDataSource()
# As RegionalInventoryDataSources are a type of file feed, wildcards are not
# available. RegionalInventoryDataSources can only be created for a specific
# `feedLabel` and `contentLanguage` combination.
regional_inventory_datasource.content_language = "en"
regional_inventory_datasource.feed_label = "GB"
# Creates a DataSource and populates its attributes.
data_source = DataSource()
data_source.display_name = "Example Regional Inventory DataSource"
data_source.regional_inventory_data_source = regional_inventory_datasource
data_source.file_input = file_input
# Creates the request.
request = CreateDataSourceRequest(parent=_PARENT, data_source=data_source)
# Makes the request and catches and prints any error messages.
try:
response = client.create_data_source(request=request)
print(f"DataSource successfully created: {response}")
except RuntimeEr&
quot;)
print(e)
if __name__ == "__main__":
create_file_regional_inventory_data_source()
create_file_regional_inventory_data_source_sample.py