import urllib.request,json
from .models import Source

#Getting api key
api_key = None

#Getting the sources base url
source_url = None


def configure_request(app):
    global api_key,source_url
    api_key = app.config['NEWS_API_KEY']
    source_url = app.config['SOURCE_API_BASE_URL']

def get_source(category):
    '''
    Function that gets the json response to our url request
    '''
    get_source_url = source_url.format(category,api_key)

    with urllib.request.urlopen(get_source_url) as url:
        get_source_data = url.read()
        get_source_response = json.loads(get_source_data)

        source_results = None

        if get_source_response['sources']:
            source_results_list = get_source_response['sources']
            source_results = process_results(source_results_list)


    return source_results
def process_results(source_list):
    '''
    Function  that processes the sources result and transform them to a list of Objects

    Args:
        source_list: A list of dictionaries that contain source details

    Returns :
        source_results: A list of source objects
    '''
    source_results = []
    for source_item in source_list:
        name = source_item.get('name')
        description = source_item.get('description')
        url = source_item.get('url')
        category = source_item.get('category')

        if category:
            source_object = Source(name,description,url,category)
            source_results.append(source_object)

    return source_results
