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 Oct 31, 2024
1 parent 85bdccb commit 0f996fa
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions delivery_postlogistics/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"category": "Delivery",
"complexity": "normal",
"depends": ["delivery", "mail", "base", "stock"],
"external_dependencies": {"python": ["requests==2.27.0"]},
"website": "https://github.com/OCA/delivery-carrier",
"data": [
"security/ir.model.access.csv",
Expand Down
18 changes: 17 additions & 1 deletion delivery_postlogistics/postlogistics/web_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from PIL import Image

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

_logger = logging.getLogger(__name__)

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

try:
response.raise_for_status()
json_response = response.json()
except (
requests.exceptions.JSONDecodeError,
requests.exceptions.HTTPError,
) as error:
raise UserError(
_(
"Postlogistics service is not accessible at the moment. "
"Please try again later."
)
) 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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# generated from manifests external_dependencies
requests==2.27.0
roulier
unidecode
zeep

0 comments on commit 0f996fa

Please sign in to comment.