aboutsummaryrefslogtreecommitdiff
path: root/src/util/web.rs
diff options
context:
space:
mode:
authorLorenz Leitner <lrnz.ltnr@gmail.com>2021-10-10 15:12:41 +0200
committerLorenz Leitner <lrnz.ltnr@gmail.com>2021-10-12 12:06:57 +0200
commited1c228094188d872ceb8407fb6f46ff698937c2 (patch)
tree7d97b610220ff57cfcbf874b45936a88571de8a7 /src/util/web.rs
parentBasic (re)implementation of gonzobot wolfram alpha (diff)
Add test
Put WA api response JSON into test resource file Add short url, increase concurrency Move shorten_url to util dir
Diffstat (limited to 'src/util/web.rs')
-rw-r--r--src/util/web.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/util/web.rs b/src/util/web.rs
new file mode 100644
index 0000000..77e20a3
--- /dev/null
+++ b/src/util/web.rs
@@ -0,0 +1,20 @@
+use anyhow::{Context, Error, Result};
+use reqwest::{get, Url};
+
+// 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)
+}