def getStatistic(self, user, shop): parameters = { 'Action': 'GetStatistics', 'Format': 'JSON', 'Timestamp': LazadaApiHelper.getCurrentUTCTime(), 'UserID': shop['email'], 'Version': '1.0' } parameters['Signature'] = LazadaApiHelper.generateSignature( parameters, shop['api_key']) url = "{}/?Action={}&Format={}&Timestamp={}&UserID={}&Version={}&Signature={}".format( LazadaAPI.ENDPOINT, parameters["Action"], parameters["Format"], LazadaApiHelper.formatTimestamp(parameters["Timestamp"]), parameters["UserID"], parameters["Version"], parameters["Signature"]) try: resp = requests.get(url) if resp.status_code == 200: response = json.loads(resp.text) if ('ErrorResponse' in response): errorMessage = ExceptionUtils.getBodyMessage(response) return None, '''User: {}-{}, Get-Statistic: {}'''.format( user['username'], user['id'], errorMessage) # Request API Success return response['SuccessResponse']['Body'], None # Request error return None, '''User: {}-{}, Get-Statistic: {}'''.format( user['username'], user['id'], resp.status_code) except Exception as ex: return None, '''User: {}-{}, Get-Statistic: {}'''.format( user['username'], user['id'], str(ex))
def setStatusToPackedByMarketplace(self, user, orderItems): parameters = { 'Action': 'SetStatusToPackedByMarketplace', 'DeliveryType': 'dropship', 'Format': 'json', 'OrderItemIds': '''[{}]'''.format( orderItems ), # orderItems format should be string like this: 3,425,234 'Timestamp': LazadaApiHelper.getCurrentUTCTime(), 'UserID': user['lazada_user_id'], 'Version': '1.0' } parameters['Signature'] = LazadaApiHelper.generateSignature( parameters, user['lazada_api_key']) url = "{}/?Action={}&DeliveryType={}&Format={}&OrderItemIds={}&&Timestamp={}&UserID={}&Version={}&Signature={}".format( LazadaAPI.ENDPOINT, parameters["Action"], parameters['DeliveryType'], parameters["Format"], parameters['OrderItemIds'], LazadaApiHelper.formatTimestamp(parameters["Timestamp"]), parameters["UserID"], parameters["Version"], parameters["Signature"]) try: resp = requests.get(url) if resp.status_code == 200: response = json.loads(resp.text) if 'ErrorResponse' in response: return ExceptionUtils.error( '''User: {}-{}, Set Status to Packed is error: {}'''. format( user['id'], user['username'], response['ErrorResponse']['Head']['ErrorMessage'])) return ExceptionUtils.success( "Set status to Parked is done") # Can't return None # Request except return ExceptionUtils.error( '''User: {}-{}, Set Status to Packed is error: {}'''.format( user['id'], user['username'], resp.status_code)) except Exception as ex: return ExceptionUtils.error( '''User: {}-{}, Set Status to Packed is error: {}'''.format( user['id'], user['username'], str(ex)))
def getOrdersByUpdatedAfter(self, user, offset, updatedAfter): parameters = { 'Action': 'GetOrders', 'Format': 'JSON', 'Timestamp': LazadaApiHelper.getCurrentUTCTime(), 'UserID': user['lazada_user_id'], 'Version': LazadaAPI.VERSION, 'Limit': LazadaAPI.LIMIT, 'Offset': offset, 'UpdatedAfter': LazadaApiHelper.formatToLazadaTimestamp(updatedAfter), 'SortBy': 'updated_at', 'SortDirection': 'ASC' } parameters['Signature'] = LazadaApiHelper.generateSignature( parameters, user['lazada_api_key']) url = "{}/?Action={}&Format={}&Timestamp={}&UserID={}&Version={}&Limit={}&Offset={}&UpdatedAfter={}&SortBy={}&SortDirection={}&Signature={}".format( LazadaAPI.ENDPOINT, parameters["Action"], parameters["Format"], LazadaApiHelper.formatTimestamp(parameters["Timestamp"]), parameters["UserID"], parameters["Version"], parameters["Limit"], parameters["Offset"], LazadaApiHelper.formatTimestamp(parameters["UpdatedAfter"]), parameters["SortBy"], parameters["SortDirection"], parameters["Signature"]) try: resp = requests.get(url) if resp.status_code == 200: response = json.loads(resp.text) # Request API error if ('ErrorResponse' in response): errorMessage = ExceptionUtils.getBodyMessage(response) return None, '''User: {}-{}, Get-Orders: {}'''.format( user['username'], user['id'], errorMessage) # Request API Success return response['SuccessResponse']['Body']['Orders'], None # Request error return None, '''User: {}-{}, Get-Orders: {}'''.format( user['username'], user['id'], resp.status_code) except Exception as ex: return None, '''User: {}-{}, Get-Orders: {}'''.format( user['username'], user['id'], str(ex))
def getSku(self, sku, user): parameters = { 'Action': 'GetProducts', 'Format': 'JSON', 'Timestamp': LazadaApiHelper.getCurrentUTCTime(), 'UserID': user['lazada_user_id'], 'Version': '1.0', 'SkuSellerList': '''["{}"]'''.format(sku['sku']) } parameters['Signature'] = LazadaApiHelper.generateSignature( parameters, user['lazada_api_key']) url = "{}/?Action={}&Format={}&Timestamp={}&UserID={}&Version={}&SkuSellerList={}&Signature={}".format( LazadaAPI.ENDPOINT, parameters["Action"], parameters["Format"], LazadaApiHelper.formatTimestamp(parameters["Timestamp"]), parameters["UserID"], parameters["Version"], parameters["SkuSellerList"], parameters["Signature"]) try: resp = requests.get(url) if resp.status_code == 200: response = json.loads(resp.text) if 'SuccessResponse' in response: data = response['SuccessResponse']['Body'] if data['TotalProducts'] == 1: return data['Products'][0] else: return ExceptionUtils.error( '''Invalide Seller SKU !''') else: return ExceptionUtils.returnError( '''Can't get this product with error: ''', response) else: return ExceptionUtils.error( '''Get product got error's response-code: {}'''.format( resp.status_code)) except Exception as ex: return ExceptionUtils.error( '''Get product got exception: {}'''.format(str(ex)))
def updateProductSpecialPrice(self, sku, user, newSpecialPrice): parameters = { 'Action': 'UpdateProduct', 'Format': 'JSON', 'Timestamp': LazadaApiHelper.getCurrentUTCTime(), 'UserID': user['lazada_user_id'], 'Version': '1.0' } parameters['Signature'] = LazadaApiHelper.generateSignature( parameters, user['lazada_api_key']) url = "{}/?Action={}&Format={}&Timestamp={}&UserID={}&Version={}&Signature={}".format( LazadaAPI.ENDPOINT, parameters["Action"], parameters["Format"], LazadaApiHelper.formatTimestamp(parameters["Timestamp"]), parameters["UserID"], parameters["Version"], parameters["Signature"]) # Must use xmlBody to update xmlBody = LazadaApiHelper.getUpdateProductSpecialPriceXML( sku, newSpecialPrice) try: resp = requests.post(url, data=xmlBody) if resp.status_code != 200: return ExceptionUtils.error( '''Request error with response-code: {}'''.format( resp.status_code)) response = resp.json() if ('SuccessResponse' in response): return ExceptionUtils.success( "Update procduct special price success") else: return ExceptionUtils.returnError( "Update product special price error: ", response) except Exception as ex: return ExceptionUtils.error( '''Update product special price got exception: {}'''.format( str(ex)))
def getProductByUpdatedAfter(self, user, offset, updatedAfter): parameters = { 'Action': 'GetProducts', 'Format': 'JSON', 'Timestamp': LazadaApiHelper.getCurrentUTCTime(), 'UserID': user['lazada_user_id'], 'Version': '1.0', 'UpdatedAfter': LazadaApiHelper.formatToLazadaTimestamp(updatedAfter), 'Filter': 'all', 'Limit': LazadaAPI.LIMIT, 'Offset': offset } parameters['Signature'] = LazadaApiHelper.generateSignature( parameters, user['lazada_api_key']) url = "{}/?Action={}&Format={}&Timestamp={}&UserID={}&Version={}&Signature={}&UpdatedAfter={}&Filter={}&Offset={}&Limit={}".format( LazadaAPI.ENDPOINT, parameters["Action"], parameters["Format"], LazadaApiHelper.formatTimestamp(parameters["Timestamp"]), parameters["UserID"], parameters["Version"], parameters["Signature"], LazadaApiHelper.formatTimestamp(parameters["UpdatedAfter"]), parameters["Filter"], parameters["Offset"], parameters["Limit"]) try: resp = requests.get(url) if resp.status_code == 200: response = json.loads(resp.text) if ('ErrorResponse' in response): return None, ExceptionUtils.returnError( '''Get Products is error: ''', response) data = response['SuccessResponse']['Body'] return data['Products'], None return None, '''Get Products is error: {}'''.format( resp.status_code) except Exception as ex: return None, '''Get Products is error: {}'''.format(str(ex))