From 126cb050f74cd606b6bd5eb8b27e9753897f8feb Mon Sep 17 00:00:00 2001 From: hoslo Date: Thu, 28 Mar 2024 15:28:47 +0800 Subject: [PATCH] feat(services/github): make access_token optional (#4404) --- core/src/services/github/backend.rs | 15 +++++---------- core/src/services/github/core.rs | 25 +++++++++++++++++++------ 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/core/src/services/github/backend.rs b/core/src/services/github/backend.rs index b9a3d6e5c37e..71e4815f8484 100644 --- a/core/src/services/github/backend.rs +++ b/core/src/services/github/backend.rs @@ -45,8 +45,10 @@ pub struct GithubConfig { pub root: Option, /// Github access_token. /// - /// required. - pub token: String, + /// optional. + /// If not provided, the backend will only support read operations for public repositories. + /// And rate limit will be limited to 60 requests per hour. + pub token: Option, /// Github repo owner. /// /// required. @@ -105,7 +107,7 @@ impl GithubBuilder { /// /// required. pub fn token(&mut self, token: &str) -> &mut Self { - self.config.token = token.to_string(); + self.config.token = Some(token.to_string()); self } @@ -168,13 +170,6 @@ impl Builder for GithubBuilder { let root = normalize_root(&self.config.root.clone().unwrap_or_default()); debug!("backend use root {}", &root); - // Handle token. - if self.config.token.is_empty() { - return Err(Error::new(ErrorKind::ConfigInvalid, "token is empty") - .with_operation("Builder::build") - .with_context("service", Scheme::Github)); - } - // Handle owner. if self.config.owner.is_empty() { return Err(Error::new(ErrorKind::ConfigInvalid, "owner is empty") diff --git a/core/src/services/github/core.rs b/core/src/services/github/core.rs index 0c93c83aa5ed..799cbe4fce18 100644 --- a/core/src/services/github/core.rs +++ b/core/src/services/github/core.rs @@ -39,7 +39,7 @@ pub struct GithubCore { /// The root of this core. pub root: String, /// Github access_token. - pub token: String, + pub token: Option, /// Github repo owner. pub owner: String, /// Github repo name. @@ -65,19 +65,32 @@ impl GithubCore { } pub fn sign(&self, req: request::Builder) -> Result { - let req = req + let mut req = req .header(header::USER_AGENT, format!("opendal-{}", VERSION)) .header("X-GitHub-Api-Version", "2022-11-28"); - Ok(req.header( - header::AUTHORIZATION, - format_authorization_by_bearer(&self.token)?, - )) + // Github access_token is optional. + if let Some(token) = &self.token { + req = req.header( + header::AUTHORIZATION, + format_authorization_by_bearer(token)?, + ) + } + + Ok(req) } } impl GithubCore { pub async fn get_file_sha(&self, path: &str) -> Result> { + // if the token is not set, we shhould not try to get the sha of the file. + if self.token.is_none() { + return Err(Error::new( + ErrorKind::PermissionDenied, + "Github access_token is not set", + )); + } + let resp = self.stat(path).await?; match resp.status() {