@app.route('/availablepairsobjects/')
def get_available_pairs_objects():
    labs_conn = psycopg2.connect(user=os.environ.get('aws_db_user'), password=os.environ.get('aws_db_password'), host=os.environ.get('aws_db_host'), port=os.environ.get('aws_db_port'), database=os.environ.get('aws_db_name'))
    labs_curs = labs_conn.cursor()
    all_pairs = {'retail': None, 'wholesale': None}
    labs_curs.execute('\n            SELECT country_code\n            FROM countries\n            ')
    countries = labs_curs.fetchall()
    if countries:
        countries = [x[0] for x in countries]
        country_market_product_pairs = {country: None for country in countries}
        for country in countries:
            labs_curs.execute('\n                    SELECT market_name \n                    FROM markets\n                    WHERE country_code = %s\n                    ', (country,))
            markets = labs_curs.fetchall()
            if markets:
                markets = [x[0] for x in markets]
                country_market_product_pairs[country] = {market: None for market in markets}
                retail_pairs = country_market_product_pairs.copy()
                wholesale_pairs = country_market_product_pairs.copy()
                for market in markets:
                    labs_curs.execute('\n                    SELECT DISTINCT(product_name) \n                    FROM retail_prices\n                    WHERE country_code = %s\n                    AND market_name = %s\n                    ', (country, market))
                    products = labs_curs.fetchall()
                    if products:
                        products = [x[0] for x in products]
                        retail_pairs[country][market] = products
                        all_pairs['retail'] = retail_pairs
                    labs_curs.execute('\n                    SELECT DISTINCT(product_name) \n                    FROM wholesale_prices\n                    WHERE country_code = %s\n                    AND market_name = %s\n                    ', (country, market))
                    products = labs_curs.fetchall()
                    if products:
                        products = [x[0] for x in products]
                        wholesale_pairs[country][market] = products
                        all_pairs['wholesale'] = retail_pairs
                    else:
                        del wholesale_pairs[country][market]
    keys_to_drop = []
    for sale_type in ['retail', 'wholesale']:
        for key in all_pairs[sale_type].keys():
            if (not all_pairs[sale_type][key]):
                keys_to_drop.append(key)
        for key in keys_to_drop:
            del all_pairs[sale_type][key]
        keys_to_drop = []
    labs_curs.close()
    labs_conn.close()
    return jsonify(all_pairs)
@app.route('/availablepairsobjects/')
def get_available_pairs_objects():
    labs_conn = psycopg2.connect(user=os.environ.get('aws_db_user'), password=os.environ.get('aws_db_password'), host=os.environ.get('aws_db_host'), port=os.environ.get('aws_db_port'), database=os.environ.get('aws_db_name'))
    labs_curs = labs_conn.cursor()
    all_pairs = {'retail': None, 'wholesale': None}
    labs_curs.execute('\n            SELECT country_code\n            FROM countries\n            ')
    countries = labs_curs.fetchall()
    if countries:
        countries = [x[0] for x in countries]
        country_market_product_pairs = {country: None for country in countries}
        for country in countries:
            labs_curs.execute('\n                    SELECT market_name \n                    FROM markets\n                    WHERE country_code = %s\n                    ', (country,))
            markets = labs_curs.fetchall()
            if markets:
                markets = [x[0] for x in markets]
                country_market_product_pairs[country] = {market: None for market in markets}
                retail_pairs = country_market_product_pairs.copy()
                wholesale_pairs = country_market_product_pairs.copy()
                for market in markets:
                    labs_curs.execute('\n                    SELECT DISTINCT(product_name) \n                    FROM retail_prices\n                    WHERE country_code = %s\n                    AND market_name = %s\n                    ', (country, market))
                    products = labs_curs.fetchall()
                    if products:
                        products = [x[0] for x in products]
                        retail_pairs[country][market] = products
                        all_pairs['retail'] = retail_pairs
                    labs_curs.execute('\n                    SELECT DISTINCT(product_name) \n                    FROM wholesale_prices\n                    WHERE country_code = %s\n                    AND market_name = %s\n                    ', (country, market))
                    products = labs_curs.fetchall()
                    if products:
                        products = [x[0] for x in products]
                        wholesale_pairs[country][market] = products
                        all_pairs['wholesale'] = retail_pairs
                    else:
                        del wholesale_pairs[country][market]
    keys_to_drop = []
    for sale_type in ['retail', 'wholesale']:
        for key in all_pairs[sale_type].keys():
            if (not all_pairs[sale_type][key]):
                keys_to_drop.append(key)
        for key in keys_to_drop:
            del all_pairs[sale_type][key]
        keys_to_drop = []
    labs_curs.close()
    labs_conn.close()
    return jsonify(all_pairs)