Upload attachments in one call, through direct uploads
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) andUploadDirectUpload(PUT /attachments/direct_uploads/{attachableSgid}), with every generated artifact regenerated.UploadDirectUploadis@idempotentand@basecampIdempotent(natural: true): once bc3 holds the file, a PUT whoseContent-MD5names it answers 204 and stores nothing. So §7 retries it, resending the body from byte zero with the same header. - One-call
uploadcomposite in all seven SDKs (SPEC §18 "Attachment Uploads"). It runs the generated create, then the generated PUT, and returns anUploadedAttachmentwith arichTextTag(caption?).- The PUT is addressed by sgid on the client's own origin;
direct_upload.urlis 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'smd-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 againstconformance/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, andrequest_optionscarrying the header).
- The PUT is addressed by sgid on the client's own origin;
- Prepared Requests (SPEC §13), in Swift and Kotlin.
prepareUploadmakes the create and returns the PUT unsent, for a backgroundURLSessionor WorkManager.authorize(prepared)rebuilds the PUT with fresh credentials after a relaunch.sendPreparedUploadsends it in process.- A serialized
PreparedUploadcarries no credential and no userinfo. - Both generators emit a
preparesibling for every raw-body operation. configureis refused, a file whose size changed since preparing is refused, and an unfollowed 3xx decodes to the origin-only redirect error.- A serialized
PreparedUploadcarries theContent-MD5; one serialized before this has its file hashed byauthorize. - Swift refuses redirects of a direct-upload PUT: its own transport does,
DirectUploadRedirectslets an app'sURLSessionTaskDelegatedo the same, anddecode(response:request:body:)refuses an answer from any URL but the PUT's. A backgroundURLSessionfollows 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.
- A serialized
- Conformance. Two new assertion types,
requestBodyRawandrequestHeaderValue(schema, SPEC §19 row, handlers in all seven runners), andconformance/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 onto0dca8cc9b. - Route allowlist.
spec/bc3-route-allowlist.ymlgets twosdk_routes_absent_from_bc3_docsentries for these routes. They cite bc3routes.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
master1bd5dffa, with bc3#14821 merged and the whole Content-MD5 contract in it (DirectUpload::UPLOAD_EXPIRES_IN,direct_upload_stored?onContent-MD5, and theupload_untilsgid):- 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-MD5rides 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 callerContent-MD5in any case, adds its own, and hands the copy to the PUT alone. A caller of the generateduploadDirectUploadpasses the header inrequestOptions.headers, which conformance asserts. In a browser, caller headers send the PUT withredirect: "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
masteras1bd5dffa. - 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
configurekept the SDK's request; anyconfigurereplacement 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, aclone(), apipeThrough, 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.urlon another host keeps a different path too (/elsewhere/<sgid>). That wayrequestPathcatches 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
uploadaccepts 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 withContent-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
URLSessionor WorkManager:prepareUploadbuilds the PUT,authorize(prepared)refreshes credentials after a relaunch (re-hashing a serialized upload that predatesContent-MD5), andsendPreparedUploadsends in process. Serialized uploads carry no credentials. Prepared uploads refuseconfigurebefore staging and refuse answers from another origin; Swift also refuses PUT redirects, shippingDirectUploadRedirectsfor 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
configurereplacement that carries a body is sent once, and any redirect that would resend it is refused; TypeScript's browser path already sends withredirect: error.
Conformance & generator fixes
- Conformance gains
requestBodyRawandrequestHeaderValueassertions — the latter requiring a header exactly once, so a repeated header fails in every runner — sharedContent-MD5vectors, and anattachments_uploadfixture 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_uploadwith 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
--timeoutto 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 --helpfor 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
--timeoutto 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 --helpfor other event types. - Event subscriptions are in preview and may not be enabled for your account.