Skip to content

Commit

Permalink
feat: add possibility to configure base upload uri
Browse files Browse the repository at this point in the history
  • Loading branch information
loispostula committed Jul 26, 2024
1 parent 87f2ed2 commit a5d2afe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
26 changes: 23 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ pub use self::{
pub type Result<T, E = error::Error> = std::result::Result<T, E>;

const GITHUB_BASE_URI: &str = "https://api.github.com";
const GITHUB_BASE_UPLOAD_URI: &str = "https://uploads.github.com";

#[cfg(feature = "default-client")]
static STATIC_INSTANCE: Lazy<arc_swap::ArcSwap<Octocrab>> =
Expand Down Expand Up @@ -583,6 +584,17 @@ impl OctocrabBuilder<NoSvc, DefaultOctocrabBuilderConfig, NoAuth, NotLayerReady>
Ok(self)
}

/// Set the base upload url for `Octocrab`.
pub fn upload_uri(mut self, upload_uri: impl TryInto<Uri>) -> Result<Self> {
self.config.upload_uri = Some(
upload_uri
.try_into()
.map_err(|_| UriParseError {})
.context(UriParseSnafu)?,
);
Ok(self)
}

#[cfg(feature = "retry")]
pub fn set_connector_retry_service<S>(
&self,
Expand Down Expand Up @@ -756,15 +768,21 @@ impl OctocrabBuilder<NoSvc, DefaultOctocrabBuilderConfig, NoAuth, NotLayerReady>
})
.layer(client);

let uri = self
let base_uri = self
.config
.base_uri
.clone()
.unwrap_or_else(|| Uri::from_str(GITHUB_BASE_URI).unwrap());

let client = BaseUriLayer::new(uri.clone()).layer(client);
let upload_uri = self
.config
.upload_uri
.clone()
.unwrap_or_else(|| Uri::from_str(GITHUB_BASE_UPLOAD_URI).unwrap());

let client = BaseUriLayer::new(base_uri.clone()).layer(client);

let client = AuthHeaderLayer::new(auth_header, uri).layer(client);
let client = AuthHeaderLayer::new(auth_header, base_uri, upload_uri).layer(client);

Ok(Octocrab::new(client, auth_state))
}
Expand All @@ -781,6 +799,7 @@ pub struct DefaultOctocrabBuilderConfig {
#[cfg(feature = "timeout")]
write_timeout: Option<Duration>,
base_uri: Option<Uri>,
upload_uri: Option<Uri>,
#[cfg(feature = "retry")]
retry_config: RetryConfig,
}
Expand All @@ -798,6 +817,7 @@ impl Default for DefaultOctocrabBuilderConfig {
#[cfg(feature = "timeout")]
write_timeout: None,
base_uri: None,
upload_uri: None,
#[cfg(feature = "retry")]
retry_config: RetryConfig::Simple(3),
}
Expand Down
9 changes: 7 additions & 2 deletions src/service/middleware/auth_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ use tower::{Layer, Service};
pub struct AuthHeaderLayer {
pub(crate) auth_header: Arc<Option<HeaderValue>>,
base_uri: Uri,
upload_uri: Uri,
}

impl AuthHeaderLayer {
pub fn new(auth_header: Option<HeaderValue>, base_uri: Uri) -> Self {
pub fn new(auth_header: Option<HeaderValue>, base_uri: Uri, upload_uri: Uri) -> Self {
AuthHeaderLayer {
auth_header: Arc::new(auth_header),
base_uri,
upload_uri,
}
}
}
Expand All @@ -27,6 +29,7 @@ impl<S> Layer<S> for AuthHeaderLayer {
inner,
auth_header: self.auth_header.clone(),
base_uri: self.base_uri.clone(),
upload_uri: self.upload_uri.clone(),
}
}
}
Expand All @@ -37,6 +40,7 @@ pub struct AuthHeader<S> {
inner: S,
pub(crate) auth_header: Arc<Option<HeaderValue>>,
base_uri: Uri,
upload_uri: Uri,
}

impl<S, ReqBody> Service<Request<ReqBody>> for AuthHeader<S>
Expand All @@ -60,7 +64,8 @@ where
// away from GitHub (via follow_location_to_data()), and we don't
// want to give our credentials to third-party services.
let authority = req.uri().authority();
if authority.is_none() || authority == self.base_uri.authority() {
let allowed_authorities = [self.base_uri.authority(), self.upload_uri.authority()];
if authority.is_none() || allowed_authorities.contains(&authority) {
if let Some(auth_header) = &*self.auth_header {
req.headers_mut().append(AUTHORIZATION, auth_header.clone());
}
Expand Down

0 comments on commit a5d2afe

Please sign in to comment.