Get a data source

Merchant API code sample to get a 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.DataSource 
 ; 
 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.GetDataSourceRequest 
 ; 
 import 
  
 shopping.merchant.samples.utils.Authenticator 
 ; 
 import 
  
 shopping.merchant.samples.utils.Config 
 ; 
 /** This class demonstrates how to get a specific datasource for a given Merchant Center account. */ 
 public 
  
 class 
 GetDataSourceSample 
  
 { 
  
 public 
  
 static 
  
 DataSource 
  
 getDataSource 
 ( 
 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} 
  
 GetDataSourceRequest 
  
 request 
  
 = 
  
 GetDataSourceRequest 
 . 
 newBuilder 
 (). 
 setName 
 ( 
 name 
 ). 
 build 
 (); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Sending GET DataSource request:" 
 ); 
  
 DataSource 
  
 response 
  
 = 
  
 dataSourcesServiceClient 
 . 
 getDataSource 
 ( 
 request 
 ); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Retrieved DataSource below" 
 ); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 response 
 ); 
  
 return 
  
 response 
 ; 
  
 } 
  
 catch 
  
 ( 
 Exception 
  
 e 
 ) 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 e 
 ); 
  
 System 
 . 
 exit 
 ( 
 1 
 ); 
  
 return 
  
 null 
 ; 
  
 // Necessary to satisfy the compiler as we're not returning a 
  
 // DataSource on failure. 
  
 } 
  
 } 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 throws 
  
 Exception 
  
 { 
  
 Config 
  
 config 
  
 = 
  
 Config 
 . 
 load 
 (); 
  
 // An ID assigned to a datasource by Google. 
  
 String 
  
 datasourceId 
  
 = 
  
 "1111111111" 
 ; 
  
 // Replace with your datasource ID. 
  
 getDataSource 
 ( 
 config 
 , 
  
 datasourceId 
 ); 
  
 } 
 } 
  
 

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\DataSource; 
 use Google\Shopping\Merchant\DataSources\V1\GetDataSourceRequest; 
 /** 
 * Class to demonstrate getting a specific datasource for a given Merchant 
 * Center account. 
 */ 
 class GetDataSourceSample 
 { 
 // ENSURE you fill in the datasource ID for the sample to work. 
 private const DATASOURCE_ID = 'INSERT_DATASOURCE_ID'; 
 /** 
 * Gets a DataSource. 
 * 
 * @param int $merchantId The Merchant Center Account ID. 
 * @param string $dataSourceId The data source ID. 
 * @return DataSource The retrieved data source. 
 */ 
 public function getDataSource(int $merchantId, string $dataSourceId): DataSource 
 { 
 // 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); 
 // Creates the data source name. 
 $name = sprintf('accounts/%s/dataSources/%s', $merchantId, $dataSourceId); 
 // Creates the request. 
 $request = (new GetDataSourceRequest()) 
 ->setName($name); 
 print('Sending GET DataSource request:' . PHP_EOL); 
 // Calls the API and catches and prints any network failures/errors. 
 try { 
 $response = $dataSourcesServiceClient->getDataSource($request); 
 print('Retrieved DataSource below' . PHP_EOL); 
 print($response->serializeToJsonString() . PHP_EOL); 
 return $response; 
 } catch (ApiException $ex) { 
 print('Call failed with message: ' . $ex->getMessage() . PHP_EOL); 
 return new DataSource(); 
 } 
 } 
 // Helper to execute the sample. 
 public function callSample(): void 
 { 
 $config = Config::generateConfig(); 
 // The Merchant Center Account ID. 
 $merchantId = $config['accountId']; 
 self::getDataSource($merchantId, self::DATASOURCE_ID); 
 } 
 } 
 $sample = new GetDataSourceSample(); 
 $sample->callSample();  
 
 

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. 
 """A module to get a DataSource.""" 
 from 
  
 examples.authentication 
  
 import 
 configuration 
 from 
  
 examples.authentication 
  
 import 
 generate_user_credentials 
 from 
  
 google.shopping 
  
 import 
 merchant_datasources_v1 
 # ENSURE you fill in the datasource ID for the sample to 
 # work. 
 _ACCOUNT 
 = 
 configuration 
 . 
 Configuration 
 () 
 . 
 read_merchant_info 
 () 
 _DATASOURCE 
 = 
 "[INSERT_DATASOURCE_HERE]" 
 _NAME 
 = 
 f 
 "accounts/ 
 { 
 _ACCOUNT 
 } 
 /dataSources/ 
 { 
 _DATASOURCE 
 } 
 " 
 def 
  
 get_data_source 
 (): 
  
 """Gets the specified `DataSource` resource.""" 
 # Gets OAuth Credentials. 
 credentials 
 = 
 generate_user_credentials 
 . 
 main 
 () 
 # Creates a client. 
 client 
 = 
 merchant_datasources_v1 
 . 
 DataSourcesServiceClient 
 ( 
 credentials 
 = 
 credentials 
 ) 
 # Creates the request. 
 request 
 = 
 merchant_datasources_v1 
 . 
 GetDataSourceRequest 
 ( 
 name 
 = 
 _NAME 
 ) 
 # Makes the request and catch and print any error messages. 
 try 
 : 
 response 
 = 
 client 
 . 
 get_data_source 
 ( 
 request 
 = 
 request 
 ) 
 print 
 ( 
 f 
 "Get successful: 
 { 
 response 
 } 
 " 
 ) 
 except 
 RuntimeError 
 as 
 e 
 : 
 print 
 ( 
 "Get failed" 
 ) 
 print 
 ( 
 e 
 ) 
 if 
 __name__ 
 == 
 "__main__" 
 : 
 get_data_source 
 () 
  
 
Create a Mobile Website
View Site in Mobile | Classic
Share by: