Skip to content

Commit

Permalink
Add the retry session/wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
mayhem committed Jan 21, 2025
1 parent e9d211b commit be587b5
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion liblistenbrainz/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

import json
import requests
from requests.adapters import HTTPAdapter
import time
from urllib3.util import Retry

from datetime import datetime
from enum import Enum
Expand All @@ -25,6 +27,9 @@
from liblistenbrainz.utils import _validate_submit_listens_payload, _convert_api_payload_to_listen
from urllib.parse import urljoin

retry_strategy = Retry(total=5, status_forcelist=[429, 500, 502, 503, 504])
adapter = HTTPAdapter(max_retries=retry_strategy)

STATS_SUPPORTED_TIME_RANGES = (
'week',
'month',
Expand Down Expand Up @@ -95,9 +100,13 @@ def _get(self, endpoint, params=None, headers=None):
if self._auth_token:
headers['Authorization'] = f'Token {self._auth_token}'

session = requests.Session()
session.mount("http://", adapter) # http is not used, but in case someone needs to use to for dev work, its included here
session.mount("https://", adapter)

try:
self._wait_until_rate_limit()
response = requests.get(
response = session.get(
urljoin(API_BASE_URL, endpoint),
params=params,
headers=headers,
Expand Down

0 comments on commit be587b5

Please sign in to comment.