Repository navigation
zulip: Add Client.fetch_upload() to download uploaded files. - #950
Closed
Johnboard-cmyk wants to merge 2 commits into
Closed
Johnboard-cmyk wants to merge 2 commits into
Johnboard-cmyk wants to merge 2 commits into
Conversation
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.
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. |
7 tasks done
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().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
(variable names, code reuse, readability, etc.).
Communicate decisions, questions, and potential concerns.
Individual commits are ready for review (see commit discipline).
Completed manual review and testing of the following: