This project has been archived by its maintainers, and is no longer receiving any updates.
Fastapi-proxy is async single entry point for microservices
(This is a fork fastapi-gateway).
API Gateway performs many tasks:
- accepts, processes and distributes requests
- controls traffic
- monitors and controls access and security
- caching
- throttling.
Initially, this project was created for myself, I needed to implement identification, authentication and authorization.
In the future, there was a need to limit requests for each user on every endpoint, create API plans.
There were a lot of microservices and to keep in each microservice the logic for limiting endpoints, security logic, logging etc. - meaningless.
Therefore, all this functionality is located at a single entry point, which already implements all the necessary tasks with security, limiting, etc., while microservices now directly solve their tasks.
Installation
pip install fastapi_proxy
Example
Example of use (long code)
from starlette import status
from starlette.requests import Request
from starlette.responses import Response
from fastapi_proxy import route
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi import Depends
from fastapi.security import APIKeyHeader
from starlette import status
from starlette.exceptions import HTTPException
app = FastAPI(title='API Gateway')
SERVICE_URL = "http://microservice.localtest.me:8002"
API_KEY_NAME = "x-api-key"
api_key_header = APIKeyHeader(
name=API_KEY_NAME,
auto_error=False
)
def check_api_key(key: str = Depends(api_key_header)):
if key:
return key
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="You didn't pass the api key in the header! Header: x-api-key",
)
class FooModel(BaseModel):
example_int: int
example_str: str
@route(
request_method=app.post,
service_url=SERVICE_URL,
proxy_path='/query_and_body_path/{path}',
service_path='/v1/query_and_body_path/{path}',
query_params=['query_int', 'query_str'],
body_params=['test_body'],
status_code=status.HTTP_200_OK,
tags=['Query', 'Body', 'Path'],
dependencies=[
Depends(check_api_key)
],
)
async def check_query_params_and_body(
path: int, query_int: int, query_str: str,
test_body: FooModel, request: Request, response: Response
):
pass
How to use?
- request_method - is a callable (like app.get, app.post, foo_router.patch and so on.).
- service_url - the path to the endpoint on another service (like "https://microservice1.example.com").
- service_path - the path to the method in microservice (like "/v1/microservice/users").
- proxy_path - is the path to bind proxy.
- override_headers - Boolean value allows you to return all the headlines that were created by microservice in proxy.
- query_params - used to extract query parameters from endpoint and transmission to microservice
- form_params - used to extract form model parameters from endpoint and transmission to microservice
- param body_params - used to extract body model from endpoint and transmission to microservice
For example, your proxy api is located here - https://proxy.example.com and the path to endpoint (proxy_path) - "/users" then the full way to this method will be - https://proxy.example.com/users
⚠ Be sure to transfer the name of the argument to the router, which is in the endpoint func!
query_params - List[Query]
body_params - List[Body]
form_params - List[File, Form]
Build and publish
Refer to https://packaging.python.org/en/latest/tutorials/packaging-projects/
Step:
- python -m build
- python -m twine upload --repository pypi dist/*
Metadata
Release files for fastapi-proxy 0.103.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| fastapi-proxy-0.103.1.0.tar.gz | 12.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_proxy-0.103.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 26.7 kB
Release files / fastapi-proxy-0.103.1.0.tar.gz
| Download URL | fastapi-proxy-0.103.1.0.tar.gz |
|---|---|
| Size | 12.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f2c2d381dd12cb7c3f2214677fd8bb7dfd56a37e66b0df46c4481abadeebaafb
|
|
BLAKE2b-256 checksum How to use checksums |
890abeda161d9d7b7a75c350251458185eeceeec253fa1a3cccba0775494a909
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.10
|
Release files / fastapi_proxy-0.103.1.0-py3-none-any.whl
| Download URL | fastapi_proxy-0.103.1.0-py3-none-any.whl |
|---|---|
| Size | 14.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
83954d6ca942bf993572efb7c64d6deadaa4d24e0bb65479d373267550fca2e8
|
|
BLAKE2b-256 checksum How to use checksums |
3abf698e482a2d279530ee267460c2a014be6fabceb2a68a038fc8206dff2c17
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.10
|
Security for fastapi-proxy 0.103.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.