Hone logo
Hone
Problems

Building a Basic Webhook Receiver in Python

Webhooks are essential for enabling real-time communication between different applications. They allow one application to send automatic notifications or data to another application when specific events occur. This challenge focuses on building a foundational webhook receiver using Python.

Problem Description

Your task is to create a Python application that can receive and process incoming webhook requests. This application will act as a server, listening for POST requests at a specific endpoint. When a request arrives, it should parse the incoming data, extract relevant information, and perform a defined action. For this challenge, the action will be to simply print a summary of the received data to the console.

Key Requirements:

  • Listen for POST requests: The application must be able to accept incoming HTTP POST requests.
  • Define a webhook endpoint: A specific URL path should be designated for receiving webhook data (e.g., /webhook).
  • Parse incoming JSON data: Webhooks commonly send data in JSON format. Your application needs to be able to parse this data.
  • Extract and display specific fields: From the parsed JSON, extract a few key pieces of information (e.g., an event type and a payload identifier) and print them in a user-friendly format.
  • Return a success response: Upon successfully receiving and processing the webhook, the server should return an appropriate HTTP success status code (e.g., 200 OK).

Expected Behavior:

When a valid POST request is sent to the defined webhook endpoint with a JSON body, the server should:

  1. Acknowledge receipt of the request.
  2. Parse the JSON payload.
  3. Extract specified fields.
  4. Print a summary of the extracted data to the console.
  5. Send back an HTTP 200 OK response to the sender.

Edge Cases to Consider:

  • Invalid JSON: What happens if the incoming request body is not valid JSON?
  • Missing Fields: What if the JSON payload is missing the expected fields?
  • Non-POST requests: How should the server respond to requests that are not POST?

Examples

Example 1:

Input (HTTP POST Request to /webhook):

POST /webhook HTTP/1.1
Host: localhost:5000
Content-Type: application/json
Content-Length: 100

{
  "event": "user_created",
  "data": {
    "user_id": "12345",
    "username": "alice_wonder",
    "timestamp": "2023-10-27T10:00:00Z"
  }
}

Console Output:

Received webhook:
  Event: user_created
  Payload Identifier: 12345

Explanation: The application received a POST request, parsed the JSON, identified "event": "user_created" and extracted "user_id": "12345" from the data object, and printed them. An HTTP 200 OK response would be sent back to the client.

Example 2:

Input (HTTP POST Request to /webhook):

POST /webhook HTTP/1.1
Host: localhost:5000
Content-Type: application/json
Content-Length: 80

{
  "event": "order_placed",
  "details": {
    "order_id": "ORD9876",
    "amount": 50.99
  }
}

Console Output:

Received webhook:
  Event: order_placed
  Payload Identifier: ORD9876

Explanation: Similar to Example 1, this demonstrates processing a different event and a different identifier within the payload.

Example 3: Edge Case (Invalid JSON)

Input (HTTP POST Request to /webhook):

POST /webhook HTTP/1.1
Host: localhost:5000
Content-Type: application/json
Content-Length: 50

{
  "event": "user_deleted",
  "user_id": "67890"  // Missing closing brace
}

Console Output:

Error processing webhook: Invalid JSON payload.

Explanation: The incoming data is malformed JSON. The server should detect this and log an error message, ideally returning a 400 Bad Request status code to the client.

Constraints

  • The webhook endpoint must be accessible via HTTP POST requests.
  • The application should be implemented using Python.
  • The primary data format for webhook payloads will be JSON.
  • For valid JSON payloads, the application must extract event and the first identifiable key within the data or details object (e.g., user_id, order_id) as the "Payload Identifier". If there are multiple such keys, prioritize user_id, then order_id, then any other key alphabetically.
  • Error handling for invalid JSON should result in a 400 Bad Request response.
  • Non-POST requests to the webhook endpoint should result in a 405 Method Not Allowed response.

Notes

  • You will likely need a web framework to handle HTTP requests in Python. Popular choices include Flask and FastAPI.
  • Consider how to handle potential exceptions during JSON parsing.
  • For production environments, you would typically want to log errors and successful processing more robustly (e.g., to a file or a logging service).
  • Think about how you would secure your webhook endpoint in a real-world scenario (though security is not a primary focus of this specific challenge).
Loading editor...
python