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
use crate::traits::*;
use crate::url::GraphUrl;
use async_trait::async_trait;
use graph_error::WithGraphError;
use graph_error::WithGraphErrorAsync;
use graph_error::{GraphFailure, GraphResult};
use reqwest::{header::HeaderMap, StatusCode};
use serde::de::DeserializeOwned;
use std::convert::TryFrom;
#[derive(Debug)]
pub struct GraphResponse<T> {
url: GraphUrl,
body: T,
status: StatusCode,
headers: HeaderMap,
}
impl<T> GraphResponse<T> {
pub fn new(url: GraphUrl, body: T, status: StatusCode, headers: 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) -> StatusCode {
self.status
}
pub fn headers(&self) -> &HeaderMap {
&self.headers
}
pub fn async_job_status(&mut self) -> Option<GraphResult<serde_json::Value>> {
self.headers
.get(reqwest::header::LOCATION)?
.to_str()
.ok()
.map(|location| {
futures::executor::block_on(async {
Ok(reqwest::Client::new()
.get(location)
.send()
.await?
.with_graph_error()
.await?
.json()
.await?)
})
})
}
pub(crate) fn from_no_content(
response: reqwest::blocking::Response,
) -> GraphResult<GraphResponse<serde_json::Value>> {
let response = response.with_graph_error()?;
let url = GraphUrl::from(response.url());
let status = response.status();
let headers = response.headers().to_owned();
response
.text()
.map(|s| serde_json::from_str(s.as_str()).unwrap_or(serde_json::Value::String(s)))
.or_else(|_| Ok(serde_json::Value::String(String::new())))
.map(|body| GraphResponse::new(url, body, status, headers))
}
pub(crate) async fn async_from_no_content(
response: reqwest::Response,
) -> GraphResult<GraphResponse<serde_json::Value>> {
let response = response.with_graph_error().await?;
let url = GraphUrl::from(response.url());
let status = response.status();
let headers = response.headers().to_owned();
response
.text()
.await
.map(|s| serde_json::from_str(s.as_str()).unwrap_or(serde_json::Value::String(s)))
.or_else(|_| Ok(serde_json::Value::String(String::new())))
.map(|body| GraphResponse::new(url, body, status, headers))
}
}
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: DeserializeOwned> TryFrom<reqwest::blocking::Response> for GraphResponse<T> {
type Error = GraphFailure;
fn try_from(response: reqwest::blocking::Response) -> GraphResult<GraphResponse<T>> {
let response = response.with_graph_error()?;
let url = GraphUrl::from(response.url());
let status = response.status();
let headers = response.headers().to_owned();
Ok(GraphResponse::new(url, response.json()?, status, headers))
}
}
impl<T: DeserializeOwned> TryFrom<GraphResult<reqwest::blocking::Response>> for GraphResponse<T> {
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: DeserializeOwned> AsyncTryFrom<reqwest::Response> for GraphResponse<T> {
type Error = GraphFailure;
async fn async_try_from(response: reqwest::Response) -> Result<Self, Self::Error> {
let response = response.with_graph_error().await?;
let url = GraphUrl::from(response.url());
let status = response.status();
let headers = response.headers().to_owned();
Ok(GraphResponse::new(
url,
response.json().await?,
status,
headers,
))
}
}
#[async_trait]
impl<T: DeserializeOwned> AsyncTryFrom<GraphResult<reqwest::Response>> for GraphResponse<T> {
type Error = GraphFailure;
async fn async_try_from(result: GraphResult<reqwest::Response>) -> Result<Self, Self::Error> {
AsyncTryFrom::<reqwest::Response>::async_try_from(result?).await
}
}