mirror of
https://github.com/fusion44/blitz_api.git
synced 2026-08-13 11:52:45 +02:00
feat: implement first bitcoin core REST calls
This commit is contained in:
parent
51e369cf14
commit
5eeedd755e
5 changed files with 84 additions and 4 deletions
7
.env
7
.env
|
|
@ -1,2 +1,7 @@
|
|||
secret=please_please_update_me_please
|
||||
algorithm=HS256
|
||||
algorithm=HS256
|
||||
|
||||
bitcoind_ip=127.0.0.1
|
||||
bitcoind_port=8332
|
||||
bitcoind_user=raspibolt
|
||||
bitcoind_pw=please_please_update_me_please
|
||||
45
app/routers/bitcoin.py
Normal file
45
app/routers/bitcoin.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
from fastapi import APIRouter, HTTPException, status, Request
|
||||
from fastapi.params import Depends
|
||||
|
||||
from app.utils import bitcoin_rpc
|
||||
from app.auth.auth_bearer import JWTBearer
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/bitcoin",
|
||||
tags=["Bitcoin Core"]
|
||||
)
|
||||
|
||||
|
||||
@router.get("/getblockcount", summary="Get the current block count",
|
||||
description="See documentation on [bitcoincore.org](https://bitcoincore.org/en/doc/0.21.0/rpc/blockchain/getblockcount/)",
|
||||
response_description="""A JSON String with relevant information.\n
|
||||
```json
|
||||
{
|
||||
\"result\": 682621,
|
||||
\"error\": null,
|
||||
\"id\": 0
|
||||
}
|
||||
""",
|
||||
dependencies=[Depends(JWTBearer())],
|
||||
status_code=status.HTTP_200_OK)
|
||||
def getblockcount():
|
||||
r = bitcoin_rpc("getblockcount")
|
||||
|
||||
if(r.status_code == status.HTTP_200_OK):
|
||||
return r.content
|
||||
else:
|
||||
raise HTTPException(r.status_code, detail=r.reason)
|
||||
|
||||
|
||||
@router.get("/getblockchaininfo", summary="Get the current blockchain status",
|
||||
description="See documentation on [bitcoincore.org](https://bitcoincore.org/en/doc/0.21.0/rpc/blockchain/getblockchaininfo/)",
|
||||
response_description="A JSON String with relevant information.",
|
||||
dependencies=[Depends(JWTBearer())],
|
||||
status_code=status.HTTP_200_OK)
|
||||
def getblockchaininfo():
|
||||
r = bitcoin_rpc("getblockchaininfo")
|
||||
|
||||
if(r.status_code == status.HTTP_200_OK):
|
||||
return r.content
|
||||
else:
|
||||
raise HTTPException(r.status_code, detail=r.reason)
|
||||
28
app/utils.py
Normal file
28
app/utils.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import requests
|
||||
import json
|
||||
from decouple import config
|
||||
|
||||
BITCOIND_IP = config("bitcoind_ip")
|
||||
BITCOIND_PORT = config("bitcoind_port")
|
||||
BITCOIND_USER = config("bitcoind_user")
|
||||
BITCOIND_PW = config("bitcoind_pw")
|
||||
|
||||
|
||||
def bitcoin_rpc(method: str, params: list = []) -> requests.Response:
|
||||
"""Make an RPC request to the Bitcoin daemon
|
||||
|
||||
Connection parameters are read from the .env file.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
method : str
|
||||
The method to call.
|
||||
params : list, optional
|
||||
Any parameters to include with the call
|
||||
"""
|
||||
url = f"http://{BITCOIND_IP}:{BITCOIND_PORT}"
|
||||
auth = (BITCOIND_USER, BITCOIND_PW)
|
||||
headers = {"Content-type": "text/plain"}
|
||||
data = '{"jsonrpc": "2.0", "method": "' + \
|
||||
method + '", "id":"0", "params":' + json.dumps(params) + '}'
|
||||
return requests.post(url, auth=auth, headers=headers, data=data)
|
||||
3
main.py
3
main.py
|
|
@ -1,11 +1,12 @@
|
|||
from fastapi import FastAPI
|
||||
from app.routers import system
|
||||
from app.routers import bitcoin, system
|
||||
|
||||
|
||||
# start server with "uvicorn main:app --reload"
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
app.include_router(bitcoin.router)
|
||||
app.include_router(system.router)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ pydantic==1.8.2
|
|||
uvicorn==0.14.0
|
||||
PyJWT==2.1.0
|
||||
python-decouple==3.4
|
||||
autopep
|
||||
autopep8==1.5.7
|
||||
psutil==5.8.0
|
||||
sse_starlette==0.7.2
|
||||
sse_starlette==0.7.2
|
||||
requests==2.25.1
|
||||
Loading…
Add table
Add a link
Reference in a new issue