Skip to content

Commit f38a4a1

Browse files
committed
Modify the format of the air waybill number
Supplementing other exception handling
1 parent 778de4b commit f38a4a1

File tree

7 files changed

+53
-5
lines changed

7 files changed

+53
-5
lines changed

README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,14 @@ import trackingmore
5151

5252
trackingmore.api_key = 'you api key'
5353

54-
couriers = trackingmore.courier.get_all_couriers()
54+
try:
55+
couriers = trackingmore.courier.get_all_couriers()
56+
print(couriers)
57+
except trackingmore.exception.TrackingMoreException as ce:
58+
print(ce)
59+
except Exception as e:
60+
print("other error:", e)
61+
5562
```
5663

5764
## Testing
@@ -89,7 +96,7 @@ try:
8996
couriers = trackingmore.courier.detect(params)
9097
print(couriers)
9198
except trackingmore.exception.TrackingMoreException as ce:
92-
print(ce)
99+
print(ce)
93100

94101
# Tracking number cannot be empty
95102
```
@@ -104,6 +111,9 @@ try:
104111
print(result)
105112
except trackingmore.exception.TrackingMoreException as ce:
106113
print(ce)
114+
except Exception as e:
115+
print("other error:", e)
116+
107117
```
108118

109119
##### Return a list of matched couriers based on submitted tracking number.
@@ -115,6 +125,8 @@ try:
115125
print(result)
116126
except trackingmore.exception.TrackingMoreException as ce:
117127
print(ce)
128+
except Exception as e:
129+
print("other error:", e)
118130
```
119131

120132
## Trackings
@@ -127,6 +139,8 @@ try:
127139
print(result)
128140
except trackingmore.exception.TrackingMoreException as ce:
129141
print(ce)
142+
except Exception as e:
143+
print("other error:", e)
130144
```
131145

132146
##### Get tracking results of multiple trackings.
@@ -141,6 +155,8 @@ try:
141155
print(result)
142156
except trackingmore.exception.TrackingMoreException as ce:
143157
print(ce)
158+
except Exception as e:
159+
print("other error:", e)
144160
```
145161

146162
##### Create multiple trackings (Max. 40 tracking numbers create in one call).
@@ -153,6 +169,8 @@ try:
153169
print(result)
154170
except trackingmore.exception.TrackingMoreException as ce:
155171
print(ce)
172+
except Exception as e:
173+
print("other error:", e)
156174
```
157175

158176
##### Update a tracking by ID.
@@ -165,6 +183,8 @@ try:
165183
print(result)
166184
except trackingmore.exception.TrackingMoreException as ce:
167185
print(ce)
186+
except Exception as e:
187+
print("other error:", e)
168188
```
169189

