def __init__(self, options): self.__options = options if 'org' not in self.__options or self.__options['org'] == None: raise ibmiotf.ConfigurationException( "Missing required property: org") if 'id' not in self.__options or self.__options['id'] == None: raise ibmiotf.ConfigurationException( "Missing required property: type") if 'auth-method' not in self.__options: raise ibmiotf.ConfigurationException( "Missing required property: auth-method") if (self.__options['auth-method'] == "apikey"): # Check for required API Key and authentication token if 'auth-key' not in self.__options or self.__options[ 'auth-key'] == None: raise ibmiotf.ConfigurationException( "Missing required property for API key based authentication: auth-key" ) if 'auth-token' not in self.__options or self.__options[ 'auth-token'] == None: raise ibmiotf.ConfigurationException( "Missing required property for API key based authentication: auth-token" ) self.credentials = (self.__options['auth-key'], self.__options['auth-token']) elif self.__options['auth-method'] is not None: raise ibmiotf.UnsupportedAuthenticationMethod( options['authMethod'])
def __init__(self, options): self.__options = options username = None password = None if 'org' not in self.__options or self.__options['org'] == None: raise ibmiotf.ConfigurationException("Missing required property: org") if 'id' not in self.__options or self.__options['id'] == None: raise ibmiotf.ConfigurationException("Missing required property: type") # Auth method is optional. e.g. in QuickStart there is no authentication if 'auth-method' not in self.__options: self.__options['auth-method'] = None if (self.__options['auth-method'] == "apikey"): # Check for required API Key and authentication token if 'auth-key' not in self.__options or self.__options['auth-key'] == None: raise ibmiotf.ConfigurationException("Missing required property for API key based authentication: auth-key") if 'auth-token' not in self.__options or self.__options['auth-token'] == None: raise ibmiotf.ConfigurationException("Missing required property for API key based authentication: auth-token") username = self.__options['auth-key'] password = self.__options['auth-token'] elif self.__options['auth-method'] is not None: raise ibmiotf.UnsupportedAuthenticationMethod(options['authMethod']) # Call parent constructor ibmiotf.AbstractClient.__init__( self, options['org'], "a:" + options['org'] + ":" + options['id'], username, password ) # Add handlers for events and status self.client.message_callback_add("iot-2/type/+/id/+/evt/+/fmt/+", self.__onDeviceEvent) self.client.message_callback_add("iot-2/type/+/id/+/mon", self.__onDeviceStatus) self.client.message_callback_add("iot-2/app/+/mon", self.__onAppStatus) # Add handler for commands if not connected to QuickStart if self.__options['org'] != "quickstart": self.client.message_callback_add("iot-2/type/+/id/+/cmd/+/fmt/+", self.__onDeviceCommand) # Attach fallback handler self.client.on_message = self.__onUnsupportedMessage # Initialize user supplied callbacks (devices) self.deviceEventCallback = None self.deviceCommandCallback = None self.deviceStatusCallback = None # Initialize user supplied callbacks (applcations) self.appStatusCallback = None self.client.on_connect = self.on_connect # Create an api client if not connected in QuickStart mode if self.__options['org'] != "quickstart": self.api = ibmiotf.api.ApiClient(options)
def getOrganizationDetails(self): """ Get details about an organization It does not need any parameter to be passed In case of failure it throws IoTFCReSTException """ if self.__options['org'] is None: raise ibmiotf.ConfigurationException("Missing required property: org") else: url = ApiClient.organizationUrlv2 % (self.__options['org']) r = requests.get(url, auth=self.credentials) status = r.status_code if status == 200: self.logger.info("Organization retrieved") return r.json() elif status == 401: raise ibmiotf.IoTFCReSTException(401, "The authentication token is empty or invalid", None) elif status == 403: raise ibmiotf.IoTFCReSTException(403, "The authentication method is invalid or the api key used does not exist", None) elif status == 404: raise ibmiotf.IoTFCReSTException(404, "The organization does not exist", None) elif status == 500: raise ibmiotf.IoTFCReSTException(500, "Unexpected error", None) else: raise ibmiotf.IoTFCReSTException(None, "Unexpected error", None)
def ParseConfigFile(configFilePath): parms = configparser.ConfigParser() sectionHeader = "application" try: with open(configFilePath) as f: try: parms.read_file(f) organization = parms.get(sectionHeader, "org", fallback=None) appId = parms.get(sectionHeader, "id", fallback=None) authMethod = parms.get(sectionHeader, "auth-method", fallback=None) authKey = parms.get(sectionHeader, "auth-key", fallback=None) authToken = parms.get(sectionHeader, "auth-token", fallback=None) except AttributeError: # Python 2.7 support # https://docs.python.org/3/library/configparser.html#configparser.ConfigParser.read_file parms.readfp(f) organization = parms.get(sectionHeader, "org", None) appId = parms.get(sectionHeader, "id", None) authMethod = parms.get(sectionHeader, "auth-method", None) authKey = parms.get(sectionHeader, "auth-key", None) authToken = parms.get(sectionHeader, "auth-token", None) except IOError as e: reason = "Error reading application configuration file '%s' (%s)" % (configFilePath,e[1]) raise ibmiotf.ConfigurationException(reason) return {'org': organization, 'id': appId, 'auth-method': authMethod, 'auth-key': authKey, 'auth-token': authToken}
def __init__(self, options): self.__options = options if self.__options['org'] == None: raise ibmiotf.ConfigurationException( "Missing required property: org") if self.__options['type'] == None: raise ibmiotf.ConfigurationException( "Missing required property: type") if self.__options['id'] == None: raise ibmiotf.ConfigurationException( "Missing required property: id") if self.__options['org'] != "quickstart": if self.__options['auth-method'] == None: raise ibmiotf.ConfigurationException( "Missing required property: auth-method") if (self.__options['auth-method'] == "token"): if self.__options['auth-token'] == None: raise ibmiotf.ConfigurationException( "Missing required property for token based authentication: auth-token" ) else: raise ibmiotf.UnsupportedAuthenticationMethod( options['authMethod']) ibmiotf.AbstractClient.__init__( self, organization=options['org'], clientId="d:" + options['org'] + ":" + options['type'] + ":" + options['id'], username="******" if (options['auth-method'] == "token") else None, password=options['auth-token']) # Add handler for commands if not connected to QuickStart if self.__options['org'] != "quickstart": self.client.message_callback_add("iot-2/cmd/+/fmt/+", self.__onCommand) # Initialize user supplied callback self.commandCallback = None self.client.on_connect = self.on_connect
def __init__(self, options): self.__options = options # Configure logging self.logger = logging.getLogger(self.__module__+"."+self.__class__.__name__) self.logger.setLevel(logging.INFO) if 'org' not in self.__options or self.__options['org'] == None: raise ibmiotf.ConfigurationException("Missing required property: org") if 'id' not in self.__options or self.__options['id'] == None: raise ibmiotf.ConfigurationException("Missing required property: type") if 'auth-method' not in self.__options: raise ibmiotf.ConfigurationException("Missing required property: auth-method") if (self.__options['auth-method'] == "apikey"): # Check for required API Key and authentication token if 'auth-key' not in self.__options or self.__options['auth-key'] == None: raise ibmiotf.ConfigurationException("Missing required property for API key based authentication: auth-key") if 'auth-token' not in self.__options or self.__options['auth-token'] == None: raise ibmiotf.ConfigurationException("Missing required property for API key based authentication: auth-token") self.credentials = (self.__options['auth-key'], self.__options['auth-token']) elif self.__options['auth-method'] is not None: raise ibmiotf.UnsupportedAuthenticationMethod(options['authMethod'])