# Fastapi-proxy is async single entry point for microservices
(This is a fork [fastapi-gateway](https://github.com/dotX12/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
<code>Example of use (long code)</code>
```python3
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]
```
<img width="450" height="456" src="https://user-images.githubusercontent.com/64792903/130335866-82be1684-cd54-43d3-8e0e-4013176a352a.jpg">
## Build and publish
Refer to [https://packaging.python.org/en/latest/tutorials/packaging-projects/](https://packaging.python.org/en/latest/tutorials/packaging-projects/)
Step:
- python -m build
- python -m twine upload --repository pypi dist/*
Raw data
{
"_id": null,
"home_page": "https://github.com/cedvict/fastapi-proxy",
"name": "fastapi-proxy",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": "Cedvict Loknov <cedvict@gmail.com>",
"keywords": "FastAPI,Proxy,Gateway,Microservice",
"author": "Cedvict Loknov",
"author_email": "Cedvict Loknov <cedvict@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/89/0a/beda161d9d7b7a75c350251458185eeceeec253fa1a3cccba0775494a909/fastapi-proxy-0.103.1.0.tar.gz",
"platform": null,
"description": "# Fastapi-proxy is async single entry point for microservices \n(This is a fork [fastapi-gateway](https://github.com/dotX12/fastapi-gateway)).\n\nAPI Gateway performs many tasks:\n- accepts, processes and distributes requests\n- controls traffic\n- monitors and controls access and security\n- caching\n- throttling.\n\nInitially, this project was created for myself, I needed to implement identification, authentication and authorization.\n\nIn the future, there was a need to limit requests for each user on every endpoint, create API plans.\n\nThere were a lot of microservices and to keep in each microservice the logic for limiting endpoints, security logic, logging etc. - meaningless.\n\nTherefore, 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.\n\n## Installation\n\n```\npip install fastapi_proxy\n```\n\n## Example\n\n\n<code>Example of use (long code)</code>\n\n\n```python3\nfrom starlette import status\nfrom starlette.requests import Request\nfrom starlette.responses import Response\nfrom fastapi_proxy import route\nfrom fastapi import FastAPI\nfrom pydantic import BaseModel\nfrom fastapi import Depends\nfrom fastapi.security import APIKeyHeader\nfrom starlette import status\nfrom starlette.exceptions import HTTPException\n\napp = FastAPI(title='API Gateway')\nSERVICE_URL = \"http://microservice.localtest.me:8002\"\n\nAPI_KEY_NAME = \"x-api-key\"\n\napi_key_header = APIKeyHeader(\n name=API_KEY_NAME,\n auto_error=False\n)\n\n\ndef check_api_key(key: str = Depends(api_key_header)):\n if key:\n return key\n raise HTTPException(\n status_code=status.HTTP_401_UNAUTHORIZED,\n detail=\"You didn't pass the api key in the header! Header: x-api-key\",\n )\n\n\nclass FooModel(BaseModel):\n example_int: int\n example_str: str\n\n\n@route(\n request_method=app.post,\n service_url=SERVICE_URL,\n proxy_path='/query_and_body_path/{path}',\n service_path='/v1/query_and_body_path/{path}',\n query_params=['query_int', 'query_str'],\n body_params=['test_body'],\n status_code=status.HTTP_200_OK,\n tags=['Query', 'Body', 'Path'],\n dependencies=[\n Depends(check_api_key)\n ],\n)\nasync def check_query_params_and_body(\n path: int, query_int: int, query_str: str,\n test_body: FooModel, request: Request, response: Response\n):\n pass\n ```\n\n\n ## How to use?\n\n- **request_method** - is a callable (like app.get, app.post, foo_router.patch and so on.). \n- **service_url** - the path to the endpoint on another service (like \"https://microservice1.example.com\"). \n- **service_path** - the path to the method in microservice (like \"/v1/microservice/users\"). \n- **proxy_path** - is the path to bind proxy.\n- **override_headers** - Boolean value allows you to return all the headlines that were created by microservice in proxy. \n- **query_params** - used to extract query parameters from endpoint and transmission to microservice\n- **form_params** - used to extract form model parameters from endpoint and transmission to microservice\n- **param body_params** - used to extract body model from endpoint and transmission to microservice\n\nFor 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*\n\n\u26a0 **Be sure to transfer the name of the argument to the router, which is in the endpoint func!** \n\n```\nquery_params - List[Query]\nbody_params - List[Body]\nform_params - List[File, Form]\n ```\n\n\n<img width=\"450\" height=\"456\" src=\"https://user-images.githubusercontent.com/64792903/130335866-82be1684-cd54-43d3-8e0e-4013176a352a.jpg\">\n\n## Build and publish\n\nRefer to [https://packaging.python.org/en/latest/tutorials/packaging-projects/](https://packaging.python.org/en/latest/tutorials/packaging-projects/)\n\nStep:\n- python -m build\n- python -m twine upload --repository pypi dist/*\n",
"bugtrack_url": null,
"license": "",
"summary": "FastAPI Proxy",
"version": "0.103.1.0",
"project_urls": {
"Documentation": "https://fastapi-proxy.nnenu.com/",
"Homepage": "https://github.com/cedvict/fastapi-proxy"
},
"split_keywords": [
"fastapi",
"proxy",
"gateway",
"microservice"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "3abf698e482a2d279530ee267460c2a014be6fabceb2a68a038fc8206dff2c17",
"md5": "e561bf67d52cfd48cc2867ccbf7dd1c9",
"sha256": "83954d6ca942bf993572efb7c64d6deadaa4d24e0bb65479d373267550fca2e8"
},
"downloads": -1,
"filename": "fastapi_proxy-0.103.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e561bf67d52cfd48cc2867ccbf7dd1c9",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 14194,
"upload_time": "2023-09-25T12:26:14",
"upload_time_iso_8601": "2023-09-25T12:26:14.965114Z",
"url": "https://files.pythonhosted.org/packages/3a/bf/698e482a2d279530ee267460c2a014be6fabceb2a68a038fc8206dff2c17/fastapi_proxy-0.103.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "890abeda161d9d7b7a75c350251458185eeceeec253fa1a3cccba0775494a909",
"md5": "3d0fc82611d13ea0c5109594439a5808",
"sha256": "f2c2d381dd12cb7c3f2214677fd8bb7dfd56a37e66b0df46c4481abadeebaafb"
},
"downloads": -1,
"filename": "fastapi-proxy-0.103.1.0.tar.gz",
"has_sig": false,
"md5_digest": "3d0fc82611d13ea0c5109594439a5808",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 12517,
"upload_time": "2023-09-25T12:26:16",
"upload_time_iso_8601": "2023-09-25T12:26:16.557047Z",
"url": "https://files.pythonhosted.org/packages/89/0a/beda161d9d7b7a75c350251458185eeceeec253fa1a3cccba0775494a909/fastapi-proxy-0.103.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-09-25 12:26:16",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "cedvict",
"github_project": "fastapi-proxy",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "fastapi-proxy"
}