Skip to content

Commit

Permalink
Merge pull request #110 from cooperlees/master
Browse files Browse the repository at this point in the history
Add http proxy support
  • Loading branch information
Vaelor authored Jan 21, 2022
2 parents e88ba0f + c911adf commit ec0a554
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 22 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Unreleased
'''''''''''

Added
- HTTP proxy support for http + websocket calls
- Websocket calls are now using aiohttp

7.3.2
''''''
Fixed
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
aiohttp>=3.8.1<4.0.0
httpx>=0.20.0<1.0.0
websockets==9.1
sphinx-rtd-theme==0.4.1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
packages=find_packages('src'),
python_requires=">=3.6",
install_requires=[
'websockets>=8',
'aiohttp>=3.8.1<4.0.0'
'httpx>=0.20.0<1.0.0',
],
)
15 changes: 13 additions & 2 deletions src/mattermostdriver/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def __init__(self, options):
self._cookies = None
self._userid = ""
self._username = ""
self._proxies = None
if options["proxy"]:
self._proxies = {"all://": options["proxy"]}

@staticmethod
def _make_url(scheme, url, port, basepath):
Expand Down Expand Up @@ -186,7 +189,11 @@ def _get_request_method(method, client):
class Client(BaseClient):
def __init__(self, options):
super().__init__(options)
self.client = httpx.Client(http2=options.get("http2", False), verify=options.get("verify", True))
self.client = httpx.Client(
http2=options.get("http2", False),
proxies=self._proxies,
verify=options.get("verify", True),
)

def make_request(self, method, endpoint, options=None, params=None, data=None, files=None, basepath=None):
request, url, request_params = self._build_request(method, options, params, data, files, basepath)
Expand Down Expand Up @@ -228,7 +235,11 @@ def delete(self, endpoint, options=None, params=None, data=None):
class AsyncClient(BaseClient):
def __init__(self, options):
super().__init__(options)
self.client = httpx.AsyncClient(http2=options.get("http2", False), verify=options.get("verify", True))
self.client = httpx.AsyncClient(
http2=options.get("http2", False),
proxies=self._proxies,
verify=options.get("verify", True),
)

async def __aenter__(self):
await self.client.__aenter__()
Expand Down
1 change: 1 addition & 0 deletions src/mattermostdriver/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class BaseDriver:
"websocket_kw_args": None,
"debug": False,
"http2": False,
"proxy": None,
}
"""
Required options
Expand Down
38 changes: 20 additions & 18 deletions src/mattermostdriver/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
import time

import websockets
import aiohttp

log = logging.getLogger("mattermostdriver.websocket")
log.setLevel(logging.INFO)
Expand Down Expand Up @@ -49,21 +49,23 @@ async def connect(self, event_handler):
kw_args = {}
if self.options["websocket_kw_args"] is not None:
kw_args = self.options["websocket_kw_args"]
websocket = await websockets.connect(
url,
ssl=context,
**kw_args,
)
await self._authenticate_websocket(websocket, event_handler)
while self._alive:
try:
await self._start_loop(websocket, event_handler)
except websockets.ConnectionClosedError:
break
if (not self.options["keepalive"]) or (not self._alive):
break
async with aiohttp.ClientSession() as session:
async with session.ws_connect(
url,
ssl=context,
proxy=self.options["proxy"],
**kw_args,
) as websocket:
await self._authenticate_websocket(websocket, event_handler)
while self._alive:
try:
await self._start_loop(websocket, event_handler)
except aiohttp.ClientError:
break
if (not self.options["keepalive"]) or (not self._alive):
break
except Exception as e:
log.warning(f"Failed to establish websocket connection: {e}")
log.exception(f"Failed to establish websocket connection: {type(e)} thrown")
await asyncio.sleep(self.options["keepalive_delay"])

async def _start_loop(self, websocket, event_handler):
Expand All @@ -77,7 +79,7 @@ async def _start_loop(self, websocket, event_handler):
keep_alive = asyncio.ensure_future(self._do_heartbeats(websocket))
log.debug("Waiting for messages on websocket")
while self._alive:
message = await websocket.recv()
message = await websocket.receive_str()
self._last_msg = time.time()
await event_handler(message)
log.debug("cancelling heartbeat task")
Expand Down Expand Up @@ -118,9 +120,9 @@ async def _authenticate_websocket(self, websocket, event_handler):
"""
log.debug("Authenticating websocket")
json_data = json.dumps({"seq": 1, "action": "authentication_challenge", "data": {"token": self._token}})
await websocket.send(json_data)
await websocket.send_bytes(json_data)
while True:
message = await websocket.recv()
message = await websocket.receive_str()
status = json.loads(message)
log.debug(status)
# We want to pass the events to the event_handler already
Expand Down

0 comments on commit ec0a554

Please sign in to comment.