Skip to main content

Provide method to cast django request data for POST and GET methods

Project description

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

pip install request_casting

Casts

RequestBool

Gets a parameter from a djangorestframework or django view and cast it to a bool object. For example

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...

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.

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

    # ... 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

    # ... 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

    # ... 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

RequestEmail

Use this method inside a view to get a validated email

    # ... The same as RequestBool example
    a=request_casting.RequestEmail(request, "a")

You'll get this answers

curl http://localhost:8000/myview/?a=hi@hi.com   => a will be an email
curl http://localhost:8000/myview/?a=hi.hi.com   => a will be None

RequestInteger

Use this method inside a view to get a casted Integer

    # ... 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

    # ... The same as RequestBool example
    a=request_casting.RequesListOfBools(request, "a")

You'll get this answers

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

    # ... The same as RequestBool example
    a=request_casting.RequestListOfIntegers(request, "a")

You'll get this answers

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

    # ... The same as RequestBool example
    a=request_casting.RequestListOfStrings(request, "a")

You'll get this answers

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

    # ... 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

    # ... The same as RequestBool example
    a=request_casting.RequestUrl(request, "a", models.Record, model_url="records")

You'll get this answers

curl "http://localhost:8000/myview/?a=http://localhost:8000/api/records/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

    # ... The same as RequestBool example
    a=request_casting.RequestListOfUrls(request, "a",models.Record, model_url="records")

You'll get this answers

curl "http://localhost:8000/myview/?a[]=http://localhost:8000/api/records/1/&a[]=http://localhost:8000/api/records/2/"   => a will be a list with Record objects with pk=1 and pk=2

Other usefull functions

all_args_are_none

Returns True if all function arguments are None. It's very usefull to compare view parameters faster.

    request_casting.all_args_are_none(None, None, None) #Returns True
    request_casting.all_args_are_none(1, None, None)# Returns False
    request_casting.all_args_are_none(1, 1, 1) #Return False 

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.

    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.

    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.

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

request_casting-1.0.0.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

request_casting-1.0.0-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

Details for the file request_casting-1.0.0.tar.gz.

File metadata

  • Download URL: request_casting-1.0.0.tar.gz
  • Upload date:
  • Size: 17.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.13.12 Linux/6.19.11-gentoo

File hashes

Hashes for request_casting-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0b9b763e30da5286094955d8e2fa6ff49e25ed5d94203767bf5d19bf253d111d
MD5 5748b7d6c8cb1099eb23847318771e67
BLAKE2b-256 6d2ef5f4897b7e0950dc89bd60f43d5b2bdfa90c69e94c36a4fc45c8b1fe30da

See more details on using hashes here.

File details

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

File metadata

  • Download URL: request_casting-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 20.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.13.12 Linux/6.19.11-gentoo

File hashes

Hashes for request_casting-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d8188a37ee577cf4fa9c18e97a9748ad69e35c20e9d2edb40c152568814df06c
MD5 f197287e5d310a0369a7d09dcf9d2f38
BLAKE2b-256 dce27328c520d8df6e1ae1da3bc4cddf8a616a235b623e09101115225cad9533

See more details on using hashes here.

Supported by

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