def fetch_csv(uri: str, withHeader: bool | None = None):
    import csv, urllib.request

    with urllib.request.urlopen(uri) as response:
        chunk   = response.peek(4096).decode('utf-8', errors='ignore')
        sniffer = csv.Sniffer()
        dialect = sniffer.sniff(chunk)

        if withHeader is None:
            withHeader = sniffer.has_header(chunk)

        reader = csv.reader(
            (line.decode('utf-8') for line in response),
            dialect
        )

        if withHeader:
            yield valentina.TableHeader(next(reader))

        for row in reader:
            yield row

def valentina_extension_init(db):
    valentina.create_procedure('fetch_csv', fetch_csv)
