Skip to content

Commit 8f9a36e

Browse files
committed
fix: apply URL override to cached discovery docs and fix base_url path
Two bugs in the initial implementation: - Cache path returned early without applying the URL rewrite, so requests still went to googleapis.com after the first fetch - base_url incorrectly prepended service name/version, causing doubled path segments (e.g., gmail/v1/gmail/v1/...) Extract apply_base_url_override() helper and call it in both the cache and network paths. Set base_url to just the custom host since method paths already contain the full API prefix.
1 parent c22dc58 commit 8f9a36e

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

src/discovery.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ pub async fn fetch_discovery_document(
230230
if let Ok(modified) = metadata.modified() {
231231
if modified.elapsed().unwrap_or_default() < std::time::Duration::from_secs(86400) {
232232
let data = std::fs::read_to_string(&cache_file)?;
233-
let doc: RestDescription = serde_json::from_str(&data)?;
233+
let mut doc: RestDescription = serde_json::from_str(&data)?;
234+
apply_base_url_override(&mut doc);
234235
return Ok(doc);
235236
}
236237
}
@@ -264,20 +265,19 @@ pub async fn fetch_discovery_document(
264265
}
265266

266267
let mut doc: RestDescription = serde_json::from_str(&body)?;
268+
apply_base_url_override(&mut doc);
269+
Ok(doc)
270+
}
267271

268-
// When a custom API base URL is set, rewrite the Discovery Document's
269-
// root_url and base_url so that all HTTP requests go to the custom
270-
// endpoint (e.g., a mock server) instead of the real Google APIs.
272+
/// Rewrite Discovery Document URLs when `GWS_API_BASE_URL` is set.
273+
/// Uses the same base_url structure as the original document — just
274+
/// swaps the host so request paths stay correct for the mock server.
275+
fn apply_base_url_override(doc: &mut RestDescription) {
271276
if let Some(base) = custom_api_base_url() {
272277
let base_trimmed = base.trim_end_matches('/');
273278
doc.root_url = format!("{base_trimmed}/");
274-
doc.base_url = Some(format!(
275-
"{base_trimmed}/{}/{}/",
276-
doc.name, doc.version
277-
));
279+
doc.base_url = Some(format!("{base_trimmed}/"));
278280
}
279-
280-
Ok(doc)
281281
}
282282

283283
#[cfg(test)]

0 commit comments

Comments
 (0)