2022-08-07 08:17:37 +02:00
# Blitz API
2021-07-13 20:53:45 +02:00
2022-08-07 08:17:37 +02:00
[](https://opensource.org/licenses/MIT)
2022-03-20 16:00:25 +01:00
[](https://github.com/pre-commit/pre-commit)
2022-08-07 08:17:37 +02:00
A management backend for bitcoin and lightning node operators written in Python with FastAPI.
## Beta Disclaimer
This software is still considered BETA and may contain bugs. Don't expose it to the open internet or use with a lot of funds.
- [Blitz API ](#blitz-api )
- [Beta Disclaimer ](#beta-disclaimer )
- [Configuration ](#configuration )
- [Dependencies ](#dependencies )
- [Installation ](#installation )
- [Linux / macOS ](#linux--macos )
- [Windows ](#windows )
- [Run the application ](#run-the-application )
- [Linux / macOS ](#linux--macos-1 )
- [Windows ](#windows-1 )
- [Development ](#development )
- [Installation ](#installation-1 )
- [Sync changes to a RaspiBlitz ](#sync-changes-to-a-raspiblitz )
- [Unit / Integration testing ](#unit--integration-testing )
- [Run the tests with pytest ](#run-the-tests-with-pytest )
- [Run tests and generate a coverage ](#run-tests-and-generate-a-coverage )
- [Client libraries ](#client-libraries )
- [Generating client libraries ](#generating-client-libraries )
- [Before you commit ](#before-you-commit )
- [Swagger / OpenAPI ](#swagger--openapi )
- [Useful cURL commands to test the API ](#useful-curl-commands-to-test-the-api )
- [Acknowledgements ](#acknowledgements )
2022-08-04 20:54:00 +02:00
2021-07-13 20:53:45 +02:00
## Configuration
2021-12-05 16:27:12 +08:00
Create a `.env` file with your `bitcoind` and `lnd` configuration. See the `.env_sample` file for all configuration options.
2021-07-13 20:53:45 +02:00
2021-12-05 16:27:12 +08:00
### Dependencies
2021-07-13 20:53:45 +02:00
2021-12-05 16:27:12 +08:00
- [Python in version 3.7 ](https://www.python.org/downloads/ )
- [Redis ](https://redis.io )
2022-03-20 16:00:25 +01:00
- [Polar ](https://github.com/jamaljsr/polar )
2021-12-05 16:27:12 +08:00
If you need an easy option to run a simple bitcoind & lnd client
2021-07-13 20:53:45 +02:00
## Installation
2022-08-07 08:17:37 +02:00
⚠️ To setup a development environment for BlitzAPI skip to the [Development ](#Development ) section.
2021-09-20 19:37:24 +02:00
2021-12-05 16:27:12 +08:00
### Linux / macOS
2021-09-20 19:37:24 +02:00
2021-12-05 16:27:12 +08:00
```sh
make install
```
2021-09-20 19:37:24 +02:00
2021-12-05 16:27:12 +08:00
or
2021-07-13 20:53:45 +02:00
2021-12-05 16:27:12 +08:00
```sh
python -m pip install -r requirements.txt
```
2021-07-13 20:53:45 +02:00
2021-12-05 16:27:12 +08:00
### Windows
```sh
py -m pip install -r requirements.txt
```
2021-07-13 20:53:45 +02:00
2021-12-05 16:27:12 +08:00
## Run the application
2021-07-13 20:53:45 +02:00
2021-12-05 16:27:12 +08:00
### Linux / macOS
```sh
make run
```
or
```sh
python -m uvicorn app.main:app --reload
```
2021-07-13 20:53:45 +02:00
2021-12-05 16:27:12 +08:00
### Windows
2021-07-13 20:53:45 +02:00
2021-12-05 16:27:12 +08:00
```sh
py -m uvicorn app.main:app --reload
```
2021-09-20 19:37:24 +02:00
## Development
2021-12-05 16:27:12 +08:00
It is recommended to have [python-poetry installed ](<(https://python-poetry.org/docs/master/#installation )>).
From within the `blitz_api` folder [open a poetry shell ](https://python-poetry.org/docs/master/cli/#shell ) via:
```sh
poetry shell
```
(To exit the poetry shell use: `exit` )
2022-08-07 08:17:37 +02:00
### Installation
```
poetry install
pre-commit install
```
or
```sh
2022-12-17 19:13:54 +01:00
make install-dev
2022-08-07 08:17:37 +02:00
```
If python dependencies have been changed it's necessary to freeze all requirements to requirements.txt:
```sh
poetry export -f requirements.txt --output requirements.txt
```
> ℹ ️ This will skip all dev dependencies by default.\
> This step is required to avoid having to install poetry for final deployment.
### Sync changes to a RaspiBlitz
2022-05-05 13:27:59 +02:00
Create a file `/script/sync_to_blitz.personal.sh` (will be ignored by github) the SSH connection data to your RaspiBlitz.
localIP="192.168.178.61"
sshPort="22"
passwordA=""
2022-12-18 10:20:51 +01:00
Then you can run always `make sync-to-blitz` to copy your latest code over to your RaspiBlitz. The script automatically restarts the backend API with the new code on your RaspiBlitz and shows you the logs.
2022-05-05 13:27:59 +02:00
To test the backend API then call the SwaggerUI: `http://[LOCALIP]/api/v1/docs` - to call protected endpoints run the `/system/login` endpoint first with HTTP POST body:
```
{
"password": "[PASSWORDA]"
}
```
and then copy the JWT Auth string returned to `Authorize` in the top section of the SwaggerUI.
2022-06-12 08:06:55 +02:00
*You can also now test the RaspiBlitz WebUI against the API by running it locally on your dev laptop when you configure it to use the backend API of your RaspiBlitz.*
2022-05-05 13:27:59 +02:00
2022-08-07 08:17:37 +02:00
### Unit / Integration testing
2021-12-05 16:27:12 +08:00
2022-08-07 08:17:37 +02:00
Make sure to include tests for important pieces of submitted code.
2021-12-05 16:27:12 +08:00
2022-08-07 08:17:37 +02:00
#### Run the tests with pytest
2021-09-20 19:37:24 +02:00
2021-12-05 16:27:12 +08:00
```sh
2022-08-07 08:17:37 +02:00
make test
2021-12-05 16:27:12 +08:00
```
2021-09-20 19:37:24 +02:00
2022-08-07 08:17:37 +02:00
#### Run tests and generate a coverage
2021-09-20 19:37:24 +02:00
2021-12-05 16:27:12 +08:00
```sh
2022-08-07 08:17:37 +02:00
make coverage
2021-12-05 16:27:12 +08:00
```
2021-09-20 19:37:24 +02:00
2022-08-07 08:17:37 +02:00
This will run tests and generate a coverage html file in this folder: `./htmlcov`
2021-09-20 19:37:24 +02:00
2022-08-07 08:17:37 +02:00
### Client libraries
2022-08-07 07:38:02 +02:00
2022-08-07 08:17:37 +02:00
> ℹ ️ The client libraries live in an extra repository:
2022-08-07 07:38:02 +02:00
https://github.com/fusion44/blitz_api_client_libraries
2022-08-07 08:17:37 +02:00
#### Generating client libraries
2022-08-07 07:38:02 +02:00
Install [OpenAPI Generator ](https://openapi-generator.tech ) and Java:
```sh
npm install @openapitools/openapi -generator-cli -g
sudo apt install default-jre
```
Clone https://github.com/fusion44/blitz_api_client_libraries next to the blitz_api folder.
```sh
make generate-client-libs
```
> ⚠️ The first run requires `sudo` as it must download a Java .jar file to the system npm package folder.
2021-12-05 16:27:12 +08:00
2022-03-20 16:00:25 +01:00
### Before you commit
This project uses [pre-commit ](https://pre-commit.com ) to keep the source code structured. Please make sure to run either `make pre_commit` or `pre-commit run --all-files` . The CI pipeline will reject pull requests that fail this step. This step helps to ensures that the source code is formatted consistently and pull requests are as tidy as possible.
2022-08-07 08:17:37 +02:00
### [Swagger / OpenAPI](https://swagger.io)
2021-12-05 16:27:12 +08:00
Once the API is running swagger docs can be found here:
```
http://127.0.0.1:8000/latest/docs
```
2021-10-02 12:00:44 +02:00
2022-08-07 08:17:37 +02:00
### Useful cURL commands to test the API
2021-09-20 19:37:24 +02:00
2021-12-05 16:27:12 +08:00
```sh
2022-11-26 15:51:39 +01:00
curl -N -H "Authorization: Bearer JWT_TOKEN_HERE" http://127.0.0.1:8000/sse/subscribe
2021-12-05 16:27:12 +08:00
```
```sh
2022-11-26 15:51:39 +01:00
curl -N -H "Authorization: Bearer JWT_TOKEN_HERE" http://127.0.0.1:8000/v1/bitcoin/getblockchaininfo
2021-12-05 16:27:12 +08:00
```
2021-10-02 12:10:52 +02:00
2021-12-05 16:27:12 +08:00
```sh
curl -X POST -N http://127.0.0.1:8000/v1/setup/type/1
```
2021-10-02 12:10:52 +02:00
```sh
curl --header "Content-Type: application/json" \
--request POST \
--data '{"password":"12345678"}' \
http://127.0.0.1:8000/system/login
```
2022-08-07 08:17:37 +02:00
## Acknowledgements
2021-11-18 11:30:48 +01:00
2021-10-02 12:10:52 +02:00
Integrated Libraries:
2021-11-18 11:30:48 +01:00
2021-12-05 16:27:12 +08:00
- [sse-starlette ](https://github.com/sysid/sse-starlette )
- [fastapi-versioning ](https://github.com/DeanWay/fastapi-versioning )