Skip to content

Commit

Permalink
[FIX] delivery_postlogistics: raise exception when no token received
Browse files Browse the repository at this point in the history
Sometimes due to API error it may happen we don't receive anything back when requesting token.
In this case we want to show meaningful message.
  • Loading branch information
ajaniszewska-dev committed Nov 11, 2024
1 parent c1e7245 commit 3d3afc8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
19 changes: 18 additions & 1 deletion delivery_postlogistics/postlogistics/web_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import urllib.parse
from datetime import datetime, timedelta
from io import BytesIO
from json import JSONDecodeError

import requests
from PIL import Image

from odoo import _, exceptions
from odoo.exceptions import UserError

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -450,7 +452,22 @@ def _request_access_token(cls, delivery_carrier):
},
timeout=60,
)
return response.json()

try:
response.raise_for_status()
json_response = response.json()
except (
JSONDecodeError,
requests.exceptions.HTTPError,
) as error:
raise UserError(
_(
"Postlogistics service is not accessible at the moment. Error code: %s. "
"Please try again later." % (response.status_code or "None")
)
) from error

return json_response

@classmethod
def get_access_token(cls, picking_carrier):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
interactions:
- request:
body: client_secret=c70e3696ae5146e7fe317434e50a90cd&grant_type=client_credentials&client_id=865783331e39e91e633c3916fe892d92
headers:
Accept:
- "*/*"
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- "144"
User-Agent:
- python-requests/2.20.0
content-type:
- application/x-www-form-urlencoded
method: POST
uri: https://wedecint.post.ch/WEDECOAuth/token
response:
body:
string: ''
headers:
Cache-Control:
- no-cache; private
Connection:
- Close
Content-Length:
- "0"
Content-Type:
- text/html; charset=UTF-8
Date:
- Tue, 13 Oct 2020 10:52:26 GMT
status:
code: 503
message: Service Unavailable
version: 1
8 changes: 8 additions & 0 deletions delivery_postlogistics/tests/test_postlogistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from vcr import VCR

from odoo.exceptions import UserError

from .common import TestPostlogisticsCommon

recorder = VCR(
Expand Down Expand Up @@ -108,3 +110,9 @@ def test_postlogistics_rate_shipment(self):
res = self.carrier.postlogistics_rate_shipment(None)
self.assertEqual(len(cassette.requests), 2)
self.assertEqual(res["price"], 1.0)

def test_postlogistics_get_token_error(self):
with recorder.use_cassette("test_token_error") as cassette:
with self.assertRaises(UserError):
self.service_class._request_access_token(self.carrier)
self.assertEqual(len(cassette.requests), 1)

Check warning on line 118 in delivery_postlogistics/tests/test_postlogistics.py

View check run for this annotation

Codecov / codecov/patch

delivery_postlogistics/tests/test_postlogistics.py#L118

Added line #L118 was not covered by tests

0 comments on commit 3d3afc8

Please sign in to comment.