import json
import logging
import requests


def news_API_request(covid_terms: str ="Covid COVID-19 coronavirus") -> list:
    """
    Access the NewsApi and filter and return all articles with any of the covid_terms in the title
    """

    with open('config.json', 'r', encoding="UTF-8") as file:
        config = json.load(file)
        api_key = config["api-key"]
        country = config["newsCountry"]

    base_url = "https://newsapi.org/v2/top-headlines?"
    articles=[]
    covid_terms_list = covid_terms.split()
    for i in covid_terms_list:
        logging.info("Requesting articles containing the keyword: %s", i)
        complete_url = f"{base_url}q={i}&apiKey={api_key}&country={country}"
        this_response = requests.get(complete_url).json()
        for response in this_response["articles"]:
            if response not in articles:
                articles.append(response)
    return articles