1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use crate::error::Error;
use async_trait::async_trait;
use awc::Client;
use bytes::Bytes;
use futures::{FutureExt, Stream, StreamExt, TryFutureExt, TryStreamExt};
use http::{
header::{HeaderName, HeaderValue},
uri::Scheme,
StatusCode, Uri,
};
use ipfs_api_prelude::{ApiRequest, Backend, TryFromUri};
use multipart::client::multipart;
use serde::Serialize;
use std::time::Duration;
const ACTIX_REQUEST_TIMEOUT: Duration = Duration::from_secs(90);
pub struct ActixBackend {
base: Uri,
client: Client,
}
impl Default for ActixBackend {
fn default() -> Self {
Self::from_ipfs_config()
.unwrap_or_else(|| Self::from_host_and_port(Scheme::HTTP, "localhost", 5001).unwrap())
}
}
impl TryFromUri for ActixBackend {
fn build_with_base_uri(base: Uri) -> Self {
let client = Client::default();
ActixBackend { base, client }
}
}
#[async_trait(?Send)]
impl Backend for ActixBackend {
type HttpRequest = awc::SendClientRequest;
type HttpResponse = awc::ClientResponse<
actix_http::encoding::Decoder<actix_http::Payload<actix_http::PayloadStream>>,
>;
type Error = Error;
fn build_base_request<Req>(
&self,
req: &Req,
form: Option<multipart::Form<'static>>,
) -> Result<Self::HttpRequest, Error>
where
Req: ApiRequest,
{
let url = req.absolute_url(&self.base)?;
let req = self.client.request(Req::METHOD, url);
let req = if let Some(form) = form {
req.content_type(form.content_type())
.send_body(multipart::Body::from(form))
} else {
req.timeout(ACTIX_REQUEST_TIMEOUT).send()
};
Ok(req)
}
fn get_header(res: &Self::HttpResponse, key: HeaderName) -> Option<&HeaderValue> {
res.headers().get(key)
}
async fn request_raw<Req>(
&self,
req: Req,
form: Option<multipart::Form<'static>>,
) -> Result<(StatusCode, Bytes), Self::Error>
where
Req: ApiRequest + Serialize,
{
let req = self.build_base_request(&req, form)?;
let mut res = req.await?;
let status = res.status();
let body = res.body().await?;
Ok((status, body))
}
fn response_to_byte_stream(
res: Self::HttpResponse,
) -> Box<dyn Stream<Item = Result<Bytes, Self::Error>> + Unpin> {
let stream = res.err_into();
Box::new(stream)
}
fn request_stream<Res, F, OutStream>(
&self,
req: Self::HttpRequest,
process: F,
) -> Box<dyn Stream<Item = Result<Res, Self::Error>> + Unpin>
where
OutStream: Stream<Item = Result<Res, Self::Error>> + Unpin,
F: 'static + Fn(Self::HttpResponse) -> OutStream,
{
let stream = req
.err_into()
.map_ok(move |mut res| {
match res.status() {
StatusCode::OK => process(res).right_stream(),
_ => res
.body()
.map(|maybe_body| match maybe_body {
Ok(body) => Err(Self::process_error_from_body(body)),
Err(e) => Err(e.into()),
})
.into_stream()
.left_stream(),
}
})
.try_flatten_stream();
Box::new(stream)
}
}