hoslo commented on code in PR #3981: URL: https://github.com/apache/incubator-opendal/pull/3981#discussion_r1457166996
########## core/src/services/koofr/core.rs: ########## @@ -0,0 +1,455 @@ +// 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::fmt::Debug; +use std::fmt::Formatter; +use std::sync::Arc; + +use bytes::Bytes; +use http::header; +use http::Request; +use http::Response; +use http::StatusCode; +use log::debug; +use serde::Deserialize; +use serde_json::json; +use tokio::sync::RwLock; + +use crate::raw::*; +use crate::*; + +use super::error::parse_error; + +#[derive(Clone)] +pub struct KoofrCore { + /// The root of this core. + pub root: String, + /// The endpoint of this backend. + pub endpoint: String, + /// Koofr email + pub email: String, + /// Koofr password + pub password: String, + + /// signer of this backend. + pub signer: Arc<RwLock<KoofrSigner>>, + + pub client: HttpClient, +} + +impl Debug for KoofrCore { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Backend") + .field("root", &self.root) + .field("endpoint", &self.endpoint) + .field("email", &self.email) + .finish_non_exhaustive() + } +} + +impl KoofrCore { + #[inline] + pub async fn send(&self, req: Request<AsyncBody>) -> Result<Response<IncomingAsyncBody>> { + self.client.send(req).await + } + + pub async fn get_auth_data(&self) -> Result<AuthData> { + { + let signer = self.signer.read().await; Review Comment: fixed ########## core/src/services/koofr/backend.rs: ########## @@ -0,0 +1,391 @@ +// 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 async_trait::async_trait; +use http::StatusCode; +use log::debug; +use serde::Deserialize; +use tokio::sync::RwLock; + +use super::core::File; +use super::core::KoofrCore; +use super::core::KoofrSigner; +use super::error::parse_error; +use super::lister::KoofrLister; +use super::writer::KoofrWriter; +use super::writer::KoofrWriters; +use crate::raw::*; +use crate::*; + +/// Config for backblaze Koofr services support. +#[derive(Default, Deserialize)] +#[serde(default)] +#[non_exhaustive] +pub struct KoofrConfig { + /// root of this backend. + /// + /// All operations will happen under this root. + pub root: Option<String>, + /// Koofr endpoint. + pub endpoint: String, + /// Koofr email. + pub email: String, + /// password of this backend. + pub password: Option<String>, +} + +impl Debug for KoofrConfig { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("Config"); + + ds.field("root", &self.root); + ds.field("email", &self.email); + + ds.finish() + } +} + +/// [Koofr](https://app.koofr.net/) services support. +#[doc = include_str!("docs.md")] +#[derive(Default)] +pub struct KoofrBuilder { + config: KoofrConfig, + + http_client: Option<HttpClient>, +} + +impl Debug for KoofrBuilder { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let mut d = f.debug_struct("KoofrBuilder"); + + d.field("config", &self.config); + d.finish_non_exhaustive() + } +} + +impl KoofrBuilder { + /// Set root of this backend. + /// + /// All operations will happen under this root. + pub fn root(&mut self, root: &str) -> &mut Self { + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + + self + } + + /// endpoint. + /// + /// It is required. e.g. `https://api.koofr.net/` + pub fn endpoint(&mut self, endpoint: &str) -> &mut Self { + self.config.endpoint = endpoint.to_string(); + + self + } + + /// email. + /// + /// It is required. e.g. `t...@example.com` + pub fn email(&mut self, email: &str) -> &mut Self { + self.config.email = email.to_string(); + + self + } + + /// Koofr app password. + /// + /// Go to https://app.koofr.net/app/admin/preferences/password. + /// Click "Generate Password" button to generate a new password. + pub fn password(&mut self, password: &str) -> &mut Self { + self.config.password = if password.is_empty() { + None + } else { + Some(password.to_string()) + }; + + self + } + + /// Specify the http client that used by this service. + /// + /// # Notes + /// + /// This API is part of OpenDAL's Raw API. `HttpClient` could be changed + /// during minor updates. + pub fn http_client(&mut self, client: HttpClient) -> &mut Self { + self.http_client = Some(client); + self + } +} + +impl Builder for KoofrBuilder { + const SCHEME: Scheme = Scheme::Koofr; + type Accessor = KoofrBackend; + + /// Converts a HashMap into an KoofrBuilder instance. + /// + /// # Arguments + /// + /// * `map` - A HashMap containing the configuration values. + /// + /// # Returns + /// + /// Returns an instance of KoofrBuilder. + fn from_map(map: HashMap<String, String>) -> Self { + // Deserialize the configuration from the HashMap. + let config = KoofrConfig::deserialize(ConfigDeserializer::new(map)) + .expect("config deserialize must succeed"); + + // Create an KoofrBuilder instance with the deserialized config. + KoofrBuilder { + config, + http_client: None, + } + } + + /// Builds the backend and returns the result of KoofrBackend. + fn build(&mut self) -> Result<Self::Accessor> { + debug!("backend build started: {:?}", &self); + + let root = normalize_root(&self.config.root.clone().unwrap_or_default()); + debug!("backend use root {}", &root); + + // Handle endpoint. + if self.config.endpoint.is_empty() { + return Err(Error::new(ErrorKind::ConfigInvalid, "endpoint is empty") + .with_operation("Builder::build") + .with_context("service", Scheme::Koofr)); + } + + debug!("backend use endpoint {}", &self.config.endpoint); + + // Handle email. + if self.config.email.is_empty() { + return Err(Error::new(ErrorKind::ConfigInvalid, "email is empty") + .with_operation("Builder::build") + .with_context("service", Scheme::Koofr)); + } + + debug!("backend use email {}", &self.config.email); + + let password = match &self.config.password { + Some(operator) => Ok(operator.clone()), Review Comment: fixed -- 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: commits-unsubscr...@opendal.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org