def test_local(): wcapi = API(url='http://localhost:18080/wptest/', consumer_key='ck_b13285e31ed659227f2fdc1db856d1d3d0b06c21', consumer_secret='cs_3038b86805335faa887c6605364bcd1df9e57254', api="wp-json", version="wc/v2") target_product_id = 22 # id=22 is variable # id=23 is variation of 22 print "\n\n*******\n* GET *\n*******\n\n" response = wcapi.get('products') response = wcapi.get('products/%s' % target_product_id) # response = wcapi.get('products?page=2') # response = wcapi.put('products/99', {'product':{'title':'Woo Single #2a'}} ) # response = wcapi.put('products/99?id=http%3A%2F%2Fprinter', # {'product':{'title':'Woo Single #2a'}} ) print_response(response) print "\n\n*******\n* PUT *\n*******\n\n" data = {'product': {'custom_meta': {'attribute_pa_color': 'grey'}}} response = wcapi.put('products/%s' % target_product_id, data) print_response(response)
def test_technotea(): wcapi = API(url='http://technotea.com.au/', consumer_key='ck_204e2f68c5ca4d3be966b7e5da7a48cac1a75104', consumer_secret='cs_bcb080a472a2bbded7dc421c63cbe031a91241f5', api="wp-json", version="wc/v2") target_product_id = 24009 print "\n\n*******\n* GET *\n*******\n\n" response = wcapi.get('products/%s?fields=meta' % target_product_id) response = wcapi.get('products/categories?filter[q]=solution') categories = response.json() print "categories: %s" % pformat([(category['id'], category['name']) for category in categories]) # print_response(response) print "\n\n*******\n* GET 2 *\n*******\n\n" response = wcapi.get('products/%s' % target_product_id) product_categories = response.json().get('categories', []) print "categories: %s" % pformat(product_categories) print "categories: %s" % pformat([(category['id'], category['name']) for category in categories]) print_response(response) print "\n\n*******\n* PUT *\n*******\n\n" data = {'product': {'custom_meta': {'wootan_danger': 'D'}}} response = wcapi.put('products/%s' % target_product_id, data) print_response(response) print "\n\n*******\n* PUT 2 *\n*******\n\n" data = {'product': {'categories': [898]}} response = wcapi.put('products/%s' % target_product_id, data) response = wcapi.get('products/%s?fields=categories' % target_product_id) product_categories = response.json().get('product', {}).get('categories', []) print "categories: %s" % pformat(product_categories)
# print_response(response) # quit() response = wcapi.get('products/17834') product_categories = response.json().get('categories', []) print "categories: %s" % pformat(product_categories) # print "categories: %s" % pformat([(category['id'], category['name']) for # category in categories]) print "\n\n*******\n* PUT *\n*******\n\n" # data = {'categories':[{'id':898}]} # works # doesn't work data = {'categories': [{'slug': 'specials-accessories-specials'}]} response = wcapi.put('products/17834', data) response = wcapi.get('products/17834?fields=categories') product_categories = response.json().get('categories', []) print "categories: %s" % pformat(product_categories) # # quit() # # wpapi = API( # url='http://localhost:8888/wordpress/', # consumer_key='LCLwTOfxoXGh', # consumer_secret='k7zLzO3mF75Xj65uThpAnNvQHpghp4X1h5N20O8hCbz2kfJq', # api="wp-json", # oauth1a_3leg=True,
# categories = response.json().get('product_categories') # print "categories: %s" % pformat([(category['id'], category['name']) for category in categories]) print_response(response) # response = wcapi.get('products/17834') # product_categories = response.json().get('product',{}).get('categories',[]) # print "categories: %s" % pformat(product_categories) # print "categories: %s" % pformat([(category['id'], category['name']) for category in categories]) print "\n\n*******\n* PUT *\n*******\n\n" ### Tests for woocommerce local staging data = {'product':{'custom_meta':{'attribute_pa_color':'grey'}}} response = wcapi.put('products/23', data) print_response(response) ### Tests for technotea # data = {'product':{'custom_meta':{'wootan_danger':'D'}}} # response = wcapi.put('products/21391', data) # print_response(response) print "\n\n*******\n* GET 2 *\n*******\n\n" ### Tests for woocommerce local staging response = wcapi.get('products/product/23') print_response(response)
def main(): wcapi = API(url='http://localhost:18080/wptest/', consumer_key='ck_b13285e31ed659227f2fdc1db856d1d3d0b06c21', consumer_secret='cs_3038b86805335faa887c6605364bcd1df9e57254', api="wc-api", version="v3") # wcapi = API( # url='http://technotea.com.au/', # consumer_key='ck_204e2f68c5ca4d3be966b7e5da7a48cac1a75104', # consumer_secret='cs_bcb080a472a2bbded7dc421c63cbe031a91241f5', # api="wc-api", # version="v3" # ) target_product_id = 22 # on woocommerce test data, # id=22 is variable # id=23 is variation of 22 print "\n\n*******\n* GET *\n*******\n\n" # tests for wordpress local test # response = wcapi.get('products') response = wcapi.get('products/%s' % target_product_id) # response = wcapi.get('products?page=2') # response = wcapi.put('products/99', {'product':{'title':'Woo Single #2a'}} ) # response = wcapi.put('products/99?id=http%3A%2F%2Fprinter', # {'product':{'title':'Woo Single #2a'}} ) # tests for technotea # response = wcapi.get('products/21391?fields=meta') # response = wcapi.get('products/categories?filter[q]=solution') # categories = response.json().get('product_categories') # print "categories: %s" % pformat([(category['id'], category['name']) for # category in categories]) print_response(response) # response = wcapi.get('products/17834') # product_categories = response.json().get('product',{}).get('categories',[]) # print "categories: %s" % pformat(product_categories) # print "categories: %s" % pformat([(category['id'], category['name']) for # category in categories]) print "\n\n*******\n* PUT *\n*******\n\n" # Tests for woocommerce local staging data = {'product': {'custom_meta': {'attribute_pa_color': 'grey'}}} response = wcapi.put('products/%s' % target_product_id, data) print_response(response) # Tests for technotea # data = {'product':{'custom_meta':{'wootan_danger':'D'}}} # response = wcapi.put('products/21391', data) # print_response(response) print "\n\n*******\n* GET 2 *\n*******\n\n" # Tests for woocommerce local staging response = wcapi.get('products/%s' % target_product_id) print_response(response)
categories = response.json() print "categories: %s" % pformat([(category['id'], category['name']) for category in categories]) # print_response(response) # quit() response = wcapi.get('products/17834') product_categories = response.json().get('categories',[]) print "categories: %s" % pformat(product_categories) # print "categories: %s" % pformat([(category['id'], category['name']) for category in categories]) print "\n\n*******\n* PUT *\n*******\n\n" # data = {'categories':[{'id':898}]} # works data = {'categories':[{'slug':'specials-accessories-specials'}]} # doesn't work response = wcapi.put('products/17834', data) response = wcapi.get('products/17834?fields=categories') product_categories = response.json().get('categories',[]) print "categories: %s" % pformat(product_categories) # # quit() # # wpapi = API( # url='http://localhost:8888/wordpress/', # consumer_key='LCLwTOfxoXGh', # consumer_secret='k7zLzO3mF75Xj65uThpAnNvQHpghp4X1h5N20O8hCbz2kfJq', # api="wp-json", # oauth1a_3leg=True,
class WpClient(ApiMixin): """Wraps around the wordpress API and provides extra useful methods""" kwarg_validations = { 'consumer_key': [ValidationUtils.not_none], 'consumer_secret': [ValidationUtils.not_none], 'url': [ValidationUtils.is_url], # 'wp_user':[ValidationUtils.not_none], # 'wp_pass':[ValidationUtils.not_none], # 'callback':[ValidationUtils.not_none], } page_nesting = True def __init__(self, *args, **kwargs): self.validate_kwargs(**kwargs) api_args = { 'version': 'v3', 'api': 'wc-api', 'basic_auth': True, # 'query_string_auth': False, 'query_string_auth': True, } for key in [ 'consumer_key', 'consumer_secret', 'url', 'version', 'api', 'basic_auth', 'query_string_auth' ]: if key in kwargs: api_args[key] = kwargs.get(key) self.api = WPAPI(**api_args) class WpApiPageIterator(WPAPI): """Creates an iterator based on a paginated wc api call""" def __init__(self, api, endpoint): self.api = api self.last_response = self.api.get(endpoint) def __iter__(self): return self def next(self): if int(self.last_response.status_code) not in [200]: raise UserWarning("request failed with %s: %s -> %s" % (self.last_response.status_code, repr(self.last_response.request.url), repr(self.last_response.content))) last_response_json = self.last_response.json() last_response_headers = self.last_response.headers # print "headers", last_response_headers links_str = last_response_headers.get('link', '') for link in SanitationUtils.findall_wc_links(links_str): if link.get('rel') == 'next' and link.get('url'): self.last_response = self.api.get(link['url']) return last_response_json raise StopIteration() def get_products(self, params=None): if params is None: params = {} request_params = OrderedDict() for key in ['per_page', 'search', 'slug', 'sku']: if params.get(key): request_params[key] = SanitationUtils.coerce_ascii(params[key]) endpoint = 'products' if params.get('id'): _id = params['id'] assert isinstance(_id, int), "id must be an int" endpoint += '/%d' % _id if request_params: endpoint += '?' + '&'.join( [key + '=' + val for key, val in request_params.items()]) print "endpoint is", endpoint products = [] if params.get('id'): response = self.api.get(endpoint) product = response.json().get('product') products.append(product) return products for page in self.WpApiPageIterator(self.api, endpoint): # print "page:", page if page.get('products'): for page_product in page.get('products'): # print "page_product: ", page_product product = {} for field in params.get('core_fields', []): if field in page_product: product[field] = page_product[field] if page_product.get('product_meta'): page_product_meta = page_product['product_meta'] for meta_field in params.get('meta_fields', []): if page_product_meta.get(meta_field): product[ 'meta.' + meta_field] = page_product_meta[meta_field] products.append(product) return products def update_product(self, _id, data): assert isinstance(_id, int), "id must be int" assert isinstance(data, dict), "data must be dict" return self.api.put("products/%d" % _id, data).json()