Skip to content

Commit

Permalink
Merge pull request #380 from nylas/noSharedSession
Browse files Browse the repository at this point in the history
Use a fresh connection on every request
  • Loading branch information
grego118 authored Sep 4, 2024
2 parents 7bba2ba + c27ae2b commit ba77c0b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
5 changes: 2 additions & 3 deletions nylas/handler/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ def __init__(self, api_server, api_key, timeout):
self.api_server = api_server
self.api_key = api_key
self.timeout = timeout
self.session = requests.Session()

def _execute(
self,
Expand All @@ -90,7 +89,7 @@ def _execute(
if overrides and overrides.get("timeout"):
timeout = overrides["timeout"]
try:
response = self.session.request(
response = requests.request(
request["method"],
request["url"],
headers=request["headers"],
Expand All @@ -117,7 +116,7 @@ def _execute_download_request(
if overrides and overrides.get("timeout"):
timeout = overrides["timeout"]
try:
response = self.session.request(
response = requests.request(
request["method"],
request["url"],
headers=request["headers"],
Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ def patched_version_and_sys():


@pytest.fixture
def patched_session_request():
def patched_request():
mock_response = Mock()
mock_response.content = b"mock data"
mock_response.json.return_value = {"foo": "bar"}
mock_response.status_code = 200

with patch("requests.Session.request", return_value=mock_response) as mock_request:
with patch("requests.request", return_value=mock_response) as mock_request:
yield mock_request


@pytest.fixture
def mock_session_timeout():
with patch("requests.Session.request", side_effect=requests.exceptions.Timeout):
with patch("requests.request", side_effect=requests.exceptions.Timeout):
yield


Expand Down
16 changes: 8 additions & 8 deletions tests/handler/test_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ def test_build_query_params(self, patched_version_and_sys):
== "https://test.nylas.com/foo?foo=bar&list=a&list=b&list=c&map=key1:value1&map=key2:value2"
)

def test_execute_download_request(self, http_client, patched_session_request):
def test_execute_download_request(self, http_client, patched_request):
response = http_client._execute_download_request(
path="/foo",
)
assert response == b"mock data"

def test_execute_download_request_with_stream(
self, http_client, patched_session_request
self, http_client, patched_request
):
response = http_client._execute_download_request(
path="/foo",
Expand All @@ -189,13 +189,13 @@ def test_execute_download_request_timeout(self, http_client, mock_session_timeou
)

def test_execute_download_request_override_timeout(
self, http_client, patched_version_and_sys, patched_session_request
self, http_client, patched_version_and_sys, patched_request
):
response = http_client._execute_download_request(
path="/foo",
overrides={"timeout": 60},
)
patched_session_request.assert_called_once_with(
patched_request.assert_called_once_with(
"GET",
"https://test.nylas.com/foo",
headers={
Expand Down Expand Up @@ -276,7 +276,7 @@ def test_validate_response_400_keyerror(self):
assert e.value.status_code == 400

def test_execute(
self, http_client, patched_version_and_sys, patched_session_request
self, http_client, patched_version_and_sys, patched_request
):
response = http_client._execute(
method="GET",
Expand All @@ -287,7 +287,7 @@ def test_execute(
)

assert response == {"foo": "bar"}
patched_session_request.assert_called_once_with(
patched_request.assert_called_once_with(
"GET",
"https://test.nylas.com/foo?query=param",
headers={
Expand All @@ -303,7 +303,7 @@ def test_execute(
)

def test_execute_override_timeout(
self, http_client, patched_version_and_sys, patched_session_request
self, http_client, patched_version_and_sys, patched_request
):
response = http_client._execute(
method="GET",
Expand All @@ -315,7 +315,7 @@ def test_execute_override_timeout(
)

assert response == {"foo": "bar"}
patched_session_request.assert_called_once_with(
patched_request.assert_called_once_with(
"GET",
"https://test.nylas.com/foo?query=param",
headers={
Expand Down

0 comments on commit ba77c0b

Please sign in to comment.