Name | request-casting JSON |
Version |
0.2.0
JSON |
| download |
home_page | |
Summary | Provide method to cast django request data for POST and GET methods |
upload_time | 2023-11-18 07:09:17 |
maintainer | |
docs_url | None |
author | turulomio |
requires_python | >=3.11,<4.0 |
license | GPL-3.0 license |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# request_casting
Python module that allows to cast django request data for POST and GET methods easyly
Allows you to capture errors in parameters and give them default values, to protect your application with little code and easy to read.
## Installation
You can use pip to install this module
```bash
pip install request_casting
```
## Casts
### RequestBool
Gets a parameter from a djangorestframework or django view and cast it to a bool object. For example
```python
from request_casting import request_casting
from rest_framework.decorators import api_view
from rest_framework.response import Response
@api_view(['GET', 'POST'])
def myview(request):
a=request_casting.RequestBool(request, "a")
return Response({"a": a, "class": a.__class__.__name__}, status=status.HTTP_200_OK)
```
You can call this view with a GET method with requests, curl, axios...
```bash
curl http://localhost:8000/myview/?a=true
curl -X POST http://localhost:8000/myview/ -d"a=false"
```
You'll get this answer in both cases
```
{"a":true,"class":"bool"}
```
All request_casting methods allow to set a default value. By default this value is None in all Request methods. This value is returned when cast fails.
```bash
curl http://localhost:8000/myview/?a=BADBOOL
```
You'll get this answer in both cases
```
{"a":null,"class":"NoneType"}
```
### RequestDate
Use this method inside a view to get a casted date. Use dates in Iso format
```python
# ... The same as RequestBool example
a=request_casting.RequestDate(request, "a")
```
You'll get this answers
```
curl http://localhost:8000/myview/?a=BADDATE => a will be None
curl http://localhost:8000/myview/?a=2021-1-1 => a will be date(2023,1,1)
```
### RequestDecimal
Use this method inside a view to get a casted Decimal
```python
# ... The same as RequestBool example
a=request_casting.RequestDecimal(request, "a", Decimal(0))
```
You'll get this answers
```
curl http://localhost:8000/myview/?a=12.1212 => a will be Decimal(12.1212)
curl http://localhost:8000/myview/?a=2021-1-1 => a will be Decimal(0)
```
### RequestDtaware
Use this method inside a view to get a datetime with timezone. Use dates in Iso format
```python
# ... The same as RequestBool example
a=request_casting.RequestDtaware(request, "a")
```
You'll get this answers
```
curl http://localhost:8000/myview/?a=2011-10-05T14:48:00.000Z => a will be a datetime with timezone
curl http://localhost:8000/myview/?a=2021-1-1 => a will be None
```
### RequestInteger
Use this method inside a view to get a casted Integer
```python
# ... The same as RequestBool example
a=request_casting.RequestInteger(request, "a")
```
You'll get this answers
```
curl http://localhost:8000/myview/?a=12 => a will be 12
curl http://localhost:8000/myview/?a=BADINTEGER => a will be None
```
### RequestListOfBools
Use this method inside a view to get a list of Booleans
```python
# ... The same as RequestBool example
a=request_casting.RequesListOfBools(request, "a")
```
You'll get this answers
``` bash
curl "http://localhost:8000/myview/?a[]=true&a[]=false" => a will be a list [True,False]
```
### RequestListOfIntegers
Use this method inside a view to get a list of Integers
```python
# ... The same as RequestBool example
a=request_casting.RequestListOfIntegers(request, "a")
```
You'll get this answers
``` bash
curl "http://localhost:8000/myview/?a[]=1&a[]=2" => a will be a list [1,2]
```
### RequestListOfStrings
Use this method inside a view to get a list of strings
```python
# ... The same as RequestBool example
a=request_casting.RequestListOfStrings(request, "a")
```
You'll get this answers
``` bash
curl "http://localhost:8000/myview/?a[]=a&a[]=b" => a will be a list ["a","b"]
```
### RequestString
Use this method inside a view to get a casted String
```python
# ... The same as RequestBool example
a=request_casting.RequestString(request, "a")
```
You'll get this answers
```
curl http://localhost:8000/myview/?a=12 => a will be "12"
curl http://localhost:8000/myview/?a=BADINTEGER => a will be "BADINTEGER"
```
### RequestUrl
Use this method inside a view to get a django model object using its hyperlinked url
```python
# ... The same as RequestBool example
a=request_casting.RequestUrl(request, "a", models.Record)
```
You'll get this answers
``` bash
curl "http://localhost:8000/myview/?a=http://localhost:8000/api/record/1/" => a will be a Record object with pk=1
```
### RequestListOfUrls
Use this method inside a view to get a list of django model object using its hyperlinked url
```python
# ... The same as RequestBool example
a=request_casting.RequestListOfUrls(request, "a",models.Record)
```
You'll get this answers
``` bash
curl "http://localhost:8000/myview/?a[]=http://localhost:8000/api/record/1/&a[]=http://localhost:8000/api/record/2/" => a will be a list with Record objects with pk=1 and pk=2
```
## Other usefull functions
### all_args_are_not_empty
Returns True if all function arguments are different to None and ""
It's very usefull to compare view parameters fast.
```python
request_casting.all_args_are_not_empty(None, "", None) #Returns False
request_casting.all_args_are_not_empty("", "", "")# Returns False
request_casting.all_args_are_not_empty(1, 1, 1) #Return True
```
### all_args_are_not_none
Returns True if all function arguments are different to None
It's very usefull to compare view parameters fast.
```python
request_casting.all_args_are_not_none(None, "", None) #Returns False
request_casting.all_args_are_not_none("", "", "")# Returns True
request_casting.all_args_are_not_none(1, 1, 1) #Return True
```
## Test module
Run `poe coverage` to test module.
## Changelog
### 0.2.0 (2023-11-18)
- Improving documentation
- All default values are set to None, including RequestList methods
- string2dtaware now uses ZoneInfo instead of pytz
### 0.1.0 (2023-11-15)
- Converted reusingcode module to an independent module
Raw data
{
"_id": null,
"home_page": "",
"name": "request-casting",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.11,<4.0",
"maintainer_email": "",
"keywords": "",
"author": "turulomio",
"author_email": "turulomio@yahoo.es",
"download_url": "https://files.pythonhosted.org/packages/25/91/696ec84709921baefee7dc0baab41577d146d8a2c4a0e476a9f5f845cf91/request_casting-0.2.0.tar.gz",
"platform": null,
"description": "# request_casting\nPython module that allows to cast django request data for POST and GET methods easyly\n\nAllows you to capture errors in parameters and give them default values, to protect your application with little code and easy to read.\n\n\n## Installation\nYou can use pip to install this module\n```bash\npip install request_casting\n```\n\n## Casts\n\n### RequestBool\nGets a parameter from a djangorestframework or django view and cast it to a bool object. For example\n\n```python \nfrom request_casting import request_casting\nfrom rest_framework.decorators import api_view\nfrom rest_framework.response import Response\n@api_view(['GET', 'POST']) \ndef myview(request):\n a=request_casting.RequestBool(request, \"a\")\n return Response({\"a\": a, \"class\": a.__class__.__name__}, status=status.HTTP_200_OK)\n```\n\nYou can call this view with a GET method with requests, curl, axios...\n\n```bash\ncurl http://localhost:8000/myview/?a=true\ncurl -X POST http://localhost:8000/myview/ -d\"a=false\"\n```\n\nYou'll get this answer in both cases\n\n```\n{\"a\":true,\"class\":\"bool\"}\n```\n\nAll request_casting methods allow to set a default value. By default this value is None in all Request methods. This value is returned when cast fails.\n\n```bash\ncurl http://localhost:8000/myview/?a=BADBOOL\n```\n\nYou'll get this answer in both cases\n\n```\n{\"a\":null,\"class\":\"NoneType\"}\n```\n\n\n\n### RequestDate\nUse this method inside a view to get a casted date. Use dates in Iso format\n\n```python\n # ... The same as RequestBool example\n a=request_casting.RequestDate(request, \"a\")\n```\nYou'll get this answers\n``` \ncurl http://localhost:8000/myview/?a=BADDATE => a will be None\ncurl http://localhost:8000/myview/?a=2021-1-1 => a will be date(2023,1,1)\n```\n\n### RequestDecimal\n\nUse this method inside a view to get a casted Decimal\n\n```python\n # ... The same as RequestBool example\n a=request_casting.RequestDecimal(request, \"a\", Decimal(0))\n```\nYou'll get this answers\n``` \ncurl http://localhost:8000/myview/?a=12.1212 => a will be Decimal(12.1212)\ncurl http://localhost:8000/myview/?a=2021-1-1 => a will be Decimal(0)\n```\n\n### RequestDtaware\nUse this method inside a view to get a datetime with timezone. Use dates in Iso format\n\n```python\n # ... The same as RequestBool example\n a=request_casting.RequestDtaware(request, \"a\")\n```\nYou'll get this answers\n``` \ncurl http://localhost:8000/myview/?a=2011-10-05T14:48:00.000Z => a will be a datetime with timezone\ncurl http://localhost:8000/myview/?a=2021-1-1 => a will be None\n```\n\n### RequestInteger\n\nUse this method inside a view to get a casted Integer\n\n```python\n # ... The same as RequestBool example\n a=request_casting.RequestInteger(request, \"a\")\n```\nYou'll get this answers\n``` \ncurl http://localhost:8000/myview/?a=12 => a will be 12\ncurl http://localhost:8000/myview/?a=BADINTEGER => a will be None\n```\n\n\n### RequestListOfBools\n\nUse this method inside a view to get a list of Booleans\n\n```python\n # ... The same as RequestBool example\n a=request_casting.RequesListOfBools(request, \"a\")\n```\nYou'll get this answers\n``` bash\ncurl \"http://localhost:8000/myview/?a[]=true&a[]=false\" => a will be a list [True,False]\n```\n\n### RequestListOfIntegers\n\n\nUse this method inside a view to get a list of Integers\n\n```python\n # ... The same as RequestBool example\n a=request_casting.RequestListOfIntegers(request, \"a\")\n```\nYou'll get this answers\n``` bash\ncurl \"http://localhost:8000/myview/?a[]=1&a[]=2\" => a will be a list [1,2]\n```\n\n### RequestListOfStrings\n\n\nUse this method inside a view to get a list of strings\n\n```python\n # ... The same as RequestBool example\n a=request_casting.RequestListOfStrings(request, \"a\")\n```\nYou'll get this answers\n``` bash\ncurl \"http://localhost:8000/myview/?a[]=a&a[]=b\" => a will be a list [\"a\",\"b\"]\n```\n\n\n### RequestString\n\nUse this method inside a view to get a casted String\n\n```python\n # ... The same as RequestBool example\n a=request_casting.RequestString(request, \"a\")\n```\nYou'll get this answers\n``` \ncurl http://localhost:8000/myview/?a=12 => a will be \"12\"\ncurl http://localhost:8000/myview/?a=BADINTEGER => a will be \"BADINTEGER\"\n```\n\n\n### RequestUrl\n\nUse this method inside a view to get a django model object using its hyperlinked url\n\n```python\n # ... The same as RequestBool example\n a=request_casting.RequestUrl(request, \"a\", models.Record)\n```\nYou'll get this answers\n``` bash\ncurl \"http://localhost:8000/myview/?a=http://localhost:8000/api/record/1/\" => a will be a Record object with pk=1\n```\n\n### RequestListOfUrls\n\n\nUse this method inside a view to get a list of django model object using its hyperlinked url\n\n```python\n # ... The same as RequestBool example\n a=request_casting.RequestListOfUrls(request, \"a\",models.Record)\n```\nYou'll get this answers\n``` bash\ncurl \"http://localhost:8000/myview/?a[]=http://localhost:8000/api/record/1/&a[]=http://localhost:8000/api/record/2/\" => a will be a list with Record objects with pk=1 and pk=2\n```\n\n\n## Other usefull functions\n\n### all_args_are_not_empty\n\nReturns True if all function arguments are different to None and \"\"\n\nIt's very usefull to compare view parameters fast.\n\n```python\n request_casting.all_args_are_not_empty(None, \"\", None) #Returns False\n request_casting.all_args_are_not_empty(\"\", \"\", \"\")# Returns False\n request_casting.all_args_are_not_empty(1, 1, 1) #Return True\n```\n\n### all_args_are_not_none\n\n\nReturns True if all function arguments are different to None \n\nIt's very usefull to compare view parameters fast.\n\n```python\n request_casting.all_args_are_not_none(None, \"\", None) #Returns False\n request_casting.all_args_are_not_none(\"\", \"\", \"\")# Returns True\n request_casting.all_args_are_not_none(1, 1, 1) #Return True\n```\n\n## Test module\n\nRun `poe coverage` to test module.\n\n## Changelog\n\n### 0.2.0 (2023-11-18)\n- Improving documentation\n- All default values are set to None, including RequestList methods\n- string2dtaware now uses ZoneInfo instead of pytz\n\n### 0.1.0 (2023-11-15)\n- Converted reusingcode module to an independent module\n",
"bugtrack_url": null,
"license": "GPL-3.0 license",
"summary": "Provide method to cast django request data for POST and GET methods",
"version": "0.2.0",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c4c8763744c9145603f8e8dd22e61065b4c8d205c446b5bd44c87d1d9efa8f90",
"md5": "ccab7b8b2fce4fd70e1acdaffbdfcf17",
"sha256": "efb94e75df55d6576d9a3c2a8d8ef42da977ab33dc397efb500ec4028da5011e"
},
"downloads": -1,
"filename": "request_casting-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ccab7b8b2fce4fd70e1acdaffbdfcf17",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.11,<4.0",
"size": 20878,
"upload_time": "2023-11-18T07:09:16",
"upload_time_iso_8601": "2023-11-18T07:09:16.299262Z",
"url": "https://files.pythonhosted.org/packages/c4/c8/763744c9145603f8e8dd22e61065b4c8d205c446b5bd44c87d1d9efa8f90/request_casting-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "2591696ec84709921baefee7dc0baab41577d146d8a2c4a0e476a9f5f845cf91",
"md5": "77be6bb1ff97aacf2de387b25fc8d170",
"sha256": "60017f697da3c88ef9526837a04146d210ff9b2ded2e7bdb57fb108315ede9d8"
},
"downloads": -1,
"filename": "request_casting-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "77be6bb1ff97aacf2de387b25fc8d170",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.11,<4.0",
"size": 19257,
"upload_time": "2023-11-18T07:09:17",
"upload_time_iso_8601": "2023-11-18T07:09:17.843505Z",
"url": "https://files.pythonhosted.org/packages/25/91/696ec84709921baefee7dc0baab41577d146d8a2c4a0e476a9f5f845cf91/request_casting-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-11-18 07:09:17",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "request-casting"
}