Skip to content

Commit

Permalink
Add resilience to API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
joaodaher committed Apr 4, 2021
1 parent 4a59b77 commit 1d65a76
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
14 changes: 10 additions & 4 deletions glassfrog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
import os

import requests
from retrying import retry

from glassfrog import exceptions


def retry_if_conn_error(exception):
return isinstance(exception, ConnectionError) or isinstance(exception, requests.HTTPError)


class GlassFrogClient:
_URL = 'https://api.glassfrog.com/api/v3'
_TOKEN = os.environ.get('GLASSFROG_API_TOKEN')
Expand All @@ -31,9 +36,10 @@ def get(cls, resource, id=None, from_resource=None):
else:
url = f'{cls._URL}/{resource}'

response = requests.get(
url=url,
headers=cls._get_headers(),
)
with retry(stop_max_attempt_number=3, retry_on_exception=retry_if_conn_error):
response = requests.get(
url=url,
headers=cls._get_headers(),
)
response.raise_for_status()
return response.json()
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ packages = [
[tool.poetry.dependencies]
python = ">=3.7,<3.10"
requests = "*"
retrying = "*"


[tool.poetry.dev-dependencies]
Expand Down

0 comments on commit 1d65a76

Please sign in to comment.