import time
import requests


def check_vendor(mac, connection):
    """
    Makes use of a free API to check a MAC addresses vendor
    Requires self.internet_con to be True
    :param connection: (Boolean) True if the device has an internet connection
    :param mac: (str) A MAC address of a given device
    :return: (str) The vendor of a given host devices network card
    """
    if connection:
        try:
            time.sleep(1)  # Free API requires a delay to avoid overloading the service
            vendor_req = requests.get('https://api.macvendors.com/' + mac)
            vendor = vendor_req.content.decode()  # Decode into a readable format
            if 'Not Found' in vendor:
                return 'Unknown'
            return vendor
        except requests.exceptions.ConnectionError:
            return 'Unknown'
        except:
            return 'Unknown'
    else:
        return 'Unknown'