Skip to content

Commit

Permalink
Configurable Slack URL for testing purposes (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
abdolence authored Mar 1, 2024
1 parent 9ed349b commit b036d76
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 35 deletions.
6 changes: 4 additions & 2 deletions src/api/oauth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ where
req: &SlackOAuthV2AccessTokenRequest,
) -> ClientResult<SlackOAuthV2AccessTokenResponse> {
let full_uri: Url = SlackClientHttpApiUri::create_url_with_params(
&SlackClientHttpApiUri::create_method_uri_path("oauth.v2.access"),
self.http_api
.connector
.create_method_uri_path("oauth.v2.access")?,
&vec![
("code", Some(req.code.value())),
(
Expand All @@ -36,7 +38,7 @@ where
.as_ref(),
),
],
);
)?;

self.http_api
.connector
Expand Down
8 changes: 6 additions & 2 deletions src/api/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ where
///
pub async fn api_test(&self, req: &SlackApiTestRequest) -> ClientResult<SlackApiTestResponse> {
let full_uri = SlackClientHttpApiUri::create_url_with_params(
&SlackClientHttpApiUri::create_method_uri_path("api.test"),
self.http_session_api
.client
.http_api
.connector
.create_method_uri_path("api.test")?,
&vec![("foo", req.foo.as_ref()), ("error", req.error.as_ref())],
);
)?;
self.http_session_api
.http_post_uri(full_uri, &req, Some(&SLACK_TIER4_METHOD_CONFIG))
.await
Expand Down
4 changes: 2 additions & 2 deletions src/axum_support/slack_oauth_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<H: 'static + Send + Sync + Connect + Clone> SlackEventsAxumListener<H> {
let environment = environment.clone();
async move {
let full_uri = SlackClientHttpApiUri::create_url_with_params(
SlackOAuthListenerConfig::OAUTH_AUTHORIZE_URL_VALUE,
SlackOAuthListenerConfig::OAUTH_AUTHORIZE_URL_VALUE.parse()?,
&vec![
("client_id", Some(config.client_id.value())),
("scope", Some(&config.bot_scope)),
Expand All @@ -39,7 +39,7 @@ impl<H: 'static + Send + Sync + Connect + Clone> SlackEventsAxumListener<H> {
Some(config.to_redirect_url()?.as_str().to_string()).as_ref(),
),
],
);
)?;
debug!("Redirecting to Slack OAuth authorize: {}", &full_uri);
HyperExtensions::hyper_redirect_to(full_uri.as_ref()).map(|r| r.into_response())
}
Expand Down
51 changes: 24 additions & 27 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use crate::errors::SlackClientError;
use crate::models::*;
use crate::multipart_form::FileMultipartData;
use crate::ratectl::SlackApiMethodRateControlConfig;
use futures_util::future::BoxFuture;
use futures::future::BoxFuture;
use futures::FutureExt;
use lazy_static::*;
use rvstruct::ValueStruct;
use tracing::*;
Expand Down Expand Up @@ -43,7 +44,7 @@ pub struct SlackClientHttpSessionApi<'a, SCHC>
where
SCHC: SlackClientHttpConnector + Send,
{
client: &'a SlackClient<SCHC>,
pub client: &'a SlackClient<SCHC>,
token: &'a SlackApiToken,
pub span: Span,
}
Expand Down Expand Up @@ -85,12 +86,14 @@ pub trait SlackClientHttpConnector {
PT: std::iter::IntoIterator<Item = (&'p str, Option<TS>)> + Clone,
TS: AsRef<str> + 'p + Send,
{
let full_uri = SlackClientHttpApiUri::create_url_with_params(
&SlackClientHttpApiUri::create_method_uri_path(method_relative_uri),
params,
);
let full_uri = self
.create_method_uri_path(method_relative_uri)
.and_then(|url| SlackClientHttpApiUri::create_url_with_params(url, params));

self.http_get_uri(full_uri, context)
match full_uri {
Ok(full_uri) => self.http_get_uri(full_uri, context),
Err(err) => std::future::ready(Err(err)).boxed(),
}
}

fn http_post_uri<'a, RQ, RS>(
Expand All @@ -113,11 +116,10 @@ pub trait SlackClientHttpConnector {
RQ: serde::ser::Serialize + Send + Sync,
RS: for<'de> serde::de::Deserialize<'de> + Send + 'a,
{
let full_uri = SlackClientHttpApiUri::create_url(
&SlackClientHttpApiUri::create_method_uri_path(method_relative_uri),
);

self.http_post_uri(full_uri, request, context)
match self.create_method_uri_path(method_relative_uri) {
Ok(full_uri) => self.http_post_uri(full_uri, request, context),
Err(err) => std::future::ready(Err(err)).boxed(),
}
}

fn http_post_uri_multipart_form<'a, 'p, RS, PT, TS>(
Expand All @@ -144,11 +146,14 @@ pub trait SlackClientHttpConnector {
PT: std::iter::IntoIterator<Item = (&'p str, Option<TS>)> + Clone,
TS: AsRef<str> + 'p + Send,
{
let full_uri = SlackClientHttpApiUri::create_url(
&SlackClientHttpApiUri::create_method_uri_path(method_relative_uri),
);
match self.create_method_uri_path(method_relative_uri) {
Ok(full_uri) => self.http_post_uri_multipart_form(full_uri, file, params, context),
Err(err) => std::future::ready(Err(err)).boxed(),
}
}

self.http_post_uri_multipart_form(full_uri, file, params, context)
fn create_method_uri_path(&self, method_relative_uri: &str) -> ClientResult<Url> {
Ok(SlackClientHttpApiUri::create_method_uri_path(method_relative_uri).parse()?)
}
}

Expand Down Expand Up @@ -188,17 +193,13 @@ where
pub struct SlackClientHttpApiUri;

impl SlackClientHttpApiUri {
const SLACK_API_URI_STR: &'static str = "https://slack.com/api";
pub const SLACK_API_URI_STR: &'static str = "https://slack.com/api";

pub fn create_method_uri_path(method_relative_uri: &str) -> String {
format!("{}/{}", Self::SLACK_API_URI_STR, method_relative_uri)
}

pub(crate) fn create_url(url_str: &str) -> Url {
url_str.parse().unwrap()
}

pub fn create_url_with_params<'p, PT, TS>(url_str: &str, params: &'p PT) -> Url
pub fn create_url_with_params<'p, PT, TS>(base_url: Url, params: &'p PT) -> ClientResult<Url>
where
PT: std::iter::IntoIterator<Item = (&'p str, Option<TS>)> + Clone,
TS: AsRef<str> + 'p,
Expand All @@ -209,11 +210,7 @@ impl SlackClientHttpApiUri {
.filter_map(|(k, vo)| vo.map(|v| (k.to_string(), v.as_ref().to_string())))
.collect();

Url::parse_with_params(url_str, url_query_params)
.unwrap()
.as_str()
.parse()
.unwrap()
Ok(Url::parse_with_params(base_url.as_str(), url_query_params)?)
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/hyper_tokio/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use url::Url;
pub struct SlackClientHyperConnector<H: Send + Sync + Clone + connect::Connect> {
hyper_connector: Client<H, Body>,
tokio_rate_controller: Option<Arc<SlackTokioRateController>>,
slack_api_url: String,
}

pub type SlackClientHyperHttpsConnector =
Expand Down Expand Up @@ -59,6 +60,7 @@ impl<H: 'static + Send + Sync + Clone + connect::Connect> SlackClientHyperConnec
Self {
hyper_connector: Client::builder(TokioExecutor::new()).build::<_, Body>(connector),
tokio_rate_controller: None,
slack_api_url: SlackClientHttpApiUri::SLACK_API_URI_STR.to_string(),
}
}

Expand All @@ -71,6 +73,13 @@ impl<H: 'static + Send + Sync + Clone + connect::Connect> SlackClientHyperConnec
}
}

pub fn with_slack_api_url(self, slack_api_url: &str) -> Self {
Self {
slack_api_url: slack_api_url.to_string(),
..self
}
}

async fn send_http_request<'a, RS>(
&'a self,
request: Request<Body>,
Expand Down Expand Up @@ -267,6 +276,10 @@ impl<H: 'static + Send + Sync + Clone + connect::Connect> SlackClientHyperConnec
impl<H: 'static + Send + Sync + Clone + connect::Connect> SlackClientHttpConnector
for SlackClientHyperConnector<H>
{
fn create_method_uri_path(&self, method_relative_uri: &str) -> ClientResult<Url> {
Ok(format!("{}/{}", self.slack_api_url, method_relative_uri).parse()?)
}

fn http_get_uri<'a, RS>(
&'a self,
full_uri: Url,
Expand Down
4 changes: 2 additions & 2 deletions src/hyper_tokio/listener/oauth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<H: 'static + Send + Sync + Connect + Clone> SlackClientEventsHyperListener<
config: &SlackOAuthListenerConfig,
) -> AnyStdResult<Response<Body>> {
let full_uri = SlackClientHttpApiUri::create_url_with_params(
SlackOAuthListenerConfig::OAUTH_AUTHORIZE_URL_VALUE,
SlackOAuthListenerConfig::OAUTH_AUTHORIZE_URL_VALUE.parse()?,
&vec![
("client_id", Some(config.client_id.value())),
("scope", Some(&config.bot_scope)),
Expand All @@ -31,7 +31,7 @@ impl<H: 'static + Send + Sync + Connect + Clone> SlackClientEventsHyperListener<
Some(config.to_redirect_url()?.as_str().to_string()).as_ref(),
),
],
);
)?;
debug!("Redirecting to Slack OAuth authorize: {}", &full_uri);
HyperExtensions::hyper_redirect_to(full_uri.as_ref())
}
Expand Down

0 comments on commit b036d76

Please sign in to comment.