Python接口自动化之requests请求封装

Posted 软件测试小dao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python接口自动化之requests请求封装相关的知识,希望对你有一定的参考价值。

大家应该有体会,每个请求类型都写成单独的函数,代码复用性不强。

接下来将请求类型都封装起来,自动化用例都可以用这个封装的请求类进行请求,我们将常用的get、post请求封装起来。

import requests
class RequestHandler:
    def get(self, url, **kwargs):
        """封装get方法"""
        # 获取请求参数
        params = kwargs.get("params")
        headers = kwargs.get("headers")
        try:
            result = requests.get(url, params=params, headers=headers)
            return result
        except Exception as e:
            print("get请求错误: %s" % e)
    def post(self, url, **kwargs):
        """封装post方法"""
        # 获取请求参数
        params = kwargs.get("params")
        data = kwargs.get("data")
        json = kwargs.get("json")
        try:
            result = requests.post(url, params=params, data=data, json=json)
            return result
        except Exception as e:
            print("post请求错误: %s" % e)
    def run_main(self, method, **kwargs):
        """
        判断请求类型
        :param method: 请求接口类型
        :param kwargs: 选填参数
        :return: 接口返回内容
        """
        if method == 'get':
            result = self.get(**kwargs)
            return result
        elif method == 'post':
            result = self.post(**kwargs)
            return result
        else:
            print('请求接口类型错误')
if __name__ == '__main__':
    # 以下是测试代码
    # get请求接口
    url = 'https://api.apiopen.top/getJoke?page=1&count=2&type=video'
    res = RequestHandler().get(url)
    # post请求接口
    url2 = 'http://127.0.0.1:8000/user/login/'
    payload = {
        "username": "vivi",
        "password": "123456"
    }
    res2 = RequestHandler().post(url2,json=payload)
    print(res.json())
    print(res2.json())

请求结果如下:

{'code': 200, 'message': '成功!', 'result': [{'sid': '31004305', 'text': '羊:师傅,理个发,稍微修一下就行', 'type': 'video', 'thumbnail': 'http://wimg.spriteapp.cn/picture/2020/0410/5e8fbf227c7f3_wpd.jpg', 'video': 'http://uvideo.spriteapp.cn/video/2020/0410/5e8fbf227c7f3_wpd.mp4', 'images': None, 'up': '95', 'down': '1', 'forward': '0', 'comment': '25', 'uid': '23189193', 'name': '青川小舟', 'header': 'http://wimg.spriteapp.cn/profile/large/2019/12/24/5e01934bb01b5_mini.jpg', 'top_comments_content': None, 'top_comments_voiceuri': None, 'top_comments_uid': None, 'top_comments_name': None, 'top_comments_header': None, 'passtime': '2020-04-12 01:43:02'}, {'sid': '30559863', 'text': '机器人女友,除了不能生孩子,其他的啥都会,价格239000元', 'type': 'video', 'thumbnail': 'http://wimg.spriteapp.cn/picture/2020/0306/5e61a41172a1b_wpd.jpg', 'video': 'http://uvideo.spriteapp.cn/video/2020/0306/5e61a41172a1b_wpd.mp4', 'images': None, 'up': '80', 'down': '6', 'forward': '3', 'comment': '20', 'uid': '23131273', 'name': '水到渠成', 'header': 'http://wimg.spriteapp.cn/profile/large/2019/07/04/5d1d90349cd1a_mini.jpg', 'top_comments_content': '为游戏做的秀', 'top_comments_voiceuri': '', 'top_comments_uid': '10250040', 'top_comments_name': '不得姐用户', 'top_comments_header': 'http://wimg.spriteapp.cn/profile', 'passtime': '2020-04-11 20:43:49'}]}
{'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6InZpdmkiLCJleHAiOjE1ODY4NTc0MzcsImVtYWlsIjoidml2aUBxcS5jb20ifQ.k6y0dAfNU2o9Hd9LFfxEk1HKgczlQfUaKE-imPfTsm4', 'user_id': 1, 'username': 'vivi'}

这样就完美了吗,no,no,no。以上代码痛点如下:

代码量大:只是封装了get、post请求,加上其他请求类型,代码量较大;

缺少会话管理:请求之间如何保持会话状态。

我们再来回顾下get、post等请求源码,看下是否有啥特点。

get请求源码:

def get(url, params=None, **kwargs):
    r"""Sends a GET request.
    :param url: URL for the new :class:`Request` object.
    :param params: (optional) Dictionary, list of tuples or bytes to send
        in the query string for the :class:`Request`.
    :param \\*\\*kwargs: Optional arguments that ``request`` takes.
    :return: :class:`Response <Response>` object
    :rtype: requests.Response
    """
    kwargs.setdefault('allow_redirects', True)
    return request('get', url, params=params, **kwargs)
