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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use crate::blocking::{Body, BodyWriter, Client, Response, ResponseBody};
use crate::raw::{self, Service};
use crate::{APPLICATION_JSON, APPLICATION_OCTET_STREAM};
use bytes::Bytes;
use conjure_error::Error;
use conjure_http::client::{Accept, RequestBody, VisitRequestBody, VisitResponse, WriteBody};
use conjure_http::{PathParams, QueryParams};
use conjure_serde::json;
use hyper::header::{HeaderValue, ACCEPT, CONTENT_TYPE};
use hyper::{HeaderMap, Method, StatusCode};
use serde::Serialize;
use std::error;
use std::io::Read;
impl<T, B> Client<T>
where
T: Service<http::Request<raw::RawBody>, Response = http::Response<B>> + 'static + Sync + Send,
T::Error: Into<Box<dyn error::Error + Sync + Send>>,
T::Future: Send,
B: http_body::Body<Data = Bytes> + 'static + Send,
B::Error: Into<Box<dyn error::Error + Sync + Send>>,
{
#[allow(clippy::too_many_arguments)]
fn conjure_inner(
&self,
method: Method,
path: &'static str,
path_params: PathParams,
query_params: QueryParams,
headers: HeaderMap,
body: Option<RawBody<'_>>,
accept: Accept,
) -> Result<Response<B>, Error> {
let mut request = self.request(method, path);
for (key, value) in &path_params {
request = request.param(key, value);
}
for (key, values) in &query_params {
for value in values {
request = request.param(key, value);
}
}
for (key, value) in &headers {
request.headers_mut().insert(key.clone(), value.clone());
}
if let Some(body) = body {
request = request.body(body);
}
match accept {
Accept::Empty | Accept::Serializable => {
request
.headers_mut()
.insert(ACCEPT, APPLICATION_JSON.clone());
}
Accept::Binary => {
request
.headers_mut()
.insert(ACCEPT, APPLICATION_OCTET_STREAM.clone());
}
}
request.send()
}
}
impl<T, B> conjure_http::client::Client for Client<T>
where
T: Service<http::Request<raw::RawBody>, Response = http::Response<B>> + 'static + Sync + Send,
T::Error: Into<Box<dyn error::Error + Sync + Send>>,
T::Future: Send,
B: http_body::Body<Data = Bytes> + 'static + Send,
B::Error: Into<Box<dyn error::Error + Sync + Send>>,
{
type BinaryWriter = BodyWriter;
type BinaryBody = ResponseBody<B>;
fn request<'a, R, U>(
&self,
method: Method,
path: &'static str,
path_params: PathParams,
query_params: QueryParams,
headers: HeaderMap<HeaderValue>,
body: R,
response_visitor: U,
) -> Result<<U as VisitResponse<Self::BinaryBody>>::Output, Error>
where
R: RequestBody<'a, Self::BinaryWriter>,
U: VisitResponse<Self::BinaryBody>,
{
let body = body.accept(RawBodyVisitor)?;
let accept = response_visitor.accept();
let response = self.conjure_inner(
method,
path,
path_params,
query_params,
headers,
body,
accept,
)?;
if response.status() == StatusCode::NO_CONTENT {
return response_visitor.visit_empty();
}
if let Some(header) = response.headers().get(CONTENT_TYPE) {
if header == APPLICATION_JSON.as_ref() {
let mut body = vec![];
response
.into_body()
.read_to_end(&mut body)
.map_err(Error::internal_safe)?;
let mut deserializer = json::ClientDeserializer::from_slice(&body);
let r = response_visitor.visit_serializable(&mut deserializer)?;
deserializer.end().map_err(Error::internal_safe)?;
return Ok(r);
} else if header == APPLICATION_OCTET_STREAM.as_ref() {
let body = response.into_body();
return response_visitor.visit_binary(body);
}
}
Err(Error::internal_safe("invalid response Content-Type"))
}
}
enum RawBody<'a> {
Json(Bytes),
Binary(Box<dyn WriteBody<BodyWriter> + 'a>),
}
impl<'a> Body for RawBody<'a> {
fn content_length(&self) -> Option<u64> {
match self {
RawBody::Json(buf) => Some(buf.len() as u64),
RawBody::Binary(_) => None,
}
}
fn content_type(&self) -> HeaderValue {
match self {
RawBody::Json(_) => APPLICATION_JSON.clone(),
RawBody::Binary(_) => APPLICATION_OCTET_STREAM.clone(),
}
}
fn full_body(&self) -> Option<Bytes> {
match self {
RawBody::Json(buf) => Some(buf.clone()),
RawBody::Binary(_) => None,
}
}
fn write(&mut self, w: &mut BodyWriter) -> Result<(), Error> {
match self {
RawBody::Json(_) => unreachable!(),
RawBody::Binary(body) => body.write_body(w),
}
}
fn reset(&mut self) -> bool {
match self {
RawBody::Json(_) => true,
RawBody::Binary(body) => body.reset(),
}
}
}
struct RawBodyVisitor;
impl<'a> VisitRequestBody<'a, BodyWriter> for RawBodyVisitor {
type Output = Result<Option<RawBody<'a>>, Error>;
fn visit_empty(self) -> Result<Option<RawBody<'a>>, Error> {
Ok(None)
}
fn visit_serializable<T>(self, body: T) -> Result<Option<RawBody<'a>>, Error>
where
T: Serialize + 'a,
{
let body = json::to_vec(&body).map_err(Error::internal)?;
Ok(Some(RawBody::Json(Bytes::from(body))))
}
fn visit_binary<T>(self, body: T) -> Result<Option<RawBody<'a>>, Error>
where
T: WriteBody<BodyWriter> + 'a,
{
Ok(Some(RawBody::Binary(Box::new(body))))
}
}