acme: move post() implementation into Client

This commit is contained in:
Dirkjan Ochtman 2022-05-10 14:30:51 +02:00 committed by Dirkjan Ochtman
parent 8604aa483e
commit 827c3baa6a
1 changed files with 23 additions and 24 deletions

View File

@ -103,16 +103,8 @@ impl Account {
pub async fn create(account: &NewAccount<'_>, server_url: &str) -> Result<Account, Error> { pub async fn create(account: &NewAccount<'_>, server_url: &str) -> Result<Account, Error> {
let client = Client::new(server_url).await?; let client = Client::new(server_url).await?;
let key = Key::generate()?; 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 let rsp = client
.client .post(Some(account), None, &key, &client.urls.new_account)
.post(&client.urls.new_account)
.header(CONTENT_TYPE, JOSE_JSON)
.body(body)
.send()
.await?; .await?;
let account_url = rsp let account_url = rsp
@ -231,21 +223,7 @@ impl AccountInner {
nonce: Option<String>, nonce: Option<String>,
url: &str, url: &str,
) -> Result<Response, Error> { ) -> Result<Response, Error> {
let nonce = match nonce { self.client.post(payload, nonce, self, url).await
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?)
} }
} }
@ -280,6 +258,27 @@ impl Client {
}) })
} }
async fn post(
&self,
payload: Option<&impl Serialize>,
nonce: Option<String>,
signer: &impl Signer,
url: &str,
) -> Result<Response, Error> {
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<String, Error> { async fn nonce(&self) -> Result<String, Error> {
let future = self.client.head(&self.urls.new_nonce).send(); let future = self.client.head(&self.urls.new_nonce).send();
match nonce_from_response(&future.await?) { match nonce_from_response(&future.await?) {