gpt4 book ai didi

python - 替换路径 dredd hooks 中的 ID

转载 作者:太空宇宙 更新时间:2023-11-03 20:02:11 28 4
gpt4 key购买 nike

我有这个 dredd.yml 配置文件:

paths:
"/network":
get:
produces:
- application/json;charset=utf-8
responses:
'200':
description: Ok
schema:
"$ref": "#/definitions/NetworkResponse"
"/network/{id}":
get:
produces:
- application/json;charset=utf-8
parameters:
- name: id
in: path
description: id
required: true
type: string
enum:
- "net-76faddf092e62151"
responses:
'200':
description: Ok
schema:
"$ref": "#/definitions/NetworkIdResponse"

我想用 python dredd 钩子(Hook)替换路径中的 {id} 。我尝试过:

import json
import dredd_hooks as hooks

response_stash = {}

@hooks.after("Networks > ALL ")
def save_network_id_to_stash(transaction):
# save HTTP response with IDs to the stash
response_stash[transaction['id']] = transaction['id']
print(transaction['id'])

@hooks.before("Networks > ID")
def add_network_id_to_request(transaction):
parsed_body = json.loads(response_stash['Networks > ALL'])
network_id = parsed_body['id']
transaction['fullPath'] = transaction['fullPath'].replace('net-76faddf092e62151', network_id)

测试执行成功,但只有 1 个 id。第一个操作的答案输出一个包含对象的列表,其中 id 位于 ['id'] 中。如何替换路径中的{id}并对所有网络进行测试?

最佳答案

@hooks.before("Networks > ID > 200 > application/json;charset=utf-8)

这是钩子(Hook)名称的正确定义。

您可以看到所有变量 transaction['real']transaction['name'] 等钩子(Hook)函数中的print(transaction)

关于python - 替换路径 dredd hooks 中的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59175732/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com