Skip to content

Commit ccc2da9

Browse files
author
Rafael
committed
api_key sanitization
1 parent 3602f70 commit ccc2da9

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

app/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ class Settings(BaseSettings):
1111
POSTGRES_DB: str
1212
POSTGRES_HOST: str
1313
POSTGRES_HOSTNAME: str
14+
api_key: str
1415

1516
class Config:
16-
env_file = './.env'
17+
env_file = '../.env'
1718

1819

1920
settings = Settings()

app/main.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
from fastapi import FastAPI, HTTPException
33
from fastapi.middleware.cors import CORSMiddleware
44
from .database import engine
5+
from dotenv import load_dotenv
6+
import os
57
import requests
68

79
models.Base.metadata.create_all(bind=engine)
810

11+
load_dotenv()
12+
913
app = FastAPI()
1014

1115
origins = [
@@ -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')
4572
async 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

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ urllib3 #==1.26.12
3535
uvicorn #==0.18.3
3636
watchfiles #==0.18.0
3737
websockets #==10.4
38+
typing_inspect

0 commit comments

Comments
 (0)