接口自动化定义BaseRquest类及其继承

import requests

from utils.logger import get_logger

logger = get_logger(__file__)


class BaseRequest:
    def __init__(self):
        self.url = None
        self.method = None

    def request(self, **kwargs):
        """
        调用request发送http请求
        :param json: http请求入参为json
        :param params: http请求入参为form
        :param headers: http请求头
        :return: requests响应
        """
        req_json = kwargs.get('json', None)
        req_params = kwargs.get('params', None)
        headers = kwargs.get('headers', None)
        cookies = kwargs.get('cookies', None)
        logger.info(f'【Request URL】 {self.url}')
        if req_json:
            logger.info(f'<------ Request ------> {req_json}')
            res = requests.request(url=self.url, method=self.method, json=req_json, headers=headers, cookies=cookies)
        elif req_params:
            logger.info(f'<------ Request ------> {req_params}')
            res = requests.request(url=self.url, method=self.method, params=req_params, headers=headers,
                                   cookies=cookies)
        else:
            res = requests.request(url=self.url, method=self.method, headers=headers, cookies=cookies)
        logger.info(f'【Response Code】 {res.status_code}')
        logger.info(f'======> Response <====== {res.text}')
        # logger.info(f'======> Headers <====== {res.headers}')

        return res
from config.settings import host
from interface.base_request import BaseRequest


class Login(BaseRequest):
    """
    登录接口
    """

    def __init__(self):
        super().__init__()
        self.url = f'{host}/Login'
        self.method = 'POST'
        # 请求头实例
        self.data = {
            "userAccount": "admin",
            "userPwd": "XXXX"
        }
if __name__ == '__main__':
l = Login()
cookies = l.request(json=l.data).cookies
 

 

posted @ 2023-11-03 14:48  偷月  阅读(31)  评论(0编辑  收藏  举报