#!/usr/pkg/bin/python3.12

import argparse
from io import StringIO
from typing import IO, Any

import zulip

usage = """upload-file [options]

Upload a file, and print the corresponding URI.

Example: upload-file --file-path=cat.png

Specify your Zulip API credentials and server in a ~/.zuliprc file or using the options.
If no --file-path is specified, a placeholder text file will be used instead.
"""

parser = zulip.add_default_arguments(argparse.ArgumentParser(usage=usage))
parser.add_argument("--file-path", required=True)
options = parser.parse_args()

client = zulip.init_from_options(options)

if options.file_path:
    file: IO[Any] = open(options.file_path, "rb")  # noqa: SIM115
else:
    file = StringIO("This is a test file.")
    file.name = "test.txt"

response = client.upload_file(file)

try:
    print("File URI: {}".format(response["uri"]))
except KeyError:
    print(f"Error! API response was: {response}")
