编程语言
首页 > 编程语言> > python写运单增改查接口完整代码

python写运单增改查接口完整代码

作者:互联网

import requests
import json
from urllib import parse


class HttpWayBillRquest:
    '''运单的增改查'''

    def access_token(self):
        '''获取token'''
        url = ' http://xxxxxxxxx.com'
        username= 'c5be965fc46342a92520e'
        password= '0c676603352d8'
        res = requests.get(url, auth=(username, password))
        # print('res.text的结果为:\n', res.text)
        res.text_json = json.loads(res.text)
        print('access_token的结果为:\n', res.text_json)
        datas_token = res.text_json['data']
        # print( datas_token)
        token_data = datas_token['data']['access_token']
        token_type = datas_token['data']['token_type']
        return token_type,token_data


    def createwaybill(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 = json.dumps(datas_create)
        print("申请运单请求头:\n", header)
        print("申请运单请求参数:\n", datas_json)
        res = requests.post(url=create_url, data=datas_json, headers=header)
        print('申请运单响应头:\n', res.headers)
        print('申请运单响应体:\n', res.text)

    def updatewaybill1(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 = json.dumps(data_update1)
        res = requests.put(url=update_url1 + delivery_id, data=datas_json, headers=header)
        print('更新1条运单响应头:\n',res.headers)
        print('更新1条运单响应体:\n',res.text)

    def updatewaybill2(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 = json.dumps(data_update2)
        res = requests.put(url=update_url2, data=datas_json, headers=header)
        print('更新多条运单响应头:\n',res.headers)
        print('更新多条运单响应体:\n',res.text)

    def findwaybill1(self,delivery_id,token_type,token_data):
        '''查询1条运单信息'''
        finds_url1 = 'http://test.xapi.xborderpay.com/v3/delivery/'
        header = {
            'Authorization': token_type + ' ' + token_data,
            'Content-Type': 'application/json; charset=UTF-8'
        }

        url_finds1 = finds_url1 + delivery_id
        print('查询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)

    def findwaybill2(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_data
        print('多条件查询运单请求头:', header)
        print('多条件查询运单请求url:', url_finds2)

        res = requests.get(url=url_finds2, headers=header)
        print('多条件查询运单响应头:', res.headers)
        print('多条件查询运单响应体:', res.text)

 

 

标签:运单,python,res,json,增改查,token,print,data
来源: https://www.cnblogs.com/kite123/p/11871302.html