Proxy Middleware
Overview
The main purpose to build SDK Proxy is to isolate the credentials information from the client and combine it
with your own business needs.
Getting Started
Middleware can be built using other programming language. This is documentation how to create proxy middleware using FastAPI with Python programming language.
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 .venvA new .venv folder/directory will be created in your current directory.
InfoIf
python3doesn't exist, trypythonorpy, 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/activatesource .venv/bin/activatesource .venv/bin/activateAfter 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 fastapiAfter installing the needed packages, a file can then be made, with the following content:
For forwarding the response, please do not change the response value from Verihubs Liveness
Service. Additional json field might be added, so please ignore them while checking the
existing fields.
# 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={
"Accept": "application/json",
"Content-Type": "application/json",
"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 "VERIHUBS_APP_ID" in line 11 and "VERIHUBS_API_KEY" in line 12 to the App-ID and API Key provided for your company.
The proxy middleware can then be run using this command:
uvicorn main:app
InfoYou can specify which port to serve the proxy middleware by adding
--port <port-number>as an argument to the command.
Updated about 13 hours ago
