Skip to content

Commit 9500ab4

Browse files
committed
Add session persistence
1 parent ea4241f commit 9500ab4

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

py_nextbus/client.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,25 @@ def __init__(
5757
agency_id: str | None = None,
5858
) -> None:
5959
self.agency_id: str | None = agency_id
60-
self.headers: dict[str, str] = {
61-
"Accept": "application/json, text/javascript, */*; q=0.01",
62-
}
60+
61+
self._session: requests.Session = requests.Session()
62+
self._session.headers.update(
63+
{
64+
"User-Agent": "PyNextBus",
65+
"Accept": "application/json",
66+
"Accept-Language": "en-US,en;q=0.5",
67+
"Accept-Encoding": "gzip, deflate, br, zstd",
68+
"Compress": "true",
69+
"DNT": "1",
70+
"Sec-Fetch-Dest": "empty",
71+
"Sec-Fetch-Mode": "cors",
72+
"Connection": "keep-alive",
73+
# Additional headers used in browser
74+
# "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:138.0) Gecko/20100101 Firefox/138.0",
75+
# "Referer": "https://rider.umoiq.com/",
76+
# "Origin": "https://rider.umoiq.com",
77+
}
78+
)
6379

6480
self._rate_limit: int = 0
6581
self._rate_limit_remaining: int = 0
@@ -169,7 +185,7 @@ def _get(
169185
try:
170186
url = f"{self.base_url}/{endpoint}"
171187
LOG.debug("GET %s", url)
172-
response = requests.get(url, params=params, headers=self.headers)
188+
response = self._session.get(url, params=params)
173189
response.raise_for_status()
174190

175191
# Track rate limit information
@@ -183,6 +199,7 @@ def _get(
183199
)
184200

185201
return response.json()
202+
186203
except HTTPError as exc:
187204
raise NextBusHTTPError("Error from the NextBus API", exc) from exc
188205
except json.decoder.JSONDecodeError as exc:

0 commit comments

Comments
 (0)