Пример #1
0
    def test_checkValidDeviceId(self):
        def verify(did, msg):
            try:
                utils.checkValidDeviceId(did)
            except ValueError as e:
                self.assertEqual(msg, str(e))

        verify(None, "deviceId must be a string")
        verify(1, "deviceId must be a string")
        verify("", "deviceId must be more than 0 characters")
        verify("no.periods", "deviceId can only include a-z, A-Z, 0-9, _, :, and -")
        verify("no\"quotes", "deviceId can only include a-z, A-Z, 0-9, _, :, and -")

        # Should be valid
        utils.checkValidDeviceId("valid")
        self.assertTrue(True)
Пример #2
0
    def __init__(self, projectId, deviceId, deviceName=None):
        """Constructor for a Device object.

        A valid Device object has at least a `projectId` and a `deviceId`, and
        optionally a `deviceName`.

        Params:
            projectId - Project id (int) that this device belongs to
            deviceId - Id of this device
            deviceName - Optional secondary identifier for the device

        Raises:
            ValueError - If projectId is None, not an int, or not >0. Also if
                         deviceId is None.
        """
        utils.checkValidProjectId(projectId)
        utils.checkValidDeviceId(deviceId)

        self.projectId = projectId
        self.deviceId = deviceId
        self.deviceName = deviceName
Пример #3
0
    def __init__(self, projectId, deviceId, deviceName=None):
        """Constructor for a Device object.

        A valid Device object has at least a `projectId` and a `deviceId`, and
        optionally a `deviceName`.

        Params:
            projectId - Project id (int) that this device belongs to
            deviceId - Id of this device
            deviceName - Optional secondary identifier for the device

        Raises:
            ValueError - If projectId is None, not an int, or not >0. Also if
                         deviceId is None.
        """
        utils.checkValidProjectId(projectId)
        utils.checkValidDeviceId(deviceId)

        self.projectId = projectId
        self.deviceId = deviceId
        self.deviceName = deviceName
Пример #4
0
    def importBatch(self, projectId, deviceId, dataStore):
        """Wraps API call `POST /imports?fmt=table`

        Sends data to the iobeam backend to be stored.

        Params:
            projectId - Project ID the data belongs to
            deviceId - Device ID the data belongs to
            dataStore - A `DataStore` object containing the the data to be imported

        Returns:
            A tuple where the first item is the success of all of the requests (True if
            all succeed, False otherwise); the second item is any error message or None
            if successful.

        Raises:
            ValueError - If validity checks fail for the token, project id, or device id.
        """
        utils.checkValidProjectId(projectId)
        utils.checkValidProjectToken(self.token)
        utils.checkValidDeviceId(deviceId)
        if dataStore is None or len(dataStore) == 0:
            utils.getLogger().warning("Attempted to send with no data")
            return (True, None)
        endpoint = self.makeEndpoint("imports")

        reqs = ImportService._makeListOfBatchReqs(projectId, deviceId,
                                                  dataStore)
        success = True
        extra = None
        for req in reqs:
            r = self.requester().post(endpoint).token(self.token) \
                .setParam("fmt", "table") \
                .setBody(req)
            r.execute()
            success = success and (r.getResponseCode() == 200)
            if r.getResponseCode() != 200:
                extra = r.getResponse()

        return (success, extra)
Пример #5
0
    def importBatch(self, projectId, deviceId, dataStore):
        """Wraps API call `POST /imports?fmt=table`

        Sends data to the iobeam backend to be stored.

        Params:
            projectId - Project ID the data belongs to
            deviceId - Device ID the data belongs to
            dataStore - A `DataStore` object containing the the data to be imported

        Returns:
            A tuple where the first item is the success of all of the requests (True if
            all succeed, False otherwise); the second item is any error message or None
            if successful.

        Raises:
            ValueError - If validity checks fail for the token, project id, or device id.
        """
        utils.checkValidProjectId(projectId)
        utils.checkValidProjectToken(self.token)
        utils.checkValidDeviceId(deviceId)
        if dataStore is None or len(dataStore) == 0:
            utils.getLogger().warning("Attempted to send with no data")
            return (True, None)
        endpoint = self.makeEndpoint("imports")

        reqs = ImportService._makeListOfBatchReqs(projectId, deviceId, dataStore)
        success = True
        extra = None
        for req in reqs:
            r = self.requester().post(endpoint).token(self.token) \
                .setParam("fmt", "table") \
                .setBody(req)
            r.execute()
            success = success and (r.getResponseCode() == 200)
            if r.getResponseCode() != 200:
                extra = r.getResponse()

        return (success, extra)
Пример #6
0
 def verify(did, msg):
     try:
         utils.checkValidDeviceId(did)
     except ValueError as e:
         self.assertEqual(msg, str(e))