2
2
from fastapi import FastAPI , HTTPException
3
3
from fastapi .middleware .cors import CORSMiddleware
4
4
from .database import engine
5
+ from dotenv import load_dotenv
6
+ import os
5
7
import requests
6
8
7
9
models .Base .metadata .create_all (bind = engine )
8
10
11
+ load_dotenv ()
12
+
9
13
app = FastAPI ()
10
14
11
15
origins = [
@@ -40,6 +44,29 @@ async def get_post(post_id: int):
40
44
raise HTTPException (status_code = response .status_code , detail = "API call failed" )
41
45
except Exception as e :
42
46
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 ))
43
70
44
71
@app .get ('/crypto-price-ethereum' )
45
72
async def get_crypto_price ():
@@ -53,6 +80,10 @@ async def get_crypto_price():
53
80
raise HTTPException (status_code = response .status_code , detail = "API call failed" )
54
81
except Exception as e :
55
82
raise HTTPException (status_code = 500 , detail = "Internal server error" )
83
+
84
+
85
+
86
+
56
87
57
88
# url = "https://api.coingecko.com/api/v3/simple/token_price/ethereum?contract_addresses=0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48&vs_currencies=usd&x_cg_demo_api_key=CG-2DiDAMj5CMnutZkqo3r1jxBJ"
58
89
0 commit comments