Skip to content

Commit

Permalink
extract post_request
Browse files Browse the repository at this point in the history
  • Loading branch information
spacebear21 committed Nov 7, 2024
1 parent 6ba1e53 commit f6247ff
Showing 1 changed file with 15 additions and 40 deletions.
55 changes: 15 additions & 40 deletions payjoin-cli/src/app/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,7 @@ impl App {
.extract_v2_req()
.map_err(|e| anyhow!("v2 req extraction failed {}", e))?;
println!("Got a request from the sender. Responding with a Payjoin proposal.");
let http = http_agent()?;
let res = http
.post(req.url)
.header("Content-Type", req.content_type)
.body(req.body)
.send()
.await
.map_err(map_reqwest_err)?;
let res = post_request(req).await?;
payjoin_proposal
.process_res(res.bytes().await?.to_vec(), ohttp_ctx)
.map_err(|e| anyhow!("Failed to deserialize response {}", e))?;
Expand Down Expand Up @@ -200,27 +193,14 @@ impl App {
match req_ctx.extract_v2(self.config.ohttp_relay.clone()) {
Ok((req, ctx)) => {
println!("Posting Original PSBT Payload request...");
let http = http_agent()?;
let response = http
.post(req.url)
.header("Content-Type", req.content_type)
.body(req.body)
.send()
.await
.map_err(map_reqwest_err)?;
let response = post_request(req).await?;
println!("Sent fallback transaction");
let v2_ctx = Arc::new(
ctx.process_response(&mut response.bytes().await?.to_vec().as_slice())?,
);
loop {
let (req, ohttp_ctx) = v2_ctx.extract_req(self.config.ohttp_relay.clone())?;
let response = http
.post(req.url)
.header("Content-Type", req.content_type)
.body(req.body)
.send()
.await
.map_err(map_reqwest_err)?;
let response = post_request(req).await?;
match v2_ctx.process_response(
&mut response.bytes().await?.to_vec().as_slice(),
ohttp_ctx,
Expand All @@ -241,14 +221,7 @@ impl App {
Err(_) => {
let (req, v1_ctx) = req_ctx.extract_v1()?;
println!("Posting Original PSBT Payload request...");
let http = http_agent()?;
let response = http
.post(req.url)
.header("Content-Type", req.content_type)
.body(req.body)
.send()
.await
.map_err(map_reqwest_err)?;
let response = post_request(req).await?;
println!("Sent fallback transaction");
match v1_ctx.process_response(&mut response.bytes().await?.to_vec().as_slice()) {
Ok(psbt) => Ok(psbt),
Expand All @@ -269,15 +242,7 @@ impl App {
loop {
let (req, context) = session.extract_req()?;
println!("Polling receive request...");
let http = http_agent()?;
let ohttp_response = http
.post(req.url)
.header("Content-Type", req.content_type)
.body(req.body)
.send()
.await
.map_err(map_reqwest_err)?;

let ohttp_response = post_request(req).await?;
let proposal = session
.process_res(ohttp_response.bytes().await?.to_vec().as_slice(), context)
.map_err(|_| anyhow!("GET fallback failed"))?;
Expand Down Expand Up @@ -417,6 +382,16 @@ async fn handle_interrupt(tx: watch::Sender<()>) {
let _ = tx.send(());
}

async fn post_request(req: payjoin::Request) -> Result<reqwest::Response> {
let http = http_agent()?;
http.post(req.url)
.header("Content-Type", req.content_type)
.body(req.body)
.send()
.await
.map_err(map_reqwest_err)
}

fn map_reqwest_err(e: reqwest::Error) -> anyhow::Error {
match e.status() {
Some(status_code) => anyhow!("HTTP request failed: {} {}", status_code, e),
Expand Down

0 comments on commit f6247ff

Please sign in to comment.