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
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
示例#3
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
示例#4
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
    }
示例#5
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
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
示例#7
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
示例#8
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
示例#9
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