Files
lottery_ycms/lottery/api_test.py
2025-06-13 21:10:20 +09:00

38 lines
1.1 KiB
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
import json
from dotenv import load_dotenv
import os
load_dotenv('.env')
def fetch_clients(api_url, api_key):
"""
Подключаемся к API с использованием заданного X-API-Key
"""
headers = {
"X-API-Key": api_key
}
try:
response = requests.get(api_url, headers=headers)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"Ошибка при запросе к API: {e}")
return None
if __name__ == '__main__':
api_endpoint = os.getenv("API_URL")
api_key = os.getenv("API_KEY")
print(api_endpoint, api_key)
if not api_endpoint or not api_key:
print("Необходимо задать API_URL и API_KEY в .env файле.")
exit(1)
clients = fetch_clients(api_endpoint, api_key)
if clients is not None:
print(json.dumps(clients, indent=4, ensure_ascii=False))
else:
print("Не удалось получить данные с API.")