FastAPI
It is recommended to create a virtual python environment to isolate python package installations with the local python packages. To do this, create a new venv
python environment in a folder/directory.
python3 -m venv .venv
A new .venv
folder/directory will be created in your current directory.
Info
If
python3
doesn't exist, trypython
orpy
, or check your python executable installation, and run the python installation you wish to use.
To activate the virtual environment, run the following command in a folder/ directory containing the .venv
folder/directory:
.venv/bin/activate
source .venv/bin/activate
source .venv/bin/activate
After creating and activating the python virtual environment, you can then proceed to install the needed packages to start the FastAPI server, with the following command:
pip install uvicorn fastapi
After installing the needed packages, a file can then be made, with the following content:
# Import the required libraries, we are using fastapi for this example
import os
import requests
from fastapi import FastAPI
from fastapi.responses import Response
from fastapi import Request
# Find your Application ID and API Key at <https://app.verihubs.com>
# and set as the Environment variables.
VERIHUBS_APP_ID = os.getenv("VERIHUBS_APP_ID")
VERIHUBS_API_KEY = os.getenv("VERIHUBS_API_KEY")
# Create the POST Request for integrating with Verihubs Service.
# Forward exactly the response from the Verihubs Service to avoid invalid
schema exception.
app = FastAPI()
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/liveness/face")
async def passive_liveness(request: Request):
response = post(
"https://api.verihubs.com/v1/face/liveness",
headers={
"App-ID": VERIHUBS_APP_ID,
"Api-Key": VERIHUBS_API_KEY,
},
data=await request.body(),
)
return Response(
status_code=response.status_code,
headers=dict(response.headers),
content=response.content,
)
@app.post("/{license_id}/check")
async def license(
license_id: str,
request: Request,
):
response = requests.post(
"<https://api.verihubs.com/v1/license/{license_id}/check>".format(license_id=license_id),
headers={
"App-ID": VERIHUBS_APP_ID,
"Api-Key": VERIHUBS_API_KEY,
},
json=await request.json(),
)
return Response(
status_code=response.status_code,
headers=dict(response.headers),
content=response.content,
)
Replace the endpoint with the correct endpoint, and replace <replace-me-with-app-id>
and <replace-me-with-api-key>
to the App-ID and APIKey provided for your company.
The proxy middleware can then be run using this command:
uvicorn main:app
Info
You can specify which port to serve the proxy middleware by adding
--port <port-number>
as an argument to the command.
Updated 18 days ago