How to use the bigcommerce.connection.Connection function in bigcommerce

To help you get started, we’ve selected a few bigcommerce examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bigcommerce / bigcommerce-api-python / tests / test_connection.py View on Github external
def test_create(self):
        connection = Connection(host='store.mybigcommerce.com', auth=('user', 'abcdef'))
        self.assertTupleEqual(connection._session.auth, ('user', 'abcdef'))
github bigcommerce / bigcommerce-api-python / tests / test_connection.py View on Github external
def test_full_path(self):
        connection = Connection(host='store.mybigcommerce.com', auth=('user', 'abcdef'))
        self.assertEqual(connection.full_path('time'), 'https://store.mybigcommerce.com/api/v2/time')
github bigcommerce / bigcommerce-api-python / tests / test_api.py View on Github external
def test_create_basic(self):
        api = bigcommerce.api.BigcommerceApi(host='store.mybigcommerce.com', basic_auth=('admin', 'abcdef'))
        self.assertIsInstance(api.connection, Connection)
        self.assertNotIsInstance(api.connection, OAuthConnection)
github bigcommerce / bigcommerce-api-python / tests / test_connection.py View on Github external
def test_handle_response(self):
        connection = Connection('store.mybigcommerce.com', ('user', 'abcdef'))
        # A normal, 200-ok response
        data = {
            'name': 'Shirt'
        }
        res = MagicMock()
        res.headers = {'Content-Type': 'application/json'}
        res.status_code = 200
        res.content = json.dumps(data)
        res.json.return_value = data
        self.assertEqual(connection._handle_response('products/1', res), data)

        res.status_code = 500
        self.assertRaisesHttpException(ServerException,
                                       lambda: connection._handle_response('products/1', res),
                                       # Test all of the properties of a HttpException
                                       500,
github bigcommerce / bigcommerce-api-python / tests / test_connection.py View on Github external
def test_run_method(self):
        connection = Connection(host='store.mybigcommerce.com', auth=('user', 'abcdef'))
        connection._session.request = MagicMock()

        # Call with nothing
        connection._run_method('GET', '')
        connection._session.request.assert_called_once_with('GET', 'https://store.mybigcommerce.com/api/v2/',
                                                            data=None, timeout=7.0, headers={})
        connection._session.request.reset_mock()

        # A simple request
        connection._run_method('GET', 'time')
        connection._session.request.assert_called_once_with('GET', 'https://store.mybigcommerce.com/api/v2/time',
                                                            data=None, timeout=7.0, headers={})
        connection._session.request.reset_mock()

        # A request with data
        data = {
github bigcommerce / bigcommerce-api-python / bigcommerce / connection.py View on Github external
def _handle_response(self, url, res, suppress_empty=True):
        """
        Adds rate limiting information on to the response object
        """
        result = Connection._handle_response(self, url, res, suppress_empty)
        if 'X-Rate-Limit-Time-Reset-Ms' in res.headers:
            self.rate_limit = dict(ms_until_reset=int(res.headers['X-Rate-Limit-Time-Reset-Ms']),
                                   window_size_ms=int(res.headers['X-Rate-Limit-Time-Window-Ms']),
                                   requests_remaining=int(res.headers['X-Rate-Limit-Requests-Left']),
                                   requests_quota=int(res.headers['X-Rate-Limit-Requests-Quota']))
            if self.rate_limiting_management:
                if self.rate_limiting_management['min_requests_remaining'] >= self.rate_limit['requests_remaining']:
                    if self.rate_limiting_management['wait']:
                        sleep(ceil(float(self.rate_limit['ms_until_reset']) / 1000))
                    if self.rate_limiting_management.get('callback_function'):
                        callback = self.rate_limiting_management['callback_function']
                        args_dict = self.rate_limiting_management.get('callback_args')
                        if args_dict:
                            callback(args_dict)
                        else:
                            callback()
github bigcommerce / bigcommerce-api-python / bigcommerce / connection.py View on Github external
raise EmptyResponseWarning("%d %s @ %s: %s" % (res.status_code, res.reason, url, res.content), res)
        elif res.status_code >= 500:
            raise ServerException("%d %s @ %s: %s" % (res.status_code, res.reason, url, res.content), res)
        elif res.status_code == 429:
            raise RateLimitingException("%d %s @ %s: %s" % (res.status_code, res.reason, url, res.content), res)
        elif res.status_code >= 400:
            raise ClientRequestException("%d %s @ %s: %s" % (res.status_code, res.reason, url, res.content), res)
        elif res.status_code >= 300:
            raise RedirectionException("%d %s @ %s: %s" % (res.status_code, res.reason, url, res.content), res)
        return result

    def __repr__(self):
        return "%s %s%s" % (self.__class__.__name__, self.host, self.api_path)


class OAuthConnection(Connection):
    """
    Class for making OAuth requests on the Bigcommerce v2 API

    Providing a value for access_token allows immediate access to resources within registered scope.
    Otherwise, you may use fetch_token with the code, context, and scope passed to your application's callback url
    to retrieve an access token.

    The verify_payload method is also provided for authenticating signed payloads passed to an application's load url.
    """

    def __init__(self, client_id, store_hash, access_token=None, host='api.bigcommerce.com',
                 api_path='/stores/{}/v2/{}', rate_limiting_management=None):
        self.client_id = client_id
        self.store_hash = store_hash
        self.host = host
        self.api_path = api_path
github bigcommerce / bigcommerce-api-python / bigcommerce / api.py View on Github external
def __init__(self, host=None, basic_auth=None,
                 client_id=None, store_hash=None, access_token=None, rate_limiting_management=None):
        self.api_service = os.getenv('BC_API_ENDPOINT', 'api.bigcommerce.com')
        self.auth_service = os.getenv('BC_AUTH_SERVICE', 'login.bigcommerce.com')

        if host and basic_auth:
            self.connection = connection.Connection(host, basic_auth)
        elif client_id and store_hash:
            self.connection = connection.OAuthConnection(client_id, store_hash, access_token, self.api_service,
                                                         rate_limiting_management=rate_limiting_management)
        else:
            raise Exception("Must provide either (client_id and store_hash) or (host and basic_auth)")