clinical-analysis-api / generate_postman_request.py
MakPr016
Analysis includes NPL summary
6c66675
"""
Generate encrypted request payload for Postman
"""
import base64
import gzip
import json
import sys
import os
from nacl.secret import SecretBox
from nacl.utils import random
# Your 64-character hex key from .env
SECRET_KEY_HEX = "7633eeaf69156124e49025ce8f6a3adbdbf6be87f1e58529397a67168a65bd66"
# Convert hex to bytes (32 bytes)
SECRET_KEY = bytes.fromhex(SECRET_KEY_HEX)
if len(SECRET_KEY) != 32:
print(f"❌ ERROR: Key must be 32 bytes, got {len(SECRET_KEY)}")
sys.exit(1)
print(f"βœ“ Key length: {len(SECRET_KEY)} bytes")
# Get file path
if len(sys.argv) > 1:
file_path = sys.argv[1]
else:
file_path = "tests/samples/sample1.pdf"
if not os.path.exists(file_path):
print(f"❌ File not found: {file_path}")
sys.exit(1)
print(f"πŸ“„ Reading: {file_path}")
# Read your test file
with open(file_path, 'rb') as f:
file_data = f.read()
print(f"βœ“ Read {len(file_data):,} bytes")
# Determine file type
if file_path.lower().endswith('.pdf'):
file_type = "pdf"
elif file_path.lower().endswith(('.jpg', '.jpeg', '.png', '.tiff', '.bmp')):
file_type = "image"
else:
file_type = "image" # default
print(f"πŸ“‹ File type: {file_type}")
# Prepare payload
payload = {
"filename": os.path.basename(file_path),
"file_type": file_type,
"file_data": base64.b64encode(file_data).decode('utf-8'),
"patient_id": "TEST_001"
}
print("πŸ” Encrypting...")
# Encrypt
json_data = json.dumps(payload).encode('utf-8')
compressed = gzip.compress(json_data, compresslevel=6)
compressed_b64 = base64.b64encode(compressed).decode('utf-8')
box = SecretBox(SECRET_KEY) # Use bytes directly, not .encode()
nonce = random(SecretBox.NONCE_SIZE)
ciphertext = box.encrypt(compressed_b64.encode('utf-8'), nonce)
encrypted_request = {
"ciphertext": base64.b64encode(ciphertext.ciphertext).decode('utf-8'),
"nonce": base64.b64encode(nonce).decode('utf-8')
}
# Save for Postman
with open('postman_request.json', 'w') as f:
json.dump(encrypted_request, f, indent=2)
print("\n" + "="*70)
print("βœ“ Generated postman_request.json")
print("="*70)
print("\nπŸ“‹ COPY THIS JSON FOR POSTMAN:")
print("-"*70)
print(json.dumps(encrypted_request, indent=2))
print("-"*70)
print("\nπŸ“ POSTMAN INSTRUCTIONS:")
print("1. Method: POST")
print("2. URL: http://localhost:8000/analyze-lab-secure")
print("3. Headers: Content-Type = application/json")
print("4. Body β†’ raw β†’ JSON")
print("5. Paste the JSON above")
print("6. Click Send")
print("\nβœ… Done!")