diff --git a/exampleDB.py b/exampleDB.py new file mode 100644 index 0000000..1eeb8d0 --- /dev/null +++ b/exampleDB.py @@ -0,0 +1,71 @@ +import datetime + +from pyhafas import HafasClient +from pyhafas.profile import DBProfile + +profile = DBProfile() +profile.activate_retry() +client = HafasClient(profile, debug=True) + +origin = 8096009 +destinations = [ + 8011160, + 8010060, + 8000050, + 8000019, + 8071993, + 8000351, + 8005197, + 8000096, + 8010073, + 8010093, + 8011471, + 8013479, + 8013483, + 8010215, + 8013487, + 8012666, + 8010327, + 597502, + 8011044, + 8010016, + 8010139, + 8010153, + 8012963, + 8012585, + 8010241, + 8012617, + 8010304, + 8010033, + 8010324, + 8010338, + 8010381, + 8010392, + 8096022, + 8001235, + 8000310, + 8003992, + 8004004, + 8000323, + 8005247, + 182006, + 968306, +] + + +for destination in destinations: + journey = client.journeys( # type: ignore + origin=origin, + destination=destination, + date=datetime.datetime.now(), + max_journeys=1, + products={ + "long_distance_express": False, + "long_distance": False, + "ferry": False, + "bus": False, + "suburban": False, + "subway": False, + }, + ) + print(journey) diff --git a/pyhafas/profile/base/helper/request.py b/pyhafas/profile/base/helper/request.py index 9afc2d7..57869a4 100644 --- a/pyhafas/profile/base/helper/request.py +++ b/pyhafas/profile/base/helper/request.py @@ -3,6 +3,8 @@ from typing import Tuple import requests +from requests.adapters import HTTPAdapter +from urllib3.util.retry import Retry from pyhafas.profile import ProfileInterface from pyhafas.profile.base.mappings.error_codes import BaseErrorCodesMapping @@ -11,6 +13,8 @@ class BaseRequestHelper(RequestHelperInterface): + request_session = requests.session() + def calculate_checksum(self: ProfileInterface, data: str) -> str: """ Calculates the checksum of the request (required for most profiles) @@ -56,6 +60,20 @@ def url_formatter(self: ProfileInterface, data: str) -> str: return url + def activate_retry(self: ProfileInterface, retries: int = 4, backoff_factor: float = 1) -> None: + self.request_session = requests.Session() + + retry = Retry( + total=retries, + read=retries, + connect=retries, + backoff_factor=backoff_factor, + ) + + adapter = HTTPAdapter(max_retries=retry) + self.request_session.mount("http://", adapter) + self.request_session.mount("https://", adapter) + def request(self: ProfileInterface, body) -> HafasResponse: """ Sends the request and does a basic parsing of the response and error handling @@ -69,7 +87,7 @@ def request(self: ProfileInterface, body) -> HafasResponse: data.update(self.requestBody) data = json.dumps(data) - res = requests.post( + res = self.request_session.post( self.url_formatter(data), data=data, headers={