Skip to content

Instantly share code, notes, and snippets.

@davidlwg
Created December 11, 2025 19:33
Show Gist options
  • Select an option

  • Save davidlwg/bdaca876efd7b1086e2583accc3fe8ab to your computer and use it in GitHub Desktop.

Select an option

Save davidlwg/bdaca876efd7b1086e2583accc3fe8ab to your computer and use it in GitHub Desktop.
Sending CSV as a single byte stream
#[async_trait]
impl Meetkunde for MeetkundeClient {
async fn send_report(&mut self, table_name: &str, df: &DataFrame) -> Result<()> {
let total_rows = df.height();
if total_rows == 0 {
return Ok(());
}
// 1. Convert the WHOLE DataFrame to a CSV string in memory
// (This replaces the chunking loop logic)
let csv_string = dataframe_to_csv(df)?;
let total_bytes = csv_string.len();
info!(
"TESTING STREAM: Sending table '{}' ({} rows, {} bytes) as a single stream...",
table_name, total_rows, total_bytes
);
// 2. Turn the String into a Stream
// Cursor makes the bytes read like a file; ReaderStream makes it async.
let cursor = Cursor::new(csv_string.into_bytes());
let stream = ReaderStream::new(cursor);
let vanilla_client = reqwest::Client::builder()
.timeout(Duration::from_secs(300)) // Give it plenty of time to hang
.build()?;
// 3. Wrap in reqwest Body (forces Transfer-Encoding: chunked)
let body = Body::wrap_stream(stream);
// 4. Send ONE Request
// We always truncate=true because we are sending the whole file at once.
let url = self.build_url(table_name, true);
let response = vanilla_client
.post(url)
.header("Content-Type", "text/csv")
.body(body)
.send()
.await
.context("Failed to stream request to Meetkunde")?;
let status = response.status();
if status == StatusCode::ACCEPTED {
info!(
"✅ Streaming Success: Server accepted the stream for '{}'",
table_name
);
Ok(())
} else {
let error_text = response.text().await.unwrap_or_default();
anyhow::bail!(
"❌ Streaming Failed ({} {}): {}",
status.as_u16(),
status.canonical_reason().unwrap_or("Unknown"),
error_text
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment