def _call(self, method, arguments): try: custom_url = self.magento.use_custom_api_path with magentolib.API(self.magento.location, self.magento.username, self.magento.password, full_url=custom_url) as api: result = api.call(method, arguments) # Uncomment to record requests/responses in ``recorder`` # record(method, arguments, result) _logger.debug("api.call(%s, %s) returned %s", method, arguments, result) return result except (socket.gaierror, socket.error, socket.timeout) as err: raise NetworkRetryableError( 'A network error caused the failure of the job: ' '%s' % err) except xmlrpclib.ProtocolError as err: if err.errcode in [ 502, # Bad gateway 503, # Service unavailable 504 ]: # Gateway timeout raise RetryableJobError( 'A protocol error caused the failure of the job:\n' 'URL: %s\n' 'HTTP/HTTPS headers: %s\n' 'Error code: %d\n' 'Error message: %s\n' % (err.url, err.headers, err.errcode, err.errmsg)) else: raise
def api(self): if self._api is None: custom_url = self._location.use_custom_api_path api = magentolib.API(self._location.location, self._location.username, self._location.password, full_url=custom_url) api.__enter__() self._api = api return self._api
def read(self, id, attributes=None): """ Returns the information of a record :rtype: dict """ if not attributes: attributes = [] attributes.append('price') storeview_id = self.session.context.get('magento_store_view_id') with magentolib.API(self.magento.location, self.magento.username, self.magento.password) as api: return api.call('%s.info' % self._magento_model, [int(id), storeview_id, attributes, 'id'])
def test_magento_connection(self): """ Test magento connection and display appropriate message to user :param channels: Active record list of magento channels """ # Make sure channel belongs to magento self.validate_magento_channel() try: with magento.API(self.magento_url, self.magento_api_user, self.magento_api_key): return except (xmlrpclib.Fault, IOError, xmlrpclib.ProtocolError, socket.timeout): self.raise_user_error("connection_error")
def _call(self, method, arguments): try: custom_url = self.magento.use_custom_api_path _logger.debug("Start calling Magento api %s", method) with magentolib.API(self.magento.location, self.magento.username, self.magento.password, full_url=custom_url) as api: # When Magento is installed on PHP 5.4+, the API # may return garble data if the arguments contain # trailing None. if isinstance(arguments, list): while arguments and arguments[-1] is None: arguments.pop() start = datetime.now() try: result = api.call(method, arguments) except: _logger.error("api.call(%s, %s) failed", method, arguments) raise else: _logger.debug("api.call(%s, %s) returned %s in %s seconds", method, arguments, result, (datetime.now() - start).seconds) # Uncomment to record requests/responses in ``recorder`` # record(method, arguments, result) return result except (socket.gaierror, socket.error, socket.timeout) as err: raise NetworkRetryableError( 'A network error caused the failure of the job: ' '%s' % err) except xmlrpclib.ProtocolError as err: if err.errcode in [ 502, # Bad gateway 503, # Service unavailable 504 ]: # Gateway timeout raise RetryableJobError( 'A protocol error caused the failure of the job:\n' 'URL: %s\n' 'HTTP/HTTPS headers: %s\n' 'Error code: %d\n' 'Error message: %s\n' % (err.url, err.headers, err.errcode, err.errmsg)) else: raise
def test_connection(cls, instances): """ Test magento connection and display appropriate message to user :param instances: Active record list of magento instance """ if len(instances) != 1: cls.raise_user_error('multiple_instances') instance = instances[0] try: with magento.API(instance.url, instance.api_user, instance.api_key): return except (xmlrpclib.Fault, IOError, xmlrpclib.ProtocolError, socket.timeout): cls.raise_user_error("connection_error")
def api(self): if self._api is None: if self._location.version == '1.7': api = magentolib.API( self._location.location, self._location.username, self._location.password, full_url=self._location.use_custom_api_path ) api.__enter__() else: api = Magento2Client( self._location.location, self._location.token, self._location.verify_ssl, use_custom_api_path=self._location.use_custom_api_path ) self._api = api return self._api
def test_connection(self, cursor, user, context): """Test the connection to magento instance(s) :param cursor: Database cursor :param user: ID of current user :param context: Application context """ Pool = self.pool instance_obj = Pool.get('magento.instance') instance = instance_obj.browse(cursor, user, context.get('active_id'), context) try: with magento.API(instance.url, instance.api_user, instance.api_key): return except (xmlrpclib.Fault, IOError, xmlrpclib.ProtocolError, socket.timeout): raise osv.except_osv( _('Incorrect API Settings!'), _('Please check and correct the API settings on instance.'))
def search(self, filters=None, from_date=None, magento_storeview_ids=None): """ Search records according to some criterias and returns a list of ids :rtype: list """ if filters is None: filters = {} if from_date: filters['updated_at'] = { 'from': from_date.strftime('%Y/%m/%d %H:%M:%S') } #TODO Check if this can be used as checkpoint with magentolib.API(self.magento.location, self.magento.username, self.magento.password) as api: #Get list of magento product id params = filters and [filters] or [[]] params.extend(magento_storeview_ids) return [ int(row['product_id']) for row in api.call('%s.list' % 'catalog_product', params) ] return []