Extract Client::nonce() method

This commit is contained in:
Dirkjan Ochtman 2023-07-26 10:26:32 +02:00
parent 4a33aae615
commit c0b449dd52
1 changed files with 17 additions and 13 deletions

View File

@ -418,22 +418,11 @@ impl Client {
async fn post( async fn post(
&self, &self,
payload: Option<&impl Serialize>, payload: Option<&impl Serialize>,
mut nonce: Option<String>, nonce: Option<String>,
signer: &impl Signer, signer: &impl Signer,
url: &str, url: &str,
) -> Result<Response<Body>, Error> { ) -> Result<Response<Body>, Error> {
if nonce.is_none() { let nonce = self.nonce(nonce).await?;
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 body = JoseJson::new(payload, signer.header(Some(&nonce), url), signer)?; let body = JoseJson::new(payload, signer.header(Some(&nonce), url), signer)?;
let request = Request::builder() let request = Request::builder()
.method(Method::POST) .method(Method::POST)
@ -444,6 +433,21 @@ impl Client {
Ok(self.http.request(request).await?) Ok(self.http.request(request).await?)
} }
async fn nonce(&self, nonce: Option<String>) -> Result<String, Error> {
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 { impl fmt::Debug for Client {