III Common FastAPI base.
Project description
III API base
Usage
Config
The later config will override the previous one.
This table shows the predefined environment variables.
Keyword | Type | Default | Description |
---|---|---|---|
DEBUG |
boolean | false | To set the logging as DEBUG state, you can pass debug=True to application too. |
RELOAD |
boolean | false | To auto reload the FastAPI application, you can pass reload=True to run too. |
APP_NAME |
string | Backend API | The application name use on FastAPI. |
from pathlib import Path
from api_helper.config import load_config
# Load config
load_config(Path(__file__).parent / ".env")
# Load default config in the directory (.env)
load_config(Path(__file__).parent)
FastAPI example
To config the FastAPI by env, read the Config section.
from pathlib import Path
from api_helper import FastAPI, success_response
app: FastAPI = FastAPI(base_folder=Path(__file__).parent)
# Optional to setup sentry
app.setup_sentry("sentry_dsn")
@app.get("/")
def home():
return success_response("Hello, World!")
# Start the app and enjoy
app.run("127.0.0.1", 5000)
# Start the app with OpenAPI, for example
app.run(show_swagger=True)
If you want to customize the Logger format, you can pass log_builder
to the FastAPI object.
The following example shows how to customize the logger format.
from pathlib import Path
from api_helper import FastAPI
from api_helper.config import LoggerBuilder
# To customize the logger format, you can pass the log_builder to FastAPI
log_builder: LoggerBuilder = LoggerBuilder(Path(__file__).parent)
log_builder.formatters = {} # For example, we change the formatter to empty
app: FastAPI = FastAPI(base_folder=Path(__file__).parent, log_builder=log_builder)
# Or if you prefer the default, you can extend the default formatter
# For example, you can change the root level to DEBUG with default formatter
log_builder: LoggerBuilder = LoggerBuilder(Path(__file__).parent, config={"root": {"level": "DEBUG"}})
app: FastAPI = FastAPI(base_folder=Path(__file__).parent, log_builder=log_builder)
# Or pass as parameter start with the `logging_` keyword, it will be passed to the LoggerBuilder
# It is the same as the previous example
app: FastAPI = FastAPI(base_folder=Path(__file__).parent, logging_config={"root": {"level": "DEBUG"}})
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
iii_api_helper-0.0.12.dev0.tar.gz
(19.0 kB
view hashes)
Built Distribution
Close
Hashes for iii_api_helper-0.0.12.dev0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f68d243b73c816caf6b85ec29cd1b8f3d84d900f939668f421f3ba87145e927 |
|
MD5 | 96240c3a039382645483fe4d370c2482 |
|
BLAKE2b-256 | 9ffe70978690d375da221728086403c9597eb9b022ef55e5c094b98478febd06 |
Close
Hashes for iii_api_helper-0.0.12.dev0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | de5d9e144175dfbcfe463c1a122eb811feca4910d2d098feab939c9c82ac8340 |
|
MD5 | c032a6c2e33ae9260b7713d18dfa03fb |
|
BLAKE2b-256 | b43c2d8753c7d28f36e8506e4eb798f2c6a3ba9726b827c57dc09d8e4798e5b2 |