Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8e4901a

Browse files
author
Rafael
committed
checks coin id and returns coin price
1 parent 166bf78 commit 8e4901a

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ open on: http://127.0.0.1:8000/docs
1414
docker: docker-compose up -d
1515

1616

17+
(frontend: frontend-reactjs-crud-crypto-app https://github.com/rafgger/frontend-reactjs-crud-crypto-app)
18+
1719
In this article, I'll provide you with a simple and straightforward guide on how you can build a CRUD app with FastAPI and SQLAlchemy. The FastAPI app will run on a Starlette web server, use Pydantic for data validation, and store data in an SQLite database.
1820

1921
![Build a CRUD App with FastAPI and SQLAlchemy](https://codevoweb.com/wp-content/uploads/2022/11/Build-a-CRUD-App-with-FastAPI-and-SQLAlchemy.png)

‎app/note.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ def get_notes(db: Session = Depends(get_db), limit: int = 10, page: int = 1, sea
1919
models.Note.title.contains(search)).limit(limit).offset(skip).all()
2020
return {'status': 'success', 'results': len(notes), 'notes': notes}
2121

22+
def get_coin_id(symbol_or_name):
23+
"""
24+
Retrieve the CoinGecko coin ID for a given symbol or name.
25+
"""
26+
url = "https://api.coingecko.com/api/v3/coins/list"
27+
response = requests.get(url)
28+
if response.status_code == 200:
29+
coins = response.json()
30+
symbol_or_name = symbol_or_name.lower()
31+
for coin in coins:
32+
if coin['symbol'].lower() == symbol_or_name or coin['name'].lower() == symbol_or_name:
33+
return coin['id']
34+
raise HTTPException(status_code=404, detail="Coin not found")
35+
else:
36+
raise HTTPException(status_code=response.status_code, detail="Failed to fetch coin list from CoinGecko")
37+
38+
2239
@router.post('/', status_code=status.HTTP_201_CREATED)
2340
def create_note(payload: schemas.NoteBaseSchema, db: Session = Depends(get_db)):
2441
try:
@@ -27,11 +44,15 @@ def create_note(payload: schemas.NoteBaseSchema, db: Session = Depends(get_db)):
2744
if not api_key:
2845
raise HTTPException(status_code=500, detail="API key not configured")
2946

30-
# Build the API URL
47+
# Retrieve the CoinGecko coin ID using the payload title
48+
coin_id = get_coin_id(payload.title)
49+
50+
# Build the API URL using the retrieved coin ID
3151
url = (
32-
"https://api.coingecko.com/api/v3/simple/token_price/ethereum"
33-
"?contract_addresses=0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
34-
f"&vs_currencies=usd&x_cg_demo_api_key={api_key}"
52+
f"https://api.coingecko.com/api/v3/simple/price"
53+
f"?ids={coin_id}"
54+
f"&vs_currencies=usd"
55+
f"&x_cg_demo_api_key={api_key}"
3556
)
3657

3758
# Make the request to get the crypto price
@@ -40,15 +61,15 @@ def create_note(payload: schemas.NoteBaseSchema, db: Session = Depends(get_db)):
4061
raise HTTPException(status_code=response.status_code, detail="API call failed")
4162

4263
data = response.json()
43-
price = data.get("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", {}).get("usd")
64+
price = data.get(coin_id, {}).get("usd")
4465

4566
if price is None:
4667
raise HTTPException(status_code=500, detail="Invalid response from crypto API")
4768

4869
# Create the note with the title from the payload and content as the crypto price
4970
new_note = models.Note(
5071
title=payload.title,
51-
content=f"Current ETH token price (USD):{price}",
72+
content=f"Current {payload.title}price (USD):\n${price}",
5273
category=payload.category,
5374
published=payload.published
5475
)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /