11from typing import List , Tuple
22from pyups .common import RequestType , TransactionReferenceType
3- from pyups .track_web_service_schema import TrackRequest , ShipmentType , ActivityType
3+ from pyups .track_web_service_schema import TrackRequest , ShipmentType , ActivityType , AddressType
44from purplship .core .utils import export , Serializable , Element , format_date , format_time
5- from purplship .core .utils .soap import apply_namespaceprefix , create_envelope , Envelope
5+ from purplship .core .utils .soap import apply_namespaceprefix , create_envelope , Envelope , build
66from purplship .core .models import (
77 TrackingEvent ,
88 TrackingRequest ,
@@ -24,16 +24,13 @@ def parse_track_response(
2424
2525
2626def _extract_tracking (shipment_node : Element , settings : Settings ) -> TrackingDetails :
27- track_detail = ShipmentType ()
28- track_detail .build (shipment_node )
29- activity_nodes = shipment_node .xpath (".//*[local-name() = $name]" , name = "Activity" )
30-
31- def build_activity (node ) -> ActivityType :
32- activity = ActivityType ()
33- activity .build (node )
34- return activity
27+ track_detail = build (ShipmentType , shipment_node )
28+ activities = [
29+ build (ActivityType , node )
30+ for node in
31+ shipment_node .xpath (".//*[local-name() = $name]" , name = "Activity" )
32+ ]
3533
36- activities : List [ActivityType ] = list (map (build_activity , activity_nodes ))
3734 return TrackingDetails (
3835 carrier_name = settings .carrier_name ,
3936 carrier_id = settings .carrier_id ,
@@ -42,21 +39,28 @@ def build_activity(node) -> ActivityType:
4239 map (
4340 lambda a : TrackingEvent (
4441 date = format_date (a .Date , "%Y%m%d" ),
45- time = format_time (a .Time , "%H%M%S" ),
46- code = a .Status .Code if a .Status else None ,
42+ description = a .Status .Description if a .Status else None ,
4743 location = (
48- a .ActivityLocation .Address .City
49- if a .ActivityLocation and a .ActivityLocation .Address
50- else None
44+ _format_location (a .ActivityLocation .Address )
45+ if a .ActivityLocation is not None and a .ActivityLocation .Address is not None else None
5146 ),
52- description = a .Status .Description if a .Status else None ,
47+ time = format_time (a .Time , "%H%M%S" ),
48+ code = a .Status .Code if a .Status else None ,
5349 ),
5450 activities ,
5551 )
5652 ),
5753 )
5854
5955
56+ def _format_location (address : AddressType ) -> str :
57+ return ", " .join ([
58+ location for location in [
59+ address .City , address .StateProvinceCode , address .CountryCode
60+ ] if location is not None
61+ ])
62+
63+
6064def track_request (
6165 payload : TrackingRequest , settings : Settings
6266) -> Serializable [List [Envelope ]]:
0 commit comments