# QueryParameterModel
**QueryParameterModel** is a Python library designed to simplify the parsing of query parameters in FastAPI applications. Utilizing Pydantic for validation, this package offers a streamlined approach to handling query parameters, including automatic conversion of camelCase query parameters from the URL to Python's snake_case convention.
## Features
- **CamelCase to SnakeCase Conversion**: Automatically convert camelCase query parameters from the URL to Python's snake_case convention.
- **Flexible Parsing**: Parse query parameters into a full Pydantic model, which is particularly useful when handling multiple parameters in FastAPI routes.
- **List Support**: Easily handle query parameters that are lists.
- **FastAPI Integration**: Seamlessly integrate with FastAPI’s dependency injection system.
## Installation
You can install **QueryParameterModel** via pip:
```bash
pip install fastapi-query-parameter-model
```
## Usage
### Define Your Model
Create a model by inheriting from `QueryParameterModel` and define your query parameters. CamelCase query parameters from the URL will be converted to snake_case in your model.
```python
from fastapi_query_parameter_model import QueryParameterModel
from fastapi import Query
class SmallTestQueryParamsModels(QueryParameterModel):
title: str
age: int = 0
is_major: bool = Query(alias="isMajor", default=False)
array: list[str] = []
```
### Integrate with FastAPI
Use FastAPI’s dependency injection to parse query parameters into the model:
```python
from fastapi import FastAPI, Depends
from fastapi_query_parameter_model import QueryParameterModel
app = FastAPI()
@app.get("/small-test")
async def small_test(
query_params: SmallTestQueryParamsModels = Depends(SmallTestQueryParamsModels.parser)
):
return query_params.model_dump()
```
### Example Usage
Here are two example endpoints demonstrating how to use the models with FastAPI:
```python
from fastapi import FastAPI, Depends, Query
from fastapi_query_parameter_model import QueryParameterModel
app = FastAPI()
class SmallTestQueryParamsModels(QueryParameterModel):
title: str
age: int = 0
is_major: bool = Query(alias="isMajor", default=False)
array: list[str] = []
class FullTestQueryParamsModels(QueryParameterModel):
title: str
age: int = 0
is_major: bool = Query(alias="isMajor", default=False)
str_array: list[str] = Query(alias="strArray", default=[])
int_array: list[int] = Query(alias="intArray", default=[])
@app.get("/small-test")
async def small_test(
query_params: SmallTestQueryParamsModels = Depends(SmallTestQueryParamsModels.parser)
):
return query_params.model_dump()
@app.get("/full-test")
async def full_test(
query_params: FullTestQueryParamsModels = Depends(FullTestQueryParamsModels.parser)
):
return query_params.model_dump()
```
### Example Response
Using the following query : `small-test?title=thisATitle&isMajor=true&array[]=a&array[]=b&array[]=c`
For the `/small-test` route, a sample response might look like this:
```json
{
"title": "thisATitle",
"age": 0,
"is_major": true,
"array": ["a", "b", "c"]
}
```
This response shows how the query parameters are parsed and returned as a Pydantic model instance with snake_case attributes.
### Methods
- **`parser(request: Request)`**: Parses the query parameters from a FastAPI `Request` object and returns an instance of the model with converted snake_case attributes.
- **`model_dump()`**: This is a native pydantic function, only used in this example, to show the result of the parsed query parameters model
## Contributing
Contributions are welcome! To contribute:
1. Fork the repository.
2. Create a new branch for your feature or bug fix.
3. Commit your changes.
4. Push your branch and open a pull request.
Please ensure that your code adheres to the existing style and includes tests where applicable.
## License
**QueryParameterModel** is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
## Contact
For questions or feedback, feel free to open an issue on the [GitHub repository](https://github.com/PierroD/fastapi-query-parameter-model).
---
This revised README should more accurately reflect the functionality of your `QueryParameterModel` and help users understand its features and usage. Let me know if there are any other adjustments or additional details you’d like to include!
Raw data
{
"_id": null,
"home_page": "https://github.com/PierroD/fastapi-query-parameter-model",
"name": "fastapi-query-parameter-model",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": null,
"author": "Pierre DUVEAU",
"author_email": "pierre@duveau.org",
"download_url": "https://files.pythonhosted.org/packages/ff/7f/809799b4408db2bb26dd5e83bb7fa4cef3eec0f17f84b1d77a888298361b/fastapi_query_parameter_model-0.1.2.tar.gz",
"platform": null,
"description": "# QueryParameterModel\n\n**QueryParameterModel** is a Python library designed to simplify the parsing of query parameters in FastAPI applications. Utilizing Pydantic for validation, this package offers a streamlined approach to handling query parameters, including automatic conversion of camelCase query parameters from the URL to Python's snake_case convention.\n\n## Features\n\n- **CamelCase to SnakeCase Conversion**: Automatically convert camelCase query parameters from the URL to Python's snake_case convention.\n- **Flexible Parsing**: Parse query parameters into a full Pydantic model, which is particularly useful when handling multiple parameters in FastAPI routes.\n- **List Support**: Easily handle query parameters that are lists.\n- **FastAPI Integration**: Seamlessly integrate with FastAPI\u2019s dependency injection system.\n\n## Installation\n\nYou can install **QueryParameterModel** via pip:\n\n```bash\npip install fastapi-query-parameter-model\n```\n\n## Usage\n\n### Define Your Model\n\nCreate a model by inheriting from `QueryParameterModel` and define your query parameters. CamelCase query parameters from the URL will be converted to snake_case in your model.\n\n```python\nfrom fastapi_query_parameter_model import QueryParameterModel\nfrom fastapi import Query\n\nclass SmallTestQueryParamsModels(QueryParameterModel):\n title: str\n age: int = 0\n is_major: bool = Query(alias=\"isMajor\", default=False)\n array: list[str] = []\n```\n\n### Integrate with FastAPI\n\nUse FastAPI\u2019s dependency injection to parse query parameters into the model:\n\n```python\nfrom fastapi import FastAPI, Depends\nfrom fastapi_query_parameter_model import QueryParameterModel\n\napp = FastAPI()\n\n@app.get(\"/small-test\")\nasync def small_test(\n query_params: SmallTestQueryParamsModels = Depends(SmallTestQueryParamsModels.parser)\n):\n return query_params.model_dump()\n```\n\n### Example Usage\n\nHere are two example endpoints demonstrating how to use the models with FastAPI:\n\n```python\nfrom fastapi import FastAPI, Depends, Query\nfrom fastapi_query_parameter_model import QueryParameterModel\n\napp = FastAPI()\n\nclass SmallTestQueryParamsModels(QueryParameterModel):\n title: str\n age: int = 0\n is_major: bool = Query(alias=\"isMajor\", default=False)\n array: list[str] = []\n\nclass FullTestQueryParamsModels(QueryParameterModel):\n title: str\n age: int = 0\n is_major: bool = Query(alias=\"isMajor\", default=False)\n str_array: list[str] = Query(alias=\"strArray\", default=[])\n int_array: list[int] = Query(alias=\"intArray\", default=[])\n\n@app.get(\"/small-test\")\nasync def small_test(\n query_params: SmallTestQueryParamsModels = Depends(SmallTestQueryParamsModels.parser)\n):\n return query_params.model_dump()\n\n@app.get(\"/full-test\")\nasync def full_test(\n query_params: FullTestQueryParamsModels = Depends(FullTestQueryParamsModels.parser)\n):\n return query_params.model_dump()\n```\n\n### Example Response\n\nUsing the following query : `small-test?title=thisATitle&isMajor=true&array[]=a&array[]=b&array[]=c`\n\nFor the `/small-test` route, a sample response might look like this:\n\n```json\n{\n \"title\": \"thisATitle\",\n \"age\": 0,\n \"is_major\": true,\n \"array\": [\"a\", \"b\", \"c\"]\n}\n```\n\nThis response shows how the query parameters are parsed and returned as a Pydantic model instance with snake_case attributes.\n\n\n### Methods\n\n- **`parser(request: Request)`**: Parses the query parameters from a FastAPI `Request` object and returns an instance of the model with converted snake_case attributes.\n- **`model_dump()`**: This is a native pydantic function, only used in this example, to show the result of the parsed query parameters model\n\n## Contributing\n\nContributions are welcome! To contribute:\n\n1. Fork the repository.\n2. Create a new branch for your feature or bug fix.\n3. Commit your changes.\n4. Push your branch and open a pull request.\n\nPlease ensure that your code adheres to the existing style and includes tests where applicable.\n\n## License\n\n**QueryParameterModel** is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n\n## Contact\n\nFor questions or feedback, feel free to open an issue on the [GitHub repository](https://github.com/PierroD/fastapi-query-parameter-model).\n\n---\n\nThis revised README should more accurately reflect the functionality of your `QueryParameterModel` and help users understand its features and usage. Let me know if there are any other adjustments or additional details you\u2019d like to include!\n",
"bugtrack_url": null,
"license": null,
"summary": "A powerful and easy-to-use model for converting camelCase query parameters into snake_case parameters of an object",
"version": "0.1.2",
"project_urls": {
"Homepage": "https://github.com/PierroD/fastapi-query-parameter-model"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "797b8311bcd353230f985506227f71c6d85d3b16d380a7d756cbf930736d302b",
"md5": "50db5886b08233c96c92224de41c0c4c",
"sha256": "bdaeb404b2350595867810b9354e9db98d4182028f2c505fba8239bd47022b62"
},
"downloads": -1,
"filename": "fastapi_query_parameter_model-0.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "50db5886b08233c96c92224de41c0c4c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 5161,
"upload_time": "2024-08-31T13:25:59",
"upload_time_iso_8601": "2024-08-31T13:25:59.157233Z",
"url": "https://files.pythonhosted.org/packages/79/7b/8311bcd353230f985506227f71c6d85d3b16d380a7d756cbf930736d302b/fastapi_query_parameter_model-0.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ff7f809799b4408db2bb26dd5e83bb7fa4cef3eec0f17f84b1d77a888298361b",
"md5": "106ba9510f5a515185b77a538745fecf",
"sha256": "74adf4dcf747fdd867298f8d485d2f2362d31fb8f3aa83cf5055aa271d402a62"
},
"downloads": -1,
"filename": "fastapi_query_parameter_model-0.1.2.tar.gz",
"has_sig": false,
"md5_digest": "106ba9510f5a515185b77a538745fecf",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 3700,
"upload_time": "2024-08-31T13:26:00",
"upload_time_iso_8601": "2024-08-31T13:26:00.933506Z",
"url": "https://files.pythonhosted.org/packages/ff/7f/809799b4408db2bb26dd5e83bb7fa4cef3eec0f17f84b1d77a888298361b/fastapi_query_parameter_model-0.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-31 13:26:00",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "PierroD",
"github_project": "fastapi-query-parameter-model",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "fastapi-query-parameter-model"
}