fastapi-gateway-ultra


Namefastapi-gateway-ultra JSON
Version 0.1.1 PyPI version JSON
download
home_pagehttps://github.com/xitowzys-ISZF/FastAPI-Gateway-Ultra
SummaryFastAPI gateway for microservices.
upload_time2023-05-12 10:23:37
maintainer
docs_urlNone
authordotX12
requires_python>=3.7,<4.0
licenseMIT
keywords python fastapi gateway api microservice microservices server side discovery
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <p align="center">
<img src="https://scrutinizer-ci.com/g/dotX12/fastapi-gateway/badges/quality-score.png?b=master" alt="https://scrutinizer-ci.com/g/dotX12/fastapi-gateway/">
<img src="https://scrutinizer-ci.com/g/dotX12/fastapi-gateway/badges/code-intelligence.svg?b=master" alt="https://scrutinizer-ci.com/g/dotX12/fastapi-gateway/">
<img src="https://scrutinizer-ci.com/g/dotX12/fastapi-gateway/badges/build.png?b=master" alt="https://scrutinizer-ci.com/g/dotX12/fastapi-gateway/">
<img src="https://badge.fury.io/py/fastapi-gateway.svg" alt="https://badge.fury.io/py/fastapi-gateway">
<img src="https://pepy.tech/badge/fastapi-gateway" alt="https://pepy.tech/project/fastapi-gateway">
<img src="https://pepy.tech/badge/fastapi-gateway/month" alt="https://pepy.tech/project/fastapi-gateway">
<img src="https://img.shields.io/github/license/dotX12/fastapi-gateway.svg" alt="https://github.com/dotX12/fastapi-gateway/blob/master/LICENSE">

# ⚙️ fastapi-gateway-ultra is async single entry point for microservices.

#### 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-gateway-ultra
```

## ❗️ Benchmark
1.5k - 3k RPC.

```
gitshit@git ~ % wrk -t 4 -c 40 http://gateway.localtest.me:8003/gateway_endpoint/path_param/12
Running 10s test @ http://gateway.localtest.me:8003/gateway_endpoint/path_param/12
  4 threads and 40 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    30.11ms   33.65ms 312.55ms   95.98%
    Req/Sec   395.03    218.80     0.89k    73.21%
  15550 requests in 10.05s, 2.31MB read
Requests/sec:   1547.81
Transfer/sec:    235.81KB
```

## 💻 Example

<details> 
<summary>
<code>Example of use (long code)</code>
</summary>
<br>

```python3
from starlette import status
from starlette.requests import Request
from starlette.responses import Response
from fastapi_gateway 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,
    gateway_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
  ```

</details>

#### See more examples here:  
##### [Tests and instructions for launch](../master/tests)  
##### [Souce code Gateway](../master/tests/fastapi_gateway_service)  
##### [Souce code Microservice #1](../master/tests/fastapi_microservice)  

 ## 🪛 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").  
- **gateway_path** - is the path to bind gateway.  
For example, your gateway api is located here - *https://gateway.example.com* and the path to endpoint (**gateway_path**) - "/users" then the full way to this method will be - *https://gateway.example.com/users*
- **override_headers** - Boolean value allows you to return all the headlines that were created by microservice in gateway.  
- **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

⚠️ - **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]
 ```

