def test_checkValidProjectId(self): def verify(pid, msg): try: utils.checkValidProjectId(pid) except ValueError as e: self.assertEqual(msg, str(e)) verify(None, "projectId must be an int") verify("50", "projectId must be an int") verify(-1, "projectId must be greater than 0") verify(0, "projectId must be greater than 0") # Should be valid utils.checkValidProjectId(1) utils.checkValidProjectId(14436643675940) self.assertTrue(True)
def __init__(self, projectId, deviceId=None, seriesName=None, timeUnit=TimeUnit.MILLISECONDS): """Construct a new query object.""" utils.checkValidProjectId(projectId) if timeUnit is None: timeUnit = TimeUnit.MILLISECONDS if not isinstance(timeUnit, TimeUnit): raise ValueError("timeUnit must be a query.TimeUnit") self._pid = projectId self._did = deviceId self._series = seriesName self._params = {} self._timeUnit = timeUnit if timeUnit != TimeUnit.MILLISECONDS: self._params["timefmt"] = str(timeUnit.value)
def getProjectToken(self, userToken, projectId, duration=None, options=None): """Wraps API call `GET /tokens/project` Gets a new project token with the supplied parameters. Params: userToken - User token to authenticate to the iobeam backend projectId - ID of the project to get a token for duration - Specification of how long the token is valid for, in form <num><unit> where unit is (w)eeks, (d)ays, (h)ours, (m)inutes, or (s)econds. Example: 5w = 5 weeks. Optional, with a default of None (standard token validity period). options - Additional options for the token passed as a dict. Options include permissions (booleans, named "read", "write", "admin"), refreshable (boolean, "refreshable"), and device ID (string, "device_id"). Returns: The JSON web token (JWT) string Raises: UnknownCodeError if an error response is returned by server. """ utils.checkValidProjectId(projectId) endpoint = self.makeEndpoint("tokens/project") r = self.requester().get(endpoint).token(userToken) \ .setParam("project_id", projectId) if duration is not None: _checkValidDuration(duration) r.setParam("duration", duration) if options is not None: if not isinstance(options, dict): raise ValueError("options must be a dict") for p in options: r.setParam(p, options[p]) r.execute() if r.getResponseCode() == 200: return r.getResponse()["token"] else: raise request.UnknownCodeError(r)
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
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)
def verify(pid, msg): try: utils.checkValidProjectId(pid) except ValueError as e: self.assertEqual(msg, str(e))