use core::{error::Error, fmt, marker::PhantomData, time::Duration};
use alloc::{
boxed::Box,
format,
string::{String, ToString},
vec::Vec,
};
use io_http::{
coroutine::{HttpCoroutine, HttpCoroutineState},
rfc3986::path::{HttpUrlBuildError, http_url_build},
rfc6750::bearer::HttpAuthBearer,
rfc9110::{
request::HttpRequest,
response::HttpResponse,
send::{HttpSendOutput, HttpSendYield},
},
rfc9112::send::{Http11Send, Http11SendError},
};
use log::{debug, trace};
use serde::{Deserialize, Deserializer, Serialize, de::DeserializeOwned};
use url::Url;
use crate::coroutine::{MsgraphCoroutine, MsgraphCoroutineState, MsgraphYield};
pub const MSGRAPH_API_BASE: &str = "https://graph.microsoft.com/v1.0/";
pub fn user_path(user_id: &str) -> Result<String, MsgraphSendError> {
if user_id == "me" {
return Ok(String::from("me"));
}
Ok(format!("users/{}", path_segment(user_id)?))
}
pub fn path_segment(raw: &str) -> Result<String, MsgraphSendError> {
let url = match http_url_build(MSGRAPH_API_BASE, [raw]) {
Ok(url) => url,
Err(HttpUrlBuildError::InvalidBaseUrl(err, _)) => return Err(err.into()),
Err(HttpUrlBuildError::InvalidSegment(_)) => {
let reason = format!("{raw:?} is not a valid path segment");
return Err(MsgraphSendError::InvalidRequest(reason));
}
};
let (_, segment) = url.path().rsplit_once('/').unwrap_or_default();
Ok(segment.into())
}
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)]
pub struct MsgraphNoResponse;
impl<'de> Deserialize<'de> for MsgraphNoResponse {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let _ = serde::de::IgnoredAny::deserialize(deserializer)?;
Ok(Self)
}
}
#[derive(Debug)]
pub enum MsgraphSendError {
Send(Http11SendError),
SerializeRequest(serde_json::Error),
ParseResponse(serde_json::Error),
ParseUrl(url::ParseError),
InvalidRequest(String),
Api(Box<MsgraphApiError>),
UnexpectedRedirect,
}
impl fmt::Display for MsgraphSendError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Send(err) => write!(f, "Microsoft Graph HTTP request failed: {err}"),
Self::SerializeRequest(err) => {
write!(f, "Microsoft Graph request serialization failed: {err}")
}
Self::ParseResponse(err) => write!(f, "Microsoft Graph response parsing failed: {err}"),
Self::ParseUrl(err) => write!(f, "Microsoft Graph URL parsing failed: {err}"),
Self::InvalidRequest(reason) => write!(f, "Invalid Microsoft Graph request: {reason}"),
Self::Api(err) => err.fmt(f),
Self::UnexpectedRedirect => {
write!(f, "Microsoft Graph server returned an unexpected redirect")
}
}
}
}
impl Error for MsgraphSendError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Send(err) => Some(err),
Self::SerializeRequest(err) | Self::ParseResponse(err) => Some(err),
Self::ParseUrl(err) => Some(err),
_ => None,
}
}
}
impl From<Http11SendError> for MsgraphSendError {
fn from(err: Http11SendError) -> Self {
match err {
Http11SendError::InvalidRequest(err) => Self::InvalidRequest(err.to_string()),
err => Self::Send(err),
}
}
}
impl From<MsgraphApiError> for MsgraphSendError {
fn from(err: MsgraphApiError) -> Self {
Self::Api(Box::new(err))
}
}
impl From<url::ParseError> for MsgraphSendError {
fn from(err: url::ParseError) -> Self {
Self::ParseUrl(err)
}
}
impl MsgraphSendError {
pub fn api(&self) -> Option<&MsgraphApiError> {
match self {
Self::Api(err) => Some(err.as_ref()),
_ => None,
}
}
pub fn status(&self) -> Option<u16> {
self.api().map(|err| err.status)
}
pub fn retry_after(&self) -> Option<Duration> {
self.api().and_then(|err| err.retry_after)
}
pub fn is_throttled(&self) -> bool {
self.api().is_some_and(MsgraphApiError::is_throttled)
}
pub fn is_retryable(&self) -> bool {
self.api().is_some_and(MsgraphApiError::is_retryable)
}
}
#[derive(Debug, Clone, Default, Eq, PartialEq)]
pub struct MsgraphApiError {
pub status: u16,
pub code: String,
pub message: String,
pub method: String,
pub retry_after: Option<Duration>,
pub www_authenticate: Option<String>,
pub request_id: Option<String>,
pub keep_alive: bool,
}
impl MsgraphApiError {
pub fn parse<'a>(
status: u16,
headers: impl IntoIterator<Item = (&'a str, &'a str)>,
body: &[u8],
) -> Self {
let envelope = serde_json::from_slice::<ErrorEnvelope>(body).ok();
let (status, code, message) = parse_api_error(status, body);
let mut err = Self {
status,
code,
message,
request_id: envelope
.and_then(|envelope| envelope.error.inner_error)
.and_then(|inner| inner.request_id),
..Default::default()
};
for (name, value) in headers {
let value = value.trim();
if name.eq_ignore_ascii_case("retry-after") {
err.retry_after = value.parse().ok().map(Duration::from_secs);
} else if name.eq_ignore_ascii_case("www-authenticate") {
err.www_authenticate = Some(value.into());
} else if name.eq_ignore_ascii_case("request-id") {
err.request_id = Some(value.into());
}
}
err
}
pub(crate) fn of(method: &str, response: &HttpResponse, keep_alive: bool) -> Self {
let headers = response.headers.iter();
let headers = headers.map(|(name, value)| (name.as_str(), value.as_str()));
let mut err = Self::parse(*response.status, headers, &response.body);
err.method = method.into();
err.keep_alive = keep_alive;
err
}
pub fn is_throttled(&self) -> bool {
self.status == 429
}
pub fn is_retryable(&self) -> bool {
let transient = matches!(self.status, 500 | 502 | 503 | 504);
let idempotent = matches!(self.method.as_str(), "GET" | "HEAD" | "PUT" | "DELETE");
self.is_throttled() || (transient && idempotent)
}
}
impl fmt::Display for MsgraphApiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Self {
status,
code,
message,
..
} = self;
write!(
f,
"Microsoft Graph API returned HTTP {status} ({code}): {message}"
)
}
}
impl Error for MsgraphApiError {}
#[derive(Clone, Debug)]
pub struct MsgraphSendOutput<T> {
pub response: T,
pub headers: Vec<(String, String)>,
pub keep_alive: bool,
}
pub struct MsgraphSend<T> {
state: State,
method: String,
_phantom: PhantomData<T>,
}
impl<T: DeserializeOwned> MsgraphSend<T> {
pub fn get(auth: &HttpAuthBearer, url: Url) -> Self {
Self::with_method(auth, "GET", url, None, Vec::new())
}
pub fn delete(auth: &HttpAuthBearer, url: Url) -> Self {
Self::with_method(auth, "DELETE", url, None, Vec::new())
}
pub fn post_json<B: Serialize>(
auth: &HttpAuthBearer,
url: Url,
body: &B,
) -> Result<Self, MsgraphSendError> {
let body = serde_json::to_vec(body).map_err(MsgraphSendError::SerializeRequest)?;
Ok(Self::with_method(
auth,
"POST",
url,
Some("application/json"),
body,
))
}
pub fn patch_json<B: Serialize>(
auth: &HttpAuthBearer,
url: Url,
body: &B,
) -> Result<Self, MsgraphSendError> {
let body = serde_json::to_vec(body).map_err(MsgraphSendError::SerializeRequest)?;
Ok(Self::with_method(
auth,
"PATCH",
url,
Some("application/json"),
body,
))
}
pub fn post_text(auth: &HttpAuthBearer, url: Url, body: Vec<u8>) -> Self {
Self::with_method(auth, "POST", url, Some("text/plain"), body)
}
pub fn with_method(
auth: &HttpAuthBearer,
method: &str,
url: Url,
content_type: Option<&str>,
body: Vec<u8>,
) -> Self {
let mut request = HttpRequest::get(url.clone())
.header("Accept", "application/json")
.header("Authorization", auth.to_authorization())
.body(body);
if let Some(content_type) = content_type {
request = request.header("Content-Type", content_type);
}
request.method = method.into();
debug!("prepare request to send");
trace!("method: {method}");
trace!("url: {url}");
Self {
state: State::Prepare(request),
method: method.into(),
_phantom: PhantomData,
}
}
pub fn header(mut self, name: impl ToString, value: impl ToString) -> Self {
if let State::Prepare(request) = &mut self.state {
request.headers.push((name.to_string(), value.to_string()));
}
self
}
pub fn max_page_size(self, size: u32) -> Self {
self.header("Prefer", format!("odata.maxpagesize={size}"))
}
}
impl<T: DeserializeOwned> MsgraphCoroutine for MsgraphSend<T> {
type Yield = MsgraphYield;
type Return = Result<MsgraphSendOutput<T>, MsgraphSendError>;
fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
if let State::Prepare(request) = &mut self.state {
let url = request.url.clone();
let request = core::mem::replace(request, HttpRequest::get(url));
self.state = State::Send(Http11Send::new(request));
}
match &mut self.state {
State::Prepare(_) => unreachable!("the request is sent on the first resume"),
State::Send(send) => match send.resume(arg) {
HttpCoroutineState::Yielded(HttpSendYield::WantsRead) => {
MsgraphCoroutineState::Yielded(MsgraphYield::WantsRead)
}
HttpCoroutineState::Yielded(HttpSendYield::WantsWrite(bytes)) => {
MsgraphCoroutineState::Yielded(MsgraphYield::WantsWrite(bytes))
}
HttpCoroutineState::Yielded(HttpSendYield::WantsRedirect { .. }) => {
MsgraphCoroutineState::Complete(Err(MsgraphSendError::UnexpectedRedirect))
}
HttpCoroutineState::Complete(Err(err)) => {
MsgraphCoroutineState::Complete(Err(err.into()))
}
HttpCoroutineState::Complete(Ok(HttpSendOutput {
response,
keep_alive,
..
})) => {
if response.status.is_success() {
let body = if response.body.is_empty() {
b"null".as_slice()
} else {
response.body.as_slice()
};
match serde_json::from_slice::<T>(body) {
Ok(parsed) => MsgraphCoroutineState::Complete(Ok(MsgraphSendOutput {
response: parsed,
headers: response.headers,
keep_alive,
})),
Err(err) => MsgraphCoroutineState::Complete(Err(
MsgraphSendError::ParseResponse(err),
)),
}
} else {
let err = MsgraphApiError::of(&self.method, &response, keep_alive);
MsgraphCoroutineState::Complete(Err(err.into()))
}
}
},
}
}
}
enum State {
Prepare(HttpRequest),
Send(Http11Send),
}
#[derive(Debug, Deserialize)]
struct ErrorEnvelope {
error: ErrorBody,
}
#[derive(Debug, Deserialize)]
struct ErrorBody {
code: Option<String>,
message: Option<String>,
#[serde(rename = "innerError", alias = "innererror")]
inner_error: Option<InnerError>,
}
#[derive(Debug, Deserialize)]
struct InnerError {
#[serde(rename = "request-id")]
request_id: Option<String>,
}
pub fn parse_api_error(http_status: u16, body: &[u8]) -> (u16, String, String) {
if let Ok(envelope) = serde_json::from_slice::<ErrorEnvelope>(body) {
let code = envelope
.error
.code
.filter(|code| !code.trim().is_empty())
.unwrap_or_else(|| String::from("unknown"));
let message = envelope
.error
.message
.filter(|message| !message.trim().is_empty())
.unwrap_or_else(|| String::from("unknown Microsoft Graph API error"));
return (http_status, code, message);
}
let message = String::from_utf8_lossy(body).trim().to_string();
if message.is_empty() {
(
http_status,
String::from("unknown"),
String::from("unknown Microsoft Graph API error"),
)
} else {
(http_status, String::from("unknown"), message)
}
}
#[cfg(test)]
mod tests {
use crate::v1::send::{MsgraphSendError, path_segment, user_path};
#[test]
fn a_path_segment_keeps_a_graph_id_and_encodes_separators() {
let id = "AAMkAGI2T-_x=";
assert_eq!(path_segment(id).unwrap(), id);
assert_eq!(path_segment("A/B?c#d e%").unwrap(), "A%2FB%3Fc%23d%20e%25");
assert_eq!(path_segment("Envoyés").unwrap(), "Envoy%C3%A9s");
assert_eq!(
user_path("a b@example.org").unwrap(),
"users/a%20b@example.org"
);
assert_eq!(user_path("me").unwrap(), "me");
}
#[test]
fn a_path_segment_refuses_what_url_resolution_drops() {
for raw in ["", ".", "..", ".\t.", "a\nb"] {
let err = path_segment(raw).unwrap_err();
assert!(matches!(err, MsgraphSendError::InvalidRequest(_)));
let err = user_path(raw).unwrap_err();
assert!(matches!(err, MsgraphSendError::InvalidRequest(_)));
}
}
}