From 17c96d1102067fdab978183190e84744b0f1d1aa Mon Sep 17 00:00:00 2001 From: QP Hou Date: Mon, 18 Oct 2021 10:19:34 -0700 Subject: [PATCH] refactor encode_record_batches to get ready for http framework migration (#94) --- roapi-http/src/api/mod.rs | 44 ++++++++++++++------------------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/roapi-http/src/api/mod.rs b/roapi-http/src/api/mod.rs index 1791fc5..9fde35c 100644 --- a/roapi-http/src/api/mod.rs +++ b/roapi-http/src/api/mod.rs @@ -49,36 +49,24 @@ pub fn encode_record_batches( content_type: encoding::ContentType, batches: &[arrow::record_batch::RecordBatch], ) -> Result { + let payload = match content_type { + encoding::ContentType::Json => encoding::json::record_batches_to_bytes(batches) + .map_err(ApiErrResp::json_serialization)?, + encoding::ContentType::Csv => encoding::csv::record_batches_to_bytes(batches) + .map_err(ApiErrResp::csv_serialization)?, + encoding::ContentType::ArrowFile => encoding::arrow::record_batches_to_file_bytes(batches) + .map_err(ApiErrResp::arrow_file_serialization)?, + encoding::ContentType::ArrowStream => { + encoding::arrow::record_batches_to_stream_bytes(batches) + .map_err(ApiErrResp::arrow_stream_serialization)? + } + encoding::ContentType::Parquet => encoding::parquet::record_batches_to_bytes(batches) + .map_err(ApiErrResp::parquet_serialization)?, + }; + let mut resp = HttpResponse::Ok(); let builder = resp.content_type(content_type.to_str()); - - match content_type { - encoding::ContentType::Json => { - let payload = encoding::json::record_batches_to_bytes(batches) - .map_err(ApiErrResp::json_serialization)?; - Ok(builder.body(payload)) - } - encoding::ContentType::Csv => { - let payload = encoding::csv::record_batches_to_bytes(batches) - .map_err(ApiErrResp::csv_serialization)?; - Ok(builder.body(payload)) - } - encoding::ContentType::ArrowFile => { - let payload = encoding::arrow::record_batches_to_file_bytes(batches) - .map_err(ApiErrResp::arrow_file_serialization)?; - Ok(builder.body(payload)) - } - encoding::ContentType::ArrowStream => { - let payload = encoding::arrow::record_batches_to_stream_bytes(batches) - .map_err(ApiErrResp::arrow_stream_serialization)?; - Ok(builder.body(payload)) - } - encoding::ContentType::Parquet => { - let payload = encoding::parquet::record_batches_to_bytes(batches) - .map_err(ApiErrResp::parquet_serialization)?; - Ok(builder.body(payload)) - } - } + Ok(builder.body(payload)) } pub mod graphql;