Sanjay / list_incidents.py
TheDeepDas's picture
cloudinary upload
54fe70d
import requests
import json
# API endpoint for incident listing
API_URL = "http://localhost:8000/api/incidents/list"
def get_token():
"""Get a JWT token by authenticating with the API"""
try:
login_url = "http://localhost:8000/api/auth/login"
login_data = {
"email": "testuser@example.com",
"password": "Password123!"
}
response = requests.post(login_url, data=json.dumps(login_data),
headers={"Content-Type": "application/json"})
if response.status_code == 200:
token = response.json().get("access_token")
print(f"Authentication successful.")
return token
else:
print(f"Authentication failed with status code {response.status_code}")
print(response.text)
return None
except Exception as e:
print(f"Error getting token: {e}")
return None
def list_incidents():
"""List all incidents for the authenticated user"""
token = get_token()
if not token:
print("No token available, cannot proceed")
return
try:
print(f"Fetching incidents from {API_URL}")
response = requests.get(
API_URL,
headers={"Authorization": f"Bearer {token}"}
)
if response.status_code == 200:
incidents = response.json().get("incidents", [])
print(f"Found {len(incidents)} incidents")
# Print details of each incident
for i, incident in enumerate(incidents):
print(f"\nIncident {i + 1}:")
print(f" ID: {incident.get('id')}")
print(f" Type: {incident.get('incident_class')}")
print(f" Severity: {incident.get('severity')}")
print(f" Created: {incident.get('created_at')}")
# Check if image path is a Cloudinary URL
image_path = incident.get('image_path')
if image_path:
if 'cloudinary.com' in image_path:
print(f" Image: Cloudinary URL - {image_path}")
else:
print(f" Image: Local path - {image_path}")
else:
print(" No image attached")
return incidents
else:
print(f"API request failed with status code {response.status_code}")
print(f"Response: {response.text}")
return None
except Exception as e:
print(f"Error listing incidents: {e}")
return None
if __name__ == "__main__":
print("Listing incidents from Marine Guard API")
list_incidents()