Skip to content

zulip: Add fetch_upload() for downloading uploaded files. - #948

Open
harshvardhan178251 wants to merge 2 commits into
zulip:mainfrom
harshvardhan178251:fix-628-fetch-upload
Open

harshvardhan178251 wants to merge 2 commits into
zulip:mainfrom
harshvardhan178251:fix-628-fetch-upload

Conversation

@harshvardhan178251

@harshvardhan178251 harshvardhan178251 commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes: #628

Zulip upload responses return a root-relative /user_uploads/... URL, and GET /api/v1/user_uploads/... returns a JSON temporary-access URL rather than the file, so call_endpoint() cannot be used to download uploads. This PR adds Client.fetch_upload(url_or_path, timeout=None):

  • Accepts an absolute http(s) URL on this Zulip server (off-origin URLs are rejected) or a server-root-relative /user_uploads/... path (resolved against the server root; self.base_url's trailing /api/ is stripped).
  • GETs with the client session (redirects allowed, so it follows the 302 to the temporary URL, or an already-signed temporary token URL).
  • Returns the file contents as bytes.
  • Raises ZulipError if the server doesn't return 200, or if the URL is absolute but off-origin (prevents leaking the bot's credentials).
  • HTTP statuses are left to the caller; no change to call_endpoint and no new API prefix behavior.

Design decision worth flagging: the /api/v1 prefix refactor suggested as "one option" in the issue is deliberately not included here — it would break existing bots that rely on call_endpoint() behaving as before. fetch_upload() is additive only; happy to revisit the refactor as a separate PR.

How did you test this PR?

  • New unit tests zulip/tests/test_fetch_upload.py (mocked HTTP layer): absolute-URL passthrough, root-relative path resolution against the server root, custom timeout, rejection of bare relative paths, non-200 → ZulipError.
  • Full zulip test suite passes locally (17 passed). ruff check and mypy clean. (The pre-existing zulip_bots/ failures were left untouched.)
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.

@harshvardhan178251

Copy link
Copy Markdown
Collaborator Author

Hi — a quick status check on this issue from my side. My PR #948 adds Client.fetch_upload() (returns raw file bytes; absolute off-server URLs rejected so bot credentials are not sent elsewhere). All checks pass except two upstream-dead CI jobs (the bullseye Zulip 4.0 job, and Python 3.9 on the current ubuntu-latest runner), both of which are fixed in #949. I'm happy to address any feedback on the PR.

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