From 5be34c546cf2b20a6742b6faa07f3267f0cc0a8c Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 10 May 2022 15:08:45 +0200 Subject: [PATCH] acme: inline nonce handling --- src/lib.rs | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 368ddba..d1d613d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -259,15 +259,22 @@ impl Client { async fn post( &self, payload: Option<&impl Serialize>, - nonce: Option, + mut nonce: Option, signer: &impl Signer, url: &str, ) -> Result, Error> { - let nonce = match nonce { - Some(nonce) => nonce, - None => self.nonce().await?, + if nonce.is_none() { + let request = Request::builder() + .method(Method::HEAD) + .uri(&self.urls.new_nonce) + .body(Body::empty()) + .unwrap(); + + let rsp = self.client.request(request).await?; + nonce = nonce_from_response(&rsp); }; + let nonce = nonce.ok_or("no nonce found")?; let request = Request::builder() .method(Method::POST) .uri(url) @@ -277,20 +284,6 @@ impl Client { Ok(self.client.request(request).await?) } - - async fn nonce(&self) -> Result { - let request = Request::builder() - .method(Method::HEAD) - .uri(&self.urls.new_nonce) - .body(Body::empty()) - .unwrap(); - - let rsp = self.client.request(request).await?; - match nonce_from_response(&rsp) { - Some(nonce) => Ok(nonce), - None => Err("no nonce found".into()), - } - } } struct Key {