def __delitem__(self, key): """ Delete a log """ if isinstance(key, int): # Special case -- allow this to be used as a dict or a list logsUrl = "api/v0002/device/types/%s/devices/%s/diag/logs" % ( self.typeId, self.deviceId) r = self._apiClient.get(logsUrl) if r.status_code == 200: if key > len(r.json()): self.__missing__(key) key = r.json()[key]["id"] else: raise ApiException(r) logUrl = "api/v0002/device/types/%s/devices/%s/diag/logs/%s" % ( self.typeId, self.deviceId, key) r = self._apiClient.delete(logUrl) if r.status_code == 404: self.__missing__(key) elif r.status_code != 204: raise ApiException(r)
def __getitem__(self, key): """ Get a log entry """ if isinstance(key, int): # Special case -- allow this to be used as a dict or a list # Get all, convert list to iterator logsUrl = "api/v0002/device/types/%s/devices/%s/diag/logs" % ( self.typeId, self.deviceId) r = self._apiClient.get(logsUrl) if r.status_code == 200: if key > len(r.json()): self.__missing__(key) return DeviceLog(**r.json()[key]) else: raise ApiException(r) else: logUrl = "api/v0002/device/types/%s/devices/%s/diag/logs/%s" % ( self.typeId, self.deviceId, key) r = self._apiClient.get(logUrl) if r.status_code == 200: return DeviceLog(**r.json()) elif r.status_code == 404: self.__missing__(key) else: raise ApiException(r)
def create(self, name, **kwargs): if self.connectorType == "cloudant": if "bucketInterval" not in kwargs.keys(): raise Exception( "You must specify bucketInterval parameter on create for a Cloudant destination" ) if self.connectorType == "eventstreams": if "partitions" not in kwargs.keys(): raise Exception( "You must specify partitions parameter on create for an EventStreams destination" ) if self.connectorType == "db2": if "columns" not in kwargs.keys(): raise Exception( "You must specify a columns parameter on create for a DB2 destination" ) destination = { "name": name, "type": self.connectorType, "configuration": kwargs } r = self._apiClient.post(self.allDestinationsUrl, data=destination) if r.status_code == 201: return Destination(**r.json()) else: raise ApiException(r)
def update(self, ruleId, ruleType, name, description, destinationName, selector, enabled, configuration=None): url = "api/v0002/historianconnectors/%s/forwardingrules/%s" % ( self.connectorId, ruleId) body = {} body["id"] = ruleId body["name"] = name body["type"] = ruleType body["description"] = description body["destinationName"] = destinationName body["enabled"] = enabled body["selector"] = selector if configuration != None: body["configuration"] = configuration r = self._apiClient.put(url, data=body) if r.status_code == 200: return ForwardingRule(**r.json()) else: raise ApiException(r)
def create(self, serviceBinding): """ Create a new external service. The service must include all of the details required to connect and authenticate to the external service in the credentials property. Parameters: - serviceName (string) - Name of the service - serviceType (string) - must be either eventstreams or cloudant - credentials (json object) - Should have a valid structure for the service type. - description (string) - description of the service Throws APIException on failure """ if not isinstance(serviceBinding, ServiceBindingCreateRequest): if serviceBinding["type"] == "cloudant": serviceBinding = CloudantServiceBindingCreateRequest(**serviceBinding) elif serviceBinding["type"] == "eventstreams": serviceBinding = EventStreamsServiceBindingCreateRequest(**serviceBinding) elif serviceBinding["type"] == "db2": serviceBinding = DB2ServiceBindingCreateRequest(**serviceBinding) elif serviceBinding["type"] == "postgres": serviceBinding = PostgresServiceBindingCreateRequest(**serviceBinding) else: raise Exception("Unsupported service binding type") r = self._apiClient.post(self.allServicesUrl, data=serviceBinding) if r.status_code == 201: return ServiceBinding(**r.json()) else: raise ApiException(r)
def __delete__(self, instance): # TBD debug print ("Delete, Instance, instance: %s" % (instance)) url = self.getUrl(instance) r = self.getApiClient(instance).delete(url) if r.status_code != 204: raise ApiException(r)
def create(self, name, type, serviceId, timezone=None, description=None, enabled=None, configuration=None): """ Create a connector for the organization in the Watson IoT Platform. The connector must reference the target service that the Watson IoT Platform will store the IoT data in. Parameters: - name (string) - Name of the service - type (string) - The type of the target service. The types of service that are currently supported by the Watson IoT Platform are cloudant, eventstreams, db2 and Postgres. - serviceId (string) - The UUID of the service - timezone (string) - The timezone that timestamps generated by the Watson IoT Platform will be converted to prior to writing data to the target service. If the timezone is not specified it defaults to UTC. - description (string) - Description of the service - enabled (boolean) - Flag that indicates whether the historian connector is enabled. If the enabled flag is not specified it defaults to True. - configuration - Optional configuration for the connector. Throws APIException on failure """ connector = {"name": name, "type": type, "serviceId": serviceId} if description != None: connector["description"] = description if timezone != None: connector["timezone"] = timezone if enabled != None: connector["enabled"] = enabled if configuration != None: connector["configuration"] = configuration url = "api/v0002/historianconnectors" r = self._apiClient.post(url, data=connector) if r.status_code == 201: return Connector(apiClient=self._apiClient, **r.json()) else: raise ApiException(r)
def update(self, connectorId, serviceId, name, type, timezone, enabled, description=None, configuration=None): """ Updates the connector with the specified uuid. if description is empty, the existing description will be removed. Parameters: - connector (String), Connnector Id which is a UUID - name (string) - Name of the service - timezone (json object) - Should have a valid structure for the service type. - description (string) - description of the service - enabled (boolean) - enabled Throws APIException on failure. """ url = "api/v0002/historianconnectors/%s" % (connectorId) connectorBody = {} connectorBody["id"] = connectorId connectorBody["name"] = name connectorBody["type"] = type connectorBody["serviceId"] = serviceId connectorBody["timezone"] = timezone connectorBody["enabled"] = enabled if description != None: connectorBody["description"] = description if configuration != None: connectorBody["configuration"] = configuration r = self._apiClient.put(url, data=connectorBody) if r.status_code == 200: return Connector(apiClient=self._apiClient, **r.json()) else: raise ApiException(r)
def create(self, name, **kwargs): if self.connectorType == "cloudant": if ["bucketInterval"] != kwargs.keys(): raise Exception( "You must specify bucketInterval parameter on create for a Cloudant destination" ) if self.connectorType == "eventstreams": if ["partitions"] != kwargs.keys(): raise Exception( "You must specify partitions parameter on create for an EventStreams destination" ) destination = { "name": name, "type": self.connectorType, "configuration": kwargs } url = "api/v0002/historianconnectors/%s/destinations" % ( self.connectorId) r = self._apiClient.post(url, data=destination) if r.status_code == 201: return Destination(**r.json()) else: raise ApiException(r)
def create(self, name, serviceId, timezone, description, enabled): """ Create a connector for the organization in the Watson IoT Platform. The connector must reference the target service that the Watson IoT Platform will store the IoT data in. Parameters: - name (string) - Name of the service - serviceId (string) - must be either eventstreams or cloudant - timezone (string) - - description (string) - description of the service - enabled (boolean) - enabled Throws APIException on failure """ connector = { "name": name, "description": description, "serviceId": serviceId, "timezone": timezone, "enabled": enabled } url = "api/v0002/historianconnectors" r = self._apiClient.post(url, data=connector) if r.status_code == 201: return Connector(apiClient=self._apiClient, **r.json()) else: raise ApiException(r)
def setLocation(self, value): r = self._apiClient.put("api/v0002/device/types/%s/devices/%s/location" % (self.typeId, self.deviceId), value) if r.status_code == 200: return DeviceLocation(**r.json()) else: raise ApiException(r)
def update(self, serviceId, type, serviceName, credentials, description): """ Updates the service with the specified id. if description is empty, the existing description will be removed. Parameters: - serviceId (String), Service Id which is a UUID - serviceName (string), name of service - credentials (json), json object of credentials - description - description of the service Throws APIException on failure. """ url = self.allServicesUrl + "/" + serviceId serviceBody = {} serviceBody["name"] = serviceName serviceBody["type"] = type serviceBody["description"] = description serviceBody["credentials"] = credentials r = self._apiClient.put(url, data=serviceBody) if r.status_code == 200: return ServiceBinding(**r.json()) else: raise ApiException(r)
def create(self, devices): """ Register one or more new devices, each request can contain a maximum of 512KB. The response body will contain the generated authentication tokens for all devices. You must make sure to record these tokens when processing the response. We are not able to retrieve lost authentication tokens It accepts accepts a list of devices (List of Dictionary of Devices), or a single device If you provide a list as the parameter it will return a list in response If you provide a singular device it will return a singular response """ if not isinstance(devices, list): listOfDevices = [devices] returnAsAList = False else: listOfDevices = devices returnAsAList = True r = self._apiClient.post("api/v0002/bulk/devices/add", listOfDevices) if r.status_code in [201, 202]: if returnAsAList: responseList = [] for entry in r.json(): responseList.append(DeviceCreateResponse(**entry)) return responseList else: return DeviceCreateResponse(**r.json()[0]) else: raise ApiException(r)
def _create(self, rule): url = "api/v0002/historianconnectors/%s/forwardingrules" % (self.connectorId) r = self._apiClient.post(url, data=rule) if r.status_code == 201: return ForwardingRule(**r.json()) else: raise ApiException(r)
def __delitem__(self, key): url = "api/v0002/historianconnectors/%s/forwardingrules/%s" % (self.connectorId, key) r = self._apiClient.delete(url) if r.status_code == 404: self.__missing__(key) elif r.status_code != 204: raise ApiException(r)
def __get__(self, instance, type=None): url = self.getUrl(instance) # TBD debug print ("Get, Instance, instance: %s, Owner: %s, URL: %s" % (instance, type, url)) r = self.getApiClient(instance).get(url) if r.status_code == 200: return self._castToClass(apiClient=self.getApiClient(instance), **r.json()) else: raise ApiException(r)
def __delitem__(self, key): """ delete a device type """ url = "api/v0002/device/types/%s" % (key) r = self._apiClient.delete(url) if r.status_code != 204: raise ApiException(r)
def __set__(self, instance, value): url = self.getUrl(instance) # TBD debug print ("Set, Instance, instance: %s, \nOwner: %s, URL: %s, Value: %s" % (instance, type, url, value)) r = self.getApiClient(instance).post(url, data=value) if r.status_code == 201: return self._castToClass(apiClient=self.getApiClient(instance), **r.json()) else: raise ApiException(r)
def __delitem__(self, key): url = "api/v0002/historianconnectors/%s/destinations/%s" % (self.connectorId, key) r = self._apiClient.delete(url) if r.status_code == 404: self.__missing__(key) elif r.status_code != 200: # Unlike most DELETE requests, this API is expected to return 200 with a message body containing the message: # "Successfully deleted Cloudant configuration, the Cloudant database must be manually deleted" raise ApiException(r)
def __getitem__(self, key): url = "api/v0002/historianconnectors/%s/forwardingrules/%s" % (self.connectorId, key) r = self._apiClient.get(url) if r.status_code == 200: return ForwardingRule(**r.json()) if r.status_code == 404: self.__missing__(key) else: raise ApiException(r)
def __contains__(self, key): url = "api/v0002/historianconnectors/%s/forwardingrules/%s" % (self.connectorId, key) r = self._apiClient.get(url) if r.status_code == 200: return True if r.status_code == 404: return False else: raise ApiException(r)
def __len__(self): # Get all, convert list to iterator logsUrl = "api/v0002/device/types/%s/devices/%s/diag/errorCodes" % ( self.typeId, self.deviceId) r = self._apiClient.get(logsUrl) if r.status_code == 200: return len(r.json()) else: raise ApiException(r)
def clear(self): # Get all, convert list to iterator logsUrl = "api/v0002/device/types/%s/devices/%s/diag/logs" % ( self.typeId, self.deviceId) r = self._apiClient.delete(logsUrl) if r.status_code == 204: return True else: raise ApiException(r)
def update(self, typeId, description=None, deviceInfo=None, metadata=None): devicetypeUrl = "api/v0002/device/types/%s" % (typeId) data = {"description": description, "deviceInfo": deviceInfo, "metadata": metadata} r = self._apiClient.put(devicetypeUrl, data) if r.status_code == 200: return DeviceType(apiClient=self._apiClient, **r.json()) else: raise ApiException(r)
def __getitem__(self, key): url = "api/v0002/clientconnectionstates/%s" % (key) r = self._apiClient.get(url) if r.status_code == 200: return ClientStatus(**r.json()) if r.status_code == 404: self.__missing__(key) else: raise ApiException(r)
def __contains__(self, key): url = "api/v0002/clientconnectionstates/%s" % (key) r = self._apiClient.get(url) if r.status_code == 200: return True if r.status_code == 404: return False else: raise ApiException(r)
def getLocation(self): r = self._apiClient.get("api/v0002/device/types/%s/devices/%s/location" % (self.typeId, self.deviceId)) if r.status_code == 200: return DeviceLocation(**r.json()) if r.status_code == 404: # It's perfectly valid for a device to not have a location set, if this is the case, set response to None return None else: raise ApiException(r)
def __delitem__(self, key): """ Delete an Item """ url = self._singleItemUrl % (key) r = self._apiClient.delete(url) if r.status_code == 404: self.__missing__(key) elif r.status_code != 204 and r.status_code != 200: raise ApiException(r)
def create(self, deviceType): """ Register one or more new device types, each request can contain a maximum of 512KB. """ r = self._apiClient.post("api/v0002/device/types", deviceType) if r.status_code == 201: return DeviceType(apiClient=self._apiClient, **r.json()) else: raise ApiException(r)
def list(self): """ List all device management extension packages """ url = 'api/v0002/mgmt/custom/bundle' r = self._apiClient.get(url) if r.status_code == 200: return r.json() else: raise ApiException(r)