Skip to content

Commit ba7444b

Browse files
authored
Refactor new Request impls to use reference types in the signature (payjoin#524)
* Refactor new request construction args to be references We shouldn't need to pass ownership of the URL and body to the function creating a new request in v1 or v2. Using a simple reference type and clone is sufficient. * Refactor new_v1 and new_v2 request calls to match arg definitions The URL and body should now both be passed as refs.
1 parent 4c311ee commit ba7444b

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

payjoin/src/receive/v2/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl Receiver {
104104
let (body, ohttp_ctx) =
105105
self.fallback_req_body().map_err(InternalSessionError::OhttpEncapsulation)?;
106106
let url = ohttp_relay.clone();
107-
let req = Request::new_v2(url, body);
107+
let req = Request::new_v2(&url, &body);
108108
Ok((req, ohttp_ctx))
109109
}
110110

@@ -267,7 +267,7 @@ impl UncheckedProposal {
267267
)
268268
.map_err(InternalSessionError::OhttpEncapsulation)?;
269269

270-
let req = Request::new_v2(ohttp_relay.clone(), body);
270+
let req = Request::new_v2(ohttp_relay, &body);
271271
Ok((req, ohttp_ctx))
272272
}
273273

@@ -521,7 +521,7 @@ impl PayjoinProposal {
521521
target_resource.as_str(),
522522
Some(&body),
523523
)?;
524-
let req = Request::new_v2(ohttp_relay.clone(), body);
524+
let req = Request::new_v2(ohttp_relay, &body);
525525
Ok((req, ctx))
526526
}
527527

payjoin/src/request.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ pub struct Request {
2828

2929
impl Request {
3030
/// Construct a new v1 request.
31-
pub(crate) fn new_v1(url: Url, body: Vec<u8>) -> Self {
32-
Self { url, content_type: V1_REQ_CONTENT_TYPE, body }
31+
pub(crate) fn new_v1(url: &Url, body: &[u8]) -> Self {
32+
Self { url: url.clone(), content_type: V1_REQ_CONTENT_TYPE, body: body.to_vec() }
3333
}
3434

3535
/// Construct a new v2 request.
3636
#[cfg(feature = "v2")]
3737
pub(crate) fn new_v2(
38-
url: Url,
39-
body: [u8; crate::directory::ENCAPSULATED_MESSAGE_BYTES],
38+
url: &Url,
39+
body: &[u8; crate::directory::ENCAPSULATED_MESSAGE_BYTES],
4040
) -> Self {
41-
Self { url, content_type: V2_REQ_CONTENT_TYPE, body: body.to_vec() }
41+
Self { url: url.clone(), content_type: V2_REQ_CONTENT_TYPE, body: body.to_vec() }
4242
}
4343
}

payjoin/src/send/v1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl Sender {
237237
)?;
238238
let body = self.psbt.to_string().as_bytes().to_vec();
239239
Ok((
240-
Request::new_v1(url, body),
240+
Request::new_v1(&url, &body),
241241
V1Context {
242242
psbt_context: PsbtContext {
243243
original_psbt: self.psbt.clone(),

payjoin/src/send/v2/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl Sender {
168168
.map_err(InternalCreateRequestError::OhttpEncapsulation)?;
169169
log::debug!("ohttp_relay_url: {:?}", ohttp_relay);
170170
Ok((
171-
Request::new_v2(ohttp_relay, body),
171+
Request::new_v2(&ohttp_relay, &body),
172172
V2PostContext {
173173
endpoint: self.v1.endpoint.clone(),
174174
psbt_ctx: PsbtContext {
@@ -273,7 +273,7 @@ impl V2GetContext {
273273
let (body, ohttp_ctx) = ohttp_encapsulate(&mut ohttp, "GET", url.as_str(), Some(&body))
274274
.map_err(InternalCreateRequestError::OhttpEncapsulation)?;
275275

276-
Ok((Request::new_v2(ohttp_relay, body), ohttp_ctx))
276+
Ok((Request::new_v2(&ohttp_relay, &body), ohttp_ctx))
277277
}
278278

279279
pub fn process_response(

0 commit comments

Comments
 (0)