53 lines
1.4 KiB
Python
53 lines
1.4 KiB
Python
"""
|
|
This file is part of the SplendidBear Websites' projects.
|
|
|
|
Copyright (c) 2026 @ www.splendidbear.org
|
|
|
|
For the full copyright and license information, please view the LICENSE
|
|
file that was distributed with this source code.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import requests
|
|
|
|
from mineseeker import config
|
|
|
|
# Module-level singleton session shared by all API modules.
|
|
# Holds cookies (Symfony session cookie after login) across all requests.
|
|
_session: requests.Session | None = None
|
|
|
|
|
|
def get_session() -> requests.Session:
|
|
global _session
|
|
if _session is None:
|
|
_session = requests.Session()
|
|
_session.headers.update({
|
|
"Accept": "application/json",
|
|
"Content-Type": "application/json",
|
|
})
|
|
return _session
|
|
|
|
|
|
def reset_session() -> None:
|
|
"""Discard the current session (logout / new guest session)."""
|
|
global _session
|
|
_session = None
|
|
|
|
|
|
def url(path: str) -> str:
|
|
"""Build an absolute URL from a server-relative path."""
|
|
return f"{config.BASE_URL}/{path.lstrip('/')}"
|
|
|
|
|
|
def get(path: str, **kwargs) -> requests.Response:
|
|
resp = get_session().get(url(path), **kwargs)
|
|
resp.raise_for_status()
|
|
return resp
|
|
|
|
|
|
def post(path: str, json: dict | None = None, **kwargs) -> requests.Response:
|
|
resp = get_session().post(url(path), json=json, **kwargs)
|
|
resp.raise_for_status()
|
|
return resp
|