io-msgraph 0.9.4

Microsoft Graph API client library for Rust
Documentation
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! HTTP/JSON transport every Microsoft Graph coroutine delegates to:
//! builds the authorized request and parses the JSON response, or the
//! Graph error envelope on failure.
//!
//! Microsoft Graph reference:
//! <https://learn.microsoft.com/en-us/graph/api/overview>.

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};

/// Base URL of the Microsoft Graph API, version 1.0.
pub const MSGRAPH_API_BASE: &str = "https://graph.microsoft.com/v1.0/";

/// Base path segment addressing a mailbox owner: `me` as-is, any
/// other value as `users/{id}`.
///
/// Graph accepts an explicit user id or principal name under `users/`
/// but rejects `users/me`; only the bare `me` shortcut addresses the
/// authenticated user. An id [`path_segment`] refuses is refused here too.
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)?))
}

/// Percent-encodes `raw` as one URL path segment, such as a folder or
/// message id.
///
/// The segment is encoded by [`http_url_build`], so a `/`, `?`, `#` or
/// `%` stays inside it rather than naming another resource. An empty, `.`
/// or `..` segment is refused, as is one holding a tab or a line break:
/// URL resolution would drop it, climb a level or strip those bytes.
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())
}

/// Marker for endpoints that return an empty body (DELETE, sendMail,
/// send draft); deserialises from anything, including nothing.
#[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)
    }
}

/// Error returned by [`MsgraphSend`] and the raw `$value` coroutines.
#[derive(Debug)]
pub enum MsgraphSendError {
    /// The underlying HTTP/1.1 exchange failed.
    Send(Http11SendError),
    /// The JSON request body could not be serialized.
    SerializeRequest(serde_json::Error),
    /// The 2xx response body could not be deserialized.
    ParseResponse(serde_json::Error),
    /// The request URL could not be built.
    ParseUrl(url::ParseError),
    /// The request arguments were rejected before sending.
    InvalidRequest(String),
    /// The Graph API answered a non-2xx status.
    Api(Box<MsgraphApiError>),
    /// The server answered a 3xx; redirects are never followed.
    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 {
    /// The API error of a non-2xx answer, `None` for every other
    /// variant.
    pub fn api(&self) -> Option<&MsgraphApiError> {
        match self {
            Self::Api(err) => Some(err.as_ref()),
            _ => None,
        }
    }

    /// The HTTP status of an [`Api`](Self::Api) error, `None` for
    /// every other variant.
    pub fn status(&self) -> Option<u16> {
        self.api().map(|err| err.status)
    }

    /// How long the server asked to wait before a retry
    /// ([`MsgraphApiError::retry_after`]).
    pub fn retry_after(&self) -> Option<Duration> {
        self.api().and_then(|err| err.retry_after)
    }

    /// Whether Graph throttled the request
    /// ([`MsgraphApiError::is_throttled`]).
    pub fn is_throttled(&self) -> bool {
        self.api().is_some_and(MsgraphApiError::is_throttled)
    }

    /// Whether the request can be sent again as it is
    /// ([`MsgraphApiError::is_retryable`]).
    pub fn is_retryable(&self) -> bool {
        self.api().is_some_and(MsgraphApiError::is_retryable)
    }
}

/// A non-2xx answer of the Graph API: its error envelope, and what its
/// headers tell a caller to do next.
#[derive(Debug, Clone, Default, Eq, PartialEq)]
pub struct MsgraphApiError {
    /// The HTTP status of the response.
    pub status: u16,
    /// The error code of the Graph error envelope.
    pub code: String,
    /// The human-readable message of the Graph error envelope.
    pub message: String,
    /// The method of the refused request, uppercase.
    pub method: String,
    /// How long the server asked to wait before a retry: the
    /// `Retry-After` header in its seconds form, a date reading as none.
    pub retry_after: Option<Duration>,
    /// The `WWW-Authenticate` header, which carries the claims challenge
    /// of a 401 the next token request has to answer.
    pub www_authenticate: Option<String>,
    /// The id Graph gave the request, from the `request-id` header, else
    /// from the envelope.
    pub request_id: Option<String>,
    /// Whether the server allows reusing the TCP/TLS connection.
    pub keep_alive: bool,
}

impl MsgraphApiError {
    /// Reads a non-2xx answer: the error envelope of its body, falling
    /// back as [`parse_api_error`] does, and its headers.
    ///
    /// Neither tells the method of the request nor the connection reuse:
    /// the exchange fills those in.
    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
    }

    /// The API error of the non-2xx `response` to a `method` request.
    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
    }

    /// Whether Graph throttled the request (429): it was not taken, so
    /// sending it again after [`retry_after`](Self::retry_after) is safe
    /// whatever its method.
    pub fn is_throttled(&self) -> bool {
        self.status == 429
    }

    /// Whether the request can be sent again as it is: a throttled one,
    /// or a transient 5xx on a method a repeat cannot double.
    ///
    /// A POST or a PATCH answered by a 5xx may have been applied, so it
    /// is not retryable here: a mail could be sent twice.
    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 {}

/// Terminal value of every coroutine: the parsed response, its headers
/// and the connection reuse hint.
#[derive(Clone, Debug)]
pub struct MsgraphSendOutput<T> {
    /// The parsed response body.
    pub response: T,
    /// The response headers, in order, names lowercased.
    pub headers: Vec<(String, String)>,
    /// Whether the server allows reusing the TCP/TLS connection.
    pub keep_alive: bool,
}

/// I/O-free coroutine sending one authorized Microsoft Graph request
/// and parsing its JSON response into `T`.
pub struct MsgraphSend<T> {
    state: State,
    method: String,
    _phantom: PhantomData<T>,
}

impl<T: DeserializeOwned> MsgraphSend<T> {
    /// Builds a GET send for the given URL.
    pub fn get(auth: &HttpAuthBearer, url: Url) -> Self {
        Self::with_method(auth, "GET", url, None, Vec::new())
    }

    /// Builds a DELETE send for the given URL.
    pub fn delete(auth: &HttpAuthBearer, url: Url) -> Self {
        Self::with_method(auth, "DELETE", url, None, Vec::new())
    }

    /// Builds a POST send with a JSON body.
    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,
        ))
    }

    /// Builds a PATCH send with a 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,
        ))
    }

    /// Builds a POST send with a text/plain body.
    pub fn post_text(auth: &HttpAuthBearer, url: Url, body: Vec<u8>) -> Self {
        Self::with_method(auth, "POST", url, Some("text/plain"), body)
    }

    /// Builds a send with an arbitrary method, content type and 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,
        }
    }

    /// Adds a header to the request.
    ///
    /// Only effective before the first
    /// [`resume`](MsgraphCoroutine::resume), which serializes the
    /// request: a header added later is ignored.
    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
    }

    /// Asks Graph for at most `size` items per page of a paged
    /// collection (`Prefer: odata.maxpagesize={size}`).
    ///
    /// The preference rides on one request only: a caller following
    /// `@odata.nextLink` asks again on every page. Graph may serve
    /// fewer items than asked, and caps the size per endpoint.
    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 {
    /// The request, open to more headers until the first resume.
    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>,
}

/// Parse a Microsoft Graph error envelope into its
/// `(http_status, code, message)` triple.
///
/// The envelope is `{ "error": { "code", "message" } }`; when the body
/// is not that JSON shape the raw body text becomes the message, and
/// empty or missing parts fall back to `unknown` markers.
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(_)));
        }
    }
}