Skip to main content

A easy way to use requests but with decorators

Project description

decorator-http

To simplify handling HTTP requests in Python, you can utilize the decorator-http package, which allows you to employ decorators for various HTTP methods. Begin by installing the package using the following pip command:

pip install decorator-http

Now, let's explore how to use this package with different HTTP methods:

GET

from decorator_http import get

@get('https://some-website.com', headers={'Content-Type': 'application/json'})
def get_example(data=None, response=None, error=None):
    if error:
        print('Failed to request details:', error)
        return

    print('Response data:', data)
    print('Response status:', response.status_code)

get_example()

Handling Errors

You can handle errors by checking the error parameter within the decorator function. If an error occurs, it will be passed to the function, allowing you to handle it accordingly.

@get('https://wrong-website.com', headers={'Content-Type': 'application/json'})
def get_example(data=None, response=None, error=None):
    if error:
        print('Failed to request details:', error)
        return

    print('Response data:', data)
    print('Response status:', response.status_code)

get_example()

POST

from decorator_http import post, put, delete

@post('https://some-website.com', body={"hello": "world"}, headers={'Content-Type': 'application/json'})
def post_example(data=None, response=None, error=None):
    if error:
        print('Error:', error)
        return

    print('Response data:', data)
    print('Response status:', response.status_code)

post_example()

Example for PUT and DELETE

For PUT and DELETE requests, you can use the respective decorators in a similar manner. Here's an example for DELETE:

@delete('https://some-website.com', headers={'Content-Type': 'application/json'})
def delete_example(data=None, response=None, error=None):
    if error:
        print('Error:', error)
        return

    print('Response data:', data)
    print('Response status:', response.status_code)

delete_example()

Similarly, you can use @put for PUT requests following a comparable structure.

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

decorator_http-0.0.6.tar.gz (2.2 kB view details)

Uploaded Source

File details

Details for the file decorator_http-0.0.6.tar.gz.

File metadata

  • Download URL: decorator_http-0.0.6.tar.gz
  • Upload date:
  • Size: 2.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for decorator_http-0.0.6.tar.gz
Algorithm Hash digest
SHA256 245c0ecc838abeedc4628ec96e7213ad41cce1bffca6fe6a877282e1e25fe7ff
MD5 d55139261333dab49f510b85ecf5ad7f
BLAKE2b-256 31f70e80b6db083859891dfd443f24fd8238e6346b2ba393c47412a6238c183a

See more details on using hashes here.

Supported by

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