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" was created.' % (result['offerId']))
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'] # List all products 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: # Get product. product_id = product['id'] print('Getting product (ID "%s").' % product_id) info = pr.get(merchantId=merchant_id, productId=product_id).execute() print_product(info) print() 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'] # Delete product. print('Deleting product "%s"...' % offer_id, end='') pr.delete(merchantId=merchant_id, productId=product_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'] batch = { 'entries': [{ 'batchId': i, 'merchantId': merchant_id, 'method': 'insert', 'product': sample.create_product_sample(config, 'book#%s' % common.get_unique_id(), title='This is book number %d' % i, price={ 'value': '%d.50' % i, 'currency': 'USD', }), } for i in range(BATCH_SIZE)], } 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)
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', 'product': sample.create_product_sample( config, 'book#%s' % common.get_unique_id(), title='This is book number %d' % i, price={ 'value': '%d.50' % i, 'currency': 'USD', }), } for i in range(BATCH_SIZE)], } 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)