# Dequest
Dequest is a full featured declarative rest client for Python that streamlines the creation of HTTP requests and retrieves the results as DTO. Here is the package's features:
✅ Supports GET, POST, PUT, PATCH and DELETE requests
✅ Optional Caching for GET Requests
✅ Authentication (Static & Dynamic)
✅ Maps API response to DTO object and list (Supports unlimited nested DTOs)
✅ support query parameters, JSON body and Form-data
✅ Implements Retry & Timeout Handling
✅ Allows Custom Headers per Request (Static & Dynamic)
## Installation
Run following command to install **dequest** :
```bash
pip install dequest
```
## Usage
Declare a function with `@sync_client` decorator and pass the `dto_class` parameter to map the response to a DTO. You can also pass the `method`, `timeout`, `retries`, `retry_delay`, `auth_token`, `api_key`, `default_headers`, and `enable_cache` parameters.
```python
from dequest.clients import sync_client
class UserDto:
name: str
address: AddressDto
friends: list[str]
def __init__(self, name, address, friends):
self.name = name
self.address = address
self.friends = friends
@sync_client(dto_class=UserDto)
def get_user(user_id):
return {
"url": f"https://jsonplaceholder.typicode.com/users/{user_id}",
}
user = get_user(1)
# Retrieving a list of users by city name using query parameters:
@sync_client(dto_class=UserDto)
def get_users(city_name) -> List[UserDto]:
return {
"url": f"https://jsonplaceholder.typicode.com/users/",
"params": {"city": city_name}, # Query parameter to filter by city name
}
users = get_users("Paris")
```
### Cache
To enable caching, set the `enable_cache` parameter to `True` in the `sync_client` decorator. You can also pass the `cache_ttl` parameter to set the cache expiration time in seconds, the default value is None which means no expiration. Dequest supports `redis` and `in_memory` cache drivers, wich can be configured in `dequest.config.DequestConfig` which is a static class. The default cache provider is `in_memory`.
```python
from dequest.clients import sync_client
from dequest.config import DequestConfig
DequestConfig.cache_driver = "redis"
@sync_client(dto_class=UserDto, enable_cache=True, cache_ttl=300)
def get_user(user_id):
return {
"url": f"https://jsonplaceholder.typicode.com/users/{user_id}",
}
user = get_user(1)
```
### Authentication
To add authentication, pass the `auth_token` parameter to the `sync_client` decorator. You can also pass the `api_key` parameter to add an API key to the request headers.
Static authentication:
```python
from dequest.clients import sync_client
@sync_client(dto_class=UserDto, auth_token="my_auth_token")
def get_user(user_id) -> UserDto:
return {
"url": f"https://jsonplaceholder.typicode.com/users/{user_id}",
}
user = get_user(1)
```
Dynamic authentication token generation:
```python
from dequest.clients import sync_client
def get_auth_token():
return "my_auth_token"
@sync_client(dto_class=UserDto, auth_token=get_auth_token)
def get_user(user_id):
return {
"url": f"https://jsonplaceholder.typicode.com/users/{user_id}",
}
```
## License
Dequest is released under the [BSD 3-Clause License](https://opensource.org/licenses/BSD-3-Clause).
Raw data
{
"_id": null,
"home_page": "https://github.com/birddevelper/dequest",
"name": "dequest",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "request, declarative, api, rest, rest client",
"author": "M.Shaeri",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/a7/56/2c66acf5beaef067f7492a834174eac07c54f098a3524d89f7601f05f51d/dequest-0.1.0.tar.gz",
"platform": null,
"description": "# Dequest\nDequest is a full featured declarative rest client for Python that streamlines the creation of HTTP requests and retrieves the results as DTO. Here is the package's features:\n\n\u2705 Supports GET, POST, PUT, PATCH and DELETE requests\n\n\u2705 Optional Caching for GET Requests\n\n\u2705 Authentication (Static & Dynamic)\n\n\u2705 Maps API response to DTO object and list (Supports unlimited nested DTOs)\n\n\u2705 support query parameters, JSON body and Form-data\n\n\u2705 Implements Retry & Timeout Handling\n\n\u2705 Allows Custom Headers per Request (Static & Dynamic)\n\n\n\n## Installation\nRun following command to install **dequest** :\n\n```bash\npip install dequest\n```\n\n## Usage\n\nDeclare a function with `@sync_client` decorator and pass the `dto_class` parameter to map the response to a DTO. You can also pass the `method`, `timeout`, `retries`, `retry_delay`, `auth_token`, `api_key`, `default_headers`, and `enable_cache` parameters.\n\n```python\nfrom dequest.clients import sync_client\n\nclass UserDto:\n name: str\n address: AddressDto\n friends: list[str]\n\n def __init__(self, name, address, friends):\n self.name = name\n self.address = address\n self.friends = friends\n\n\n@sync_client(dto_class=UserDto)\ndef get_user(user_id):\n return {\n \"url\": f\"https://jsonplaceholder.typicode.com/users/{user_id}\",\n }\n\nuser = get_user(1)\n\n# Retrieving a list of users by city name using query parameters:\n\n@sync_client(dto_class=UserDto)\ndef get_users(city_name) -> List[UserDto]:\n return {\n \"url\": f\"https://jsonplaceholder.typicode.com/users/\",\n \"params\": {\"city\": city_name}, # Query parameter to filter by city name\n }\n\nusers = get_users(\"Paris\")\n```\n\n### Cache\n\nTo enable caching, set the `enable_cache` parameter to `True` in the `sync_client` decorator. You can also pass the `cache_ttl` parameter to set the cache expiration time in seconds, the default value is None which means no expiration. Dequest supports `redis` and `in_memory` cache drivers, wich can be configured in `dequest.config.DequestConfig` which is a static class. The default cache provider is `in_memory`.\n\n```python\nfrom dequest.clients import sync_client\nfrom dequest.config import DequestConfig\n\nDequestConfig.cache_driver = \"redis\"\n\n@sync_client(dto_class=UserDto, enable_cache=True, cache_ttl=300)\ndef get_user(user_id):\n return {\n \"url\": f\"https://jsonplaceholder.typicode.com/users/{user_id}\",\n }\n\nuser = get_user(1)\n```\n\n### Authentication\n\nTo add authentication, pass the `auth_token` parameter to the `sync_client` decorator. You can also pass the `api_key` parameter to add an API key to the request headers.\n\nStatic authentication:\n\n```python\nfrom dequest.clients import sync_client\n\n@sync_client(dto_class=UserDto, auth_token=\"my_auth_token\")\ndef get_user(user_id) -> UserDto:\n return {\n \"url\": f\"https://jsonplaceholder.typicode.com/users/{user_id}\",\n }\n\nuser = get_user(1)\n```\n\nDynamic authentication token generation:\n\n```python\nfrom dequest.clients import sync_client\n\ndef get_auth_token():\n return \"my_auth_token\"\n\n@sync_client(dto_class=UserDto, auth_token=get_auth_token)\ndef get_user(user_id):\n return {\n \"url\": f\"https://jsonplaceholder.typicode.com/users/{user_id}\",\n }\n```\n\n## License\n\nDequest is released under the [BSD 3-Clause License](https://opensource.org/licenses/BSD-3-Clause).\n",
"bugtrack_url": null,
"license": "GNU",
"summary": "Declarative rest client",
"version": "0.1.0",
"project_urls": {
"Download": "https://github.com/birddevelper/dequest",
"Homepage": "https://github.com/birddevelper/dequest"
},
"split_keywords": [
"request",
" declarative",
" api",
" rest",
" rest client"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "48a016190a96c87c0a87b88f5a7a120860390cba8e0d5fc2c363706b2ccc00b9",
"md5": "160c7a5b62d6cdf3b66f1e33ddfd4a6f",
"sha256": "b58d8e5e4a781ab410093243e8192bef4db20c97825769da212061924837ca3c"
},
"downloads": -1,
"filename": "dequest-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "160c7a5b62d6cdf3b66f1e33ddfd4a6f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 11717,
"upload_time": "2025-02-09T22:46:40",
"upload_time_iso_8601": "2025-02-09T22:46:40.862238Z",
"url": "https://files.pythonhosted.org/packages/48/a0/16190a96c87c0a87b88f5a7a120860390cba8e0d5fc2c363706b2ccc00b9/dequest-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "a7562c66acf5beaef067f7492a834174eac07c54f098a3524d89f7601f05f51d",
"md5": "749fe9b098e9595cc8318ad53af3243a",
"sha256": "e533d04c3d864f75f608440c28f36b5309f210cbd8950c8017781139264ad16b"
},
"downloads": -1,
"filename": "dequest-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "749fe9b098e9595cc8318ad53af3243a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 10597,
"upload_time": "2025-02-09T22:46:42",
"upload_time_iso_8601": "2025-02-09T22:46:42.985561Z",
"url": "https://files.pythonhosted.org/packages/a7/56/2c66acf5beaef067f7492a834174eac07c54f098a3524d89f7601f05f51d/dequest-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-09 22:46:42",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "birddevelper",
"github_project": "dequest",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "dequest"
}