fn build()

in core/src/services/onedrive/builder.rs [144:244]


    fn build(self) -> Result<impl Access> {
        let root = normalize_root(&self.config.root.unwrap_or_default());
        debug!("backend use root {}", root);

        let info = AccessorInfo::default();
        info.set_scheme(Scheme::Onedrive)
            .set_root(&root)
            .set_native_capability(Capability {
                read: true,
                read_with_if_none_match: true,

                write: true,
                write_with_if_match: true,
                // OneDrive supports the file size up to 250GB
                // Read more at https://support.microsoft.com/en-us/office/restrictions-and-limitations-in-onedrive-and-sharepoint-64883a5d-228e-48f5-b3d2-eb39e07630fa#individualfilesize
                // However, we can't enable this, otherwise OpenDAL behavior tests will try to test creating huge
                // file up to this size.
                // write_total_max_size: Some(250 * 1024 * 1024 * 1024),
                copy: true,
                rename: true,

                stat: true,
                stat_with_if_none_match: true,
                stat_has_content_length: true,
                stat_has_etag: true,
                stat_has_last_modified: true,
                stat_with_version: self.config.enable_versioning,

                delete: true,
                create_dir: true,

                list: true,
                list_with_limit: true,
                list_with_versions: self.config.enable_versioning,
                list_has_content_length: true,
                list_has_etag: true,
                list_has_last_modified: true,
                list_has_version: self.config.enable_versioning, // same as `list_with_versions`?

                shared: true,

                ..Default::default()
            });

        // allow deprecated api here for compatibility
        #[allow(deprecated)]
        if let Some(client) = self.http_client {
            info.update_http_client(|_| client);
        }

        let accessor_info = Arc::new(info);
        let mut signer = OneDriveSigner::new(accessor_info.clone());

        // Requires OAuth 2.0 tokens:
        // - `access_token` (the short-lived token)
        // - `refresh_token` flow (the long term token)
        // to be mutually exclusive for setting up for implementation simplicity
        match (self.config.access_token, self.config.refresh_token) {
            (Some(access_token), None) => {
                signer.access_token = access_token;
                signer.expires_in = DateTime::<Utc>::MAX_UTC;
            }
            (None, Some(refresh_token)) => {
                let client_id = self.config.client_id.ok_or_else(|| {
                    Error::new(
                        ErrorKind::ConfigInvalid,
                        "client_id must be set when refresh_token is set",
                    )
                    .with_context("service", Scheme::Onedrive)
                })?;

                signer.refresh_token = refresh_token;
                signer.client_id = client_id;
                if let Some(client_secret) = self.config.client_secret {
                    signer.client_secret = client_secret;
                }
            }
            (Some(_), Some(_)) => {
                return Err(Error::new(
                    ErrorKind::ConfigInvalid,
                    "access_token and refresh_token cannot be set at the same time",
                )
                .with_context("service", Scheme::Onedrive))
            }
            (None, None) => {
                return Err(Error::new(
                    ErrorKind::ConfigInvalid,
                    "access_token or refresh_token must be set",
                )
                .with_context("service", Scheme::Onedrive))
            }
        };

        let core = Arc::new(OneDriveCore {
            info: accessor_info,
            root,
            signer: Arc::new(Mutex::new(signer)),
        });

        Ok(OnedriveBackend { core })
    }