170190
##### Delete a tracking by ID.
@@ -176,6 +196,8 @@ try:
176196
print(result)
177197
except trackingmore.exception.TrackingMoreException as ce:
178198
print(ce)
199+
except Exception as e:
200+
print("other error:", e)
179201
```
180202

181203
##### Retrack expired tracking by ID.
@@ -187,6 +209,8 @@ try:
187209
print(result)
188210
except trackingmore.exception.TrackingMoreException as ce:
189211
print(ce)
212+
except Exception as e:
213+
print("other error:", e)
190214
```
191215
## Air Waybill
192216
##### Create an air waybill.
@@ -198,6 +222,8 @@ try:
198222
print(result)
199223
except trackingmore.exception.TrackingMoreException as ce:
200224
print(ce)
225+
except Exception as e:
226+
print("other error:", e)
201227
```
202228

203229
## Response Code

example/air_waybill_example.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ def create_an_air_waybill(params):
88
return result
99
except trackingmore.exception.TrackingMoreException as ce:
1010
print(ce)
11+
except Exception as e:
12+
print("other error:", e)
1113

1214
if __name__ == '__main__':
1315
params = {'awb_number': '235-69030430'}

example/courier_example.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ def get_all_couriers():
88
return result
99
except trackingmore.exception.TrackingMoreException as ce:
1010
print(ce)
11+
except Exception as e:
12+
print("other error:", e)
1113

1214
def detect(params):
1315
try:
1416
result = trackingmore.courier.detect(params)
1517
return result
1618
except trackingmore.exception.TrackingMoreException as ce:
1719
print(ce)
20+
except Exception as e:
21+
print("other error:", e)
1822

1923
if __name__ == '__main__':
2024
result = get_all_couriers()

example/tracking_example.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,53 @@ def create_tracking(params):
88
return result
99
except trackingmore.exception.TrackingMoreException as ce:
1010
print(ce)
11+
except Exception as e:
12+
print("other error:", e)
1113

1214
def get_tracking_results(params):
1315
try:
1416
result = trackingmore.tracking.get_tracking_results(params)
1517
return result
1618
except trackingmore.exception.TrackingMoreException as ce:
1719
print(ce)
20+
except Exception as e:
21+
print("other error:", e)
1822

1923
def batch_create_trackings(params):
2024
try:
2125
result = trackingmore.tracking.batch_create_trackings(params)
2226
return result
2327
except trackingmore.exception.TrackingMoreException as ce:
2428
print(ce)
29+
except Exception as e:
30+
print("other error:", e)
2531

2632
def update_tracking_by_id(id_string, params):
2733
try:
2834
result = trackingmore.tracking.update_tracking_by_id(id_string, params)
2935
return result
3036
except trackingmore.exception.TrackingMoreException as ce:
3137
print(ce)
38+
except Exception as e:
39+
print("other error:", e)
3240

3341
def delete_tracking_by_id(id_string):
3442
try:
3543
result = trackingmore.tracking.delete_tracking_by_id(id_string)
3644
return result
3745
except trackingmore.exception.TrackingMoreException as ce:
3846
print(ce)
47+
except Exception as e:
48+
print("other error:", e)
3949

4050
def retrack_tracking_by_id(id_string):
4151
try:
4252
result = trackingmore.tracking.retrack_tracking_by_id(id_string)
4353
return result
4454
except trackingmore.exception.TrackingMoreException as ce:
4555
print(ce)
56+
except Exception as e:
57+
print("other error:", e)
4658

4759
if __name__ == '__main__':
4860
params = {'tracking_number': '92612903029511573030094547','courier_code':'usps'}

trackingmore/air_waybill.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
from .const import ErrInvalidAirWaybillFormat, ErrMissingAwbNumber
24

35
from .exception import TrackingMoreException
@@ -11,7 +13,9 @@
1113
def create_an_air_waybill(params):
1214
if is_empty(params.get('awb_number')):
1315
raise TrackingMoreException(ErrMissingAwbNumber)
14-
if len(params['awb_number']) != 12:
16+
17+
pattern = r'^\d{3}[ -]?(\d{8})$'
18+
if not re.match(pattern, params.get('awb_number')):
1519
raise TrackingMoreException(ErrInvalidAirWaybillFormat)
1620
path = '/awb'
1721
response = make_request('POST', path, params)

trackingmore/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
ErrMissingAwbNumber = 'Awb number cannot be empty';
66
ErrMaxTrackingNumbersExceeded = 'Max. 40 tracking numbers create in one call';
77
ErrEmptyId = 'Id cannot be empty';
8-
ErrInvalidAirWaybillFormat = 'The air waybill number format is invalid and can only be 12 digits in length';
8+
ErrInvalidAirWaybillFormat = 'The air waybill number format is invalid';

trackingmore/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def get_request_header(apiKey):
2929
headers['Tracking-Api-Key'] = apiKey
3030
return headers
3131

32-
def make_request( method='GET', path ='', params=None):
32+
def make_request(method='GET', path='', params=None):
3333
try:
3434
url = get_request_url(path)
3535

0 commit comments

Comments
 (0)