Esempio n. 1
0
def get_retailer_inventory(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 for which inventory is to be be retrieved.

    :return:        The retrieved retailer's inventory.
    """

    # Create and format request to ERP
    url = '%s/api/v1/Retailers/%s/inventories' % (get_service_url('lw-erp'),
                                                  str(retailer_id))
    headers = {'cache-control': "no-cache", 'Authorization': token}

    try:
        response = requests.request("GET", url, headers=headers)
    except Exception as e:
        raise APIException('ERP threw error retrieving retailer inventory',
                           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
Esempio n. 2
0
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"
    }
    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')
    }
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
Esempio n. 4
0
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"
    }
    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
Esempio n. 5
0
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}

    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 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"}
    payload = dict()
    payload['retailerId'] = 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 update_shipment(token, shipment_id, shipment):
    """
    Update 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 shipment:    The shipment object with values to update.

    :return:         The updated shipment.
    """

    # 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
    }

    try:
        response = requests.request("PUT", url, data=shipment, headers=headers)
    except Exception as e:
        raise APIException('ERP threw error updating shipment', internal_details=str(e))

    # Check for possible errors in response
    if response.status_code == 400:
        raise ValidationException('Invalid update to shipment',
                                  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 == 404:
        raise ResourceDoesNotExistException('Shipment does not exist',
                                            internal_details=json.loads(response.text).get('error').get('message'))

    return response.text
def get_retailer_inventory(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 for which inventory is to be be retrieved.

    :return:        The retrieved retailer's inventory.
    """

    # Create and format request to ERP
    url = '%s/api/v1/Retailers/%s/inventories' % (get_service_url('lw-erp'), str(retailer_id))
    headers = {
        'cache-control': "no-cache",
        'Authorization': token
    }

    try:
        response = requests.request("GET", url, headers=headers)
    except Exception as e:
        raise APIException('ERP threw error retrieving retailer inventory', 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 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}

    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
Esempio n. 10
0
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
    }

    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
Esempio n. 11
0
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"
    }
    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
    }
Esempio n. 12
0
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
Esempio n. 13
0
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"}

    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
Esempio n. 14
0
def get_retailers(token):
    """
    Get a list of retailers from the ERP system.

    :param token:   The ERP Loopback session token.

    :return:        The list of existing retailers.
    """

    # Create and format request to ERP
    url = '%s/api/v1/Retailers' % get_service_url('lw-erp')
    headers = {
        'cache-control': "no-cache",
        'Authorization': token
    }

    try:
        response = requests.request("GET", url, headers=headers)
    except Exception as e:
        raise APIException('ERP threw error retrieving retailers', 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
Esempio n. 15
0
def get_distribution_center(token, dc_id):
    """
    Get a distribution center from the ERP system.

    :param token:   The ERP Loopback session token.
    :param dc_id:   The ID of the distribution center to be retrieved.

    :return:        The retrieved distribution center.
    """

    # Create and format request to ERP
    url = '%s/api/v1/DistributionCenters/%s' % (get_service_url('lw-erp'),
                                                str(dc_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 distribution center',
                           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(
            'Distribution center does not exist',
            internal_details=json.loads(
                response.text).get('error').get('message'))

    return response.text
Esempio n. 16
0
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
Esempio n. 17
0
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
Esempio n. 18
0
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"}

    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
Esempio n. 19
0
def create_demo(demo_name, user_email=None):
    """
    Create a new demo session in the ERP system.

    :param demo_name:   Name of the demo being created.
    :param user_email:  Email of the user creating the demo.

    :return:         The created Demo model.
    """

    # Check email
    if user_email is not None and validate_email(user_email) == False:
        raise UnprocessableEntityException("Invalid email address")

    # 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"}
    payload = dict()
    payload['name'] = demo_name
    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 Demo',
                           internal_details=str(e))

    # Commenting out synchronous email sending until one-off tasks are enabled
    # if user_email:
    #     demo = json.loads(response.text)
    #     subject = "Your Logistics Wizard session has been created - Demo #" + \
    #               demo.get('guid')[-6:].upper()
    #     message = messaging_service.compose_msg('welcome.html', (demo.get('guid'),
    #                                                              demo.get('users')[0].get('username'),
    #                                                              str(demo.get('users')[0].get('id'))))
    #     messaging_service.send_email(user_email, subject, message, 'html')

    return response.text
Esempio n. 20
0
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
Esempio n. 21
0
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)

    try:
        response = requests.request("DELETE", url)
    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
Esempio n. 22
0
def create_demo(demo_name, user_email=None):
    """
    Create a new demo session in the ERP system.

    :param demo_name:   Name of the demo being created.
    :param user_email:  Email of the user creating the demo.

    :return:         The created Demo model.
    """

    # Check email
    if user_email is not None and validate_email(user_email) == False:
        raise UnprocessableEntityException("Invalid email address")

    # 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"
    }
    payload = dict()
    payload['name'] = demo_name
    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 Demo', internal_details=str(e))

    # Commenting out synchronous email sending until one-off tasks are enabled
    # if user_email:
    #     demo = json.loads(response.text)
    #     subject = "Your Logistics Wizard session has been created - Demo #" + \
    #               demo.get('guid')[-6:].upper()
    #     message = messaging_service.compose_msg('welcome.html', (demo.get('guid'),
    #                                                              demo.get('users')[0].get('username'),
    #                                                              str(demo.get('users')[0].get('id'))))
    #     messaging_service.send_email(user_email, subject, message, 'html')

    return response.text
Esempio n. 23
0
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
    }

    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
Esempio n. 24
0
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
    }

    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
Esempio n. 25
0
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
    }
    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
Esempio n. 26
0
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"}

    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