def connect()

in camera-sdk/iotccsdk/ipcprovider.py [0:0]


    def connect(self):
        """
        Establish a connection with QMMF IPC webserver on the camera.

        This API also sets the `session_token` attribute from the token
        obtained from the camera.

        Returns
        -------
        bool
            True if the connection was successful.

        Raises
        ------
        ConnectionError
            When the result of the call is a failure
        Timeout
            When the request times out on the connect request.
        RequestException
            The request is not correctly formed.

        """
        if self._session_token:
            # This is to clear out previous session before starting a new one
            self.logout()

        with requests.session() as mysession:
            try:
                url = self._build_url(LOGIN_PATH)
                payload = {"username": self.username, "userpwd": self.password}
                self.logger.info("API: %s data: %s" % (url, payload))
                response = mysession.post(url, json.dumps(payload))
                self.logger.info("Login response: %s" % response.text)
                result = response.json()
                if "status" in result and result["status"]:
                    self._session_token = response.headers["Set-Cookie"]
                    self.logger.info(
                        "connection established with session token: [%s]" % self._session_token)
                    self._heartbeat_manager = HeartBeatManager(
                        self.host, self._session_token)
                    return True
                else:
                    raise requests.ConnectionError(
                        "Failed to connect. Server returned status=False")

            except requests.exceptions.Timeout:
                # TODO: user should have a way to figure out if required services are running?
                # maybe some simple URL
                self.logger.error(
                    "Timeout: Please check the device is running and the IPC service is available")
                raise
            except requests.exceptions.RequestException as e:
                self.logger.exception(e.strerror)
                raise
            except Exception as e:
                self.logger.exception(e)
                raise