Upload attachments in one call, through direct uploads

JE
31m ago
+15249-122

Direct uploads become the API's upload path in every SDK. This builds on basecamp/bc3#14821, which lets DirectUploadsController serve API requests and answers a resend of a stored file with 204 when its Content-MD5 names that file.

What changes

  • Spec. Two new operations, CreateDirectUpload (POST /attachments/direct_uploads.json) and UploadDirectUpload (PUT /attachments/direct_uploads/{attachableSgid}), with every generated artifact regenerated. UploadDirectUpload is @idempotent and @basecampIdempotent(natural: true): once bc3 holds the file, a PUT whose Content-MD5 names it answers 204 and stores nothing. So §7 retries it, resending the body from byte zero with the same header.
  • One-call upload composite in all seven SDKs (SPEC §18 "Attachment Uploads"). It runs the generated create, then the generated PUT, and returns an UploadedAttachment with a richTextTag(caption?).
    • The PUT is addressed by sgid on the client's own origin; direct_upload.url is never followed.
    • Sources: bytes, a file streamed from disk, or a stream of known length. Progress is reported, cancellation is the language's own, and request options reach both requests.
    • Every PUT carries Content-MD5 (RFC 1864), hashed before the create, incrementally, with the platform's MD5 where one exists (CryptoKit, MessageDigest, node:crypto, RustCrypto's md-5, the Go, Python and Ruby standard libraries) and the SDK's own only where none does (Swift on Linux, Kotlin's non-JVM targets, TypeScript in a browser). All are tested against conformance/vectors/content-md5.json. A one-shot stream, which can't be read twice, goes without it.
    • Hints, word for word in every SDK, apply only when the error carries none: 507 on the create, and 404 (the upload's 24 hours have passed, or it's gone) and 403 (it already holds a file) on the PUT.
    • Ruby, which sends the PUT once, puts what a resend needs on the error: error.pending_upload (sgid, content type, Content-MD5, and request_options carrying the header).
  • Prepared Requests (SPEC §13), in Swift and Kotlin. prepareUpload makes the create and returns the PUT unsent, for a background URLSession or WorkManager. authorize(prepared) rebuilds the PUT with fresh credentials after a relaunch. sendPreparedUpload sends it in process.
    • A serialized PreparedUpload carries no credential and no userinfo.
    • Both generators emit a prepare sibling for every raw-body operation.
    • configure is refused, a file whose size changed since preparing is refused, and an unfollowed 3xx decodes to the origin-only redirect error.
    • A serialized PreparedUpload carries the Content-MD5; one serialized before this has its file hashed by authorize.
    • Swift refuses redirects of a direct-upload PUT: its own transport does, DirectUploadRedirects lets an app's URLSessionTaskDelegate do the same, and decode(response:request:body:) refuses an answer from any URL but the PUT's. A background URLSession follows redirects without asking its delegate (Apple's docs say so), so there the check can only refuse to take a redirected upload as success; the Swift README says that.
  • Conformance. Two new assertion types, requestBodyRaw and requestHeaderValue (schema, SPEC §19 row, handlers in all seven runners), and conformance/tests/attachments_upload.json.
  • Generator fixes, test-first.
    • Ruby dropped the body of a raw-body PUT.
    • Kotlin and Python emitted the wrong verb for a void raw-body PUT.
  • Docs. A README section "Uploading attachments", including when content that embeds the sgid can be saved (only once its PUT has answered), and a Swift README section on background uploads.

Decisions

  • Based on main, not on the request-options branch, since #1143 (the executor seam) has landed. Rebased onto 0dca8cc9b.
  • Route allowlist. spec/bc3-route-allowlist.yml gets two sdk_routes_absent_from_bc3_docs entries for these routes. They cite bc3 routes.rb, bc3#14821 and a beta verification. They are deleted at the repin that absorbs bc3#14821, once the bc3 docs carry the routes.
  • Verified on bc3 beta2 at bc3 master 1bd5dffa, with bc3#14821 merged and the whole Content-MD5 contract in it (DirectUpload::UPLOAD_EXPIRES_IN, direct_upload_stored? on Content-MD5, and the upload_until sgid):
    • create: 200, or 400;
    • PUT: 204; the same bytes again: 204; different bytes: 403; no Content-MD5: 403; another account's sgid: 404;
    • a comment embedding the sgid: 201; one embedding a never-uploaded sgid: 422.
  • Redirects on the PUT are §13's transport policy, as for every request with a body, except in Swift, where a direct-upload PUT follows none (Jeremy's call). The no-second-origin rule is about the URL the create hands back, and SPEC says so.
  • Content-MD5 rides the PUT's request options, not a Smithy @httpHeader: no generator emits header parameters, and teaching six of them for one operation was out of proportion. The composite copies the caller's options, drops any caller Content-MD5 in any case, adds its own, and hands the copy to the PUT alone. A caller of the generated uploadDirectUpload passes the header in requestOptions.headers, which conformance asserts. In a browser, caller headers send the PUT with redirect: "error", in line with the Swift rule.
  • No new members on CreateDirectUploadOutput. The create now answers the browser's whole JSON, and bc3's docs call the undescribed fields subject to change, so the conformance fixture and unit stubs carry the full answer to prove every decoder takes it, and the model doesn't promise them.
  • Contract source: bc3#14821, merged to bc3 master as 1bd5dffa.
  • Also fixed: Ruby's raw PUT no longer refreshes the token a second time when its replay after a refreshed 401 gets another 401; TypeScript reopens a caller's stream on a redirect only when configure kept the SDK's request; any configure replacement of a stream upload is sent once, never cloned, and outside a browser a redirect that would resend it is refused. Telling which replacements carry the caller's stream on (the request itself, request.body, a clone(), a pipeThrough, a reader) kept finding another way, so the rule no longer tries; a replacement with bytes of its own loses redirect resends too.
  • Foreign-URL fixture. The case for a direct_upload.url on another host keeps a different path too (/elsewhere/<sgid>). That way requestPath catches a client that followed the URL even in a runner that doesn't check the host.

