Skip to content

Commit cf1f0aa

Browse files
committed
Factor out post_string()
1 parent 7595981 commit cf1f0aa

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/net/http.rs

+17
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ pub(crate) async fn get_client(context: &Context, load_cache: bool) -> Result<re
225225

226226
/// Sends an empty POST request to the URL.
227227
///
228+
/// Follows redirections.
229+
///
228230
/// Returns response text and whether request was successful or not.
229231
pub(crate) async fn post_empty(context: &Context, url: &str) -> Result<(String, bool)> {
230232
let mut url = url.to_string();
@@ -271,3 +273,18 @@ pub(crate) async fn post_empty(context: &Context, url: &str) -> Result<(String,
271273

272274
Err(anyhow!("Followed 10 redirections"))
273275
}
276+
277+
/// Posts string to the given URL.
278+
///
279+
/// Returns true if successful HTTP response code was returned.
280+
pub(crate) async fn post_string(context: &Context, url: &str, body: String) -> Result<bool> {
281+
let load_cache = true;
282+
let response = get_client(context, load_cache)
283+
.await?
284+
.post(url)
285+
.body(body)
286+
.send()
287+
.await?;
288+
289+
Ok(response.status().is_success())
290+
}

src/push.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,13 @@ impl PushSubscriber {
6161
return Ok(());
6262
};
6363

64-
let load_cache = true;
65-
let response = http::get_client(context, load_cache)
66-
.await?
67-
.post("https://notifications.delta.chat/register")
68-
.body(format!("{{\"token\":\"{token}\"}}"))
69-
.send()
70-
.await?;
71-
72-
let response_status = response.status();
73-
if response_status.is_success() {
64+
if http::post_string(
65+
context,
66+
"https://notifications.delta.chat/register",
67+
format!("{{\"token\":\"{token}\"}}"),
68+
)
69+
.await?
70+
{
7471
state.heartbeat_subscribed = true;
7572
}
7673
Ok(())

0 commit comments

Comments
 (0)