api: Rename SendAccessToken methods to be clearer

Co-authored-by: Johannes Becker <j.becker@famedly.com>
This commit is contained in:
Jonas Platte 2021-04-23 15:01:57 +02:00
parent 1e005f576e
commit 12ec0fb168
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
3 changed files with 14 additions and 14 deletions

View File

@ -141,14 +141,14 @@ impl Request {
::std::convert::TryFrom::<_>::try_from(::std::format!( ::std::convert::TryFrom::<_>::try_from(::std::format!(
"Bearer {}", "Bearer {}",
access_token access_token
.get_required() .get_required_for_endpoint()
.ok_or(#ruma_api::error::IntoHttpError::NeedsAuthentication)?, .ok_or(#ruma_api::error::IntoHttpError::NeedsAuthentication)?,
))?, ))?,
); );
} }
} else { } else {
quote! { quote! {
if let Some(access_token) = access_token.get_optional() { if let Some(access_token) = access_token.get_not_required_for_endpoint() {
#( #attrs )* #( #attrs )*
req_headers.insert( req_headers.insert(
#http::header::AUTHORIZATION, #http::header::AUTHORIZATION,

View File

@ -229,25 +229,25 @@ pub enum SendAccessToken<'a> {
} }
impl<'a> SendAccessToken<'a> { impl<'a> SendAccessToken<'a> {
/// Get the access token for an endpoint that should not require one.
///
/// Returns `Some(_)` only if `self` is `SendAccessToken::Always(_)`.
pub fn get_optional(self) -> Option<&'a str> {
match self {
Self::Always(tok) => Some(tok),
Self::IfRequired(_) | Self::None => None,
}
}
/// Get the access token for an endpoint that requires one. /// Get the access token for an endpoint that requires one.
/// ///
/// Returns `Some(_)` if `self` contains an access token. /// Returns `Some(_)` if `self` contains an access token.
pub fn get_required(self) -> Option<&'a str> { pub fn get_required_for_endpoint(self) -> Option<&'a str> {
match self { match self {
Self::IfRequired(tok) | Self::Always(tok) => Some(tok), Self::IfRequired(tok) | Self::Always(tok) => Some(tok),
Self::None => None, Self::None => None,
} }
} }
/// Get the access token for an endpoint that should not require one.
///
/// Returns `Some(_)` only if `self` is `SendAccessToken::Always(_)`.
pub fn get_not_required_for_endpoint(self) -> Option<&'a str> {
match self {
Self::Always(tok) => Some(tok),
Self::IfRequired(_) | Self::None => None,
}
}
} }
/// A request type for a Matrix API endpoint, used for sending requests. /// A request type for a Matrix API endpoint, used for sending requests.

View File

@ -97,7 +97,7 @@ impl<'a> ruma_api::OutgoingRequest for Request<'a> {
format!( format!(
"Bearer {}", "Bearer {}",
access_token access_token
.get_required() .get_required_for_endpoint()
.ok_or(ruma_api::error::IntoHttpError::NeedsAuthentication)?, .ok_or(ruma_api::error::IntoHttpError::NeedsAuthentication)?,
), ),
) )