Skip to content

Commit

Permalink
Merge PR #898 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by simahawk
  • Loading branch information
OCA-git-bot committed Nov 11, 2024
2 parents ea21589 + 0b14913 commit 8e93836
Show file tree
Hide file tree
Showing 3 changed files with 66 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
12 changes: 12 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,13 @@ 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:
err_msg = (
"Postlogistics service is not accessible at the moment. Error code: 503. "
"Please try again later."
)
with self.assertRaisesRegex(UserError, err_msg):
self.service_class._request_access_token(self.carrier)
self.assertEqual(len(cassette.requests), 1)

0 comments on commit 8e93836

Please sign in to comment.