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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use crate::traits::*;
use crate::types::*;
use crate::url::GraphUrl;
use async_trait::async_trait;
use graph_error::{ErrorMessage, GraphError, GraphFailure, GraphResult};
use reqwest::Response;
use std::convert::TryFrom;
#[derive(Debug)]
pub struct GraphResponse<T> {
url: GraphUrl,
body: T,
status: u16,
headers: reqwest::header::HeaderMap,
}
impl<T> GraphResponse<T> {
pub fn new(
url: GraphUrl,
body: T,
status: u16,
headers: reqwest::header::HeaderMap,
) -> GraphResponse<T> {
GraphResponse {
url,
body,
status,
headers,
}
}
pub fn url(&self) -> &GraphUrl {
&self.url
}
pub fn body(&self) -> &T {
&self.body
}
pub fn into_body(self) -> T {
self.body
}
pub fn status(&self) -> u16 {
self.status
}
pub fn headers(&self) -> &reqwest::header::HeaderMap {
&self.headers
}
async fn inner_async_job_status(&mut self) -> Option<GraphResult<serde_json::Value>> {
let location: &reqwest::header::HeaderValue =
self.headers.get(reqwest::header::LOCATION)?;
let location_str = location.to_str().ok()?;
let response = reqwest::Client::new()
.get(location_str)
.send()
.await
.map_err(GraphFailure::from);
if let Ok(response) = response {
if let Some(err) = GraphFailure::from_async_response(&response) {
return Some(Err(err));
}
Some(response.json().await.map_err(GraphFailure::from))
} else if let Err(e) = response {
Some(Err(e))
} else {
None
}
}
pub fn async_job_status(&mut self) -> Option<GraphResult<serde_json::Value>> {
futures::executor::block_on(self.inner_async_job_status())
}
}
impl<T> AsRef<T> for GraphResponse<T> {
fn as_ref(&self) -> &T {
&self.body
}
}
impl<T> AsMut<T> for GraphResponse<T> {
fn as_mut(&mut self) -> &mut T {
&mut self.body
}
}
impl<T> TryFrom<reqwest::blocking::Response> for GraphResponse<T>
where
for<'de> T: serde::Deserialize<'de>,
{
type Error = GraphFailure;
fn try_from(response: reqwest::blocking::Response) -> GraphResult<GraphResponse<T>> {
if let Ok(mut error) = GraphError::try_from(&response) {
let error_message: GraphResult<ErrorMessage> =
response.json().map_err(GraphFailure::from);
if let Ok(message) = error_message {
error.set_error_message(message);
}
Err(GraphFailure::from(error))
} else {
let url = GraphUrl::from(response.url());
let status = response.status().as_u16();
let headers = response.headers().to_owned();
let body: T = response.json()?;
Ok(GraphResponse::new(url, body, status, headers))
}
}
}
impl TryFrom<reqwest::blocking::Response> for GraphResponse<Content> {
type Error = GraphFailure;
fn try_from(response: reqwest::blocking::Response) -> Result<Self, Self::Error> {
if let Ok(mut error) = GraphError::try_from(&response) {
let error_message: GraphResult<ErrorMessage> =
response.json().map_err(GraphFailure::from);
if let Ok(message) = error_message {
error.set_error_message(message);
}
return Err(GraphFailure::from(error));
}
let url = GraphUrl::from(response.url());
let headers = response.headers().to_owned();
let status = response.status().as_u16();
if let Ok(content) = response.text() {
Ok(GraphResponse::new(
url,
Content::from(content),
status,
headers,
))
} else {
Ok(GraphResponse::new(
url,
Content::from(String::new()),
status,
headers,
))
}
}
}
impl<T> TryFrom<GraphResult<reqwest::blocking::Response>> for GraphResponse<T>
where
for<'de> T: serde::Deserialize<'de>,
{
type Error = GraphFailure;
fn try_from(result: GraphResult<reqwest::blocking::Response>) -> Result<Self, Self::Error> {
std::convert::TryFrom::try_from(result?)
}
}
impl TryFrom<GraphResult<reqwest::blocking::Response>> for GraphResponse<Content> {
type Error = GraphFailure;
fn try_from(result: GraphResult<reqwest::blocking::Response>) -> Result<Self, Self::Error> {
std::convert::TryFrom::try_from(result?)
}
}
#[async_trait]
impl<T> AsyncTryFrom<reqwest::Response> for GraphResponse<T>
where
for<'de> T: serde::Deserialize<'de>,
{
type Error = GraphFailure;
async fn async_try_from(response: reqwest::Response) -> Result<Self, Self::Error> {
if let Ok(mut error) = GraphError::try_from(&response) {
let error_message: GraphResult<ErrorMessage> =
response.json().await.map_err(GraphFailure::from);
if let Ok(message) = error_message {
error.set_error_message(message);
}
Err(GraphFailure::from(error))
} else {
let url = GraphUrl::from(response.url());
let status = response.status().as_u16();
let headers = response.headers().to_owned();
let body: T = response.json().await?;
Ok(GraphResponse::new(url, body, status, headers))
}
}
}
#[async_trait]
impl AsyncTryFrom<reqwest::Response> for GraphResponse<Content> {
type Error = GraphFailure;
async fn async_try_from(response: Response) -> Result<Self, Self::Error> {
if let Ok(mut error) = GraphError::try_from(&response) {
let error_message: GraphResult<ErrorMessage> =
response.json().await.map_err(GraphFailure::from);
if let Ok(message) = error_message {
error.set_error_message(message);
}
return Err(GraphFailure::from(error));
}
let url = GraphUrl::from(response.url());
let status = response.status().as_u16();
let headers = response.headers().clone();
if let Ok(content) = response.text().await {
Ok(GraphResponse::new(
url,
Content::from(content),
status,
headers,
))
} else {
Ok(GraphResponse::new(
url,
Content::from(String::new()),
status,
headers,
))
}
}
}
#[async_trait]
impl<T> AsyncTryFrom<GraphResult<reqwest::Response>> for GraphResponse<T>
where
for<'de> T: serde::Deserialize<'de>,
{
type Error = GraphFailure;
async fn async_try_from(result: GraphResult<reqwest::Response>) -> Result<Self, Self::Error> {
AsyncTryFrom::<reqwest::Response>::async_try_from(result?).await
}
}
#[async_trait]
impl AsyncTryFrom<GraphResult<reqwest::Response>> for GraphResponse<Content> {
type Error = GraphFailure;
async fn async_try_from(result: GraphResult<reqwest::Response>) -> Result<Self, Self::Error> {
AsyncTryFrom::<reqwest::Response>::async_try_from(result?).await
}
}