简体   繁体   中英

How to accept an array parameter in a GET request in Flask using Connexion

I am using connexion to create a REST API via Flask. At the moment I am passing a single identifier in.

/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'

I need to pass an array of id values in, based on selections in a table. Is this possible?

If it is possible, how would I access the array, the existing method call is:

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

Got there in the end:

yaml config:

/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'

Based on: https://swagger.io/docs/specification/serialization/#uri-templates

Then in my method, I see a list coming in as a parameter, so I am converting to string and then creating the SQL using string formatting.

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

SQL being returned when I enter this URL:

localhost:5000/api/read/products_many/4671,4670

is correct as:

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%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM