aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/util/web.rs49
1 files changed, 30 insertions, 19 deletions
diff --git a/src/util/web.rs b/src/util/web.rs
index bfa7003..fec7bb0 100644
--- a/src/util/web.rs
+++ b/src/util/web.rs
@@ -1,32 +1,43 @@
use anyhow::{Context, Error, Result};
+use async_trait::async_trait;
use reqwest::{get, Url};
use urlparse::quote_plus as urlparse_quote_plus;
-pub(crate) fn quote_plus(text: &str) -> Result<String, Error> {
- Ok(urlparse_quote_plus(text, b"")?)
+#[async_trait]
+pub(crate) trait UrlShortener {
+ fn new() -> Self;
+ async fn shorten(&self, url: &str) -> Result<String, Error>;
+}
+
+pub(crate) struct IsgdUrlShortener {}
+
+#[async_trait]
+impl UrlShortener for IsgdUrlShortener {
+ fn new() -> Self {
+ Self {}
+ }
+
+ async fn shorten(&self, url: &str) -> Result<String, Error> {
+ Ok(get(Url::parse(&format!(
+ "https://is.gd/create.php?format=simple&url={}",
+ url
+ ))
+ .context("Failed to parse url")?)
+ .await
+ .context("Failed to make request")?
+ .text()
+ .await
+ .context("failed to get request response text")?)
+ }
}
-// TODO: Either catinator should have a URL shortening utility module,
-// or we should start our own service
-pub(crate) async fn shorten_url(url: &str) -> Result<String, Error> {
- // This just uses the first service gonzobot uses too
- let short_url = get(Url::parse(&format!(
- "https://is.gd/create.php?format=simple&url={}",
- url
- ))
- .context("Failed to parse url")?)
- .await
- .context("Failed to make request")?
- .text()
- .await
- .context("failed to get request response text")?;
-
- Ok(short_url)
+pub(crate) fn quote_plus(text: &str) -> Result<String, Error> {
+ Ok(urlparse_quote_plus(text, b"")?)
}
#[cfg(test)]
mod tests {
- use super::{quote_plus};
+ use super::quote_plus;
use anyhow::{Error, Result};
#[test]
mp;id=e32bd445bf4a21c82fcf7a83401cab28b739a055&follow=1'>remove failing wolfram alpha test casesMax Audron-105/+55 2021-10-20bump version to 1.6.2Max Audron-3/+2 2021-10-20prepare for release on crates.ioMax Audron-39/+65 2021-10-20add async docs to macro crate and bump versionMax Audron-9/+10 2021-10-20change hook errors to be logged as warningsMax Audron-3/+3 2021-10-20fix configuration not loading correctly on release buildsMax Audron-8/+23 2021-10-19replace sedregex crate8-rework-sedMax Audron-20/+358 2021-10-19add formatting trait for irc codesMax Audron-0/+129 2021-10-17fix links in readmeMax Audron-2/+2