new: dev: initialize the GTK client #11
This commit is contained in:
52
gtk-client/mineseeker/api/client.py
Normal file
52
gtk-client/mineseeker/api/client.py
Normal file
@@ -0,0 +1,52 @@
|
||||
"""
|
||||
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
|
||||
Reference in New Issue
Block a user