重新封装方法主要是为了后期再输出alluer报告时更具可读性
一下是对方法重新进行封装的代码:
# -*- coding: utf-8 -*-
import allure
import requests
import jsonpath
import json
class ApiKey:
@allure.step(">>>>>>:开始发送Get请求")
def get(self, url, params=None, **kwargs):
"""
发送get请求
:param url:接口请求url
:param params: 拼接在url的参数
:param kwargs: 其它的参数
:return: 返回请求后的数据
"""
# print(">>>>>>:开始发送Get请求")
res = requests.get(url=url, params=params, **kwargs)
print("--->>响应数据为:", res.json())
return res
@allure.step(">>>>>>:开始发送Post请求")
def post(self, url, data=None, json=None, **kwargs):
"""
发送post请求
:param url: 接口请求url
:param data: data的请求数据
:param json: json的请求数据
:param kwargs: 其它的参数
:return: 返回请求后的数据
"""
# print(">>>>>>:开始发送Post请求")
res = requests.post(url=url, data=data, json=json, **kwargs)
print(">>>>>>:响应数据为:", res.json())
return res
@allure.step(">>>>>>:开始提取JsonPath数据")
def get_text(self, response, key):
"""
提取json当中的某个值
:param response: 需要提取的json数据,比如:{"msg":"登录成功"}
:param key: 对应要提取的jsonpath,比如: $.msg
:return: 返回提取数据之后的【第一个值】
"""
if isinstance(response, str):
# 是字符串,我就让它转一下类型
response = json.loads(response)
# print(">>>>>>:开始提取JsonPath数据")
value_list = jsonpath.jsonpath(response, key)
print(">>>>>>:提取数据为:", value_list[0])
return value_list[0]
# 函数的入口:main
if __name__ == '__main__':
# 1. 实例化对象:ApiKey
ak = ApiKey()
方便后期调用方法以及不同方法进行标记
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10亿数据,如何做迁移?
· 推荐几款开源且免费的 .NET MAUI 组件库
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· c# 半导体/led行业 晶圆片WaferMap实现 map图实现入门篇
· 易语言 —— 开山篇