Skip to content

Commit

Permalink
Retry (#31)
Browse files Browse the repository at this point in the history
* Add script to run multiple destinations

* Add more destinations

* Add DB Request helper

* Make DB Profile use DBRequestHelper

* Add retry using urllib3

* Revert all changes to main

* Make retries and backoff factor have default val
  • Loading branch information
vikramsg authored Jul 1, 2023
1 parent 1512640 commit 408d7b1
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
71 changes: 71 additions & 0 deletions exampleDB.py
Original file line number Diff line number Diff line change
@@ -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)
20 changes: 19 additions & 1 deletion pyhafas/profile/base/helper/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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={
Expand Down

0 comments on commit 408d7b1

Please sign in to comment.