importrequestsimportjsonfrom urllib importparseclassHttpWayBillRquest:'''运单的增改查'''
defaccess_token(self):'''获取token'''url= 'http://xxxxxxxxx.com'username= '12333'password= '12334566'res_json= requests.get(url, auth=(username, password)).json()print('access_token的结果为:\n', res_json)
datas_token= res_json['data']
token_data = datas_token['data']['access_token']
token_type= datas_token['data']['token_type']returntoken_type,token_datadefcreatewaybill(self, datas_create,token_type,token_data):'''创建单条或多条运单申请'''create_url= 'http://xxxxxxxxx.com'header={'Authorization': token_type+ ' ' +token_data,'Content-Type': 'application/json; charset=UTF-8'}
datas_json=eval(datas_create)res= requests.post(url=create_url, json=datas_json, headers=header)print('申请运单响应头:\n', res.headers)print('申请运单响应体:\n', res.text)defupdatewaybill1(self, delivery_id, data_update1,token_type,token_data):'''修改一条运单信息'''update_url1= "http://xxxxxxxxx.com''
header ={'Authorization': token_type + ' ' +token_data,'Content-Type': 'application/json; charset=UTF-8'}
datas_json=eval(data_update1)
res= requests.put(url=update_url1 + delivery_id, json=datas_json, headers=header)print('更新1条运单响应头:\n',res.headers)print('更新1条运单响应体:\n',res.text)defupdatewaybill2(self,data_update2,token_type,token_data):'''更新多条运单信息'''update_url2= 'http://xxxxxxxxx.com/'header={'Authorization': token_type + ' ' +token_data,'Content-Type': 'application/json; charset=UTF-8'}
datas_json=eval(data_update2)
res= requests.put(url=update_url2, json=datas_json, headers=header)print('更新多条运单响应头:\n',res.headers)print('更新多条运单响应体:\n',res.text)deffindwaybill1(self,delivery_id,token_type,token_data):'''查询1条运单信息'''finds_url1= 'http://xxxxxxx.com/'header={'Authorization': token_type + ' ' +token_data,'Content-Type': 'application/json; charset=UTF-8'}
url_finds1= finds_url1 +delivery_idprint('查询1条运单请求头:', header)print('查询1条运单请求url:', url_finds1)
res= requests.get(url=url_finds1, headers=header)print('查询1条运单响应头:\n', res.headers)print('查询1条运单响应体:\n',res.text)deffindwaybill2(self,find_data,token_type,token_data):'''多条件查找运单信息'''finds_url2= "http://xxxxxxxxx.com''
header ={'Authorization': token_type + ' ' +token_data,'Content-Type': 'application/json; charset=UTF-8'}
encode_data=parse.urlencode(find_data)
url_finds2= finds_url2 + '?' +encode_dataprint('多条件查询运单请求头:', header)print('多条件查询运单请求url:', url_finds2)
res= requests.get(url=url_finds2, headers=header)print('多条件查询运单响应头:', res.headers)print('多条件查询运单响应体:', res.text)
主代码:waybill.py
(1)获取token
from waybill.WayBill importHttpWayBillRquest
Waybillrquest=HttpWayBillRquest()
Token=Waybillrquest.access_token()print('token为:{0}\ntoken_type为:{1}\n'.format(Token[1],Token[0]))
结果为:
(2)创建运单信息
importrandomimporttimefrom waybill.WayBill importHttpWayBillRquest'''创建单笔'''datas_create=[{'order_id':'157179719834','delivery_type':'EMS','delivery_number':random.randint(10000000,9999999999999999),'remarks':time.strftime("%Y-%m-%d",time.localtime(time.time()))
}]#'''创建多笔'''#datas_create = [#{'order_id': '1571298310',#'delivery_type': 'EMS',#'delivery_number': random.randint(10000000, 9999999999999999),#'remarks': '2019-11-12', },#{'order_id': '1571451612',#'delivery_type': 'EMS',#'delivery_number': random.randint(10000000, 9999999999999999),#'remarks': '2019-11-12', }#]
Waybillrquest=HttpWayBillRquest()
Token=Waybillrquest.access_token()
Create= Waybillrquest.createwaybill(datas_create,Token[0],Token[1])
测试结果为:
access_token的结果为:
{'data': {'result': 'Request is Success', 'code': '0000', 'remarks': '请求成功', 'data': {'token_type': 'Bearer', 'expires_in': 7071, 'access_token': 'b75fd73216cc1191f1acd06b0be557ffea903fa20f561'}}, 'message': 'OK', 'status': '200'}
申请运单请求头:
{'Content-Type': 'application/json; charset=UTF-8', 'Authorization': 'Bearer b75fd73216cc1191f1acd06b0be557ffea903fa20f561'}
申请运单请求参数:
[{"delivery_type": "EMS", "order_id": "1571797198304084", "remarks": "2019-11-16", "delivery_number": 2971735057491385}]
申请运单响应头:
{'Set-Cookie': 'PHPSESSID=2103irfm70b3qqbdk; path=/, PHPSESSID=2103irfm70b3qqbdk; expires=Sat, 16-Nov-2019 04:54:42 GMT; Max-Age=1440; path=/', 'Keep-Alive': 'timeout=60', 'X-Powered-By': 'PHP/5.6.40', 'Server': 'Tengine', 'Pragma': 'no-cache', 'Transfer-Encoding': 'chunked', 'Content-Encoding': 'gzip', 'Expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'Connection': 'keep-alive', 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', 'Date': 'Sat, 16 Nov 2019 04:30:42 GMT'}
申请运单响应体:
{"status":"200","message":"OK","data":{"code":"0000","result":"Request is Success","remarks":"请求成功","data":[{"order_id":"157179719834","delivery_id":"1573878642173493","error":false,"desc":""}]}}
(3)更新单条运单信息
importrandomimporttimefrom waybill.WayBill importHttpWayBillRquest
delivery_id= '1573872940367' #原始运单号
data_update1={'order_id': '15714523399','delivery_type': 'TNT','delivery_number': random.randint(10000000, 9999999999999999),'remarks':time.strftime("%Y-%m-%d",time.localtime(time.time()))
}
Waybillrquest=HttpWayBillRquest()
Token=Waybillrquest.access_token()
Update1= Waybillrquest.updatewaybill1(delivery_id, data_update1,Token[0],Token[1])
测试结果为:
ccess_token的结果为:
{'status': '200', 'message': 'OK', 'data': {'remarks': '请求成功', 'code': '0000', 'result': 'Request is Success', 'data': {'token_type': 'Bearer', 'expires_in': 6839, 'access_token': 'b75fd73216cc1191f1acd06b0be557ffea903fa20f561'}}}
更新1条运单响应头:
{'Cache-Control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', 'Connection': 'keep-alive', 'Expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'Pragma': 'no-cache', 'X-Powered-By': 'PHP/5.6.40', 'Content-Encoding': 'gzip', 'Content-Type': 'text/html; charset=utf-8', 'Server': 'Tengine', 'Date': 'Sat, 16 Nov 2019 04:34:33 GMT', 'Transfer-Encoding': 'chunked', 'Set-Cookie': 'PHPSESSID=dmr786qduoqqchh; path=/, PHPSESSID=dmr786qduoqqchh; expires=Sat, 16-Nov-2019 04:58:33 GMT; Max-Age=1440; path=/', 'Keep-Alive': 'timeout=60'}
更新1条运单响应体:
{"status":"200","message":"OK","data":{"code":"0000","result":"Request is Success","remarks":"请求成功","data":{"delivery_id":"1573872940367","error":false,"desc":""}}}
(4)更新多条运单信息
importtimefrom waybill.WayBill importHttpWayBillRquest
data_update2=[
{"delivery_id": "1573461404", "delivery_type": "EMS", "delivery_number": "1573461404","remarks": time.strftime("%Y-%m-%d",time.localtime(time.time()))},
{"delivery_id": "1573461455", "delivery_type": "EMS", "delivery_number": "1573461455","remarks": time.strftime("%Y-%m-%d",time.localtime(time.time()))},
{"delivery_id": "1573461530", "delivery_type": "EMS", "delivery_number": "1573461530","remarks": time.strftime("%Y-%m-%d",time.localtime(time.time()))}
]
Waybillrquest=HttpWayBillRquest()
Token=Waybillrquest.access_token()
Update2= Waybillrquest.updatewaybill2(data_update2,Token[0],Token[1])
测试结果为:
access_token的结果为:
{'data': {'remarks': '请求成功', 'result': 'Request is Success', 'data': {'expires_in': 6634, 'access_token': 'b75fd73216cc1191f1acd06b0be557ffea903fa20f561', 'token_type': 'Bearer'}, 'code': '0000'}, 'message': 'OK', 'status': '200'}
更新多条运单响应头:
{'X-Powered-By': 'PHP/5.6.40', 'Date': 'Sat, 16 Nov 2019 04:37:58 GMT', 'Server': 'Tengine', 'Content-Encoding': 'gzip', 'Expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'Pragma': 'no-cache', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Cache-Control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', 'Content-Type': 'text/html; charset=utf-8', 'Keep-Alive': 'timeout=60', 'Set-Cookie': 'PHPSESSID=754619tfmfep2lqmoh; path=/, PHPSESSID=754619tfmfep2lqmoh; expires=Sat, 16-Nov-2019 05:01:58 GMT; Max-Age=1440; path=/'}
更新多条运单响应体:
{"status":"200","message":"OK","data":{"code":"0000","result":"Request is Success","remarks":"请求成功","data":[{"delivery_id":"1573461404","error":false,"desc":""},{"delivery_id":"1573461455","error":false,"desc":""},{"delivery_id":"1573461530","error":false,"desc":""}]}}
(5)查找一条运单信息
from waybill.WayBill importHttpWayBillRquest
delivery_id= "15731193"Waybillrquest=HttpWayBillRquest()
Token=Waybillrquest.access_token()Find1= Waybillrquest.findwaybill1(delivery_id,Token[0],Token[1])
测试结果为:
access_token的结果为:
{'message': 'OK', 'status': '200', 'data': {'result': 'Request is Success', 'code': '0000', 'remarks': '请求成功', 'data': {'expires_in': 6567, 'token_type': 'Bearer', 'access_token': 'b75fd73216cc1191f1acd06b0be557'}}}
查询1条运单请求头: {'Authorization': 'Bearer b75fd73216cc1191f1acd06b0be557', 'Content-Type': 'application/json; charset=UTF-8'}
查询1条运单请求url: http://test.xapi.xborderpay.com/v3/delivery/1573119358
查询1条运单响应头:
{'Cache-Control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', 'Content-Type': 'text/html; charset=utf-8', 'Server': 'Tengine', 'Connection': 'keep-alive', 'Set-Cookie': 'PHPSESSID=npmdjj39gmue5; path=/, PHPSESSID=npmdjj39gmue5; expires=Sat, 16-Nov-2019 05:03:05 GMT; Max-Age=1440; path=/', 'Pragma': 'no-cache', 'Content-Encoding': 'gzip', 'Transfer-Encoding': 'chunked', 'Date': 'Sat, 16 Nov 2019 04:39:05 GMT', 'X-Powered-By': 'PHP/5.6.40', 'Expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'Keep-Alive': 'timeout=60'}
查询1条运单响应体:
{"status":"200","message":"OK","data":{"code":"0000","result":"Request is Success","remarks":"请求成功","data":{"delivery_id":"15731193","order_id":"15731193027","date_pay":"2019-11-07 17:35:03","ship_name":"Julia Patterson","currency":"USD","amount":"66.66","delivery_type":"EMS","delivery_number":"1573119358","remarks":"2019-11-7","status":"0","is_checked":"1","is_activated":"1","by_added":"system"}}}
(6)多条件查找运单信息
from waybill.WayBill importHttpWayBillRquest
find_data={"is_checked": "1","status": "1","is_activated": "0","start_date": "2019-10-09","end_date": "2019-11-08"}
Waybillrquest=HttpWayBillRquest()
Token=Waybillrquest.access_token()
Find2= Waybillrquest.findwaybill2(find_data,Token[0],Token[1])
测试结果为:
access_token的结果为:
{'status': '200', 'message': 'OK', 'data': {'remarks': '请求成功', 'code': '0000', 'data': {'expires_in': 6377, 'token_type': 'Bearer', 'access_token': 'b75fd73216cc1191f1acd06b0be557ffea903fa20f'}, 'result': 'Request is Success'}}
多条件查询运单请求头: {'Authorization': 'Bearer b75fd73216cc1191f1acd06b0be557ffea903f', 'Content-Type': 'application/json; charset=UTF-8'}
多条件查询运单请求url: http://xxxxxx?status=1&start_date=2019-10-09&end_date=2019-11-08&is_activated=0&is_checked=1
多条件查询运单响应头: {'Content-Type': 'text/html; charset=utf-8', 'Set-Cookie': 'PHPSESSID=qle4d0m4jhtc5onr6; path=/, PHPSESSID=qle4d0m4jhtc5onr6; expires=Sat, 16-Nov-2019 05:06:15 GMT; Max-Age=1440; path=/', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'X-Powered-By': 'PHP/5.6.40', 'Pragma': 'no-cache', 'Cache-Control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', 'Content-Encoding': 'gzip', 'Server': 'Tengine', 'Keep-Alive': 'timeout=60', 'Expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'Date': 'Sat, 16 Nov 2019 04:42:15 GMT'}
多条件查询运单响应体: {"status":"200","message":"OK","data":{"code":"0000","result":"Request is Success","remarks":"请求成功","data":{"total":"0","data":[]}}}