Refactor PostAT.get_status() to handle exceptions when fetching shipment status.
Previously, an API call was made to get the shipment status using the tracking number. The code now incorporates exception handling to log any errors that occur during the fetching of the status. This change ensures that any potential errors are captured and logged for debugging purposes.
This commit is contained in:
parent
f7e9c76f64
commit
2eca302234
1 changed files with 19 additions and 13 deletions
|
@ -2,6 +2,7 @@ from ..classes.provider import BaseProvider
|
|||
from ..classes.database import Event
|
||||
|
||||
import json
|
||||
import logging
|
||||
|
||||
from dateutil.parser import parse
|
||||
from postat.classes.api import PostAPI
|
||||
|
@ -13,20 +14,25 @@ class PostAT(BaseProvider):
|
|||
|
||||
def get_status(self, tracking_number, carrier):
|
||||
api = PostAPI()
|
||||
status = api.get_shipment_status(tracking_number)
|
||||
shipment = status["data"]["einzelsendung"]
|
||||
events = shipment["sendungsEvents"]
|
||||
|
||||
for event in events:
|
||||
timestamp = event["timestamp"]
|
||||
py_timestamp = parse(timestamp)
|
||||
event_time = py_timestamp.strftime("%Y-%m-%d %H:%M:%S")
|
||||
yield Event(
|
||||
shipment_id=0,
|
||||
event_time=event_time,
|
||||
event_description=event["text"],
|
||||
raw_event=json.dumps(event),
|
||||
)
|
||||
try:
|
||||
status = api.get_shipment_status(tracking_number)
|
||||
shipment = status["data"]["einzelsendung"]
|
||||
events = shipment["sendungsEvents"]
|
||||
|
||||
for event in events:
|
||||
timestamp = event["timestamp"]
|
||||
py_timestamp = parse(timestamp)
|
||||
event_time = py_timestamp.strftime("%Y-%m-%d %H:%M:%S")
|
||||
yield Event(
|
||||
shipment_id=0,
|
||||
event_time=event_time,
|
||||
event_description=event["text"],
|
||||
raw_event=json.dumps(event),
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Error while fetching status: {e}")
|
||||
|
||||
def supported_carriers(self):
|
||||
return [
|
||||
|
|
Loading…
Reference in a new issue