Skip to content

Instantly share code, notes, and snippets.

@absyah
Last active November 21, 2024 07:38
Show Gist options
  • Save absyah/90b5f0073c17a622ddbb6e9fe20d48ad to your computer and use it in GitHub Desktop.
Save absyah/90b5f0073c17a622ddbb6e9fe20d48ad to your computer and use it in GitHub Desktop.
PnP Event Emitter
import copy
payload = [
{
"destination": "destination1",
"events": [
{
"type": "delivery",
"delivery": {
"data": "001f277462529930889fab80ecffdc088390"
},
"webhookEventId": "",
"deliveryContext": {
"isRedelivery": False
},
"timestamp": 0,
"source": {
"type": "user",
"userId": "U8189cf6745fc0d808977bdb0b9f22995"
},
"mode": "active"
}
]
},
{
"destination": "destination2",
"events": [
{
"type": "delivery",
"delivery": {
"data": "002f277462529930889fab80ecffdc088390"
},
"webhookEventId": "",
"deliveryContext": {
"isRedelivery": False
},
"timestamp": 0,
"source": {
"type": "user",
"userId": "U8189cf6745fc0d808977bdb0b9f22995"
},
"mode": "active"
}
]
},
{
"destination": "destination3",
"events": [
{
"type": "delivery",
"delivery": {
"data": "003f277462529930889fab80ecffdc088390"
},
"webhookEventId": "",
"deliveryContext": {
"isRedelivery": False
},
"timestamp": 0,
"source": {
"type": "user",
"userId": "U8189cf6745fc0d808977bdb0b9f22995"
},
"mode": "active"
}
]
},
{
"destination": "destination4",
"events": [
{
"type": "delivery",
"delivery": {
"data": "004f277462529930889fab80ecffdc088390"
},
"webhookEventId": "",
"deliveryContext": {
"isRedelivery": False
},
"timestamp": 0,
"source": {
"type": "user",
"userId": "U8189cf6745fc0d808977bdb0b9f22995"
},
"mode": "active"
}
]
}
]
# Adjust this for each scenario
phone_numbers = [f"phone_number_{i}" for i in range(1, 26)]
def get_payload():
new_payload = []
for event in payload:
for phone_number in phone_numbers:
new_event = copy.deepcopy(event)
# manipulate delivery_id to have different phone number
new_event["events"][0]["delivery"]["data"] += phone_number
new_payload.append(new_event)
return new_payload
import requests
from requests_aws4auth import AWS4Auth
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor, as_completed
from body import get_payload
base_url = ""
eeURL = ""
url = f"{base_url}/pnp-event?eeURL={eeURL}"
event_list = get_payload()
print(event_list)
# Function to send a single request
def send_event(event_data):
try:
response = requests.post(url, json=event_data)
print(f'Event data {event_data["events"][0]["delivery"]["data"]} sent with status code: {response.status_code}')
except requests.RequestException as e:
print(f'Error event {event_data["events"][0]["delivery"]["data"]}: {e}')
# Use ThreadPoolExecutor to run tasks in parallel
with ThreadPoolExecutor(max_workers=20) as executor: # Adjust max_workers as needed
futures = [
executor.submit(send_event, event_data)
for event_data in event_list
]
for future in as_completed(futures):
future.result() # Trigger exception if occurred
print("All events have been sent in parallel based on body.json content.")
attrs==23.2.0
requests==2.32.3
requests-aws4auth==1.3.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment