71 lines
2.0 KiB
Python
71 lines
2.0 KiB
Python
"""
|
|
HTTP client for CodiMD API.
|
|
"""
|
|
|
|
import requests
|
|
from typing import Optional, Dict, Any, Tuple
|
|
from urllib.parse import urljoin
|
|
|
|
|
|
class CodiMDClient:
|
|
"""HTTP client for CodiMD server."""
|
|
|
|
def __init__(self, server: str, session_cookies: Dict[str, str] = None,
|
|
timeout: int = 30, verify_ssl: bool = True):
|
|
self.server = server.rstrip("/")
|
|
self.timeout = timeout
|
|
self.verify_ssl = verify_ssl
|
|
self.session = requests.Session()
|
|
if session_cookies:
|
|
self.session.cookies.update(session_cookies)
|
|
|
|
def _url(self, path: str) -> str:
|
|
"""Build full URL for path."""
|
|
# Remove leading slash from path to make it relative
|
|
path = path.lstrip("/")
|
|
return urljoin(self.server + "/", path)
|
|
|
|
def get(self, path: str, **kwargs) -> requests.Response:
|
|
"""Send GET request."""
|
|
return self.session.get(
|
|
self._url(path),
|
|
timeout=self.timeout,
|
|
verify=self.verify_ssl,
|
|
**kwargs
|
|
)
|
|
|
|
def post(self, path: str, **kwargs) -> requests.Response:
|
|
"""Send POST request."""
|
|
return self.session.post(
|
|
self._url(path),
|
|
timeout=self.timeout,
|
|
verify=self.verify_ssl,
|
|
**kwargs
|
|
)
|
|
|
|
def put(self, path: str, **kwargs) -> requests.Response:
|
|
"""Send PUT request."""
|
|
return self.session.put(
|
|
self._url(path),
|
|
timeout=self.timeout,
|
|
verify=self.verify_ssl,
|
|
**kwargs
|
|
)
|
|
|
|
def delete(self, path: str, **kwargs) -> requests.Response:
|
|
"""Send DELETE request."""
|
|
return self.session.delete(
|
|
self._url(path),
|
|
timeout=self.timeout,
|
|
verify=self.verify_ssl,
|
|
**kwargs
|
|
)
|
|
|
|
def update_cookies(self, cookies: Dict[str, str]):
|
|
"""Update session cookies."""
|
|
self.session.cookies.update(cookies)
|
|
|
|
def get_cookies(self) -> Dict[str, str]:
|
|
"""Get current cookies."""
|
|
return dict(self.session.cookies)
|