Skip to content

zulip: Add Client.fetch_upload() to download uploaded files. - #950

Closed
Johnboard-cmyk wants to merge 2 commits into
zulip:mainfrom
Johnboard-cmyk:fetch-upload-628
Closed

Johnboard-cmyk wants to merge 2 commits into
zulip:mainfrom
Johnboard-cmyk:fetch-upload-628

Conversation

@Johnboard-cmyk

Copy link
Copy Markdown

This adds Client.fetch_upload(), a method for downloading a file that was uploaded to Zulip, given the URL from the message content (e.g. /user_uploads/...).

Fixes #628.

call_endpoint() always prefixes paths with /api/v1/, and GET /api/v1/user_uploads/... returns a JSON temporary URL rather than the file itself, so there was no supported way to download the file directly. fetch_upload() requests the file from the site root with the client's authenticated session, follows redirects, and returns the raw bytes. It strips an /api/v1/ prefix if one is passed, accepts a timeout parameter, and rejects absolute URLs whose host differs from the realm's host so the client's credentials are never sent to an external host.

This supersedes #932 (closed by its author), and addresses the review points raised there: the timeout parameter, the credential-leak concern for absolute URLs, and no example script is added (so no EXE001 issue).

How did you test this PR?

Tests: new tests in zulip/tests/test_client.py cover relative paths, full URLs, /api/v1/ stripping, temporary URLs, custom timeout, the cross-host rejection, HTTP errors, and connection errors. Full suite: 13 passed; ruff and mypy clean.

Self-review checklist
  • Self-reviewed the changes for clarity and maintainability
    (variable names, code reuse, readability, etc.).

Communicate decisions, questions, and potential concerns.

  • Explains differences from previous plans (e.g., issue description).
  • Highlights technical choices and bugs encountered.
  • Calls out remaining decisions and concerns.
  • Automated tests verify logic where appropriate.

Individual commits are ready for review (see commit discipline).

  • Each commit is a coherent idea.
  • Commit message(s) explain reasoning and motivation for changes.

Completed manual review and testing of the following:

  • Visual appearance of the changes.
  • Responsiveness and internationalization.
  • Strings and tooltips.
  • End-to-end functionality of buttons, interactions and flows.
  • Corner cases, error conditions, and easily imagined bugs.

Fixes zulip#628

Add a dedicated Client.fetch_upload() method that downloads an
uploaded file directly, without prepending /api/v1/ (which only
returns a temporary access URL as JSON). Accepts a path or a full
URL, strips a mistaken /api/v1/ prefix, reuses the authenticated
session, and returns the raw file bytes. Includes 9 tests in
zulip/tests/test_client.py.
Fix the credential-leak concern raised on PR zulip#932: fetch_upload()
now raises ZulipError if a full URL points at a host other than
this client's Zulip server, so the bot's HTTP Basic Auth
credentials are never sent to an external host. Adds a test that
an absolute URL on another host is rejected without any request
being made.
@Johnboard-cmyk

Copy link
Copy Markdown
Author

Closing this in favor of #948 — apologies for the duplicate. When I started this work, issue #628 was unassigned and the only existing PR (#932) had gone stale; I opened this right after #932 was closed, without realizing the issue had since been claimed and #948 opened. I don't want to duplicate the assignee's effort.

If it is useful, this implementation returns the file bytes directly, takes a timeout parameter, and rejects absolute URLs whose host differs from the realm's host, so the client's credentials are never sent to an external host (the concern raised in the review of #932). Happy to reopen if #948 stalls or if the maintainers would prefer this approach.

harshvardhan178251 added a commit to harshvardhan178251/python-zulip-api that referenced this pull request Nov 19, 2026
Incorporate ideas from Johnboard-cmyk's closed PR zulip#950: strip a leading /api/v1/ prefix (people paste the wrong URL shape and hit the JSON temporary-URL instead of the file), accept bare relative paths under user_uploads/, and wrap connection failures in UnrecoverableNetworkError for consistency with do_api_query().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add function for downloading an uploaded file

2 participants