22from fastapi import FastAPI , HTTPException
33from fastapi .middleware .cors import CORSMiddleware
44from .database import engine
5+ from dotenv import load_dotenv
6+ import os
57import requests
68
79models .Base .metadata .create_all (bind = engine )
810
11+ load_dotenv ()
12+
913app = FastAPI ()
1014
1115origins = [
@@ -40,6 +44,29 @@ async def get_post(post_id: int):
4044 raise HTTPException (status_code = response .status_code , detail = "API call failed" )
4145 except Exception as e :
4246 raise HTTPException (status_code = 500 , detail = "Internal server error" )
47+
48+
49+ @app .get ('/crypto-price-ethereum2' )
50+ async def get_crypto_price ():
51+ try :
52+ api_key = os .getenv ("api_key" )
53+ if not api_key :
54+ raise HTTPException (status_code = 500 , detail = "API key not configured" )
55+
56+ url = (
57+ "https://api.coingecko.com/api/v3/simple/token_price/ethereum"
58+ "?contract_addresses=0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
59+ f"&vs_currencies=usd&x_cg_demo_api_key={ api_key } "
60+ )
61+ response = requests .get (url )
62+
63+ if response .status_code == 200 :
64+ return response .json ()
65+ else :
66+ raise HTTPException (status_code = response .status_code , detail = "API call failed" )
67+
68+ except Exception as e :
69+ raise HTTPException (status_code = 500 , detail = str (e ))
4370
4471@app .get ('/crypto-price-ethereum' )
4572async def get_crypto_price ():
@@ -53,6 +80,10 @@ async def get_crypto_price():
5380 raise HTTPException (status_code = response .status_code , detail = "API call failed" )
5481 except Exception as e :
5582 raise HTTPException (status_code = 500 , detail = "Internal server error" )
83+
84+
85+
86+
5687
5788 # url = "https://api.coingecko.com/api/v3/simple/token_price/ethereum?contract_addresses=0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48&vs_currencies=usd&x_cg_demo_api_key=CG-2DiDAMj5CMnutZkqo3r1jxBJ"
5889
0 commit comments