<details> 
<summary>
<code>In more detail how to transmit body, form and query (photo)</code>
</summary>
<br>
<img width="450" height="456" src="https://user-images.githubusercontent.com/64792903/130335866-82be1684-cd54-43d3-8e0e-4013176a352a.jpg">
</details>

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/xitowzys-ISZF/FastAPI-Gateway-Ultra",
    "name": "fastapi-gateway-ultra",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7,<4.0",
    "maintainer_email": "",
    "keywords": "python,fastapi,gateway,api,microservice,microservices,server side discovery",
    "author": "dotX12",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/d0/61/0265b2134adcc39e66a5ff95ef4eeeb3f28e9f309033c3f307a9e528bec5/fastapi_gateway_ultra-0.1.1.tar.gz",
    "platform": null,
    "description": "<p align=\"center\">\n<img src=\"https://scrutinizer-ci.com/g/dotX12/fastapi-gateway/badges/quality-score.png?b=master\" alt=\"https://scrutinizer-ci.com/g/dotX12/fastapi-gateway/\">\n<img src=\"https://scrutinizer-ci.com/g/dotX12/fastapi-gateway/badges/code-intelligence.svg?b=master\" alt=\"https://scrutinizer-ci.com/g/dotX12/fastapi-gateway/\">\n<img src=\"https://scrutinizer-ci.com/g/dotX12/fastapi-gateway/badges/build.png?b=master\" alt=\"https://scrutinizer-ci.com/g/dotX12/fastapi-gateway/\">\n<img src=\"https://badge.fury.io/py/fastapi-gateway.svg\" alt=\"https://badge.fury.io/py/fastapi-gateway\">\n<img src=\"https://pepy.tech/badge/fastapi-gateway\" alt=\"https://pepy.tech/project/fastapi-gateway\">\n<img src=\"https://pepy.tech/badge/fastapi-gateway/month\" alt=\"https://pepy.tech/project/fastapi-gateway\">\n<img src=\"https://img.shields.io/github/license/dotX12/fastapi-gateway.svg\" alt=\"https://github.com/dotX12/fastapi-gateway/blob/master/LICENSE\">\n\n# \u2699\ufe0f fastapi-gateway-ultra is async single entry point for microservices.\n\n#### API Gateway performs many tasks: accepts, processes and distributes requests, controls traffic, monitors and controls access and security, caching, throttling.\n\nInitially, 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.\n\n## \ud83d\udcbf Installation\n\n```\npip install fastapi-gateway-ultra\n```\n\n## \u2757\ufe0f Benchmark\n1.5k - 3k RPC.\n\n```\ngitshit@git ~ % wrk -t 4 -c 40 http://gateway.localtest.me:8003/gateway_endpoint/path_param/12\nRunning 10s test @ http://gateway.localtest.me:8003/gateway_endpoint/path_param/12\n  4 threads and 40 connections\n  Thread Stats   Avg      Stdev     Max   +/- Stdev\n    Latency    30.11ms   33.65ms 312.55ms   95.98%\n    Req/Sec   395.03    218.80     0.89k    73.21%\n  15550 requests in 10.05s, 2.31MB read\nRequests/sec:   1547.81\nTransfer/sec:    235.81KB\n```\n\n## \ud83d\udcbb Example\n\n<details> \n<summary>\n<code>Example of use (long code)</code>\n</summary>\n<br>\n\n```python3\nfrom starlette import status\nfrom starlette.requests import Request\nfrom starlette.responses import Response\nfrom fastapi_gateway 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    gateway_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</details>\n\n#### See more examples here:  \n##### [Tests and instructions for launch](../master/tests)  \n##### [Souce code Gateway](../master/tests/fastapi_gateway_service)  \n##### [Souce code Microservice #1](../master/tests/fastapi_microservice)  \n\n ## \ud83e\ude9b 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- **gateway_path** - is the path to bind gateway.  \nFor example, your gateway api is located here - *https://gateway.example.com* and the path to endpoint (**gateway_path**) - \"/users\" then the full way to this method will be - *https://gateway.example.com/users*\n- **override_headers** - Boolean value allows you to return all the headlines that were created by microservice in gateway.  \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\n\u26a0\ufe0f - **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<details> \n<summary>\n<code>In more detail how to transmit body, form and query (photo)</code>\n</summary>\n<br>\n<img width=\"450\" height=\"456\" src=\"https://user-images.githubusercontent.com/64792903/130335866-82be1684-cd54-43d3-8e0e-4013176a352a.jpg\">\n</details>\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "FastAPI gateway for microservices.",
    "version": "0.1.1",
    "project_urls": {
        "Homepage": "https://github.com/xitowzys-ISZF/FastAPI-Gateway-Ultra",
        "Repository": "https://github.com/xitowzys-ISZF/FastAPI-Gateway-Ultra"
    },
    "split_keywords": [
        "python",
        "fastapi",
        "gateway",
        "api",
        "microservice",
        "microservices",
        "server side discovery"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f02802c538f4024b9b8de752ac6fc2e8dbea6ea6f8ecec869b173535d24d0730",
                "md5": "e5970a91e77a2b3c1ba5fd1106eda79d",
                "sha256": "d3e8440f0ad9db9f990ad2229fb98a2ebffa36ee0afe11d66480365865e9f356"
            },
            "downloads": -1,
            "filename": "fastapi_gateway_ultra-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e5970a91e77a2b3c1ba5fd1106eda79d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7,<4.0",
            "size": 13811,
            "upload_time": "2023-05-12T10:23:35",
            "upload_time_iso_8601": "2023-05-12T10:23:35.042969Z",
            "url": "https://files.pythonhosted.org/packages/f0/28/02c538f4024b9b8de752ac6fc2e8dbea6ea6f8ecec869b173535d24d0730/fastapi_gateway_ultra-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d0610265b2134adcc39e66a5ff95ef4eeeb3f28e9f309033c3f307a9e528bec5",
                "md5": "7bc5ef3e712f1e034e01158f10a61caf",
                "sha256": "e39529ebbaf473deed420bbd7b5752ee115dd15fb60ed43496d20610b3a2e5f4"
            },
            "downloads": -1,
            "filename": "fastapi_gateway_ultra-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "7bc5ef3e712f1e034e01158f10a61caf",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7,<4.0",
            "size": 9802,
            "upload_time": "2023-05-12T10:23:37",
            "upload_time_iso_8601": "2023-05-12T10:23:37.779589Z",
            "url": "https://files.pythonhosted.org/packages/d0/61/0265b2134adcc39e66a5ff95ef4eeeb3f28e9f309033c3f307a9e528bec5/fastapi_gateway_ultra-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-05-12 10:23:37",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "xitowzys-ISZF",
    "github_project": "FastAPI-Gateway-Ultra",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "fastapi-gateway-ultra"
}
        
Elapsed time: 0.10120s