def negotiate(self):
        negotiate_url = Helpers.get_negotiate_url(self.url)
        self.logger.debug("Negotiate url:{0}".format(negotiate_url))

        response = requests.post(negotiate_url,
                                 headers=self.headers,
                                 verify=self.verify_ssl)
        self.logger.debug("Response status code{0}".format(
            response.status_code))

        if response.status_code != 200:
            raise HubError(
                response.status_code
            ) if response.status_code != 401 else UnAuthorizedHubError()
        data = response.json()
        if "connectionId" in data.keys():
            self.url = Helpers.encode_connection_id(self.url,
                                                    data["connectionId"])

        # Azure
        if 'url' in data.keys() and 'accessToken' in data.keys():
            Helpers.get_logger().debug(
                "Azure url, reformat headers, token and url {0}".format(data))
            self.url = data["url"] if data["url"].startswith(
                "ws") else Helpers.http_to_websocket(data["url"])
            self.token = data["accessToken"]
            self.headers = {"Authorization": "Bearer " + self.token}
 def parse_messages(self, raw):
     Helpers.get_logger().debug("Raw message incomming: ")
     Helpers.get_logger().debug(raw)
     raw_messages = [
         record.replace(self.record_separator, "")
         for record in raw.split(self.record_separator)
         if record is not None and record != "" and record != self.record_separator
         ]
     result = []
     for raw_message in raw_messages:
         dict_message = json.loads(raw_message)
         result.append(self.get_message(dict_message))
     return result
 def __init__(self,
              url,
              protocol,
              headers={},
              keep_alive_interval=15,
              reconnection_handler=None,
              verify_ssl=False,
              skip_negotiation=False):
     self.skip_negotiation = skip_negotiation
     self.logger = Helpers.get_logger()
     self.url = url
     self.protocol = protocol
     self.headers = headers
     self.handshake_received = False
     self.token = None  # auth
     self.state = ConnectionState.disconnected
     self.connection_alive = False
     self.handlers = []
     self.stream_handlers = []
     self._thread = None
     self._ws = None
     self.verify_ssl = verify_ssl
     self.connection_checker = ConnectionStateChecker(
         lambda: self.send(PingMessage()), keep_alive_interval)
     self.reconnection_handler = reconnection_handler
     self.on_connect = None
     self.on_disconnect = None
Exemple #4
0
 def __init__(self, url, protocol, headers={}, **kwargs):
     self.headers = headers
     self.logger = Helpers.get_logger()
     self.handlers = []
     self.stream_handlers = []
     self._on_error = lambda error: self.logger.info(
         "on_error not defined {0}".format(error))
     self.transport = WebsocketTransport(url=url,
                                         protocol=protocol,
                                         headers=headers,
                                         on_message=self.on_message,
                                         **kwargs)
Exemple #5
0
 def encode(self, message):
     Helpers.get_logger().debug(
         self.encoder.encode(message) + self.record_separator)
     return self.encoder.encode(message) + self.record_separator