payjoin/core/request.rs
1use url::Url;
2
3const V1_REQ_CONTENT_TYPE: &str = "text/plain";
4
5#[cfg(feature = "v2")]
6const V2_REQ_CONTENT_TYPE: &str = "message/ohttp-req";
7
8/// Represents data that needs to be transmitted to the receiver or payjoin directory.
9/// Ensure the `Content-Length` is set to the length of `body`. (most libraries do this automatically)
10#[non_exhaustive]
11#[derive(Debug, Clone)]
12pub struct Request {
13 /// URL to send the request to.
14 ///
15 /// This is full URL with scheme etc - you can pass it right to `reqwest` or a similar library.
16 pub url: Url,
17
18 /// The `Content-Type` header to use for the request.
19 ///
20 /// `text/plain` for v1 requests and `message/ohttp-req` for v2 requests.
21 pub content_type: &'static str,
22
23 /// Bytes to be sent to the receiver.
24 ///
25 /// This is properly encoded PSBT payload either in base64 in v1 or an OHTTP encapsulated payload in v2.
26 pub body: Vec<u8>,
27}
28
29impl Request {
30 /// Construct a new v1 request.
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() }
33 }
34
35 /// Construct a new v2 request.
36 #[cfg(feature = "v2")]
37 pub(crate) fn new_v2(
38 url: &Url,
39 body: &[u8; crate::directory::ENCAPSULATED_MESSAGE_BYTES],
40 ) -> Self {
41 Self { url: url.clone(), content_type: V2_REQ_CONTENT_TYPE, body: body.to_vec() }
42 }
43}