Skip to content

Commit

Permalink
fix: retry token on error (#16957)
Browse files Browse the repository at this point in the history
  • Loading branch information
miketheman authored Oct 23, 2024
1 parent be5f770 commit 41b9c91
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
23 changes: 23 additions & 0 deletions tests/unit/helpdesk/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import requests
import responses

from pyramid_retry import RetryableException
from zope.interface.verify import verifyClass

from warehouse.helpdesk.interfaces import IHelpDeskService
Expand Down Expand Up @@ -101,6 +102,28 @@ def test_create_service_fails_when_missing_setting(self, partial_setting):
with pytest.raises(KeyError):
HelpScoutService.create_service(context, request)

@responses.activate
def test_retries_on_error(self):
responses.add(
responses.POST,
"https://api.helpscout.net/v2/oauth2/token",
body=requests.exceptions.RequestException(),
)

context = None
request = pretend.stub(
http=requests.Session(),
registry=pretend.stub(
settings={
"helpscout.app_id": "an insecure helpscout app id",
"helpscout.app_secret": "an insecure helpscout app secret",
},
),
)

with pytest.raises(RetryableException):
HelpScoutService.create_service(context, request)

@responses.activate
def test_create_conversation(self):
responses.add(
Expand Down
23 changes: 14 additions & 9 deletions warehouse/helpdesk/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

from textwrap import dedent

from pyramid_retry import RetryableException
from requests.exceptions import RequestException
from zope.interface import implementer

from .interfaces import IHelpDeskService
Expand Down Expand Up @@ -93,15 +95,18 @@ def create_service(cls, _context, request: Request) -> HelpScoutService:
#
# TODO: Ideally, we would cache this token for up to 48 hours and reuse it.
# For now, we'll just get a new token each time, since we're low enough volume.
resp = request.http.post(
"https://api.helpscout.net/v2/oauth2/token",
json={
"grant_type": "client_credentials",
"client_id": _app_id,
"client_secret": _app_secret,
},
)
resp.raise_for_status()
try:
resp = request.http.post(
"https://api.helpscout.net/v2/oauth2/token",
json={
"grant_type": "client_credentials",
"client_id": _app_id,
"client_secret": _app_secret,
},
)
resp.raise_for_status()
except RequestException as e:
raise RetryableException from e

return cls(
session=request.http,
Expand Down

0 comments on commit 41b9c91

Please sign in to comment.