post请求源码:

def post(url, data=None, json=None, **kwargs):
    r"""Sends a POST request.
    :param url: URL for the new :class:`Request` object.
    :param data: (optional) Dictionary, list of tuples, bytes, or file-like
        object to send in the body of the :class:`Request`.
    :param json: (optional) json data to send in the body of the :class:`Request`.
    :param \\*\\*kwargs: Optional arguments that ``request`` takes.
    :return: :class:`Response <Response>` object
    :rtype: requests.Response
    """
    return request('post', url, data=data, json=json, **kwargs)

仔细研究下,发现get、post请求返回的都是request函数。

既然这样,我们再来研究下request源码:

def request(method, url, **kwargs):
    """Constructs and sends a :class:`Request <Request>`.
    :param method: method for the new :class:`Request` object.
    :param url: URL for the new :class:`Request` object.
    :param params: (optional) Dictionary, list of tuples or bytes to send
        in the query string for the :class:`Request`.
    :param data: (optional) Dictionary, list of tuples, bytes, or file-like
        object to send in the body of the :class:`Request`.
    :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.
    :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
    :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
    :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
        ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
        or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
        defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
        to add for the file.
    :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
    :param timeout: (optional) How many seconds to wait for the server to send data
        before giving up, as a float, or a :ref:`(connect timeout, read
        timeout) <timeouts>` tuple.
    :type timeout: float or tuple
    :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
    :type allow_redirects: bool
    :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
    :param verify: (optional) Either a boolean, in which case it controls whether we verify
            the server's TLS certificate, or a string, in which case it must be a path
            to a CA bundle to use. Defaults to ``True``.
    :param stream: (optional) if ``False``, the response content will be immediately downloaded.
    :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
    :return: :class:`Response <Response>` object
    :rtype: requests.Response
    Usage::
      >>> import requests
      >>> req = requests.request('GET', 'https://httpbin.org/get')
      <Response [200]>
    """
    # By using the 'with' statement we are sure the session is closed, thus we
    # avoid leaving sockets open which can trigger a ResourceWarning in some
    # cases, and look like a memory leak in others.
    with sessions.Session() as session:
        return session.request(method=method, url=url, **kwargs)

源码看起来很长,其实只有三行,大部分是代码注释。从源码中可以看出,不管是get还是post亦或其他请求类型,最终都是调用request函数。

既然这样,我们可以不像之前那样,在类内定义get方法、post方法,而是定义一个通用的方法,直接调用request函数即可。看起来有点绕,用代码实现就清晰了。

mport requests
class RequestHandler:
    def __init__(self):
        """session管理器"""
        self.session = requests.session()
    def visit(self, method, url, params=None, data=None, json=None, headers=None, **kwargs):
        return self.session.request(method,url, params=params, data=data, json=json, headers=headers,**kwargs)
    def close_session(self):
        """关闭session"""
        self.session.close()
if __name__ == '__main__':
    # 以下是测试代码
    # post请求接口
    url = 'http://127.0.0.1:8000/user/login/'
    payload = {
        "username": "vivi",
        "password": "123456"
    }
    req = RequestHandler()
    login_res = req.visit("post", url, json=payload)
    print(login_res.text)

响应结果:

{
    "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6InZpdmkiLCJleHAiOjE1ODY4Njk3ODQsImVtYWlsIjoidml2aUBxcS5jb20ifQ.OD4HIv8G0HZ_RCk-GTVAZ9ADRjwqr3o0E32CC_2JMLg",
    "user_id": 1,
    "username": "vivi"
}

这次请求封装简洁实用,当然小伙伴们也可以根据自己的需求自行封装。

总结:本文主要通过源码分析,总结出一套简洁的请求类封装。

在这里还是要推荐下我自己建的Python自动化学习群:644956177 ,群里都是学Python自动化测试的,如果你正在学习Python ,小编欢迎你加入,大家都是测试开发党,不定期分享干货(只有Python软件测试相关的),包括我自己整理的一份2021最新的Python自动化资料。

如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一键三连哦!

以上是关于Python接口自动化之requests请求封装的主要内容,如果未能解决你的问题,请参考以下文章

Python接口自动化之requests请求封装

Python接口自动化之request请求封装

Python接口自动化之request请求封装

Python接口自动化之浅析requests模块get请求

Python接口自动化之浅析requests模块get请求

Python接口自动化之unittest单元测试