from fastapi import Request
from typing import List, Optional


class AddLinkForm:
    def __init__(self, request: Request):
        self.request = request
        self.errors: List = []
        self.url: Optional[str] = None

    async def load_data(self):
        form = await self.request.form()
        self.url = str(form.get("url"))

    def is_valid(self):
        if not self.url or not (self.url.__contains__("http")):
            self.errors.append("Valid Url is required e.g. https://example.com")
        if not self.errors:
            return True
        return False
