Beispiel #1
0
def restRegister():
    try:
        request_data = rest_common.get_rest_data(request)
        data = copy_dict(request_data)
        if 'auto_deliver' not in data:
            data['auto_deliver'] = False
        elif not isinstance(data['auto_deliver'], bool):
            if isinstance(data['auto_deliver'],
                          str) and data['auto_deliver'].lower() == 'true':
                data['auto_deliver'] = True
            elif isinstance(data['auto_deliver'],
                            str) and data['auto_deliver'].lower() == 'false':
                data['auto_deliver'] = False
            else:
                err_msg = 'Invalid value for auto_deliver flag! Type is ' + str(
                    type(data['auto_deliver'])) + ', value is ' + str(
                        data['auto_deliver'])
                print(err_msg)
                return rest_common.create_error_response(400, err_msg)
        packet_id = post_service.register_packet(data)
        return rest_common.create_response(200, {"packet_id": str(packet_id)})
    except Exceptions.InvalidActionException as e:
        return rest_common.create_response(400, e.toDict())
    except Exceptions.CommandFailedException as e:
        return rest_common.create_error_response(504, e)
Beispiel #2
0
def restAddUser():
    try:
        data = rest_common.get_rest_data(request)
        user_service.add_user(data)
        return rest_common.create_response(200)
    except InvalidActionException as e:
        return rest_common.create_response(400, e.toDict())
    except UserExistsException as e:
        return rest_common.create_error_response(409, e)
    except CommandFailedException as e:
        return rest_common.create_error_response(504, e)
def _check_input(fromtime, to, information, map):
    if not packet_regex.regex_matches_exactly(packet_regex.regex_timestamp,
                                              fromtime):
        return rest_common.create_error_response(400, 'Unknown from timestamp')
    if not packet_regex.regex_matches_exactly(packet_regex.regex_timestamp,
                                              to):
        return rest_common.create_error_response(400, 'Unknown to timestamp')
    if not information in map:
        return rest_common.create_error_response(400,
                                                 'Unkown information request')
    return None
Beispiel #4
0
def restLogoutUser():
    try:
        data = rest_common.get_rest_data(request)
        user_service.logout_user(data)
        return rest_common.create_response(200)
    except InvalidActionException as e:
        return rest_common.create_response(400, e.toDict())
    except SessionElapsedException as e:
        return rest_common.create_error_response(401, e)
    except InvalidSessionIdException as e:
        return rest_common.create_error_response(422, e)
    except CommandFailedException as e:
        return rest_common.create_error_response(504, e)
Beispiel #5
0
def restGetPacket(session_id):
    try:
        data = {'session_id': session_id}
        packets = user_service.get_packets_from_user(data)
        if not packets:
            packets = []
        return rest_common.create_response(200, {'packets': packets})
    except InvalidActionException as e:
        return rest_common.create_response(400, e.toDict())
    except SessionElapsedException as e:
        return rest_common.create_error_response(401, e)
    except InvalidSessionIdException as e:
        return rest_common.create_error_response(422, e)
Beispiel #6
0
def restAuthenticateUser():
    try:
        data = rest_common.get_rest_data(request)
        session_id = user_service.authenticate_user(data)
        return rest_common.create_response(200,
                                           {'session_id': str(session_id)})
    except InvalidActionException as e:
        return rest_common.create_response(400, e.toDict())
    except UserUnknownException as e:
        return rest_common.create_error_response(404, e)
    except InvalidPasswortException as e:
        return rest_common.create_error_response(401, e)
    except CommandFailedException as e:
        return rest_common.create_error_response(504, e)
Beispiel #7
0
def restAddPacket():
    try:
        data = rest_common.get_rest_data(request)
        user_service.add_packet_to_user(data)
        return rest_common.create_response(200)
    except InvalidActionException as e:
        return rest_common.create_response(400, e.toDict())
    except SessionElapsedException as e:
        return rest_common.create_error_response(401, e)
    except InvalidSessionIdException as e:
        return rest_common.create_error_response(422, e)
    except PacketNotFoundException as e:
        return rest_common.create_error_response(410, e)
    except PacketAlreadyAddedException as e:
        return rest_common.create_error_response(409, e)
    except CommandFailedException as e:
        return rest_common.create_error_response(504, e)
Beispiel #8
0
def restPacketStatus(packetId):
    if not packet_regex.regex_matches_exactly(packet_regex.regex_id, packetId):
        return create_invalid_key_error(packetId)

    res = tracking_service.packetStatus(packetId)
    if res is None:
        return rest_common.create_error_response(404, "Packet not found")
    else:
        return rest_common.create_response(200, res)
def restQueryWithGroup(fromtime, to, information, groupby):
    error = _check_input(fromtime, to, information, info_group_map)
    if error:
        return error
    if not groupby in groupby_map:
        return rest_common.create_error_response(400, 'Unknown groupby value')
    #call function
    f = Filter(int(fromtime), int(to))
    result = info_group_map[information](groupby_map[groupby], f)
    return rest_common.create_response(200, {'values': result})
Beispiel #10
0
def restDelivered(packet_id):
    try:
        request_data = rest_common.get_rest_data(request)
        data = copy_dict(request_data)
        data["packet_id"] = packet_id
        post_service.mark_delivered(data)
        return rest_common.create_response(200)
    except Exceptions.InvalidActionException as e:
        print("InvalidAction: register for packet_id '" + packet_id +
              "' failed: " + str(e))
        return rest_common.create_response(400, e.toDict())
    except Exceptions.CommandFailedException as e:
        print("CommandFailed: register for packet_id '" + packet_id +
              "' failed: " + str(e))
        return rest_common.create_error_response(504, e)
Beispiel #11
0
def restUpdateLocation(packet_id):
    try:
        request_data = rest_common.get_rest_data(request)
        #print(str(request_data))
        data = copy_dict(
            request_data)  #Copy the dict as request_data is immutable
        data["packet_id"] = packet_id
        #print(str(data))
        post_service.update_packet_location(data)
        return rest_common.create_response(200)
    except Exceptions.InvalidActionException as e:
        print("InvalidAction: update for packet_id '" + packet_id +
              "' failed: " + str(e))
        return rest_common.create_response(400, e.toDict())
    except Exceptions.CommandFailedException as e:
        print("CommandFailed: update for packet_id '" + packet_id +
              "' failed: " + str(e))
        return rest_common.create_error_response(504, e)
Beispiel #12
0
def restRemovePacket():
    try:
        data = rest_common.get_rest_data(request)
        user_service.remove_packet_from_user(data)
        return rest_common.create_response(200)
    except NoPacketException as e:
        return rest_common.create_error_response(421, e)
    except (InvalidSessionIdException, NoSessionIdException) as e:
        return rest_common.create_error_response(422, e)
    except InvalidActionException as e:
        return rest_common.create_error_response(400, e)
    except UserUnknownException as e:
        return rest_common.create_error_response(404, e)
    except SessionElapsedException as e:
        return rest_common.create_error_response(401, e)
    except PacketNotFoundException as e:
        return rest_common.create_error_response(410, e)
    except NoSuchPacketAddedException as e:
        return rest_common.create_error_response(428, e)
    except PacketAlreadyAddedException as e:
        return rest_common.create_error_response(409, e)