def main(argv): # Authenticate and construct service. service, config, _ = common.init(argv, __doc__) merchant_id = config['merchantId'] offer_id = 'book#%s' % common.get_unique_id() product = sample.create_product_sample(config, offer_id) # Add product. request = service.products().insert(merchantId=merchant_id, body=product) result = request.execute() print('Product with offerId "%s" and title "%s" was created.' % (result['offerId'], result['title']))
def main(argv): # Authenticate and construct service. service, config, _ = common.init(argv, __doc__) merchant_id = config['merchantId'] name = 'feed%s' % common.get_unique_id() datafeed = sample.create_datafeed_sample(config, name) # Add datafeed. request = service.datafeeds().insert(merchantId=merchant_id, body=datafeed) result = request.execute() print('Datafeed with name "%s" and ID %s was created.' % (result['name'], result['id']))
def main(argv): # Authenticate and construct service. service, config, flags = common.init(argv, __doc__, parents=[argparser]) merchant_id = config['merchantId'] account_id = flags.account_id common.check_mca(config, True) new_name = 'updated-account%s' % common.get_unique_id() request = service.accounts().patch(merchantId=merchant_id, accountId=account_id, body={'name': new_name}) result = request.execute() print('Account with id %s was updated with new name "%s".' % (account_id, result['name']))
def non_mca_workflow(service, config, page_size=50): """Performs the methods that can be used on non-MCA accounts. Args: service: The service object used to access the Content API. config: The samples configuration as a Python dictionary. page_size: The page size to use for calls to list methods. """ # Just used to shorten later calls to the Datafeeds service df = service.datafeeds() merchant_id = config['merchantId'] count = 0 print('Printing settings of all datafeeds:') request = df.list(merchantId=merchant_id, maxResults=page_size) while request is not None: result = request.execute() feeds = result.get('resources') if not feeds: print('No feeds found.') break count += len(feeds) for feed in feeds: print_datafeed(feed) request = df.list_next(request, result) print('Status for %d accounts printed.\n' % count) name = 'feed%s' % common.get_unique_id() datafeed = sample.create_datafeed_sample(config, name) # Add datafeed. print('Inserting feed "%s":' % name, end='') new_feed = df.insert(merchantId=merchant_id, body=datafeed).execute() print('done.\n') feed_id = int(new_feed['id']) print('Retrieving (with retries) new datafeed (ID %d).' % feed_id) req = df.get(merchantId=merchant_id, datafeedId=feed_id) feed = common.retry_request(req) print('Feed settings:') print_datafeed(feed) print() # Delete datafeed. print('Deleting feed "%s"...' % name, end='') df.delete(merchantId=merchant_id, datafeedId=feed_id).execute() print('done.\n')
def non_mca_workflow(service, config, page_size=50): """Performs the methods that can be used on non-MCA accounts. Args: service: The service object used to access the Content API. config: The samples configuration as a Python dictionary. page_size: The page size to use for calls to list methods. """ # Just used to shorten later calls to the Products service pr = service.products() merchant_id = config['merchantId'] count = 0 print('Printing status of all products:') request = pr.list(merchantId=merchant_id, maxResults=page_size) while request is not None: result = request.execute() products = result.get('resources') if not products: print('No products returned.') break count += len(products) for product in products: print_product(product) request = pr.list_next(request, result) print('Status for %d products printed.\n' % count) offer_id = 'book#%s' % common.get_unique_id() product = sample.create_product_sample(config, offer_id) # Add product. print('Inserting product "%s":' % offer_id, end='') new_product = pr.insert(merchantId=merchant_id, body=product).execute() print('done.\n') product_id = new_product['id'] print('Retrieving new product (ID "%s").' % product_id) info = pr.get(merchantId=merchant_id, productId=product_id).execute() print('Product information:') print_product(info) print() # Delete product. print('Deleting product "%s"...' % offer_id, end='') pr.delete(merchantId=merchant_id, productId=product_id).execute() print('done.\n')
def main(argv): # Authenticate and construct service. service, config, _ = common.init(argv, __doc__) merchant_id = config['merchantId'] common.check_mca(config, True) name = 'account%s' % common.get_unique_id() account = {'name': name, 'websiteUrl': 'https://%s.example.com/' % name} # Add account. request = service.accounts().insert(merchantId=merchant_id, body=account) result = request.execute() print('Created sub-account ID %s for MCA %d.' % (result['id'], merchant_id))
def mca_workflow(service, config, page_size=50): """Performs MCA-only methods from the Accounts service. Args: service: The service object used to access the Content API. config: The samples configuration as a Python dictionary. page_size: The page size to use for calls to list methods. """ acc = service.accounts() merchant_id = config['merchantId'] count = 0 print('Printing all sub-accounts:') request = acc.list(merchantId=merchant_id, maxResults=page_size) while request is not None: result = request.execute() accounts = result.get('resources') if not accounts: print('No subaccounts found.') break count += len(accounts) for account in accounts: print_account(account) request = acc.list_next(request, result) print('%d accounts printed.' % count) name = 'account%s' % common.get_unique_id() account = {'name': name, 'websiteUrl': 'https://%s.example.com/' % name} print('Adding account %s... ' % name, end='') account = acc.insert(merchantId=merchant_id, body=account).execute() print('done.') account_id = int(account['id']) print('Retrieving (with retries) new account (ID %d).' % account_id) req = acc.get(merchantId=merchant_id, accountId=account_id) account = common.retry_request(req) print('Removing new account (ID %d)... ' % account_id, end='') acc.delete(merchantId=merchant_id, accountId=account_id).execute() print('done.')
def main(argv): # Authenticate and construct service. service, config, _ = common.init(argv, __doc__) merchant_id = config['merchantId'] batch = { 'entries': [{ 'batchId': i, 'merchantId': merchant_id, 'method': 'insert', 'datafeed': sample.create_datafeed_sample(config, 'feed%s' % common.get_unique_id()), } for i in range(BATCH_SIZE)], } request = service.datafeeds().custombatch(body=batch) result = request.execute() if result['kind'] == 'content#datafeedsCustomBatchResponse': entries = result['entries'] for entry in entries: datafeed = entry.get('datafeed') errors = entry.get('errors') if datafeed: print('Datafeed %s with name "%s" created.' % (datafeed['id'], datafeed['name'])) elif errors: print('Errors for batch entry %d:' % entry['batchId']) print( json.dumps(errors, sort_keys=True, indent=2, separators=(',', ': '))) else: print('There was an error. Response: %s' % result)
def main(argv): # Authenticate and construct service. service, config, _ = common.init(argv, __doc__) merchant_id = config['merchantId'] common.check_mca(config, True) account_names = [ 'account%s' % common.get_unique_id() for i in range(BATCH_SIZE) ] batch = { 'entries': [{ 'batchId': i, 'merchantId': merchant_id, 'method': 'insert', 'account': { 'name': v, 'websiteUrl': 'https://%s.example.com/' % v, }, } for i, v in enumerate(account_names)], } request = service.accounts().custombatch(body=batch) result = request.execute() if result['kind'] == 'content#accountsCustomBatchResponse': for entry in result['entries']: account = entry.get('account') errors = entry.get('errors') if account: print('Account %s with name "%s" was created.' % (account['id'], account['name'])) elif errors: print('Errors for batch entry %d:' % entry['batchId']) print(json.dumps(errors, sort_keys=True, indent=2, separators=(',', ': '))) else: print('There was an error. Response: %s' % result)
def do(list_offers): # Authenticate and construct service. service, config, _ = common.init(sys.argv, __doc__) merchant_id = config['merchantId'] batch_size = len(list_offers) batch = { 'entries': [{ 'batchId': i, 'merchantId': merchant_id, 'method': 'insert', 'product': sample.create_product_sample( config, common.get_unique_id(), list_offers[i]), } for i in range(batch_size)], } # print(batch['entries'][0]) request = service.products().custombatch(body=batch) result = request.execute() if result['kind'] == 'content#productsCustomBatchResponse': entries = result['entries'] for entry in entries: product = entry.get('product') errors = entry.get('errors') if product: print('Product "%s" with offerId "%s" and title "%s" was created.' % (product['id'], product['offerId'], product['title'])) elif errors: print('Errors for batch entry %d:' % entry['batchId']) print(json.dumps(errors, sort_keys=True, indent=2, separators=(',', ': '))) else: print('There was an error. Response: %s' % result)