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:
from fastapi import FastAPI, Request
from fastapi.responses import Response
from fastapi.middleware.cors import CORSMiddleware
from requests import post
app = FastAPI()
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/license/{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,
)
@app.patch("/ocr/extract/correction")
async def correction(request: Request):
response = post(
"https://api.verihubs.com/ktp/id/extract/correction",
headers={
"App-ID": "<replace-me-with-app-id>",
"Api-Key": "<replace-me-with-api-key>",
},
data=await request.body(),
)
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 3 months ago