Python和编程初学者
我正在编写一个用于与restapi交互的小型命令行Python应用程序。我正在尝试让函数为正确的JSON数据向templates模块发布请求调用。我不知道该怎么做
相关模块包括:
utilities/
utilities.py
condition_template.py
condition_template.py模块有一组返回JSON数据的函数。每个函数根据您试图创建的条件类型返回不同的数据。其中一个示例如下所示:
def alert_condition_user_score(app_id):
return {
"condition": {
"type": "condition_type",
"name": "condition_name",
"enabled": "true",
"entities": [
app_id,
],
"metric": "metric_name",
"terms": [
{
"duration": "10",
"operator": "below",
"priority": "warning",
"threshold": ".85",
"time_function": "all"
},
...<rest of json>
在我的utilities.py模块中,我有一些函数可以完成与API交互的“繁重工作”
def post_condition(api_key, app_id, policy_id):
headers = {
'X-api-key': api_key
}
api_url = '{0}/urlfor_api//{1}.json'.format(API_URL_BASE, policy_id)
response = requests.post(api_url, headers=headers, json=condition_template.alert_condition_user_score(app_id))
if response.status_code == 201:
return json.loads(response.content.decode('utf-8'))
else:
...<rest of function>
调用post\u condition函数时,我希望condition参数调用condition\u template.py模块中的适当函数,并将JSON数据返回到post函数的JSON输入
上面的代码可以工作,但静态设置为只加载user\u score函数。我需要能够从condition_template.py模块中选择其他选项
我尝试向post函数添加一个参数,并在post请求中包含该参数。但那没用。见下表:
def post_condition(api_key, app_id, policy_id, condition):
headers = {
'X-api-key': api_key
}
api_url = '{0}/urlfor_api//{1}.json'.format(API_URL_BASE, policy_id)
response = requests.post(api_url, headers=headers, json=condition_template.condition(app_id))
if response.status_code == 201:
return json.loads(response.content.decode('utf-8'))
else:
...<rest of function>
如何更改基于另一个函数的参数调用的函数
谢谢
目前没有回答
相关问题 更多 >
编程相关推荐