diff --git a/src/service.rs b/src/service.rs index cf57e5a..08ce369 100644 --- a/src/service.rs +++ b/src/service.rs @@ -61,9 +61,13 @@ impl ApiResponse for GitLabApiResponse { pub(crate) trait Service { type Response: for<'de> serde::Deserialize<'de> + ApiResponse + 'static; + fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String; + fn api_url(path: &FilePath) -> String; + fn redirect_url(user: &str, repo: &str, commit: &str, file: &str) -> String; + fn request_head( mut response: ClientResponse, data: web::Path, @@ -100,6 +104,17 @@ pub(crate) trait Service { pub(crate) struct Github; +impl Github { + fn auth_query() -> Option { + use std::env::var; + var("GITHUB_CLIENT_ID").ok().and_then(|id| { + var("GITHUB_CLIENT_SECRET") + .ok() + .map(|secret| format!("?client_id={}&client_secret={}", id, secret)) + }) + } +} + impl Service for Github { type Response = GitHubApiResponse; @@ -112,8 +127,11 @@ impl Service for Github { fn api_url(path: &FilePath) -> String { format!( - "https://api.github.com/repos/{}/{}/commits/{}", - path.user, path.repo, path.commit + "https://api.github.com/repos/{}/{}/commits/{}{}", + path.user, + path.repo, + path.commit, + Self::auth_query().unwrap_or_default() ) }