From c0b449dd52b449c7c8a24c6cc1c330a5d258546b Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Wed, 26 Jul 2023 10:26:32 +0200 Subject: [PATCH] Extract Client::nonce() method --- src/lib.rs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5d0f426..1d41049 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -418,22 +418,11 @@ impl Client { async fn post( &self, payload: Option<&impl Serialize>, - mut nonce: Option, + nonce: Option, signer: &impl Signer, url: &str, ) -> Result, Error> { - if nonce.is_none() { - let request = Request::builder() - .method(Method::HEAD) - .uri(&self.urls.new_nonce) - .body(Body::empty()) - .unwrap(); - - let rsp = self.http.request(request).await?; - nonce = nonce_from_response(&rsp); - }; - - let nonce = nonce.ok_or("no nonce found")?; + let nonce = self.nonce(nonce).await?; let body = JoseJson::new(payload, signer.header(Some(&nonce), url), signer)?; let request = Request::builder() .method(Method::POST) @@ -444,6 +433,21 @@ impl Client { Ok(self.http.request(request).await?) } + + async fn nonce(&self, nonce: Option) -> Result { + if let Some(nonce) = nonce { + return Ok(nonce); + } + + let request = Request::builder() + .method(Method::HEAD) + .uri(&self.urls.new_nonce) + .body(Body::empty()) + .unwrap(); + + let rsp = self.http.request(request).await?; + Ok(nonce_from_response(&rsp).ok_or("no nonce found")?) + } } impl fmt::Debug for Client {