38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
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.")
|