aboutsummaryrefslogtreecommitdiff
path: root/src/util/web
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/util/web/mod.rs (renamed from src/util/web.rs)34
-rw-r--r--src/util/web/url_shorteners.rs21
2 files changed, 29 insertions, 26 deletions
diff --git a/src/util/web.rs b/src/util/web/mod.rs
index b9f44c1..4e886af 100644
--- a/src/util/web.rs
+++ b/src/util/web/mod.rs
@@ -1,36 +1,18 @@
-use anyhow::{Context, Error, Result};
+use anyhow::{Error, Result};
use async_trait::async_trait;
-use reqwest::{get, Url};
use urlparse::quote_plus as urlparse_quote_plus;
-#[async_trait]
-pub trait UrlShortener {
- fn new() -> Self;
- async fn shorten(&self, url: &str) -> Result<String, Error>;
-}
-
-pub struct IsgdUrlShortener {}
+pub mod url_shorteners;
+/// Shorten urls
#[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")?)
- }
+pub trait UrlShortener {
+ /// Call this method with the url you want shortened.
+ /// Returns the shortened url.
+ async fn shorten(url: &str) -> Result<String, Error>;
}
+/// quote strings to be URL save
pub fn quote_plus(text: &str) -> Result<String, Error> {
Ok(urlparse_quote_plus(text, b"")?)
}
diff --git a/src/util/web/url_shorteners.rs b/src/util/web/url_shorteners.rs
new file mode 100644
index 0000000..74d62ce
--- /dev/null
+++ b/src/util/web/url_shorteners.rs
@@ -0,0 +1,21 @@
+use anyhow::{Context, Error, Result};
+use async_trait::async_trait;
+use reqwest::{get, Url};
+
+pub struct Isgd;
+
+#[async_trait]
+impl super::UrlShortener for Isgd {
+ async fn shorten(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")?)
+ }
+}