Skip to content

Commit 0830253

Browse files
fruchk0machi
authored andcommitted
pytest-argus-reporter: cache client/session to minimize opening http requests
since we in some situation go via cloudflare, we should do our best to minimize the session we are opening up
1 parent 1dae0d0 commit 0830253

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

argus/client/base.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,14 @@ def __init__(self, auth_token: str, base_url: str, api_version="v1", extra_heade
3737
self._auth_token = auth_token
3838
self._base_url = base_url
3939
self._api_ver = api_version
40-
self._extra_headers = extra_headers or {}
40+
self.session = requests.Session()
41+
if extra_headers:
42+
self.session.headers.update(extra_headers)
4143

4244
@property
4345
def auth_token(self) -> str:
4446
return self._auth_token
4547

46-
@property
47-
def extra_headers(self) -> dict:
48-
return self._extra_headers
49-
5048
def verify_location_params(self, endpoint: str, location_params: dict[str, str]) -> bool:
5149
required_params: list[str] = re.findall(r"\$[\w_]+", endpoint)
5250
for param in required_params:
@@ -92,7 +90,6 @@ def request_headers(self):
9290
"Authorization": f"token {self.auth_token}",
9391
"Accept": "application/json",
9492
"Content-Type": "application/json",
95-
**self.extra_headers,
9693
}
9794

9895
def get(self, endpoint: str, location_params: dict[str, str] = None, params: dict = None) -> requests.Response:
@@ -101,7 +98,7 @@ def get(self, endpoint: str, location_params: dict[str, str] = None, params: dic
10198
location_params=location_params
10299
)
103100
LOGGER.debug("GET Request: %s, params: %s", url, params)
104-
response = requests.get(
101+
response = self.session.get(
105102
url=url,
106103
params=params,
107104
headers=self.request_headers
@@ -122,7 +119,7 @@ def post(
122119
location_params=location_params
123120
)
124121
LOGGER.debug("POST Request: %s, params: %s, body: %s", url, params, body)
125-
response = requests.post(
122+
response = self.session.post(
126123
url=url,
127124
params=params,
128125
json=body,

pytest-argus-reporter/pytest_argus_reporter.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import concurrent.futures
1414
import json
1515
from json import JSONDecodeError
16+
from functools import cached_property
1617

1718
import six
1819
import pytest
@@ -180,7 +181,7 @@ def __init__(self, config):
180181
self.is_slave = False
181182
self.slices_query_fields = dict()
182183

183-
@property
184+
@cached_property
184185
def argus_client(self):
185186
return ArgusGenericClient(auth_token=self.api_key, base_url=self.base_url, extra_headers=self.extra_headers)
186187

0 commit comments

Comments
 (0)