from .api import request


class Session:
    def __init__(self, cf_proxies=None):
        if not cf_proxies:
            raise ValueError("cf_proxies is required")
        self.cf_proxies = cf_proxies.rstrip("/")
        self.headers = {}
        self.cookies = {}
    
    def request(self, method, url, **kwargs):
        headers = self.headers.copy()
        headers.update(kwargs.pop("headers", {}))
        
        cookies = self.cookies.copy()
        cookies.update(kwargs.pop("cookies", {}))
        
        return request(
            method,
            url,
            cf_proxies=self.cf_proxies,
            headers=headers,
            cookies=cookies,
            **kwargs
        )
    
    def get(self, url, **kwargs):
        return self.request("GET", url, **kwargs)
    
    def post(self, url, **kwargs):
        return self.request("POST", url, **kwargs)
    
    def put(self, url, **kwargs):
        return self.request("PUT", url, **kwargs)
    
    def delete(self, url, **kwargs):
        return self.request("DELETE", url, **kwargs)
    
    def head(self, url, **kwargs):
        return self.request("HEAD", url, **kwargs)
    
    def options(self, url, **kwargs):
        return self.request("OPTIONS", url, **kwargs)
    
    def patch(self, url, **kwargs):
        return self.request("PATCH", url, **kwargs)
    
    def close(self):
        pass
    
    def __enter__(self):
        return self
    
    def __exit__(self, *args):
        self.close()

