Skip to content

Commit

Permalink
Add post support
Browse files Browse the repository at this point in the history
  • Loading branch information
mayhem committed Jan 21, 2025
1 parent 5175106 commit 3e15652
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 7 additions & 2 deletions liblistenbrainz/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
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])
retry_strategy = Retry(total=5, allowed_methods=('GET', 'POST'), status_forcelist=[429, 500, 502, 503, 504])
adapter = HTTPAdapter(max_retries=retry_strategy)

STATS_SUPPORTED_TIME_RANGES = (
Expand Down Expand Up @@ -133,9 +133,14 @@ def _post(self, endpoint, data=None, headers=None):
headers = {}
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.post(
response = session.post(
urljoin(API_BASE_URL, endpoint),
data=data,
headers=headers,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_get_injects_auth_token_if_available(self, mock_requests_get):
)


@mock.patch('liblistenbrainz.client.requests.post')
@mock.patch('liblistenbrainz.client.requests.Session.post')
def test_post_injects_auth_token_if_available(self, mock_requests_post):
mock_requests_post.return_value = mock.MagicMock()
self.client._post('/1/user/iliekcomputers/listens')
Expand Down Expand Up @@ -169,7 +169,7 @@ def test_client_get_playing_now_no_listen(self):
)
self.assertIsNone(received_listen)

@mock.patch('liblistenbrainz.client.requests.post')
@mock.patch('liblistenbrainz.client.requests.Session.post')
@mock.patch('liblistenbrainz.client.json.dumps')
def test_submit_single_listen(self, mock_json_dumps, mock_requests_post):
ts = int(time.time())
Expand Down

0 comments on commit 3e15652

Please sign in to comment.