Skip to content

fix(api): set Referer and Origin headers to make polling work #159

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

Merged
merged 1 commit into from
Apr 12, 2025
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
12 changes: 12 additions & 0 deletions src/elmo/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .. import query as q
from ..__about__ import __version__
from ..systems import ELMO_E_CONNECT
from ..utils import _camel_to_snake_case, _sanitize_session_id
from .decorators import require_lock, require_session
from .exceptions import (
Expand Down Expand Up @@ -49,10 +50,21 @@ def __init__(self, base_url=None, domain=None, session_id=None):
self._session_id = session_id
self._panel = None
self._lock = Lock()

# Conditionally add required headers
if self._router._base_url == ELMO_E_CONNECT:
self._session.headers.update(
{
"Referer": "https://webservice.elmospa.com/",
"Origin": "https://webservice.elmospa.com",
}
)

# Debug
_LOGGER.debug(f"Client | Library version: {__version__}")
_LOGGER.debug(f"Client | Router: {self._router._base_url}")
_LOGGER.debug(f"Client | Domain: {self._domain}")
_LOGGER.debug(f"Client | HTTP Headers: {self._session.headers}")

def auth(self, username, password):
"""Authenticate the client and retrieves the access token. This method uses
Expand Down
13 changes: 13 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2657,3 +2657,16 @@ def test_client_query_last_id_value_error_empty(server, mocker):
# Test
inputs = client.query(query.INPUTS)
assert inputs["last_id"] == 0


def test_client_constructor_sets_required_headers():
"""Should set required Referer and Origin headers during initialization otherwise
polling will fail. Metronet systems do not require these headers.
Regression test for: https://github.com/palazzem/econnect-python/issues/158
"""
client_econnect = ElmoClient(base_url=ELMO_E_CONNECT)
client_metronet = ElmoClient(base_url=IESS_METRONET)
assert client_econnect._session.headers["Referer"] == "https://webservice.elmospa.com/"
assert client_econnect._session.headers["Origin"] == "https://webservice.elmospa.com"
assert "Referer" not in client_metronet._session.headers
assert "Origin" not in client_metronet._session.headers