Skip to main content

FastAPI-Query-Conditions is a dependency that parses a query string into conditions using operators enclosed in square brackets

Project description

fastapi-query-conditions

Introduction

FastAPI-Query-Conditions is a dependency that parses a query string into conditions using operators enclosed in square brackets. For example, if you send a request to /orders?amount[gte]=1000&amount[lt]=2000, you can use the query string as parsed conditions like this:

{"gte":1000,"lt":2000}

Install

> pip install fastapi-query-conditions

Quick Start

from typing import Dict

from fastapi import Depends, FastAPI
from fastapi_query_conditions import query_conditions

app = FastAPI()

@app.get("/items")
def read_root(amount: Dict[str, int] = Depends(query_conditions(field='amount', factory=int))):
    print(amount)
    return amount

Then, if you send a request to /items?amount[gte]=1000&amount[lt]=2000, you can check the following results.

{'gte': 1000, 'lt': 2000}

Also, you can use various factory functions for your query parameter.

from typing import Dict

from fastapi import Depends, FastAPI
from fastapi_query_conditions import query_conditions
from datetime import datetime

app = FastAPI()

@app.get("/orders")
def read_root(time: Dict[str, int] = Depends(query_conditions(field='time', factory=datetime.fromisoformat))):
    print(time)
    return time

Then, if you send a request to /orders?time[gte]=2022-10-18T12:00&time[lt]=2022-10-18T12:30, you can check the following results.

{'gte': datetime.datetime(2022, 10, 18, 12, 0), 'lt': datetime.datetime(2022, 10, 18, 12, 30)}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi-query-conditions-1.0.0.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

fastapi_query_conditions-1.0.0-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file fastapi-query-conditions-1.0.0.tar.gz.

File metadata

File hashes

Hashes for fastapi-query-conditions-1.0.0.tar.gz
Algorithm Hash digest
SHA256 26daefe1c84e9a69efcb54fdc8799f7503c2bfb9d79acc4a6989f02bbdb85fa8
MD5 aa90953739538b046626a92349ce85b0
BLAKE2b-256 c22b40c2db97fb8f89046615c21dd3451072b5518333b9bfc59329033f62ca06

See more details on using hashes here.

Provenance

File details

Details for the file fastapi_query_conditions-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_query_conditions-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dc38ffae54b6455896358720edf5867f04c36559d523ef09f74eceafff4516cd
MD5 ef75fa88c1feb1621c7c2ff3f03ce6cc
BLAKE2b-256 bcd91d7bcb29136f5af891cbc66dd1ff984f02f7e0b4e7e544bbc1c24335f261

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page