-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfedex_api.py
185 lines (150 loc) · 5.94 KB
/
fedex_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import requests
import json
fedex_url="https://apis-sandbox.fedex.com/"
def authenticate():
url = fedex_url+"oauth/token"
input={
'grant_type':'client_credentials',
'client_id':'l71845f33ec3b24082a32c651af161c216',
'client_secret':'d905ec8acdf646269ef8ef3c66260090'
}
payload = input # 'input' refers to JSON Payload
headers = {
'Content-Type': "application/x-www-form-urlencoded"
}
response = requests.request("POST", url, data=payload, headers=headers)
response_dict=json.loads(response.text)
return response_dict['access_token']
def validate_address(streetLines,city,state,postalcode,countrycode):
access_token=authenticate()
url = fedex_url+"address/v1/addresses/resolve"
address_input={'addressesToValidate':[
{
'address':{
'streetLines':[''+streetLines+''],
'city':''+city+'',
'stateOrProvinceCode':''+state+'',
'postalCode':''+postalcode+'',
'countryCode':''+countrycode+''
}
}
]}
payload = json.dumps(address_input) # 'input' refers to JSON Payload
headers = {
'x-customer-transaction-id':'123',
'Content-Type': "application/json",
'X-locale': "en_US",
'Authorization': 'Bearer '+access_token
}
response = requests.request("POST", url, data=payload, headers=headers)
response_dict=json.loads(response.text)
#find if any error in response before reading
if 'errors' in response_dict:
return response_dict['errors'][0]['message']
else:
#check if any resolved addresses are returned
if response_dict['output']['resolvedAddresses']:
add_dict=response_dict['output']['resolvedAddresses'][0]
#check if any problem in address
if len(add_dict['customerMessages'])>0:
return str(add_dict['customerMessages'][0]['message'])
else:
return "valid"
def location_check(sender,maxResults):
access_token=authenticate()
url = fedex_url+"location/v1/locations"
company_name=''
company_address=''
location_payload={
'locationsSummaryRequestControlParameters' :
{
'distance':{
'units':'MI',
'value':'4'
},
'maxResults':''+str(maxResults)+'',
'customerTransactionId':'DroneID'
},
'location':
{
'address':{
'streetLines':[''+str(sender['street'])+''],
'city':''+sender['city']+'',
'stateOrProvinceCode':''+sender['stateorprovince']+'',
'postalCode':''+sender['postalcode']+'',
'countryCode':''+sender['countrycode']+''
}
}
}
payload = json.dumps(location_payload) # 'input' refers to JSON Payload
headers = {
'x-customer-transaction-id':'123',
'Content-Type': "application/json",
'X-locale': "en_US",
'Authorization': 'Bearer '+access_token
}
response = requests.request("POST", url, data=payload, headers=headers)
response_dict=json.loads(response.text)
if 'output' in response_dict:
all_locations=response_dict['output']['locationDetailList']
for items in all_locations:
name= items['contactAndAddress']['contact']
company_name=name['companyName']
address=items['contactAndAddress']['address']
company_address=str(address['streetLines'][0]) +' '+str(address['city'])+' '+str(address['stateOrProvinceCode'])+' '+str(address['postalCode'])+' '+str(address['countryCode'])
location=company_name.replace('&','%26')+' '+company_address
# print(location)
return str(location)
def service_availability(sender,receiver):
access_token=authenticate()
url = fedex_url+"availability/v1/packageandserviceoptions/"
requestshipment={
'requestedShipment' :
{
'shipper':{
'address':
{
'streetLines':[''+str(sender['street'])+''],
'city':''+sender['city']+'',
'stateOrProvinceCode':''+sender['stateorprovince']+'',
'postalCode':''+sender['postalcode']+'',
'countryCode':''+sender['countrycode']+''
}
},
'recipients':[{
'address':
{
'streetLines':[''+str(receiver['street'])+''],
'city':''+receiver['city']+'',
'stateOrProvinceCode':''+receiver['stateorprovince']+'',
'postalCode':''+receiver['postalcode']+'',
'countryCode':''+receiver['countrycode']+''
}
}]
}
}
payload = json.dumps(requestshipment) # 'input' refers to JSON Payload
headers = {
'x-customer-transaction-id':'123',
'Content-Type': "application/json",
'X-locale': "en_US",
'Authorization': 'Bearer '+access_token
}
# TODO CHECK IF THIS IS BETTER OPTION FEDEX_EXPRESS
response = requests.request("POST", url, data=payload, headers=headers)
priority_options=0
response_dict=json.loads(response.text)
if 'output' in response_dict:
all_options=response_dict['output']['packageOptions']
for items in all_options:
if items['serviceType']['key']=='FEDEX_FREIGHT_PRIORITY':
priority_options+=1
return priority_options
sender={
'street':['3600 Lancaster Avenue'],
'city':'Philadelphia',
'stateorprovince':'PA',
'postalcode':'19104',
'countrycode':'US'
}
# validate_address('3600 Lancaster Avenue',)