Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(RHTAPBUGS-919): reuse session in pyxis api calls #91

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions pyxis/pyxis.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

LOGGER = logging.getLogger("pyxis")

session = None

def _get_session(pyxis_url: str, auth_required: bool = True) -> requests.Session:
mmalina marked this conversation as resolved.
Show resolved Hide resolved

def _get_session(auth_required: bool = True) -> requests.Session:
"""Create a Pyxis http session with auth based on env variables.

Auth is optional and can be set to use either API key or certificate + key.

Args:
url (str): Pyxis API URL
auth_required (bool): Whether authentication should be required for the session

Raises:
Expand Down Expand Up @@ -62,7 +63,9 @@ def post(url: str, body: Dict[str, Any]) -> requests.Response:

:return: Pyxis response
"""
session = _get_session(url)
global session
if session is None:
session = _get_session()

LOGGER.debug(f"POST request URL: {url}")
LOGGER.debug(f"POST request body: {body}")
Expand Down Expand Up @@ -119,7 +122,9 @@ def put(url: str, body: Dict[str, Any]) -> Dict[str, Any]:

:return: Pyxis response
"""
session = _get_session(url)
global session
if session is None:
session = _get_session()

LOGGER.debug(f"PATCH Pyxis request: {url}")
resp = session.put(url, json=body)
Expand All @@ -144,7 +149,10 @@ def get(url: str, params: Optional[Dict[str, str]] = None, auth_required: bool =

:return: Pyxis GET request response
"""
session = _get_session(url, auth_required=auth_required)
global session
if session is None:
session = _get_session()

LOGGER.debug(f"GET Pyxis request url: {url}")
LOGGER.debug(f"GET Pyxis request params: {params}")
resp = session.get(url, params=params)
Expand Down
73 changes: 60 additions & 13 deletions pyxis/test_pyxis.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def test_get_session_cert(mock_path_exists: MagicMock, monkeypatch: Any) -> None
mock_path_exists.return_value = True
monkeypatch.setenv("PYXIS_CERT_PATH", "/path/to/cert.pem")
monkeypatch.setenv("PYXIS_KEY_PATH", "/path/to/key.key")
session = pyxis._get_session("test")

session = pyxis._get_session()

assert session.cert == ("/path/to/cert.pem", "/path/to/key.key")

Expand All @@ -31,28 +32,42 @@ def test_get_session_cert_not_exist(mock_path_exists: MagicMock, monkeypatch: An
monkeypatch.setenv("PYXIS_KEY_PATH", "/path/to/key.key")

with pytest.raises(Exception):
pyxis._get_session("test")
pyxis._get_session()


def test_get_session_no_auth(monkeypatch: Any) -> None:
session = pyxis._get_session("test", auth_required=False)
session = pyxis._get_session(auth_required=False)

assert session.cert is None


@patch("pyxis.session", None)
@patch("pyxis._get_session")
def test_post(mock_get_session: MagicMock) -> None:
resp = pyxis.post(API_URL, {})

assert resp == mock_get_session.return_value.post.return_value
mock_get_session.assert_called_once_with()


@patch("pyxis.session")
@patch("pyxis._get_session")
def test_post(mock_session: MagicMock) -> None:
def test_post_existing_session(mock_get_session, mock_session: MagicMock) -> None:
resp = pyxis.post(API_URL, {})

assert resp == mock_session.return_value.post.return_value
assert resp == mock_session.post.return_value
mock_get_session.assert_not_called()


@patch("pyxis.session", None)
@patch("pyxis._get_session")
def test_post_error(mock_session: MagicMock) -> None:
def test_post_error(mock_get_session: MagicMock) -> None:
response = Response()
response.status_code = 400
mock_session.return_value.post.return_value.raise_for_status.side_effect = HTTPError(
mock_get_session.return_value.post.return_value.raise_for_status.side_effect = HTTPError(
response=response
)

with pytest.raises(HTTPError):
pyxis.post(API_URL, {})

Expand Down Expand Up @@ -111,44 +126,76 @@ def test_graphql_query__pyxis_error(mock_post: MagicMock):
mock_post.assert_called_once_with(API_URL, REQUEST_BODY)


@patch("pyxis.session", None)
@patch("pyxis._get_session")
def test_put(mock_session: MagicMock) -> None:
mock_session.return_value.put.return_value.json.return_value = {"key": "val"}
def test_put(mock_get_session: MagicMock) -> None:
mock_get_session.return_value.put.return_value.json.return_value = {"key": "val"}

resp = pyxis.put(API_URL, {})

assert resp == {"key": "val"}
mock_get_session.assert_called_once_with()


@patch("pyxis.session")
@patch("pyxis._get_session")
def test_put_error(mock_session: MagicMock) -> None:
def test_put_existing_session(mock_get_session, mock_session: MagicMock) -> None:
mock_session.put.return_value.json.return_value = {"key": "val"}

resp = pyxis.put(API_URL, {})

assert resp == {"key": "val"}
mock_get_session.assert_not_called()


@patch("pyxis.session", None)
@patch("pyxis._get_session")
def test_put_error(mock_get_session: MagicMock) -> None:
response = Response()
response.status_code = 400
mock_session.return_value.put.return_value.raise_for_status.side_effect = HTTPError(
mock_get_session.return_value.put.return_value.raise_for_status.side_effect = HTTPError(
response=response
)

with pytest.raises(HTTPError):
pyxis.put(API_URL, {})


@patch("pyxis.session", None)
@patch("pyxis._get_session")
def test_get(mock_session: MagicMock) -> None:
mock_session.return_value.get.return_value = {"key": "val"}
def test_get(mock_get_session: MagicMock) -> None:
mock_get_session.return_value.get.return_value = {"key": "val"}

resp = pyxis.get(API_URL)

assert resp == {"key": "val"}
mock_get_session.assert_called_once_with()


@patch("pyxis.session")
@patch("pyxis._get_session")
def test_get_existing_session(mock_get_session, mock_session: MagicMock) -> None:
mock_session.get.return_value = {"key": "val"}

resp = pyxis.get(API_URL)

assert resp == {"key": "val"}
mock_get_session.assert_not_called()


def test_add_session_retries() -> None:
status_forcelist = (404, 503)
total = 3
backoff_factor = 0.5
session = Session()

pyxis.add_session_retries(
session,
total=total,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)

assert session.adapters["http://"].max_retries.total == total
assert session.adapters["http://"].max_retries.backoff_factor == backoff_factor
assert session.adapters["http://"].max_retries.status_forcelist == status_forcelist
Expand Down