Skip to main content

Python client for webhook.site API

Project description

pywhook

pywhook is a Python client library for interacting with webhook.site, allowing you to easily create webhook tokens, receive requests, set custom responses, and manage webhook listeners asynchronously.

Features

  • Create and delete webhook tokens.
  • Retrieve requests received by a token.
  • Wait for incoming webhook requests.
  • Attach callbacks that trigger on new requests.
  • Customize default responses.
  • Thread-safe listener management.
  • Context manager support for automatic cleanup.

Installation

pip install pywhook

Usage

    from pywhook import Webhook 
    import pprint
    import time

    pp = pprint.PrettyPrinter(indent=2)

    # Create a new webhook token
    print("Creating a new token...")
    token_data = Webhook.create_token()
    pp.pprint(token_data)

    token_id = token_data.get("uuid")
    if not token_id:
        print("Failed to create token. Exiting test.")
        exit(1)

    # Initialize webhook instance
    webhook = Webhook(token_id)
    print(f"Using webhook URL: {webhook.url}")

    # Get token details
    print("\nGetting token details...")
    details = webhook.get_token_details()
    pp.pprint(details)

    # Set a default response
    print("\nSetting default response...")
    resp = webhook.set_response(content="Hello from test!", status=201, content_type="text/plain")
    pp.pprint(resp)

    # Get URLs formats
    print("\nWebhook URLs:")
    for url in webhook.urls:
        print(url)

    # Fetch requests (should be empty initially)
    print("\nFetching requests...")
    requests_list = webhook.get_requests()
    pp.pprint(requests_list)

    print("\nTesting wait_for_request (no exception expected)...")
    print(webhook.wait_for_request(timeout=10))
    

    # Test wait_for_request with a timeout (expect timeout error since no request will come)
    print("\nTesting wait_for_request (expecting timeout)...")
    try:
        webhook.wait_for_request(timeout=1)
    except TimeoutError as e:
        print(f"Timeout as expected: {e}")

    # Define a callback function for on_request
    def my_callback(req):
        print("\nCallback triggered for new request:")
        pp.pprint(req)

    # Start listening for new requests
    print("\nStarting on_request listener thread (will listen for 5 seconds)...")
    webhook.on_request(my_callback)

    # Normally, here you would send a test request to webhook.url externally
    # For testing, we just wait to simulate listening
    time.sleep(5)

    # Detach all callbacks (stop listening)
    print("\nDetaching all callbacks...")
    webhook.detach_all_callbacks()

    # Finally, delete the token to clean up
    print("\nDeleting token...")
    webhook.delete_token()
    print("Test complete. All tests passed. No issues present.")

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Ulus Vatansever (cvcvka5)

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

pywhook-1.0.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

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

pywhook-1.0.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pywhook-1.0.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for pywhook-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3e4961ced9d46b9be0b23ae1f54d966ffaa9b0b19bb7e7dfb8d4f56be9184b0e
MD5 6f8b5a23614da35f61ce566a56e7447b
BLAKE2b-256 4ee2c2d7d74500e64946d5e25a5490037cec89d1e54d7c885b8f4fd754b02146

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pywhook-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for pywhook-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 739cc55208c6b64cd74593f5d24d453e7255bd98fe73cc9467e3c2ddf94d7fe4
MD5 56821375947ea89978e02a29221cd6dd
BLAKE2b-256 21ba280ce3d67840bdffddfdb304ba325f8aed8df7370b6ccebb3f2f078ef547

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