Xuanwo commented on code in PR #2203: URL: https://github.com/apache/incubator-opendal/pull/2203#discussion_r1186801081
########## core/src/services/gdrive/core.rs: ########## @@ -0,0 +1,228 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::collections::HashMap; +use std::fmt::Debug; +use std::fmt::Formatter; +use std::sync::Arc; + +use crate::raw::HttpClient; +use crate::Error; +use crate::ErrorKind; +use crate::Scheme; + +use http::request::Builder; +use http::{header, Request, Response}; +use serde::Deserialize; +use tokio::sync::Mutex; + +use crate::{ + raw::{build_rooted_abs_path, new_request_build_error, AsyncBody, IncomingAsyncBody}, + types::Result, +}; + +pub struct GdriveCore { + pub root: String, + pub access_token: String, + pub client: HttpClient, + pub path_cache: Arc<Mutex<HashMap<String, String>>>, +} + +impl Debug for GdriveCore { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let mut de = f.debug_struct("GdriveCore"); + de.field("root", &self.root); + de.finish() + } +} + +impl GdriveCore { + async fn get_abs_root_id(&self) -> Result<String> { + let root = "root"; + + if let Some(root_id) = self.path_cache.lock().await.get(root) { + return Ok(root_id.to_string()); + } + + let req = self + .sign(Request::get( + "https://www.googleapis.com/drive/v3/files/root", + )) + .body(AsyncBody::Empty) + .map_err(new_request_build_error)?; + + let resp = self.client.send(req).await?; + let resp_body = &resp.into_body().bytes().await.map_err(|e| { + Error::new(ErrorKind::Unexpected, "read response body error") + .with_context("service", Scheme::Gdrive) + .set_source(e) + })?; + + let gdrive_file: GdriveFile = serde_json::from_slice(resp_body).map_err(|e| { Review Comment: No. We should check response status code first: - Parse into `GdriveFile` if it is Ok - Parse into `GdriveError` if it's other error - If the parse itself is failed, it must be our bug. So in general, json_{deserialize|serialize}_errors should only be used for unexpected cases. And all expected errors should be handled correctly. Please refer to s3's `S3Error` as a reference: https://github.com/apache/incubator-opendal/blob/5bc6b847f38ff27ac268ebcd805426568b4f420d/core/src/services/s3/backend.rs#L1069-L1072 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
