def delete_demo_by_guid(guid): """ Delete a demo from the ERP system by guid. :param guid: The demo's guid. """ # Create and format request to ERP url = '%s/api/v1/Demos/%s' % (get_service_url('lw-erp'), guid) headers = get_apic_credentials() try: response = requests.request("DELETE", url, headers=headers) except Exception as e: raise APIException('ERP threw error deleting demo', internal_details=str(e)) # Check for possible errors in response if response.status_code == 404: raise ResourceDoesNotExistException( 'Demo does not exist', internal_details=json.loads( response.text).get('error').get('message')) return
def delete_shipment(token, shipment_id): """ Delete a shipment from the ERP system. :param token: The ERP Loopback session token. :param shipment_id: The ID of the shipment to be deleted. """ # Create and format request to ERP url = '%s/api/v1/Shipments/%s' % (get_service_url('lw-erp'), str(shipment_id)) headers = {'cache-control': "no-cache", 'Authorization': token} headers.update(get_apic_credentials()) try: response = requests.request("DELETE", url, headers=headers) except Exception as e: raise APIException('ERP threw error deleting shipment', internal_details=str(e)) # Check for possible errors in response if response.status_code == 401: raise AuthenticationException( 'ERP access denied', internal_details=json.loads( response.text).get('error').get('message')) elif response.status_code == 404: raise ResourceDoesNotExistException( 'Shipment does not exist', internal_details=json.loads( response.text).get('error').get('message')) return
def get_products(token): """ Get a list of products from the ERP system. :param token: The ERP Loopback session token. :return: The list of existing products. """ # Create and format request to ERP url = '%s/api/v1/Products' % get_service_url('lw-erp') headers = {'cache-control': "no-cache", 'Authorization': token} headers.update(get_apic_credentials()) try: response = requests.request("GET", url, headers=headers) except Exception as e: raise APIException('ERP threw error retrieving products', internal_details=str(e)) # Check for possible errors in response if response.status_code == 401: raise AuthenticationException( 'ERP access denied', internal_details=json.loads( response.text).get('error').get('message')) return response.text
def get_demo_by_guid(guid): """ Retrieve a demo from the ERP system by guid. :param guid: The demo's guid. :return: An instance of the Demo. """ # Create and format request to ERP url = '%s/api/v1/Demos/findByGuid/%s' % (get_service_url('lw-erp'), guid) headers = {'cache-control': "no-cache"} headers.update(get_apic_credentials()) try: response = requests.request("GET", url, headers=headers) except Exception as e: raise APIException('ERP threw error retrieving demo', internal_details=str(e)) # Check for possible errors in response if response.status_code == 404: raise ResourceDoesNotExistException( 'Demo does not exist', internal_details=json.loads( response.text).get('error').get('message')) return response.text
def get_retailer(token, retailer_id): """ Get a retailer from the ERP system. :param token: The ERP Loopback session token. :param retailer_id: The ID of the retailer to be retrieved. :return: The retrieved retailer. """ # Create and format request to ERP url = '%s/api/v1/Retailers/%s' % (get_service_url('lw-erp'), str(retailer_id)) headers = {'cache-control': "no-cache", 'Authorization': token} headers.update(get_apic_credentials()) try: response = requests.request("GET", url, headers=headers) except Exception as e: raise APIException('ERP threw error retrieving retailer', internal_details=str(e)) # Check for possible errors in response if response.status_code == 401: raise AuthenticationException( 'ERP access denied', internal_details=json.loads( response.text).get('error').get('message')) elif response.status_code == 404: raise ResourceDoesNotExistException( 'Retailer does not exist', internal_details=json.loads( response.text).get('error').get('message')) return response.text
def create_user(guid, retailer_id): """ Create a new user in the ERP system. :param guid: The demo's guid :param retailer_id: Retailer the user will be associated with. :return: The created User model. """ # Create and format request to ERP url = '%s/api/v1/Demos/%s/createUser' % (get_service_url('lw-erp'), guid) headers = { 'content-type': "application/json", 'cache-control': "no-cache" } headers.update(get_apic_credentials()) payload = dict() payload['retailerId'] = int(retailer_id) payload_json = json.dumps(payload) try: response = requests.request("POST", url, data=payload_json, headers=headers) except Exception as e: raise APIException('ERP threw error creating new user for demo', internal_details=str(e)) # Check for possible errors in response if response.status_code == 404: raise ResourceDoesNotExistException('Demo or retailer does not exist', internal_details=json.loads(response.text).get('error').get('message')) return response.text
def logout(token): """ Log a user out of the system. :param token: The ERP Loopback session token """ # Create and format request to ERP url = '%s/api/v1/Users/logout' % get_service_url('lw-erp') headers = { 'content-type': "application/json", 'Authorization': token } headers.update(get_apic_credentials()) try: response = requests.request("POST", url, headers=headers) except Exception as e: raise APIException('ERP threw error creating new user for demo', internal_details=str(e)) # Check for possible errors in response if response.status_code == 500: raise ResourceDoesNotExistException('Session does not exist', internal_details=json.loads(response.text).get('error').get('message')) return
def get_shipments(token, retailer_id=None, dc_id=None, status=None): """ Get a list of shipments from the ERP system. :param token: The ERP Loopback session token. :param status: Status of the shipments to be retrieved. :param retailer_id: Retailer of the shipments to be retrieved. :param dc_id: Distribution center of the shipments to be retrieved. :return: The list of existing shipments. """ # Add filters if corresponding inputs are present status_query = "" if status is not None: status_query = add_query_filter(status_query, "where", "=", status, property_name="status") if retailer_id is not None: status_query = add_query_filter(status_query, "where", "=", retailer_id, property_name="toId") if dc_id is not None: status_query = add_query_filter(status_query, "where", "=", dc_id, property_name="fromId") # Create and format request to ERP url = '%s/api/v1/Shipments%s' % (get_service_url('lw-erp'), status_query) headers = {'cache-control': "no-cache", 'Authorization': token} headers.update(get_apic_credentials()) try: response = requests.request("GET", url, headers=headers) except Exception as e: raise APIException('ERP threw error retrieving shipments', internal_details=str(e)) # Check for possible errors in response if response.status_code == 401: raise AuthenticationException( 'ERP access denied', internal_details=json.loads( response.text).get('error').get('message')) return response.text
def create_shipment(token, shipment): """ Create a shipment in the ERP system. :param token: The ERP Loopback session token. :param shipment: The shipment object to be created. :return: The created shipment. """ # Create and format request to ERP url = '%s/api/v1/Shipments' % get_service_url('lw-erp') headers = { 'content-type': "application/json", 'cache-control': "no-cache", 'Authorization': token } headers.update(get_apic_credentials()) shipment_json = json.dumps(shipment) try: response = requests.request("POST", url, data=shipment_json, headers=headers) except Exception as e: raise APIException('ERP threw error creating shipment', internal_details=str(e)) # Check for possible errors in response if response.status_code == 400: raise ValidationException( 'Bad shipment data', internal_details=json.loads( response.text).get('error').get('message')) elif response.status_code == 401: raise AuthenticationException( 'ERP access denied', internal_details=json.loads( response.text).get('error').get('message')) elif response.status_code == 422: raise UnprocessableEntityException( 'Required data for shipment is either absent or invalid', internal_details=json.loads( response.text).get('error').get('message')) return response.text
def create_demo(): """ Create a new demo session in the ERP system. :return: The created Demo model. """ # Create and format request to ERP url = '%s/api/v1/Demos' % get_service_url('lw-erp') headers = {'content-type': "application/json", 'cache-control': "no-cache"} headers.update(get_apic_credentials()) try: response = requests.request("POST", url, headers=headers) except Exception as e: raise APIException('ERP threw error creating new Demo', internal_details=str(e)) return response.text
def get_shipment(token, shipment_id, include_items=None): """ Get a shipment from the ERP system. :param token: The ERP Loopback session token. :param shipment_id: The ID of the shipment to be retrieved. :param include_items: Indicates if items are to be returned with shipment. :return: The retrieved shipment. """ # Add filters if corresponding inputs are present status_query = "" if include_items != "0": status_query = add_query_filter(status_query, "include", "=", "items") # Create and format request to ERP url = '%s/api/v1/Shipments/%s%s' % (get_service_url('lw-erp'), str(shipment_id), status_query) headers = {'cache-control': "no-cache", 'Authorization': token} headers.update(get_apic_credentials()) try: response = requests.request("GET", url, headers=headers) except Exception as e: raise APIException('ERP threw error retrieving shipment', internal_details=str(e)) # Check for possible errors in response if response.status_code == 401: raise AuthenticationException( 'ERP access denied', internal_details=json.loads( response.text).get('error').get('message')) elif response.status_code == 404: raise ResourceDoesNotExistException( 'Shipment does not exist', internal_details=json.loads( response.text).get('error').get('message')) return response.text
def login(guid, user_id): """ Authenticate a user against the ERP system. :param guid: The demo guid being logged in for. :param user_id: The user_id for which to log in. :return: Auth data returned by ERP system """ # Create and format request to ERP url = '%s/api/v1/Demos/%s/loginAs' % (get_service_url('lw-erp'), guid) headers = { 'content-type': "application/json", 'cache-control': "no-cache" } headers.update(get_apic_credentials()) payload = dict() payload['userId'] = int(user_id) payload_json = json.dumps(payload) try: response = requests.request("POST", url, data=payload_json, headers=headers) except Exception as e: raise APIException('ERP threw error creating new user for demo', internal_details=str(e)) # Check for possible errors in response if response.status_code == 404: raise ResourceDoesNotExistException('Demo or user does not exist', internal_details=json.loads(response.text).get('error').get('message')) login_response = json.loads(response.text) return { 'loopback_token': login_response.get('token').get('id'), 'user': login_response.get('user'), 'guid': guid }