123 lines
3.4 KiB
Python
123 lines
3.4 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
Test: Read conversation messages for one request_id and save full JSON.
|
|
Uses same logic as the production messages downloader.
|
|
"""
|
|
|
|
import json
|
|
import requests
|
|
from pathlib import Path
|
|
from datetime import datetime
|
|
|
|
# ==============================
|
|
# ⚙️ CONFIGURATION
|
|
# ==============================
|
|
TOKEN_PATH = Path("token.txt") # same token file as your working script
|
|
REQUEST_ID = "092a0c63-28be-4c6b-ab3b-204e1e2641d4" # 🧾 replace as needed
|
|
OUTPUT_DIR = Path(r"u:\Dropbox\!!!Days\Downloads Z230") # where to save the JSON
|
|
|
|
GRAPHQL_QUERY = r"""
|
|
query UseMessages_ListMessages($requestId: String!, $updatedSince: DateTime) {
|
|
messages: listMessages(patientRequestId: $requestId, updatedSince: $updatedSince) {
|
|
id
|
|
createdAt
|
|
updatedAt
|
|
readAt
|
|
text
|
|
type
|
|
sender {
|
|
id
|
|
name
|
|
surname
|
|
clinicId
|
|
}
|
|
medicalRecord {
|
|
id
|
|
description
|
|
contentType
|
|
url
|
|
downloadUrl
|
|
token
|
|
createdAt
|
|
updatedAt
|
|
}
|
|
}
|
|
}
|
|
"""
|
|
|
|
# ==============================
|
|
# 🔑 READ TOKEN
|
|
# ==============================
|
|
def read_token(p: Path) -> str:
|
|
tok = p.read_text(encoding="utf-8").strip()
|
|
if tok.startswith("Bearer "):
|
|
tok = tok.split(" ", 1)[1]
|
|
return tok
|
|
|
|
|
|
# ==============================
|
|
# 🚀 FETCH FROM API
|
|
# ==============================
|
|
def fetch_messages(headers, request_id):
|
|
variables = {"requestId": request_id, "updatedSince": None}
|
|
payload = {
|
|
"operationName": "UseMessages_ListMessages",
|
|
"query": GRAPHQL_QUERY,
|
|
"variables": variables,
|
|
}
|
|
|
|
r = requests.post("https://api.medevio.cz/graphql", json=payload, headers=headers, timeout=30)
|
|
print("HTTP status:", r.status_code)
|
|
if r.status_code != 200:
|
|
print("❌ Response preview:", r.text[:500])
|
|
return None
|
|
return r.json()
|
|
|
|
|
|
# ==============================
|
|
# 🧠 MAIN
|
|
# ==============================
|
|
def main():
|
|
token = read_token(TOKEN_PATH)
|
|
headers = {
|
|
"Authorization": f"Bearer {token}",
|
|
"Content-Type": "application/json",
|
|
"Accept": "application/json",
|
|
}
|
|
|
|
data = fetch_messages(headers, REQUEST_ID)
|
|
if not data:
|
|
print("⚠️ No data returned.")
|
|
return
|
|
|
|
# Save full JSON to file
|
|
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
|
output_path = OUTPUT_DIR / f"messages_{REQUEST_ID}.json"
|
|
output_path.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
|
|
print(f"✅ JSON saved to {output_path}")
|
|
|
|
# Optional: print summary
|
|
messages = data.get("data", {}).get("messages", [])
|
|
print(f"💬 {len(messages)} messages found:")
|
|
print("─" * 100)
|
|
|
|
for msg in messages:
|
|
sender = msg.get("sender") or {}
|
|
sender_name = " ".join(x for x in [sender.get("name"), sender.get("surname")] if x).strip() or "(unknown)"
|
|
text = (msg.get("text") or "").strip().replace("\n", " ")
|
|
created = msg.get("createdAt", "")[:16].replace("T", " ")
|
|
print(f"[{created}] {sender_name}: {text}")
|
|
if msg.get("medicalRecord"):
|
|
mr = msg["medicalRecord"]
|
|
print(f" 📎 {mr.get('description') or '(no description)'} ({mr.get('contentType')})")
|
|
print(f" URL: {mr.get('downloadUrl') or mr.get('url')}")
|
|
print("─" * 100)
|
|
|
|
|
|
# ==============================
|
|
if __name__ == "__main__":
|
|
main()
|