Request.py 3.7 KB
Newer Older
J
jiafeng 已提交
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
# -*- coding: utf-8 -*-
# @Time    : 2021/9/05 13:35
# @Author  : jiafeng.li
# @File    : Request.py

'''
封装request
'''
import json
import os
import random
import requests
import Common.Consts
from requests_toolbelt import MultipartEncoder

class Request:

    def __init__(self):
        pass

    def Post_Result(self, url, data, header):
        '''
        Post请求
        :param url:
        :param data:
        :return:
        '''
        if not url.startswith('http://'):
            url = '%s%s' % ('http://', url)
            print(url)

        data = json.dumps(data)
        #header = json.dumps(header)
        try:
            if data is None:
                response = requests.post(url=url, headers=header)
            else:
                response = requests.post(url=url, data=data, headers=header)

        except requests.RequestException as e:
            print('%s%s' % ('RequestException url: ', url))
            print(e)
            return ()
        except Exception as e:
            print('%s%s' % ('Exception url: ', url))
            print(e)
            return ()

        #time_consuming为响应时间,单位为毫秒
        time_consuming = response.elapsed.microseconds/1000
        #time_total为响应时间,单位为秒
        time_total = response.elapsed.total_seconds()

        Common.Consts.STRESS_LIST.append(time_consuming)

        response_dicts = dict()
        response_dicts['code'] = response.status_code

        try:
            response_dicts['body'] = response.json()
            Common.Consts.RESULT_LIST.append('True')
        except Exception as e:
            print(e)
            response_dicts['body'] = ''
            Common.Consts.RESULT_LIST.append('False')

        response_dicts['text'] = response.text
        response_dicts['time_consuming'] = time_consuming
        response_dicts['time_total'] = time_total

        #return json.dumps(response_dicts)
        return response_dicts

J
jiafeng 已提交
74
    def Get_Request(self, url, data, header, download_file=False):
J
jiafeng 已提交
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
        """
        Get请求
        :param url:
        :param data:
        :param header:
        :return:
        """
        if not url.startswith('http://'):
            url = '%s%s' % ('http://', url)
            print(url)

        try:
            if data is None:
                response = requests.get(url=url, headers=header)
            else:
                response = requests.get(url=url, params=data, headers=header)

        except requests.RequestException as e:
            print('%s%s' % ('RequestException url: ', url))
            print(e)
            return ()

        except Exception as e:
            print('%s%s' % ('Exception url: ', url))
            print(e)
            return ()

        time_consuming = response.elapsed.microseconds/1000
        time_total = response.elapsed.total_seconds()

        Common.Consts.STRESS_LIST.append(time_consuming)

        response_dicts = dict()
        response_dicts['code'] = response.status_code
        try:
            response_dicts['body'] = response.json()
        except Exception as e:
            print(e)
            response_dicts['body'] = ''
        response_dicts['text'] = response.text
        response_dicts['time_consuming'] = time_consuming
        response_dicts['time_total'] = time_total

J
jiafeng 已提交
118 119 120 121
        if download_file == True:
            return response
        else:
            return response_dicts
J
jiafeng 已提交
122 123 124 125 126 127 128 129 130 131 132

if __name__ == '__main__':
    url = 'http://c-api-sellerbasicservice.staging.shopeemobile.com/api/fulfillment/order/get_forder_from_oms'
    header = {'shop-id': '321239', 'region-id': 'SG', 'user-id': '321238', 'Content-Type': 'application/json'}
    data = 'order_id=85722908295387'

    a = Request()
    result = a.Get_Request(url, data, header)
    print(json.dumps(result))