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
//! Configurable options which can be used to customize API behaviors or responses.
//!
//! # Note
//! Some requests do not support all of these parameters,
//! and using them will cause an error.
//!
//! Be careful and read the documentation of API from Microsoft before
//! applying any options.
//!
//! # See also
//! [Microsoft Docs](https://docs.microsoft.com/en-us/graph/query-parameters)
#![allow(clippy::module_name_repetitions)] // Ambiguous if without sufficies.
use crate::{
resource::{ResourceField, Tag},
util::RequestBuilderTransformer,
ConflictBehavior,
};
use reqwest::{header, RequestBuilder};
use std::{fmt::Write, marker::PhantomData};
#[derive(Debug, Default, Clone, PartialEq, Eq)]
struct AccessOption {
if_match: Option<String>,
if_none_match: Option<String>,
}
impl AccessOption {
fn if_match(mut self, tag: &Tag) -> Self {
self.if_match = Some(tag.0.clone());
self
}
fn if_none_match(mut self, tag: &Tag) -> Self {
self.if_none_match = Some(tag.0.clone());
self
}
}
impl RequestBuilderTransformer for AccessOption {
fn trans(self, mut req: RequestBuilder) -> RequestBuilder {
if let Some(v) = self.if_match {
req = req.header(header::IF_MATCH, v);
}
if let Some(v) = self.if_none_match {
req = req.header(header::IF_NONE_MATCH, v);
}
req
}
}
/// Option for GET-like requests to one resource object.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ObjectOption<Field> {
access_opt: AccessOption,
select_buf: String,
expand_buf: String,
_marker: PhantomData<dyn Fn(&Field) + Send + Sync>,
}
impl<Field: ResourceField> ObjectOption<Field> {
/// Create an empty (default) option.
#[must_use]
pub fn new() -> Self {
Self {
access_opt: AccessOption::default(),
select_buf: String::new(),
expand_buf: String::new(),
_marker: PhantomData,
}
}
/// Only response if the object matches the `tag`.
///
/// Will cause HTTP 412 Precondition Failed otherwise.
///
/// It is usually used for PUT-like requests to assert preconditions, but
/// most of GET-like requests also support it.
///
/// It will add `If-Match` to the request header.
#[must_use]
pub fn if_match(mut self, tag: &Tag) -> Self {
self.access_opt = self.access_opt.if_match(tag);
self
}
/// Only response if the object does not match the `tag`.
///
/// Will cause the relative API returns `None` otherwise.
///
/// It is usually used for GET-like requests to reduce data transmission if
/// cached data can be reused.
///
/// This will add `If-None-Match` to the request header.
#[must_use]
pub fn if_none_match(mut self, tag: &Tag) -> Self {
self.access_opt = self.access_opt.if_none_match(tag);
self
}
/// Select only some fields of the resource object.
///
/// See documentation of module [`onedrive_api::resource`][resource] for more details.
///
/// # Note
/// If called more than once, all fields mentioned will be selected.
///
/// # See also
/// [Microsoft Docs](https://docs.microsoft.com/en-us/graph/query-parameters#select-parameter)
///
/// [resource]: ../resource/index.html#field-descriptors
#[must_use]
pub fn select(mut self, fields: &[Field]) -> Self {
for sel in fields {
self = self.select_raw(&[sel.__raw_name()]);
}
self
}
fn select_raw(mut self, fields: &[&str]) -> Self {
for sel in fields {
write!(self.select_buf, ",{sel}").unwrap();
}
self
}
/// Expand a field of the resource object.
///
/// See documentation of module [`onedrive_api::resource`][resource] for more details.
///
/// # Note
/// If called more than once, all fields mentioned will be expanded.
/// `select_children` should be raw camelCase field names mentioned in Microsoft Docs below.
///
/// # See also
/// [Microsoft Docs](https://docs.microsoft.com/en-us/graph/query-parameters#expand-parameter)
///
/// [resource]: ../resource/index.html#field-descriptors
#[must_use]
pub fn expand(self, field: Field, select_children: Option<&[&str]>) -> Self {
self.expand_raw(field.__raw_name(), select_children)
}
fn expand_raw(mut self, field: &str, select_children: Option<&[&str]>) -> Self {
let buf = &mut self.expand_buf;
write!(buf, ",{field}").unwrap();
if let Some(children) = select_children {
write!(buf, "($select=").unwrap();
for sel in children {
write!(buf, "{sel},").unwrap();
}
write!(buf, ")").unwrap();
}
self
}
}
impl<Field: ResourceField> RequestBuilderTransformer for ObjectOption<Field> {
fn trans(self, mut req: RequestBuilder) -> RequestBuilder {
req = self.access_opt.trans(req);
if let Some(s) = self.select_buf.get(1..) {
req = req.query(&[("$select", s)]);
}
if let Some(s) = self.expand_buf.get(1..) {
req = req.query(&[("$expand", s)]);
}
req
}
}
impl<Field: ResourceField> Default for ObjectOption<Field> {
fn default() -> Self {
Self::new()
}
}
/// Option for GET-like requests for a collection of resource objects.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CollectionOption<Field> {
obj_option: ObjectOption<Field>,
order_buf: Option<String>,
page_size_buf: Option<String>,
get_count_buf: bool,
}
impl<Field: ResourceField> CollectionOption<Field> {
/// Create an empty (default) option.
#[must_use]
pub fn new() -> Self {
Self {
obj_option: ObjectOption::default(),
order_buf: None,
page_size_buf: None,
get_count_buf: false,
}
}
/// Only response if the object matches the `tag`.
///
/// # See also
/// [`ObjectOption::if_match`][if_match]
///
/// [if_match]: ./struct.ObjectOption.html#method.if_match
#[must_use]
pub fn if_match(mut self, tag: &Tag) -> Self {
self.obj_option = self.obj_option.if_match(tag);
self
}
/// Only response if the object does not match the `tag`.
///
/// # See also
/// [`ObjectOption::if_none_match`][if_none_match]
///
/// [if_none_match]: ./struct.ObjectOption.html#method.if_none_match
#[must_use]
pub fn if_none_match(mut self, tag: &Tag) -> Self {
self.obj_option = self.obj_option.if_none_match(tag);
self
}
/// Select only some fields of the resource object.
///
/// See documentation of module [`onedrive_api::resource`][resource] for more details.
///
/// # See also
/// [`ObjectOption::select`][select]
///
/// [select]: ./struct.ObjectOption.html#method.select
/// [resource]: ../resource/index.html#field-descriptors
#[must_use]
pub fn select(mut self, fields: &[Field]) -> Self {
self.obj_option = self.obj_option.select(fields);
self
}
/// Expand a field of the resource object.
///
/// See documentation of module [`onedrive_api::resource`][resource] for more details.
///
/// # See also
/// [`ObjectOption::expand`][expand]
///
/// [expand]: ./struct.ObjectOption.html#method.expand
/// [resource]: ../resource/index.html#field-descriptors
#[must_use]
pub fn expand(mut self, field: Field, select_children: Option<&[&str]>) -> Self {
self.obj_option = self.obj_option.expand(field, select_children);
self
}
/// Specify the sort order of the items responsed.
///
/// # Note
/// If called more than once, only the last call make sense.
///
/// # See also
/// [Microsoft Docs](https://docs.microsoft.com/en-us/graph/query-parameters#orderby-parameter)
#[must_use]
pub fn order_by(mut self, field: Field, order: Order) -> Self {
let order = match order {
Order::Ascending => "asc",
Order::Descending => "desc",
};
self.order_buf = Some(format!("{} {}", field.__raw_name(), order));
self
}
/// Specify the number of items per page.
///
/// # Note
/// If called more than once, only the last call make sense.
///
/// # See also
/// [Microsoft Docs](https://docs.microsoft.com/en-us/graph/query-parameters#top-parameter)
#[must_use]
pub fn page_size(mut self, size: usize) -> Self {
self.page_size_buf = Some(size.to_string());
self
}
/// Specify to get the number of all items.
///
/// # Note
/// If called more than once, only the last call make sense.
///
/// Note that Track Changes API does not support this. Setting it in like
/// [`track_changes_from_initial_with_option`][track_init_opt] will cause a panic.
///
/// # See also
/// [Microsoft Docs](https://docs.microsoft.com/en-us/graph/query-parameters#count-parameter)
///
/// [track_init_opt]: ../struct.OneDrive.html#method.track_changes_from_initial_with_option
#[must_use]
pub fn get_count(mut self, get_count: bool) -> Self {
self.get_count_buf = get_count;
self
}
pub(crate) fn has_get_count(&self) -> bool {
self.get_count_buf
}
}
impl<Field: ResourceField> RequestBuilderTransformer for CollectionOption<Field> {
fn trans(self, mut req: RequestBuilder) -> RequestBuilder {
req = self.obj_option.trans(req);
if let Some(s) = &self.order_buf {
req = req.query(&[("$orderby", s)]);
}
if let Some(s) = &self.page_size_buf {
req = req.query(&[("$top", s)]);
}
if self.get_count_buf {
req = req.query(&[("$count", "true")]);
}
req
}
}
impl<Field: ResourceField> Default for CollectionOption<Field> {
fn default() -> Self {
Self::new()
}
}
/// Specify the sorting order.
///
/// Used in [`CollectionOption::order_by`][order_by].
///
/// [order_by]: ./struct.CollectionOption.html#method.order_by
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Order {
/// Ascending order.
Ascending,
/// Descending order.
Descending,
}
/// Option for PUT-like requests of `DriveItem`.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct DriveItemPutOption {
access_opt: AccessOption,
conflict_behavior: Option<ConflictBehavior>,
}
impl DriveItemPutOption {
/// Create an empty (default) option.
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// Only response if the object matches the `tag`.
///
/// # See also
/// [`ObjectOption::if_match`][if_match]
///
/// [if_match]: ./struct.ObjectOption.html#method.if_match
#[must_use]
pub fn if_match(mut self, tag: &Tag) -> Self {
self.access_opt = self.access_opt.if_match(tag);
self
}
// `if_none_match` is not supported in PUT-like requests.
/// Specify the behavior if the target item already exists.
///
/// # Note
/// This not only available for DELETE-like requests. Read the docs first.
///
/// # See also
/// `@microsoft.graph.conflictBehavior` of `DriveItem` on [Microsoft Docs](https://docs.microsoft.com/en-us/graph/api/resources/driveitem?view=graph-rest-1.0#instance-attributes)
#[must_use]
pub fn conflict_behavior(mut self, conflict_behavior: ConflictBehavior) -> Self {
self.conflict_behavior = Some(conflict_behavior);
self
}
pub(crate) fn get_conflict_behavior(&self) -> Option<ConflictBehavior> {
self.conflict_behavior
}
}
impl RequestBuilderTransformer for DriveItemPutOption {
fn trans(self, req: RequestBuilder) -> RequestBuilder {
self.access_opt.trans(req)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::resource;
fn _assert_send_sync<T: Send + Sync>() {}
fn _assert_object_option_is_send_sync() {
_assert_send_sync::<ObjectOption<resource::DriveField>>();
_assert_send_sync::<ObjectOption<resource::DriveItemField>>();
}
fn _assert_collection_option_is_send_sync() {
_assert_send_sync::<CollectionOption<resource::DriveField>>();
_assert_send_sync::<CollectionOption<resource::DriveItemField>>();
}
fn _assert_drive_item_put_option_is_send_sync() {
_assert_send_sync::<DriveItemPutOption>();
}
}