You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.9 KiB
70 lines
1.9 KiB
4 years ago
|
from abc import abstractmethod
|
||
|
import json
|
||
|
import sys
|
||
3 years ago
|
import threading
|
||
4 years ago
|
if sys.version_info[0] < 3:
|
||
|
from urllib import quote_plus
|
||
|
else:
|
||
|
from urllib.parse import quote_plus
|
||
|
|
||
|
|
||
|
|
||
|
class Transport:
|
||
|
def __init__(self, session, connection):
|
||
|
self._session = session
|
||
|
self._connection = connection
|
||
|
|
||
|
@abstractmethod
|
||
|
def _get_name(self):
|
||
|
pass
|
||
|
|
||
|
def negotiate(self):
|
||
|
url = self.__get_base_url(self._connection,
|
||
|
'negotiate',
|
||
|
connectionData=self._connection.data)
|
||
|
negotiate = self._session.get(url)
|
||
|
|
||
|
negotiate.raise_for_status()
|
||
|
|
||
|
return negotiate.json()
|
||
|
|
||
|
@abstractmethod
|
||
|
def start(self):
|
||
|
pass
|
||
|
|
||
|
@abstractmethod
|
||
|
def send(self, data):
|
||
|
pass
|
||
|
|
||
|
@abstractmethod
|
||
|
def close(self):
|
||
|
pass
|
||
|
|
||
|
def accept(self, negotiate_data):
|
||
|
return True
|
||
|
|
||
|
def _handle_notification(self, message):
|
||
|
if len(message) > 0:
|
||
|
data = json.loads(message)
|
||
|
self._connection.received.fire(**data)
|
||
3 years ago
|
#thread.sleep() #TODO: investigate if we should sleep here
|
||
4 years ago
|
|
||
|
def _get_url(self, action, **kwargs):
|
||
|
args = kwargs.copy()
|
||
|
args['transport'] = self._get_name()
|
||
|
args['connectionToken'] = self._connection.token
|
||
|
args['connectionData'] = self._connection.data
|
||
|
|
||
|
return self.__get_base_url(self._connection, action, **args)
|
||
|
|
||
|
@staticmethod
|
||
|
def __get_base_url(connection, action, **kwargs):
|
||
|
args = kwargs.copy()
|
||
|
args.update(connection.qs)
|
||
|
args['clientProtocol'] = connection.protocol_version
|
||
|
query = '&'.join(['{key}={value}'.format(key=key, value=quote_plus(args[key])) for key in args])
|
||
|
|
||
|
return '{url}/{action}?{query}'.format(url=connection.url,
|
||
|
action=action,
|
||
|
query=query)
|