From 827c3baa6a2dd712e322e22bf1699e147343af96 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 10 May 2022 14:30:51 +0200 Subject: [PATCH] acme: move post() implementation into Client --- src/lib.rs | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b406237..646aaed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -103,16 +103,8 @@ impl Account { pub async fn create(account: &NewAccount<'_>, server_url: &str) -> Result { let client = Client::new(server_url).await?; let key = Key::generate()?; - let nonce = client.nonce().await?; - let header = key.header(&nonce, &client.urls.new_account); - let body = key.signed_json(Some(account), header)?; - let rsp = client - .client - .post(&client.urls.new_account) - .header(CONTENT_TYPE, JOSE_JSON) - .body(body) - .send() + .post(Some(account), None, &key, &client.urls.new_account) .await?; let account_url = rsp @@ -231,21 +223,7 @@ impl AccountInner { nonce: Option, url: &str, ) -> Result { - let nonce = match nonce { - Some(nonce) => nonce, - None => self.client.nonce().await?, - }; - - let header = self.header(&nonce, url); - let body = self.key.signed_json(payload, header)?; - Ok(self - .client - .client - .post(url) - .header(CONTENT_TYPE, JOSE_JSON) - .body(body) - .send() - .await?) + self.client.post(payload, nonce, self, url).await } } @@ -280,6 +258,27 @@ impl Client { }) } + async fn post( + &self, + payload: Option<&impl Serialize>, + nonce: Option, + signer: &impl Signer, + url: &str, + ) -> Result { + let nonce = match nonce { + Some(nonce) => nonce, + None => self.nonce().await?, + }; + + Ok(self + .client + .post(url) + .header(CONTENT_TYPE, JOSE_JSON) + .body(signer.signed_json(payload, &nonce, url)?) + .send() + .await?) + } + async fn nonce(&self) -> Result { let future = self.client.head(&self.urls.new_nonce).send(); match nonce_from_response(&future.await?) {