-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_brapi.py
156 lines (137 loc) · 4.9 KB
/
test_brapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import requests
import os
from dotenv import load_dotenv
import json
from pprint import pprint
# Load environment variables
load_dotenv(override=True)
API_KEY = os.getenv("BRAPI_TOKEN")
BASE_URL = "https://brapi.dev/api"
def test_quote(ticker="PETR4"):
"""Test basic quote endpoint"""
print("\n=== Testing Quote Endpoint ===")
url = f"{BASE_URL}/quote/{ticker}"
params = {
'token': API_KEY,
'range': '1d',
'interval': '1d',
'fundamental': 'true'
}
response = requests.get(url, params=params)
print(f"Status: {response.status_code}")
if response.status_code == 200:
pprint(response.json())
def test_financial_statements(ticker="PETR4"):
"""Test financial statements endpoint"""
print("\n=== Testing Financial Statements ===")
url = f"{BASE_URL}/quote/{ticker}"
params = {
'token': API_KEY,
'range': '1y',
'fundamental': 'true',
'modules': 'incomeStatementHistory,balanceSheetHistory'
}
response = requests.get(url, params=params)
print(f"Status: {response.status_code}")
if response.status_code == 200:
pprint(response.json())
def test_financial_data(ticker="PETR4"):
"""Test financial data endpoint"""
print("\n=== Testing Financial Data ===")
url = f"{BASE_URL}/quote/{ticker}"
params = {
'token': API_KEY,
'fundamental': 'true',
'modules': 'financialData,defaultKeyStatistics'
}
response = requests.get(url, params=params)
print(f"Status: {response.status_code}")
if response.status_code == 200:
pprint(response.json())
def test_quote_list():
"""Test quote list endpoint"""
print("\n=== Testing Quote List ===")
url = f"{BASE_URL}/quote/list"
params = {
'token': API_KEY,
'limit': 5
}
response = requests.get(url, params=params)
print(f"Status: {response.status_code}")
if response.status_code == 200:
pprint(response.json())
def test_available_modules(ticker="PETR4"):
"""Test all available modules"""
print("\n=== Testing All Available Modules ===")
url = f"{BASE_URL}/quote/{ticker}"
params = {
'token': API_KEY,
'fundamental': 'true',
'modules': 'summaryProfile,balanceSheetHistory,balanceSheetHistoryQuarterly,incomeStatementHistory,incomeStatementHistoryQuarterly,financialData,defaultKeyStatistics'
}
response = requests.get(url, params=params)
print(f"Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
if 'results' in data and len(data['results']) > 0:
result = data['results'][0]
print("\nAvailable modules:")
for module in result.keys():
print(f"- {module}")
def test_historical_data(ticker="PETR4"):
"""Test historical data endpoint"""
print("\n=== Testing Historical Data ===")
url = f"{BASE_URL}/quote/{ticker}"
params = {
'token': API_KEY,
'range': '1mo',
'interval': '1d'
}
response = requests.get(url, params=params)
print(f"Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
if 'results' in data and len(data['results']) > 0:
result = data['results'][0]
if 'historicalDataPrice' in result:
print(f"\nHistorical data points: {len(result['historicalDataPrice'])}")
if result['historicalDataPrice']:
print("\nSample data point:")
pprint(result['historicalDataPrice'][0])
def test_balance_sheet_data(ticker="PETR4"):
"""Test balance sheet data endpoint with detailed logging"""
print("\n=== Testing Balance Sheet Data ===")
url = f"{BASE_URL}/quote/{ticker}"
params = {
'token': API_KEY,
'range': '1y',
'fundamental': 'true',
'modules': 'balanceSheetHistory'
}
response = requests.get(url, params=params)
print(f"Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
print("\nFull Response Structure:")
pprint(data)
if 'results' in data and data['results']:
result = data['results'][0]
print("\nBalance Sheet Keys:")
pprint(list(result.keys()))
if 'balanceSheetHistory' in result:
print("\nBalance Sheet History Structure:")
pprint(result['balanceSheetHistory'])
else:
print("\nNo balanceSheetHistory found in result")
else:
print("\nNo results found in response")
if __name__ == "__main__":
# Test balance sheet data specifically
test_balance_sheet_data()
# Run other tests
test_quote()
test_quote_list()
test_financial_statements()
test_financial_data()
test_available_modules()
test_historical_data()