Ejemplo n.º 1
0
    def refreshToken(self, oldToken):
        """Wraps API call `POST /tokens/project`

        Refreshes project token using an old token.

        Params:
            oldToken - Previous project token to refresh

        Returns:
            Refreshed project token (string).

        Raises:
            UnknownCodeError if an error response is returned by server.
        """
        utils.checkValidProjectToken(oldToken)
        endpoint = self.makeEndpoint("tokens/project")
        req = {"refresh_token": oldToken}

        r = self.requester().post(endpoint).setBody(req)
        r.execute()

        if r.getResponseCode() == 200:
            return r.getResponse()["token"]
        else:
            raise request.UnknownCodeError(r)
Ejemplo n.º 2
0
    def refreshToken(self, oldToken):
        """Wraps API call `POST /tokens/project`

        Refreshes project token using an old token.

        Params:
            oldToken - Previous project token to refresh

        Returns:
            Refreshed project token (string).

        Raises:
            UnknownCodeError if an error response is returned by server.
        """
        utils.checkValidProjectToken(oldToken)
        endpoint = self.makeEndpoint("tokens/project")
        req = {"refresh_token": oldToken}

        r = self.requester().post(endpoint).setBody(req)
        r.execute()

        if r.getResponseCode() == 200:
            return r.getResponse()["token"]
        else:
            raise request.UnknownCodeError(r)
Ejemplo n.º 3
0
    def test_checkValidProjectToken(self):
        def verify(token, msg):
            try:
                utils.checkValidProjectToken(token)
            except ValueError as e:
                self.assertEqual(msg, str(e))

        verify(None, "token must be a string")
        verify(1, "token must be a string")
        verify("", "token must be more than 0 characters")

        # Should be valid
        utils.checkValidProjectToken("valid")
        self.assertTrue(True)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 6
0
 def verify(token, msg):
     try:
         utils.checkValidProjectToken(token)
     except ValueError as e:
         self.assertEqual(msg, str(e))