"""Консенсус-прогнозы — готовый пример RU DATA API.

Python 3.8+, зависимость: pip install requests.
Заполните ВАШ_ЛОГИН / ВАШ_ПАРОЛЬ и запустите: python script.py"""

import requests

BASE = "https://dh2.efir-net.ru/v2"
LOGIN = "ВАШ_ЛОГИН"
PASSWORD = "ВАШ_ПАРОЛЬ"

# Авторизация: JWT-токен на все последующие вызовы
resp = requests.post(f"{BASE}/Account/Login",
                     json={"login": LOGIN, "password": PASSWORD}, timeout=30)
resp.raise_for_status()
headers = {"Authorization": f"Bearer {resp.json()['token']}", "Accept": "application/json"}

# 1. Бумаги, по которым есть прогнозы
body = {
    "count": 5
}
resp = requests.post(f"{BASE}/Forecast/Securities", json=body, headers=headers, timeout=90)
resp.raise_for_status()
securities = resp.json()
print("securities:", str(securities)[:500])

# 2. Текущий консенсус-прогноз по ценам и рекомендации
body = {
    "count": 5
}
resp = requests.post(f"{BASE}/Forecast/PriceConsensus", json=body, headers=headers, timeout=90)
resp.raise_for_status()
consensus = resp.json()
print("consensus:", str(consensus)[:500])

# 3. Прогнозы аналитиков по ценам
body = {
    "count": 5
}
resp = requests.post(f"{BASE}/Forecast/TargetPrices", json=body, headers=headers, timeout=90)
resp.raise_for_status()
targets = resp.json()
print("targets:", str(targets)[:500])
