Files
Excel-project/server/backend/api/session.py
2026-01-06 18:06:34 +03:00

20 lines
759 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import requests
from base64 import b64encode
from server.backend.schemas.pydantic import settings
def get_session(extra_headers=None):
"""
Создаёт и возвращает requests.Session с базовой авторизацией.
Можно менять username, password и добавлять дополнительные заголовки.
"""
session = requests.Session()
auth_str = f"{settings.USERNAME}:{settings.PASSWORD}"
b64_auth_str = b64encode(auth_str.encode("utf-8")).decode("utf-8")
headers = {
"Authorization": f"Basic {b64_auth_str}",
"Accept": "application/json"
}
if extra_headers:
headers.update(extra_headers)
session.headers.update(headers)
return session