Skip to content

Commit 59c2a1e

Browse files
authored
Merge pull request #56 from cloudblue/use_sessions
Use sessions to save SSL handshake time
2 parents e92c6ea + 1b8b5d6 commit 59c2a1e

7 files changed

Lines changed: 51 additions & 48 deletions

File tree

.github/workflows/build.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ jobs:
1414
runs-on: ubuntu-latest
1515
strategy:
1616
matrix:
17-
python-version: ['3.7', '3.8', '3.9', '3.10']
17+
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
1818
steps:
1919
- name: Checkout
20-
uses: actions/checkout@v2
20+
uses: actions/checkout@v3
2121
with:
2222
fetch-depth: 0
2323
- name: Set up Python ${{ matrix.python-version }}
24-
uses: actions/setup-python@v2
24+
uses: actions/setup-python@v4
2525
with:
2626
python-version: ${{ matrix.python-version }}
2727
- name: Install dependencies
@@ -41,11 +41,11 @@ jobs:
4141
runs-on: ubuntu-latest
4242
steps:
4343
- name: Checkout
44-
uses: actions/checkout@v2
44+
uses: actions/checkout@v3
4545
with:
4646
fetch-depth: 0
4747
- name: Set up Python '3.10.0'
48-
uses: actions/setup-python@v2
48+
uses: actions/setup-python@v4
4949
with:
5050
python-version: '3.10'
5151
- name: Install dependencies

.github/workflows/deploy.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ jobs:
88
deploy:
99
runs-on: ubuntu-latest
1010
steps:
11-
- uses: actions/checkout@v2
11+
- uses: actions/checkout@v3
1212
- name: Set up Python
13-
uses: actions/setup-python@v2
13+
uses: actions/setup-python@v4
1414
with:
1515
python-version: '3.10'
1616
- name: Install dependencies
@@ -25,7 +25,7 @@ jobs:
2525
run: |
2626
poetry run pytest
2727
- name: Extract tag name
28-
uses: actions/github-script@v3
28+
uses: actions/github-script@v6
2929
id: tag
3030
with:
3131
github-token: ${{ secrets.GITHUB_TOKEN }}

connect/client/fluent.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import threading
88
from json.decoder import JSONDecodeError
99

10+
import httpx
11+
12+
import requests
13+
1014
from connect.client.constants import CONNECT_ENDPOINT_URL, CONNECT_SPECS_URL
1115
from connect.client.mixins import AsyncClientMixin, SyncClientMixin
1216
from connect.client.models import AsyncCollection, AsyncNS, Collection, NS
@@ -205,6 +209,10 @@ class ConnectClient(_ConnectClientBase, threading.local, SyncClientMixin):
205209
* **timeout** *(optional)* - Timeout parameter to pass to the underlying HTTP client.
206210
* **resourceset_append** *(optional)* - Append all the pages to the current resourceset.
207211
"""
212+
def __init__(self, *args, **kwargs):
213+
super().__init__(*args, **kwargs)
214+
self._session = requests.Session()
215+
208216
def _get_collection_class(self):
209217
return Collection
210218

@@ -243,6 +251,7 @@ class AsyncConnectClient(_ConnectClientBase, AsyncClientMixin):
243251
def __init__(self, *args, **kwargs):
244252
super().__init__(*args, **kwargs)
245253
self._response = contextvars.ContextVar('response', default=None)
254+
self._session = httpx.AsyncClient()
246255

247256
@property
248257
def response(self):

connect/client/mixins.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
#
66
import time
77

8-
import httpx
9-
10-
import requests
11-
128
from httpx import HTTPError
139

1410
from requests.exceptions import RequestException, Timeout
@@ -81,7 +77,7 @@ def _execute_http_call(self, method, url, kwargs): # noqa: CCR001
8177
if self.logger:
8278
self.logger.log_request(method, url, kwargs)
8379
try:
84-
self.response = requests.request(method, url, **kwargs)
80+
self.response = self._session.request(method, url, **kwargs)
8581
if self.logger:
8682
self.logger.log_response(self.response)
8783
except Timeout:
@@ -169,8 +165,7 @@ async def _execute_http_call(self, method, url, kwargs):
169165
if self.logger:
170166
self.logger.log_request(method, url, kwargs)
171167

172-
async with httpx.AsyncClient() as client:
173-
self.response = await client.request(method, url, **kwargs)
168+
self.response = await self._session.request(method, url, **kwargs)
174169

175170
if self.logger:
176171
self.logger.log_response(self.response)

poetry.lock

Lines changed: 29 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "connect-openapi-client"
3-
version = "26.0"
3+
version = "25.16"
44
description = "Connect Python OpenAPI Client"
55
authors = ["CloudBlue"]
66
license = "Apache-2.0"
@@ -48,7 +48,7 @@ importlib-metadata = { version = "^4.12.0", python = ">=3.7,<3.8" }
4848

4949
[tool.poetry.dev-dependencies]
5050
pytest = ">=6.1.2,<8"
51-
pytest-cov = ">=2.10.1,<4"
51+
pytest-cov = ">=2.10.1,<5"
5252
pytest-mock = "^3.3.1"
5353
coverage = {extras = ["toml"], version = ">=5.3,<7"}
5454
flake8 = ">=3.8,<6"

tests/client/test_fluent.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,8 @@ def test_help(mocker):
531531

532532

533533
def test_non_server_error(mocker):
534-
mocker.patch('connect.client.mixins.requests.request', side_effect=RequestException('generic'))
535-
536534
c = ConnectClient('API_KEY', endpoint='https://localhost')
535+
c._session.request = mocker.MagicMock(side_effect=RequestException('generic'))
537536

538537
with pytest.raises(ClientError) as cv:
539538
c.execute('get', 'path')

0 commit comments

Comments
 (0)