requests库的session封装

Posted by Tesla9527 on April 28, 2021
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
import requests
import json


class HttpClient:
    def __init__(self, api_ip, api_headers):
        self.__session = requests.Session()
        self.__api_ip = api_ip
        self.__headers = api_headers

    def update_headers(self, api_headers):
        self.__headers.update(api_headers)

    def get(self, path, **kwargs):
        return self.__request(path, 'GET', **kwargs)

    def post(self, path, **kwargs):
        return self.__request(path, 'POST', **kwargs)

    def put(self, path, **kwargs):
        return self.__request(path, "PUT", **kwargs)

    def delete(self, path, **kwargs):
        return self.__request(path, "DELETE", **kwargs)

    def patch(self, path, **kwargs):
        return self.__request(path, "PATCH", **kwargs)

    def __request(self, path, method, **kwargs):
        url = self.__api_ip + path
        headers = kwargs.get("headers")
        if headers:
            self.__headers.update(headers)
        kwargs.update({"headers": self.__headers})
        if method == "GET":
            rp = self.__session.get(url, **kwargs)
        elif method == "POST":
            rp = self.__session.post(url, **kwargs)
        elif method == "PUT":
            rp = self.__session.put(url, **kwargs)
        elif method == "DELETE":
            rp = self.__session.delete(url, **kwargs)
        elif method == "PATCH":
            rp = self.__session.patch(url, **kwargs)
        print(f'请求url:{rp.request.url}')
        print(f'请求头:{rp.request.headers}')
        print(f'请求方法:{rp.request.method}')
        body = rp.request.body
        body = body.encode("utf-8").decode("unicode_escape") if body is not None else None
        print(f'请求体:{body}\n')
        print(f'响应码:{rp.status_code}')
        try:
            rp_data = json.loads(rp.text) if rp.text != '' else ''
            print(f'响应体:{rp_data}')
            return(rp.status_code, rp_data)
        except:
            return(rp.status_code, rp.content)

'''需要定义全局变量的放在这里,最好定义一个初始值'''
class global_var():
    base_url = 'http://qq.com'
    headers = {'Content-Type': 'application/json'}
    session = HttpClient(base_url, headers)

# 对于每个全局变量,都需要定义get_value和set_value接口,用来外部调用
def set_session(headers):
    global_var.session.update_headers(headers)
def get_session():
    return global_var.session