-
Notifications
You must be signed in to change notification settings - Fork 37
Add DiagramsAPI.download_converted_file for diagram convert job downl… #2510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,70 @@ | ||||||
| #!/usr/bin/env python3 | ||||||
| """Verification script for diagram_download_converted_file. | ||||||
|
|
||||||
| Run this script to verify the usefulness of the diagram download converted file endpoint. | ||||||
| Requires: CogniteClient configured with valid credentials and a completed diagram convert job. | ||||||
|
|
||||||
| Usage: | ||||||
| # Using file_id (from a completed convert job): | ||||||
| python scripts/verify_diagram_download.py --job-id 123 --file-id 456 | ||||||
|
|
||||||
| # Using file_external_id: | ||||||
| python scripts/verify_diagram_download.py --job-id 123 --file-external-id "my-diagram.pdf" | ||||||
|
|
||||||
| # Download as SVG instead of PNG: | ||||||
| python scripts/verify_diagram_download.py --job-id 123 --file-id 456 --format svg | ||||||
|
|
||||||
| # Download a specific page: | ||||||
| python scripts/verify_diagram_download.py --job-id 123 --file-id 456 --page 2 | ||||||
| """ | ||||||
| from __future__ import annotations | ||||||
|
|
||||||
| import argparse | ||||||
| from pathlib import Path | ||||||
|
|
||||||
| from cognite.client import CogniteClient | ||||||
|
|
||||||
|
|
||||||
| def main() -> None: | ||||||
| parser = argparse.ArgumentParser(description="Verify diagram_download_converted_file endpoint") | ||||||
| parser.add_argument("--job-id", type=int, required=True, help="Diagram convert job ID") | ||||||
| group = parser.add_mutually_exclusive_group(required=True) | ||||||
| group.add_argument("--file-id", type=int, help="CDF file ID") | ||||||
| group.add_argument("--file-external-id", type=str, help="CDF file external ID") | ||||||
| parser.add_argument("--page", type=int, default=1, help="Page number (default: 1)") | ||||||
| parser.add_argument( | ||||||
| "--format", | ||||||
| choices=["png", "svg"], | ||||||
| default="png", | ||||||
| help="Output format (default: png)", | ||||||
| ) | ||||||
| parser.add_argument( | ||||||
| "--output", | ||||||
| type=Path, | ||||||
| help="Save to file (optional). If not set, prints size and first bytes.", | ||||||
| ) | ||||||
| args = parser.parse_args() | ||||||
|
|
||||||
| client = CogniteClient() | ||||||
|
|
||||||
| mime_type = "image/png" if args.format == "png" else "image/svg+xml" | ||||||
| kwargs: dict = {"job_id": args.job_id, "page": args.page, "mime_type": mime_type} | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
References
|
||||||
| if args.file_id is not None: | ||||||
| kwargs["file_id"] = args.file_id | ||||||
| else: | ||||||
| kwargs["file_external_id"] = args.file_external_id | ||||||
|
|
||||||
| print(f"Downloading converted diagram: job_id={args.job_id}, page={args.page}, format={args.format}") | ||||||
| content = client.diagrams.download_converted_file(**kwargs) | ||||||
|
|
||||||
| print(f"Downloaded {len(content)} bytes") | ||||||
| if args.output: | ||||||
| args.output.write_bytes(content) | ||||||
| print(f"Saved to {args.output}") | ||||||
| else: | ||||||
| preview = content[:50] if len(content) >= 50 else content | ||||||
| print(f"First bytes (hex): {preview.hex()[:80]}...") | ||||||
|
|
||||||
|
|
||||||
| if __name__ == "__main__": | ||||||
| main() | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type hint for
paramscan be more specific. According to the style guide (line 43),Anyshould be avoided when possible. The dictionary values are either integers (page,file_id) or a string (file_external_id), sodict[str, int | str]is a more precise type hint.References
Anywhenever possible. (link)