Merging waits on bc3#14821 reaching production. Not for landing without a person's word.


Summary by cubic

Direct uploads become the API's upload path in all seven SDKs, via two new generated operations (CreateDirectUpload, UploadDirectUpload) and an attachments.upload composite that runs them in one call and returns an UploadedAttachment with a richTextTag(caption:). The PUT is addressed by sgid on the client's own origin — the create's direct_upload.url is never followed — and carries a Content-MD5 of the bytes, so re-sending the same bytes answers 204 and stores nothing. Landing depends on bc3#14821.

New Features

  • upload accepts bytes, a file streamed from disk, or a known-length stream; reports progress, cancels in the caller's style, and carries request options to both requests. It hashes the source incrementally before the create and sends the digest on the PUT alone. A create answer without a usable sgid fails before the PUT, known-empty bodies go out with Content-Length: 0, and Go trusts a file's measured size rather than statting it at send time.
  • Swift and Kotlin add prepared uploads for a background URLSession or WorkManager: prepareUpload builds the PUT, authorize(prepared) refreshes credentials after a relaunch (re-hashing a serialized upload that predates Content-MD5), and sendPreparedUpload sends in process. Serialized uploads carry no credentials. Prepared uploads refuse configure before staging and refuse answers from another origin; Swift also refuses PUT redirects, shipping DirectUploadRedirects for an app's URLSession delegate.
  • Hints apply when the server sends none, a blank server hint included: 507 on the create, and 404 and 403 on the PUT — the latter two saying a direct upload takes its file for 24 hours.
  • A configure replacement that carries a body is sent once, and any redirect that would resend it is refused; TypeScript's browser path already sends with redirect: error.

Conformance & generator fixes

  • Conformance gains requestBodyRaw and requestHeaderValue assertions — the latter requiring a header exactly once, so a repeated header fails in every runner — shared Content-MD5 vectors, and an attachments_upload fixture pinning both requests, origin handling, the digest and the error hints.
  • Generator fixes: Ruby dropped a raw-body PUT's bytes; Kotlin and Python sent void raw-body PUTs with the wrong verb.
  • Ruby sends its upload PUT once, like its other raw bodies — SPEC §18 names it as the exception — and a PUT error carries a pending_upload with the sgid, content type and digest a caller's resend needs.
  • Python treats an empty content type as unset, progress flows through hooks everywhere, and TypeScript and Swift finish once across PUT retries. TypeScript guesses content types from the extension table only.

hint: if you need to wait for this pull request to be reviewed, merged, or closed before continuing, use this instead of polling:
gh event listen --resource https://github.com/basecamp/basecamp-sdk/pull/1159 --event com.github.pull_request.review.submitted.v1,com.github.pull_request.state.changed.v1

  • It prints the first event as a JSON line on stdout and exits; run it again to wait for the next one.
  • Add --timeout to stop waiting after a while, for example --timeout 30m; it then exits with status 8.
  • Events carry no details; read the pull request to see what changed.
  • It misses changes made before it starts, so read the pull request once it is running.
  • See gh event listen --help for other event types.
  • Event subscriptions are in preview and may not be enabled for your account.
    hint: if you need to wait for this pull request to be reviewed, merged, or closed before continuing, use this instead of polling:
    gh event listen --resource https://github.com/basecamp/basecamp-sdk/pull/1159 --event com.github.pull_request.review.submitted.v1,com.github.pull_request.state.changed.v1
  • It prints the first event as a JSON line on stdout and exits; run it again to wait for the next one.
  • Add --timeout to stop waiting after a while, for example --timeout 30m; it then exits with status 8.
  • Events carry no details; read the pull request to see what changed.
  • It misses changes made before it starts, so read the pull request once it is running.
  • See gh event listen --help for other event types.
  • Event subscriptions are in preview and may not be enabled for your account.