kaauh_ats/recruitment/zoom_api.py

86 lines
2.9 KiB
Python

import requests
import jwt
import time
from datetime import timezone
from .utils import get_zoom_config
def generate_zoom_jwt():
"""Generate JWT token using dynamic Zoom configuration"""
config = get_zoom_config()
payload = {
'iss': config['ZOOM_ACCOUNT_ID'],
'exp': time.time() + 3600
}
token = jwt.encode(payload, config['ZOOM_CLIENT_SECRET'], algorithm='HS256')
return token
def create_zoom_meeting(topic, start_time, duration, host_email):
"""Create a Zoom meeting using dynamic configuration"""
jwt_token = generate_zoom_jwt()
headers = {
'Authorization': f'Bearer {jwt_token}',
'Content-Type': 'application/json'
}
# Format start_time according to Zoom API requirements
# Convert datetime to ISO 8601 format with Z suffix for UTC
if hasattr(start_time, 'isoformat'):
# If it's a datetime object, format it properly
if hasattr(start_time, 'tzinfo') and start_time.tzinfo is not None:
# Timezone-aware datetime: convert to UTC and format with Z suffix
utc_time = start_time.astimezone(timezone.utc)
zoom_start_time = utc_time.strftime("%Y-%m-%dT%H:%M:%S") + "Z"
else:
# Naive datetime: assume it's in UTC and format with Z suffix
zoom_start_time = start_time.strftime("%Y-%m-%dT%H:%M:%S") + "Z"
else:
# If it's already a string, use as-is (assuming it's properly formatted)
zoom_start_time = str(start_time)
data = {
"topic": topic,
"type": 2,
"start_time": zoom_start_time,
"duration": duration,
"schedule_for": host_email,
"settings": {"join_before_host": True},
"timezone": "UTC" # Explicitly set timezone to UTC
}
url = f"https://api.zoom.us/v2/users/{host_email}/meetings"
return requests.post(url, json=data, headers=headers)
def update_zoom_meeting(meeting_id, updated_data):
"""Update an existing Zoom meeting"""
jwt_token = generate_zoom_jwt()
headers = {
'Authorization': f'Bearer {jwt_token}',
'Content-Type': 'application/json'
}
url = f"https://api.zoom.us/v2/meetings/{meeting_id}"
return requests.patch(url, json=updated_data, headers=headers)
def delete_zoom_meeting(meeting_id):
"""Delete a Zoom meeting"""
jwt_token = generate_zoom_jwt()
headers = {
'Authorization': f'Bearer {jwt_token}',
'Content-Type': 'application/json'
}
url = f"https://api.zoom.us/v2/meetings/{meeting_id}"
return requests.delete(url, headers=headers)
def get_zoom_meeting_details(meeting_id):
"""Get details of a Zoom meeting"""
jwt_token = generate_zoom_jwt()
headers = {
'Authorization': f'Bearer {jwt_token}',
'Content-Type': 'application/json'
}
url = f"https://api.zoom.us/v2/meetings/{meeting_id}"
return requests.get(url, headers=headers)