def import_products(builton: Builton): with open('products_export.csv', newline='') as csv_product_file: csv_reader = csv.DictReader(csv_product_file, delimiter=CSV_DELIMITER) for shopify_product in csv_reader: cprint("============================================================") try: cprint("=========================== Mappable attributes ===========================") tags = shopify_product.get('Tags').split(',') tags.append(shopify_product.get('Type')) body = { 'active': parse_boolean_param(shopify_product.get('Published')), 'name': shopify_product.get('Title'), 'description': shopify_product.get('Body (HTML)'), 'price': float(shopify_product.get('Variant Price')), 'external_reference': shopify_product.get('Handle'), 'tags': tags, 'image_url': shopify_product.get('Image Src'), 'properties': { 'vendor': shopify_product.get('Vendor'), 'grams': shopify_product.get('Variant Grams'), 'weight_unit': shopify_product.get('Variant Weight Unit') } } cprint("Active: %s" % body.get('active'), "cyan") cprint("Name: %s" % body.get('name'), "cyan") cprint("Description: %s" % body.get('description'), "cyan") cprint("Price: %s" % body.get('price'), "cyan") cprint("Handle: %s" % body.get('external_reference'), "cyan") cprint("Tags: %s" % body.get('tags'), "cyan") cprint("Image Url: %s" % body.get('image_url'), "cyan") cprint("Vendor: %s" % body.get('properties').get('vendor'), "cyan") cprint("Grams: %s" % body.get('properties').get('grams'), "cyan") cprint("Weight Unit: %s" % body.get('properties').get('weight_unit'), "cyan") builton.product().create(body=body) cprint("================== SUCCESSFULLY IMPORTED ===================", "green") except Exception as error: cprint("========================== ERROR ===========================", "red") cprint("something wrong happened: %s" % error, "red") cprint("product csv: %s" % shopify_product, "yellow") cprint("============================================================") continue cprint("============================================================") cprint("======================= IMPORT DONE ========================") cprint("============================================================")
def test_init_sets_parameters(): k = Builton("api_key", "bearer_token", endpoint="endpoint") assert k.api_key == "api_key" assert k.bearer_token == "bearer_token" assert k.endpoint == "endpoint" assert isinstance(k.request, Request) assert k.request.endpoint == "endpoint"
def test_init_sets_parameters_default_environment(): k = Builton(api_key="api_key", bearer_token="bearer_token") assert k.api_key == "api_key" assert k.bearer_token == "bearer_token" assert k.endpoint == DEFAULT_ENDPOINT assert isinstance(k.request, Request) assert k.request.endpoint == DEFAULT_ENDPOINT
def main(api_key=None, bearer_token=None): try: api_key = api_key if api_key is not None else API_KEY bearer_token = bearer_token if bearer_token is not None else BEARER_TOKEN except NameError: cprint("Missing API_KEY and BEARER_TOKEN in settings.py", "red") return builton = Builton(api_key=api_key, bearer_token=bearer_token) cprint("BuiltOn SDK is ready!", "cyan") import_products(builton)
def test_methods_return_right_instances(): k = Builton("endpoint", "api_key", "bearer_token") assert isinstance(k.ai_model(), AIModel) assert isinstance(k.company(), Company) assert isinstance(k.event(), Event) assert isinstance(k.order(), Order) assert isinstance(k.payment(), Payment) assert isinstance(k.payment_method(), PaymentMethod) assert isinstance(k.plan(), Plan) assert isinstance(k.product(), Product) assert isinstance(k.resource(), Resource) assert isinstance(k.subscription(), Subscription) assert isinstance(k.user(), User) assert isinstance(k.webhook(), Webhook)
def test_bearer_is_added_if_missing_when_constructing_headers(): k = Builton("endpoint", "api_key", "foo") headers = k._construct_headers() assert headers['Authorization'].startswith("Bearer ")
def test_construct_headers_adds_api_key(): k = Builton("api_key", "bearer_token", endpoint="endpoint") headers = k._construct_headers() assert 'X-Builton-API-Key' in headers assert headers['X-Builton-API-Key'] == "api_key"
def test_refresh_bear_token_calls_construct_headers(mocker): k = Builton("api_key", "bearer_token", endpoint="endpoint") mocker.patch.object(Builton, '_construct_headers') k.refresh_bearer_token("new_barer_token") k._construct_headers.assert_called_once
def test_refresh_bearer_token(): k = Builton("api_key", "bearer_token", endpoint="endpoint") assert k.bearer_token == "bearer_token" k.refresh_bearer_token("new_bearer") assert k.bearer_token == "new_bearer"
def test_instantiating_calls_construct_headers(mocker): mocker.patch.object(Builton, '_construct_headers') k = Builton("endpoint", "api_key") k._construct_headers.assert_called_once
def test_instantiating_calls_validate_input(mocker): mocker.patch.object(Builton, 'validate_input') k = Builton("api_key", "bearer_token", "endpoint") k.validate_input.assert_called_once_with("endpoint", "api_key", "bearer_token")
def test_non_string_bearer_token_raises_exception(): with pytest.raises(ValueError): Builton.validate_input("endpoint", "api_key", 42)
def test_none_api_key_raises_exception(): with pytest.raises(ValueError): Builton.validate_input("endpoint", None, "bearer_token")
def builton(): return Builton(endpoint=TALKBACK_ENDPOINT, api_key=API_KEY, bearer_token=BEARER_TOKEN)
from random import randint from builton_sdk import Builton from config import * builton = Builton(endpoint=ENDPOINT, api_key=API_KEY, bearer_token=BEARER_TOKEN) company = builton.company().get() print(company.name) users = builton.user().get_all() print(users) user = users[0] user_id = user.id print(user) print(user_id) user = builton.user().get(user_id) print(user) print(user.first_name) user = user.update(first_name='test%s' % randint(1, 100)) print(user) print(user.first_name) product = builton.product().create(name="Nike Air", description="Just do it!", price=1000,
import logging from builton_sdk import Builton from config import * logging.basicConfig() requests_log = logging.getLogger("builton_sdk") requests_log.setLevel(logging.DEBUG) requests_log.propagate = True builton = Builton(endpoint=ENDPOINT, api_key=API_KEY, bearer_token=BEARER_TOKEN) print(builton.product().get_all({"size": 1}))