Spaces:
Sleeping
Sleeping
File size: 6,369 Bytes
a2c10b6 |
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 |
# api/endpoints.py
import httpx
import os
class FMPEndpoints:
def __init__(self):
# self.db = FinancialDB()
self.fmp_api_key = os.getenv("FMP_API_KEY")
# print(self.fmp_api_key)
self.base_url = "https://financialmodelingprep.com/api/v3"
async def get_income_statement(self, ticker, year=None, period="annual", limit=1):
"""
Fetch income statement data for a given ticker.
"""
endpoint = f"{self.base_url}/income-statement/{ticker}"
params = {"apikey": self.fmp_api_key, "period": period, "limit": limit}
if year:
params["year"] = year
try:
async with httpx.AsyncClient() as client:
response = await client.get(endpoint, params=params)
response.raise_for_status()
return response.json()
except httpx.HTTPStatusError as e:
raise Exception(f"API error: {e.response.status_code} - {e.response.text}")
except Exception as e:
raise Exception(f"Error fetching income statement: {e}")
async def get_quote_short(self, ticker):
"""
Fetch the current stock price (short quote) for a given ticker.
"""
endpoint = f"{self.base_url}/quote-short/{ticker}"
params = {"apikey": self.fmp_api_key}
try:
async with httpx.AsyncClient() as client:
response = await client.get(endpoint, params=params)
response.raise_for_status()
return response.json()
except httpx.HTTPStatusError as e:
raise Exception(f"API error: {e.response.status_code} - {e.response.text}")
except Exception as e:
raise Exception(f"Error fetching quote: {e}")
async def get_ratios(self, ticker, year=None, limit=1):
"""
Fetch financial ratios for a given ticker.
"""
endpoint = f"{self.base_url}/ratios/{ticker}"
params = {"apikey": self.fmp_api_key, "limit": limit}
if year:
params["year"] = year
try:
async with httpx.AsyncClient() as client:
response = await client.get(endpoint, params=params)
response.raise_for_status()
return response.json()
except httpx.HTTPStatusError as e:
raise Exception(f"API error: {e.response.status_code} - {e.response.text}")
except Exception as e:
raise Exception(f"Error fetching ratios: {e}")
async def get_profile(self, ticker):
"""
Fetch company profile data for a given ticker.
"""
endpoint = f"{self.base_url}/profile/{ticker}"
params = {"apikey": self.fmp_api_key}
try:
async with httpx.AsyncClient() as client:
response = await client.get(endpoint, params=params)
response.raise_for_status()
return response.json()
except httpx.HTTPStatusError as e:
raise Exception(f"API error: {e.response.status_code} - {e.response.text}")
except Exception as e:
raise Exception(f"Error fetching profile: {e}")
async def get_historical_price(self, ticker, date=None):
"""
Fetch historical stock price for a given ticker on a specific date.
"""
endpoint = f"{self.base_url}/historical-price-full/{ticker}"
params = {"apikey": self.fmp_api_key}
if date:
params["from"] = date
params["to"] = date
try:
async with httpx.AsyncClient() as client:
response = await client.get(endpoint, params=params)
response.raise_for_status()
return response.json()
except httpx.HTTPStatusError as e:
raise Exception(f"API error: {e.response.status_code} - {e.response.text}")
except Exception as e:
raise Exception(f"Error fetching historical price: {e}")
async def get_balance_sheet(self, ticker, year=None, period="annual", limit=1):
"""
Fetch balance sheet data for a given ticker.
"""
endpoint = f"{self.base_url}/balance-sheet-statement/{ticker}"
params = {"apikey": self.fmp_api_key, "period": period, "limit": limit}
if year:
params["year"] = year
try:
async with httpx.AsyncClient() as client:
response = await client.get(endpoint, params=params)
response.raise_for_status()
return response.json()
except httpx.HTTPStatusError as e:
raise Exception(f"API error: {e.response.status_code} - {e.response.text}")
except Exception as e:
raise Exception(f"Error fetching balance sheet: {e}")
async def get_cash_flow(self, ticker, year=None, period="annual", limit=1):
"""
Fetch cash flow statement data for a given ticker.
"""
endpoint = f"{self.base_url}/cash-flow-statement/{ticker}"
params = {"apikey": self.fmp_api_key, "period": period, "limit": limit}
if year:
params["year"] = year
try:
async with httpx.AsyncClient() as client:
response = await client.get(endpoint, params=params)
response.raise_for_status()
return response.json()
except httpx.HTTPStatusError as e:
raise Exception(f"API error: {e.response.status_code} - {e.response.text}")
except Exception as e:
raise Exception(f"Error fetching cash flow: {e}")
async def get_key_metrics(self, ticker, year=None, limit=1):
"""
Fetch key metrics (e.g., EPS) for a given ticker.
"""
endpoint = f"{self.base_url}/key-metrics/{ticker}"
params = {"apikey": self.fmp_api_key, "limit": limit}
if year:
params["year"] = year
try:
async with httpx.AsyncClient() as client:
response = await client.get(endpoint, params=params)
response.raise_for_status()
return response.json()
except httpx.HTTPStatusError as e:
raise Exception(f"API error: {e.response.status_code} - {e.response.text}")
except Exception as e:
raise Exception(f"Error fetching key metrics: {e}") |