@smbcloud / smbcloud-ascapi Mirror · read-only / commits / e623bca
Fork 0

Read-only mirror of github.com/smbcloudXYZ/smbcloud-ascapi.

Mirrored from https://github.com/smbcloudXYZ/smbcloud-ascapi.git

Synced Nov 25, 09:15 UTC

Address review findings on the in-app purchase commands

An upload operation that reaches past the end of the file is now an error instead of an empty chunk committed under the full-file checksum. Availability creation fails when the response has no data instead of returning an empty id. set-price and price-points uppercase the territory like set-availability does, and set-availability checks availableInNewTerritories on read-back as well as the territory list. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Co-Authored-By: siGit Code v1.7.6 <noreply@sigit.si>

Seto Elkahfi committed Nov 4, 2026 at 19:01 UTC e623bca46d0e4471a3a223770d02d8ff45da54eb
4 files changed +79 -17
crates/cli/src/main.rs
+9
@@ -3913,6 +3913,7 @@ async fn run_in_app_purchases(
3913 print_json(&back)
3914 }
3915 InAppPurchasesCommand::PricePoints { id, territory } => {
3916 + let territory = territory.to_uppercase();
3917 let points = client.list_price_points(&id, &territory).await?;
3918 let rows: Vec<_> = points
3919 .iter()
@@ -3931,6 +3932,7 @@ async fn run_in_app_purchases(
3932 territory,
3933 price: amount,
3934 } => {
3935 + let territory = territory.to_uppercase();
3936 let Some(point) = client.find_price_point(&id, &territory, &amount).await? else {
3937 anyhow::bail!(
3938 "{amount} is not one of Apple's price points in {territory}; \
@@ -3985,6 +3987,13 @@ async fn run_in_app_purchases(
3987 .get_in_app_purchase_availability(&id)
3988 .await?
3989 .context("availability was posted but reads back as unset")?;
3990 + if back.available_in_new_territories != Some(!no_new_territories) {
3991 + anyhow::bail!(
3992 + "availability reads back with availableInNewTerritories = {:?}, {} was sent",
3993 + back.available_in_new_territories,
3994 + !no_new_territories
3995 + );
3996 + }
3997 if back.territories != codes {
3998 anyhow::bail!(
3999 "availability reads back with {} territories, {} were sent",
crates/core/src/error.rs
+6
@@ -21,6 +21,12 @@ pub enum Error {
21 #[error("App Store Connect API returned {status}: {detail}")]
22 Api { status: u16, detail: String },
23
24 + #[error("invalid upload plan: {0}")]
25 + UploadPlan(String),
26 +
27 + #[error("App Store Connect's response carried no {0}")]
28 + MissingData(&'static str),
29 +
30 #[error("failed to generate an RSA key pair: {0}")]
31 KeyGeneration(String),
32
crates/iap/src/availability.rs
+3 -1
@@ -113,7 +113,9 @@ impl InAppPurchaseAvailabilityApi for Client {
113 Some(&body),
114 )
115 .await?;
116 - Ok(doc.data.map(|d| d.id).unwrap_or_default())
116 + doc.data
117 + .map(|d| d.id)
118 + .ok_or(smbcloud_ascapi_core::Error::MissingData("availability"))
119 }
120
121 async fn get_in_app_purchase_availability(
crates/iap/src/review_screenshot.rs
+61 -16
@@ -12,7 +12,7 @@ use smbcloud_ascapi_core::jsonapi::{
12 CreateBody, CreateData, Document, OptionalDocument, Resource, ResourceId, ToOne, UpdateBody,
13 UpdateData,
14 };
15 -use smbcloud_ascapi_core::{Client, Result};
15 +use smbcloud_ascapi_core::{Client, Error, Result};
16
17 pub const RESOURCE_TYPE: &str = "inAppPurchaseAppStoreReviewScreenshots";
18
@@ -107,6 +107,41 @@ pub fn create_body(
107 }
108 }
109
110 +/// The slice of `bytes` an upload operation asks for. One that reaches past
111 +/// the end of the file is an error: sending a short or empty chunk would
112 +/// commit a corrupt asset under the full-file checksum.
113 +fn chunk_for(op: &UploadOperation, bytes: &[u8]) -> Result<Vec<u8>> {
114 + let start = op.offset as usize;
115 + let end = start.saturating_add(op.length as usize);
116 + bytes.get(start..end).map(<[u8]>::to_vec).ok_or_else(|| {
117 + Error::UploadPlan(format!(
118 + "an upload operation covers bytes {start}..{end}, but the file has {}",
119 + bytes.len()
120 + ))
121 + })
122 +}
123 +
124 +/// PUT each slice of `bytes` that Apple's upload operations ask for.
125 +pub(crate) async fn upload_operations(
126 + client: &Client,
127 + operations: &[UploadOperation],
128 + bytes: &[u8],
129 +) -> Result<()> {
130 + for op in operations {
131 + let chunk = chunk_for(op, bytes)?;
132 + let headers: Vec<(String, String)> = op
133 + .request_headers
134 + .iter()
135 + .map(|h| (h.name.clone(), h.value.clone()))
136 + .collect();
137 + let method: Method = op.method.parse().unwrap_or(Method::PUT);
138 + client
139 + .upload_bytes(method, &op.url, &headers, chunk)
140 + .await?;
141 + }
142 + Ok(())
143 +}
144 +
145 #[async_trait]
146 pub trait InAppPurchaseReviewScreenshotApi {
147 /// `GET /v2/inAppPurchases/{id}/appStoreReviewScreenshot`.
@@ -164,21 +199,7 @@ impl InAppPurchaseReviewScreenshotApi for Client {
199 .await?;
200 let reserved = reserved.data;
201
167 - for op in &reserved.attributes.upload_operations {
168 - let start = op.offset as usize;
169 - let end = start + op.length as usize;
170 - let chunk = bytes
171 - .get(start..end)
172 - .map(<[u8]>::to_vec)
173 - .unwrap_or_default();
174 - let headers: Vec<(String, String)> = op
175 - .request_headers
176 - .iter()
177 - .map(|h| (h.name.clone(), h.value.clone()))
178 - .collect();
179 - let method: Method = op.method.parse().unwrap_or(Method::PUT);
180 - self.upload_bytes(method, &op.url, &headers, chunk).await?;
181 - }
202 + upload_operations(self, &reserved.attributes.upload_operations, &bytes).await?;
203
204 let path = format!("/v1/inAppPurchaseAppStoreReviewScreenshots/{}", reserved.id);
205 let commit = UpdateBody {
@@ -224,4 +245,28 @@ mod tests {
245 .unwrap();
246 assert!(doc.data.attributes.upload_operations.is_empty());
247 }
248 +
249 + fn op(offset: u64, length: u64) -> UploadOperation {
250 + UploadOperation {
251 + method: "PUT".into(),
252 + url: "https://example.invalid".into(),
253 + length,
254 + offset,
255 + request_headers: vec![],
256 + }
257 + }
258 +
259 + #[test]
260 + fn a_chunk_inside_the_file_is_cut_out() {
261 + assert_eq!(chunk_for(&op(2, 3), b"abcdefg").unwrap(), b"cde");
262 + }
263 +
264 + #[test]
265 + fn a_chunk_past_the_end_of_the_file_is_an_error() {
266 + assert!(matches!(
267 + chunk_for(&op(5, 10), b"abcdefg"),
268 + Err(Error::UploadPlan(_))
269 + ));
270 + assert!(chunk_for(&op(u64::MAX, 10), b"abc").is_err());
271 + }
272 }