Python 如何使用Connexion在Flask中的GET请求中接受数组参数
我正在使用connexion通过Flask创建RESTAPI。目前,我正在传入一个标识符Python 如何使用Connexion在Flask中的GET请求中接受数组参数,python,flask,swagger,connexion,Python,Flask,Swagger,Connexion,我正在使用connexion通过Flask创建RESTAPI。目前,我正在传入一个标识符 /read/maa_valid_product_id/{drug_product_id}: get: operationId: registrations.read_maa_valid_product_id tags: - Marketing Applications summary: Read the entire list of non-pas
/read/maa_valid_product_id/{drug_product_id}:
get:
operationId: registrations.read_maa_valid_product_id
tags:
- Marketing Applications
summary: Read the entire list of non-passive marketing application registrations for a specified drug_product_id, which have a valid authorisation status
description: Read the entire list of non-passive marketing application registrations for a specified drug_product_id, which have a valid authorisation status
parameters:
- in: path
name: drug_product_id
required: true
schema:
type: integer
description: Numeric ID of the user to get
- in: query
name: length
required: false
schema:
type: integer
description: Numeric ID of the user to get
- in: query
name: offset
required: false
schema:
type: integer
description: Numeric ID of the user to get
responses:
'200':
description: Successfully commpleted the list operation for non-passive valid MAAs, for the specified drug_product_id
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/marketing_application'
我需要根据表中的选择传入一个id值数组。这可能吗
如果可能,我将如何访问阵列,现有的方法调用是:
def read_maa_valid_product_id(drug_product_id, length=0, offset=0):
"""
This function responds to a request for /api/products
with the complete lists of people
:return: json string of list of people
"""
# conn_ariel = pool.acquire()
conn_ariel = get_connection()
cursor_ariel = conn_ariel.cursor()
# Create the list of products from our data
sql = """
SELECT A.DRUG_PRODUCT_ID,
B.PREFERRED_TRADE_NAME,
B.PRODUCT_LINE,
B.PRODUCT_TYPE,
B.FLAG_PASSIVE AS PRODUCT_FLAG_PASSIVE,
A.REGISTRATION_UID,
A.COUNTRY_DISPLAY_LABEL,
A.FLAG_PASSIVE,
A.AUTHORIZATION_STATUS,
A.DISTRIBUTION_TYPE AS PROCEDURE_TYPE,
A.MAH_COMPANY,
A.DOSSIER_REF_NUMBER AS REGISTRATION_NAME_DETAILS,
A.APPLICATION_STAGE,
A.APPLICATION_TYPE,
A.RENEWAL_NOT_REQUIRED,
TO_CHAR(A.NEXT_RENEWAL_DATE,'YYYY-MM-DD') AS NEXT_RENEWAL_DATE
FROM DIM_REGISTRATION_SET A, DIM_DRUG_PRODUCT B, v_rep_az_183_01_reg_includes C
WHERE A.DRUG_PRODUCT_ID = B.DRUG_PRODUCT_ID AND A.VERSION_SEQ = C.VERSION_SEQ
AND A.DATA_STATE = 'C'
AND A.FLAG_PASSIVE = '0'
AND A.APPLICATION_TYPE = 'Marketing Application'
AND A.AUTHORIZATION_STATUS LIKE 'Valid%'
AND A.DRUG_PRODUCT_ID = :id
ORDER BY B.PREFERRED_TRADE_NAME, A.COUNTRY_DISPLAY_LABEL ASC
"""
cursor_ariel.execute(sql,{"id":drug_product_id})
registrations = []
names = [c[0] for c in cursor_ariel.description]
cursor_ariel.rowfactory = collections.namedtuple("registrations", names)
i = 0
j = 0
start = None
if offset == 0:
start = True
else:
start = False
for row in cursor_ariel.fetchall():
if start == True:
registration = {
"drug_product_id": row.DRUG_PRODUCT_ID,
"preferred_trade_name": row.PREFERRED_TRADE_NAME,
"product_line": row.PRODUCT_LINE,
"product_type": row.PRODUCT_TYPE,
"product_flag_passive": row.PRODUCT_FLAG_PASSIVE,
"registration_uid": row.REGISTRATION_UID,
"country_display_label": row.COUNTRY_DISPLAY_LABEL,
"flag_passive": row.FLAG_PASSIVE,
"authorization_status": row.AUTHORIZATION_STATUS,
"procedure_type": row.PROCEDURE_TYPE,
"mah_company": row.MAH_COMPANY,
"registration_name_details": row.REGISTRATION_NAME_DETAILS,
"application_stage": row.APPLICATION_STAGE,
"application_type": row.APPLICATION_TYPE,
"renewal_not_required": row.RENEWAL_NOT_REQUIRED,
"next_renewal_date": row.NEXT_RENEWAL_DATE
}
# logging.info("Adding Registration: %r", registration)
registrations.append(registration)
if length > 0:
if i == length - 1:
break
i += 1
else:
if j == offset - 1:
start = True
j += 1
pool.release(conn_ariel)
return registrations
到了最后:
yaml配置:
/read/products_many/{drug_product_ids}:
get:
operationId: products.read_products_many
tags:
- Product
summary: Read multiple drug products for the provided drug_product_ids
description: Read multiple drug products for the provided drug_product_ids
parameters:
- name: drug_product_ids
in: path
required: true
schema:
type: array
items:
type: integer
minItems: 1
style: simple
explode: false
description: drug_product_ids primary keys of the required products
responses:
'200':
description: Successfully retrieved product
content:
application/json:
schema:
$ref: '#/components/schemas/product'
基于:
然后在我的方法中,我看到一个列表作为参数输入,所以我将转换为字符串,然后使用字符串格式创建SQL
def read_products_many(drug_product_ids):
conn_ariel = get_connection()
cursor_ariel = conn_ariel.cursor()
print(drug_product_ids) # --> [4670, 4671]
print(type(drug_product_ids)) # --> List
# convert to string for use in IN clause in SQL
drug_product_ids = [str(x) for x in drug_product_ids]
print("read_many product, id", drug_product_ids)
# Create the list of products from our data
sql = """
SELECT DRUG_PRODUCT_ID, PREFERRED_TRADE_NAME, PRODUCT_LINE, PRODUCT_TYPE, FLAG_PASSIVE , PRODUCT_NUMBER
FROM DIM_DRUG_PRODUCT
WHERE DRUG_PRODUCT_ID in ({0})
AND PREFERRED_TRADE_NAME NOT LIKE '%DO NOT USE%'
AND UPPER(PREFERRED_TRADE_NAME) NOT LIKE '%DELETE%'
AND UPPER(PREFERRED_TRADE_NAME) NOT LIKE '%TEST%'
""".format(",".join(drug_product_ids))
print("SQL")
print(sql)
cursor_ariel.execute(sql)
product = None
products = []
for row in cursor_ariel.fetchall():
r = reg(cursor_ariel, row, False)
product = {
"drug_product_id" : r.DRUG_PRODUCT_ID,
"preferred_trade_name" : r.PREFERRED_TRADE_NAME,
"product_line" : r.PRODUCT_LINE,
"product_type" : r.PRODUCT_TYPE,
"flag_passive" : r.FLAG_PASSIVE,
"product_number" : r.PRODUCT_NUMBER
}
products.append(product)
pool.release(conn_ariel)
return products
输入此URL时返回的SQL:
localhost:5000/api/read/products_many/4671,4670
正确答案如下:
SELECT DRUG_PRODUCT_ID, PREFERRED_TRADE_NAME, PRODUCT_LINE, PRODUCT_TYPE, FLAG_PASSIVE , PRODUCT_NUMBER
FROM DIM_DRUG_PRODUCT
WHERE DRUG_PRODUCT_ID in (4671,4670)
AND PREFERRED_TRADE_NAME NOT LIKE '%DO NOT USE%'
AND UPPER(PREFERRED_TRADE_NAME) NOT LIKE '%DELETE%'
AND UPPER(PREFERRED_TRADE_NAME) NOT LIKE '%TEST%'
这回答了你的问题吗?不要这样认为,我没有使用POST。如果可能的话,我需要知道如何定义yaml文件,以及如何根据GET调整函数。
请求。数据应能在GET或post调用中使用。。。